TableTower/ui/main_selection.gd
RobinFiredrake 9fcaf5c123 No more drag walls; drag box selects; middle/Space-drag pans; groups move with the selection
Cards, selections, new cards and inserted templates may sit anywhere,
including left of or above the old edge. In Build a drag on empty space
draws a selection box (Ctrl/Shift adds); middle-drag or Space+drag pans
(a plain drag still pans in Play). The box also picks up collapsed groups
it touches and expanded groups it covers, and dragging any selected card
or group moves the whole selection (MainSelection). Ctrl-click a group's
title to add it to the selection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:30:13 -06:00

88 lines
2.5 KiB
GDScript

class_name MainSelection
extends RefCounted
## Groups in the Build selection, alongside Main.selection's cards. A drag box
## picks up collapsed groups it touches and expanded groups it covers whole;
## dragging any selected card or group then moves everything selected together
## (groups can be moved as one even when they can't be grouped).
var m: Main
## gid -> true
var groups: Dictionary = {}
func _init(p_main: Main) -> void:
m = p_main
func select_in_rect(r: Rect2) -> void:
for n in m.board.groups.get_children() + m.board.cards.get_children():
var gc := n as GroupCard
if gc == null or not gc.visible or gc.group == null:
continue
var gr := Rect2(gc.position, gc.size)
if (gc.group.collapsed and gr.intersects(r)) or (not gc.group.collapsed and r.encloses(gr)):
set_group(gc.group.id, true)
func set_group(gid: String, on: bool) -> void:
if on:
groups[gid] = true
else:
groups.erase(gid)
var n := m.group_node(gid)
if n:
n.set_selected(on)
func toggle_group(gid: String) -> void:
set_group(gid, not groups.has(gid))
m.editing._update_selection_bar()
func clear() -> void:
for gid: String in groups.keys():
set_group(gid, false)
## True when a selected group carries this card (moving the group moves it).
func covers(tid: String) -> bool:
var g := m.data.group_of(tid)
return g != null and groups.has(g.id)
## True when dragging this card should move the whole selection.
func moves_together(tid: String) -> bool:
return m.mode == Main.Mode.BUILD and (m.selection.has(tid) or covers(tid)) \
and m.selection.size() + groups.size() > 1
## Moves every selected group by `delta`.
func move_groups(delta: Vector2) -> void:
for gid: String in groups:
m.move_group(gid, delta)
## A group's title was dragged: moves just it, or the whole selection when it's
## part of one.
func drag_group(gid: String, delta: Vector2) -> void:
if not groups.has(gid) or m.selection.size() + groups.size() < 2:
m.move_group(gid, delta)
return
move_groups(delta)
for tid: String in m.selection:
if not covers(tid):
var c := m.card_for(tid)
if c:
c.position += delta
c.table.position = c.position
m.board.geometry_rev += 1
## "3 cards, 1 group" for the selection bar.
func describe(cards: int) -> String:
var parts := PackedStringArray()
if cards > 0:
parts.append("%d %s" % [cards, "card" if cards == 1 else "cards"])
if not groups.is_empty():
parts.append("%d %s" % [groups.size(), "group" if groups.size() == 1 else "groups"])
return ", ".join(parts)