TableTower/tests/test_sheet_editor.gd
RobinFiredrake dd0323c53f Sheets phase C: every field kind, details panel, canvas panning
- 25 palette items over 23 kinds: basics, trackers (bars, ticks, clocks,
  dots, step/usage dice, ladders), formulas and dice buttons (rule
  language with sheet @names), lists (rows, mini table, chips, point pool
  that may go negative, bonds), slots, conditions, spell slots, XP,
  currency, heading, divider, picture.
- Per-kind settings in the inspector; hover excerpts and an in-app
  details panel for list entries; live redraw of dependent boxes.
- Canvas: the page stays put after edits (boxes can go anywhere), a
  screen of room past the boxes, drag-to-pan and edge scrolling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 16:28:04 -06:00

215 lines
9 KiB
GDScript

extends AppTestBase
## The main menu and the sheet editor in the app: switching screens, making a
## sheet, adding boxes from the field palette, moving them on the snap grid,
## undo, tabs, number fields, saving, and keys staying on their own screen.
## Run: tools/run_tests.sh sheet_editor
func run() -> void:
if not await start_main(false):
return
await _test_menu()
await _test_new_sheet()
await _test_boxes()
await _test_tabs()
await _test_keys()
await _test_kinds()
finish()
func _test_menu() -> void:
var screens := main.screens
_check(screens.showing(MainScreens.SPREAD), "tests start on the spread")
main.top_bar.brand_title.get_parent().gui_input.emit(_click())
_check(screens.menu.visible and not screens.spread_root.visible, "clicking the logo opens the main menu")
var labels: Array = screens.menu.buttons.keys()
_eq(labels, ["Spreads", "Sheets", "Settings", "Quit"], "the menu lists Spreads first, titles only")
(screens.menu.buttons["Sheets"] as Button).pressed.emit()
_check(screens.sheets.visible and screens.sheets.list_page.visible, "Sheets opens the sheet list")
await _frames()
func _test_new_sheet() -> void:
var sheets := main.screens.sheets
sheets.open_new_sheet()
await _frames()
var dialog: NewSheetDialog = null
for c in main.get_children():
if c is NewSheetDialog:
dialog = c
_check(dialog != null, "+ New sheet asks for a name")
dialog.name_edit.text = "Rusty Mule"
dialog._create()
await _frames()
var ed := sheets.editor
_check(ed.visible and ed.doc.name == "Rusty Mule" and ed.building(), "a blank sheet opens in Build")
_eq(sheets.store.list().size(), 1, "it is saved")
func _test_boxes() -> void:
var ed := main.screens.sheets.editor
(ed.palette.list.find_child("Number", true, false) as Button).pressed.emit()
await _frames()
_eq((ed.current_tab()["boxes"] as Array).size(), 1, "clicking Number adds a box")
var box: SheetBox = ed.current_tab()["boxes"][0]
_eq(ed.selected, box.id, "the new box is selected")
var spot := ed.canvas.global_position + ed.canvas.size / 2.0 + Vector2(17, 9)
var want := ed.canvas.drop_point(spot)
_check(ed.palette.drop_at(ed.palette._add.bind(SheetKinds.TEXT), spot), "dropping on the canvas adds there")
var text_box: SheetBox = ed.current_tab()["boxes"][1]
_eq(Vector2(text_box.x, text_box.y), Vector2(SheetKinds.snap(want.x), SheetKinds.snap(want.y)), "a dropped box snaps to the grid")
var view: SheetBoxView = ed.canvas.views[box.id]
view.begin_drag("move")
view.drag_to(Vector2(37, 21), false)
view.end_drag()
_eq(Vector2(box.x, box.y).posmod(SheetKinds.GRID), Vector2.ZERO, "moving snaps to the grid")
var snapped := Vector2(box.x, box.y)
view = ed.canvas.views[box.id]
view.begin_drag("move")
view.drag_to(Vector2(5, 3), true)
view.end_drag()
_eq(Vector2(box.x, box.y), snapped + Vector2(5, 3), "Alt places it off the grid")
ed.undo()
box = ed.doc.find_box(box.id)
_eq(Vector2(box.x, box.y), snapped, "undo puts the box back")
await _frames()
var page_x := ed.canvas.area.position.x
view = ed.canvas.views[box.id]
view.begin_drag("move")
view.drag_to(Vector2(ed.canvas.size.x * 1.5, 0), false)
view.end_drag()
ed.canvas._fit_area()
_eq(ed.canvas.area.position.x, page_x, "moving a box far right doesn't shift the page")
_check(ed.canvas.holder.custom_minimum_size.x > box.x + box.w + ed.canvas.size.x * 0.9, "the canvas keeps a screen of room past the box")
ed.canvas.pan_to(Vector2(300, 0))
await _frames()
_eq(ed.canvas.scroll_horizontal, 300, "panning scrolls the canvas")
ed.undo()
box = ed.doc.find_box(box.id)
ed.redo()
ed.set_mode(Main.Mode.PLAY)
await _frames()
_check(ed.palette.notice.visible and not ed.inspector.visible, "Play greys the palette and hides the inspector")
view = ed.canvas.views[box.id]
(view.find_child("Plus", true, false) as Button).pressed.emit()
_eq(int(ed.doc.find_box(box.id).value), 1, "+ adds one in Play")
ed.undo()
_eq(int(ed.doc.find_box(box.id).value), 0, "a value change can be undone")
ed.save_now()
var saved := main.screens.sheets.store.load_sheet(ed.doc.id)
_eq((saved.tabs[0]["boxes"] as Array).size(), 2, "the sheet's boxes are saved")
ed.set_mode(Main.Mode.BUILD)
func _test_tabs() -> void:
var ed := main.screens.sheets.editor
ed.tabs_bar.add_tab()
await _frames()
_check(ed.doc.tabs.size() == 2 and ed.tab_id == str(ed.doc.tabs[1]["id"]), "+ Tab adds a tab and opens it")
_eq(ed.canvas.views.size(), 0, "a new tab has its own empty canvas")
var second := str(ed.doc.tabs[1]["id"])
ed.tabs_bar.move_tab(second, -1)
_eq(str(ed.doc.tabs[0]["id"]), second, "Move left moves it")
ed.tabs_bar.delete_tab(second)
_eq(ed.doc.tabs.size(), 1, "an empty tab deletes without asking")
_check(ed.tabs_bar.menu_for(str(ed.doc.tabs[0]["id"]))[3]["disabled"], "the last tab can't be deleted")
ed.undo()
_eq(ed.doc.tabs.size(), 2, "undo brings the tab back")
func _test_keys() -> void:
var ed := main.screens.sheets.editor
var spread_label := main.undo_redo.edits.undo_label() if main.undo_redo.edits else ""
var key := InputEventKey.new()
key.keycode = KEY_Z
key.ctrl_pressed = true
key.pressed = true
var step := ed.history.undo_label()
main.layout._shortcut_input(key)
_eq(ed.history.redo_label(), step, "Ctrl+Z on the Sheets screen undoes the sheet")
_eq(main.undo_redo.edits.undo_label() if main.undo_redo.edits else "", spread_label, "and leaves the spread alone")
ed.screen.show_list()
_check(ed.screen.list_page.visible and ed.doc == null, "← Sheets closes the sheet")
main.screens.show(MainScreens.SPREAD)
## The example sheet: every kind draws in Play and Build; clicks and typing
## change values (and undo); lists add, remove and go over their pool; ⓘ
## opens the details panel; dice roll; a setting change redraws the box.
func _test_kinds() -> void:
var sheets := main.screens.sheets
main.screens.show(MainScreens.SHEETS)
var doc := SheetDemo.create("kinds-demo")
sheets.store.save(doc)
sheets.open_doc(doc)
var ed := sheets.editor
ed.set_mode(Main.Mode.PLAY)
for tab in ed.doc.tabs:
ed.show_tab(str(tab["id"]))
_eq(ed.canvas.views.size(), (tab["boxes"] as Array).size(), "every box on %s draws" % tab["name"])
ed.set_mode(Main.Mode.BUILD)
_eq(ed.canvas.views.size(), (ed.current_tab()["boxes"] as Array).size(), "and draws in Build")
ed.set_mode(Main.Mode.PLAY)
ed.show_tab(str(ed.doc.tabs[0]["id"]))
var stress := _box(ed, "Stress")
_press(ed, stress, "Mark4")
_eq(int(stress.value), 4, "clicking the 4th tick box marks four")
ed.undo()
_eq(int(_box(ed, "Stress").value), 2, "undo puts the ticks back")
var talents := _box(ed, "Talents")
_press(ed, talents, "Add")
_eq((_box(ed, "Talents").value as Array).size(), 4, "+ Add adds an entry")
var cost: LineEdit = _view(ed, "Talents").find_child("Row3", true, false).find_child("Cost", true, false)
cost.text = "9"
cost.text_changed.emit("9")
var pool: Label = _view(ed, "Talents").find_child("Pool", true, false)
_check(pool.text.contains("over by 7"), "the pool shows red past zero: " + pool.text)
_press(ed, _box(ed, "Talents"), "Details")
_check(ed.details.visible and ed.details.index == 0, "ⓘ opens the details panel")
var details: TextEdit = ed.details.find_child("DetailsText", true, false)
details.text = "New text"
details.text_changed.emit()
_eq(str((_box(ed, "Talents").value as Array)[0]["details"]), "New text", "typing in the panel saves the details")
var row: Control = _view(ed, "Talents").find_child("Row0", true, false)
_check(row.tooltip_text.begins_with("Shield Wall\nNew text"), "hovering an entry shows its details")
_press(ed, _box(ed, "Conditions"), "Poisoned")
_check((_box(ed, "Conditions").value as Array).has("Poisoned"), "a condition chip switches on")
_press(ed, _box(ed, "Longsword"), "Roll")
_check(str(_box(ed, "Longsword").value).begins_with("Rolled"), "the dice button rolls")
(_view(ed, "Strength").find_child("Plus", true, false) as Button).pressed.emit()
var shown: Label = _view(ed, "STR mod").find_child("Value", true, false)
_eq(shown.text, "2", "a formula follows the number it reads (15 → still 2)")
(_view(ed, "Strength").find_child("Plus", true, false) as Button).pressed.emit()
shown = _view(ed, "STR mod").find_child("Value", true, false)
_eq(shown.text, "3", "and changes when it should (16 → 3)")
ed.set_mode(Main.Mode.BUILD)
ed.select(talents.id)
var chips: Button = ed.inspector.find_child("chips", true, false)
chips.pressed.emit()
_eq(str(_box(ed, "Talents").config["style"]), "chips", "the inspector switches a list to chips")
_check(_view(ed, "Talents").find_child("Chip0", true, false) != null, "and the box shows chips")
sheets.show_list()
func _box(ed: SheetEditor, title: String) -> SheetBox:
for tab in ed.doc.tabs:
for b: SheetBox in tab["boxes"]:
if b.title == title:
return b
return null
func _view(ed: SheetEditor, title: String) -> SheetBoxView:
return ed.canvas.views.get(_box(ed, title).id)
func _press(ed: SheetEditor, box: SheetBox, child: String) -> void:
var b: Button = (ed.canvas.views[box.id] as SheetBoxView).find_child(child, true, false)
b.pressed.emit()
func _click() -> InputEventMouseButton:
var mb := InputEventMouseButton.new()
mb.button_index = MOUSE_BUTTON_LEFT
mb.pressed = true
return mb