Main (3679 lines) becomes a shell: shared state, start-up, cards, input, rules view and one-line delegators for the methods other files call. Each area moves with its state into a RefCounted helper holding a back-reference (m: Main): main_cli.gd command-line and screenshot flags main_top_bar.gd top bar, toast, endless-loop chip main_editing.gd add/delete cards, results and pipes, selection bar main_groups.gd groups and card drags main_templates.gd templates main_rolling.gd rolling, choice questions, playback, session seed, copy main_collections.gd collections, import/export, table settings, test board main_state.gd decks, variables, roll history (local state) main_navigation.gd search, focus, minimap, arrange/pack main_history.gd undo/redo and mark_dirty main_inspector.gd inspector selection and refresh Tests that reached moved private members now go through the helper (main.undo_redo.edits, main.rolling.pending_choice(), ...). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
357 lines
12 KiB
GDScript
357 lines
12 KiB
GDScript
class_name MainHistory
|
|
extends RefCounted
|
|
## Main's undo and redo, and its toolbar buttons.
|
|
##
|
|
## Every change to the saved collection goes through mark_dirty, which records
|
|
## an undo step in `edits` (EditHistory; one per collection, kept in memory
|
|
## while the app runs, so switching collections and back keeps it). Steps are
|
|
## whole-board snapshots, so undo needs nothing from the card editors.
|
|
##
|
|
## Coalescing: typing in a text field commits once typing pauses for
|
|
## EDIT_PAUSE, and later typing in the same field merges into that step until
|
|
## focus moves; other edits commit at the end of the frame (a whole drag is one
|
|
## step), and repeated edits to the same card or pipe within
|
|
## EditHistory.COALESCE_SECS merge. Main's actions call _begin_edit
|
|
## before changing anything (committing pending typing as its own step) and
|
|
## _end_edit after, which commits at once.
|
|
##
|
|
## Collapsing cards and groups is saved but treated as view state: it isn't a
|
|
## step, and undo leaves things collapsed or expanded as they are.
|
|
##
|
|
## Ctrl+Z undoes, Ctrl+Shift+Z or Ctrl+Y redoes, in Build and Play, unless a
|
|
## text field has focus (it keeps its own Ctrl+Z for the text being typed) or
|
|
## a dialog is open.
|
|
|
|
const EDIT_PAUSE := 0.8
|
|
|
|
var m: Main
|
|
|
|
var edits: EditHistory
|
|
var _histories: Dictionary = {} # collection id -> EditHistory
|
|
var _edit_timer: Timer
|
|
var _edit_pending := false
|
|
var _edit_focus := 0
|
|
var _commit_queued := false
|
|
var _restoring := false
|
|
var _keep_history := false
|
|
var _undo_button: Button
|
|
var _redo_button: Button
|
|
## How long the last undo or redo took to rebuild the board (microseconds).
|
|
var _last_restore_usec := 0
|
|
|
|
|
|
func _init(p_main: Main) -> void:
|
|
m = p_main
|
|
|
|
|
|
## Call after changing the saved collection: schedules the save, refreshes
|
|
## what depends on the board and records an undo step (see the top of this file).
|
|
## `label` names the step and commits it at once; without one the step is
|
|
## named from the change, and committed at the end of the frame, or once
|
|
## typing pauses when a text field has focus. `undoable` false is for saved
|
|
## view state (collapsing), which isn't an undo step.
|
|
func mark_dirty(label: String = "", undoable: bool = true) -> void:
|
|
m._board_changed()
|
|
if not undoable or edits == null or _restoring:
|
|
return
|
|
_edit_pending = true
|
|
if label != "":
|
|
_commit_edits(label)
|
|
return
|
|
var f := m.get_viewport().gui_get_focus_owner() if m.is_inside_tree() else null
|
|
if f is LineEdit or f is TextEdit:
|
|
_edit_focus = f.get_instance_id()
|
|
_edit_timer.start()
|
|
else:
|
|
_edit_focus = 0
|
|
_queue_commit()
|
|
|
|
|
|
func _build_edit_timer() -> void:
|
|
_edit_timer = Timer.new()
|
|
_edit_timer.one_shot = true
|
|
_edit_timer.wait_time = EDIT_PAUSE
|
|
_edit_timer.timeout.connect(func() -> void: _commit_edits())
|
|
m.add_child(_edit_timer)
|
|
m.get_viewport().gui_focus_changed.connect(_on_focus_changed)
|
|
|
|
|
|
## Typing in one field and then moving to another makes two steps.
|
|
func _on_focus_changed(c: Control) -> void:
|
|
if _edit_pending and _edit_focus != 0 and (c == null or c.get_instance_id() != _edit_focus):
|
|
_queue_commit()
|
|
m.inspecting._inspect_focus(c)
|
|
|
|
|
|
## Picks this collection's history (making one) and takes the board as its
|
|
## baseline; a history whose board changed from outside starts over.
|
|
func _sync_history() -> void:
|
|
if m.data == null:
|
|
return
|
|
if _edit_timer:
|
|
_edit_timer.stop()
|
|
_edit_pending = false
|
|
var h: EditHistory = _histories.get(m.data.id)
|
|
if h == null:
|
|
h = EditHistory.new(m.data)
|
|
_histories[m.data.id] = h
|
|
else:
|
|
h.resync(m.data)
|
|
edits = h
|
|
_update_undo_buttons()
|
|
|
|
|
|
## Before an action changes the board: commits edits still waiting (typing)
|
|
## as a step of their own, so the action's step starts from here.
|
|
func _begin_edit() -> void:
|
|
_commit_edits()
|
|
|
|
|
|
## After an action changed the board: like mark_dirty, but the step is
|
|
## committed now (named `label`, or from the change).
|
|
func _end_edit(label: String = "") -> void:
|
|
m._board_changed()
|
|
if edits == null or _restoring:
|
|
return
|
|
_edit_pending = true
|
|
_commit_edits(label)
|
|
|
|
|
|
func _queue_commit() -> void:
|
|
if not _commit_queued:
|
|
_commit_queued = true
|
|
_commit_edits.call_deferred()
|
|
|
|
|
|
## Records waiting edits as an undo step now (see EditHistory.commit).
|
|
func _commit_edits(label: String = "") -> void:
|
|
_commit_queued = false
|
|
if _edit_timer:
|
|
_edit_timer.stop()
|
|
if not _edit_pending or edits == null:
|
|
return
|
|
_edit_pending = false
|
|
var r := edits.commit(m.data, label, _edit_focus)
|
|
if r["step"]:
|
|
if m.top_bar._toast_serial >= 0 and edits.top_serial() != m.top_bar._toast_serial:
|
|
m.top_bar._toast_serial = -1
|
|
m.top_bar._toast_undo.hide()
|
|
_update_undo_buttons()
|
|
|
|
|
|
func can_undo() -> bool:
|
|
return edits != null and (edits.can_undo() or _edit_pending)
|
|
|
|
|
|
func can_redo() -> bool:
|
|
return edits != null and edits.can_redo() and not _edit_pending
|
|
|
|
|
|
## Steps back one edit. Returns false when there was nothing to undo (or it
|
|
## has to wait for a roll or a dialog).
|
|
func undo() -> bool:
|
|
if not _history_ready():
|
|
return false
|
|
_commit_edits()
|
|
if not edits.can_undo():
|
|
m.toast("Nothing to undo")
|
|
return false
|
|
var e := edits.pop_undo()
|
|
_apply_history(e, "Undid")
|
|
return true
|
|
|
|
|
|
func redo() -> bool:
|
|
if not _history_ready():
|
|
return false
|
|
_commit_edits()
|
|
if not edits.can_redo():
|
|
m.toast("Nothing to redo")
|
|
return false
|
|
var e := edits.pop_redo()
|
|
_apply_history(e, "Redid")
|
|
return true
|
|
|
|
|
|
func _history_ready() -> bool:
|
|
if edits == null:
|
|
return false
|
|
if m.rolling_ctx != null or m.rolling._choice_waiting("Answer the waiting question first"):
|
|
m.toast("Wait for the roll to finish before undoing")
|
|
return false
|
|
return true
|
|
|
|
|
|
## Puts the board back to a snapshot from the history, keeping the view, the
|
|
## mode, the open search and the lit results of tables that still exist. Only
|
|
## what changed is rebuilt (EditHistory.patch, _refresh_patched); cards hidden
|
|
## in collapsed groups stay unbuilt.
|
|
func _apply_history(e: Dictionary, verb: String) -> void:
|
|
var t0 := Time.get_ticks_usec()
|
|
var d := EditHistory.diff(e["from"], e["snap"])
|
|
_restoring = true
|
|
if m.inspecting._pipe_popover and m.inspecting._pipe_popover.visible:
|
|
m.inspecting._pipe_popover.hide()
|
|
if m.inspector:
|
|
m.inspector.close_menus()
|
|
m.details().hide_for()
|
|
m.board.pipes.hover_key = ""
|
|
m.editing._drop_card = null
|
|
m.editing._link_from = {}
|
|
m.clear_selection()
|
|
var p := EditHistory.patch(m.data, e["snap"], e["from"])
|
|
if not p.is_empty():
|
|
edits.cache = p["snap"]
|
|
_refresh_patched(p, d)
|
|
else:
|
|
edits.cache = EditHistory.restore(m.data, e["snap"])
|
|
_keep_history = true
|
|
m._rebuild_cards()
|
|
_keep_history = false
|
|
_restoring = false
|
|
m._board_changed()
|
|
m._refresh_lit()
|
|
m.tray.render()
|
|
_update_undo_buttons()
|
|
m.toast("%s: %s" % [verb, e["label"]])
|
|
_last_restore_usec = Time.get_ticks_usec() - t0
|
|
_flash_changes(d)
|
|
|
|
|
|
## After EditHistory.patch: frees the cards of removed tables, rebuilds the
|
|
## groups if they changed, builds cards for tables brought back (unless a
|
|
## collapsed group hides them) and moves and rebuilds the changed ones. A
|
|
## changed variable card rebuilds every card (other cards' effect chips name
|
|
## it).
|
|
func _refresh_patched(p: Dictionary, d: Dictionary) -> void:
|
|
for tid: String in p["removed"]:
|
|
var c := m.card_for(tid)
|
|
m.cards.erase(tid)
|
|
if c:
|
|
c.get_parent().remove_child(c)
|
|
c.queue_free()
|
|
if p["groups_changed"]:
|
|
m.grouping._groups_changed()
|
|
else:
|
|
for gid: String in p["groups"]:
|
|
var n := m.group_node(gid)
|
|
if n:
|
|
n.sync_rect()
|
|
m.grouping._apply_group_visibility()
|
|
var all := false
|
|
for tid: String in p["tables"]:
|
|
var t := m.data.get_table(tid)
|
|
if t and t.kind == RollTable.KIND_VARIABLE and (d["changed"] as Array).has(tid):
|
|
all = true
|
|
for tid: String in (m.cards.keys() if all else p["tables"]):
|
|
var c := m.card_for(tid)
|
|
if c == null:
|
|
continue
|
|
c.position = c.table.position
|
|
if all or (d["changed"] as Array).has(tid):
|
|
c.rebuild()
|
|
m._update_collapse_all()
|
|
|
|
|
|
## Rings the cards an undo or redo changed, once they have laid out. One
|
|
## changed card that's off screen is brought into view (without changing the
|
|
## zoom); bigger changes leave the view alone.
|
|
func _flash_changes(d: Dictionary) -> void:
|
|
for i in 2:
|
|
await m.get_tree().process_frame
|
|
var nodes: Array[Control] = []
|
|
var hidden := m.hidden_groups()
|
|
for tid: String in EditHistory.touched_tables(d):
|
|
var n: Control = m.card_for(tid)
|
|
if n == null:
|
|
n = hidden.get(tid)
|
|
if n and is_instance_valid(n) and not nodes.has(n):
|
|
nodes.append(n)
|
|
for gid: String in (d["gadded"] as Array) + (d["gchanged"] as Array):
|
|
var n := m.group_node(gid)
|
|
if n and not nodes.has(n):
|
|
nodes.append(n)
|
|
if nodes.is_empty():
|
|
return
|
|
var view := m.board.view_rect()
|
|
if nodes.size() == 1 and not view.intersects(Rect2(nodes[0].position, nodes[0].size)):
|
|
m.board.animate_to(Rect2(nodes[0].position, nodes[0].size).get_center(), m.board.zoom)
|
|
view = Rect2()
|
|
var shown: Array[Control] = []
|
|
for n in nodes:
|
|
if view.size == Vector2.ZERO or view.intersects(Rect2(n.position, n.size)):
|
|
shown.append(n)
|
|
m.board.cards.flash_all(shown)
|
|
|
|
|
|
func _make_history_buttons(tools: HBoxContainer) -> void:
|
|
_undo_button = Style.bar_button("Undo")
|
|
_undo_button.pressed.connect(func() -> void: undo())
|
|
m.top_bar._bar_icon(_undo_button, func(c: Control) -> void: _draw_turn_arrow(c, false))
|
|
_redo_button = Style.bar_button("Redo")
|
|
_redo_button.pressed.connect(func() -> void: redo())
|
|
m.top_bar._bar_icon(_redo_button, func(c: Control) -> void: _draw_turn_arrow(c, true))
|
|
for b in [_undo_button, _redo_button]:
|
|
# Icon-only (the tooltip names the step), so the Build bar fits at 1280 px.
|
|
(b as Button).text = ""
|
|
for n in ["normal", "hover", "pressed", "hover_pressed", "disabled"]:
|
|
var sb := (b as Button).get_theme_stylebox(n) as StyleBoxFlat
|
|
if sb:
|
|
sb.content_margin_left = 26
|
|
(b as Button).focus_mode = Control.FOCUS_NONE
|
|
(b as Button).add_theme_color_override("font_disabled_color", Color(Style.ON_FELT, 0.35))
|
|
tools.add_child(b)
|
|
|
|
|
|
## A curved arrow, pointing left (undo) or right (redo).
|
|
func _draw_turn_arrow(c: Control, right: bool) -> void:
|
|
var b := c.get_parent() as Button
|
|
var col := Style.ON_FELT if b == null or not b.disabled else Color(Style.ON_FELT, 0.35)
|
|
var s := -1.0 if right else 1.0
|
|
var o := Vector2(8, 9)
|
|
c.draw_arc(o, 5.0, -PI * 0.95 if not right else -PI * 0.05, PI * 0.5 if not right else -PI * 1.5, 16, col, 1.8, true)
|
|
var tip := o + Vector2(-5.0 * s, -0.4)
|
|
c.draw_colored_polygon(PackedVector2Array([tip + Vector2(-3.2 * s, -1.0), tip + Vector2(3.2 * s, -1.0), tip + Vector2(0, 3.6)]), col)
|
|
|
|
|
|
func _update_undo_buttons() -> void:
|
|
if _undo_button == null:
|
|
return
|
|
_undo_button.disabled = not can_undo()
|
|
_redo_button.disabled = not can_redo()
|
|
if edits and edits.can_undo() and not _edit_pending:
|
|
_undo_button.tooltip_text = "Undo: %s (Ctrl+Z)" % edits.undo_label()
|
|
elif _edit_pending:
|
|
_undo_button.tooltip_text = "Undo your latest edit (Ctrl+Z)"
|
|
else:
|
|
_undo_button.tooltip_text = "Nothing to undo"
|
|
_redo_button.tooltip_text = ("Redo: %s (Ctrl+Shift+Z or Ctrl+Y)" % edits.redo_label()) if can_redo() else "Nothing to redo"
|
|
for b in [_undo_button, _redo_button]:
|
|
for k in (b as Button).get_children():
|
|
(k as CanvasItem).queue_redraw()
|
|
|
|
|
|
## True while a dialog (a ModalCard) is open over the board.
|
|
func _modal_open() -> bool:
|
|
for c in m.get_children():
|
|
if c is ModalCard and (c as ModalCard).visible:
|
|
return true
|
|
return false
|
|
|
|
|
|
## Ctrl+Z / Ctrl+Shift+Z / Ctrl+Y outside text fields.
|
|
func _history_shortcut(k: InputEventKey) -> bool:
|
|
if not (k.ctrl_pressed or k.meta_pressed) or k.alt_pressed:
|
|
return false
|
|
var is_undo := k.keycode == KEY_Z and not k.shift_pressed
|
|
var is_redo := (k.keycode == KEY_Z and k.shift_pressed) or (k.keycode == KEY_Y and not k.shift_pressed)
|
|
if not is_undo and not is_redo:
|
|
return false
|
|
var f := m.get_viewport().gui_get_focus_owner()
|
|
if f is LineEdit or f is TextEdit or _modal_open():
|
|
return false
|
|
if is_undo:
|
|
undo()
|
|
else:
|
|
redo()
|
|
return true
|