320 lines
10 KiB
GDScript
320 lines
10 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.
|
|
##
|
|
## The collapsed card itself is built by GroupCardCompact.
|
|
|
|
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 (built by GroupCardCompact) ----------
|
|
|
|
func _build_compact() -> void:
|
|
GroupCardCompact.build(self)
|
|
|
|
|
|
## 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():
|
|
return Vector2(position.x + size.x, main.board.world_rect_of(row).get_center().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()
|