Groups get an optional colour (Colour menu on the group); uncoloured groups use the theme token GROUP. The group frame's border matches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
462 lines
16 KiB
GDScript
462 lines
16 KiB
GDScript
class_name GroupCard
|
|
extends MarginContainer
|
|
## A group on the board (see TableGroup). Expanded, it's a frame a little
|
|
## darker than the felt, drawn behind its member cards, with a title bar
|
|
## (chevron, name, member count, details pop-up; in Build a ⋯ menu). Dragging
|
|
## the title bar moves the group and its members.
|
|
##
|
|
## Collapsed, the members are hidden and the group shows as one compact card
|
|
## (drawn as a little stack) with its name, "N cards" and the last rolled
|
|
## results of any lit members. Pipes into members then attach to its inlet, and
|
|
## pipes out of members leave from its right edge (PipeLayer asks anchor_for).
|
|
##
|
|
## Main keeps expanded groups in BoardView.groups (behind the pipes) and
|
|
## collapsed ones in BoardView.cards, with the table cards.
|
|
|
|
const INLET_Y := 30.0
|
|
## Most lit results a collapsed group lists.
|
|
const MAX_LIT := 6
|
|
|
|
var main: Main
|
|
var group: TableGroup
|
|
## "tid:eid" -> row, for the lit results a collapsed group lists.
|
|
var lit_rows: Dictionary = {}
|
|
## The Build-mode ⋯ menu, or null.
|
|
var menu_button: MenuButton
|
|
|
|
var _card: PanelContainer
|
|
var _header: Control
|
|
var _lit_keys: Array = []
|
|
var _rolling := false
|
|
var _drop := false
|
|
var _dragging := false
|
|
var _drag_last := Vector2.ZERO
|
|
var _frame_box: StyleBoxFlat
|
|
var _frame_drop: StyleBoxFlat
|
|
var _band_box: StyleBoxFlat
|
|
var _stack_box: StyleBoxFlat
|
|
var _card_box: StyleBoxFlat
|
|
var _card_rolling: StyleBoxFlat
|
|
var _card_selected: StyleBoxFlat
|
|
## In the Build selection (MainSelection).
|
|
var _selected := false
|
|
## The zoomed-out cover of a collapsed group (CardShield); null when expanded.
|
|
var shield: CardShield
|
|
|
|
|
|
func setup(p_main: Main, p_group: TableGroup) -> void:
|
|
main = p_main
|
|
group = p_group
|
|
mouse_filter = MOUSE_FILTER_IGNORE
|
|
theme = Style.card_theme()
|
|
_frame_box = Style.box(Color(0, 0, 0, 0.16), 16)
|
|
Style.bordered(_frame_box, 1, Color(1, 1, 1, 0.08))
|
|
_frame_drop = Style.box(Color(Style.ACCENT, 0.08), 16)
|
|
Style.bordered(_frame_drop, 2, Style.ACCENT)
|
|
_band_box = Style.box(Color(0, 0, 0, 0.16), 0)
|
|
_band_box.corner_radius_top_left = 16
|
|
_band_box.corner_radius_top_right = 16
|
|
_stack_box = Style.bordered(Style.box(Style.CARD_SHADE, 10), 1, Color(0, 0, 0, 0.12))
|
|
_card_box = Style.box(Style.CARD, 10)
|
|
_card_box.shadow_color = Color(0, 0, 0, 0.32)
|
|
_card_box.shadow_size = 16
|
|
_card_box.shadow_offset = Vector2(0, 8)
|
|
_card_rolling = _card_box.duplicate() as StyleBoxFlat
|
|
Style.bordered(_card_rolling, 3, Style.ACCENT)
|
|
_card_rolling.set_expand_margin_all(3)
|
|
_card_rolling.shadow_color = Color(Style.ACCENT, 0.45)
|
|
_card_rolling.shadow_size = 26
|
|
_card_rolling.shadow_offset = Vector2.ZERO
|
|
_card_selected = _card_box.duplicate() as StyleBoxFlat
|
|
Style.bordered(_card_selected, 3, Style.TRIM)
|
|
_card_selected.set_expand_margin_all(4)
|
|
minimum_size_changed.connect(func() -> void: _shrink.call_deferred())
|
|
rebuild()
|
|
|
|
|
|
func rebuild() -> void:
|
|
for c in get_children():
|
|
remove_child(c)
|
|
c.queue_free()
|
|
lit_rows.clear()
|
|
menu_button = null
|
|
shield = null
|
|
_card = null
|
|
_header = null
|
|
position = group.position
|
|
if group.collapsed:
|
|
_lit_keys = main.lit_keys_for_group(group)
|
|
_build_compact()
|
|
else:
|
|
_lit_keys = []
|
|
_build_frame()
|
|
queue_redraw()
|
|
|
|
|
|
## Follows the model's rect after the board refits the frame.
|
|
func sync_rect() -> void:
|
|
position = group.position
|
|
if not group.collapsed:
|
|
custom_minimum_size = group.size
|
|
size = group.size
|
|
queue_redraw()
|
|
|
|
|
|
func _shrink() -> void:
|
|
if group == null:
|
|
return
|
|
if group.collapsed:
|
|
size = Vector2(card_width(), 0)
|
|
else:
|
|
size = group.size
|
|
|
|
|
|
func card_width() -> float:
|
|
return TableCard.BUILD_WIDTH if main.mode == Main.Mode.BUILD else TableCard.WIDTH
|
|
|
|
|
|
func _draw() -> void:
|
|
if group == null:
|
|
return
|
|
if group.collapsed:
|
|
# Two card edges peeking out behind: a stack of cards.
|
|
for k in [2, 1]:
|
|
var off := Vector2(5.0 * k, 5.0 * k)
|
|
draw_style_box(_stack_box, Rect2(off, size))
|
|
return
|
|
var r := Rect2(Vector2.ZERO, size)
|
|
# The border in the group's colour, matching its members' corner marks.
|
|
Style.bordered(_frame_box, 2, Color(GroupMark.color_of(group), 0.55))
|
|
draw_style_box(_frame_drop if _drop or _selected else _frame_box, r)
|
|
draw_style_box(_band_box, Rect2(0, 0, size.x, TableGroup.TITLE_H))
|
|
|
|
|
|
# ---------- Expanded frame ----------
|
|
|
|
func _build_frame() -> void:
|
|
custom_minimum_size = group.size
|
|
size = group.size
|
|
var col := VBoxContainer.new()
|
|
col.mouse_filter = MOUSE_FILTER_IGNORE
|
|
col.add_theme_constant_override("separation", 0)
|
|
add_child(col)
|
|
var bar := PanelContainer.new()
|
|
bar.add_theme_stylebox_override("panel", Style.box(Color(0, 0, 0, 0), 0, [10, 0, 10, 0]))
|
|
bar.custom_minimum_size = Vector2(0, TableGroup.TITLE_H)
|
|
bar.mouse_filter = MOUSE_FILTER_STOP
|
|
bar.mouse_default_cursor_shape = CURSOR_MOVE
|
|
bar.gui_input.connect(_on_title_input.bind(bar))
|
|
bar.mouse_entered.connect(_show_details.bind(bar))
|
|
col.add_child(bar)
|
|
_header = bar
|
|
var h := TableCard.hbox(8)
|
|
h.size_flags_vertical = SIZE_SHRINK_CENTER
|
|
bar.add_child(h)
|
|
var chevron := ChevronButton.new()
|
|
chevron.use_felt_colors()
|
|
chevron.open = true
|
|
chevron.tooltip_text = "Collapse %s into one card" % Formatter.strip_tags(group.name)
|
|
chevron.size_flags_vertical = SIZE_SHRINK_CENTER
|
|
chevron.pressed.connect(func() -> void: main.toggle_group(group.id))
|
|
h.add_child(chevron)
|
|
var build := main.mode == Main.Mode.BUILD
|
|
if build:
|
|
h.add_child(_name_edit(true))
|
|
else:
|
|
var title := Style.make_label(Formatter.strip_tags(group.name), Style.display(true), 20, Style.ON_BG)
|
|
title.size_flags_vertical = SIZE_SHRINK_CENTER
|
|
title.size_flags_horizontal = SIZE_EXPAND_FILL
|
|
title.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
|
|
title.custom_minimum_size = Vector2(30, 0)
|
|
title.mouse_filter = MOUSE_FILTER_IGNORE
|
|
h.add_child(title)
|
|
var count := Style.make_label(count_text(), Style.body(true), 13, Style.ON_BG_MUTED)
|
|
count.size_flags_vertical = SIZE_SHRINK_CENTER
|
|
count.mouse_filter = MOUSE_FILTER_IGNORE
|
|
if group.details.strip_edges() != "":
|
|
count.text += " " + RichText.INFO_MARK
|
|
h.add_child(count)
|
|
if build:
|
|
h.add_child(_menu_button(true))
|
|
|
|
|
|
func count_text() -> String:
|
|
var text := MainSelection.describe_counts(main.data.tables_in(group.id).size(), main.data.groups_in(group.id).size() - 1)
|
|
return text if text != "" else "0 cards"
|
|
|
|
|
|
func set_drop_target(on: bool) -> void:
|
|
if _drop != on:
|
|
_drop = on
|
|
queue_redraw()
|
|
|
|
|
|
# ---------- Collapsed card ----------
|
|
|
|
func _build_compact() -> void:
|
|
custom_minimum_size = Vector2(card_width(), 0)
|
|
_shrink.call_deferred()
|
|
_card = PanelContainer.new()
|
|
_card.mouse_filter = MOUSE_FILTER_STOP
|
|
_card.add_theme_stylebox_override("panel", _card_style())
|
|
_card.gui_input.connect(func(ev: InputEvent) -> void:
|
|
# Clicks on the card body shouldn't pan the board.
|
|
var mb := ev as InputEventMouseButton
|
|
if mb and mb.button_index == MOUSE_BUTTON_LEFT:
|
|
_card.accept_event()
|
|
elif _open_menu_on(mb):
|
|
_card.accept_event()
|
|
)
|
|
add_child(_card)
|
|
var col := TableCard.vbox(0)
|
|
_card.add_child(col)
|
|
|
|
var header := PanelContainer.new()
|
|
var hs := Style.box(Color(0, 0, 0, 0), 0, [12, 10, 10, 9])
|
|
hs.border_width_bottom = 1
|
|
hs.border_color = Style.RULE
|
|
header.add_theme_stylebox_override("panel", hs)
|
|
header.mouse_filter = MOUSE_FILTER_STOP
|
|
header.mouse_default_cursor_shape = CURSOR_MOVE
|
|
header.gui_input.connect(_on_title_input.bind(header))
|
|
header.mouse_entered.connect(_show_details.bind(header))
|
|
col.add_child(header)
|
|
_header = header
|
|
var row := TableCard.hbox(8)
|
|
header.add_child(row)
|
|
var chevron := ChevronButton.new()
|
|
chevron.open = false
|
|
chevron.tooltip_text = "Expand %s" % Formatter.strip_tags(group.name)
|
|
chevron.size_flags_vertical = SIZE_SHRINK_CENTER
|
|
chevron.pressed.connect(func() -> void: main.toggle_group(group.id))
|
|
row.add_child(chevron)
|
|
var build := main.mode == Main.Mode.BUILD
|
|
if build:
|
|
row.add_child(_name_edit(false))
|
|
else:
|
|
var titles := TableCard.vbox(3)
|
|
titles.size_flags_horizontal = SIZE_EXPAND_FILL
|
|
titles.mouse_filter = MOUSE_FILTER_IGNORE
|
|
var title := Style.make_label(Formatter.strip_tags(group.name), Style.display(true), 20, Style.INK)
|
|
title.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
|
title.mouse_filter = MOUSE_FILTER_IGNORE
|
|
titles.add_child(title)
|
|
var sub := Style.make_label("Group · " + count_text(), Style.body(true), 12, Style.MUTED)
|
|
if group.details.strip_edges() != "":
|
|
sub.text += " " + RichText.INFO_MARK
|
|
sub.mouse_filter = MOUSE_FILTER_IGNORE
|
|
titles.add_child(sub)
|
|
row.add_child(titles)
|
|
var badge := _stack_badge()
|
|
row.add_child(badge)
|
|
if build:
|
|
row.add_child(_menu_button(false))
|
|
|
|
shield = CardShield.new(main, _on_title_input, _open_menu_on)
|
|
shield.set_name_tip(Formatter.strip_tags(group.name))
|
|
_card.add_child.call_deferred(shield)
|
|
|
|
var pad := MarginContainer.new()
|
|
pad.mouse_filter = MOUSE_FILTER_PASS
|
|
pad.add_theme_constant_override("margin_top", 6)
|
|
pad.add_theme_constant_override("margin_bottom", 8)
|
|
col.add_child(pad)
|
|
var list := TableCard.vbox(2)
|
|
pad.add_child(list)
|
|
var shown := _lit_keys.slice(maxi(0, _lit_keys.size() - MAX_LIT))
|
|
if _lit_keys.size() > shown.size():
|
|
var more := Style.make_label("+%d earlier results" % (_lit_keys.size() - shown.size()), Style.body_italic(true), 13, Style.MUTED)
|
|
more.add_theme_constant_override("line_spacing", 0)
|
|
var mm := MarginContainer.new()
|
|
mm.add_theme_constant_override("margin_left", 14)
|
|
mm.add_child(more)
|
|
list.add_child(mm)
|
|
for key: String in shown:
|
|
list.add_child(_lit_row(key))
|
|
if shown.is_empty():
|
|
var names := PackedStringArray()
|
|
var inside := main.data.tables_in(group.id)
|
|
for tid in inside.slice(0, 3):
|
|
var t := main.data.get_table(tid)
|
|
if t:
|
|
names.append(Formatter.strip_tags(t.name))
|
|
var txt := ", ".join(names)
|
|
if inside.size() > 3:
|
|
txt += " and %d more" % (inside.size() - 3)
|
|
var note := Style.make_label(txt if txt != "" else "No cards yet", Style.body_italic(true), 14, Style.MUTED)
|
|
note.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
|
var nm := MarginContainer.new()
|
|
nm.mouse_filter = MOUSE_FILTER_PASS
|
|
nm.add_theme_constant_override("margin_left", 14)
|
|
nm.add_theme_constant_override("margin_right", 14)
|
|
nm.add_theme_constant_override("margin_top", 2)
|
|
nm.add_theme_constant_override("margin_bottom", 2)
|
|
nm.add_child(note)
|
|
list.add_child(nm)
|
|
|
|
|
|
## A small pile-of-cards mark with the member count, at the header's right.
|
|
func _stack_badge() -> Control:
|
|
var c := Control.new()
|
|
c.custom_minimum_size = Vector2(30, 28)
|
|
c.size_flags_vertical = SIZE_SHRINK_CENTER
|
|
c.mouse_filter = MOUSE_FILTER_IGNORE
|
|
c.tooltip_text = count_text()
|
|
c.draw.connect(func() -> void:
|
|
var back := Style.bordered(Style.box(Style.CARD_SHADE, 3), 1, Style.TRIM_DARK)
|
|
var front := Style.bordered(Style.box(Style.BG, 3), 1, Style.BG_DEEP)
|
|
c.draw_style_box(back, Rect2(8, 2, 18, 22))
|
|
c.draw_style_box(front, Rect2(3, 6, 18, 22))
|
|
var s := str(main.data.tables_in(group.id).size())
|
|
var f := Style.display(true)
|
|
var w := f.get_string_size(s, HORIZONTAL_ALIGNMENT_LEFT, -1, 14).x
|
|
c.draw_string(f, Vector2(12.0 - w / 2.0, 22), s, HORIZONTAL_ALIGNMENT_LEFT, -1, 14, Style.ACCENT)
|
|
)
|
|
return c
|
|
|
|
|
|
func _lit_row(key: String) -> Control:
|
|
var i := key.rfind(":")
|
|
var tid := key.substr(0, i)
|
|
var eid := key.substr(i + 1)
|
|
var t := main.data.get_table(tid)
|
|
var row := PanelContainer.new()
|
|
row.mouse_filter = MOUSE_FILTER_PASS
|
|
row.add_theme_stylebox_override("panel", Style.box(Style.LIT, 0, [14, 5, 22, 6]))
|
|
var v := TableCard.vbox(1)
|
|
row.add_child(v)
|
|
var cap := Style.make_label(Formatter.strip_tags(t.name if t else tid).to_upper(), Style.caps(), 10, Style.TRIM_DARK)
|
|
cap.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
|
|
cap.custom_minimum_size = Vector2(20, 0)
|
|
cap.mouse_filter = MOUSE_FILTER_IGNORE
|
|
v.add_child(cap)
|
|
var text := RichText.label(RichText.decorate(main.lit_text(tid, eid)), 15, Style.INK, true)
|
|
text.size_flags_horizontal = SIZE_EXPAND_FILL
|
|
RichText.hook_hover(text, main)
|
|
v.add_child(text)
|
|
row.mouse_entered.connect(func() -> void: main.details().show_entry(row, tid, eid))
|
|
lit_rows[key] = row
|
|
return row
|
|
|
|
|
|
## Re-renders a collapsed group when the lit results of its members changed.
|
|
func refresh_lit(keys: Array) -> void:
|
|
if group.collapsed and keys != _lit_keys:
|
|
rebuild()
|
|
|
|
|
|
func _card_style() -> StyleBox:
|
|
return _card_rolling if _rolling else (_card_selected if _selected else _card_box)
|
|
|
|
|
|
func set_selected(on: bool) -> void:
|
|
_selected = on
|
|
if is_instance_valid(_card):
|
|
_card.add_theme_stylebox_override("panel", _card_style())
|
|
queue_redraw()
|
|
|
|
|
|
func set_rolling(on: bool) -> void:
|
|
_rolling = on
|
|
if is_instance_valid(_card):
|
|
_card.add_theme_stylebox_override("panel", _card_style())
|
|
|
|
|
|
# ---------- Shared parts ----------
|
|
|
|
func _name_edit(on_felt: bool) -> LineEdit:
|
|
var le := LineEdit.new()
|
|
le.text = group.name
|
|
le.placeholder_text = "Group name"
|
|
le.size_flags_horizontal = SIZE_EXPAND_FILL
|
|
le.size_flags_vertical = SIZE_SHRINK_CENTER
|
|
le.custom_minimum_size = Vector2(40, 0)
|
|
le.add_theme_font_override("font", Style.display(true))
|
|
le.add_theme_font_size_override("font_size", 18)
|
|
if on_felt:
|
|
le.add_theme_color_override("font_color", Style.ON_BG)
|
|
le.add_theme_color_override("caret_color", Style.ON_BG)
|
|
le.add_theme_color_override("font_placeholder_color", Style.ON_BG_MUTED)
|
|
le.add_theme_stylebox_override("normal", Style.bordered(Style.box(Color(0, 0, 0, 0.12), 6, [7, 3, 7, 3]), 1, Color(1, 1, 1, 0.14)))
|
|
le.add_theme_stylebox_override("focus", Style.bordered(Style.box(Color(0, 0, 0, 0.25), 6, [7, 3, 7, 3]), 1, Style.ACCENT))
|
|
else:
|
|
le.add_theme_stylebox_override("normal", Style.bordered(Style.box(Color.WHITE, 6, [7, 3, 7, 3]), 1, Style.RULE))
|
|
le.tooltip_text = "Rename this group"
|
|
le.text_changed.connect(func(s: String) -> void:
|
|
group.name = s
|
|
main.mark_dirty()
|
|
)
|
|
return le
|
|
|
|
|
|
func _menu_button(on_felt: bool) -> MenuButton:
|
|
var mb := Style.overflow_button("Group options", on_felt)
|
|
menu_button = mb
|
|
var pm := mb.get_popup()
|
|
Actions.fill(pm, Actions.group(main, group))
|
|
pm.about_to_popup.connect(func() -> void: Actions.fill(pm, Actions.group(main, group)))
|
|
return mb
|
|
|
|
|
|
func _show_details(owner: Control) -> void:
|
|
if _dragging or group.details.strip_edges() == "":
|
|
return
|
|
main.details().show_for(owner, Formatter.strip_tags(group.name), group.details,
|
|
"Group · %s%s" % [count_text(), " · collapsed" if group.collapsed else ""])
|
|
|
|
|
|
# ---------- Geometry (world coordinates) ----------
|
|
|
|
func inlet_point() -> Vector2:
|
|
return Vector2(position.x, position.y + INLET_Y)
|
|
|
|
|
|
## Where a pipe from a hidden member's entry leaves the collapsed card: that
|
|
## result's row when it's listed, otherwise the right edge by the title.
|
|
func anchor_for(tid: String, eid: String) -> Vector2:
|
|
var row: Control = lit_rows.get(Roller.entry_key(tid, eid))
|
|
if row != null and row.is_inside_tree():
|
|
var to_world := main.board.world.get_global_transform().affine_inverse()
|
|
var r := row.get_global_rect()
|
|
return Vector2(position.x + size.x, (to_world * (r.position + Vector2(0, r.size.y / 2.0))).y)
|
|
return Vector2(position.x + size.x, position.y + INLET_Y)
|
|
|
|
|
|
# ---------- Input ----------
|
|
|
|
## Right-click: the group's actions menu (Actions.group).
|
|
func _open_menu_on(mb: InputEventMouseButton) -> bool:
|
|
if mb == null or mb.button_index != MOUSE_BUTTON_RIGHT or not mb.pressed:
|
|
return false
|
|
Actions.popup_at(main, Actions.group(main, group), mb.global_position)
|
|
return true
|
|
|
|
|
|
func _on_title_input(ev: InputEvent, owner: Control) -> void:
|
|
var mb := ev as InputEventMouseButton
|
|
if _open_menu_on(mb):
|
|
owner.accept_event()
|
|
return
|
|
if mb and mb.button_index == MOUSE_BUTTON_LEFT:
|
|
if mb.pressed:
|
|
main.details().hide_for()
|
|
if main.mode == Main.Mode.BUILD and (mb.ctrl_pressed or mb.meta_pressed):
|
|
main.selecting.toggle_group(group.id)
|
|
owner.accept_event()
|
|
return
|
|
_dragging = true
|
|
_drag_last = main.board.world_mouse()
|
|
if group.collapsed:
|
|
get_parent().move_child(self, -1)
|
|
elif _dragging:
|
|
_dragging = false
|
|
main.mark_dirty()
|
|
owner.accept_event()
|
|
return
|
|
var mm := ev as InputEventMouseMotion
|
|
if mm and _dragging:
|
|
var delta := (main.board.world_mouse() - _drag_last).round()
|
|
if delta != Vector2.ZERO:
|
|
main.selecting.drag_group(group.id, delta)
|
|
_drag_last += delta
|
|
owner.accept_event()
|