TableTower/ui/table_card_input.gd
tachyonwolf 2241937eb2 Build: clicking a card's empty body selects the card
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 09:03:20 -06:00

98 lines
3.5 KiB
GDScript

class_name TableCardInput
extends RefCounted
## Mouse input on a card: the context menu and dragging by the header.
var card: TableCard
func _init(p_card: TableCard) -> void:
card = p_card
func gui_input(ev: InputEvent) -> void:
# Clicks on the card body shouldn't pan the board. The wheel zooms the
# board, except over a list that scrolls: that list keeps it, even at its
# ends, so scrolling past the last result doesn't start zooming.
var mb := ev as InputEventMouseButton
if mb and mb.button_index == MOUSE_BUTTON_LEFT:
# In Build, a click on the card's own body (no control took it) selects the card.
# (A click on a result row already selected that result; leave it.)
if mb.pressed and card.main.mode == Main.Mode.BUILD and not _is_inspected():
card.main.select_card(card.table.id)
card.accept_event()
elif mb and _wheel_in_list(mb):
card.accept_event()
elif open_menu_on(mb):
card.accept_event()
## True when the inspector already shows this card or one of its results.
func _is_inspected() -> bool:
var selection := card.main.inspector_selection()
return str(selection.get("tid", "")) == card.table.id and str(selection.get("kind", "")) != "pipe"
func _wheel_in_list(mb: InputEventMouseButton) -> bool:
if mb.button_index != MOUSE_BUTTON_WHEEL_UP and mb.button_index != MOUSE_BUTTON_WHEEL_DOWN:
return false
var list := card.list_scroll
if mb.ctrl_pressed or mb.shift_pressed or not is_instance_valid(list) or not list.get_v_scroll_bar().visible:
return false
return list.get_global_rect().has_point(card.get_global_mouse_position())
## The grip, then the zoomed-out shield, stay on top of the rebuilt content.
func raise_covers() -> void:
card.move_child(card.grip, -1)
card.move_child(card.shield, -1)
card.shield.set_name_tip(Formatter.strip_tags(card.table.name))
if card.table.kind == RollTable.KIND_VARIABLE:
card.shield.on_roll = Callable()
## Right-click: the card's actions menu (Actions.card). True when it opened.
func open_menu_on(mb: InputEventMouseButton) -> bool:
if mb == null:
return false
if mb == null or mb.button_index != MOUSE_BUTTON_RIGHT or not mb.pressed:
return false
card.main.details().hide_for()
Actions.popup_at(card.main, Actions.card(card.main, card.table), mb.global_position)
return true
## Dragging the header moves the card (and, in Build, every selected card).
## In Build, Ctrl- or Shift-click adds the card to the selection or takes it
## out; dropping a card into a group frame adds it to that group (Main).
func on_header_input(ev: InputEvent, header: Control) -> void:
var mb := ev as InputEventMouseButton
if open_menu_on(mb):
header.accept_event()
return
if mb and mb.button_index == MOUSE_BUTTON_LEFT:
if mb.pressed:
card.main.details().hide_for()
var build := card.main.mode == Main.Mode.BUILD
if build and (mb.ctrl_pressed or mb.shift_pressed or mb.meta_pressed):
card.main.toggle_selected(card.table.id)
header.accept_event()
return
if build and not card.main.is_selected(card.table.id):
card.main.clear_selection()
card._dragging = true
card._drag_offset = card.main.board.world_mouse() - card.position
card.get_parent().move_child(card, -1)
card.main.begin_card_drag(card)
elif card._dragging:
card._dragging = false
card.main.end_card_drag(card)
else:
card.main.mark_dirty()
card.skin.apply_style()
header.accept_event()
return
var mm := ev as InputEventMouseMotion
if mm and card._dragging:
var p := card.main.board.world_mouse() - card._drag_offset
card.main.drag_card(card, p.round())
header.accept_event()