TableTower/ui/main_selection.gd

110 lines
2.9 KiB
GDScript3
Raw Normal View History

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, at any depth (moving the
## group moves it).
func covers(tid: String) -> bool:
var g := m.data.group_of(tid)
while g != null:
if groups.has(g.id):
return true
g = m.data.get_group(g.parent)
return false
## True when a selected group carries this group (so moving it would move it twice).
func covers_group(gid: String) -> bool:
var g := m.data.get_group(gid)
var p := m.data.get_group(g.parent) if g else null
while p != null:
if groups.has(p.id):
return true
p = m.data.get_group(p.parent)
return false
## True when dragging this card should move the whole selection.
func moves_together(tid: String) -> bool:
return (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:
if not covers_group(gid):
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:
return describe_counts(cards, groups.size())
static func describe_counts(cards: int, n_groups: int) -> String:
var parts := PackedStringArray()
if cards > 0:
parts.append("%d %s" % [cards, "card" if cards == 1 else "cards"])
if n_groups > 0:
parts.append("%d %s" % [n_groups, "group" if n_groups == 1 else "groups"])
return ", ".join(parts)