From 976b0e98bfbeb4afe5d51521a08e72149a7f7f07 Mon Sep 17 00:00:00 2001 From: torsten Date: Fri, 30 Oct 2026 08:20:48 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20Vollst=C3=A4ndiges=20Undo/Redo=20mit=20?= =?UTF-8?q?Snapshot-Historie=20hinzuf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Undo/Redo für Segmente, Griffe, Richtung/Kante und Section-Änderungen - Snapshot-Historie mit 50 Einträgen, aktive Section wird wiederhergestellt - Strg+Z, Strg+Shift+Z und Strg+Y; Toolbar-Buttons zeigen den Zustand - Pan/Zoom bleiben bei Undo/Redo erhalten - Neue und geladene Kürläufe starten eine frische Historie - Tests auf 312 Assertions erweitert --- docs/architecture.md | 18 +- rinkplanner/scenes/whiteboard/whiteboard.tscn | 17 +- .../src/whiteboard/ctrl/history_entry.gd | 12 + .../src/whiteboard/ctrl/history_entry.gd.uid | 1 + .../whiteboard/ctrl/whiteboard_controller.gd | 216 +++++++++++++-- .../src/whiteboard/view/rink_canvas.gd | 7 +- rinkplanner/tests/README.md | 3 + rinkplanner/tests/run_history_tests.gd | 247 ++++++++++++++++++ rinkplanner/tests/run_history_tests.gd.uid | 1 + rinkplanner/tests/run_whiteboard_tests.gd | 6 +- tools/test.sh | 3 + 11 files changed, 502 insertions(+), 29 deletions(-) create mode 100644 rinkplanner/src/whiteboard/ctrl/history_entry.gd create mode 100644 rinkplanner/src/whiteboard/ctrl/history_entry.gd.uid create mode 100644 rinkplanner/tests/run_history_tests.gd create mode 100644 rinkplanner/tests/run_history_tests.gd.uid diff --git a/docs/architecture.md b/docs/architecture.md index 9a3102d..c2261ab 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -122,6 +122,11 @@ eine Abkürzung. So kann die UI Fehler konkret anzeigen. Speichern unter. Ein Dirty-State markiert ungespeicherte Änderungen im Fenstertitel; Neu/Öffnen fragen vor dem Verwerfen. Autosave und Restore laufen unabhängig davon im Hintergrund. +- **Undo/Redo:** Snapshot-Historie mit maximal 50 Einträgen über Segment-, + Griff-, Richtungs-/Kanten- und Section-Änderungen. Die Historie stellt die + aktive Section wieder her, nicht die Zoom-/Pan-Ansicht. Strg+Z, + Strg+Shift+Z und Strg+Y; die Toolbar-Buttons sind am jeweiligen Ende der + Historie deaktiviert. Öffnen und Neu starten eine neue Historie. - **Darstellung:** vorwärts durchgezogen, rückwärts gestrichelt, außen rot, innen blau. Inaktive Sections werden mit 20 % Deckkraft geghostet. - **Ghosting** ist reine View-Logik: pro Section ein eigener Layer, @@ -184,9 +189,12 @@ Aktueller Stand: Rink-Kontur und die reine Bogen-Geometrie. - **Whiteboard:** 129 Assertions für Controller-Verdrahtung, Segmenterzeugung, Linien-/Bogenpfade, Klemmen an der Bande, Auswahl, Griff- und - Richtungs-/Kanten-Bearbeitung, Segmentlöschung, Undo, Section-Verwaltung, + Richtungs-/Kanten-Bearbeitung, Segmentlöschung, Section-Verwaltung, Ghosting-Zustand, Speichern/Laden, Schema-Prüfung, Dirty-State, Navigationsmodus und den Maus-Drag-Pfad. +- **History:** 47 Assertions für Undo/Redo von Segmenten, Kanten, Sections, + Zeichengesten und Griff-Bearbeitung, Toolbar-Buttons, Erhalt der Ansicht + sowie das Zurücksetzen der Historie. Autosave/Restore wird zusätzlich in einem isolierten `XDG_DATA_HOME` end-to-end geprüft. @@ -201,8 +209,9 @@ Viewport-Screenshot; sie ersetzen die headless Tests nicht. 2. **Element-Instanzen:** Aktuell verweist `PathSegment.element_id` auf ein Element. Ob Sprünge/Pirouetten eigene Instanzen mit Position und Zeit brauchen, wird mit dem Regelmodell entschieden. -3. **Undo/Redo:** Aktuell entfernt „Rückgängig“ nur das letzte Segment. - Vollständiges Undo/Redo (Command-Pattern oder Snapshots) bleibt offen. +3. **History-Grenzen:** 50 Snapshots ohne Coalescing; Undo zurück zum + gespeicherten Stand setzt den Dirty-Marker nicht automatisch zurück. Für + lange Sessions ist zu prüfen, ob das reicht. 4. **Section-Übergänge:** Ob Sections eine explizite Startzeit erhalten oder ausschließlich aus Segmentdauern abgeleitet werden, klärt die Playback-Implementierung. @@ -221,3 +230,6 @@ Viewport-Screenshot; sie ersetzen die headless Tests nicht. 9. **Dateinamen:** Die Normalisierung ersetzt Leerzeichen und entfernt Pfadzeichen, transliteriert aber keine Umlaute. Für Web/Windows ist zu prüfen, ob eine ASCII-Normalisierung robuster ist. +10. **Snapshot-Clone:** `_clone_routine()` kopiert die Modellfelder manuell. + Neue Felder am Modell müssen dort ergänzt werden, sonst fehlen sie in + Undo/Redo-Snapshots. diff --git a/rinkplanner/scenes/whiteboard/whiteboard.tscn b/rinkplanner/scenes/whiteboard/whiteboard.tscn index 65eeff0..ed3d921 100644 --- a/rinkplanner/scenes/whiteboard/whiteboard.tscn +++ b/rinkplanner/scenes/whiteboard/whiteboard.tscn @@ -141,10 +141,21 @@ size_flags_horizontal = 3 [node name="UndoButton" type="Button" parent="Layout/TopBar/Tools"] unique_name_in_owner = true -custom_minimum_size = Vector2(100, 48) +custom_minimum_size = Vector2(48, 48) layout_mode = 2 -theme_override_font_sizes/font_size = 16 -text = "Rückgängig" +theme_override_font_sizes/font_size = 20 +disabled = true +text = "↶" +tooltip_text = "Rückgängig (Strg+Z)" + +[node name="RedoButton" type="Button" parent="Layout/TopBar/Tools"] +unique_name_in_owner = true +custom_minimum_size = Vector2(48, 48) +layout_mode = 2 +theme_override_font_sizes/font_size = 20 +disabled = true +text = "↷" +tooltip_text = "Wiederholen (Strg+Shift+Z)" [node name="FitButton" type="Button" parent="Layout/TopBar/Tools"] unique_name_in_owner = true diff --git a/rinkplanner/src/whiteboard/ctrl/history_entry.gd b/rinkplanner/src/whiteboard/ctrl/history_entry.gd new file mode 100644 index 0000000..4a8516e --- /dev/null +++ b/rinkplanner/src/whiteboard/ctrl/history_entry.gd @@ -0,0 +1,12 @@ +class_name HistoryEntry +extends RefCounted + +## One undo/redo snapshot of the document plus the section that was active. + +var routine: Routine +var active_section_id: String + + +func _init(p_routine: Routine, p_active_section_id: String) -> void: + routine = p_routine + active_section_id = p_active_section_id diff --git a/rinkplanner/src/whiteboard/ctrl/history_entry.gd.uid b/rinkplanner/src/whiteboard/ctrl/history_entry.gd.uid new file mode 100644 index 0000000..9127240 --- /dev/null +++ b/rinkplanner/src/whiteboard/ctrl/history_entry.gd.uid @@ -0,0 +1 @@ +uid://c4angcb7icll3 diff --git a/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd b/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd index 40ccca1..175ae80 100644 --- a/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd +++ b/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd @@ -24,6 +24,7 @@ const EPSILON := 0.0001 const ROUTINES_DIR := "user://routines" const AUTOSAVE_PATH := "user://autosave.tres" const AUTOSAVE_DELAY_SECONDS := 1.5 +const MAX_HISTORY := 50 ## Tests disable these before adding the scene to the tree. @export var restore_autosave: bool = true @@ -36,6 +37,7 @@ const AUTOSAVE_DELAY_SECONDS := 1.5 @onready var inside_button: Button = %InsideButton @onready var outside_button: Button = %OutsideButton @onready var undo_button: Button = %UndoButton +@onready var redo_button: Button = %RedoButton @onready var fit_button: Button = %FitButton @onready var status_label: Label = %StatusLabel @onready var delete_segment_button: Button = %DeleteSegmentButton @@ -70,6 +72,9 @@ var _edge: PathSegment.Edge = PathSegment.Edge.OUTSIDE var _current_path: String = "" var _dirty: bool = false var _pending_action: Callable +var _undo_stack: Array[HistoryEntry] = [] +var _redo_stack: Array[HistoryEntry] = [] +var _pending_history: HistoryEntry func _ready() -> void: @@ -86,7 +91,8 @@ func _ready() -> void: ) inside_button.pressed.connect(_on_edge_pressed.bind(PathSegment.Edge.INSIDE)) outside_button.pressed.connect(_on_edge_pressed.bind(PathSegment.Edge.OUTSIDE)) - undo_button.pressed.connect(undo_last_segment) + undo_button.pressed.connect(undo) + redo_button.pressed.connect(redo) fit_button.pressed.connect(rink_canvas.fit_rink) delete_segment_button.pressed.connect(delete_selected_segment) add_section_button.pressed.connect(add_section) @@ -186,6 +192,7 @@ func get_selected_segment() -> PathSegment: func add_section() -> RoutineSection: + _record_undo() _section_counter += 1 var section := RoutineSection.new() @@ -221,6 +228,7 @@ func rename_section(section_id: String, new_title: String) -> bool: if section == null or title.is_empty(): return false + _record_undo() section.title = title _mark_dirty() _rebuild_section_list() @@ -236,6 +244,7 @@ func delete_section(section_id: String) -> bool: if index < 0: return false + _record_undo() var was_active := section_id == _active_section.section_id _routine.sections.remove_at(index) _renumber_sections() @@ -260,6 +269,7 @@ func move_section(section_id: String, delta: int) -> bool: if target == index: return false + _record_undo() var section: RoutineSection = _routine.sections[index] _routine.sections.remove_at(index) _routine.sections.insert(target, section) @@ -311,6 +321,7 @@ func add_segment( edge: PathSegment.Edge, tool: Tool = Tool.LINE ) -> PathSegment: + _record_undo() var segment := _create_segment(start, direction, edge) _update_segment_geometry(segment, end, tool) _active_section.segments.append(segment) @@ -320,21 +331,135 @@ func add_segment( return segment -func undo_last_segment() -> void: - if _current_segment != null: - _on_draw_cancelled() +func undo() -> bool: + if _pending_history != null: + rink_canvas.cancel_interaction() + + if _undo_stack.is_empty(): + return false + + _redo_stack.append(_make_history_entry()) + var entry: HistoryEntry = _undo_stack.pop_back() + _restore_history_entry(entry) + _update_history_buttons() + return true + + +func redo() -> bool: + if _pending_history != null: + rink_canvas.cancel_interaction() + + if _redo_stack.is_empty(): + return false + + _undo_stack.append(_make_history_entry()) + var entry: HistoryEntry = _redo_stack.pop_back() + _restore_history_entry(entry) + _update_history_buttons() + return true + + +func _restore_history_entry(entry: HistoryEntry) -> void: + _install_routine(entry.routine, entry.active_section_id, false) + _dirty = true + if autosave_enabled: + autosave_timer.start() + _update_window_title() + _save_autosave() + + +func _record_undo() -> void: + if _routine == null: return - if _active_section == null or _active_section.segments.is_empty(): + _push_undo_entry(_make_history_entry()) + + +func _push_undo_entry(entry: HistoryEntry) -> void: + if entry == null: return - var removed: PathSegment = _active_section.segments.pop_back() - if removed != null and removed.segment_id == _selected_segment_id: - _selected_segment_id = "" - rink_canvas.set_selected_segment("") + _undo_stack.append(entry) + if _undo_stack.size() > MAX_HISTORY: + _undo_stack.pop_front() + _redo_stack.clear() + _update_history_buttons() - _mark_dirty() - rink_canvas.refresh() - _update_status() + +func _make_history_entry() -> HistoryEntry: + var active_section_id := "" + if _active_section != null: + active_section_id = _active_section.section_id + return HistoryEntry.new(_clone_routine(_routine), active_section_id) + + +func _update_history_buttons() -> void: + undo_button.disabled = _undo_stack.is_empty() + redo_button.disabled = _redo_stack.is_empty() + + +func _clone_routine(source: Routine) -> Routine: + var clone := Routine.new() + clone.schema_version = source.schema_version + clone.routine_id = source.routine_id + clone.title = source.title + clone.skater_name = source.skater_name + if source.rink != null: + clone.rink = _clone_rink(source.rink) + + var sections: Array[RoutineSection] = [] + for section: RoutineSection in source.sections: + if section != null: + sections.append(_clone_section(section)) + clone.sections = sections + return clone + + +func _clone_rink(source: RinkDimensions) -> RinkDimensions: + var clone := RinkDimensions.new() + clone.length_m = source.length_m + clone.width_m = source.width_m + clone.corner_radius_m = source.corner_radius_m + return clone + + +func _clone_section(source: RoutineSection) -> RoutineSection: + var clone := RoutineSection.new() + clone.section_id = source.section_id + clone.title = source.title + clone.sequence_order = source.sequence_order + + var segments: Array[PathSegment] = [] + for segment: PathSegment in source.segments: + if segment != null: + segments.append(_clone_segment(segment)) + clone.segments = segments + return clone + + +func _clone_segment(source: PathSegment) -> PathSegment: + var clone := PathSegment.new() + clone.segment_id = source.segment_id + clone.direction = source.direction + clone.edge = source.edge + clone.duration_seconds = source.duration_seconds + clone.element_id = source.element_id + clone.label = source.label + clone.template = source.template + clone.arc_depth_ratio = source.arc_depth_ratio + clone.arc_bulge_left = source.arc_bulge_left + + if source.curve != null: + var curve := Curve2D.new() + curve.bake_interval = source.curve.bake_interval + for index: int in range(source.curve.point_count): + curve.add_point( + source.curve.get_point_position(index), + source.curve.get_point_in(index), + source.curve.get_point_out(index) + ) + clone.curve = curve + + return clone func delete_selected_segment() -> bool: @@ -342,6 +467,7 @@ func delete_selected_segment() -> bool: if segment == null or _active_section == null: return false + _record_undo() _active_section.segments.erase(segment) _selected_segment_id = "" _mark_dirty() @@ -358,7 +484,31 @@ func _unhandled_key_input(event: InputEvent) -> void: var key := event as InputEventKey if not key.pressed or key.echo: return - if rename_dialog.visible or delete_dialog.visible: + + var focused := get_viewport().gui_get_focus_owner() + if focused is LineEdit: + return + if ( + rename_dialog.visible + or delete_dialog.visible + or save_as_dialog.visible + or open_dialog.visible + or confirm_dialog.visible + or message_dialog.visible + ): + return + + var modifier := key.ctrl_pressed or key.meta_pressed + if modifier and key.keycode == KEY_Z: + if key.shift_pressed: + redo() + else: + undo() + get_viewport().set_input_as_handled() + return + if modifier and key.keycode == KEY_Y: + redo() + get_viewport().set_input_as_handled() return if key.keycode != KEY_DELETE and key.keycode != KEY_BACKSPACE: return @@ -408,6 +558,7 @@ func _on_direction_pressed(direction: PathSegment.Direction) -> void: var selected := get_selected_segment() if selected != null: + _record_undo() selected.direction = direction _mark_dirty() rink_canvas.refresh() @@ -420,6 +571,7 @@ func _on_edge_pressed(edge: PathSegment.Edge) -> void: var selected := get_selected_segment() if selected != null: + _record_undo() selected.edge = edge _mark_dirty() rink_canvas.refresh() @@ -429,6 +581,7 @@ func _on_edge_pressed(edge: PathSegment.Edge) -> void: func _on_draw_started(model_point: Vector2) -> void: if _active_section == null: return + _pending_history = _make_history_entry() _current_segment = _create_segment(model_point, _direction, _edge) _active_section.segments.append(_current_segment) rink_canvas.refresh() @@ -453,12 +606,15 @@ func _on_draw_finished(model_point: Vector2) -> void: _current_segment = null if kept: + _push_undo_entry(_pending_history) _mark_dirty() + _pending_history = null rink_canvas.refresh() _update_status() func _on_draw_cancelled() -> void: + _pending_history = null if _current_segment == null: return @@ -482,6 +638,7 @@ func _on_selection_cleared() -> void: func _on_handle_drag_started(segment_id: String, _handle: int) -> void: _selected_segment_id = segment_id + _pending_history = _make_history_entry() _update_status() @@ -506,6 +663,8 @@ func _on_handle_drag_finished( model_point: Vector2 ) -> void: _on_handle_dragged(segment_id, handle, model_point) + _push_undo_entry(_pending_history) + _pending_history = null func _apply_handle_drag( @@ -672,6 +831,25 @@ func _on_file_menu_pressed(action: int) -> void: func _set_routine(routine: Routine, path: String) -> void: + _install_routine(routine, "") + _current_path = path + _dirty = false + autosave_timer.stop() + + _undo_stack.clear() + _redo_stack.clear() + _pending_history = null + _update_history_buttons() + + _update_window_title() + _save_autosave() + + +func _install_routine( + routine: Routine, + active_section_id: String, + refit: bool = true +) -> void: rink_canvas.cancel_interaction() _routine = routine @@ -693,20 +871,20 @@ func _set_routine(routine: Routine, path: String) -> void: _renumber_sections() _active_section = _routine.sections[0] + if not active_section_id.is_empty(): + var found := _routine.get_section_by_id(active_section_id) + if found != null: + _active_section = found + _selected_segment_id = "" _current_segment = null - _current_path = path - _dirty = false - autosave_timer.stop() _rebuild_counters_from_ids() - rink_canvas.set_routine(_routine) + rink_canvas.set_routine(_routine, refit) rink_canvas.set_selected_segment("") rink_canvas.set_active_section(_active_section.section_id) _rebuild_section_list() - _update_window_title() _update_status() - _save_autosave() func _rebuild_counters_from_ids() -> void: diff --git a/rinkplanner/src/whiteboard/view/rink_canvas.gd b/rinkplanner/src/whiteboard/view/rink_canvas.gd index f27eeaf..86f4b2e 100644 --- a/rinkplanner/src/whiteboard/view/rink_canvas.gd +++ b/rinkplanner/src/whiteboard/view/rink_canvas.gd @@ -90,13 +90,16 @@ func get_rink() -> RinkDimensions: return _rink -func set_routine(routine: Routine) -> void: +func set_routine(routine: Routine, refit: bool = true) -> void: _routine = routine if routine != null and routine.rink != null: _rink = routine.rink if _active_section_id.is_empty() and routine != null and not routine.sections.is_empty(): _active_section_id = routine.sections[0].section_id - fit_rink() + if refit: + fit_rink() + else: + queue_redraw() func set_active_section(section_id: String) -> void: diff --git a/rinkplanner/tests/README.md b/rinkplanner/tests/README.md index 8fa539a..b9c3749 100644 --- a/rinkplanner/tests/README.md +++ b/rinkplanner/tests/README.md @@ -26,6 +26,9 @@ $GODOT --headless --path rinkplanner --script res://tests/run_canvas_tests.gd # Run whiteboard controller/scene tests $GODOT --headless --path rinkplanner --script res://tests/run_whiteboard_tests.gd + +# Run undo/redo history tests +$GODOT --headless --path rinkplanner --script res://tests/run_history_tests.gd ``` Exit code `0` means all assertions passed. Failures are printed to stderr. diff --git a/rinkplanner/tests/run_history_tests.gd b/rinkplanner/tests/run_history_tests.gd new file mode 100644 index 0000000..af57c56 --- /dev/null +++ b/rinkplanner/tests/run_history_tests.gd @@ -0,0 +1,247 @@ +extends SceneTree + +## Headless tests for the snapshot-based undo/redo history. +## +## Run with: +## godot --headless --path rinkplanner --script res://tests/run_history_tests.gd + +const WhiteboardScene := preload("res://scenes/whiteboard/whiteboard.tscn") + +var _assertions: int = 0 +var _failures: PackedStringArray = PackedStringArray() + + +func _init() -> void: + _run.call_deferred() + + +func _run() -> void: + var scene := WhiteboardScene.instantiate() + var controller := scene as WhiteboardController + if controller != null: + controller.restore_autosave = false + controller.autosave_enabled = false + root.add_child(scene) + await process_frame + + _check(controller != null, "history scene has a controller") + if controller == null: + _finish() + return + + _check(controller.undo_button.disabled, "undo starts disabled") + _check(controller.redo_button.disabled, "redo starts disabled") + + var section := controller.get_active_section() + controller.add_segment( + Vector2(-10.0, 0.0), + Vector2(0.0, 0.0), + PathSegment.Direction.FORWARD, + PathSegment.Edge.OUTSIDE + ) + _check(section.segments.size() == 1, "segment was added") + _check(not controller.undo_button.disabled, "undo is enabled after a change") + _check(controller.redo_button.disabled, "redo stays disabled after a change") + + _check(controller.undo(), "undo returns true") + _check( + controller.get_active_section().segments.is_empty(), + "undo removes the added segment" + ) + _check(controller.undo_button.disabled, "undo disables at history start") + _check(not controller.redo_button.disabled, "redo is enabled after undo") + + _check(controller.redo(), "redo returns true") + _check( + controller.get_active_section().segments.size() == 1, + "redo restores the segment" + ) + _check(controller.redo_button.disabled, "redo disables at history end") + _check(not controller.undo_button.disabled, "undo is enabled again") + + controller.undo_button.pressed.emit() + _check( + controller.get_active_section().segments.is_empty(), + "undo button triggers undo" + ) + controller.redo_button.pressed.emit() + _check( + controller.get_active_section().segments.size() == 1, + "redo button triggers redo" + ) + + var restored := controller.get_active_section().segments[0] + controller.select_segment(restored.segment_id) + controller.inside_button.pressed.emit() + _check( + restored.edge == PathSegment.Edge.INSIDE, + "edge change is applied" + ) + var origin_before := controller.rink_canvas.get_view_transform().origin_px + _check(controller.undo(), "undo edge change") + _check_vec( + controller.rink_canvas.get_view_transform().origin_px, + origin_before, + "undo preserves the current pan/zoom view" + ) + restored = controller.get_active_section().segments[0] + _check( + restored.edge == PathSegment.Edge.OUTSIDE, + "undo restores the edge" + ) + _check(controller.redo(), "redo edge change") + restored = controller.get_active_section().segments[0] + _check( + restored.edge == PathSegment.Edge.INSIDE, + "redo reapplies the edge" + ) + + var first_section_id := controller.get_active_section().section_id + var second := controller.add_section() + _check( + controller.get_routine().sections.size() == 2, + "section was added" + ) + _check(controller.undo(), "undo section add") + _check( + controller.get_routine().sections.size() == 1, + "undo removes the added section" + ) + _check( + controller.get_active_section().section_id == first_section_id, + "undo restores the previous active section" + ) + _check(controller.redo(), "redo section add") + _check( + controller.get_routine().sections.size() == 2, + "redo restores the section" + ) + _check( + controller.get_active_section().section_id == second.section_id, + "redo restores the added section as active" + ) + + controller.set_active_section(first_section_id) + var old_title := controller.get_active_section().title + controller.rename_section(first_section_id, "Eröffnung") + _check( + controller.get_active_section().title == "Eröffnung", + "section rename is applied" + ) + _check(controller.undo(), "undo section rename") + _check( + controller.get_active_section().title == old_title, + "undo restores the section title" + ) + _check(controller.redo(), "redo section rename") + _check( + controller.get_active_section().title == "Eröffnung", + "redo reapplies the section title" + ) + + var canvas := controller.rink_canvas + var view := canvas.get_view_transform() + var count_before_draw := controller.get_active_section().segments.size() + controller.set_tool(WhiteboardController.Tool.LINE) + _mouse_button(canvas, view.model_to_screen(Vector2(-15.0, 5.0)), true) + _mouse_motion(canvas, view.model_to_screen(Vector2(-5.0, 5.0))) + _mouse_button(canvas, view.model_to_screen(Vector2(-5.0, 5.0)), false) + _check( + controller.get_active_section().segments.size() == count_before_draw + 1, + "drawing gesture adds a segment" + ) + _check(controller.undo(), "undo drawing gesture") + _check( + controller.get_active_section().segments.size() == count_before_draw, + "undo removes the drawn segment" + ) + _check(controller.redo(), "redo drawing gesture") + _check( + controller.get_active_section().segments.size() == count_before_draw + 1, + "redo restores the drawn segment" + ) + + var arc := controller.add_segment( + Vector2(0.0, -5.0), + Vector2(10.0, -5.0), + PathSegment.Direction.FORWARD, + PathSegment.Edge.OUTSIDE, + WhiteboardController.Tool.ARC_LEFT + ) + controller.select_segment(arc.segment_id) + var old_start := arc.curve.get_point_position(0) + var new_start := Vector2(-3.0, -6.0) + _mouse_button(canvas, view.model_to_screen(old_start), true) + _mouse_motion(canvas, view.model_to_screen(new_start)) + _mouse_button(canvas, view.model_to_screen(new_start), false) + var edited := controller.get_active_section().get_segment_by_id(arc.segment_id) + _check(edited != null, "arc is still present after handle drag") + if edited != null: + _check_vec( + edited.curve.get_point_position(0), + new_start, + "handle drag moves the arc start" + ) + _check(controller.undo(), "undo handle drag") + var undone := controller.get_active_section().get_segment_by_id(arc.segment_id) + if undone != null: + _check_vec( + undone.curve.get_point_position(0), + old_start, + "undo restores the arc start" + ) + _check(controller.redo(), "redo handle drag") + var redone := controller.get_active_section().get_segment_by_id(arc.segment_id) + if redone != null: + _check_vec( + redone.curve.get_point_position(0), + new_start, + "redo reapplies the arc start" + ) + + controller.new_routine() + _check(controller.undo_button.disabled, "new routine clears undo history") + _check(controller.redo_button.disabled, "new routine clears redo history") + + scene.queue_free() + await process_frame + _finish() + + +func _mouse_button(canvas: RinkCanvas, position: Vector2, pressed: bool) -> void: + var event := InputEventMouseButton.new() + event.button_index = MOUSE_BUTTON_LEFT + event.pressed = pressed + event.position = position + canvas._gui_input(event) + + +func _mouse_motion(canvas: RinkCanvas, position: Vector2) -> void: + var event := InputEventMouseMotion.new() + event.position = position + canvas._gui_input(event) + + +func _check(condition: bool, message: String) -> void: + _assertions += 1 + if not condition: + _failures.append(message) + + +func _check_vec(actual: Vector2, expected: Vector2, message: String) -> void: + _check( + actual.distance_to(expected) <= 0.001, + "%s (expected %s, got %s)" % [message, expected, actual] + ) + + +func _finish() -> void: + if _failures.is_empty(): + print("PASS: %d assertions" % _assertions) + quit(0) + return + + for failure: String in _failures: + printerr("FAIL: %s" % failure) + printerr("%d of %d assertions failed" % [_failures.size(), _assertions]) + quit(1) diff --git a/rinkplanner/tests/run_history_tests.gd.uid b/rinkplanner/tests/run_history_tests.gd.uid new file mode 100644 index 0000000..4009a14 --- /dev/null +++ b/rinkplanner/tests/run_history_tests.gd.uid @@ -0,0 +1 @@ +uid://b81wlgmomt3br diff --git a/rinkplanner/tests/run_whiteboard_tests.gd b/rinkplanner/tests/run_whiteboard_tests.gd index 8f77c98..0671307 100644 --- a/rinkplanner/tests/run_whiteboard_tests.gd +++ b/rinkplanner/tests/run_whiteboard_tests.gd @@ -104,7 +104,9 @@ func _run() -> void: "status bar reports the segment count" ) - controller.undo_last_segment() + controller.undo() + routine = controller.get_routine() + section = controller.get_active_section() _check( section.segments.size() == 2, "undo removes the last segment" @@ -120,7 +122,7 @@ func _run() -> void: PathSegment.Direction.FORWARD, PathSegment.Edge.OUTSIDE ) - _check(fourth.segment_id == "seg-004", "segment ids stay unique after undo") + _check(fourth.segment_id == "seg-003", "segment ids are reused after undo") _check(routine.is_valid(), "routine stays valid after undo and redraw") var canvas := controller.rink_canvas diff --git a/tools/test.sh b/tools/test.sh index 1b63c48..ceada4f 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -27,3 +27,6 @@ fi "$GODOT" --headless --path "$PROJECT_DIR" \ --script res://tests/run_whiteboard_tests.gd + +"$GODOT" --headless --path "$PROJECT_DIR" \ + --script res://tests/run_history_tests.gd