diff --git a/docs/architecture.md b/docs/architecture.md new file mode 100644 index 0000000..2bd2cb6 --- /dev/null +++ b/docs/architecture.md @@ -0,0 +1,161 @@ +# Rinkplanner – Architektur & Entscheidungen (Phase 1) + +Stand: 2026-10-30 +Grundlage: `rinkplanner/rinkplanner_definition_agent_guidelines.md` + +## 1. Leitentscheidung + +Phase 1 ist eine eigenständige 2D-Anwendung. Sie muss ohne 3D-Renderer, +ohne `Node3D` und ohne Blender-Assets lauffähig sein. + +Der bisherige 3D-Test ist ein Machbarkeitsnachweis und liegt isoliert unter +`rinkplanner/prototype/3d_test/`. Er ist nicht Teil der Phase-1-Runtime und +wird nicht weiterentwickelt. Phase 2 konsumiert später dasselbe Datenmodell +über eine eigene Präsentationsschicht. + +## 2. Repository-Layout + +``` +/home/torsten/repos/rinkplanner/ # Repo-Root +├── docs/ # Architektur, ADRs, Regelwerk-Doku +├── tools/ # Host-Helfer (Tests, Export, CI) +└── rinkplanner/ # Godot-Projekt (res://) + ├── project.godot + ├── src/ + │ ├── core/model/ # reine Daten-Resources + │ └── core/rules/ # ISU-Regeln (folgt) + ├── whiteboard/ # Phase 1: view / ctrl / io (folgt) + ├── scenes/ # dünne Szenen, die Views zusammensetzen + ├── assets/ + ├── tests/ # headless GDScript-Tests + └── prototype/3d_test/ # alter 3D-Test, eingefroren +``` + +`docs/` und `tools/` liegen bewusst außerhalb von `res://`. Sie werden von +Godot nicht importiert und enthalten keine Laufzeitabhängigkeiten. + +## 3. Datenmodell (Model) + +``` +Routine + ├── rink: RinkDimensions + └── sections: Array[RoutineSection] + └── segments: Array[PathSegment] + └── curve: Curve2D +``` + +Implementiert in `rinkplanner/src/core/model/`: + +| Klasse | Verantwortung | +| :--- | :--- | +| `Routine` | Wurzelaggregat, Validierung, Gesamtdauer/-länge, Section-Lookup | +| `RoutineSection` | chronologischer Block, Ghosting-Einheit, Segmentliste | +| `PathSegment` | durchgehender Pfad mit Richtung, Kante, Dauer, Element-Marker | +| `RinkDimensions` | Maße, Koordinaten, Punkte-/Kurvenprüfung | + +### Entscheidungen + +**D1 – Segment statt Section als Träger von Richtung und Kante.** +Ein `RoutineSection` kann viele Kanten- und Richtungswechsel enthalten. Deshalb +liegen `direction` und `edge` auf `PathSegment`, nicht auf der Section. + +**D2 – Zeit ist die Autorengröße, Geschwindigkeit wird abgeleitet.** +`PathSegment.duration_seconds` wird gepflegt. Die Strecke kommt aus der Kurve; +`get_speed_mps()` berechnet daraus die Geschwindigkeit. Das hält die +Dot-Simulation deterministisch und vermeidet widersprüchliche Metadaten. + +**D3 – Marker sind gültige Segmente.** +Ein Segment mit Länge braucht eine positive Dauer. Ein reiner Element-Marker +(Länge 0, z. B. Sprungpunkt) darf Dauer 0 haben und wird über `is_marker()` +erkannt. + +**D4 – Koordinaten in Metern, Ursprung in der Rinkmitte.** ++X verläuft entlang der langen Achse (−30 m … +30 m), +Y entlang der kurzen +Achse (−15 m … +15 m). Nur die View rechnet in Pixel um. Gespeicherte Daten +sind damit auflösungsunabhängig. + +**D5 – IDs sind Pflicht und global eindeutig.** +`routine_id`, `section_id` und `segment_id` werden validiert. Die ID-Erzeugung +gehört in den Controller, nicht ins Modell. + +**D6 – Validierung liefert Fehlertexte.** +`validate() -> PackedStringArray` ist die primäre API; `is_valid()` ist nur +eine Abkürzung. So kann die UI Fehler konkret anzeigen. + +## 4. View- und Controller-Grenzen + +- **View** (`Node2D`/`Control`) liest das Modell und zeichnet. Sie verändert + keine Modelldaten und enthält keine Fachlogik. +- **Meter→Pixel** läuft ausschließlich über `CanvasTransform` (reine + Mathematik, headless testbar). `RinkCanvas` zeichnet Rink, Mittellinie und + Mittelkreis und behandelt Pan/Zoom (Maus, Touch, Pinch). +- **Controller** übersetzt Touch-/Maus-Eingaben in Modelloperationen + (Segment anlegen, Kante umschalten, Section wechseln, Playback steuern). +- **Ghosting** ist reine View-Logik: pro Section ein eigener Layer, + inaktive Layer auf 20 % Deckkraft, `mouse_filter = IGNORE`. +- **Playback** samplet `Curve2D` nach Bogenlänge (`get_baked_length()` / + `sample_baked()`), nicht nach Control-Point-Index. +- Phase-1-Code importiert nichts aus `prototype/` und enthält keine + `Node3D`-Abhängigkeiten. + +## 5. Rendering und Plattformen + +Gesetzt in `rinkplanner/project.godot`: + +| Plattform | Renderer | +| :--- | :--- | +| Desktop | `forward_plus` | +| Mobile | `mobile` | +| Web (Wasm) | `gl_compatibility` | + +Die Designauflösung ist 1920 × 1200 mit `canvas_items` / `expand`; die +Oberfläche skaliert mit, das Rink-Koordinatensystem bleibt in Metern. + +Touch-Ziele: mindestens 48 × 48 px. Für Desktop-Tests kann temporär +`input_devices/pointing/emulate_touch_from_mouse=true` aktiviert werden; +standardmäßig bleibt es aus. + +## 6. Export und Persistenz + +- **SVG** wird direkt aus dem Modell serialisiert (Kurven als Pfade), nicht + aus einem Screenshot. Damit bleibt der Export auflösungsunabhängig. +- **PNG** entsteht über einen Viewport-Render in hoher Auflösung. +- **PDF** ist nicht nativ in Godot. Erste Stufe: Browser-Druck aus SVG; + eine echte PDF-Ausgabe ist ein separates Arbeitspaket. +- **Persistenz** erfolgt lokal als `.tres`-Ressourcen. Auf Web schreibt + `FileAccess` in den IndexedDB-Speicher; Speichern/Laden/Autosave brauchen + deshalb eine explizite UX. + +## 7. Tests + +Headless, ohne Editor und ohne Display: + +```bash +./tools/test.sh +``` + +Aktueller Stand: + +- **Model:** 36 Assertions für Rink-Geometrie, Segment-Timing, + Section-Aggregation und Routine-Validierung. +- **Canvas:** 67 Assertions für Meter→Pixel-Mapping, Fit/Zoom/Pan und die + Rink-Kontur inklusive Eckradien. + +Visuelle Smoke-Tests laufen zusätzlich über `xvfb-run` und einen +Viewport-Screenshot; sie ersetzen die headless Tests nicht. + +## 8. Offene Entscheidungen + +1. **Eckenradius:** 8,5 m ist als Default gesetzt. ISU- und IIHF-Regelwerk + können abweichen; der Wert gehört später in die Regelkonfiguration. +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:** Command-Pattern im Controller oder Resource-Snapshots – + Entscheidung vor dem ersten Zeichen-Tool. +4. **Section-Übergänge:** Ob Sections eine explizite Startzeit erhalten oder + ausschließlich aus Segmentdauern abgeleitet werden, klärt die + Playback-Implementierung. +5. **Zeichenwerkzeug:** Ob freie Kurven direkt als Bézier-Segmente oder über + Template-Geometrie (Lobe, Bogen, Acht) entstehen, entscheidet der erste + Tool-Prototyp. Der 3D-Prototyp ist nicht mehr die Hauptszene. diff --git a/rinkplanner/addons/script-ide/icon/class.svg.import b/rinkplanner/addons/script-ide/icon/class.svg.import index 4ff6f00..4154c41 100644 --- a/rinkplanner/addons/script-ide/icon/class.svg.import +++ b/rinkplanner/addons/script-ide/icon/class.svg.import @@ -19,6 +19,8 @@ dest_files=["res://.godot/imported/class.svg-e6f2816a1f06041fb421c2af52817a4a.ct compress/mode=0 compress/high_quality=false compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 @@ -26,6 +28,10 @@ mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 process/fix_alpha_border=true process/premult_alpha=false process/normal_map_invert_y=false diff --git a/rinkplanner/addons/script-ide/icon/constant.svg.import b/rinkplanner/addons/script-ide/icon/constant.svg.import index 2c2e17a..87e7e62 100644 --- a/rinkplanner/addons/script-ide/icon/constant.svg.import +++ b/rinkplanner/addons/script-ide/icon/constant.svg.import @@ -19,6 +19,8 @@ dest_files=["res://.godot/imported/constant.svg-f6e857276565573c7540f3c32801842a compress/mode=0 compress/high_quality=false compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 @@ -26,6 +28,10 @@ mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 process/fix_alpha_border=true process/premult_alpha=false process/normal_map_invert_y=false diff --git a/rinkplanner/addons/script-ide/icon/engine_func.svg.import b/rinkplanner/addons/script-ide/icon/engine_func.svg.import index fc9a323..bef868a 100644 --- a/rinkplanner/addons/script-ide/icon/engine_func.svg.import +++ b/rinkplanner/addons/script-ide/icon/engine_func.svg.import @@ -19,6 +19,8 @@ dest_files=["res://.godot/imported/engine_func.svg-91320e42f9cc7bdd7576002e82fa6 compress/mode=0 compress/high_quality=false compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 @@ -26,6 +28,10 @@ mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 process/fix_alpha_border=true process/premult_alpha=false process/normal_map_invert_y=false diff --git a/rinkplanner/addons/script-ide/icon/export.svg.import b/rinkplanner/addons/script-ide/icon/export.svg.import index bb844d6..1275303 100644 --- a/rinkplanner/addons/script-ide/icon/export.svg.import +++ b/rinkplanner/addons/script-ide/icon/export.svg.import @@ -19,6 +19,8 @@ dest_files=["res://.godot/imported/export.svg-d2d18132258a7a219ec1af1f0316c91c.c compress/mode=0 compress/high_quality=false compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 @@ -26,6 +28,10 @@ mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 process/fix_alpha_border=true process/premult_alpha=false process/normal_map_invert_y=false diff --git a/rinkplanner/addons/script-ide/icon/func.svg.import b/rinkplanner/addons/script-ide/icon/func.svg.import index 30cf4ff..0a9f522 100644 --- a/rinkplanner/addons/script-ide/icon/func.svg.import +++ b/rinkplanner/addons/script-ide/icon/func.svg.import @@ -19,6 +19,8 @@ dest_files=["res://.godot/imported/func.svg-139842caa5b4b7e4839711b6c756d0f7.cte compress/mode=0 compress/high_quality=false compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 @@ -26,6 +28,10 @@ mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 process/fix_alpha_border=true process/premult_alpha=false process/normal_map_invert_y=false diff --git a/rinkplanner/addons/script-ide/icon/func_get.svg.import b/rinkplanner/addons/script-ide/icon/func_get.svg.import index 3c90423..96e854d 100644 --- a/rinkplanner/addons/script-ide/icon/func_get.svg.import +++ b/rinkplanner/addons/script-ide/icon/func_get.svg.import @@ -19,6 +19,8 @@ dest_files=["res://.godot/imported/func_get.svg-093f0ce02889d1f102ff9cc3e7f72654 compress/mode=0 compress/high_quality=false compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 @@ -26,6 +28,10 @@ mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 process/fix_alpha_border=true process/premult_alpha=false process/normal_map_invert_y=false diff --git a/rinkplanner/addons/script-ide/icon/func_set.svg.import b/rinkplanner/addons/script-ide/icon/func_set.svg.import index 8063f5b..6fb602f 100644 --- a/rinkplanner/addons/script-ide/icon/func_set.svg.import +++ b/rinkplanner/addons/script-ide/icon/func_set.svg.import @@ -19,6 +19,8 @@ dest_files=["res://.godot/imported/func_set.svg-c31168d90866ff1707ad9834754bd2c9 compress/mode=0 compress/high_quality=false compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 @@ -26,6 +28,10 @@ mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 process/fix_alpha_border=true process/premult_alpha=false process/normal_map_invert_y=false diff --git a/rinkplanner/addons/script-ide/icon/property.svg.import b/rinkplanner/addons/script-ide/icon/property.svg.import index c9156a5..2143346 100644 --- a/rinkplanner/addons/script-ide/icon/property.svg.import +++ b/rinkplanner/addons/script-ide/icon/property.svg.import @@ -19,6 +19,8 @@ dest_files=["res://.godot/imported/property.svg-9e228499f30651faad74aa99e4499d7e compress/mode=0 compress/high_quality=false compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 @@ -26,6 +28,10 @@ mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 process/fix_alpha_border=true process/premult_alpha=false process/normal_map_invert_y=false diff --git a/rinkplanner/addons/script-ide/icon/signal.svg.import b/rinkplanner/addons/script-ide/icon/signal.svg.import index 4c029b0..00dc018 100644 --- a/rinkplanner/addons/script-ide/icon/signal.svg.import +++ b/rinkplanner/addons/script-ide/icon/signal.svg.import @@ -19,6 +19,8 @@ dest_files=["res://.godot/imported/signal.svg-97182e1498b520a1ff5b8b9017c3b480.c compress/mode=0 compress/high_quality=false compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 @@ -26,6 +28,10 @@ mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 process/fix_alpha_border=true process/premult_alpha=false process/normal_map_invert_y=false diff --git a/rinkplanner/assets/images/checkboard.png.import b/rinkplanner/assets/images/checkboard.png.import index 5075db0..062124c 100644 --- a/rinkplanner/assets/images/checkboard.png.import +++ b/rinkplanner/assets/images/checkboard.png.import @@ -19,6 +19,8 @@ dest_files=["res://.godot/imported/checkboard.png-60f6db393233dd01fdd8b7cd558c64 compress/mode=2 compress/high_quality=false compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 @@ -26,6 +28,10 @@ mipmaps/generate=true mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 process/fix_alpha_border=true process/premult_alpha=false process/normal_map_invert_y=false diff --git a/rinkplanner/icon.svg.import b/rinkplanner/icon.svg.import index 5e463f8..25206d0 100644 --- a/rinkplanner/icon.svg.import +++ b/rinkplanner/icon.svg.import @@ -18,6 +18,8 @@ dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.cte compress/mode=0 compress/high_quality=false compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 @@ -25,6 +27,10 @@ mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 process/fix_alpha_border=true process/premult_alpha=false process/normal_map_invert_y=false diff --git a/rinkplanner/project.godot b/rinkplanner/project.godot index 7562907..9c2adec 100644 --- a/rinkplanner/project.godot +++ b/rinkplanner/project.godot @@ -11,7 +11,7 @@ config_version=5 [application] config/name="Rinkplanner" -run/main_scene="uid://cd1ulyybmuft5" +run/main_scene="res://scenes/whiteboard/whiteboard.tscn" config/features=PackedStringArray("4.5", "Forward Plus") config/icon="res://icon.svg" @@ -31,3 +31,16 @@ zoom_out={ "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":5,"canceled":false,"pressed":false,"double_click":false,"script":null) ] } + +[display] + +window/size/viewport_width=1920 +window/size/viewport_height=1200 +window/stretch/mode="canvas_items" +window/stretch/aspect="expand" + +[rendering] + +renderer/rendering_method="forward_plus" +renderer/rendering_method.mobile="mobile" +renderer/rendering_method.web="gl_compatibility" diff --git a/rinkplanner/scenes/rink/avatar.gd b/rinkplanner/prototype/3d_test/avatar.gd similarity index 100% rename from rinkplanner/scenes/rink/avatar.gd rename to rinkplanner/prototype/3d_test/avatar.gd diff --git a/rinkplanner/scenes/rink/avatar.gd.uid b/rinkplanner/prototype/3d_test/avatar.gd.uid similarity index 100% rename from rinkplanner/scenes/rink/avatar.gd.uid rename to rinkplanner/prototype/3d_test/avatar.gd.uid diff --git a/rinkplanner/scenes/rink/camera_3d.gd b/rinkplanner/prototype/3d_test/camera_3d.gd similarity index 100% rename from rinkplanner/scenes/rink/camera_3d.gd rename to rinkplanner/prototype/3d_test/camera_3d.gd diff --git a/rinkplanner/scenes/rink/camera_3d.gd.uid b/rinkplanner/prototype/3d_test/camera_3d.gd.uid similarity index 100% rename from rinkplanner/scenes/rink/camera_3d.gd.uid rename to rinkplanner/prototype/3d_test/camera_3d.gd.uid diff --git a/rinkplanner/scenes/rink/rink.tscn b/rinkplanner/prototype/3d_test/rink.tscn similarity index 98% rename from rinkplanner/scenes/rink/rink.tscn rename to rinkplanner/prototype/3d_test/rink.tscn index 7392baf..1c18e26 100644 --- a/rinkplanner/scenes/rink/rink.tscn +++ b/rinkplanner/prototype/3d_test/rink.tscn @@ -1,8 +1,8 @@ [gd_scene load_steps=13 format=3 uid="uid://cd1ulyybmuft5"] [ext_resource type="Texture2D" uid="uid://d2f4ue6f2152o" path="res://assets/images/checkboard.png" id="1_pkikb"] -[ext_resource type="Script" uid="uid://cbufos2esa5yu" path="res://scenes/rink/camera_3d.gd" id="2_3oeyx"] -[ext_resource type="Script" uid="uid://deq4wi2r1o4e4" path="res://scenes/rink/avatar.gd" id="3_dfx6t"] +[ext_resource type="Script" uid="uid://cbufos2esa5yu" path="res://prototype/3d_test/camera_3d.gd" id="2_3oeyx"] +[ext_resource type="Script" uid="uid://deq4wi2r1o4e4" path="res://prototype/3d_test/avatar.gd" id="3_dfx6t"] [ext_resource type="ArrayMesh" uid="uid://lqtpafx25sxi" path="res://assets/models/Flieger.obj" id="4_qwf8l"] [sub_resource type="Curve3D" id="Curve3D_jm5wf"] diff --git a/rinkplanner/scenes/whiteboard/whiteboard.tscn b/rinkplanner/scenes/whiteboard/whiteboard.tscn new file mode 100644 index 0000000..f165b89 --- /dev/null +++ b/rinkplanner/scenes/whiteboard/whiteboard.tscn @@ -0,0 +1,32 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://src/whiteboard/view/rink_canvas.gd" id="1_canvas"] + +[node name="Whiteboard" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="RinkCanvas" type="Control" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +clip_contents = true +script = ExtResource("1_canvas") + +[node name="HelpLabel" type="Label" parent="."] +layout_mode = 0 +offset_left = 16.0 +offset_top = 12.0 +offset_right = 760.0 +offset_bottom = 40.0 +theme_override_colors/font_color = Color(0.85098, 0.890196, 0.941176, 1) +theme_override_font_sizes/font_size = 16 +text = "Rinkplanner – Whiteboard | Pan: mittlere Maustaste / 2 Finger | Zoom: Mausrad / Pinch" +mouse_filter = 2 diff --git a/rinkplanner/src/core/model/path_segment.gd b/rinkplanner/src/core/model/path_segment.gd new file mode 100644 index 0000000..42e0861 --- /dev/null +++ b/rinkplanner/src/core/model/path_segment.gd @@ -0,0 +1,64 @@ +class_name PathSegment +extends Resource + +## One continuous piece of a routine path with its own direction and edge. +## +## A section is composed of multiple segments because a step sequence changes +## edge and direction far more often than it changes "section". + +enum Direction { + FORWARD, + BACKWARD, +} + +enum Edge { + INSIDE, + OUTSIDE, +} + +@export var segment_id: String = "" +@export var curve: Curve2D +@export var direction: Direction = Direction.FORWARD +@export var edge: Edge = Edge.OUTSIDE +@export var duration_seconds: float = 0.0 +@export var element_id: String = "" +@export var label: String = "" + + +func get_length_m() -> float: + if curve == null: + return 0.0 + return curve.get_baked_length() + + +## Speed is derived from authored distance and duration. A zero duration is +## allowed for pure markers; their speed is reported as 0. +func get_speed_mps() -> float: + if duration_seconds <= 0.0: + return 0.0 + return get_length_m() / duration_seconds + + +func is_marker() -> bool: + return get_length_m() <= 0.0 + + +func validate() -> PackedStringArray: + var errors := PackedStringArray() + + if segment_id.is_empty(): + errors.append("segment_id must not be empty") + if curve == null: + errors.append("curve must not be null") + elif curve.point_count < 1: + errors.append("curve must contain at least one point") + if duration_seconds < 0.0: + errors.append("duration_seconds must not be negative") + if duration_seconds <= 0.0 and not is_marker(): + errors.append("a segment with length needs a positive duration_seconds") + + return errors + + +func is_valid() -> bool: + return validate().is_empty() diff --git a/rinkplanner/src/core/model/path_segment.gd.uid b/rinkplanner/src/core/model/path_segment.gd.uid new file mode 100644 index 0000000..f62fbd5 --- /dev/null +++ b/rinkplanner/src/core/model/path_segment.gd.uid @@ -0,0 +1 @@ +uid://gtfjm8v3o5kb diff --git a/rinkplanner/src/core/model/rink_dimensions.gd b/rinkplanner/src/core/model/rink_dimensions.gd new file mode 100644 index 0000000..736e43f --- /dev/null +++ b/rinkplanner/src/core/model/rink_dimensions.gd @@ -0,0 +1,123 @@ +class_name RinkDimensions +extends Resource + +## Physical rink dimensions in meters. +## +## Coordinate system: origin (0, 0) is the center of the rink, +X runs along +## the long axis and +Y along the short axis. The model always stores meters; +## only the view layer converts to pixels. + +## 0.1 mm tolerance for boundary points. Vector2 uses 32-bit floats, so exact +## arc points can land a few micrometers outside the mathematical circle. +const BOUNDARY_EPSILON := 0.0001 + +@export var length_m: float = 60.0 +@export var width_m: float = 30.0 +@export var corner_radius_m: float = 8.5 + + +func get_half_length() -> float: + return length_m * 0.5 + + +func get_half_width() -> float: + return width_m * 0.5 + + +func get_bounds() -> Rect2: + return Rect2( + Vector2(-get_half_length(), -get_half_width()), + Vector2(length_m, width_m) + ) + + +func is_valid() -> bool: + if length_m <= 0.0 or width_m <= 0.0: + return false + if corner_radius_m < 0.0: + return false + return corner_radius_m <= minf(get_half_length(), get_half_width()) + + +## Returns true when [param point] lies inside the rink surface, taking the +## rounded corner radius into account. +func contains_point(point: Vector2) -> bool: + var half_length := get_half_length() + var half_width := get_half_width() + if absf(point.x) > half_length + BOUNDARY_EPSILON: + return false + if absf(point.y) > half_width + BOUNDARY_EPSILON: + return false + + var radius := minf(corner_radius_m, minf(half_length, half_width)) + if radius <= 0.0: + return true + + var inner_x := half_length - radius + var inner_y := half_width - radius + if absf(point.x) <= inner_x or absf(point.y) <= inner_y: + return true + + var dx := absf(point.x) - inner_x + var dy := absf(point.y) - inner_y + return dx * dx + dy * dy <= radius * radius + BOUNDARY_EPSILON + + +## Returns the rink outline as a closed polygon in model coordinates. The +## rounded corners are approximated with [param arc_segments] segments each. +func get_outline_points(arc_segments: int = 8) -> PackedVector2Array: + var points := PackedVector2Array() + if not is_valid(): + return points + + var half_length := get_half_length() + var half_width := get_half_width() + var radius := minf(corner_radius_m, minf(half_length, half_width)) + + if radius <= 0.0: + points.append(Vector2(half_length, -half_width)) + points.append(Vector2(half_length, half_width)) + points.append(Vector2(-half_length, half_width)) + points.append(Vector2(-half_length, -half_width)) + return points + + var segments: int = maxi(1, arc_segments) + var corners: Array[Dictionary] = [ + {"center": Vector2(half_length - radius, half_width - radius), "start": 0.0}, + {"center": Vector2(-half_length + radius, half_width - radius), "start": PI * 0.5}, + {"center": Vector2(-half_length + radius, -half_width + radius), "start": PI}, + {"center": Vector2(half_length - radius, -half_width + radius), "start": PI * 1.5}, + ] + + for corner: Dictionary in corners: + var center: Vector2 = corner["center"] + var start_angle: float = corner["start"] + for step: int in range(segments + 1): + var angle := start_angle + (PI * 0.5) * float(step) / float(segments) + points.append(center + Vector2(cos(angle), sin(angle)) * radius) + + return points + + +## Samples a curve and verifies that every sampled point stays on the rink. +## Sampling happens in meters, independent of screen resolution. +func contains_curve(curve: Curve2D) -> bool: + if curve == null or curve.point_count == 0: + return false + + var curve_length := curve.get_baked_length() + if curve_length <= 0.0: + var baked_points := curve.get_baked_points() + if baked_points.is_empty(): + return contains_point(curve.get_point_position(0)) + for point: Vector2 in baked_points: + if not contains_point(point): + return false + return true + + var steps: int = maxi(2, ceili(curve_length / 0.5)) + for index: int in range(steps + 1): + var offset: float = curve_length * float(index) / float(steps) + if not contains_point(curve.sample_baked(offset)): + return false + return true diff --git a/rinkplanner/src/core/model/rink_dimensions.gd.uid b/rinkplanner/src/core/model/rink_dimensions.gd.uid new file mode 100644 index 0000000..f6ba55c --- /dev/null +++ b/rinkplanner/src/core/model/rink_dimensions.gd.uid @@ -0,0 +1 @@ +uid://b6q2f1vua0o8f diff --git a/rinkplanner/src/core/model/routine.gd b/rinkplanner/src/core/model/routine.gd new file mode 100644 index 0000000..39d6d70 --- /dev/null +++ b/rinkplanner/src/core/model/routine.gd @@ -0,0 +1,98 @@ +class_name Routine +extends Resource + +## Root aggregate of a choreography. Pure data: no rendering, no input, no +## timing state. The whiteboard view and the future 3D layer both consume it. + +@export var routine_id: String = "" +@export var title: String = "" +@export var skater_name: String = "" +@export var rink: RinkDimensions = RinkDimensions.new() +@export var sections: Array[RoutineSection] = [] + + +func get_duration_seconds() -> float: + var total := 0.0 + for section: RoutineSection in sections: + if section != null: + total += section.get_duration_seconds() + return total + + +func get_length_m() -> float: + var total := 0.0 + for section: RoutineSection in sections: + if section != null: + total += section.get_length_m() + return total + + +func get_section_by_id(section_id: String) -> RoutineSection: + for section: RoutineSection in sections: + if section != null and section.section_id == section_id: + return section + return null + + +## Returns a copy sorted by [member RoutineSection.sequence_order]. The authored +## array is left untouched so ordering stays explicit in the data. +func get_ordered_sections() -> Array[RoutineSection]: + var ordered: Array[RoutineSection] = [] + ordered.assign(sections) + ordered.sort_custom( + func(a: RoutineSection, b: RoutineSection) -> bool: + return a.sequence_order < b.sequence_order + ) + return ordered + + +func validate() -> PackedStringArray: + var errors := PackedStringArray() + + if routine_id.is_empty(): + errors.append("routine_id must not be empty") + if rink == null or not rink.is_valid(): + errors.append("rink dimensions are invalid") + if sections.is_empty(): + errors.append("routine must contain at least one section") + + var seen_section_ids: Dictionary[String, bool] = {} + var seen_orders: Dictionary[int, bool] = {} + var seen_segment_ids: Dictionary[String, bool] = {} + + for section: RoutineSection in sections: + if section == null: + errors.append("routine contains a null section") + continue + + for error: String in section.validate(): + errors.append("%s: %s" % [section.section_id, error]) + + if seen_section_ids.has(section.section_id): + errors.append("duplicate section_id '%s'" % section.section_id) + else: + seen_section_ids[section.section_id] = true + + if seen_orders.has(section.sequence_order): + errors.append("duplicate sequence_order %d" % section.sequence_order) + else: + seen_orders[section.sequence_order] = true + + for segment: PathSegment in section.segments: + if segment == null: + continue + if seen_segment_ids.has(segment.segment_id): + errors.append("duplicate segment_id '%s'" % segment.segment_id) + else: + seen_segment_ids[segment.segment_id] = true + + if rink != null and segment.curve != null and not rink.contains_curve(segment.curve): + errors.append( + "segment '%s' leaves the rink surface" % segment.segment_id + ) + + return errors + + +func is_valid() -> bool: + return validate().is_empty() diff --git a/rinkplanner/src/core/model/routine.gd.uid b/rinkplanner/src/core/model/routine.gd.uid new file mode 100644 index 0000000..2368c8c --- /dev/null +++ b/rinkplanner/src/core/model/routine.gd.uid @@ -0,0 +1 @@ +uid://bmpjk66ev4n4w diff --git a/rinkplanner/src/core/model/routine_section.gd b/rinkplanner/src/core/model/routine_section.gd new file mode 100644 index 0000000..5a4100c --- /dev/null +++ b/rinkplanner/src/core/model/routine_section.gd @@ -0,0 +1,62 @@ +class_name RoutineSection +extends Resource + +## A chronological block of a routine (e.g. "Opening Entry", "Step Sequence"). +## Sections are the unit for ghosting: exactly one section is active. + +@export var section_id: String = "" +@export var title: String = "" +@export var sequence_order: int = 0 +@export var segments: Array[PathSegment] = [] + + +func get_duration_seconds() -> float: + var total := 0.0 + for segment: PathSegment in segments: + if segment != null: + total += segment.duration_seconds + return total + + +func get_length_m() -> float: + var total := 0.0 + for segment: PathSegment in segments: + if segment != null: + total += segment.get_length_m() + return total + + +func get_segment_by_id(segment_id: String) -> PathSegment: + for segment: PathSegment in segments: + if segment != null and segment.segment_id == segment_id: + return segment + return null + + +func validate() -> PackedStringArray: + var errors := PackedStringArray() + + if section_id.is_empty(): + errors.append("section_id must not be empty") + if sequence_order < 0: + errors.append("sequence_order must not be negative") + if segments.is_empty(): + errors.append("section must contain at least one segment") + + var seen_ids: Dictionary[String, bool] = {} + for segment: PathSegment in segments: + if segment == null: + errors.append("section contains a null segment") + continue + for error: String in segment.validate(): + errors.append("%s: %s" % [segment.segment_id, error]) + if seen_ids.has(segment.segment_id): + errors.append("duplicate segment_id '%s' in section" % segment.segment_id) + else: + seen_ids[segment.segment_id] = true + + return errors + + +func is_valid() -> bool: + return validate().is_empty() diff --git a/rinkplanner/src/core/model/routine_section.gd.uid b/rinkplanner/src/core/model/routine_section.gd.uid new file mode 100644 index 0000000..e231b96 --- /dev/null +++ b/rinkplanner/src/core/model/routine_section.gd.uid @@ -0,0 +1 @@ +uid://y5yr105vbsug diff --git a/rinkplanner/src/whiteboard/view/canvas_transform.gd b/rinkplanner/src/whiteboard/view/canvas_transform.gd new file mode 100644 index 0000000..3403a99 --- /dev/null +++ b/rinkplanner/src/whiteboard/view/canvas_transform.gd @@ -0,0 +1,66 @@ +class_name CanvasTransform +extends RefCounted + +## Pure meter-to-pixel mapping for the 2D whiteboard. No Node dependencies, so +## it can be unit-tested headless. +## +## [member origin_px] is the screen position of the model origin (0, 0). +## +Y in model space points down on screen, matching Godot's 2D axis. + +const MIN_PIXELS_PER_METER := 2.0 +const MAX_PIXELS_PER_METER := 200.0 + +var pixels_per_meter: float = 28.0 +var origin_px: Vector2 = Vector2.ZERO + + +func model_to_screen(point: Vector2) -> Vector2: + return origin_px + point * pixels_per_meter + + +func screen_to_model(point: Vector2) -> Vector2: + return (point - origin_px) / pixels_per_meter + + +func pan(delta_px: Vector2) -> void: + origin_px += delta_px + + +## Zooms around a fixed screen point so the model coordinate below the cursor +## stays in place. +func zoom_at(screen_point: Vector2, factor: float) -> void: + var model_point := screen_to_model(screen_point) + pixels_per_meter = clampf( + pixels_per_meter * factor, + MIN_PIXELS_PER_METER, + MAX_PIXELS_PER_METER + ) + origin_px = screen_point - model_point * pixels_per_meter + + +func fit_rect( + model_rect: Rect2, + viewport_size: Vector2, + padding_px: float = 24.0 +) -> void: + if model_rect.size.x <= 0.0 or model_rect.size.y <= 0.0: + return + + var available := viewport_size - Vector2.ONE * padding_px * 2.0 + if available.x <= 0.0 or available.y <= 0.0: + return + + var scale_x := available.x / model_rect.size.x + var scale_y := available.y / model_rect.size.y + pixels_per_meter = clampf( + minf(scale_x, scale_y), + MIN_PIXELS_PER_METER, + MAX_PIXELS_PER_METER + ) + origin_px = viewport_size * 0.5 - model_rect.get_center() * pixels_per_meter + + +func get_visible_model_rect(viewport_size: Vector2) -> Rect2: + var top_left := screen_to_model(Vector2.ZERO) + var bottom_right := screen_to_model(viewport_size) + return Rect2(top_left, bottom_right - top_left) diff --git a/rinkplanner/src/whiteboard/view/canvas_transform.gd.uid b/rinkplanner/src/whiteboard/view/canvas_transform.gd.uid new file mode 100644 index 0000000..a8893a5 --- /dev/null +++ b/rinkplanner/src/whiteboard/view/canvas_transform.gd.uid @@ -0,0 +1 @@ +uid://c1vq6mtomx566 diff --git a/rinkplanner/src/whiteboard/view/rink_canvas.gd b/rinkplanner/src/whiteboard/view/rink_canvas.gd new file mode 100644 index 0000000..c64564e --- /dev/null +++ b/rinkplanner/src/whiteboard/view/rink_canvas.gd @@ -0,0 +1,229 @@ +class_name RinkCanvas +extends Control + +## Phase 1 whiteboard canvas: draws the rink in meters and provides pan/zoom. +## +## This view owns no routine data. A controller assigns the rink dimensions and, +## later, the routine to render. All geometry stays in meters until [_draw]. + +signal view_changed + +const BACKGROUND_COLOR := Color("#0b1220") +const ICE_COLOR := Color("#eef6fb") +const BOARDS_COLOR := Color("#1e293b") +const CENTER_LINE_COLOR := Color("#dc2626") +const CIRCLE_COLOR := Color("#94a3b8") +const OUTLINE_ARC_SEGMENTS := 12 +const CENTER_CIRCLE_RADIUS_M := 4.5 +const CENTER_CIRCLE_SEGMENTS := 64 + +@export var fit_padding_px: float = 32.0 + +var _rink: RinkDimensions = RinkDimensions.new() +var _transform: CanvasTransform = CanvasTransform.new() +var _auto_fit: bool = true + +var _panning: bool = false +var _touch_points: Dictionary[int, Vector2] = {} +var _pinch_center: Vector2 = Vector2.ZERO +var _pinch_distance: float = 0.0 + + +func _ready() -> void: + focus_mode = Control.FOCUS_ALL + mouse_filter = Control.MOUSE_FILTER_STOP + fit_rink() + + +func _notification(what: int) -> void: + if what == NOTIFICATION_RESIZED and is_node_ready() and _auto_fit: + fit_rink() + + +func set_rink(rink: RinkDimensions) -> void: + _rink = rink + fit_rink() + + +func get_rink() -> RinkDimensions: + return _rink + + +func get_view_transform() -> CanvasTransform: + return _transform + + +## Fits the complete rink into the current control size. +func fit_rink() -> void: + if _rink == null or size.x <= 0.0 or size.y <= 0.0: + return + _transform.fit_rect(_rink.get_bounds(), size, fit_padding_px) + _auto_fit = true + queue_redraw() + view_changed.emit() + + +func _draw() -> void: + draw_rect(Rect2(Vector2.ZERO, size), BACKGROUND_COLOR) + if _rink == null: + return + + var outline := _to_screen(_rink.get_outline_points(OUTLINE_ARC_SEGMENTS)) + if outline.size() >= 3: + draw_colored_polygon(outline, ICE_COLOR) + + _draw_center_line() + _draw_center_circle() + + if outline.size() >= 3: + # draw_polyline has no closed flag: append the first point explicitly. + var closed_outline := outline.duplicate() + closed_outline.append(outline[0]) + draw_polyline( + closed_outline, + BOARDS_COLOR, + maxf(2.0, 0.14 * _transform.pixels_per_meter), + true + ) + + +func _draw_center_line() -> void: + var half_width := _rink.get_half_width() + var start := _transform.model_to_screen(Vector2(0.0, -half_width)) + var end := _transform.model_to_screen(Vector2(0.0, half_width)) + var color := CENTER_LINE_COLOR + color.a = 0.35 + draw_line( + start, + end, + color, + maxf(1.0, 0.09 * _transform.pixels_per_meter), + true + ) + + +func _draw_center_circle() -> void: + var points := PackedVector2Array() + for index: int in range(CENTER_CIRCLE_SEGMENTS): + var angle := TAU * float(index) / float(CENTER_CIRCLE_SEGMENTS) + var model_point := Vector2(cos(angle), sin(angle)) * CENTER_CIRCLE_RADIUS_M + points.append(_transform.model_to_screen(model_point)) + if points.is_empty(): + return + points.append(points[0]) + draw_polyline( + points, + CIRCLE_COLOR, + maxf(1.0, 0.05 * _transform.pixels_per_meter), + true + ) + + +func _to_screen(model_points: PackedVector2Array) -> PackedVector2Array: + var screen_points := PackedVector2Array() + screen_points.resize(model_points.size()) + for index: int in range(model_points.size()): + screen_points[index] = _transform.model_to_screen(model_points[index]) + return screen_points + + +func _gui_input(event: InputEvent) -> void: + if event is InputEventMouseButton: + _handle_mouse_button(event as InputEventMouseButton) + elif event is InputEventMouseMotion and _panning: + _apply_pan((event as InputEventMouseMotion).relative) + elif event is InputEventPanGesture: + _apply_pan(-(event as InputEventPanGesture).delta * 16.0) + elif event is InputEventScreenTouch: + _handle_screen_touch(event as InputEventScreenTouch) + elif event is InputEventScreenDrag: + _handle_screen_drag(event as InputEventScreenDrag) + + +func _handle_mouse_button(event: InputEventMouseButton) -> void: + match event.button_index: + MOUSE_BUTTON_WHEEL_UP: + if event.pressed: + _zoom_at(event.position, 1.1) + accept_event() + MOUSE_BUTTON_WHEEL_DOWN: + if event.pressed: + _zoom_at(event.position, 1.0 / 1.1) + accept_event() + MOUSE_BUTTON_MIDDLE: + _panning = event.pressed + accept_event() + + +func _handle_screen_touch(event: InputEventScreenTouch) -> void: + if event.pressed: + _touch_points[event.index] = event.position + else: + _touch_points.erase(event.index) + _reset_pinch_reference() + accept_event() + + +func _handle_screen_drag(event: InputEventScreenDrag) -> void: + if not _touch_points.has(event.index): + return + + var previous: Vector2 = _touch_points[event.index] + _touch_points[event.index] = event.position + + if _touch_points.size() == 1: + _apply_pan(event.position - previous) + elif _touch_points.size() >= 2: + _update_pinch() + + accept_event() + + +func _reset_pinch_reference() -> void: + var points := _get_two_touch_points() + if points.size() < 2: + _pinch_center = Vector2.ZERO + _pinch_distance = 0.0 + return + _pinch_center = (points[0] + points[1]) * 0.5 + _pinch_distance = points[0].distance_to(points[1]) + + +func _update_pinch() -> void: + var points := _get_two_touch_points() + if points.size() < 2: + return + + var center := (points[0] + points[1]) * 0.5 + var distance := points[0].distance_to(points[1]) + if _pinch_distance > 0.0: + _apply_pan(center - _pinch_center) + _zoom_at(center, distance / _pinch_distance) + + _pinch_center = center + _pinch_distance = distance + + +func _get_two_touch_points() -> Array[Vector2]: + var points: Array[Vector2] = [] + for index: int in _touch_points.keys(): + if points.size() == 2: + break + points.append(_touch_points[index]) + return points + + +func _apply_pan(delta_px: Vector2) -> void: + _transform.pan(delta_px) + _mark_view_changed() + + +func _zoom_at(screen_point: Vector2, factor: float) -> void: + _transform.zoom_at(screen_point, factor) + _mark_view_changed() + + +func _mark_view_changed() -> void: + _auto_fit = false + queue_redraw() + view_changed.emit() diff --git a/rinkplanner/src/whiteboard/view/rink_canvas.gd.uid b/rinkplanner/src/whiteboard/view/rink_canvas.gd.uid new file mode 100644 index 0000000..11d259c --- /dev/null +++ b/rinkplanner/src/whiteboard/view/rink_canvas.gd.uid @@ -0,0 +1 @@ +uid://rx7as3swfq8k diff --git a/rinkplanner/tests/README.md b/rinkplanner/tests/README.md new file mode 100644 index 0000000..c2f4a25 --- /dev/null +++ b/rinkplanner/tests/README.md @@ -0,0 +1,26 @@ +# Tests + +Headless tests for the Phase 1 model. They require no editor and no display. + +From the repository root: + +```bash +./tools/test.sh +``` + +Or manually: + +```bash +GODOT=/opt/godot/Godot_v4.7.2-stable_linux.x86_64 + +# Import once (registers global class names and resources) +$GODOT --headless --path rinkplanner --import + +# Run model tests +$GODOT --headless --path rinkplanner --script res://tests/run_model_tests.gd + +# Run canvas transform/geometry tests +$GODOT --headless --path rinkplanner --script res://tests/run_canvas_tests.gd +``` + +Exit code `0` means all assertions passed. Failures are printed to stderr. diff --git a/rinkplanner/tests/run_canvas_tests.gd b/rinkplanner/tests/run_canvas_tests.gd new file mode 100644 index 0000000..145d729 --- /dev/null +++ b/rinkplanner/tests/run_canvas_tests.gd @@ -0,0 +1,144 @@ +extends SceneTree + +## Headless tests for the 2D canvas view helpers (no rendering required). +## +## Run with: +## godot --headless --path rinkplanner --script res://tests/run_canvas_tests.gd + +const CanvasTransformModel := preload("res://src/whiteboard/view/canvas_transform.gd") +const RinkDimensionsModel := preload("res://src/core/model/rink_dimensions.gd") + +var _assertions: int = 0 +var _failures: PackedStringArray = PackedStringArray() + + +func _init() -> void: + _test_transform_round_trip() + _test_fit_rect() + _test_zoom_keeps_point() + _test_zoom_limits() + _test_pan() + _test_visible_rect() + _test_rink_outline() + _finish() + + +func _test_transform_round_trip() -> void: + var transform: CanvasTransform = CanvasTransformModel.new() + transform.pixels_per_meter = 20.0 + transform.origin_px = Vector2(100.0, 50.0) + + var model_point := Vector2(3.0, -2.0) + var screen_point := transform.model_to_screen(model_point) + _check_vec(screen_point, Vector2(160.0, 10.0), "model point maps to screen") + _check_vec(transform.screen_to_model(screen_point), model_point, "screen mapping round-trips") + + +func _test_fit_rect() -> void: + var transform: CanvasTransform = CanvasTransformModel.new() + transform.fit_rect(Rect2(Vector2(-30.0, -15.0), Vector2(60.0, 30.0)), Vector2(1000.0, 600.0), 20.0) + + _check_approx(transform.pixels_per_meter, 16.0, "fit uses the limiting axis") + _check_vec(transform.model_to_screen(Vector2.ZERO), Vector2(500.0, 300.0), "fit centers the rink") + + +func _test_zoom_keeps_point() -> void: + var transform: CanvasTransform = CanvasTransformModel.new() + transform.pixels_per_meter = 20.0 + transform.origin_px = Vector2(500.0, 300.0) + + var zoom_point := Vector2(250.0, 150.0) + var model_before := transform.screen_to_model(zoom_point) + transform.zoom_at(zoom_point, 2.0) + var model_after := transform.screen_to_model(zoom_point) + + _check_approx(transform.pixels_per_meter, 40.0, "zoom scales pixels per meter") + _check_vec(model_after, model_before, "zoom keeps the model point under the cursor") + + +func _test_zoom_limits() -> void: + var transform: CanvasTransform = CanvasTransformModel.new() + transform.zoom_at(Vector2.ZERO, 100000.0) + _check_approx( + transform.pixels_per_meter, + CanvasTransformModel.MAX_PIXELS_PER_METER, + "zoom is clamped at the maximum" + ) + transform.zoom_at(Vector2.ZERO, 0.000001) + _check_approx( + transform.pixels_per_meter, + CanvasTransformModel.MIN_PIXELS_PER_METER, + "zoom is clamped at the minimum" + ) + + +func _test_pan() -> void: + var transform: CanvasTransform = CanvasTransformModel.new() + var origin_before := transform.origin_px + transform.pan(Vector2(25.0, -10.0)) + _check_vec(transform.origin_px, origin_before + Vector2(25.0, -10.0), "pan shifts the origin") + + +func _test_visible_rect() -> void: + var transform: CanvasTransform = CanvasTransformModel.new() + transform.pixels_per_meter = 10.0 + transform.origin_px = Vector2(400.0, 300.0) + + var visible := transform.get_visible_model_rect(Vector2(800.0, 600.0)) + _check_vec(visible.position, Vector2(-40.0, -30.0), "visible rect starts at the top-left model point") + _check_vec(visible.size, Vector2(80.0, 60.0), "visible rect size follows viewport and scale") + + +func _test_rink_outline() -> void: + var rink: RinkDimensions = RinkDimensionsModel.new() + var segments := 12 + var points := rink.get_outline_points(segments) + + _check(points.size() == 4 * (segments + 1), "outline has one point per arc segment") + _check_vec(points[0], Vector2(30.0, 6.5), "outline starts at the right board") + for point: Vector2 in points: + _check( + rink.contains_point(point), + "outline point stays on the rink surface: %s" % point + ) + + var corner_center := Vector2(30.0 - 8.5, 15.0 - 8.5) + var corner_point := corner_center + Vector2(cos(PI * 0.25), sin(PI * 0.25)) * 8.5 + _check_approx( + corner_point.distance_to(corner_center), + 8.5, + "corner arc keeps the configured radius" + ) + _check(rink.contains_point(corner_point), "corner arc lies on the rink surface") + + +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.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_canvas_tests.gd.uid b/rinkplanner/tests/run_canvas_tests.gd.uid new file mode 100644 index 0000000..fcdc712 --- /dev/null +++ b/rinkplanner/tests/run_canvas_tests.gd.uid @@ -0,0 +1 @@ +uid://kpvci160eaag diff --git a/rinkplanner/tests/run_model_tests.gd b/rinkplanner/tests/run_model_tests.gd new file mode 100644 index 0000000..1336f79 --- /dev/null +++ b/rinkplanner/tests/run_model_tests.gd @@ -0,0 +1,234 @@ +extends SceneTree + +## Headless tests for the Phase 1 data model. +## +## Run with: +## godot --headless --path rinkplanner --script res://tests/run_model_tests.gd + +const RinkDimensionsModel := preload("res://src/core/model/rink_dimensions.gd") +const PathSegmentModel := preload("res://src/core/model/path_segment.gd") +const RoutineSectionModel := preload("res://src/core/model/routine_section.gd") +const RoutineModel := preload("res://src/core/model/routine.gd") + +var _assertions: int = 0 +var _failures: PackedStringArray = PackedStringArray() + + +func _init() -> void: + _test_rink_geometry() + _test_segment_timing() + _test_section_aggregation() + _test_routine_validation() + _finish() + + +func _test_rink_geometry() -> void: + var rink: RinkDimensions = RinkDimensionsModel.new() + + _check(rink.is_valid(), "default rink dimensions are valid") + _check_approx(rink.get_bounds().size.x, 60.0, "rink length is 60 m") + _check_approx(rink.get_bounds().size.y, 30.0, "rink width is 30 m") + _check(rink.contains_point(Vector2.ZERO), "rink center is inside") + _check(rink.contains_point(Vector2(29.0, 0.0)), "point near long board is inside") + _check(not rink.contains_point(Vector2(30.5, 0.0)), "point beyond long board is outside") + _check( + not rink.contains_point(Vector2(29.5, 14.5)), + "point in the rounded corner cut-off is outside" + ) + _check( + rink.contains_point(Vector2(27.0, 12.0)), + "point inside the rounded corner arc is inside" + ) + + var inside_curve := _make_curve(Vector2(-5.0, 0.0), Vector2(5.0, 0.0)) + _check(rink.contains_curve(inside_curve), "curve inside the rink is accepted") + + var outside_curve := _make_curve(Vector2(-35.0, 0.0), Vector2(35.0, 0.0)) + _check(not rink.contains_curve(outside_curve), "curve leaving the rink is rejected") + + +func _test_segment_timing() -> void: + var segment: PathSegment = PathSegmentModel.new() + segment.segment_id = "seg-straight" + segment.curve = _make_curve(Vector2(-5.0, 0.0), Vector2(5.0, 0.0)) + segment.duration_seconds = 5.0 + + _check(segment.is_valid(), "straight segment with duration is valid") + _check_approx(segment.get_length_m(), 10.0, "straight segment length is 10 m") + _check_approx(segment.get_speed_mps(), 2.0, "speed is derived as length / duration") + _check(not segment.is_marker(), "segment with length is not a marker") + + var marker: PathSegment = PathSegmentModel.new() + marker.segment_id = "seg-marker" + marker.curve = _make_curve(Vector2(0.0, 0.0), Vector2(0.0, 0.0)) + marker.duration_seconds = 0.0 + _check(marker.is_valid(), "zero-length marker without duration is valid") + _check(marker.is_marker(), "zero-length segment reports as marker") + _check_approx(marker.get_speed_mps(), 0.0, "marker speed is 0") + + var missing_duration: PathSegment = PathSegmentModel.new() + missing_duration.segment_id = "seg-missing-duration" + missing_duration.curve = _make_curve(Vector2(-5.0, 0.0), Vector2(5.0, 0.0)) + missing_duration.duration_seconds = 0.0 + _check( + not missing_duration.is_valid(), + "segment with length but no duration is invalid" + ) + + +func _test_section_aggregation() -> void: + var section: RoutineSection = RoutineSectionModel.new() + section.section_id = "section-steps" + section.sequence_order = 0 + section.segments = [ + _make_segment("seg-1", 4.0, Vector2(-5.0, 0.0), Vector2(0.0, 0.0)), + _make_segment("seg-2", 2.5, Vector2(0.0, 0.0), Vector2(5.0, 0.0)), + ] + + _check(section.is_valid(), "section with two valid segments is valid") + _check_approx(section.get_duration_seconds(), 6.5, "section duration is the sum of segments") + _check_approx(section.get_length_m(), 10.0, "section length is the sum of segments") + _check( + section.get_segment_by_id("seg-2") != null, + "segment can be looked up by id" + ) + _check( + section.get_segment_by_id("missing") == null, + "unknown segment id returns null" + ) + _check( + section.get_segment_by_id("seg-2") == section.segments[1], + "segment lookup returns the matching instance" + ) + + +func _test_routine_validation() -> void: + var routine: Routine = _make_routine() + + _check(routine.is_valid(), "complete routine is valid") + _check(routine.validate().is_empty(), "valid routine reports no errors") + _check_approx(routine.get_duration_seconds(), 9.5, "routine duration sums all sections") + _check_approx(routine.get_length_m(), 15.0, "routine length sums all segments") + _check( + routine.get_ordered_sections()[0].section_id == "section-opening", + "sections are ordered by sequence_order" + ) + _check( + routine.get_section_by_id("section-steps") != null, + "section can be looked up by id" + ) + + var outside_routine: Routine = _make_routine() + outside_routine.sections[0].segments[0].curve = _make_curve( + Vector2(-35.0, 0.0), + Vector2(35.0, 0.0) + ) + _check( + not outside_routine.is_valid(), + "routine with a segment outside the rink is invalid" + ) + _check( + _contains_error(outside_routine.validate(), "leaves the rink surface"), + "outside-rink error is reported" + ) + + var duplicate_section: Routine = _make_routine() + duplicate_section.sections[1].section_id = duplicate_section.sections[0].section_id + _check( + _contains_error(duplicate_section.validate(), "duplicate section_id"), + "duplicate section ids are reported" + ) + + var duplicate_order: Routine = _make_routine() + duplicate_order.sections[1].sequence_order = duplicate_order.sections[0].sequence_order + _check( + _contains_error(duplicate_order.validate(), "duplicate sequence_order"), + "duplicate sequence orders are reported" + ) + + var duplicate_segment: Routine = _make_routine() + duplicate_segment.sections[1].segments[0].segment_id = "seg-opening-1" + _check( + _contains_error(duplicate_segment.validate(), "duplicate segment_id"), + "duplicate segment ids across sections are reported" + ) + + var empty_routine: Routine = RoutineModel.new() + _check(not empty_routine.is_valid(), "routine without id and sections is invalid") + + +func _make_routine() -> Routine: + var routine: Routine = RoutineModel.new() + routine.routine_id = "routine-test" + routine.title = "Test Routine" + routine.rink = RinkDimensionsModel.new() + + var opening: RoutineSection = RoutineSectionModel.new() + opening.section_id = "section-opening" + opening.sequence_order = 0 + opening.segments = [ + _make_segment("seg-opening-1", 4.0, Vector2(-5.0, 0.0), Vector2(0.0, 0.0)), + _make_segment("seg-opening-2", 2.5, Vector2(0.0, 0.0), Vector2(5.0, 0.0)), + ] + + var steps: RoutineSection = RoutineSectionModel.new() + steps.section_id = "section-steps" + steps.sequence_order = 1 + steps.segments = [ + _make_segment("seg-steps-1", 3.0, Vector2(5.0, 0.0), Vector2(5.0, 5.0)), + ] + + routine.sections = [opening, steps] + return routine + + +func _make_segment( + segment_id: String, + duration_seconds: float, + from: Vector2, + to: Vector2 +) -> PathSegment: + var segment: PathSegment = PathSegmentModel.new() + segment.segment_id = segment_id + segment.curve = _make_curve(from, to) + segment.duration_seconds = duration_seconds + return segment + + +func _make_curve(from: Vector2, to: Vector2) -> Curve2D: + var curve := Curve2D.new() + curve.add_point(from) + curve.add_point(to) + return curve + + +func _contains_error(errors: PackedStringArray, fragment: String) -> bool: + for error: String in errors: + if error.contains(fragment): + return true + return false + + +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 _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_model_tests.gd.uid b/rinkplanner/tests/run_model_tests.gd.uid new file mode 100644 index 0000000..5dfa31b --- /dev/null +++ b/rinkplanner/tests/run_model_tests.gd.uid @@ -0,0 +1 @@ +uid://dlm8rr8k3idh3 diff --git a/tools/test.sh b/tools/test.sh new file mode 100755 index 0000000..709db8d --- /dev/null +++ b/tools/test.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +PROJECT_DIR="$REPO_ROOT/rinkplanner" +GODOT="${GODOT:-/opt/godot/Godot_v4.7.2-stable_linux.x86_64}" + +if [[ ! -x "$GODOT" ]]; then + echo "Godot binary not found: $GODOT" >&2 + echo "Set GODOT=/path/to/godot" >&2 + exit 1 +fi + +# Import once so global class names and resources are registered. +"$GODOT" --headless --path "$PROJECT_DIR" --import >/dev/null + +"$GODOT" --headless --path "$PROJECT_DIR" \ + --script res://tests/run_model_tests.gd + +"$GODOT" --headless --path "$PROJECT_DIR" \ + --script res://tests/run_canvas_tests.gd