Add collapsible tile detail drawer UI

This commit is contained in:
dingo
2026-04-14 11:06:58 -04:00
parent a219b2fb31
commit ad6ae689ed
2 changed files with 303 additions and 50 deletions
@@ -242,6 +242,21 @@ layout_mode = 2
theme_override_font_sizes/font_size = 12
text = "Settings"
[node name="TileDrawerToggleBtn" type="Button" parent="HUD"]
unique_name_in_owner = true
custom_minimum_size = Vector2(36, 48)
anchors_preset = 6
anchor_left = 1.0
anchor_top = 0.5
anchor_right = 1.0
anchor_bottom = 0.5
offset_left = -48.0
offset_top = -24.0
offset_right = -12.0
offset_bottom = 24.0
theme_override_font_sizes/font_size = 12
text = "<"
[node name="ContextPopup" type="PanelContainer" parent="HUD"]
unique_name_in_owner = true
visible = false
+288 -50
View File
@@ -53,6 +53,7 @@ enum Mode {
@onready var _draw_btn : Button = %DrawBtn
@onready var _end_turn_btn : Button = %EndTurnBtn
@onready var _settings_btn : Button = %SettingsBtn
@onready var _tile_drawer_toggle_btn : Button = %TileDrawerToggleBtn
@onready var _in_game_settings: PanelContainer = %InGameSettings
@onready var _ctx_popup : PanelContainer = %ContextPopup
@onready var _ctx_tile_lbl : Label = %ContextTileLabel
@@ -181,12 +182,20 @@ var _triggered_request_id : String = "" # requestId to echo back in the resp
var _tile_drawer : PanelContainer = null
var _tile_drawer_open : bool = false
var _tile_drawer_tween : Tween = null
var _tile_drawer_lbl_coord : Label = null
var _tile_drawer_lbl_aff : Label = null
var _tile_drawer_lbl_b1 : Label = null
var _tile_drawer_lbl_b2 : Label = null
var _tile_drawer_lbl_ruin : Label = null
var _tile_drawer_lbl_occ : Label = null
var _tile_drawer_last_tile : Dictionary = {}
var _tile_drawer_tile_labels : Array = []
var _tile_drawer_spirit_labels: Array = []
var _tile_drawer_token_labels : Array = []
var _tile_drawer_arch_labels : Array = []
var _tile_drawer_ruin_labels : Array = []
var _tile_drawer_tile_section : Dictionary = {}
var _tile_drawer_spirit_section : Dictionary = {}
var _tile_drawer_token_section : Dictionary = {}
var _tile_drawer_arch_section : Dictionary = {}
var _tile_drawer_ruin_section : Dictionary = {}
const _TILE_DRAWER_WIDTH: float = 260.0
const _TILE_DRAWER_TOGGLE_WIDTH: float = 36.0
const _TILE_DRAWER_TOGGLE_MARGIN: float = 12.0
# ── Unit ownership lookup ─────────────────────────────────────────────────────
# Rebuilt on each state update for O(1) ownership checks.
@@ -275,6 +284,7 @@ func _ready() -> void:
_settings_btn.pressed.connect(func() -> void: _in_game_settings.open())
_discard_btn.pressed.connect(func() -> void: _show_own_discard())
_opp_discard_btn.pressed.connect(func() -> void: _show_opp_discard())
_tile_drawer_toggle_btn.pressed.connect(_on_tile_drawer_toggle_pressed)
_ctx_close_btn.pressed.connect(func() -> void: _ctx_popup.visible = false)
_dv_close_btn.pressed.connect(func() -> void: _dv_overlay.visible = false)
_go_menu_btn.pressed.connect(func() -> void:
@@ -2534,7 +2544,7 @@ func _valid_play_tiles(card: Dictionary) -> Array:
results.append(Vector2i(tx, ty))
"Repentance", "Instinct", "Ruin":
pass # No tile target — played directly (server handles placement).
pass # No tile target — played directly (server handles placement for Ruins, repentance and Instinct have own target systems).
return results
@@ -2799,7 +2809,7 @@ func _create_tile_drawer() -> void:
var title_row := HBoxContainer.new()
title_row.layout_mode = 2
var title_lbl := Label.new()
title_lbl.text = "Tile Detail"
title_lbl.text = "Tile Details"
title_lbl.size_flags_horizontal = Control.SIZE_EXPAND_FILL
title_lbl.add_theme_font_size_override("font_size", 15)
var close_btn := Button.new()
@@ -2814,68 +2824,296 @@ func _create_tile_drawer() -> void:
sep.layout_mode = 2
vbox.add_child(sep)
# Detail labels.
_tile_drawer_lbl_coord = Label.new()
_tile_drawer_lbl_aff = Label.new()
_tile_drawer_lbl_b1 = Label.new()
_tile_drawer_lbl_b2 = Label.new()
_tile_drawer_lbl_ruin = Label.new()
_tile_drawer_lbl_occ = Label.new()
for lbl: Label in [_tile_drawer_lbl_coord, _tile_drawer_lbl_aff,
_tile_drawer_lbl_b1, _tile_drawer_lbl_b2,
_tile_drawer_lbl_ruin, _tile_drawer_lbl_occ]:
_tile_drawer_tile_section = _create_tile_drawer_section(vbox, "Tile Details", true)
_tile_drawer_spirit_section = _create_tile_drawer_section(vbox, "Spirit Details", false)
_tile_drawer_token_section = _create_tile_drawer_section(vbox, "Spirit Token Details", false)
_tile_drawer_arch_section = _create_tile_drawer_section(vbox, "ArchSpirit Details", false)
_tile_drawer_ruin_section = _create_tile_drawer_section(vbox, "Ruin Details", false)
_tile_drawer_tile_labels = _create_detail_labels(_tile_drawer_tile_section["content"], 5)
_tile_drawer_spirit_labels = _create_detail_labels(_tile_drawer_spirit_section["content"], 5)
_tile_drawer_token_labels = _create_detail_labels(_tile_drawer_token_section["content"], 5)
_tile_drawer_arch_labels = _create_detail_labels(_tile_drawer_arch_section["content"], 6)
_tile_drawer_ruin_labels = _create_detail_labels(_tile_drawer_ruin_section["content"], 2)
_set_tile_drawer_button_state()
func _create_tile_drawer_section(parent: VBoxContainer, title: String,
expanded: bool) -> Dictionary:
var section: VBoxContainer = VBoxContainer.new()
section.layout_mode = 2
section.add_theme_constant_override("separation", 4)
parent.add_child(section)
var header: Button = Button.new()
header.layout_mode = 2
header.toggle_mode = true
header.button_pressed = expanded
header.alignment = HORIZONTAL_ALIGNMENT_LEFT
header.text = _tile_drawer_section_title(title, expanded)
section.add_child(header)
var content: VBoxContainer = VBoxContainer.new()
content.layout_mode = 2
content.visible = expanded
content.add_theme_constant_override("separation", 2)
section.add_child(content)
section.visible = expanded
header.toggled.connect(func(is_pressed: bool) -> void:
content.visible = is_pressed
header.text = _tile_drawer_section_title(title, is_pressed))
return {
"root": section,
"header": header,
"content": content,
"title": title,
}
func _tile_drawer_section_title(title: String, expanded: bool) -> String:
return "%s %s" % ["[-]" if expanded else "[+]", title]
func _create_detail_labels(parent: VBoxContainer, count: int) -> Array:
var labels: Array = []
for _i in range(count):
var lbl: Label = Label.new()
lbl.layout_mode = 2
lbl.mouse_filter = Control.MOUSE_FILTER_STOP
lbl.add_theme_font_size_override("font_size", 12)
lbl.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
vbox.add_child(lbl)
parent.add_child(lbl)
labels.append(lbl)
return labels
func _append_tile_drawer_label(parent: VBoxContainer, labels: Array) -> void:
var lbl: Label = Label.new()
lbl.layout_mode = 2
lbl.mouse_filter = Control.MOUSE_FILTER_STOP
lbl.add_theme_font_size_override("font_size", 12)
lbl.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
parent.add_child(lbl)
labels.append(lbl)
func _set_tile_drawer_lines(section: Dictionary, labels: Array, lines: Array,
tooltips: Array = []) -> void:
var root: Control = section.get("root") as Control
var content: VBoxContainer = section.get("content") as VBoxContainer
if root == null:
return
if content == null:
return
while labels.size() < lines.size():
_append_tile_drawer_label(content, labels)
root.visible = not lines.is_empty()
for i in range(labels.size()):
var lbl: Label = labels[i] as Label
var show_line: bool = i < lines.size()
lbl.visible = show_line
if show_line:
lbl.text = str(lines[i])
lbl.tooltip_text = str(tooltips[i]) if i < tooltips.size() else ""
else:
lbl.tooltip_text = ""
func _find_unit_owner_account_id(unit_id: String) -> String:
if unit_id.is_empty():
return ""
for player: Dictionary in Game.state.get("players", []):
for unit: Dictionary in player.get("field", []):
if str(unit.get("instanceId", "")) == unit_id:
return str(player.get("accountId", ""))
var arch: Dictionary = player.get("archSpirit", {})
if str(arch.get("instanceId", "")) == unit_id:
return str(player.get("accountId", ""))
return ""
func _build_unit_detail_lines(unit: Dictionary, owner_id: String,
include_tier: bool = false) -> Dictionary:
var lines: Array = [
"Name: %s" % str(unit.get("name", "?")),
"Owner: %s" % Game.get_display_name(owner_id),
]
var tooltips: Array = ["", ""]
if include_tier:
lines.append("Tier: %d" % int(unit.get("tier", 0)))
tooltips.append("")
lines.append("HP: %d / %d" % [int(unit.get("currentHealth", 0)), int(unit.get("maxHealth", 0))])
lines.append("DEF: %d / %d" % [int(unit.get("currentDefense", 0)), int(unit.get("maxDefense", 0))])
lines.append("ATK: %d" % int(unit.get("damage", 0)))
lines.append("MP: %d / %d" % [int(unit.get("currentMp", 0)), int(unit.get("maxMp", 0))])
lines.append("Can Move: %s" % ("Yes" if bool(unit.get("canMove", false)) else "No"))
lines.append("Can Attack: %s" % ("Yes" if bool(unit.get("canAttack", false)) else "No"))
tooltips.append_array(["", "", "", "", "", ""])
var raw_abilities: Variant = unit.get("abilitiesJson")
var parsed_abilities: Variant = JSON.parse_string("[]" if raw_abilities == null else str(raw_abilities))
if parsed_abilities is Array:
for ability_index: int in range((parsed_abilities as Array).size()):
var ability: Dictionary = (parsed_abilities as Array)[ability_index] as Dictionary
var ability_name: String = str(ability.get("name", ability.get("Name", "Ability %d" % ability_index)))
var ability_desc: String = str(ability.get("description", ability.get("Description", "")))
lines.append("Ability: %s" % ability_name)
tooltips.append(ability_desc)
return {
"lines": lines,
"tooltips": tooltips,
}
func _set_tile_drawer_content(tile: Dictionary) -> void:
_tile_drawer_last_tile = tile.duplicate(true)
_set_tile_drawer_button_state()
if tile.is_empty():
_set_tile_drawer_lines(_tile_drawer_tile_section, _tile_drawer_tile_labels, [])
_set_tile_drawer_lines(_tile_drawer_spirit_section, _tile_drawer_spirit_labels, [])
_set_tile_drawer_lines(_tile_drawer_token_section, _tile_drawer_token_labels, [])
_set_tile_drawer_lines(_tile_drawer_arch_section, _tile_drawer_arch_labels, [])
_set_tile_drawer_lines(_tile_drawer_ruin_section, _tile_drawer_ruin_labels, [])
return
var coord : Dictionary = tile.get("coord", {})
var cx : int = int(coord.get("x", 0))
var cy : int = int(coord.get("y", 0))
var aff : String = str(tile.get("resolvedAffinity", "?"))
var b1 : String = str(tile.get("base1", "?"))
var b2 : String = str(tile.get("base2", "?"))
var ruin_id_value: Variant = tile.get("ruinId", "")
var ruin_id : String = "" if ruin_id_value == null else str(ruin_id_value)
var ruin_owner_id : String = str(tile.get("ruinOwner", ""))
var occ_id : String = str(tile.get("occupantId", ""))
var occ_owner_id : String = str(tile.get("occupantOwner", ""))
var occ_unit: Dictionary = Game.get_unit_by_id(occ_id)
var occ_type : String = str(occ_unit.get("cardType", ""))
var occ_name : String = str(occ_unit.get("name", occ_id))
var tile_lines : Array = [
"Coord: (%d, %d)" % [cx, cy],
"Affinity: %s" % aff,
"Base1: %s" % b1,
"Base2: %s" % b2,
"Occupant: %s" % ("None" if occ_id.is_empty() or occ_id == "null" else "%s (%s)" % [occ_name, occ_type]),
]
_set_tile_drawer_lines(_tile_drawer_tile_section, _tile_drawer_tile_labels, tile_lines)
if not occ_id.is_empty() and occ_id != "null" and occ_type == "Spirit":
var spirit_owner_id: String = occ_owner_id if not occ_owner_id.is_empty() else _find_unit_owner_account_id(occ_id)
var spirit_detail: Dictionary = _build_unit_detail_lines(occ_unit, spirit_owner_id)
_set_tile_drawer_lines(
_tile_drawer_spirit_section,
_tile_drawer_spirit_labels,
spirit_detail.get("lines", []),
spirit_detail.get("tooltips", []))
else:
_set_tile_drawer_lines(_tile_drawer_spirit_section, _tile_drawer_spirit_labels, [])
if not occ_id.is_empty() and occ_id != "null" and occ_type == "SpiritToken":
var token_owner_id: String = occ_owner_id if not occ_owner_id.is_empty() else _find_unit_owner_account_id(occ_id)
var token_detail: Dictionary = _build_unit_detail_lines(occ_unit, token_owner_id)
_set_tile_drawer_lines(
_tile_drawer_token_section,
_tile_drawer_token_labels,
token_detail.get("lines", []),
token_detail.get("tooltips", []))
else:
_set_tile_drawer_lines(_tile_drawer_token_section, _tile_drawer_token_labels, [])
if not occ_id.is_empty() and occ_id != "null" and occ_type == "ArchSpirit":
var arch_owner_id: String = occ_owner_id if not occ_owner_id.is_empty() else _find_unit_owner_account_id(occ_id)
var arch_detail: Dictionary = _build_unit_detail_lines(occ_unit, arch_owner_id, true)
_set_tile_drawer_lines(
_tile_drawer_arch_section,
_tile_drawer_arch_labels,
arch_detail.get("lines", []),
arch_detail.get("tooltips", []))
else:
_set_tile_drawer_lines(_tile_drawer_arch_section, _tile_drawer_arch_labels, [])
if ruin_id_value != null and not ruin_id.is_empty() and ruin_id != "null":
var ruin_lines : Array = [
"Ruin: %s" % ruin_id,
"Owner: %s" % Game.get_display_name(ruin_owner_id),
]
_set_tile_drawer_lines(_tile_drawer_ruin_section, _tile_drawer_ruin_labels, ruin_lines)
else:
_set_tile_drawer_lines(_tile_drawer_ruin_section, _tile_drawer_ruin_labels, [])
func _set_tile_drawer_button_state() -> void:
if not is_instance_valid(_tile_drawer_toggle_btn):
return
var has_tile: bool = not _tile_drawer_last_tile.is_empty()
_tile_drawer_toggle_btn.disabled = not has_tile
_tile_drawer_toggle_btn.text = ">" if _tile_drawer_open else "<"
_layout_tile_drawer_toggle_button()
func _layout_tile_drawer_toggle_button() -> void:
if not is_instance_valid(_tile_drawer_toggle_btn):
return
if _tile_drawer_open:
_tile_drawer_toggle_btn.offset_left = -_TILE_DRAWER_WIDTH - _TILE_DRAWER_TOGGLE_WIDTH
_tile_drawer_toggle_btn.offset_right = -_TILE_DRAWER_WIDTH
else:
_tile_drawer_toggle_btn.offset_left = -(_TILE_DRAWER_TOGGLE_WIDTH + _TILE_DRAWER_TOGGLE_MARGIN)
_tile_drawer_toggle_btn.offset_right = -_TILE_DRAWER_TOGGLE_MARGIN
_tile_drawer_toggle_btn.offset_top = -24.0
_tile_drawer_toggle_btn.offset_bottom = 24.0
func _on_tile_drawer_toggle_pressed() -> void:
if _tile_drawer_open:
_close_tile_drawer()
return
if _tile_drawer_last_tile.is_empty():
return
_open_tile_drawer(_tile_drawer_last_tile)
func _open_tile_drawer(tile: Dictionary) -> void:
if not is_instance_valid(_tile_drawer): return
var coord : Dictionary = tile.get("coord", {})
var cx : int = int(coord.get("x", 0))
var cy : int = int(coord.get("y", 0))
var aff : String = str(tile.get("resolvedAffinity", "?"))
var b1 : String = str(tile.get("base1", "?"))
var b2 : String = str(tile.get("base2", "?"))
var ruin_id : String = str(tile.get("ruinId", ""))
var occ_id : String = str(tile.get("occupantId", ""))
_tile_drawer_lbl_coord.text = "(%d, %d)" % [cx, cy]
_tile_drawer_lbl_aff.text = "Affinity: %s" % aff
_tile_drawer_lbl_b1.text = "Base1: %s" % b1
_tile_drawer_lbl_b2.text = "Base2: %s" % b2
if not ruin_id.is_empty() and ruin_id != "null":
_tile_drawer_lbl_ruin.text = "Ruin: %s" % ruin_id
_tile_drawer_lbl_ruin.visible = true
else:
_tile_drawer_lbl_ruin.visible = false
if not occ_id.is_empty() and occ_id != "null":
var occ_unit := Game.get_unit_by_id(occ_id)
_tile_drawer_lbl_occ.text = "Occupant: %s" % (
occ_unit.get("name", occ_id) if not occ_unit.is_empty() else occ_id)
_tile_drawer_lbl_occ.visible = true
else:
_tile_drawer_lbl_occ.visible = false
_set_tile_drawer_content(tile)
if _tile_drawer_open:
_set_tile_drawer_button_state()
return # already open, content updated above
_tile_drawer_open = true
_tile_drawer.visible = true
_set_tile_drawer_button_state()
if _tile_drawer_tween: _tile_drawer_tween.kill()
_tile_drawer.offset_left = 0.0
_tile_drawer_tween = create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_CUBIC)
_tile_drawer_tween.tween_property(_tile_drawer, "offset_left", -260.0, 0.3)
_tile_drawer_tween.parallel().tween_property(_tile_drawer, "offset_left", -_TILE_DRAWER_WIDTH, 0.3)
_tile_drawer_tween.parallel().tween_property(
_tile_drawer_toggle_btn, "offset_left",
-_TILE_DRAWER_WIDTH - _TILE_DRAWER_TOGGLE_WIDTH, 0.3)
_tile_drawer_tween.parallel().tween_property(
_tile_drawer_toggle_btn, "offset_right",
-_TILE_DRAWER_WIDTH, 0.3)
func _close_tile_drawer() -> void:
if not is_instance_valid(_tile_drawer) or not _tile_drawer_open: return
_tile_drawer_open = false
_set_tile_drawer_button_state()
if _tile_drawer_tween: _tile_drawer_tween.kill()
_tile_drawer_tween = create_tween().set_ease(Tween.EASE_IN).set_trans(Tween.TRANS_CUBIC)
_tile_drawer_tween.tween_property(_tile_drawer, "offset_left", 0.0, 0.25)
_tile_drawer_tween.parallel().tween_property(_tile_drawer, "offset_left", 0.0, 0.25)
_tile_drawer_tween.parallel().tween_property(
_tile_drawer_toggle_btn, "offset_left",
-(_TILE_DRAWER_TOGGLE_WIDTH + _TILE_DRAWER_TOGGLE_MARGIN), 0.25)
_tile_drawer_tween.parallel().tween_property(
_tile_drawer_toggle_btn, "offset_right",
-_TILE_DRAWER_TOGGLE_MARGIN, 0.25)
_tile_drawer_tween.tween_callback(func() -> void:
if is_instance_valid(_tile_drawer): _tile_drawer.visible = false)
@@ -2908,7 +3146,7 @@ func _handle_board_click(mouse_pos: Vector2) -> void:
unit_id = _occ
# Show tile detail drawer and optionally pan camera.
if coord.x >= 0:
_open_tile_drawer(Game.get_tile(coord.x, coord.y))
_set_tile_drawer_content(Game.get_tile(coord.x, coord.y))
if Settings.click_to_focus_camera:
_focus_camera_on_tile(coord.x, coord.y)
# Unit selection.
@@ -4104,7 +4342,7 @@ func _update_hud(state: Dictionary) -> void:
_turn_lbl.modulate = Color(0.4, 1.0, 0.5) if is_mine else Color(1.0, 0.6, 0.6)
_sp_lbl.text = "%d" % int(my.get("currentSp", 0))
_st_lbl.text = "ST: %d" % int(my.get("currentSt", 0))
_st_lbl.text = "%d" % int(my.get("currentSt", 0))
_deck_lbl.text = "Deck: %d" % int(my.get("deckCount", 0))
_opp_name_lbl.text = Game.get_display_name(str(opp.get("accountId", "")))