TableTower/ui/main_groups.gd

321 lines
9.9 KiB
GDScript3
Raw Normal View History

class_name MainGroups
extends RefCounted
## Main's groups. 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.
## Card drags live in MainCardDrag (ui/main_card_drag.gd).
var m: Main
var _group_nodes: Dictionary = {} # group id -> GroupCard
var _hidden_cache: Variant = null # table id -> GroupCard, or null when stale
## Card drags (ui/main_card_drag.gd).
var card_drag: MainCardDrag
## The group frame lit as a drop target during a drag (tests read it).
var _drag_over: GroupCard:
get:
return card_drag.drag_over
func _init(p_main: Main) -> void:
m = p_main
card_drag = MainCardDrag.new(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:
# The outermost collapsed group stands in for everything inside it.
for g in m.data.groups:
var n := group_node(g.id)
if g.collapsed and n and m.data.hidden_by(g) == null:
for tid in m.data.tables_in(g.id):
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
# Outer frames first, so inner frames draw on top of them. Groups inside a
# collapsed group get no node.
var order := m.data.groups.duplicate()
order.sort_custom(func(a: TableGroup, b: TableGroup) -> bool: return m.data.depth_of(a) < m.data.depth_of(b))
for g: TableGroup in order:
if m.data.hidden_by(g) != null:
continue
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 m.data.tables_in(g.id):
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 m.data.tables_in(gid):
var c := m.card_for(tid)
if c:
c.position = m.data.get_table(tid).position
for inner in m.data.groups_in(gid):
var n := group_node(inner.id)
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)
# Selected groups go inside the new group; cards they carry move with them.
# An open group whose cards are all selected counts as selected too, so a
# box drawn over open groups nests them instead of emptying them.
var picked := m.selecting.groups.duplicate()
for group in m.data.groups:
var cards_inside := m.data.tables_in(group.id)
if not group.collapsed and not cards_inside.is_empty() and cards_inside.all(func(tid: String) -> bool: return m.selection.has(tid)):
picked[group.id] = true
var inner: Array[String] = []
for gid: String in picked:
if m.data.get_group(gid) and not _inside_picked(m.data.get_group(gid).parent, picked):
inner.append(gid)
ids = ids.filter(func(tid: String) -> bool: return not _inside_picked(m.data.group_of(tid).id if m.data.group_of(tid) else "", picked))
if ids.is_empty() and inner.is_empty():
m.toast("Select some cards or groups first: drag a box around them, or Ctrl-click their headers")
return null
m.undo_redo._begin_edit()
var outer := _common_parent(ids, inner)
var g := m.data.add_group(_free_group_name(), ids)
g.parent = outer
var rects: Array = []
for tid in ids:
var c := m.card_for(tid)
if c:
rects.append(Rect2(c.position, c.size))
for gid in inner:
m.data.set_group_parent(gid, g.id)
var n := group_node(gid)
rects.append(Rect2(n.position, n.size) if n else m.data.get_group(gid).rect())
g.fit_to(rects)
# Emptied groups stay, so they can be refilled or ungrouped.
m.clear_selection()
_groups_changed()
m.undo_redo._end_edit()
m.toast("Grouped %s as “%s”" % [m.selecting.describe_counts(ids.size(), inner.size()), g.name], true)
return g
## True when `gid` or a group around it is in `picked` (gid -> true).
func _inside_picked(gid: String, picked: Dictionary) -> bool:
var group := m.data.get_group(gid)
while group != null:
if picked.has(group.id):
return true
group = m.data.get_group(group.parent)
return false
## The group the new group goes in: the one every picked card and group
## already shares, else the spread itself ("").
func _common_parent(ids: Array[String], inner: Array[String]) -> String:
var parents := {}
for tid in ids:
var g := m.data.group_of(tid)
parents[g.id if g else ""] = true
for gid in inner:
parents[m.data.get_group(gid).parent] = true
return str(parents.keys()[0]) if parents.size() == 1 else ""
## 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 := m.data.tables_in(gid).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)