TableTower/ui/main_groups.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

371 lines
11 KiB
GDScript

class_name MainGroups
extends RefCounted
## Main's groups and card drags. A group is a frame around a block of cards
## (TableGroup). Expanded, its GroupCard sits in board.groups behind the pipes;
## collapsed, it sits with the cards and its members are hidden. Groups are
## presentation only: rolling, lighting and the tray never look at them.
var m: Main
var _group_nodes: Dictionary = {} # group id -> GroupCard
var _hidden_cache: Variant = null # table id -> GroupCard, or null when stale
## Ids of the cards being dragged and the group the pointer is over (Build).
## Dragging moves the whole Build selection (cards and groups).
var _drag_all := false
var _drag_set: Array[String] = []
var _drag_from: Dictionary = {} # table id -> its position when the drag began
var _drag_over: GroupCard
func _init(p_main: Main) -> void:
m = p_main
func group_node(gid: String) -> GroupCard:
var n: GroupCard = _group_nodes.get(gid)
return n if is_instance_valid(n) else null
## Table id -> the GroupCard of the collapsed group hiding it.
func hidden_groups() -> Dictionary:
if _hidden_cache == null:
var out := {}
if m.data:
for g in m.data.groups:
var n := group_node(g.id)
if g.collapsed and n:
for tid in g.members:
out[tid] = n
_hidden_cache = out
return _hidden_cache
## Rebuilds every group's node, in the right layer, and hides the members of
## collapsed groups.
func _rebuild_groups() -> void:
for n in _group_nodes.values():
if is_instance_valid(n):
if (n as Node).get_parent():
(n as Node).get_parent().remove_child(n)
(n as Node).queue_free()
_group_nodes.clear()
_hidden_cache = null
if m.data == null or m.board == null:
return
for g in m.data.groups:
var n := GroupCard.new()
if g.collapsed:
m.board.cards.add_child(n)
else:
m.board.groups.add_child(n)
n.setup(m, g)
_group_nodes[g.id] = n
_apply_group_visibility()
## After a membership or group change: rebuild the group nodes and keep the
## rest in step.
func _groups_changed() -> void:
_rebuild_groups()
## Builds the cards that are on show and frees the ones hidden in collapsed
## groups, so a big board with most of it collapsed stays light.
func _apply_group_visibility() -> void:
_hidden_cache = null
var hidden := hidden_groups()
var built := false
for t in m.data.tables:
var c: TableCard = m.cards.get(t.id)
if hidden.has(t.id):
if c:
m.selection.erase(t.id)
m.cards.erase(t.id)
# Hidden until it goes at the end of the frame (a deferred
# call of its own may still run).
c.hide()
c.queue_free()
elif c == null:
c = TableCard.new()
m.board.cards.add_child(c)
c.setup(m, t)
m.cards[t.id] = c
built = true
if built:
# Collapsed group cards stay on top of the table cards.
for g in m.data.groups:
var n := group_node(g.id)
if g.collapsed and n and n.get_parent() == m.board.cards:
m.board.cards.move_child(n, -1)
m.editing._update_selection_bar()
## "tid:eid" keys of a group's lit results, in roll order.
func lit_keys_for_group(g: TableGroup) -> Array:
var members := {}
for tid in g.members:
members[tid] = true
var out: Array = []
for k: String in m.lit_texts:
var i := k.rfind(":")
if i > 0 and members.has(k.substr(0, i)):
out.append(k)
return out
func _refresh_group_lits() -> void:
for g in m.data.groups:
var n := group_node(g.id)
if n and g.collapsed:
n.refresh_lit(lit_keys_for_group(g))
func toggle_group(gid: String) -> void:
var g := m.data.get_group(gid)
if g == null:
return
var compact := Rect2()
var blocks: Array = []
var n := group_node(gid)
if g.collapsed and n:
compact = Rect2(n.position, n.size)
blocks = m.nav._layout_blocks(gid)
g.collapsed = not g.collapsed
m.details().hide_for()
_groups_changed()
m.mark_dirty("", false)
if not g.collapsed:
var room := m.nav._make_room_for(g, compact, blocks)
if int(room["moved"]) > 0:
m.toast("Opened %s and moved %d %s to make room" % [Formatter.strip_tags(g.name), room["moved"],
"block" if int(room["moved"]) == 1 else "blocks"], true)
func set_group_collapsed(gid: String, collapsed: bool) -> void:
var g := m.data.get_group(gid)
if g and g.collapsed != collapsed:
toggle_group(gid)
## Moves a group and its members (dragging the title bar).
func move_group(gid: String, delta: Vector2) -> void:
var g := m.data.get_group(gid)
if g == null:
return
m.data.move_group(gid, delta)
for tid in g.members:
var c := m.card_for(tid)
if c:
c.position = m.data.get_table(tid).position
var n := group_node(gid)
if n:
n.sync_rect()
## "Group 1", "Group 2"… whichever isn't used.
func _free_group_name() -> String:
var used := {}
for g in m.data.groups:
used[g.name] = true
var k := 1
while used.has("Group %d" % k):
k += 1
return "Group %d" % k
## Ctrl+G: groups the selected cards (taking them out of any other group).
func group_selection() -> TableGroup:
var ids: Array[String] = []
for t in m.data.tables:
if m.selection.has(t.id):
ids.append(t.id)
if ids.is_empty():
m.toast("Select some cards first: Ctrl- or Shift-click their headers, or drag a box around them")
return null
m.undo_redo._begin_edit()
var g := m.data.add_group(_free_group_name(), ids)
var rects: Array = []
for tid in ids:
var c := m.card_for(tid)
rects.append(Rect2(c.position, c.size))
g.fit_to(rects)
# Groups don't nest: emptied groups stay, so they can be refilled or ungrouped.
m.clear_selection()
_groups_changed()
m.undo_redo._end_edit()
m.toast("Grouped %d %s as “%s”" % [ids.size(), "card" if ids.size() == 1 else "cards", g.name], true)
return g
## Removes the group; its cards stay where they are.
func ungroup(gid: String) -> void:
var g := m.data.get_group(gid)
if g == null:
return
m.undo_redo._begin_edit()
m.data.ungroup(gid)
_groups_changed()
m.undo_redo._end_edit()
m.toast("Ungrouped “%s”" % Formatter.strip_tags(g.name), true)
func confirm_delete_group(gid: String) -> void:
var g := m.data.get_group(gid)
if g == null:
return
var n := g.members.size()
m._add_modal(ModalCard.confirm("Delete group",
"Delete “%s” and the %d %s in it? Threads from other cards into them are removed too. You can undo this from the message that follows." % [Formatter.strip_tags(g.name), n, "card" if n == 1 else "cards"],
"Delete group and cards", func() -> void: delete_group(gid), true))
## Deletes a group and every card in it (undo from the toast).
func delete_group(gid: String) -> void:
var g := m.data.get_group(gid)
if g == null:
return
m.undo_redo._begin_edit()
var gname := Formatter.strip_tags(g.name)
var gone := m.data.delete_group(gid)
for tid in gone:
m.local_state.forget_table_state(tid)
m.selection.erase(tid)
var card := m.card_for(tid)
m.cards.erase(tid)
if card:
card.get_parent().remove_child(card)
card.queue_free()
for c in m.cards.values():
(c as TableCard).rebuild()
_groups_changed()
m.undo_redo._end_edit()
m.toast("Deleted “%s” and its %d %s" % [gname, gone.size(), "card" if gone.size() == 1 else "cards"], true)
## The group details editor (Build): a card-styled dialog with the text.
func edit_group_details(gid: String) -> void:
var g := m.data.get_group(gid)
if g == null:
return
var d := ModalCard.new("Details for %s" % Formatter.strip_tags(g.name), 520.0)
var te := TextEdit.new()
te.text = g.details
te.placeholder_text = "Shown when hovering the group's title (BBCode)"
te.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY
te.custom_minimum_size = Vector2(0, 120)
ModalCard.style_text_edit(te)
d.body.add_child(te)
d.add_button("Cancel", "plain", d.close)
d.add_button("Save", "primary", func() -> void:
m.undo_redo._begin_edit()
g.details = te.text.strip_edges()
d.close()
var n := group_node(gid)
if n:
n.rebuild()
m.undo_redo._end_edit()
)
m._add_modal(d)
## The expanded group frame containing a world point (the smallest, if they overlap).
func _frame_at(p: Vector2) -> TableGroup:
var best: TableGroup = null
for g in m.data.groups:
if g.collapsed or not g.rect().has_point(p):
continue
if best == null or g.rect().get_area() < best.rect().get_area():
best = g
return best
# ----- Card drags (TableCard calls these) -----
func begin_card_drag(card: TableCard) -> void:
if m.mode == Main.Mode.BUILD and card and card.table:
m.select_card(card.table.id)
m.undo_redo._begin_edit()
_drag_set.clear()
_drag_all = m.selecting.moves_together(card.table.id)
if _drag_all:
# Cards in selected groups move with their group.
for tid: String in m.selection:
if m.cards.has(tid) and not m.selecting.covers(tid):
_drag_set.append(tid)
else:
_drag_set.append(card.table.id)
_drag_from.clear()
# The frames of the dragged cards' groups hold still, so a card can leave them.
for tid in _drag_set:
_drag_from[tid] = m.data.get_table(tid).position
var g := m.data.group_of(tid)
if g:
m.board.groups.frozen[g.id] = true
## Moves the dragged card (and the rest of a multi-selection) so `card` lands at `to`.
func drag_card(card: TableCard, to: Vector2) -> void:
var delta := to - card.position
if _drag_all:
for tid in _drag_set:
var c := m.card_for(tid)
c.position += delta
c.table.position = c.position
m.selecting.move_groups(delta)
else:
card.position = to
card.table.position = to
if m.mode == Main.Mode.BUILD:
var over := _frame_at(card.position + card.size / 2.0)
var node := group_node(over.id) if over else null
if node != _drag_over:
if is_instance_valid(_drag_over):
_drag_over.set_drop_target(false)
_drag_over = node
if node:
node.set_drop_target(true)
## Drop: in Build, a card dropped in a group frame joins it, and one dropped
## outside its own (held) frame leaves it.
func end_card_drag(_card: TableCard) -> void:
if is_instance_valid(_drag_over):
_drag_over.set_drop_target(false)
_drag_over = null
var joined := {}
var left := 0
if m.mode == Main.Mode.BUILD:
for tid in _drag_set:
var c := m.card_for(tid)
if c == null:
continue
var center := c.position + c.size / 2.0
var g0 := m.data.group_of(tid)
var target := _frame_at(center)
if target != null and target != g0:
m.data.add_to_group(tid, target.id)
joined[target.name] = int(joined.get(target.name, 0)) + 1
elif target == null and g0 != null and not g0.rect().has_point(center):
m.data.remove_from_group(tid)
left += 1
m.board.groups.frozen.clear()
var moved := false
for tid in _drag_set:
var t := m.data.get_table(tid)
if t and t.position != _drag_from.get(tid, t.position):
moved = true
_drag_set.clear()
_drag_from.clear()
if not joined.is_empty() or left > 0:
_groups_changed()
var parts := PackedStringArray()
for gname: String in joined:
parts.append("added %d to “%s”" % [joined[gname], Formatter.strip_tags(gname)])
if left > 0:
parts.append("took %d out of %s" % [left, "its group" if left == 1 else "their groups"])
var msg := ", ".join(parts)
m.undo_redo._end_edit()
m.toast(msg.substr(0, 1).to_upper() + msg.substr(1), true)
return
# A click on a header without a drag isn't an edit.
m.mark_dirty("", moved)