diff --git a/docs/architecture.md b/docs/architecture.md index c2261ab..fe50fd1 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -127,6 +127,15 @@ eine Abkürzung. So kann die UI Fehler konkret anzeigen. 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. +- **Playback:** Ein Marker durchläuft alle Sections und Segmente + kontinuierlich per Bogenlängen-Sampling (`sample_baked()`). Die Blickrichtung + folgt der Kurventangente; bei Rückwärts-Segmenten wird sie um 180° gedreht, + sodass die Bewegung entlang des Pfades und die Körperrichtung getrennt + sichtbar sind. Während des Playbacks wird die aktuelle Section hervorgehoben, + ohne die Bearbeitungs-Section zu wechseln. Steuerung: Play/Pause, Reset, + Timeline-Scrubbing und Geschwindigkeit 0,25×–2,0×. Die Dauer kommt aus den + Segmentdauern (aktuell 6 m/s). Nicht verbundene Segmente führen zu einem + Sprung des Markers an den Start des nächsten Segments. - **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, @@ -195,6 +204,9 @@ Aktueller Stand: - **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. +- **Playback:** 38 Assertions für Dauer/Timeline, Vorwärts-/Rückwärts- + Blickrichtung, Section-Hervorhebung, Play/Pause/Reset inklusive + Button-Verdrahtung, Scrubbing und Geschwindigkeitswechsel. Autosave/Restore wird zusätzlich in einem isolierten `XDG_DATA_HOME` end-to-end geprüft. @@ -212,9 +224,10 @@ Viewport-Screenshot; sie ersetzen die headless Tests nicht. 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. +4. **Section-Übergänge:** Playback läuft derzeit kontinuierlich ohne + Übergangspause; Section-Startzeiten werden ausschließlich aus den + Segmentdauern abgeleitet. Ob explizite Übergangszeiten nötig sind, zeigt + die Praxis. 5. **Kurven und Templates:** Linie, Kreisbogen und die Bearbeitung von Start/Ende, Seite und Tiefe sind umgesetzt. Offen sind Freihand sowie weitere Templates wie Acht und Dreier. @@ -233,3 +246,8 @@ Viewport-Screenshot; sie ersetzen die headless Tests nicht. 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. +11. **Segment-Timing:** Die Dauer wird weiterhin mit 6 m/s aus der Länge + abgeleitet. Eine direkte Dauer-/Geschwindigkeitsbearbeitung pro Segment + ist der nächste Timing-Schritt. +12. **Playback-Komfort:** Loop, Scrubbing während des Abspielens und + Audio-Synchronisation sind bewusst noch nicht enthalten. diff --git a/rinkplanner/scenes/whiteboard/whiteboard.tscn b/rinkplanner/scenes/whiteboard/whiteboard.tscn index ed3d921..b258a9c 100644 --- a/rinkplanner/scenes/whiteboard/whiteboard.tscn +++ b/rinkplanner/scenes/whiteboard/whiteboard.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=8 format=3] +[gd_scene load_steps=9 format=3] [ext_resource type="Script" path="res://src/whiteboard/ctrl/whiteboard_controller.gd" id="1_controller"] [ext_resource type="Script" path="res://src/whiteboard/view/rink_canvas.gd" id="2_canvas"] @@ -24,6 +24,15 @@ bg_color = Color(0.0431373, 0.0705882, 0.12549, 1) border_width_top = 1 border_color = Color(0.121569, 0.160784, 0.219608, 1) +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_playback"] +content_margin_left = 16.0 +content_margin_top = 8.0 +content_margin_right = 16.0 +content_margin_bottom = 8.0 +bg_color = Color(0.0588235, 0.0901961, 0.164706, 1) +border_width_top = 1 +border_color = Color(0.121569, 0.160784, 0.219608, 1) + [sub_resource type="StyleBoxFlat" id="StyleBoxFlat_sections"] content_margin_left = 12.0 content_margin_top = 12.0 @@ -264,6 +273,59 @@ size_flags_horizontal = 3 theme_override_font_sizes/font_size = 14 text = "Abschnitt löschen" +[node name="PlaybackBar" type="PanelContainer" parent="Layout"] +custom_minimum_size = Vector2(0, 64) +layout_mode = 2 +theme_override_styles/panel = SubResource("StyleBoxFlat_playback") + +[node name="PlaybackContent" type="HBoxContainer" parent="Layout/PlaybackBar"] +layout_mode = 2 +theme_override_constants/separation = 8 + +[node name="PlayButton" type="Button" parent="Layout/PlaybackBar/PlaybackContent"] +unique_name_in_owner = true +custom_minimum_size = Vector2(80, 48) +layout_mode = 2 +theme_override_font_sizes/font_size = 14 +disabled = true +text = "Start" +tooltip_text = "Abspielen" + +[node name="ResetPlaybackButton" type="Button" parent="Layout/PlaybackBar/PlaybackContent"] +unique_name_in_owner = true +custom_minimum_size = Vector2(80, 48) +layout_mode = 2 +theme_override_font_sizes/font_size = 14 +disabled = true +text = "Reset" +tooltip_text = "Zurücksetzen" + +[node name="PlaybackTimeLabel" type="Label" parent="Layout/PlaybackBar/PlaybackContent"] +unique_name_in_owner = true +custom_minimum_size = Vector2(150, 48) +layout_mode = 2 +theme_override_colors/font_color = Color(0.85098, 0.890196, 0.941176, 1) +theme_override_font_sizes/font_size = 14 +text = "0,0 / 0,0 s" +horizontal_alignment = 1 +vertical_alignment = 1 + +[node name="PlaybackTimeline" type="HSlider" parent="Layout/PlaybackBar/PlaybackContent"] +unique_name_in_owner = true +custom_minimum_size = Vector2(0, 48) +layout_mode = 2 +size_flags_horizontal = 3 +max_value = 0.0 +step = 0.01 +editable = false + +[node name="SpeedOption" type="OptionButton" parent="Layout/PlaybackBar/PlaybackContent"] +unique_name_in_owner = true +custom_minimum_size = Vector2(100, 48) +layout_mode = 2 +theme_override_font_sizes/font_size = 14 +tooltip_text = "Wiedergabegeschwindigkeit" + [node name="StatusBar" type="PanelContainer" parent="Layout"] custom_minimum_size = Vector2(0, 56) layout_mode = 2 diff --git a/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd b/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd index 175ae80..fb4c88f 100644 --- a/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd +++ b/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd @@ -58,6 +58,11 @@ const MAX_HISTORY := 50 @onready var open_list: VBoxContainer = %OpenList @onready var confirm_dialog: ConfirmationDialog = %ConfirmDialog @onready var message_dialog: AcceptDialog = %MessageDialog +@onready var play_button: Button = %PlayButton +@onready var reset_playback_button: Button = %ResetPlaybackButton +@onready var playback_time_label: Label = %PlaybackTimeLabel +@onready var playback_timeline: HSlider = %PlaybackTimeline +@onready var speed_option: OptionButton = %SpeedOption var _routine: Routine var _active_section: RoutineSection @@ -76,11 +81,23 @@ var _undo_stack: Array[HistoryEntry] = [] var _redo_stack: Array[HistoryEntry] = [] var _pending_history: HistoryEntry +var _playing: bool = false +var _playback_time: float = 0.0 +var _playback_speed: float = 1.0 +var _playback_entries: Array[Dictionary] = [] +var _playback_total_duration: float = 0.0 +var _playback_section_id: String = "" +var _playback_segment_id: String = "" +var _playback_position: Vector2 = Vector2.ZERO +var _playback_heading: Vector2 = Vector2.RIGHT +var _updating_timeline: bool = false + func _ready() -> void: autosave_timer.wait_time = AUTOSAVE_DELAY_SECONDS _setup_tool_options() _setup_file_menu() + _setup_playback_controls() tool_option.item_selected.connect(_on_tool_selected) forward_button.pressed.connect( @@ -117,6 +134,10 @@ func _ready() -> void: save_as_dialog.confirmed.connect(_on_save_as_confirmed) confirm_dialog.confirmed.connect(_on_confirm_discard_confirmed) confirm_dialog.canceled.connect(_on_confirm_discard_canceled) + play_button.pressed.connect(_toggle_playback) + reset_playback_button.pressed.connect(reset_playback) + playback_timeline.value_changed.connect(_on_timeline_changed) + speed_option.item_selected.connect(_on_speed_selected) _ensure_routines_dir() if not (restore_autosave and _try_load_autosave()): @@ -213,6 +234,9 @@ func set_active_section(section_id: String) -> void: if section == null: return + if _playing or not _playback_section_id.is_empty(): + reset_playback() + rink_canvas.cancel_interaction() _active_section = section _selected_segment_id = "" @@ -581,6 +605,9 @@ func _on_edge_pressed(edge: PathSegment.Edge) -> void: func _on_draw_started(model_point: Vector2) -> void: if _active_section == null: return + if _playing or not _playback_section_id.is_empty(): + reset_playback() + _pending_history = _make_history_entry() _current_segment = _create_segment(model_point, _direction, _edge) _active_section.segments.append(_current_segment) @@ -625,6 +652,9 @@ func _on_draw_cancelled() -> void: func _on_segment_selected(segment_id: String) -> void: + if _playing or not _playback_section_id.is_empty(): + reset_playback() + _selected_segment_id = segment_id rink_canvas.set_selected_segment(segment_id) _sync_toolbar_to_selection() @@ -808,6 +838,280 @@ func _update_duration(segment: PathSegment) -> void: ) +func is_playing() -> bool: + return _playing + + +func get_playback_time() -> float: + return _playback_time + + +func get_playback_duration() -> float: + return _playback_total_duration + + +func get_playback_speed() -> float: + return _playback_speed + + +func get_playback_position() -> Vector2: + return _playback_position + + +func get_playback_heading() -> Vector2: + return _playback_heading + + +func get_playback_section_id() -> String: + return _playback_section_id + + +func get_playback_segment_id() -> String: + return _playback_segment_id + + +func play() -> void: + if _playback_total_duration <= 0.0: + return + + if _playback_time >= _playback_total_duration - 0.0001: + _playback_time = 0.0 + + _selected_segment_id = "" + rink_canvas.cancel_interaction() + rink_canvas.set_selected_segment("") + _playing = true + _update_playback_state(0.0, true) + _update_play_button() + _update_playback_ui() + _update_status() + + +func pause() -> void: + if not _playing: + return + _playing = false + _update_play_button() + _update_playback_ui() + + +func reset_playback() -> void: + _playing = false + _playback_time = 0.0 + _playback_section_id = "" + _playback_segment_id = "" + _update_play_button() + rink_canvas.clear_playback_state() + _update_playback_ui() + + +func seek_playback(time: float) -> void: + _playback_time = clampf(time, 0.0, _playback_total_duration) + if _playback_total_duration <= 0.0: + rink_canvas.clear_playback_state() + else: + _update_playback_state(0.0, true) + _update_playback_ui() + + +func advance_playback(delta: float) -> void: + if not _playing or _playback_total_duration <= 0.0: + return + + _playback_time = minf( + _playback_time + delta * _playback_speed, + _playback_total_duration + ) + + if _playback_time >= _playback_total_duration - 0.0001: + _playback_time = _playback_total_duration + _update_playback_state(0.0, true) + _playing = false + _update_play_button() + _update_playback_ui() + return + + _update_playback_state(delta) + + +func _process(delta: float) -> void: + advance_playback(delta) + + +func _setup_playback_controls() -> void: + speed_option.clear() + speed_option.add_item("0,25×", 25) + speed_option.add_item("0,5×", 50) + speed_option.add_item("1×", 100) + speed_option.add_item("1,5×", 150) + speed_option.add_item("2×", 200) + speed_option.select(speed_option.get_item_index(100)) + _playback_speed = 1.0 + + playback_timeline.min_value = 0.0 + playback_timeline.max_value = 0.0 + playback_timeline.step = 0.01 + playback_timeline.editable = false + play_button.disabled = true + reset_playback_button.disabled = true + _update_play_button() + + +func _toggle_playback() -> void: + if _playing: + pause() + else: + play() + + +func _on_speed_selected(index: int) -> void: + _playback_speed = float(speed_option.get_item_id(index)) / 100.0 + + +func _on_timeline_changed(value: float) -> void: + if _updating_timeline: + return + seek_playback(value) + + +func _refresh_playback_timeline() -> void: + var entries: Array[Dictionary] = [] + var total := 0.0 + if _routine != null: + for section: RoutineSection in _routine.sections: + if section == null: + continue + for segment: PathSegment in section.segments: + if segment == null: + continue + var duration := maxf(segment.duration_seconds, 0.0) + if duration <= 0.0: + continue + entries.append({ + "segment": segment, + "section_id": section.section_id, + "start": total, + "duration": duration, + }) + total += duration + + _playback_entries = entries + _playback_total_duration = total + + if total <= 0.0: + _playback_time = 0.0 + _playing = false + _playback_section_id = "" + _playback_segment_id = "" + rink_canvas.clear_playback_state() + else: + _playback_time = clampf(_playback_time, 0.0, total) + if _playing and _playback_time >= total: + _playing = false + + _update_play_button() + _update_playback_ui() + + if total > 0.0 and (_playing or _playback_time > 0.0): + _update_playback_state(0.0, true) + + +func _update_playback_state( + delta: float, + force_heading: bool = false +) -> void: + if _playback_entries.is_empty() or _routine == null: + rink_canvas.clear_playback_state() + return + + var entry: Dictionary = _playback_entries[_playback_entries.size() - 1] + for candidate: Dictionary in _playback_entries: + var candidate_start: float = candidate["start"] + var candidate_duration: float = candidate["duration"] + if _playback_time < candidate_start + candidate_duration - 0.0001: + entry = candidate + break + + var segment: PathSegment = entry["segment"] + var section_id: String = entry["section_id"] + var start: float = entry["start"] + var duration: float = entry["duration"] + var local_time := clampf(_playback_time - start, 0.0, duration) + var fraction := 0.0 if duration <= 0.0 else local_time / duration + + var curve := segment.curve + var length := segment.get_length_m() + var position := Vector2.ZERO + var tangent := Vector2.RIGHT + + if curve != null and length > 0.0: + var offset := clampf(fraction, 0.0, 1.0) * length + position = curve.sample_baked(offset) + var epsilon := maxf(0.01, length * 0.01) + var before := curve.sample_baked(maxf(0.0, offset - epsilon)) + var after := curve.sample_baked(minf(length, offset + epsilon)) + var direction_vector := after - before + if direction_vector.length_squared() > 0.0: + tangent = direction_vector.normalized() + elif curve != null and curve.point_count > 0: + position = curve.get_point_position(0) + + var facing := tangent + if segment.direction == PathSegment.Direction.BACKWARD: + facing = -facing + + if force_heading or delta <= 0.0 or _playback_heading.length_squared() <= 0.0: + _playback_heading = facing + else: + var turn := clampf(delta * 12.0, 0.0, 1.0) + _playback_heading = Vector2.from_angle( + lerp_angle(_playback_heading.angle(), facing.angle(), turn) + ) + + _playback_position = position + _playback_section_id = section_id + _playback_segment_id = segment.segment_id + + rink_canvas.set_playback_state( + position, + _playback_heading, + section_id, + segment.edge, + segment.direction + ) + _update_playback_ui() + + +func _update_playback_ui() -> void: + _updating_timeline = true + playback_timeline.max_value = maxf(_playback_total_duration, 0.0) + playback_timeline.value = clampf( + _playback_time, + 0.0, + _playback_total_duration + ) + _updating_timeline = false + + playback_time_label.text = "%s / %s s" % [ + _format_seconds(_playback_time), + _format_seconds(_playback_total_duration), + ] + + var has_playback := _playback_total_duration > 0.0 + play_button.disabled = not has_playback + reset_playback_button.disabled = not has_playback + playback_timeline.editable = has_playback + + +func _update_play_button() -> void: + play_button.text = "Pause" if _playing else "Start" + play_button.tooltip_text = "Pause" if _playing else "Abspielen" + + +func _format_seconds(seconds: float) -> String: + return ("%.1f" % seconds).replace(".", ",") + + func _setup_file_menu() -> void: var popup := file_menu_button.get_popup() popup.add_item("Neu", FileAction.NEW) @@ -850,6 +1154,11 @@ func _install_routine( active_section_id: String, refit: bool = true ) -> void: + _playing = false + _playback_time = 0.0 + _playback_section_id = "" + _playback_segment_id = "" + rink_canvas.clear_playback_state() rink_canvas.cancel_interaction() _routine = routine @@ -1200,6 +1509,8 @@ func _update_status() -> void: if _routine == null or _active_section == null: return + _refresh_playback_timeline() + var segment_count := _active_section.segments.size() var segment_word := "Segment" if segment_count == 1 else "Segmente" var text := ( diff --git a/rinkplanner/src/whiteboard/view/rink_canvas.gd b/rinkplanner/src/whiteboard/view/rink_canvas.gd index 86f4b2e..15efa73 100644 --- a/rinkplanner/src/whiteboard/view/rink_canvas.gd +++ b/rinkplanner/src/whiteboard/view/rink_canvas.gd @@ -69,6 +69,13 @@ var _handle_drag_segment_id: String = "" var _handle_touch_index: int = -1 var _last_handle_model_point: Vector2 = Vector2.ZERO +var _playback_visible: bool = false +var _playback_position: Vector2 = Vector2.ZERO +var _playback_heading: Vector2 = Vector2.RIGHT +var _playback_section_id: String = "" +var _playback_edge: PathSegment.Edge = PathSegment.Edge.OUTSIDE +var _playback_direction: PathSegment.Direction = PathSegment.Direction.FORWARD + func _ready() -> void: focus_mode = Control.FOCUS_ALL @@ -131,6 +138,32 @@ func set_selected_segment(segment_id: String) -> void: queue_redraw() +func set_playback_state( + position: Vector2, + heading: Vector2, + section_id: String, + edge: PathSegment.Edge, + direction: PathSegment.Direction +) -> void: + _playback_visible = true + _playback_position = position + _playback_heading = heading + _playback_section_id = section_id + _playback_edge = edge + _playback_direction = direction + queue_redraw() + + +func clear_playback_state() -> void: + _playback_visible = false + _playback_section_id = "" + queue_redraw() + + +func get_playback_section_id() -> String: + return _playback_section_id + + ## Aborts any in-progress draw/handle gesture and clears the selection. func cancel_interaction() -> void: _abort_handle_drag() @@ -188,6 +221,9 @@ func _draw() -> void: _draw_selection_handles() + if _playback_visible: + _draw_playback_marker() + func _draw_center_line() -> void: var half_width := _rink.get_half_width() @@ -225,14 +261,53 @@ func _draw_routine() -> void: if _routine == null: return - # Ghost all inactive sections first, then draw the active section on top. + var highlight_id := _active_section_id + if _playback_visible and not _playback_section_id.is_empty(): + highlight_id = _playback_section_id + + # Ghost all inactive sections first, then draw the highlighted one on top. for section: RoutineSection in _routine.sections: - if section != null and section.section_id != _active_section_id: + if section != null and section.section_id != highlight_id: _draw_section(section, GHOST_ALPHA) - var active := _get_active_section() - if active != null: - _draw_section(active, 1.0) + var highlighted := _get_section_by_id(highlight_id) + if highlighted != null: + _draw_section(highlighted, 1.0) + + +func _draw_playback_marker() -> void: + var center := _transform.model_to_screen(_playback_position) + var radius := clampf(0.45 * _transform.pixels_per_meter, 8.0, 20.0) + var marker_color := _edge_color(_playback_edge) + + draw_circle(center, radius + 2.0, HANDLE_BORDER_COLOR) + draw_circle(center, radius, HANDLE_FILL_COLOR) + draw_arc( + center, + radius, + 0.0, + TAU, + 32, + marker_color, + maxf(2.0, radius * 0.25), + true + ) + + var heading := _playback_heading + if heading.length_squared() <= 0.0: + heading = Vector2.RIGHT + heading = heading.normalized() + var normal := Vector2(-heading.y, heading.x) + var tip := center + heading * radius * 2.0 + var base := center + heading * radius * 0.65 + draw_colored_polygon( + PackedVector2Array([ + tip, + base + normal * radius * 0.55, + base - normal * radius * 0.55, + ]), + marker_color + ) func _draw_section(section: RoutineSection, alpha: float) -> void: @@ -404,10 +479,14 @@ func _segment_width() -> float: func _get_active_section() -> RoutineSection: + return _get_section_by_id(_active_section_id) + + +func _get_section_by_id(section_id: String) -> RoutineSection: if _routine == null: return null for section: RoutineSection in _routine.sections: - if section != null and section.section_id == _active_section_id: + if section != null and section.section_id == section_id: return section return null diff --git a/rinkplanner/tests/README.md b/rinkplanner/tests/README.md index b9c3749..2e647bf 100644 --- a/rinkplanner/tests/README.md +++ b/rinkplanner/tests/README.md @@ -29,6 +29,9 @@ $GODOT --headless --path rinkplanner --script res://tests/run_whiteboard_tests.g # Run undo/redo history tests $GODOT --headless --path rinkplanner --script res://tests/run_history_tests.gd + +# Run playback dot/timeline tests +$GODOT --headless --path rinkplanner --script res://tests/run_playback_tests.gd ``` Exit code `0` means all assertions passed. Failures are printed to stderr. diff --git a/rinkplanner/tests/run_playback_tests.gd b/rinkplanner/tests/run_playback_tests.gd new file mode 100644 index 0000000..3e23af5 --- /dev/null +++ b/rinkplanner/tests/run_playback_tests.gd @@ -0,0 +1,211 @@ +extends SceneTree + +## Headless tests for the playback dot and timeline. +## +## Run with: +## godot --headless --path rinkplanner --script res://tests/run_playback_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, "playback scene has a controller") + if controller == null: + _finish() + return + + _check_approx(controller.get_playback_duration(), 0.0, "empty routine has no duration") + _check(controller.play_button.disabled, "play is disabled without segments") + _check(controller.playback_timeline.editable == false, "timeline is disabled without segments") + + var section_one := controller.get_active_section() + var forward := controller.add_segment( + Vector2(-20.0, 0.0), + Vector2(-8.0, 0.0), + PathSegment.Direction.FORWARD, + PathSegment.Edge.OUTSIDE + ) + var backward := controller.add_segment( + Vector2(-8.0, 0.0), + Vector2(4.0, 0.0), + PathSegment.Direction.BACKWARD, + PathSegment.Edge.INSIDE + ) + + var section_two := controller.add_section() + var last := controller.add_segment( + Vector2(4.0, 0.0), + Vector2(16.0, 0.0), + PathSegment.Direction.FORWARD, + PathSegment.Edge.OUTSIDE + ) + + _check_approx(controller.get_playback_duration(), 6.0, "duration sums all sections") + _check(not controller.play_button.disabled, "play is enabled with segments") + _check(controller.playback_timeline.editable, "timeline is enabled with segments") + _check_approx( + controller.playback_timeline.max_value, + 6.0, + "timeline covers the whole routine" + ) + + controller.seek_playback(1.0) + _check(not controller.is_playing(), "seeking does not start playback") + _check_vec( + controller.get_playback_position(), + Vector2(-14.0, 0.0), + "dot follows the first segment" + ) + _check( + controller.get_playback_heading().x > 0.9, + "forward segment faces the travel direction" + ) + _check( + controller.get_playback_section_id() == section_one.section_id, + "playback reports the first section" + ) + _check( + controller.get_playback_segment_id() == forward.segment_id, + "playback reports the first segment" + ) + _check( + controller.rink_canvas.get_playback_section_id() == section_one.section_id, + "canvas highlights the playing section" + ) + + controller.seek_playback(3.0) + _check_vec( + controller.get_playback_position(), + Vector2(-2.0, 0.0), + "dot follows the backward segment" + ) + _check( + controller.get_playback_heading().x < -0.9, + "backward segment faces against the travel direction" + ) + _check( + controller.get_playback_segment_id() == backward.segment_id, + "playback reports the backward segment" + ) + + controller.seek_playback(5.0) + _check_vec( + controller.get_playback_position(), + Vector2(10.0, 0.0), + "dot follows the second section" + ) + _check( + controller.get_playback_section_id() == section_two.section_id, + "playback reports the second section" + ) + _check( + controller.get_playback_segment_id() == last.segment_id, + "playback reports the last segment" + ) + _check( + controller.rink_canvas.get_playback_section_id() == section_two.section_id, + "canvas highlights the second section" + ) + + controller.play_button.pressed.emit() + _check(controller.is_playing(), "play button starts playback") + controller.play_button.pressed.emit() + _check(not controller.is_playing(), "play button pauses playback") + + controller.play() + _check(controller.is_playing(), "play starts playback") + _check(controller.play_button.text == "Pause", "play button switches to pause") + controller.advance_playback(0.5) + _check_approx(controller.get_playback_time(), 5.5, "playback advances by delta") + controller.advance_playback(1.0) + _check(not controller.is_playing(), "playback stops at the end") + _check_approx(controller.get_playback_time(), 6.0, "playback ends at the total duration") + _check(controller.play_button.text == "Start", "play button switches back after the end") + + controller.play() + _check_approx( + controller.get_playback_time(), + 0.0, + "playing after the end restarts at the start" + ) + _check(controller.is_playing(), "restarted playback is running") + controller.pause() + _check(not controller.is_playing(), "pause stops playback") + controller.reset_playback_button.pressed.emit() + _check_approx(controller.get_playback_time(), 0.0, "reset returns to the start") + _check( + controller.rink_canvas.get_playback_section_id().is_empty(), + "reset clears the playback highlight" + ) + + controller.playback_timeline.value = 2.5 + _check_approx( + controller.get_playback_time(), + 2.5, + "timeline scrubbing seeks the playback" + ) + + controller.speed_option.item_selected.emit(4) + _check_approx(controller.get_playback_speed(), 2.0, "speed option selects 2x") + controller.reset_playback() + controller.play() + controller.advance_playback(1.0) + _check_approx( + controller.get_playback_time(), + 2.0, + "2x speed doubles the elapsed routine time" + ) + controller.pause() + controller.speed_option.item_selected.emit(2) + _check_approx(controller.get_playback_speed(), 1.0, "speed option returns to 1x") + + scene.queue_free() + await process_frame + _finish() + + +func _check(condition: bool, message: String) -> void: + _assertions += 1 + if not condition: + _failures.append(message) + + +func _check_approx(actual: float, expected: float, message: String) -> void: + _check( + absf(actual - expected) <= 0.001, + "%s (expected %.3f, got %.3f)" % [message, expected, actual] + ) + + +func _check_vec(actual: Vector2, expected: Vector2, message: String) -> void: + _check( + actual.distance_to(expected) <= 0.01, + "%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_playback_tests.gd.uid b/rinkplanner/tests/run_playback_tests.gd.uid new file mode 100644 index 0000000..0fca90c --- /dev/null +++ b/rinkplanner/tests/run_playback_tests.gd.uid @@ -0,0 +1 @@ +uid://2ho2htoy3p6i diff --git a/tools/test.sh b/tools/test.sh index ceada4f..dc0f2a0 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -30,3 +30,6 @@ fi "$GODOT" --headless --path "$PROJECT_DIR" \ --script res://tests/run_history_tests.gd + +"$GODOT" --headless --path "$PROJECT_DIR" \ + --script res://tests/run_playback_tests.gd