TableTower/ui/group_layer.gd
RobinFiredrake 5d427d3856 Nested groups: groups can hold groups
TableGroup.parent (JSON "parent") puts a group inside another. Grouping a
selection that includes groups nests them (the new group goes in the group
they all share). A collapsed group hides every card and group inside it and
stands in for them on threads; moving, deleting, search, focus (opens every
collapsed group around a card), packing, undo and templates all work
through the nesting. Ungroup hands the contents to the outer group.

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

45 lines
1.5 KiB
GDScript

class_name GroupLayer
extends Control
## Holds the frames of expanded groups, behind the pipes and the cards, and
## keeps each frame fitted around its member cards. A group whose member is
## being dragged is frozen, so the card can be dragged out of the frame.
var main: Main
## Group ids that don't refit right now (a member is being dragged).
var frozen: Dictionary = {}
func _init() -> void:
mouse_filter = MOUSE_FILTER_IGNORE
# Controls only draw inside their own rect, so cover the whole world.
size = Vector2(BoardView.WORLD_SIZE, BoardView.WORLD_SIZE)
func _process(_delta: float) -> void:
if main == null or main.data == null:
return
refit()
## Fits every expanded, unfrozen group's frame around its visible members and
## inner groups, innermost first so outer frames fit the inner ones' new size.
func refit() -> void:
var order := main.data.groups.duplicate()
if order.size() > 1:
order.sort_custom(func(a: TableGroup, b: TableGroup) -> bool: return main.data.depth_of(a) > main.data.depth_of(b))
for g: TableGroup in order:
if g.collapsed or frozen.has(g.id):
continue
var rects: Array = []
for tid in g.members:
var c := main.card_for(tid)
if c and c.visible and c.size.x > 0.0:
rects.append(Rect2(c.position, c.size))
for inner in main.data.child_groups(g.id):
var n := main.group_node(inner.id)
if n and n.visible:
rects.append(Rect2(n.position, n.size) if inner.collapsed else inner.rect())
if g.fit_to(rects):
var n := main.group_node(g.id)
if n:
n.sync_rect()