feat(analyse): Pfeile zeichnen wie bei Lichess, Zugliste als Baum
public
feat(analyse): Pfeile zeichnen wie bei Lichess, Zugliste als Baum Rechte Maustaste zieht Pfeile und Kreise (Umschalt rot, Alt blau, beides gelb). Auf dem Analysebrett gehören sie zum Zug und stehen im PGN als [%cal]/[%csl], auch an der Partie selbst (GameState.shapes); sonst sind sie vorübergehend. Die Zugliste zeigt Nebenvarianten als aufklappbaren Baum mit anklickbaren Zügen; ↑/↓ durch sichtbare Zeilen, → auf/hinein, ← zu/zurück. Plan für Aufnahme und Wiedergabe in roadmap/nachbesprechung.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 82b1120eb5aecc5afdb408f34f9f4e52dc90d75d |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 3c9deb297eb3 |
| Children | b5a695421283 |
| Pushlog | by timo via ssh by timo via ssh |
modified ROADMAP.md +1 −0
| @@ -634,6 +634,7 @@ behält Reihenfolge und offene Fragen. | ||
| 634 | 634 | | Datei | Inhalt | Status | |
| 635 | 635 | | --- | --- | --- | |
| 636 | 636 | | [`roadmap/analysebrett.md`](roadmap/analysebrett.md) | Analysebrett mit Variantenbaum, „Warum?“ und „Warum nicht?“ | Stufen 1–4 umgesetzt | |
| 637 | +| [`roadmap/nachbesprechung.md`](roadmap/nachbesprechung.md) | Pfeile zeichnen, Zugliste als Baum, Partie vertont durchgehen (Aufnahme, Wiedergabe) | Stufen 1–2 umgesetzt | | |
| 637 | 638 | | [`roadmap/training.md`](roadmap/training.md) | Rätsel aus eigenen Partien, „Aus Fehlern lernen“, Wiederholung mit FSRS und SQLite | Schritte 1 und 2 umgesetzt, zurückgenommene Züge im PGN | |
| 638 | 639 | |
| 639 | 640 | ## Reihenfolge |
added roadmap/nachbesprechung.md +56 −0
| @@ -0,0 +1,56 @@ | ||
| 1 | +# Nachbesprechung: Partie vertont durchgehen | |
| 2 | + | |
| 3 | +Florian spielt eine Partie und geht sie danach laut kommentierend durch, zum Beispiel für | |
| 4 | +seine Lehrerin, die er nur einmal pro Woche sieht. Kein Schnittprogramm: Aufnahme und | |
| 5 | +Wiedergabe gehören in die App. | |
| 6 | + | |
| 7 | +## 1. Pfeile und Kreise zeichnen (umgesetzt) | |
| 8 | + | |
| 9 | +Wie bei Lichess: rechte Maustaste ziehen gibt einen Pfeil, Rechtsklick auf ein Feld einen | |
| 10 | +Kreis. Ohne Taste grün, Umschalt (oder Strg) rot, Alt blau, beides gelb. Derselbe Pfeil | |
| 11 | +noch einmal nimmt ihn weg, in anderer Farbe färbt ihn um. Ein Linksklick aufs Brett räumt | |
| 12 | +alles weg. | |
| 13 | + | |
| 14 | +- Auf dem Analysebrett zur Partie gehören die Pfeile zum Zug: Sie stehen im Spielstand und | |
| 15 | + im PGN als `[%cal]`/`[%csl]`, an Nebenvarianten wie an der Partie selbst | |
| 16 | + (`GameState.shapes`). | |
| 17 | +- Sonst (laufende Partie, Kurs, Rätsel) sind sie vorübergehend und nach dem nächsten Zug weg. | |
| 18 | +- Beim Aufbauen einer Stellung nimmt der Rechtsklick weiter eine Figur weg. | |
| 19 | +- PGN-Farben: gelb bleibt gelb, blau bleibt blau. Früher wurden sie beim Laden zu den Farben | |
| 20 | + von Drohung und Hinweis. | |
| 21 | + | |
| 22 | +## 2. Zugliste als Baum (umgesetzt) | |
| 23 | + | |
| 24 | +Die Partie läuft Zeile für Zeile nach unten. Ein Zug mit Nebenvarianten hat einen | |
| 25 | +Aufklapp-Pfeil, darunter stehen die Varianten. Jeder Zug darin ist anklickbar, Alternativen | |
| 26 | +innerhalb einer Variante hängen eine Stufe tiefer und lassen sich ebenso aufklappen. Steht | |
| 27 | +das Analysebrett in einer Variante, klappt die Liste bis dorthin auf und markiert den Zug. | |
| 28 | + | |
| 29 | +Tastatur wie bei Bäumen üblich (WAI-ARIA Treeview): ↑/↓ durch die sichtbaren Zeilen, bei | |
| 30 | +zugeklappten Zweigen also nur durch die Partie. → klappt auf bzw. geht in die erste | |
| 31 | +Variante, ← klappt zu bzw. zurück zur Zeile darüber. Gibt es nichts auf- oder zuzuklappen, | |
| 32 | +gehen ← und → wie bisher ans Brett. | |
| 33 | + | |
| 34 | +## 3. Aufnahme (offen) | |
| 35 | + | |
| 36 | +Kein Bildschirmvideo, sondern Ton plus Zeitleiste: Während der Aufnahme schreibt die App | |
| 37 | +mit, wann welcher Knoten zu sehen war und welche Pfeile darauf lagen. Die Zeitleiste kommt | |
| 38 | +als eigener Header in die PGN (z. B. `[Replay "…"]`), andere Programme ignorieren ihn. | |
| 39 | + | |
| 40 | +- Mikrofon: GStreamer über `gi` (`autoaudiosrc ! audioconvert ! opusenc ! oggmux`). Unter | |
| 41 | + Linux ist das da, wo GTK läuft; GTKs eigene Medienwiedergabe nutzt ebenfalls GStreamer. | |
| 42 | + Die Alternative `sounddevice` (PortAudio) bräuchte noch einen Opus-Encoder. | |
| 43 | +- Speichern als Opus (nur Ton, 10 Minuten etwa 3–5 MB), WebM oder MP4. Das Video rendert der | |
| 44 | + Video-Export aus der Zeitleiste und legt den Ton darunter. | |
| 45 | +- Die PGN steckt in den Metadaten. Geprüft: ffmpeg schreibt sie mit Zeilenumbrüchen in | |
| 46 | + Opus, WebM, MP4 und M4A, und sie kommt unverändert zurück. | |
| 47 | + | |
| 48 | +## 4. Wiedergabe (offen) | |
| 49 | + | |
| 50 | +Datei öffnen, PGN aus den Metadaten holen, Ton mit `Gtk.MediaFile` abspielen, das | |
| 51 | +Analysebrett folgt der Zeitleiste. Mit Schieberegler zum Spulen. Ein Video wird nur | |
| 52 | +angehört, das Bild kommt vom Analysebrett. | |
| 53 | + | |
| 54 | +- Opus selbst lesen, ohne ffprobe: Ogg-Seiten, darin der OpusTags-Block mit Vorbis-Kommentaren. | |
| 55 | +- WebM selbst lesen: EBML, über den SeekHead zu den Tags. Etwas mehr Code als Opus, aber | |
| 56 | + ohne ffmpeg machbar. | |
modified schach/app.py +11 −9
| @@ -341,6 +341,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 341 | 341 | self.movelist.on_continue = self._continue_from |
| 342 | 342 | self.movelist.on_why_not = self._why_not_from_list |
| 343 | 343 | self.movelist.on_variation = self._on_list_variation |
| 344 | + self.view.on_shapes = self._drawn | |
| 344 | 345 | |
| 345 | 346 | self.split = Adw.OverlaySplitView( |
| 346 | 347 | content=box, sidebar=self._build_sidebar(self.movelist), sidebar_position=Gtk.PackType.END, |
| @@ -575,6 +576,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 575 | 576 | if self._browsing(): |
| 576 | 577 | shown = self._board_at(self.browse_ply) |
| 577 | 578 | v.board = shown |
| 579 | + v.show_drawn(("stellung", shown.fen())) | |
| 578 | 580 | v.last_move = shown.peek() if shown.move_stack else None |
| 579 | 581 | v.interactive = False |
| 580 | 582 | v.selected = None |
| @@ -589,6 +591,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 589 | 591 | return |
| 590 | 592 | |
| 591 | 593 | v.board = self.board |
| 594 | + v.show_drawn(("stellung", self.board.fen())) # vorübergehend: nach dem nächsten Zug weg | |
| 592 | 595 | v.last_move = self.board.peek() if self.board.move_stack else None |
| 593 | 596 | v.interactive = self._human_to_move() |
| 594 | 597 | if not v.interactive: |
| @@ -1488,19 +1491,17 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1488 | 1491 | |
| 1489 | 1492 | def _movelist_update(self): |
| 1490 | 1493 | """Zugliste samt Nebenvarianten vom Analysebrett; markiert, wo das Analysebrett gerade steht.""" |
| 1491 | - self.list_variations = self._list_variations() | |
| 1494 | + branches, self.list_nodes = self._list_variations() | |
| 1492 | 1495 | selected = None |
| 1493 | 1496 | if self.demo is not None and self.demo.get("game") and not self._demo_real(self.demo["node"]): |
| 1494 | - top = self.demo["node"] | |
| 1495 | - while not self._demo_real(top.parent): | |
| 1496 | - top = top.parent | |
| 1497 | - selected = next((i for i, (_p, _t, n) in enumerate(self.list_variations) if n is top), None) | |
| 1497 | + selected = id(self.demo["node"]) if id(self.demo["node"]) in self.list_nodes else None | |
| 1498 | 1498 | self.movelist.update(self.board, self.evals, self.symbols, None if selected is not None else self._list_ply(), |
| 1499 | - [(p, t) for p, t, _n in self.list_variations], selected, times=self.times) | |
| 1499 | + branches, selected, times=self.times) | |
| 1500 | 1500 | |
| 1501 | 1501 | def _on_list_variation(self, index): |
| 1502 | - if 0 <= index < len(getattr(self, "list_variations", ())) and not (self.demo and self.demo["busy"]): | |
| 1503 | - self._analysis_open_node(self.list_variations[index][2]) | |
| 1502 | + node = getattr(self, "list_nodes", {}).get(index) | |
| 1503 | + if node is not None and not (self.demo and self.demo["busy"]): | |
| 1504 | + self._analysis_open_node(node) | |
| 1504 | 1505 | |
| 1505 | 1506 | def _list_ply(self): |
| 1506 | 1507 | """Markierte Zeile in der Zugliste: beim Analysebrett der Zug der Partie, an dem man gerade ist.""" |
| @@ -1945,6 +1946,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1945 | 1946 | variations=self._game_variations() if variations else {}, |
| 1946 | 1947 | takebacks={p: list(v) for p, v in self.takebacks.items()}, |
| 1947 | 1948 | times=dict(self.times), |
| 1949 | + shapes=self._game_shapes() if variations else {}, | |
| 1948 | 1950 | ) |
| 1949 | 1951 | |
| 1950 | 1952 | def load_state(self, state): |
| @@ -1963,7 +1965,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1963 | 1965 | self.times = dict(state.times) |
| 1964 | 1966 | self._times_len, self._turn_since = len(self.board.move_stack), time.monotonic() |
| 1965 | 1967 | self.takebacks = {p: list(v) for p, v in state.takebacks.items()} |
| 1966 | - self._game_tree_restore(state.variations) | |
| 1968 | + self._game_tree_restore(state.variations, state.shapes) | |
| 1967 | 1969 | self.save_name = state.name |
| 1968 | 1970 | self.save_path = state.path |
| 1969 | 1971 | self.end_shown = self._over() |
modified schach/board.py +77 −8
| @@ -7,7 +7,7 @@ import gi | ||
| 7 | 7 | |
| 8 | 8 | gi.require_version("Gtk", "4.0") |
| 9 | 9 | gi.require_version("Adw", "1") |
| 10 | -from gi.repository import Adw, GLib, Gtk # noqa: E402 | |
| 10 | +from gi.repository import Adw, Gdk, GLib, Gtk # noqa: E402 | |
| 11 | 11 | |
| 12 | 12 | from .diagram import ( # noqa: E402,F401 – Namen bleiben hier importierbar |
| 13 | 13 | BADGE, BUILTIN_THEME, CLOCK_LOW, CLOCK_RUNNING, DEFAULT_SCHEME, LOW_TIME_MS, SCHEMES, |
| @@ -16,6 +16,24 @@ from .diagram import ( # noqa: E402,F401 – Namen bleiben hier importierbar | ||
| 16 | 16 | from . import diagram # noqa: E402 |
| 17 | 17 | |
| 18 | 18 | ANIM_SECONDS = 0.22 |
| 19 | +# Pfeilfarben beim Zeichnen wie bei Lichess: ohne Taste grün, Umschalt (oder Strg) rot, Alt blau, beides gelb | |
| 20 | +BRUSHES = ("green", "red", "blue", "yellow") | |
| 21 | + | |
| 22 | + | |
| 23 | +def brush(state): | |
| 24 | + """Pfeilfarbe zu den gedrückten Tasten (Gdk.ModifierType).""" | |
| 25 | + red = bool(state & (Gdk.ModifierType.SHIFT_MASK | Gdk.ModifierType.CONTROL_MASK)) | |
| 26 | + blue = bool(state & (Gdk.ModifierType.ALT_MASK | Gdk.ModifierType.META_MASK)) | |
| 27 | + return BRUSHES[red + 2 * blue] | |
| 28 | + | |
| 29 | + | |
| 30 | +def toggle_shape(shapes, shape): | |
| 31 | + """Gleicher Pfeil (oder Kreis) in gleicher Farbe: weg; in anderer Farbe: umgefärbt; sonst dazu.""" | |
| 32 | + tail, head, color = shape | |
| 33 | + kept = tuple(s for s in shapes if (s[0], s[1]) != (tail, head)) | |
| 34 | + if any(s == shape for s in shapes): | |
| 35 | + return kept | |
| 36 | + return kept + (shape,) | |
| 19 | 37 | |
| 20 | 38 | |
| 21 | 39 | def scheme_colors(name): |
| @@ -40,6 +58,11 @@ class BoardView(Gtk.DrawingArea, BoardPa | ||
| 40 | 58 | self.on_edit_move = None # callback(von, nach oder None = vom Brett) |
| 41 | 59 | self.on_edit_erase = None # callback(feld) |
| 42 | 60 | self._edit_start = None |
| 61 | + # Pfeile und Kreise zeichnen (rechte Maustaste, wie bei Lichess). ``drawn_key``: wozu die | |
| 62 | + # Zeichnung gehört (Knoten des Analysebretts, sonst die Stellung); ändert er sich, ist sie weg. | |
| 63 | + self.on_shapes = None # callback(key, shapes) – Zeichnung geändert | |
| 64 | + self.drawn_key = None | |
| 65 | + self._sketch_start = None | |
| 43 | 66 | |
| 44 | 67 | self.set_draw_func(lambda _area, cr, _w, _h: self.paint(cr)) |
| 45 | 68 | drag = Gtk.GestureDrag() |
| @@ -47,9 +70,11 @@ class BoardView(Gtk.DrawingArea, BoardPa | ||
| 47 | 70 | drag.connect("drag-update", self._on_update) |
| 48 | 71 | drag.connect("drag-end", self._on_end) |
| 49 | 72 | self.add_controller(drag) |
| 50 | - erase = Gtk.GestureClick(button=3) | |
| 51 | - erase.connect("pressed", self._on_erase) | |
| 52 | - self.add_controller(erase) | |
| 73 | + sketch = Gtk.GestureDrag(button=Gdk.BUTTON_SECONDARY) | |
| 74 | + sketch.connect("drag-begin", self._on_sketch_begin) | |
| 75 | + sketch.connect("drag-update", self._on_sketch_update) | |
| 76 | + sketch.connect("drag-end", self._on_sketch_end) | |
| 77 | + self.add_controller(sketch) | |
| 53 | 78 | |
| 54 | 79 | def set_scheme(self, scheme): |
| 55 | 80 | self.scheme = scheme |
| @@ -109,12 +134,56 @@ class BoardView(Gtk.DrawingArea, BoardPa | ||
| 109 | 134 | |
| 110 | 135 | # --- Eingabe ----------------------------------------------------------- |
| 111 | 136 | |
| 112 | - def _on_erase(self, _gesture, _n, x, y): | |
| 113 | - s = self._square_at(x, y) | |
| 114 | - if self.on_edit_erase is not None and s is not None: | |
| 115 | - self.on_edit_erase(s) | |
| 137 | + def show_drawn(self, key, shapes=None): | |
| 138 | + """Zeichnung für ``key`` zeigen: ``shapes`` (gespeichert) oder, ohne, die bisherige behalten, | |
| 139 | + solange ``key`` gleich bleibt – sonst ist sie weg (z. B. nach einem Zug in der Partie).""" | |
| 140 | + if shapes is not None: | |
| 141 | + self.drawn = tuple(shapes) | |
| 142 | + elif key != self.drawn_key: | |
| 143 | + self.drawn = () | |
| 144 | + self.drawn_key = key | |
| 145 | + | |
| 146 | + def _set_drawn(self, shapes): | |
| 147 | + self.drawn = shapes | |
| 148 | + self.queue_draw() | |
| 149 | + if self.on_shapes: | |
| 150 | + self.on_shapes(self.drawn_key, shapes) | |
| 151 | + | |
| 152 | + def _on_sketch_begin(self, gesture, x, y): | |
| 153 | + self._sketch_start = self._square_at(x, y) | |
| 154 | + if self._sketch_start is None or self.on_edit_erase is not None: | |
| 155 | + return | |
| 156 | + self.selected = None | |
| 157 | + self.drawing = (self._sketch_start, self._sketch_start, brush(gesture.get_current_event_state())) | |
| 158 | + self.queue_draw() | |
| 159 | + | |
| 160 | + def _on_sketch_update(self, gesture, dx, dy): | |
| 161 | + if self.drawing is None: | |
| 162 | + return | |
| 163 | + _, sx, sy = gesture.get_start_point() | |
| 164 | + target = self._square_at(sx + dx, sy + dy) | |
| 165 | + if target is not None and target != self.drawing[1]: | |
| 166 | + self.drawing = (self.drawing[0], target, self.drawing[2]) | |
| 167 | + self.queue_draw() | |
| 168 | + | |
| 169 | + def _on_sketch_end(self, gesture, dx, dy): | |
| 170 | + start, self._sketch_start = self._sketch_start, None | |
| 171 | + if self.on_edit_erase is not None: # Stellung aufbauen: Rechtsklick nimmt die Figur weg | |
| 172 | + if start is not None: | |
| 173 | + self.on_edit_erase(start) | |
| 174 | + return | |
| 175 | + shape, self.drawing = self.drawing, None | |
| 176 | + if shape is None: | |
| 177 | + return | |
| 178 | + _, sx, sy = gesture.get_start_point() | |
| 179 | + if self._square_at(sx + dx, sy + dy) is None: # neben dem Brett losgelassen: verworfen | |
| 180 | + self.queue_draw() | |
| 181 | + return | |
| 182 | + self._set_drawn(toggle_shape(self.drawn, shape)) | |
| 116 | 183 | |
| 117 | 184 | def _on_begin(self, gesture, x, y): |
| 185 | + if self.drawn and self.on_edit_click is None: # Linksklick aufs Brett räumt die Zeichnung weg | |
| 186 | + self._set_drawn(()) | |
| 118 | 187 | if self.on_edit_click is not None: |
| 119 | 188 | s = self._edit_start = self._square_at(x, y) |
| 120 | 189 | if s is not None and self.board.piece_at(s): |
modified schach/diagram.py +4 −1
| @@ -192,6 +192,8 @@ class BoardPainter: | ||
| 192 | 192 | self.branches = () # Gabelung in der Vorführung: ((von, nach, rgb, Schild), …), anklickbar |
| 193 | 193 | self._branch_hits = [] # (links, oben, rechts, unten, Index) der gezeichneten Schilder |
| 194 | 194 | self.shapes = () # Pfeile und Felder aus Kurskommentaren: ((von, nach, Farbe), …) |
| 195 | + self.drawn = () # selbst gezeichnete Pfeile und Kreise (rechte Maustaste), gleiches Format | |
| 196 | + self.drawing = None # Pfeil, der gerade gezogen wird (von, nach, Farbe) | |
| 195 | 197 | self.preview = None # Zug, der halb durchsichtig gezeigt wird (Explorer: Maus über einer Zeile) |
| 196 | 198 | self.hint_preview = None # Hinweiszug, sobald beide Felder gezeigt werden: ebenso halb durchsichtig |
| 197 | 199 | self.threat_preview = None # Drohzug des Gegners, sobald beide Felder gezeigt werden |
| @@ -350,7 +352,8 @@ class BoardPainter: | ||
| 350 | 352 | hops = [s for s in (self.spotlight_from, self.spotlight_via) if s is not None] + [self.spotlight] |
| 351 | 353 | for a, b in zip(hops, hops[1:]): |
| 352 | 354 | self._draw_arrow(cr, a, b, sq, SPOTLIGHT, outline=True) |
| 353 | - for tail, head, color in self.shapes: | |
| 355 | + sketch = (self.drawing,) if self.drawing else () | |
| 356 | + for tail, head, color in (*self.shapes, *self.drawn, *sketch): | |
| 354 | 357 | rgb = SHAPE_COLORS.get(color, SHAPE_COLORS["green"]) |
| 355 | 358 | if tail == head: |
| 356 | 359 | self._ring(cr, tail, sq, rgb) |
modified schach/movelist.py +154 −34
| @@ -1,10 +1,14 @@ | ||
| 1 | -"""Zugliste für die Analyse: ein Halbzug pro Zeile, dahinter ein Weiß/Schwarz-Balken.""" | |
| 1 | +"""Zugliste für die Analyse: ein Halbzug pro Zeile, dahinter ein Weiß/Schwarz-Balken. | |
| 2 | + | |
| 3 | +Nebenvarianten vom Analysebrett hängen als Baum darunter: aufklappbar, jeder Zug anklickbar. | |
| 4 | +Tastatur wie bei Bäumen üblich: ↑/↓ durch die sichtbaren Zeilen (zugeklappt: nur die Partie), | |
| 5 | +→ klappt auf bzw. geht in die erste Variante, ← klappt zu bzw. zurück zur Zeile darüber.""" | |
| 2 | 6 | |
| 3 | 7 | import chess |
| 4 | 8 | import gi |
| 5 | 9 | |
| 6 | 10 | gi.require_version("Gtk", "4.0") |
| 7 | -from gi.repository import Gtk # noqa: E402 | |
| 11 | +from gi.repository import Gdk, GLib, Gtk # noqa: E402 | |
| 8 | 12 | |
| 9 | 13 | from . import notation # noqa: E402 |
| 10 | 14 | from .board import BADGE # noqa: E402 |
| @@ -28,14 +32,15 @@ CSS = b""" | ||
| 28 | 32 | font-feature-settings: "tnum"; |
| 29 | 33 | } |
| 30 | 34 | .move-row-buttons button { min-height: 22px; min-width: 22px; padding: 1px; } |
| 31 | -.variation-row { padding: 2px 8px 2px 30px; } | |
| 35 | +.variation-row { padding: 2px 8px 2px 0; } | |
| 32 | 36 | .variation-pill { |
| 33 | 37 | background: alpha(@accent_bg_color, 0.12); |
| 34 | 38 | border-radius: 6px; |
| 35 | 39 | padding: 1px 7px; |
| 36 | 40 | font-size: smaller; |
| 37 | 41 | } |
| 38 | -.variation-pill.selected-move { background: @accent_bg_color; color: @accent_fg_color; } | |
| 42 | +.variation-pill link, .variation-pill link:visited { color: inherit; text-decoration-line: none; } | |
| 43 | +.tree-toggle { min-height: 20px; min-width: 20px; padding: 0; } | |
| 39 | 44 | """ |
| 40 | 45 | |
| 41 | 46 | |
| @@ -56,7 +61,7 @@ class MoveList(Gtk.Box): | ||
| 56 | 61 | self.on_copy = None # callback(halbzug) |
| 57 | 62 | self.on_continue = None # callback(halbzug) |
| 58 | 63 | self.on_why_not = None # callback(halbzug): „Warum nicht?“ bei ?!, ? und ?? |
| 59 | - self.on_variation = None # callback(nummer): Nebenvariante vom Analysebrett angeklickt | |
| 64 | + self.on_variation = None # callback(nummer): Zug einer Nebenvariante vom Analysebrett angeklickt | |
| 60 | 65 | |
| 61 | 66 | header = Gtk.Box(spacing=6, margin_start=12, margin_end=6, margin_top=6, margin_bottom=6) |
| 62 | 67 | header.append(Gtk.Box(hexpand=True)) # Überschrift steht schon im Umschalter „Partie | Buch | Lichess“ |
| @@ -69,6 +74,9 @@ class MoveList(Gtk.Box): | ||
| 69 | 74 | |
| 70 | 75 | self.listbox = Gtk.ListBox(selection_mode=Gtk.SelectionMode.SINGLE, css_classes=["movelist"]) |
| 71 | 76 | self.listbox.connect("row-selected", self._on_row_selected) |
| 77 | + keys = Gtk.EventControllerKey() # ← und → klappen auf und zu; sonst weiter ans Fenster (Brett) | |
| 78 | + keys.connect("key-pressed", self._on_key) | |
| 79 | + self.listbox.add_controller(keys) | |
| 72 | 80 | self.scroller = Gtk.ScrolledWindow(vexpand=True, hscrollbar_policy=Gtk.PolicyType.NEVER) |
| 73 | 81 | self.scroller.set_child(self.listbox) |
| 74 | 82 | self.append(self.scroller) |
| @@ -76,8 +84,12 @@ class MoveList(Gtk.Box): | ||
| 76 | 84 | self.notation = "de" # Schreibweise der Züge, siehe notation.py |
| 77 | 85 | self._rows = [] |
| 78 | 86 | self._variation_rows = [] |
| 87 | + self._toggles = {} # Schlüssel -> Knopf zum Auf- und Zuklappen | |
| 88 | + self._open = set() # aufgeklappte Schlüssel: ("zug", Halbzug) bzw. ("zweig", erste Nummer) | |
| 89 | + self._token_rows = {} # Nummer eines Variantenzugs -> Zeile, in der er steht | |
| 79 | 90 | self._selected = None |
| 80 | 91 | self._selected_variation = None |
| 92 | + self._link_clicked = 0 # Zeitpunkt des letzten Klicks auf einen Zug in einer Variante | |
| 81 | 93 | self._signature = None |
| 82 | 94 | self._updating = False |
| 83 | 95 | |
| @@ -86,13 +98,14 @@ class MoveList(Gtk.Box): | ||
| 86 | 98 | def update(self, board, evals, symbols, selected, variations=(), selected_variation=None, times=None): |
| 87 | 99 | """Baut die Liste neu, wenn sich Züge, Bewertungen, Zeichen, Zugzeiten oder Varianten geändert haben. |
| 88 | 100 | |
| 89 | - ``variations``: [(Halbzug, Text)] – Nebenvarianten vom Analysebrett, eingerückt unter dem Zug, | |
| 90 | - nach dem sie abzweigen (-1: vor dem ersten Zug). ``selected_variation``: Nummer darin. | |
| 101 | + ``variations``: {Halbzug: Zweige} – Nebenvarianten vom Analysebrett, aufklappbar unter dem Zug, | |
| 102 | + nach dem sie abzweigen (-1: vor dem ersten Zug, immer offen). Zweig = ((Nummer, Zugnummer, Zug, | |
| 103 | + Unterzweige), …), siehe ``training._list_variations``. ``selected_variation``: Nummer darin. | |
| 91 | 104 | ``times``: {Halbzug: ms} – Bedenkzeit, rechts in der Zeile.""" |
| 92 | 105 | moves = list(board.move_stack) |
| 93 | 106 | times = times or {} |
| 94 | 107 | signature = (tuple(moves), tuple(sorted(evals.items())), tuple(sorted(symbols.items())), self.notation, |
| 95 | - tuple(variations), tuple(sorted(times.items()))) | |
| 108 | + tuple(sorted(dict(variations).items())), tuple(sorted(times.items()))) | |
| 96 | 109 | if signature != self._signature: |
| 97 | 110 | self._signature = signature |
| 98 | 111 | self._rebuild(board, moves, evals, symbols, variations, times) |
| @@ -100,25 +113,32 @@ class MoveList(Gtk.Box): | ||
| 100 | 113 | |
| 101 | 114 | def _rebuild(self, board, moves, evals, symbols, variations=(), times=None): |
| 102 | 115 | times = times or {} |
| 116 | + after = dict(variations) | |
| 103 | 117 | self._updating = True |
| 104 | 118 | self.listbox.remove_all() |
| 105 | 119 | self._rows = [] |
| 106 | 120 | self._variation_rows = [] |
| 107 | - after = {} | |
| 108 | - for index, (ply, text) in enumerate(variations): | |
| 109 | - after.setdefault(ply, []).append((index, text)) | |
| 110 | - ordered = [self._make_variation_row(i, t) for i, t in after.get(-1, [])] | |
| 121 | + self._toggles = {} | |
| 122 | + self._token_rows = {} | |
| 123 | + ordered = [] | |
| 124 | + for branch in after.get(-1, ()): | |
| 125 | + ordered += self._branch_rows(branch, (), None, 0) | |
| 111 | 126 | replay = board.root() |
| 112 | 127 | for ply, move in enumerate(moves): |
| 113 | 128 | number = replay.fullmove_number |
| 114 | 129 | prefix = f"{number}." if replay.turn == chess.WHITE else f"{number}…" |
| 115 | 130 | san = notation.move(replay, move, self.notation) |
| 116 | 131 | replay.push(move) |
| 117 | - self._rows.append(self._make_row(ply, prefix, san, symbols.get(ply), evals.get(ply), times.get(ply))) | |
| 118 | - ordered.append(self._rows[-1][0]) | |
| 119 | - ordered += [self._make_variation_row(i, t) for i, t in after.get(ply, [])] | |
| 132 | + key = ("zug", ply) if after.get(ply) else None | |
| 133 | + row, buttons = self._make_row(ply, prefix, san, symbols.get(ply), evals.get(ply), times.get(ply), | |
| 134 | + key, indent=bool(after)) | |
| 135 | + self._rows.append((row, buttons)) | |
| 136 | + ordered.append(row) | |
| 137 | + for branch in after.get(ply, ()): | |
| 138 | + ordered += self._branch_rows(branch, (key,), row, 1) | |
| 120 | 139 | for row in ordered: |
| 121 | 140 | self.listbox.append(row) |
| 141 | + self._show_open() | |
| 122 | 142 | self._updating = False |
| 123 | 143 | if self._rows: |
| 124 | 144 | # nach dem Layout ans Ende scrollen |
| @@ -129,7 +149,14 @@ class MoveList(Gtk.Box): | ||
| 129 | 149 | if self.listbox.get_selected_row() is None: |
| 130 | 150 | adj.set_value(adj.get_upper() - adj.get_page_size()) |
| 131 | 151 | |
| 132 | - def _make_row(self, ply, prefix, san, symbol, white_pct, ms=None): | |
| 152 | + def _toggle_button(self, key, tooltip): | |
| 153 | + button = Gtk.Button(icon_name="pan-end-symbolic", css_classes=["flat", "circular", "tree-toggle"], | |
| 154 | + valign=Gtk.Align.CENTER, tooltip_text=tooltip) | |
| 155 | + button.connect("clicked", lambda *_: self._set_open(key, key not in self._open)) | |
| 156 | + self._toggles[key] = button | |
| 157 | + return button | |
| 158 | + | |
| 159 | + def _make_row(self, ply, prefix, san, symbol, white_pct, ms=None, key=None, indent=False): | |
| 133 | 160 | bar = Gtk.DrawingArea(content_height=30, hexpand=True) |
| 134 | 161 | bar.set_draw_func(self._draw_bar, white_pct) |
| 135 | 162 | |
| @@ -156,7 +183,11 @@ class MoveList(Gtk.Box): | ||
| 156 | 183 | buttons.append(copy_btn) |
| 157 | 184 | buttons.append(play_btn) |
| 158 | 185 | |
| 159 | - content = Gtk.Box(spacing=6, margin_start=8, margin_end=8) | |
| 186 | + content = Gtk.Box(spacing=6, margin_start=2 if indent else 8, margin_end=8) | |
| 187 | + if key is not None: | |
| 188 | + content.append(self._toggle_button(key, "Nebenvarianten zeigen")) | |
| 189 | + elif indent: # Platz wie der Knopf daneben, damit die Züge untereinander stehen | |
| 190 | + content.append(Gtk.Box(width_request=20)) | |
| 160 | 191 | content.append(pill) |
| 161 | 192 | content.append(Gtk.Box(hexpand=True)) |
| 162 | 193 | if ms is not None: |
| @@ -169,6 +200,7 @@ class MoveList(Gtk.Box): | ||
| 169 | 200 | row = Gtk.ListBoxRow(child=overlay) |
| 170 | 201 | row.ply = ply |
| 171 | 202 | row.pill = pill |
| 203 | + row.key, row.needs, row.parent_row = key, (), None | |
| 172 | 204 | # Knöpfe schon beim Darüberfahren zeigen |
| 173 | 205 | hover = Gtk.EventControllerMotion() |
| 174 | 206 | hover.connect("enter", lambda *_: buttons.set_visible(True)) |
| @@ -176,18 +208,92 @@ class MoveList(Gtk.Box): | ||
| 176 | 208 | row.add_controller(hover) |
| 177 | 209 | return row, buttons |
| 178 | 210 | |
| 179 | - def _make_variation_row(self, index, text): | |
| 180 | - """Eingerückte Zeile für eine Nebenvariante: „↳ 4. a3 Sxe4 5. d4 …“.""" | |
| 181 | - pill = Gtk.Label(use_markup=True, label=f"↳ {text}", css_classes=["variation-pill"], | |
| 182 | - ellipsize=3, xalign=0) # 3 = Pango.EllipsizeMode.END | |
| 183 | - content = Gtk.Box(css_classes=["variation-row"]) | |
| 211 | + def _branch_rows(self, branch, needs, parent_row, depth): | |
| 212 | + """Zeile für einen Zweig, darunter (eine Stufe tiefer) die Alternativen innerhalb des Zweigs. | |
| 213 | + ``needs``: Schlüssel, die offen sein müssen, damit die Zeile zu sehen ist.""" | |
| 214 | + subs = [sub for move in branch for sub in move[3]] | |
| 215 | + key = ("zweig", branch[0][0]) if subs else None | |
| 216 | + pill = Gtk.Label(use_markup=True, css_classes=["variation-pill"], wrap=True, xalign=0, hexpand=False, | |
| 217 | + halign=Gtk.Align.START) | |
| 218 | + pill.connect("activate-link", self._on_link) | |
| 219 | + content = Gtk.Box(spacing=4, css_classes=["variation-row"], margin_start=2 + 18 * depth) | |
| 220 | + content.append(self._toggle_button(key, "Untervarianten zeigen") if key else Gtk.Box(width_request=20)) | |
| 184 | 221 | content.append(pill) |
| 185 | 222 | row = Gtk.ListBoxRow(child=content, tooltip_text="Nebenvariante vom Analysebrett") |
| 186 | 223 | row.ply = None |
| 187 | - row.variation = index | |
| 224 | + row.variation = branch[0][0] | |
| 225 | + row.branch = branch | |
| 188 | 226 | row.pill = pill |
| 227 | + row.key, row.needs, row.parent_row = key, needs, parent_row | |
| 228 | + for move in branch: | |
| 229 | + self._token_rows[move[0]] = row | |
| 189 | 230 | self._variation_rows.append(row) |
| 190 | - return row | |
| 231 | + self._branch_markup(row, None) | |
| 232 | + rows = [row] | |
| 233 | + for sub in subs: | |
| 234 | + rows += self._branch_rows(sub, needs + (key,), row, depth + 1) | |
| 235 | + return rows | |
| 236 | + | |
| 237 | + @staticmethod | |
| 238 | + def _branch_markup(row, selected): | |
| 239 | + words = [] | |
| 240 | + for token, prefix, san, _subs in row.branch: | |
| 241 | + word = f"{prefix} {san}" if prefix else san | |
| 242 | + if token == selected: | |
| 243 | + word = f"<span background='#3584e4' foreground='white'><b> {word} </b></span>" | |
| 244 | + words.append(f"<a href='{token}' title=''>{word}</a>") | |
| 245 | + row.pill.set_markup("↳ " + " ".join(words)) | |
| 246 | + row.marked = selected | |
| 247 | + | |
| 248 | + def _on_link(self, _label, uri): | |
| 249 | + self._link_clicked = GLib.get_monotonic_time() # Zeile wird beim selben Klick gewählt: nicht ihr erster Zug | |
| 250 | + if self.on_variation and uri.isdigit(): | |
| 251 | + self.on_variation(int(uri)) | |
| 252 | + return True | |
| 253 | + | |
| 254 | + # --- Auf- und Zuklappen ------------------------------------------------ | |
| 255 | + | |
| 256 | + def _set_open(self, key, is_open): | |
| 257 | + if is_open: | |
| 258 | + self._open.add(key) | |
| 259 | + else: | |
| 260 | + self._open.discard(key) | |
| 261 | + self._show_open() | |
| 262 | + | |
| 263 | + def _show_open(self): | |
| 264 | + for key, button in self._toggles.items(): | |
| 265 | + button.set_icon_name("pan-down-symbolic" if key in self._open else "pan-end-symbolic") | |
| 266 | + for row in self._variation_rows: | |
| 267 | + row.set_visible(all(k is None or k in self._open for k in row.needs)) | |
| 268 | + | |
| 269 | + def _children(self, row): | |
| 270 | + return [r for r in self._variation_rows if r.parent_row is row] | |
| 271 | + | |
| 272 | + def _on_key(self, _controller, keyval, _keycode, state): | |
| 273 | + if state & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.ALT_MASK | Gdk.ModifierType.SHIFT_MASK): | |
| 274 | + return False | |
| 275 | + row = self.listbox.get_selected_row() | |
| 276 | + if row is None or keyval not in (Gdk.KEY_Right, Gdk.KEY_Left): | |
| 277 | + return False | |
| 278 | + if keyval == Gdk.KEY_Right: | |
| 279 | + if row.key is None: | |
| 280 | + return False # nichts aufzuklappen: das Brett geht einen Zug weiter | |
| 281 | + if row.key not in self._open: | |
| 282 | + self._set_open(row.key, True) | |
| 283 | + else: | |
| 284 | + children = self._children(row) | |
| 285 | + if children: | |
| 286 | + self.listbox.select_row(children[0]) | |
| 287 | + children[0].grab_focus() | |
| 288 | + return True | |
| 289 | + if row.key is not None and row.key in self._open: | |
| 290 | + self._set_open(row.key, False) | |
| 291 | + return True | |
| 292 | + if row.parent_row is not None: | |
| 293 | + self.listbox.select_row(row.parent_row) | |
| 294 | + row.parent_row.grab_focus() | |
| 295 | + return True | |
| 296 | + return False | |
| 191 | 297 | |
| 192 | 298 | @staticmethod |
| 193 | 299 | def _draw_bar(area, cr, width, height, white_pct): |
| @@ -211,20 +317,27 @@ class MoveList(Gtk.Box): | ||
| 211 | 317 | |
| 212 | 318 | def _apply_selection(self, selected, selected_variation=None): |
| 213 | 319 | self._updating = True |
| 214 | - if selected_variation is not None and 0 <= selected_variation < len(self._variation_rows): | |
| 215 | - self.listbox.select_row(self._variation_rows[selected_variation]) | |
| 320 | + holder = self._token_rows.get(selected_variation) | |
| 321 | + if holder is not None and selected_variation != self._selected_variation: | |
| 322 | + step = holder # gewählter Zug in einer Variante: alles darüber aufklappen | |
| 323 | + while step is not None: | |
| 324 | + self._open.update(k for k in step.needs if k is not None) | |
| 325 | + step = step.parent_row | |
| 326 | + self._show_open() | |
| 327 | + self._selected_variation = selected_variation if holder is not None else None | |
| 328 | + if holder is not None: | |
| 329 | + self.listbox.select_row(holder) | |
| 216 | 330 | elif selected is None or selected < 0 or selected >= len(self._rows): |
| 217 | 331 | self.listbox.unselect_all() |
| 218 | 332 | else: |
| 219 | 333 | self.listbox.select_row(self._rows[selected][0]) |
| 220 | 334 | self._updating = False |
| 221 | 335 | self._show_buttons(None if selected_variation is not None else selected) |
| 222 | - for index, row in enumerate(self._variation_rows): | |
| 223 | - if index == selected_variation: | |
| 224 | - row.pill.add_css_class("selected-move") | |
| 225 | - else: | |
| 226 | - row.pill.remove_css_class("selected-move") | |
| 227 | - self.live_btn.set_sensitive(selected is not None or selected_variation is not None) | |
| 336 | + for row in self._variation_rows: | |
| 337 | + marked = self._selected_variation if row is holder else None | |
| 338 | + if getattr(row, "marked", None) != marked: | |
| 339 | + self._branch_markup(row, marked) | |
| 340 | + self.live_btn.set_sensitive(selected is not None or holder is not None) | |
| 228 | 341 | |
| 229 | 342 | def _show_buttons(self, selected): |
| 230 | 343 | self._selected = selected |
| @@ -239,11 +352,18 @@ class MoveList(Gtk.Box): | ||
| 239 | 352 | if self._updating: |
| 240 | 353 | return |
| 241 | 354 | if row is not None and getattr(row, "variation", None) is not None: |
| 242 | - if self.on_variation: | |
| 243 | - self.on_variation(row.variation) | |
| 355 | + GLib.idle_add(self._row_chosen, row) | |
| 244 | 356 | return |
| 245 | 357 | self._select(row.ply if row is not None else None) |
| 246 | 358 | |
| 359 | + def _row_chosen(self, row): | |
| 360 | + """Variantenzeile gewählt (Tastatur, Klick neben die Züge): ihr erster Zug.""" | |
| 361 | + recent = GLib.get_monotonic_time() - self._link_clicked < 500_000 # gerade einen Zug angeklickt | |
| 362 | + if not recent and self.on_variation and self._token_rows.get(self._selected_variation) is not row: | |
| 363 | + if row.get_parent() is self.listbox: | |
| 364 | + self.on_variation(row.variation) | |
| 365 | + return GLib.SOURCE_REMOVE | |
| 366 | + | |
| 247 | 367 | def _select(self, ply): |
| 248 | 368 | # Die letzte Zeile ist die aktuelle Stellung – dort braucht es keinen Ansichtsmodus |
| 249 | 369 | if self.on_select: |
modified schach/savegames.py +24 −5
| @@ -47,6 +47,8 @@ class GameState: | ||
| 47 | 47 | # Nebenvariante mit [%zurueck kurs] bzw. [%zurueck selbst] im Kommentar des ersten Zugs. |
| 48 | 48 | takebacks: dict = dataclasses.field(default_factory=dict) |
| 49 | 49 | times: dict = dataclasses.field(default_factory=dict) # Halbzug-Index -> Bedenkzeit für diesen Zug (ms) |
| 50 | + # Selbst gezeichnete Pfeile und Kreise an der Partie: {Halbzug: ((von, nach, Farbe), …)}, -1 = Anfang | |
| 51 | + shapes: dict = dataclasses.field(default_factory=dict) | |
| 50 | 52 | |
| 51 | 53 | def truncated(self, ply): |
| 52 | 54 | """Kopie bis einschließlich Halbzug ``ply`` (für Abzweigungen).""" |
| @@ -63,6 +65,7 @@ class GameState: | ||
| 63 | 65 | variations={p: v for p, v in self.variations.items() if p < n}, |
| 64 | 66 | takebacks={p: v for p, v in self.takebacks.items() if p < n}, |
| 65 | 67 | times={p: t for p, t in self.times.items() if p < n}, |
| 68 | + shapes={p: s for p, s in self.shapes.items() if p < n}, | |
| 66 | 69 | ) |
| 67 | 70 | |
| 68 | 71 | |
| @@ -105,6 +108,9 @@ def to_pgn(state): | ||
| 105 | 108 | node.comment = f"[%wp {state.evals[ply]:.1f}]" |
| 106 | 109 | if ply in state.times: |
| 107 | 110 | node.set_emt(round(state.times[ply] / 1000, 1)) # [%emt 0:00:03.5] |
| 111 | + for ply, shapes in state.shapes.items(): | |
| 112 | + if ply in played and shapes: | |
| 113 | + _set_shapes(played[ply], shapes) | |
| 108 | 114 | for table in (state.variations, state.takebacks): |
| 109 | 115 | for ply, variations in sorted(table.items()): |
| 110 | 116 | if ply in played: |
| @@ -113,9 +119,18 @@ def to_pgn(state): | ||
| 113 | 119 | return str(game) + "\n" |
| 114 | 120 | |
| 115 | 121 | |
| 116 | -# Pfeilfarben des Analysebretts als PGN-Farben ([%cal Bd2d4]): eigene Züge blau, Gegner gelb | |
| 117 | -PGN_COLOR = {"hint": "blue", "threat": "yellow", "red": "red", "green": "green"} | |
| 118 | -SHAPE_COLOR = {pgn: shape for shape, pgn in PGN_COLOR.items()} | |
| 122 | +# Pfeilfarben als PGN-Farben ([%cal Bd2d4]); Spur der Vorführung: eigene Züge blau, Gegner gelb | |
| 123 | +SHAPE_COLOR = {"green": "green", "red": "red", "blue": "blue", "yellow": "yellow"} | |
| 124 | +PGN_COLOR = {"hint": "blue", "threat": "yellow", **SHAPE_COLOR} | |
| 125 | + | |
| 126 | + | |
| 127 | +def read_shapes(node): | |
| 128 | + """Pfeile und Kreise ([%cal]/[%csl]) eines PGN-Knotens: ((von, nach, Farbe), …).""" | |
| 129 | + return tuple((a.tail, a.head, SHAPE_COLOR.get(a.color, "green")) for a in node.arrows()) | |
| 130 | + | |
| 131 | + | |
| 132 | +def _set_shapes(node, shapes): | |
| 133 | + node.set_arrows(chess.svg.Arrow(tail, head, color=PGN_COLOR.get(color, "green")) for tail, head, color in shapes) | |
| 119 | 134 | COMMAND_RE = re.compile(r"\[%(?!zurueck )[^\]]*\]") # [%zurueck …] bleibt: markiert Zurückgenommenes |
| 120 | 135 | TAKEBACK_RE = re.compile(r"\[%zurueck (\w+)\]") |
| 121 | 136 | TAKEBACK_WORDS = {"kurs": "Vom Kurs zurückgenommen.", "selbst": "Zurückgenommen."} |
| @@ -165,7 +180,7 @@ def read_variations(game): | ||
| 165 | 180 | """Nebenvarianten einer PGN-Partie im Format von GameState.variations (Kommentare ohne [%…], |
| 166 | 181 | außer der Marke [%zurueck …] für Zurückgenommenes, siehe ``split_takebacks``).""" |
| 167 | 182 | def pack(node): |
| 168 | - shapes = tuple((a.tail, a.head, SHAPE_COLOR.get(a.color, "green")) for a in node.arrows()) | |
| 183 | + shapes = read_shapes(node) | |
| 169 | 184 | comment = " ".join(COMMAND_RE.sub(" ", node.comment or "").split()) |
| 170 | 185 | return (node.move, comment, shapes, [pack(child) for child in node.variations]) |
| 171 | 186 | found, node, ply = {}, game, -1 |
| @@ -181,7 +196,7 @@ def _add_variation(parent, variation): | ||
| 181 | 196 | if move not in parent.board().legal_moves: |
| 182 | 197 | return |
| 183 | 198 | node = parent.add_variation(move, comment=comment or "") |
| 184 | - node.set_arrows(chess.svg.Arrow(tail, head, color=PGN_COLOR.get(color, "green")) for tail, head, color in shapes) | |
| 199 | + _set_shapes(node, shapes) | |
| 185 | 200 | for child in children: |
| 186 | 201 | _add_variation(node, child) |
| 187 | 202 | |
| @@ -221,8 +236,11 @@ def from_pgn(text, path=""): | ||
| 221 | 236 | board = game.board() |
| 222 | 237 | symbols, evals = {}, {} |
| 223 | 238 | times = read_times(game) |
| 239 | + shapes = {-1: read_shapes(game)} if game.arrows() else {} | |
| 224 | 240 | for ply, node in enumerate(game.mainline()): |
| 225 | 241 | board.push(node.move) |
| 242 | + if node.arrows(): | |
| 243 | + shapes[ply] = read_shapes(node) | |
| 226 | 244 | for nag in node.nags: |
| 227 | 245 | if nag in SYMBOL_FOR_NAG: |
| 228 | 246 | symbols[ply] = SYMBOL_FOR_NAG[nag] |
| @@ -245,6 +263,7 @@ def from_pgn(text, path=""): | ||
| 245 | 263 | variations=variations, |
| 246 | 264 | takebacks=takebacks, |
| 247 | 265 | times=times, |
| 266 | + shapes=shapes, | |
| 248 | 267 | ) |
| 249 | 268 | |
| 250 | 269 | |
modified schach/training.py +55 −24
| @@ -558,12 +558,16 @@ class OpeningMixin: | ||
| 558 | 558 | self.game_tree = tree |
| 559 | 559 | return tree |
| 560 | 560 | |
| 561 | - def _game_tree_restore(self, variations): | |
| 562 | - """Neuer Partiebaum mit den Nebenvarianten aus einem Spielstand (GameState.variations).""" | |
| 561 | + def _game_tree_restore(self, variations, shapes=None): | |
| 562 | + """Neuer Partiebaum mit den Nebenvarianten (GameState.variations) und den gezeichneten Pfeilen | |
| 563 | + an der Partie (GameState.shapes) aus einem Spielstand.""" | |
| 563 | 564 | self.game_tree = None |
| 564 | - if not variations: | |
| 565 | + if not variations and not shapes: | |
| 565 | 566 | return |
| 566 | 567 | tree = self._game_tree() |
| 568 | + for ply, found in (shapes or {}).items(): | |
| 569 | + if -1 <= ply < len(self.board.move_stack) and found: | |
| 570 | + tree.info(self._game_node(ply))["shapes"] = tuple(found) | |
| 567 | 571 | |
| 568 | 572 | def unpack(parent, pack): |
| 569 | 573 | move, comment, shapes, children = pack |
| @@ -617,6 +621,24 @@ class OpeningMixin: | ||
| 617 | 621 | if self._game_tree() is not self.demo["tree"] or not _attached(self.demo["node"], self.game_tree): |
| 618 | 622 | self.demo = None # der gezeigte Zug wurde zurückgenommen |
| 619 | 623 | |
| 624 | + def _game_shapes(self): | |
| 625 | + """Gezeichnete Pfeile an den Zügen der Partie: {Halbzug: Pfeile} (-1: Ausgangsstellung).""" | |
| 626 | + tree = self.game_tree | |
| 627 | + if tree is None or not self.board.move_stack or self._game_tree() is not tree: | |
| 628 | + return {} | |
| 629 | + found = {} | |
| 630 | + for ply in range(-1, len(self.board.move_stack)): | |
| 631 | + shapes = tree.shapes(self._game_node(ply)) | |
| 632 | + if shapes: | |
| 633 | + found[ply] = tuple(shapes) | |
| 634 | + return found | |
| 635 | + | |
| 636 | + def _drawn(self, key, shapes): | |
| 637 | + """Auf dem Brett gezeichnet: auf dem Analysebrett zur Partie gehört es zum Zug (Spielstand).""" | |
| 638 | + if self.demo is not None and self.demo.get("game") and key is self.demo["node"]: | |
| 639 | + self.demo["tree"].info(key)["shapes"] = tuple(shapes) | |
| 640 | + self._schedule_autosave() | |
| 641 | + | |
| 620 | 642 | def _game_variations(self): |
| 621 | 643 | """Selbst gezogene Nebenvarianten für PGN: {Halbzug: [(Zug, Kommentar, Pfeile, [Fortsetzungen])]}.""" |
| 622 | 644 | tree = self.game_tree |
| @@ -635,32 +657,38 @@ class OpeningMixin: | ||
| 635 | 657 | return found |
| 636 | 658 | node, ply = main, ply + 1 |
| 637 | 659 | |
| 638 | - VARIATION_PLIES = 8 # so viele Halbzüge zeigt eine Variantenzeile der Zugliste, dann „…“ | |
| 660 | + def _list_variations(self): | |
| 661 | + """Nebenvarianten für die Zugliste als Baum: ({Halbzug: Zweige}, Knoten). | |
| 639 | 662 | |
| 640 | - def _list_variations(self): | |
| 641 | - """Nebenvarianten für die Zugliste: [(Halbzug, Text, Knoten)], Text als Pango-Markup.""" | |
| 663 | + Zweig = ((Nummer, Zugnummer, Zug, Unterzweige), …) entlang der Hauptfortsetzung; Unterzweige | |
| 664 | + sind die Alternativen zum nächsten Zug. Nummer: Index in Knoten (für Klicks und Markierung).""" | |
| 642 | 665 | tree = self.game_tree |
| 643 | 666 | if tree is None or not self._analysis_allowed() or self._game_tree() is not tree: |
| 644 | - return [] | |
| 645 | - style = self._notation() | |
| 646 | - found, node, ply = [], tree.game, -1 | |
| 667 | + return {}, {} | |
| 668 | + style, nodes = self._notation(), {} | |
| 669 | + | |
| 670 | + def branch(first): | |
| 671 | + moves, step, board = [], first, first.parent.board() | |
| 672 | + while step is not None: | |
| 673 | + prefix = f"{board.fullmove_number}." if board.turn == chess.WHITE else ( | |
| 674 | + f"{board.fullmove_number}…" if not moves else "") | |
| 675 | + san = notation.move(board, step.move, style) | |
| 676 | + board.push(step.move) | |
| 677 | + token = id(step) # bleibt gleich, wenn Varianten dazukommen (aufgeklappt bleibt aufgeklappt) | |
| 678 | + nodes[token] = step | |
| 679 | + subs = tuple(branch(alt) for alt in step.variations[1:]) | |
| 680 | + moves.append((token, prefix, _escape(san), subs)) | |
| 681 | + step = step.variations[0] if step.variations else None | |
| 682 | + return tuple(moves) | |
| 683 | + | |
| 684 | + found, node, ply = {}, tree.game, -1 | |
| 647 | 685 | while True: |
| 648 | - for child in node.variations: | |
| 649 | - if tree.meta.get(child, {}).get("played"): | |
| 650 | - continue | |
| 651 | - words, step, board = [], child, node.board() | |
| 652 | - while step is not None and len(words) < self.VARIATION_PLIES: | |
| 653 | - if board.turn == chess.WHITE or not words: | |
| 654 | - words.append(f"{board.fullmove_number}." if board.turn == chess.WHITE | |
| 655 | - else f"{board.fullmove_number}…") | |
| 656 | - words.append(notation.move(board, step.move, style)) | |
| 657 | - board.push(step.move) | |
| 658 | - step = step.variations[0] if step.variations else None | |
| 659 | - text = " ".join(words) + (" …" if step is not None else "") | |
| 660 | - found.append((ply, _escape(text), child)) | |
| 686 | + side = [c for c in node.variations if not tree.meta.get(c, {}).get("played")] | |
| 687 | + if side: | |
| 688 | + found[ply] = tuple(branch(c) for c in side) | |
| 661 | 689 | main = node.variations[0] if node.variations else None |
| 662 | 690 | if main is None or not tree.meta.get(main, {}).get("played"): |
| 663 | - return found | |
| 691 | + return found, nodes | |
| 664 | 692 | node, ply = main, ply + 1 |
| 665 | 693 | |
| 666 | 694 | def _analysis_open_node(self, node): |
| @@ -718,7 +746,10 @@ class OpeningMixin: | ||
| 718 | 746 | v.badge = None |
| 719 | 747 | v.hint_from = v.hint_to = v.threat_from = v.threat_to = None |
| 720 | 748 | v.hint_preview = v.threat_preview = None |
| 721 | - v.shapes = tree.shapes(node) | |
| 749 | + game = bool(self.demo.get("game")) | |
| 750 | + # Analysebrett zur Partie: Pfeile gehören zum Zug und werden gespeichert; in Kursen nur vorübergehend | |
| 751 | + v.shapes = () if game else tree.shapes(node) | |
| 752 | + v.show_drawn(node, tree.shapes(node) if game else None) | |
| 722 | 753 | if self.coach_trace.get_active(): |
| 723 | 754 | arrows, step = [], node |
| 724 | 755 | while step.parent is not None and not self._demo_real(step): # Spur nur für Gedachtes |
added tests/test_drawing.py +42 −0
| @@ -0,0 +1,42 @@ | ||
| 1 | +"""Pfeile und Kreise mit der rechten Maustaste, wie bei Lichess.""" | |
| 2 | + | |
| 3 | +import unittest | |
| 4 | + | |
| 5 | +import chess | |
| 6 | +import gi | |
| 7 | + | |
| 8 | +gi.require_version("Gdk", "4.0") | |
| 9 | +from gi.repository import Gdk # noqa: E402 | |
| 10 | + | |
| 11 | +from schach.board import brush, toggle_shape # noqa: E402 | |
| 12 | + | |
| 13 | +M = Gdk.ModifierType | |
| 14 | + | |
| 15 | + | |
| 16 | +class BrushTest(unittest.TestCase): | |
| 17 | + def test_modifiers_pick_lichess_colors(self): | |
| 18 | + self.assertEqual(brush(M(0)), "green") | |
| 19 | + self.assertEqual(brush(M.SHIFT_MASK), "red") | |
| 20 | + self.assertEqual(brush(M.CONTROL_MASK), "red") | |
| 21 | + self.assertEqual(brush(M.ALT_MASK), "blue") | |
| 22 | + self.assertEqual(brush(M.SHIFT_MASK | M.ALT_MASK), "yellow") | |
| 23 | + | |
| 24 | + | |
| 25 | +class ToggleTest(unittest.TestCase): | |
| 26 | + def test_same_arrow_again_removes_it(self): | |
| 27 | + arrow = (chess.E2, chess.E4, "green") | |
| 28 | + shapes = toggle_shape((), arrow) | |
| 29 | + self.assertEqual(shapes, (arrow,)) | |
| 30 | + self.assertEqual(toggle_shape(shapes, arrow), ()) | |
| 31 | + | |
| 32 | + def test_other_color_recolors_in_place_of_adding(self): | |
| 33 | + shapes = toggle_shape(((chess.F7, chess.F7, "green"),), (chess.F7, chess.F7, "red")) | |
| 34 | + self.assertEqual(shapes, ((chess.F7, chess.F7, "red"),)) | |
| 35 | + | |
| 36 | + def test_different_arrows_add_up(self): | |
| 37 | + shapes = toggle_shape(((chess.E2, chess.E4, "green"),), (chess.E4, chess.E2, "green")) | |
| 38 | + self.assertEqual(len(shapes), 2) | |
| 39 | + | |
| 40 | + | |
| 41 | +if __name__ == "__main__": | |
| 42 | + unittest.main() | |
modified tests/test_export.py +15 −1
| @@ -105,7 +105,21 @@ class VariationTest(unittest.TestCase): | ||
| 105 | 105 | def test_saved_variations_come_back(self): |
| 106 | 106 | state = self.state() |
| 107 | 107 | loaded = savegames.from_pgn(savegames.to_pgn(state)) |
| 108 | - self.assertEqual(loaded.variations, state.variations) # Kommentar ohne [%cal], Pfeil wieder „threat“ | |
| 108 | + # Kommentar ohne [%cal]; die Drohung kommt als Lichess-Gelb zurück (gelb bleibt gelb beim Zeichnen) | |
| 109 | + (move, comment, shapes, children), = loaded.variations[4] | |
| 110 | + self.assertEqual((move, comment, children), state.variations[4][0][:2] + (state.variations[4][0][3],)) | |
| 111 | + self.assertEqual(shapes, ((chess.G8, chess.F6, "yellow"),)) | |
| 112 | + | |
| 113 | + def test_drawn_shapes_on_the_game_come_back_in_lichess_colors(self): | |
| 114 | + state = sample() | |
| 115 | + state.shapes = {-1: ((chess.E2, chess.E4, "green"),), | |
| 116 | + 2: ((chess.F7, chess.F7, "red"), (chess.B1, chess.C3, "blue"), (chess.D8, chess.H4, "yellow"))} | |
| 117 | + text = savegames.to_pgn(state) | |
| 118 | + self.assertIn("[%csl Rf7]", text) | |
| 119 | + self.assertIn("[%cal Bb1c3,Yd8h4]", text) | |
| 120 | + loaded = savegames.from_pgn(text) | |
| 121 | + self.assertEqual(loaded.shapes, state.shapes) | |
| 122 | + self.assertEqual(state.truncated(1).shapes, {-1: state.shapes[-1]}) | |
| 109 | 123 | |
| 110 | 124 | def test_branching_off_keeps_earlier_variations_only(self): |
| 111 | 125 | state = self.state() |