Fix: defer z_index + tween to prevent Godot GUI re-triggering card press

Setting widget.z_index = 20 synchronously while a mouse button is held causes
Godot 4 to re-evaluate GUI focus and deliver a synthetic second pressed=true
event to the widget.  This fired card_pressed a second time in the same frame,
making _on_hand_card_clicked see the card already selected → immediate deselect.

Fix: move tween creation and z_index assignment into a new _tween_hand_card()
helper, called via call_deferred() so it runs after the current input event is
fully processed (mouse button already released by then).

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-03-08 14:09:40 -04:00
co-authored by Claude Happy
parent 1a2fdad9ba
commit f5621d6285
@@ -487,6 +487,7 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color) -> void:
# ── Highlights ────────────────────────────────────────────────────────────────
func _clear_highlights() -> void:
print("Clearing")
for c in _hi_root.get_children(): c.queue_free()
@@ -709,14 +710,17 @@ func _handle_board_click(mouse_pos: Vector2) -> void:
Mode.CARD_FROM_HAND:
var card := _find_hand_card(_sel_card_id)
if card.is_empty():
print("Card empty, mode none")
_set_mode(Mode.NONE); return
var card_type : String = card.get("cardType", "")
if card_type in ["Repentance", "Instinct", "Ruin"]:
Game.send_play_card(_sel_card_id, 0, 0)
_set_mode(Mode.NONE); return
if coord.x < 0:
print("x coor < 0")
_set_mode(Mode.NONE); return
Game.send_play_card(_sel_card_id, coord.x, coord.y)
print("Send play card")
_set_mode(Mode.NONE)
Mode.TERRAMORPHA_SELECT:
@@ -818,6 +822,7 @@ func _select_unit(unit_id: String) -> void:
func _set_mode(new_mode: Mode) -> void:
print("Setting mode: ", new_mode)
_mode = new_mode
_terra_targets = []
_terra_count = 0
@@ -1068,11 +1073,13 @@ func _on_hand_card_clicked(iid: String, _panel: PanelContainer) -> void:
# Deselect if same card clicked again.
if _mode == Mode.CARD_FROM_HAND and _sel_card_id == iid:
print("Deselect")
_set_mode(Mode.NONE); return
# If switching cards (or coming from TERRAMORPHA_SELECT), explicitly reset
# so the previous card's scale-up tween is released before entering the new mode.
if _mode in [Mode.CARD_FROM_HAND, Mode.TERRAMORPHA_SELECT]:
print("switching cards catch")
_set_mode(Mode.NONE)
# Normal Spirit / Repentance / Instinct.
@@ -1129,16 +1136,25 @@ func _refresh_hand_selection() -> void:
if widget.has_method("set_selected"):
widget.set_selected(sel)
# Tween scale on selection change.
# IMPORTANT: defer the tween + z_index update so they run AFTER the
# current input event is fully processed. Setting z_index synchronously
# while a mouse button is held causes Godot 4 to re-evaluate GUI focus
# and deliver a synthetic second pressed=true event → immediate deselect.
if sel != prev_sel:
widget.set_meta("_sel", sel)
widget.pivot_offset = widget.size * 0.5
var tw := create_tween()
if sel:
tw.tween_property(widget, "scale", Vector2(1.12, 1.12), 0.14)
widget.z_index = 20
else:
tw.tween_property(widget, "scale", Vector2(1.0, 1.0), 0.10)
widget.z_index = 0
_tween_hand_card.call_deferred(widget, sel)
func _tween_hand_card(widget: Control, sel: bool) -> void:
if not is_instance_valid(widget): return
widget.pivot_offset = widget.size * 0.5
var tw := create_tween()
if sel:
tw.tween_property(widget, "scale", Vector2(1.12, 1.12), 0.14)
widget.z_index = 20
else:
tw.tween_property(widget, "scale", Vector2(1.0, 1.0), 0.10)
widget.z_index = 0
func _find_hand_card(iid: String) -> Dictionary:
@@ -1342,6 +1358,7 @@ func _on_state_updated(state: Dictionary) -> void:
_:
# Unit selected, sacrifice, ability — clear these on any state change
# because unit positions / health / eligibility may have changed.
print("This is firing")
_set_mode(Mode.NONE)
# Auto-draw.