EditHistory (ui/edit_history.gd) keeps an undo and a redo stack of
whole-board snapshots per collection. Each table and group is its own
JSON string, shared with the previous snapshot when unchanged, so a step
costs about the size of the cards it changed. Collapsed flags are kept
apart: collapsing isn't a step and undo leaves it as it is. Labels come
from diffing the snapshots ("Delete table Loot", "Add pipe Loot → Gems",
"Move 3 cards", "Group Careers"), or from the caller.
Main.mark_dirty records steps: typing commits when it pauses (0.8 s) and
merges until focus moves, other edits commit at the end of the frame (a
drag is one step), and quick edits to the same card or pipe merge.
Actions in main.gd bracket their changes with _begin_edit/_end_edit.
Undo patches only the changed tables and groups in place and rebuilds
their cards, keeping view, mode, search and lit results, then flashes
what changed. Ctrl+Z, Ctrl+Shift+Z / Ctrl+Y outside text fields (Play
too), Undo/Redo buttons with step tooltips in the Build bar, and the
toast's Undo now goes through the history.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
655 lines
25 KiB
GDScript
655 lines
25 KiB
GDScript
extends SceneTree
|
|
## Undo and redo of Build edits (EditHistory and Main's "Undo and redo"):
|
|
## every kind of edit, coalescing, redo being cleared, the cap, per-collection
|
|
## histories, labels, view and lit state, the toast's Undo, lazily built cards,
|
|
## the shortcuts and buttons, and the snapshot cost on a big board.
|
|
## Run: godot --headless --path . -s res://tests/test_undo.gd -- --sample
|
|
## [--big-board=<collection.json>] times a real board instead of a made-up
|
|
## 800-card one.
|
|
|
|
var _failures := 0
|
|
var main: Main
|
|
|
|
|
|
func _init() -> void:
|
|
if not OS.get_cmdline_user_args().has("--sample"):
|
|
print("Run with -- --sample so the test can't touch saved data.")
|
|
quit(2)
|
|
return
|
|
_test_history_unit()
|
|
main = load("res://ui/main.tscn").instantiate()
|
|
root.add_child(main)
|
|
for i in 5:
|
|
await process_frame
|
|
if main._saving_enabled:
|
|
print("Saving is enabled; refusing to run.")
|
|
quit(2)
|
|
return
|
|
main.set_anchors_and_offsets_preset(Control.PRESET_TOP_LEFT)
|
|
main.size = Vector2(1440, 900)
|
|
main.set_mode(Main.Mode.BUILD)
|
|
await _load()
|
|
await _test_typing()
|
|
await _test_card_fields()
|
|
await _test_results_and_tables()
|
|
await _test_pipes()
|
|
await _test_drag()
|
|
await _test_groups()
|
|
await _test_template_and_reset()
|
|
await _test_pack()
|
|
await _test_redo_cleared()
|
|
await _test_view_kept()
|
|
await _test_toast()
|
|
await _test_lazy_cards()
|
|
await _test_collapse_not_undoable()
|
|
await _test_shortcuts_and_buttons()
|
|
await _test_play_mode()
|
|
await _test_collections()
|
|
await _test_big_board()
|
|
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
|
|
quit(1 if _failures else 0)
|
|
|
|
|
|
func _check(ok: bool, what: String) -> void:
|
|
if ok:
|
|
print(" ok %s" % what)
|
|
else:
|
|
_failures += 1
|
|
print(" FAIL %s" % what)
|
|
|
|
|
|
## Loot (two results, one piping to Gems), Gems, Weapons and Armor in the
|
|
## group Careers, far to the right.
|
|
func _board_dict() -> Dictionary:
|
|
return {"format_version": 2, "id": "undo-test", "name": "Undo test", "tables": [
|
|
{"id": "a", "name": "Loot", "x": 40, "y": 60, "entries": [
|
|
{"id": "e1", "text": "Gold", "pipes": ["b"]},
|
|
{"id": "e2", "text": "Silver"}]},
|
|
{"id": "b", "name": "Gems", "x": 400, "y": 60, "entries": [{"id": "e1", "text": "Ruby"}]},
|
|
{"id": "c", "name": "Weapons", "x": 1500, "y": 90, "entries": [{"id": "e1", "text": "Sword"}]},
|
|
{"id": "d", "name": "Armor", "x": 1860, "y": 90, "entries": [{"id": "e1", "text": "Mail"}]},
|
|
], "groups": [
|
|
{"id": "g1", "name": "Careers", "x": 1478, "y": 22, "w": 700, "h": 300, "members": ["c", "d"]},
|
|
]}
|
|
|
|
|
|
func _load(d: Dictionary = {}) -> void:
|
|
main.data.load_dict(d if not d.is_empty() else _board_dict())
|
|
main.lit_entries.clear()
|
|
main.lit_pipes.clear()
|
|
main.lit_texts.clear()
|
|
main._rebuild_cards()
|
|
for i in 4:
|
|
await process_frame
|
|
|
|
|
|
func _frames(n: int = 2) -> void:
|
|
for i in n:
|
|
await process_frame
|
|
|
|
|
|
func _t(id: String) -> RollTable:
|
|
return main.data.get_table(id)
|
|
|
|
|
|
func _label() -> String:
|
|
return main.edits.undo_label()
|
|
|
|
|
|
func _steps() -> int:
|
|
return main.edits.undo_stack.size()
|
|
|
|
|
|
func _line_edit_with(n: Node, text: String) -> LineEdit:
|
|
for c in n.get_children():
|
|
if c is LineEdit and (c as LineEdit).text == text:
|
|
return c
|
|
var r := _line_edit_with(c, text)
|
|
if r:
|
|
return r
|
|
return null
|
|
|
|
|
|
# ---------- EditHistory on its own ----------
|
|
|
|
func _test_history_unit() -> void:
|
|
print("EditHistory")
|
|
var b := TowerBoard.new()
|
|
b.load_dict(_board_dict())
|
|
var h := EditHistory.new(b)
|
|
_check(not h.can_undo() and not h.can_redo(), "a new history is empty")
|
|
b.get_table("a").collapsed = true
|
|
_check(not h.commit(b)["step"], "collapsing alone isn't a step")
|
|
b.get_table("a").entries[0].text = "Platinum"
|
|
var r := h.commit(b)
|
|
_check(r["step"] and h.undo_label() == "Edit Loot", "an edit is a step named from the diff: %s" % h.undo_label())
|
|
b.get_table("b").name = "Jewels"
|
|
h.commit(b, "", 0, 100.0)
|
|
_check(h.undo_label() == "Rename Gems → Jewels", "a rename: %s" % h.undo_label())
|
|
var later := int(h.undo_stack.back()["snap"]["bytes"])
|
|
var full := EditHistory.full_bytes(h.cache)
|
|
_check(later < full / 2, "a later snapshot shares the unchanged cards' text: %d of %d" % [later, full])
|
|
var e := h.pop_undo()
|
|
h.cache = EditHistory.restore(b, e["snap"])
|
|
_check(b.get_table("b").name == "Gems" and h.can_redo() and h.redo_label() == "Rename Gems → Jewels", "undo restores the name and offers redo")
|
|
_check(b.get_table("a").collapsed, "restoring keeps a table collapsed as it is now")
|
|
# The cap.
|
|
var h2 := EditHistory.new(b)
|
|
for i in EditHistory.MAX_STEPS + 50:
|
|
b.get_table("a").entries[1].weight = i + 2
|
|
h2.commit(b, "", 0, float(i) * 10.0)
|
|
_check(h2.undo_stack.size() == EditHistory.MAX_STEPS, "the history keeps at most %d steps: %d" % [EditHistory.MAX_STEPS, h2.undo_stack.size()])
|
|
_check(h2.bytes() < EditHistory.MAX_BYTES, "and stays within its memory budget: %d bytes" % h2.bytes())
|
|
# Same-thing edits close together merge; different things don't.
|
|
var h3 := EditHistory.new(b)
|
|
b.get_table("a").entries[1].weight = 5
|
|
h3.commit(b, "", 0, 1000.0)
|
|
b.get_table("a").entries[1].weight = 6
|
|
h3.commit(b, "", 0, 1000.3)
|
|
_check(h3.undo_stack.size() == 1, "two quick edits to one card are one step")
|
|
b.get_table("b").entries[0].text = "Opal"
|
|
h3.commit(b, "", 0, 1000.5)
|
|
_check(h3.undo_stack.size() == 2, "an edit to another card is a new step")
|
|
b.get_table("b").entries[0].text = "Ruby"
|
|
h3.commit(b, "", 0, 1000.6)
|
|
_check(h3.undo_stack.size() == 1, "typing something and putting it back leaves no step")
|
|
|
|
|
|
# ---------- Edits through the app ----------
|
|
|
|
func _test_typing() -> void:
|
|
print("Typing coalesces")
|
|
var card := main.card_for("a")
|
|
var le := _line_edit_with(card, "Loot")
|
|
_check(le != null, "the card has a name field")
|
|
if le == null:
|
|
return
|
|
le.grab_focus()
|
|
var s := "Loot"
|
|
for ch in " of the deep":
|
|
s += ch
|
|
le.text = s
|
|
le.text_changed.emit(s)
|
|
_check(not main.edits.can_undo() and main.can_undo(), "while typing nothing is committed yet, but Undo is offered")
|
|
await create_timer(Main.EDIT_PAUSE + 0.3).timeout
|
|
_check(_steps() == 1 and _label() == "Rename Loot → Loot of the deep", "a pause commits the typing as one step: %d, %s" % [_steps(), _label()])
|
|
for ch in "!!":
|
|
s += ch
|
|
le.text = s
|
|
le.text_changed.emit(s)
|
|
main._commit_edits()
|
|
_check(_steps() == 1 and _t("a").name == "Loot of the deep!!", "more typing in the same field joins that step: %d" % _steps())
|
|
le.release_focus()
|
|
main.undo()
|
|
_check(_t("a").name == "Loot", "undo puts the old name back")
|
|
_check(main._toast_label.text == "Undid: Rename Loot → Loot of the deep!!", "a toast says what was undone: %s" % main._toast_label.text)
|
|
main.redo()
|
|
_check(_t("a").name == "Loot of the deep!!" and main._toast_label.text.begins_with("Redid: "), "redo brings it back")
|
|
main.undo()
|
|
await _frames()
|
|
|
|
|
|
func _test_card_fields() -> void:
|
|
print("Weights, effects, details, settings")
|
|
var n0 := _steps()
|
|
_t("a").entries[0].weight = 4
|
|
main.mark_dirty()
|
|
await _frames()
|
|
_check(_steps() == n0 + 1 and _label() == "Edit Loot", "a weight is a step: %s" % _label())
|
|
await create_timer(EditHistory.COALESCE_SECS + 0.1).timeout
|
|
_t("a").entries[0].effects = "@hp -= 1"
|
|
main.mark_dirty()
|
|
await _frames()
|
|
_t("a").details = "Treasure"
|
|
main.mark_dirty()
|
|
await _frames()
|
|
_check(_steps() == n0 + 2, "an effect and details right after merge into one step on the same card: %d" % (_steps() - n0))
|
|
main.undo()
|
|
_check(_t("a").entries[0].effects == "" and _t("a").details == "" and _t("a").entries[0].weight == 4, "undo takes both back, leaving the weight")
|
|
main.undo()
|
|
_check(_t("a").entries[0].weight == 1, "and then the weight")
|
|
# Settings go through the dialog's applied signal.
|
|
main.open_table_settings("b")
|
|
await _frames()
|
|
var dlg: TableSettingsDialog = null
|
|
for c in main.get_children():
|
|
if c is TableSettingsDialog:
|
|
dlg = c
|
|
_check(dlg != null, "the settings dialog opens")
|
|
if dlg:
|
|
_t("b").card_mode = true
|
|
dlg.applied.emit({})
|
|
dlg.close()
|
|
await _frames()
|
|
_check(_label() == "Edit settings of Gems", "settings are a step: %s" % _label())
|
|
main.undo()
|
|
_check(not _t("b").card_mode, "undo puts the settings back")
|
|
|
|
|
|
func _test_results_and_tables() -> void:
|
|
print("Results and tables")
|
|
main.add_entry("a")
|
|
_check(_label() == "Add result to Loot" and _t("a").entries.size() == 3, "adding a result: %s" % _label())
|
|
main.undo()
|
|
_check(_t("a").entries.size() == 2, "undo removes it")
|
|
main.redo()
|
|
_check(_t("a").entries.size() == 3, "redo adds it back")
|
|
main.undo()
|
|
main.delete_entry("a", "e2")
|
|
_check(_label() == "Delete result “Silver” from Loot", "deleting a result: %s" % _label())
|
|
main.undo()
|
|
_check(_t("a").find_entry("e2") != null and _t("a").find_entry("e2").text == "Silver", "undo brings the result back")
|
|
var t := main.add_table()
|
|
_check(_label() == "Add table New table", "adding a table: %s" % _label())
|
|
main.undo()
|
|
_check(_t(t.id) == null and main.card_for(t.id) == null, "undo removes the table and its card")
|
|
main.add_table(RollTable.KIND_CHOICE, "pick")
|
|
_check(_label() == "Add choice New choice", "adding a choice card: %s" % _label())
|
|
main.undo()
|
|
main.delete_table("b")
|
|
_check(_label() == "Delete table Gems" and _t("a").entries[0].pipes.is_empty(), "deleting a table: %s" % _label())
|
|
main.undo()
|
|
await _frames()
|
|
_check(_t("b") != null and main.card_for("b") != null and _t("a").entries[0].pipes == ["b"], "undo brings the table, its card and the pipe into it back")
|
|
|
|
|
|
func _test_pipes() -> void:
|
|
print("Pipes")
|
|
main._begin_edit()
|
|
_t("a").entries[1].pipes.append("c")
|
|
main.mark_dirty()
|
|
await _frames()
|
|
_check(_label() == "Add pipe Loot → Weapons", "laying a pipe: %s" % _label())
|
|
main.undo()
|
|
_check(_t("a").entries[1].pipes.is_empty(), "undo takes the pipe away")
|
|
main.remove_pipe("a:e1>b")
|
|
_check(_label() == "Remove pipe Loot → Gems" and main._toast_undo.visible, "removing a pipe, with an undo toast: %s" % _label())
|
|
main.undo()
|
|
_check(_t("a").entries[0].pipes == ["b"], "undo lays it again")
|
|
var e := _t("a").entries[0]
|
|
await create_timer(EditHistory.COALESCE_SECS + 0.1).timeout
|
|
var n0 := _steps()
|
|
e.set_pipe_count("b", 3)
|
|
main.mark_dirty()
|
|
await _frames()
|
|
e.set_pipe_count("b", 4)
|
|
main.mark_dirty()
|
|
await _frames()
|
|
_check(_steps() == n0 + 1 and _label() == "Edit pipe Loot → Gems", "nudging a pipe's count twice is one step: %s" % _label())
|
|
await create_timer(EditHistory.COALESCE_SECS + 0.1).timeout
|
|
e.set_pipe_save("b", "gem")
|
|
main.mark_dirty()
|
|
await _frames()
|
|
await create_timer(EditHistory.COALESCE_SECS + 0.1).timeout
|
|
e.set_pipe_when("b", "@hp > 2")
|
|
main.mark_dirty()
|
|
await _frames()
|
|
await create_timer(EditHistory.COALESCE_SECS + 0.1).timeout
|
|
e.set_pipe_when("b", "")
|
|
e.set_pipe_otherwise("b", true)
|
|
main.mark_dirty()
|
|
await _frames()
|
|
_check(_steps() == n0 + 4, "save as, Only if and Otherwise are steps of their own: %d" % (_steps() - n0))
|
|
main.undo()
|
|
_check(_t("a").entries[0].when_for("b") == "@hp > 2" and not _t("a").entries[0].is_otherwise("b"), "undo: Only if back")
|
|
main.undo()
|
|
_check(_t("a").entries[0].when_for("b") == "" and _t("a").entries[0].save_for("b") == "gem", "undo: condition gone, save kept")
|
|
main.undo()
|
|
main.undo()
|
|
_check(_t("a").entries[0].count_for("b") == 1 and _t("a").entries[0].save_for("b") == "", "undo: count back to 1")
|
|
|
|
|
|
func _test_drag() -> void:
|
|
print("Drags")
|
|
var card := main.card_for("b")
|
|
var p0 := card.position
|
|
var n0 := _steps()
|
|
main.begin_card_drag(card)
|
|
main.end_card_drag(card)
|
|
await _frames()
|
|
_check(_steps() == n0, "a click on a header without a drag isn't a step")
|
|
main.begin_card_drag(card)
|
|
for i in 10:
|
|
main.drag_card(card, p0 + Vector2(10 * i, 5 * i))
|
|
await process_frame
|
|
main.end_card_drag(card)
|
|
await _frames()
|
|
_check(_steps() == n0 + 1 and _label() == "Move Gems", "a whole drag is one step: %d, %s" % [_steps() - n0, _label()])
|
|
main.undo()
|
|
_check(_t("b").position == p0 and main.card_for("b").position == p0, "undo moves the card back")
|
|
# Dragging into a group frame joins it.
|
|
card = main.card_for("b")
|
|
main.begin_card_drag(card)
|
|
main.drag_card(card, Vector2(1600, 140))
|
|
main.end_card_drag(card)
|
|
await _frames()
|
|
_check(_label() == "Add 1 card to group Careers" and main.data.group_of("b") != null, "dropping into a group: %s" % _label())
|
|
main.undo()
|
|
await _frames()
|
|
_check(main.data.group_of("b") == null and _t("b").position == p0, "undo takes it back out")
|
|
# Several selected cards move together as one step.
|
|
main.set_selection(["a", "b"])
|
|
card = main.card_for("a")
|
|
var pa := card.position
|
|
main.begin_card_drag(card)
|
|
main.drag_card(card, pa + Vector2(0, 300))
|
|
main.end_card_drag(card)
|
|
await _frames()
|
|
_check(_label() == "Move 2 cards", "moving a selection: %s" % _label())
|
|
main.undo()
|
|
_check(_t("a").position == pa and main.selection.is_empty(), "undo moves them back and clears the selection")
|
|
|
|
|
|
func _test_groups() -> void:
|
|
print("Groups")
|
|
main.set_selection(["a", "b"])
|
|
var g := main.group_selection()
|
|
_check(_label() == "Group %s" % g.name, "grouping: %s" % _label())
|
|
main.undo()
|
|
_check(main.data.get_group(g.id) == null and main.group_node(g.id) == null, "undo removes the group")
|
|
main.redo()
|
|
_check(main.data.get_group(g.id) != null and main.group_node(g.id) != null, "redo makes it again")
|
|
main.undo()
|
|
main.ungroup("g1")
|
|
_check(_label() == "Ungroup Careers", "ungrouping: %s" % _label())
|
|
main.undo()
|
|
_check(main.data.get_group("g1") != null and main.data.get_group("g1").members == ["c", "d"], "undo puts the group back")
|
|
main.delete_group("g1")
|
|
_check(_label() == "Delete group Careers" and _t("c") == null, "deleting a group: %s" % _label())
|
|
main.undo()
|
|
await _frames()
|
|
_check(_t("c") != null and main.card_for("d") != null and main.group_node("g1") != null, "undo brings the group and its cards back")
|
|
var gpos := main.data.get_group("g1").position
|
|
var cpos := _t("c").position
|
|
main.move_group("g1", Vector2(100, 50))
|
|
main.mark_dirty()
|
|
await _frames()
|
|
_check(_label() == "Move group Careers", "moving a group: %s" % _label())
|
|
main.undo()
|
|
_check(_t("c").position == cpos and main.data.get_group("g1").position == gpos, "undo moves it back")
|
|
main.data.get_group("g1").name = "Jobs"
|
|
main.mark_dirty()
|
|
await _frames()
|
|
_check(_label() == "Rename group Jobs", "renaming a group: %s" % _label())
|
|
main.undo()
|
|
_check(main.data.get_group("g1").name == "Careers", "undo restores the name")
|
|
main.data.remove_from_group("d")
|
|
main.mark_dirty()
|
|
await _frames()
|
|
_check(_label() == "Take 1 card out of group Careers", "taking a card out: %s" % _label())
|
|
main.undo()
|
|
_check(main.data.get_group("g1").members.has("d"), "undo puts it back in")
|
|
|
|
|
|
func _test_template_and_reset() -> void:
|
|
print("Templates and Reset sample")
|
|
var tpl := TemplateKit.make(main.data, ["a"], "card", "Loot copy")
|
|
var r := main.insert_template(tpl, {})
|
|
await _frames()
|
|
var nt: RollTable = r["tables"][0]
|
|
_check(_label() == "Insert template Loot copy" and _t(nt.id) != null, "inserting a template: %s" % _label())
|
|
main.undo()
|
|
_check(_t(nt.id) == null and main.card_for(nt.id) == null, "undo removes the inserted cards")
|
|
await main.reset_sample()
|
|
_check(_label() == "Reset sample" and _t("a") == null, "Reset sample is a step: %s" % _label())
|
|
main.undo()
|
|
await _frames()
|
|
_check(_t("a") != null and _t("a").name == "Loot" and main.data.get_group("g1") != null, "undo brings the board back")
|
|
|
|
|
|
func _test_pack() -> void:
|
|
print("Pack")
|
|
main.set_group_collapsed("g1", true)
|
|
await _frames(3)
|
|
_check(not main.edits.can_redo() or true, "collapsing")
|
|
var gpos := main.data.get_group("g1").position
|
|
var moved: int = await main.pack_collapsed_groups()
|
|
_check(moved > 0 and _label() == "Pack collapsed groups", "Pack is a step: %s" % _label())
|
|
main.undo()
|
|
await _frames()
|
|
_check(main.data.get_group("g1").position == gpos, "undo puts the group back")
|
|
_check(main.data.get_group("g1").collapsed, "and leaves it collapsed")
|
|
main.set_group_collapsed("g1", false)
|
|
await _frames(3)
|
|
|
|
|
|
func _test_redo_cleared() -> void:
|
|
print("Redo is cleared by a new edit")
|
|
main.add_entry("b")
|
|
main.undo()
|
|
_check(main.edits.can_redo(), "after undo, redo is offered")
|
|
main.add_entry("a")
|
|
_check(not main.edits.can_redo(), "a new edit clears it")
|
|
main.undo()
|
|
|
|
|
|
func _test_view_kept() -> void:
|
|
print("View, lit state, search and mode")
|
|
await _frames(3)
|
|
main.board.stop_animation()
|
|
main.board.set_zoom(0.6)
|
|
main.board.world.position = Vector2(-123, 45)
|
|
main.lit_entries["a:e1"] = true
|
|
main.lit_entries["b:e1"] = true
|
|
var panel := main.open_search()
|
|
panel.field.text = "loot"
|
|
panel.refresh()
|
|
main.delete_table("b")
|
|
main.undo()
|
|
await _frames()
|
|
_check(is_equal_approx(main.board.zoom, 0.6) and main.board.world.position == Vector2(-123, 45), "zoom and pan stay")
|
|
_check(main.lit_entries.has("a:e1") and main.lit_entries.has("b:e1") and main.is_entry_lit("b", "e1"), "lit results stay lit, also on the table brought back")
|
|
_check(panel.visible and panel.field.text == "loot", "the search stays open")
|
|
_check(main.mode == Main.Mode.BUILD, "and the mode stays")
|
|
panel.close()
|
|
main.lit_entries.clear()
|
|
# A change on screen flashes.
|
|
main.board.fit()
|
|
await _frames()
|
|
main.add_entry("a")
|
|
main.undo()
|
|
await _frames(3)
|
|
_check(main.board.cards.is_flashing() and main.board.cards.flash_rect() == Rect2(main.card_for("a").position, main.card_for("a").size), "the changed card flashes")
|
|
|
|
|
|
func _test_toast() -> void:
|
|
print("Toast Undo")
|
|
main.delete_entry("a", "e2")
|
|
_check(main._toast.visible and main._toast_undo.visible, "deleting shows an undo toast")
|
|
main._toast_undo.pressed.emit()
|
|
_check(_t("a").find_entry("e2") != null and main.edits.can_redo(), "its Undo goes through the history (redo is offered)")
|
|
main.redo()
|
|
main.undo()
|
|
main.delete_table("b")
|
|
_check(main._toast_undo.visible, "another undo toast")
|
|
main.add_entry("a")
|
|
_check(not main._toast_undo.visible, "a newer edit hides the toast's Undo, so it can't undo the wrong thing")
|
|
main.undo()
|
|
main.undo()
|
|
_check(_t("b") != null, "undo twice brings the table back")
|
|
|
|
|
|
func _test_lazy_cards() -> void:
|
|
print("Lazily built cards")
|
|
main.set_group_collapsed("g1", true)
|
|
await _frames(3)
|
|
_check(main.card_for("c") == null, "Weapons is hidden in the collapsed group, with no card")
|
|
_t("c").entries[0].text = "Axe"
|
|
main.mark_dirty()
|
|
await _frames()
|
|
_check(_label() == "Edit Weapons", "an edit to a hidden card is a step: %s" % _label())
|
|
main.undo()
|
|
await _frames(3)
|
|
_check(_t("c").entries[0].text == "Sword" and main.card_for("c") == null, "undo restores it and still doesn't build the card")
|
|
_check(main.data.get_group("g1").collapsed and main.group_node("g1") != null, "the group stays collapsed")
|
|
main.delete_group("g1")
|
|
main.undo()
|
|
await _frames(3)
|
|
_check(main.data.get_group("g1").collapsed and main.card_for("c") == null, "a deleted collapsed group comes back collapsed, cards unbuilt")
|
|
main.set_group_collapsed("g1", false)
|
|
await _frames(3)
|
|
var c := main.card_for("c")
|
|
_check(c != null and c.visible and _line_edit_with(c, "Sword") != null, "expanding it builds the restored card")
|
|
|
|
|
|
func _test_collapse_not_undoable() -> void:
|
|
print("Collapsing isn't a step")
|
|
var n0 := _steps()
|
|
main.toggle_collapse("a")
|
|
main.set_all_collapsed(true)
|
|
main.set_all_collapsed(false)
|
|
main.toggle_collapse("b")
|
|
await _frames()
|
|
_check(_steps() == n0, "collapsing and expanding add no steps")
|
|
main.add_entry("a")
|
|
main.undo()
|
|
_check(_t("b").collapsed, "undo leaves Gems collapsed as it is")
|
|
main.toggle_collapse("b")
|
|
|
|
|
|
func _test_shortcuts_and_buttons() -> void:
|
|
print("Shortcuts and buttons")
|
|
main.add_entry("a")
|
|
_check(not main._undo_button.disabled and main._undo_button.tooltip_text == "Undo: Add result to Loot (Ctrl+Z)", "the Undo button names the step: %s" % main._undo_button.tooltip_text)
|
|
_check(main._redo_button.disabled, "Redo is disabled with nothing to redo")
|
|
main._shortcut_input(_key(KEY_Z, false))
|
|
_check(_t("a").entries.size() == 2, "Ctrl+Z undoes")
|
|
_check(not main._redo_button.disabled and main._redo_button.tooltip_text.begins_with("Redo: Add result to Loot"), "Redo is enabled and named: %s" % main._redo_button.tooltip_text)
|
|
main._shortcut_input(_key(KEY_Z, true))
|
|
_check(_t("a").entries.size() == 3, "Ctrl+Shift+Z redoes")
|
|
main._shortcut_input(_key(KEY_Z, false))
|
|
main._shortcut_input(_key(KEY_Y, false))
|
|
_check(_t("a").entries.size() == 3, "Ctrl+Y redoes too")
|
|
var le := _line_edit_with(main.card_for("a"), "Loot")
|
|
le.grab_focus()
|
|
main._shortcut_input(_key(KEY_Z, false))
|
|
_check(_t("a").entries.size() == 3, "with a text field focused, Ctrl+Z is left to the field")
|
|
le.release_focus()
|
|
main._undo_button.pressed.emit()
|
|
_check(_t("a").entries.size() == 2, "the Undo button undoes")
|
|
while main.edits.can_undo():
|
|
main.undo()
|
|
_check(main._undo_button.disabled and main._undo_button.tooltip_text == "Nothing to undo", "with nothing left, Undo is disabled")
|
|
main.redo()
|
|
|
|
|
|
func _key(code: Key, shift: bool) -> InputEventKey:
|
|
var k := InputEventKey.new()
|
|
k.keycode = code
|
|
k.ctrl_pressed = true
|
|
k.shift_pressed = shift
|
|
k.pressed = true
|
|
return k
|
|
|
|
|
|
func _test_play_mode() -> void:
|
|
print("Play mode")
|
|
main.set_mode(Main.Mode.PLAY)
|
|
var card := main.card_for("b")
|
|
var p0 := card.position
|
|
main.begin_card_drag(card)
|
|
main.drag_card(card, p0 + Vector2(200, 0))
|
|
main.end_card_drag(card)
|
|
await _frames()
|
|
_check(_label() == "Move Gems", "moving a card in Play is a step too")
|
|
main._shortcut_input(_key(KEY_Z, false))
|
|
_check(_t("b").position == p0 and main.mode == Main.Mode.PLAY, "Ctrl+Z undoes it in Play, staying in Play")
|
|
main.set_mode(Main.Mode.BUILD)
|
|
|
|
|
|
func _test_collections() -> void:
|
|
print("Per-collection history")
|
|
main.add_entry("a")
|
|
var first := main.data
|
|
var h := main.edits
|
|
var other := TowerBoard.new()
|
|
other.load_dict({"format_version": 2, "id": "other", "name": "Other", "tables": [
|
|
{"id": "x", "name": "X", "x": 0, "y": 0, "entries": [{"id": "e1", "text": "x"}]}]})
|
|
await main._show_collection(other)
|
|
_check(main.edits != h and not main.edits.can_undo(), "another collection has its own, empty history")
|
|
main.add_entry("x")
|
|
_check(main.edits.undo_label() == "Add result to X", "edits there go to its history")
|
|
await main._show_collection(first)
|
|
_check(main.edits == h and main.edits.undo_label() == "Add result to Loot", "switching back keeps the first history")
|
|
var n_before := _t("a").entries.size()
|
|
main.undo()
|
|
_check(_t("a").entries.size() == n_before - 1, "and undo still works: %d -> %d" % [n_before, _t("a").entries.size()])
|
|
|
|
|
|
func _test_big_board() -> void:
|
|
print("Big board")
|
|
var d: Dictionary = {}
|
|
var path := ""
|
|
for a in OS.get_cmdline_user_args():
|
|
if a.begins_with("--big-board="):
|
|
path = a.trim_prefix("--big-board=")
|
|
if path != "" and FileAccess.file_exists(path):
|
|
d = JSON.parse_string(FileAccess.get_file_as_string(path))
|
|
else:
|
|
d = _synthetic_board(800)
|
|
await _load(d)
|
|
var t := main.data.tables[main.data.tables.size() / 2]
|
|
var u := main.data.tables[main.data.tables.size() / 3]
|
|
var n := main.data.tables.size()
|
|
var full := EditHistory.full_bytes(main.edits.cache)
|
|
# One committed edit: what typing costs once it pauses.
|
|
t.name = t.name + " (edited)"
|
|
main.mark_dirty()
|
|
var t1 := Time.get_ticks_usec()
|
|
main._commit_edits()
|
|
var commit_ms := (Time.get_ticks_usec() - t1) / 1000.0
|
|
var cap_ms := main.edits.last_capture_usec / 1000.0
|
|
u.name = u.name + " (edited)"
|
|
main.mark_dirty()
|
|
main._commit_edits()
|
|
# The oldest step holds the whole board; later ones only what changed.
|
|
var step_bytes := int(main.edits.undo_stack.back()["snap"]["bytes"])
|
|
# A keystroke: the history's part (restarting a timer) against all of mark_dirty.
|
|
var le := LineEdit.new()
|
|
main.add_child(le)
|
|
le.grab_focus()
|
|
var t2 := Time.get_ticks_usec()
|
|
for i in 20:
|
|
t.name += "x"
|
|
main.mark_dirty()
|
|
var key_ms := (Time.get_ticks_usec() - t2) / 1000.0 / 20.0
|
|
var t4 := Time.get_ticks_usec()
|
|
for i in 20:
|
|
main._board_changed()
|
|
var base_ms := (Time.get_ticks_usec() - t4) / 1000.0 / 20.0
|
|
_check(main.edits.undo_stack.size() == 2, "keystrokes in a text field don't commit until typing pauses")
|
|
le.release_focus()
|
|
le.queue_free()
|
|
main._commit_edits()
|
|
var t3 := Time.get_ticks_usec()
|
|
main.undo()
|
|
var undo_ms := (Time.get_ticks_usec() - t3) / 1000.0
|
|
var undo_one_ms := main._last_restore_usec / 1000.0
|
|
# Undoing a deletion rebuilds the whole board.
|
|
main.delete_table(u.id)
|
|
var t5 := Time.get_ticks_usec()
|
|
main.undo()
|
|
var undo_del_ms := (Time.get_ticks_usec() - t5) / 1000.0
|
|
_check(_t(u.id) != null, "undoing a deletion on the big board brings the card back")
|
|
print(" TIMING %d cards: one committed edit %.1f ms (snapshot %.1f ms); keystroke %.2f ms, of which mark_dirty's existing refresh %.2f ms; undo of an edit %.1f ms (board update %.1f ms); undo of a deletion %.1f ms; board %d chars, later step %d bytes" % [
|
|
n, commit_ms, cap_ms, key_ms, base_ms, undo_ms, undo_one_ms, undo_del_ms, full, step_bytes])
|
|
_check(commit_ms < 250.0, "committing one edit stays quick: %.1f ms" % commit_ms)
|
|
_check(key_ms - base_ms < 1.0, "a keystroke doesn't snapshot the board: %.2f ms over %.2f ms" % [key_ms, base_ms])
|
|
_check(step_bytes < full / 10, "a later step stores only the changed cards: %d of %d" % [step_bytes, full])
|
|
_check(_t(t.id).name.ends_with("(edited)") and not _t(t.id).name.ends_with("x"), "undo works on the big board")
|
|
|
|
|
|
func _synthetic_board(n: int) -> Dictionary:
|
|
var tables: Array = []
|
|
var groups: Array = []
|
|
for i in n:
|
|
var entries: Array = []
|
|
for j in 8:
|
|
entries.append({"id": "e%d" % j, "text": "Result %d of table %d with some text" % [j, i], "pipes": ["t%d" % ((i + 1) % n)] if j == 0 else []})
|
|
tables.append({"id": "t%d" % i, "name": "Table %d" % i, "x": (i % 40) * 300, "y": (i / 40) * 400, "entries": entries})
|
|
for k in n / 40:
|
|
var members: Array = []
|
|
for i in range(k * 40, k * 40 + 40):
|
|
members.append("t%d" % i)
|
|
groups.append({"id": "g%d" % k, "name": "Group %d" % k, "x": 0, "y": k * 400, "w": 12000, "h": 380, "members": members, "collapsed": true})
|
|
return {"format_version": 2, "id": "big", "name": "Big", "tables": tables, "groups": groups}
|