Files
owniverse/Stage/stage.gd
T

252 lines
7.8 KiB
GDScript
Raw Normal View History

2026-03-05 20:13:45 +03:00
extends Control
2026-03-30 00:48:20 +03:00
const ITEM_BUTTON = preload("uid://d3xjfxwe3w16v")
2026-04-05 20:15:07 +03:00
const SPEED_TEXTURE_NAME_TEMPLATE = "res://UI/TopMenu/Time&Speed/Time%s.png"
2026-03-09 16:10:35 +03:00
const PAGE_COUNT = 5
const PAGE_WIDTH = 1080
const DRAG_TOLERANCE = 360
const SWIPE_DURATION: float = 2
const SWIPE_DELAY = 2000 # msec
2026-04-05 20:15:07 +03:00
@onready var stage_page_scroller: ScrollContainer = $StageRows/PageScroller
@onready var item_pages: Array[Control] = [
$StageRows/PageScroller/HBoxContainer/ForcePage/Forces,
$StageRows/PageScroller/HBoxContainer/ResourcePage/HBoxContainer/Resources,
$StageRows/PageScroller/HBoxContainer/StarPage/Stars,
$StageRows/PageScroller/HBoxContainer/StructurePage/Structures,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Life,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/LivingPlanet,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Civ0,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Civ1,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Civ2,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Civ3,
$StageRows/PageScroller/HBoxContainer/LifePage/Planets/Civ4
]
2026-03-30 00:48:20 +03:00
@onready var g_entity_descriptions: Array = [
$StageRows/PageScroller/HBoxContainer/ForcePage/Description,
$StageRows/PageScroller/HBoxContainer/ResourcePage/Description,
$StageRows/PageScroller/HBoxContainer/StarPage/Description,
$StageRows/PageScroller/HBoxContainer/StructurePage/Description,
$StageRows/PageScroller/HBoxContainer/LifePage/Description
]
2026-04-05 20:15:07 +03:00
@onready var date_time: Label = $StageRows/TopMenu/InfoPanel/DateTime
@onready var g_speed_icons: Array = [
$StageRows/TopMenu/InfoPanel/Speed/Speed1,
$StageRows/TopMenu/InfoPanel/Speed/Speed2,
$StageRows/TopMenu/InfoPanel/Speed/Speed3,
$StageRows/TopMenu/InfoPanel/Speed/Speed4,
$StageRows/TopMenu/InfoPanel/Speed/Speed5,
$StageRows/TopMenu/InfoPanel/Speed/Speed6,
$StageRows/TopMenu/InfoPanel/Speed/Speed7,
$StageRows/TopMenu/InfoPanel/Speed/Speed8
]
@onready var g_time_minus: TextureButton = $StageRows/TopMenu/TimeMinus
@onready var g_time_plus: TextureButton = $StageRows/TopMenu/TimePlus
2026-03-30 00:48:20 +03:00
2026-03-09 16:10:35 +03:00
var g_tween: Tween = null
var g_dragged_start: Vector2 = Vector2.ZERO
var g_scroll_start: int = 0
var g_current_page: int = 0
var g_last_scroll_updated: int = 0
var f_check_swipe: bool = false
var g_swipe_time_delta: float = 0
var g_counters: Dictionary[String, Counter] = {
"swipe": Counter.new(),
}
2026-03-30 00:48:20 +03:00
var ActiveEntities: Dictionary = {}
2026-03-09 16:10:35 +03:00
2026-03-30 00:48:20 +03:00
var owniverse = Owniverse.new()
2026-04-05 20:15:07 +03:00
func set_datetime(value: String):
date_time.text = value
func on_putton_pressed(button_name: String):
#emit_signal("button_pressed", button_name)
print(button_name)
#SaveManager.save_settings()
#SaveManager.ch()
#print(Global.filename)
#func _on_speed_change(delta: int) -> void:
#print(delta)
2026-03-30 00:48:20 +03:00
func entity_activated(entity: OwniverseEntity) -> void:
_control_page(entity)
_set_description(entity)
2026-03-09 16:10:35 +03:00
2026-03-30 00:48:20 +03:00
func _set_description(entity: OwniverseEntity) -> void:
var description: String = "[b]" \
+ Global.get_localized_item_description(entity.entity_name, "label") \
+ "[/b]" \
+ "\n" \
+ Global.get_localized_item_description(entity.entity_name, "description")
var desc_page = clamp(entity.entity_page, 0, 4)
g_entity_descriptions[desc_page].text = description
2026-03-09 16:10:35 +03:00
pass
2026-03-30 00:48:20 +03:00
func _control_page(entity: OwniverseEntity) -> void:
if !ActiveEntities.has(entity.entity_page):
ActiveEntities[entity.entity_page] = null
var current_entity:OwniverseEntity = ActiveEntities[entity.entity_page]
if current_entity == entity:
return
2026-03-09 16:10:35 +03:00
2026-03-30 00:48:20 +03:00
ActiveEntities[entity.entity_page] = entity
if current_entity != null and current_entity.entity_page == entity.entity_page:
current_entity.set_selection(false)
entity.set_selection(true)
# subpages
if entity.sub_page != -1:
item_pages[entity.sub_page].visible = true
if current_entity == null \
or current_entity.sub_page == -1 \
or current_entity.sub_page == entity.sub_page:
return
item_pages[current_entity.sub_page].visible = false
func _init_entities() -> bool:
for e_id in Global.Entities:
var entity_properties: Dictionary = Global.Entities[e_id]
var entity: OwniverseEntity = ITEM_BUTTON.instantiate()
if !entity_properties.has("page"):
printerr("Entity `", e_id, "` has no `page` key.")
return false
var page: int = int(entity_properties.page)
item_pages[page].add_child(entity)
entity.init_button(entity_properties)
entity.entity_activated.connect(entity_activated)
return false
2026-03-09 16:10:35 +03:00
func _check_counters(delta: float) -> Array:
var actions: Array = []
for k in g_counters:
var cnt: Counter = g_counters[k]
if not cnt.is_active:
continue
cnt.value += delta
if cnt.value >= cnt.sec:
actions.append(k)
cnt.value = 0
cnt.is_active = false
return actions
2026-04-05 20:15:07 +03:00
2026-03-09 16:10:35 +03:00
func _do_actions(actions: Array) -> void:
for action: String in actions:
match action:
"swipe":
_swipe_to_nearest_page()
2026-03-30 00:48:20 +03:00
#region Swipe
2026-03-09 16:10:35 +03:00
func _swipe_to_nearest_page() -> void:
if stage_page_scroller.scroll_horizontal % PAGE_WIDTH == 0: return
var new_page: int = round(1.0 * stage_page_scroller.scroll_horizontal / PAGE_WIDTH)
if g_tween != null:
if g_tween.is_running():
return
g_tween = stage_page_scroller.get_tree().create_tween()
g_tween.tween_property(stage_page_scroller, "scroll_horizontal",
new_page * PAGE_WIDTH, SWIPE_DURATION).set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT)
func _input(event: InputEvent) -> void:
if event is InputEventScreenDrag:
stage_page_scroller.scroll_horizontal = clamp(
stage_page_scroller.scroll_horizontal - PAGE_COUNT * event.screen_relative.x,
0, PAGE_WIDTH * (PAGE_COUNT - 1))
g_counters.swipe.is_active = true
g_counters.swipe.value = 0
elif event is InputEventPanGesture:
g_counters.swipe.is_active = true
g_counters.swipe.value = 0
elif event is InputEventScreenTouch:
pass
2026-03-30 00:48:20 +03:00
#endregion
func _ready() -> void:
_init_entities()
2026-04-05 20:15:07 +03:00
func _on_button_pressed(extra_param): # теперь 1 параметр
print("Кнопка нажата! Параметр:", extra_param)
2026-03-30 00:48:20 +03:00
func _process(delta: float) -> void:
# Update date
2026-04-05 20:15:07 +03:00
var sdt: String = str(snapped(owniverse.add_time_delta(delta), 0.1))
set_datetime(sdt)
2026-03-30 00:48:20 +03:00
var actions = _check_counters(delta)
_do_actions(actions)
2026-04-05 20:15:07 +03:00
func _on_burger_button_pressed() -> void:
print("b")
pass # Replace with function body.
func _on_report_button_pressed() -> void:
print("r")
pass # Replace with function body.
func _on_time_minus_pressed() -> void:
_change_speed(-1)
func _on_time_plus_pressed() -> void:
_change_speed(+1)
func _change_speed(delta: int) -> void:
var speed: int = owniverse.change_speed(delta)
var ind: int = 1
for icon in g_speed_icons:
if ind <= speed:
icon.visible = true
else:
icon.visible = false
ind += 1
if speed == 1:
g_time_minus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % "x")
g_time_plus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % "2")
elif speed == Global.SPEED_LIMIT:
g_time_minus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % str(Global.SPEED_LIMIT - 1))
g_time_plus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % "x")
else:
g_time_minus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % str(speed - 1))
g_time_plus.texture_normal = load(SPEED_TEXTURE_NAME_TEMPLATE % str(speed + 1))