diff --git a/ui/main.gd b/ui/main.gd index 394ec25..c49c1fa 100644 --- a/ui/main.gd +++ b/ui/main.gd @@ -54,6 +54,8 @@ var demo: DemoMode var _save_timer: Timer var _saving_enabled := true +## When there's no `state` (--sample), the rules view lives here (docs/PLAIN.md §6). +var _rules_view_mem := "plain" # Local state for the current collection (see load_local_state). Kept in memory # and written to `state` when there is one (saving disabled: memory only). @@ -146,8 +148,6 @@ func _ready() -> void: func _exit_tree() -> void: _save_now() -# ---------- UI ---------- - # ---------- Cards ---------- func card_for(id: String) -> TableCard: @@ -156,7 +156,7 @@ func card_for(id: String) -> TableCard: func is_entry_lit(tid: String, eid: String) -> bool: return lit_entries.has(Roller.entry_key(tid, eid)) -# ---------- Collapsing ---------- +# ---------- Live cards and pop-ups ---------- ## Re-renders the live cards (variables, sheets, questions). Called after a ## roll, after variable edits and after edits in Build, never per frame, so @@ -176,171 +176,120 @@ func details() -> DetailsPopup: _details_popup.setup(self) return _details_popup -# ---------- Input ---------- - -# ---------- Rules view (docs/PLAIN.md §6) ---------- - -## When there's no `state` (--sample), the rules view lives here. -var _rules_view_mem := "plain" - # ---------- Delegators: what other files call on Main ---------- +# One block per helper; each line pair passes the call straight on. func toast(message: String, undoable: bool = false) -> void: top_bar.toast(message, undoable) func add_variable_card(label: String, var_name: String, type: String = "number") -> RollTable: return editing.add_variable_card(label, var_name, type) - func add_entry(tid: String) -> void: editing.add_entry(tid) - func delete_entry(tid: String, eid: String) -> void: editing.delete_entry(tid, eid) - func delete_table(tid: String) -> void: editing.delete_table(tid) - func remove_pipe(key: String) -> void: editing.remove_pipe(key) - func start_link(tid: String, eid: String) -> void: editing.start_link(tid, eid) - func update_link() -> void: editing.update_link() - func finish_link() -> void: editing.finish_link() - func is_selected(tid: String) -> bool: return editing.is_selected(tid) - func toggle_selected(tid: String) -> void: editing.toggle_selected(tid) - func clear_selection() -> void: editing.clear_selection() - func select_in_rect(r: Rect2) -> void: editing.select_in_rect(r) func group_node(gid: String) -> GroupCard: return grouping.group_node(gid) - func hidden_groups() -> Dictionary: return grouping.hidden_groups() - func lit_keys_for_group(g: TableGroup) -> Array: return grouping.lit_keys_for_group(g) - func toggle_group(gid: String) -> void: grouping.toggle_group(gid) - func move_group(gid: String, delta: Vector2) -> void: grouping.move_group(gid, delta) - func ungroup(gid: String) -> void: grouping.ungroup(gid) - func confirm_delete_group(gid: String) -> void: grouping.confirm_delete_group(gid) - func edit_group_details(gid: String) -> void: grouping.edit_group_details(gid) - func begin_card_drag(card: TableCard) -> void: - grouping.begin_card_drag(card) - + grouping.card_drag.begin_card_drag(card) func drag_card(card: TableCard, to: Vector2) -> void: - grouping.drag_card(card, to) - + grouping.card_drag.drag_card(card, to) func end_card_drag(_card: TableCard) -> void: - grouping.end_card_drag(_card) + grouping.card_drag.end_card_drag(_card) func save_template_from(table_ids: Array, kind: String, group_ids: Array = []) -> void: templating.save_template_from(table_ids, kind, group_ids) - func begin_template_insert(tpl: Dictionary) -> void: templating.begin_template_insert(tpl) - func export_template(id: String) -> void: templating.export_template(id) - func import_template_file(picker: TemplatePicker = null) -> void: templating.import_template_file(picker) func start_roll(tid: String, fast: bool = false) -> void: await rolling.start_roll(tid, fast) - func copy_current(kind: int) -> void: rolling.copy_current(kind) func collections_enabled() -> bool: return collections.collections_enabled() - func open_table_settings(table_id: String) -> void: collections.open_table_settings(table_id) - func switch_collection(id: String) -> void: collections.switch_collection(id) - func new_collection() -> void: collections.new_collection() - func rename_collection() -> void: collections.rename_collection() - func duplicate_collection() -> void: collections.duplicate_collection() - func delete_collection() -> void: collections.delete_collection() func cards_left(tid: String) -> Vector2i: return local_state.cards_left(tid) - func reshuffle_table(tid: String) -> void: local_state.reshuffle_table(tid) - func lit_text(tid: String, eid: String) -> String: return local_state.lit_text(tid, eid) - func ref_targets_for(e: TableEntry) -> Array[String]: return local_state.ref_targets_for(e) - func set_variable(name: String, value: String) -> void: local_state.set_variable(name, value) - func clear_variable(name: String) -> void: local_state.clear_variable(name) - func variable_card(name: String) -> RollTable: return local_state.variable_card(name) - func variable_names() -> Array[String]: return local_state.variable_names() - func reset_variable(name: String) -> void: local_state.reset_variable(name) - func reset_all_variables() -> void: local_state.reset_all_variables() - func reroll_variable(name: String) -> void: local_state.reroll_variable(name) - func can_reroll_variable(name: String) -> bool: return local_state.can_reroll_variable(name) - func set_collection_member(t: RollTable, item: String, value: String) -> void: local_state.set_collection_member(t, item, value) - func remove_collection_member(t: RollTable, item: String) -> void: local_state.remove_collection_member(t, item) func focus_table(table_id: String, entry_id: String = "") -> void: await nav.focus_table(table_id, entry_id) - func focus_group(gid: String) -> void: nav.focus_group(gid) @@ -349,22 +298,16 @@ func mark_dirty(label: String = "", undoable: bool = true) -> void: func open_pipe_options(key: String, at: Vector2) -> void: inspecting.open_pipe_options(key, at) - func request_table_settings(tid: String) -> void: inspecting.request_table_settings(tid) - func select_card(table_id: String) -> void: inspecting.select_card(table_id) - func select_result(table_id: String, entry_id: String) -> void: inspecting.select_result(table_id, entry_id) - func clear_inspector() -> void: inspecting.clear_inspector() - func inspector_selection() -> Dictionary: return inspecting.inspector_selection() - func inspector_edited(tid: String, all_cards: bool = false) -> void: inspecting.inspector_edited(tid, all_cards) @@ -372,47 +315,35 @@ func inspector_edited(tid: String, all_cards: bool = false) -> void: func _save_now() -> void: layout._save_now() - func _rebuild_cards() -> void: layout._rebuild_cards() - func _refresh_lit() -> void: layout._refresh_lit() - func set_mode(m: Mode) -> void: layout.set_mode(m) - func _board_changed() -> void: layout._board_changed() - func _any_expanded() -> bool: return layout._any_expanded() - func toggle_collapse(tid: String) -> void: layout.toggle_collapse(tid) - func set_all_collapsed(collapsed: bool) -> void: layout.set_all_collapsed(collapsed) ## Opens the system file dialog; `on_path` gets the chosen path. func file_dialog(title: String, filters: Array, mode: FileDialog.FileMode, on_path: Callable, file_name: String = "") -> void: layout.file_dialog(title, filters, mode, on_path, file_name) - func export_dialog(title: String, filter: String, file_name: String, text_for: Callable) -> void: layout.export_dialog(title, filter, file_name, text_for) ## "plain" (Describe's phrases) or "text" (the stored rule text). func rules_view() -> String: return layout.rules_view() - func rules_plain() -> bool: return layout.rules_plain() - func set_rules_view(v: String) -> void: layout.set_rules_view(v) - func _shortcut_input(ev: InputEvent) -> void: layout._shortcut_input(ev) - func _unhandled_input(ev: InputEvent) -> void: layout._unhandled_input(ev) diff --git a/ui/main_card_drag.gd b/ui/main_card_drag.gd new file mode 100644 index 0000000..8f5ed7f --- /dev/null +++ b/ui/main_card_drag.gd @@ -0,0 +1,119 @@ +class_name MainCardDrag +extends RefCounted +## Dragging cards on the board. A drag moves one card, or the whole Build +## selection (cards and groups). In Build, a card dropped in a group frame +## joins that group, and one dropped outside its own frame leaves it. +## MainGroups holds it; TableCard reaches it through Main's delegators. + +var m: Main + +## Ids of the cards being dragged and the group the pointer is over (Build). +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 + + +## The expanded group frame containing a world point (the smallest, if they overlap). +func _frame_at(point: Vector2) -> TableGroup: + var best: TableGroup = null + for group in m.data.groups: + if group.collapsed or not group.rect().has_point(point): + continue + if best == null or group.rect().get_area() < best.rect().get_area(): + best = group + return best + + +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 group := m.data.group_of(tid) + if group: + m.board.groups.frozen[group.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 other := m.card_for(tid) + other.position += delta + other.table.position = other.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 := m.grouping.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 dragged := m.card_for(tid) + if dragged == null: + continue + var center := dragged.position + dragged.size / 2.0 + var old_group := m.data.group_of(tid) + var target := _frame_at(center) + if target != null and target != old_group: + m.data.add_to_group(tid, target.id) + joined[target.name] = int(joined.get(target.name, 0)) + 1 + elif target == null and old_group != null and not old_group.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 table := m.data.get_table(tid) + if table and table.position != _drag_from.get(tid, table.position): + moved = true + _drag_set.clear() + _drag_from.clear() + if not joined.is_empty() or left > 0: + m.grouping._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) diff --git a/ui/main_card_drag.gd.uid b/ui/main_card_drag.gd.uid new file mode 100644 index 0000000..c4bfaa8 --- /dev/null +++ b/ui/main_card_drag.gd.uid @@ -0,0 +1 @@ +uid://l6w4wj03jkjp diff --git a/ui/main_groups.gd b/ui/main_groups.gd index 48c7b66..04cb811 100644 --- a/ui/main_groups.gd +++ b/ui/main_groups.gd @@ -1,24 +1,26 @@ 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 +## 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 -## 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 +## 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: @@ -299,106 +301,3 @@ func edit_group_details(gid: String) -> void: 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)