Files
windroost/p2_godot_sample/scripts/ui_slice_shell.gd
T

314 lines
9.5 KiB
GDScript

class_name UiSliceShell
extends Control
## U1 vertical-slice shell: SceneCanvas -> page host -> PrimaryCTADock -> NavBar.
## Battle chrome, numbers, save and locale files are owned elsewhere and are
## not touched here. Safe areas are injected by the capture runner.
signal cta_pressed
signal more_pressed
const PAGE_SKYPORT := "skyport"
const PAGE_WORLD := "world"
const PAGE_SETTLEMENT := "settlement"
const NAV_HEIGHT := 64
const HEADER_HEIGHT := 56
var locale: String = "zh_CN"
var reduced_motion: bool = false
var safe_top: int = 24
var safe_bottom: int = 34
var current_page: String = PAGE_SKYPORT
var current_state: String = ""
var _background: TextureRect
var _header: PanelContainer
var _header_title: Label
var _page_host: Control
var _dock: PrimaryCTADock
var _nav: PanelContainer
var _nav_buttons: Array = []
var _nav_specs: Array = []
var _nav_icons: Array = []
var _pages: Dictionary = {}
func _ready() -> void:
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
mouse_filter = Control.MOUSE_FILTER_PASS
_build_background()
_build_header()
_page_host = Control.new()
_page_host.mouse_filter = Control.MOUSE_FILTER_PASS
add_child(_page_host)
_build_nav()
_dock = PrimaryCTADock.new("", "", safe_bottom)
add_child(_dock)
_dock.cta_pressed.connect(_on_dock_cta)
_rebuild_pages()
_layout()
_refresh()
func _build_background() -> void:
_background = TextureRect.new()
_background.texture = load("res://assets/windplain_pixel_background_source.png") as Texture2D
_background.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
_background.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_COVERED
_background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
_background.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
_background.mouse_filter = Control.MOUSE_FILTER_IGNORE
_background.modulate = Color(0.55, 0.62, 0.76, 1.0)
add_child(_background)
var veil := ColorRect.new()
veil.color = Color(0.03, 0.08, 0.14, 0.16)
veil.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
veil.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(veil)
func _build_header() -> void:
_header = UiTheme.make_status_strip(0.88)
_header.mouse_filter = Control.MOUSE_FILTER_PASS
add_child(_header)
var margin := MarginContainer.new()
margin.add_theme_constant_override("margin_left", 14)
margin.add_theme_constant_override("margin_right", 14)
margin.add_theme_constant_override("margin_top", 6)
margin.add_theme_constant_override("margin_bottom", 6)
_header.add_child(margin)
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 8)
margin.add_child(row)
row.add_child(UiTheme.make_icon("res://assets/icons/icon_gold.png", 18))
var gold := UiTheme.make_label("240", 13, UiTheme.COLOR_BORDER)
gold.name = "HeaderGold"
row.add_child(gold)
row.add_child(UiTheme.make_icon("res://assets/icons/icon_crystal.png", 18))
var crystal := UiTheme.make_label("12", 13, UiTheme.COLOR_MINT)
crystal.name = "HeaderCrystal"
row.add_child(crystal)
_header_title = UiTheme.make_label("", 14)
_header_title.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
_header_title.size_flags_horizontal = Control.SIZE_EXPAND_FILL
row.add_child(_header_title)
func _build_nav() -> void:
_nav = UiTheme.glass_panel(0.94)
_nav.mouse_filter = Control.MOUSE_FILTER_STOP
add_child(_nav)
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 2)
row.size_flags_horizontal = Control.SIZE_EXPAND_FILL
_nav.add_child(row)
_nav_specs = [
{"id": PAGE_SKYPORT, "icon": "res://assets/icons/icon_hammer.png", "key": "hud.nav.skyport"},
{"id": PAGE_WORLD, "icon": "res://assets/icons/icon_flag.png", "key": "hud.nav.expedition"},
{"id": PAGE_SETTLEMENT, "icon": "res://assets/icons/icon_chest.png", "key": "modal.victory.title"},
{"id": "more", "icon": "res://assets/icons/icon_click_gear.png", "key": "shell.side.title"},
]
for i in _nav_specs.size():
var spec: Dictionary = _nav_specs[i]
var button := UiTheme.make_menu_button(
str(spec["icon"]), UiText.t(_nav_text_key(i), locale), "secondary", 11, true, true)
button.custom_minimum_size = Vector2(0, 48)
button.pressed.connect(_on_nav.bind(str(spec["id"])))
row.add_child(button)
_nav_buttons.append(button)
_nav_icons.append(button.icon)
func _nav_narrow_en() -> bool:
# Same narrow en_US text-fit gate already used by game_root.gd. No locale
# table or wider-layout presentation is changed.
return locale.begins_with("en") and get_viewport_rect().size.x <= 400.0
func _nav_text_key(index: int) -> String:
# "shell.side.title" ("Company Affairs") overflows a <=400px viewport and
# clips the fourth tab. Reuse the existing short key; every other tab,
# locale and viewport keeps its exact authored key.
if index == 3 and _nav_narrow_en():
return "hud.nav.company"
return str(_nav_specs[index]["key"])
func _rebuild_pages() -> void:
for child in _page_host.get_children():
_page_host.remove_child(child)
child.free()
_pages.clear()
var skyport := UiPageSkyport.new()
skyport.build(locale, reduced_motion)
_pages[PAGE_SKYPORT] = skyport
var world := UiPageWorld.new()
world.build(locale, reduced_motion)
_pages[PAGE_WORLD] = world
var settlement := UiPageSettlement.new()
settlement.set_state(current_state if not current_state.is_empty() else "victory")
settlement.build(locale, reduced_motion)
_pages[PAGE_SETTLEMENT] = settlement
for page in _pages.values():
_page_host.add_child(page)
show_page(current_page, current_state)
func _layout() -> void:
var nav_h := NAV_HEIGHT if current_page != PAGE_SETTLEMENT else 0
_header.set_anchors_and_offsets_preset(Control.PRESET_TOP_WIDE)
_header.offset_left = 8
_header.offset_right = -8
_header.offset_top = safe_top
_header.custom_minimum_size = Vector2(0, HEADER_HEIGHT)
_nav.visible = nav_h > 0
_nav.set_anchors_and_offsets_preset(Control.PRESET_BOTTOM_WIDE)
_nav.offset_left = 0
_nav.offset_right = 0
_nav.offset_top = -(NAV_HEIGHT + safe_bottom)
_nav.offset_bottom = -safe_bottom
var dock_h: int = _dock.min_height() + safe_bottom
_dock.set_anchors_and_offsets_preset(Control.PRESET_BOTTOM_WIDE)
_dock.offset_left = 0
_dock.offset_right = 0
_dock.offset_top = -(dock_h + nav_h)
_dock.offset_bottom = -nav_h
_dock.set_safe_bottom(safe_bottom)
_page_host.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
_page_host.offset_left = 0
_page_host.offset_right = 0
_page_host.offset_top = safe_top + HEADER_HEIGHT + 8
_page_host.offset_bottom = -(dock_h + nav_h + 8)
func _refresh() -> void:
var page: Control = _pages.get(current_page) as Control
if page == null:
return
_header_title.text = UiText.t(page.title_key(), locale)
var gold := _get_label("HeaderGold")
var crystal := _get_label("HeaderCrystal")
if gold != null:
gold.text = "240"
if crystal != null:
crystal.text = "12"
for i in _nav_buttons.size():
var button := _nav_buttons[i] as Button
button.text = UiText.t(_nav_text_key(i), locale)
# The nav row sizes to content, so the gear icon still pushes the short
# label off-screen at 360/390. Suppress only that icon here; the tab
# count/order, underline selected style and press routing stay intact.
button.icon = null if i == 3 and _nav_narrow_en() else (_nav_icons[i] as Texture2D)
_dock.set_label(page.cta_label())
_dock.set_icon(page.cta_icon())
_dock.set_enabled(page.cta_enabled())
func _get_label(node_name: String) -> Label:
var stack: Array = [_header]
while not stack.is_empty():
var node: Node = stack.pop_back()
if node.name == node_name and node is Label:
return node as Label
for child in node.get_children():
stack.append(child)
return null
func _on_dock_cta() -> void:
cta_pressed.emit()
func _on_nav(page: String) -> void:
if page == "more":
more_pressed.emit()
if _pages.has(PAGE_SKYPORT):
(_pages[PAGE_SKYPORT] as UiPageSkyport)._toggle_drawer()
return
show_page(page)
func set_locale(value: String) -> void:
if value == locale:
return
locale = value
_rebuild_pages()
_layout()
_refresh()
func set_reduced_motion(value: bool) -> void:
if value == reduced_motion:
return
reduced_motion = value
_rebuild_pages()
_layout()
_refresh()
func set_safe_area(top: int, bottom: int) -> void:
safe_top = top
safe_bottom = bottom
_layout()
_refresh()
func show_page(page: String, state: String = "") -> void:
if not _pages.has(page):
return
current_page = page
current_state = state
for key in _pages.keys():
var control: Control = _pages[key] as Control
control.visible = key == page
var active: Control = _pages[page] as Control
if not state.is_empty() and active.has_method("set_state"):
active.call("set_state", state)
_layout()
_refresh()
func dock() -> PrimaryCTADock:
return _dock
func dock_rect() -> Rect2:
return _dock.button_global_rect()
func page_host_rect() -> Rect2:
return _page_host.get_global_rect()
func scroll_container() -> ScrollContainer:
var page: Control = _pages.get(current_page) as Control
if page == null:
return null
return page.get("scroll") as ScrollContainer
func selected_node() -> String:
var page: Control = _pages.get(PAGE_WORLD) as Control
if page == null:
return ""
return page.call("selected_node")
func tap_targets() -> Dictionary:
var out: Dictionary = {}
if _dock.button != null:
out["cta_primary"] = _dock.button
if _nav.visible:
for i in _nav_buttons.size():
out["nav_%d" % i] = _nav_buttons[i]
# Only the visible page has meaningful global rects; hidden pages report 0x0.
var active: Control = _pages.get(current_page) as Control
if active != null:
var targets: Dictionary = active.get("tap_targets")
for key in targets.keys():
out[key] = targets[key]
return out