| @@ -8,13 +8,14 @@ zurückgenommen: spielbarer Buchzug, kein Buchzug aber in Ordnung, oder Fehler. |
| 8 | 8 | |
| 9 | 9 | import os |
| 10 | 10 | import random |
| 11 | +import re |
| 11 | 12 | |
| 12 | 13 | import chess |
| 13 | 14 | import gi |
| 14 | 15 | |
| 15 | 16 | gi.require_version("Gtk", "4.0") |
| 16 | 17 | gi.require_version("Adw", "1") |
| 17 | | -from gi.repository import Adw, Gio, GLib, Gtk # noqa: E402 |
| 18 | +from gi.repository import Adw, Gio, GLib, Gtk, Pango # noqa: E402 |
| 18 | 19 | |
| 19 | 20 | from . import annotate, notation, opening, repetition # noqa: E402 |
| 20 | 21 | from .engines import maia_candidates # noqa: E402 |
| @@ -77,6 +78,52 @@ def _due_text(course): |
| 77 | 78 | return f"Heute dran: {' und '.join(parts)} (von {total} Linien)." |
| 78 | 79 | |
| 79 | 80 | |
| 81 | +PIECE_LETTERS = "KQRBNDTLS" # deutsch und englisch |
| 82 | +SQUARE_RE = re.compile(r"[a-h][1-8](?![0-9A-Za-zäöüß])") |
| 83 | + |
| 84 | + |
| 85 | +def squares_in(text): |
| 86 | + """Feldnamen im Text: [(Anfang, Ende, Feld)]. „e4“, „Lc4“, „Sxe5“, „Sbd2“ – aber nicht „Ende4“.""" |
| 87 | + found = [] |
| 88 | + for match in SQUARE_RE.finditer(text): |
| 89 | + start = match.start() |
| 90 | + before = text[start - 1] if start else " " |
| 91 | + if before in "abcdefgh": # nur als Zusatz wie in „Sbd2“: davor muss eine Figur stehen |
| 92 | + if start < 2 or text[start - 2] not in PIECE_LETTERS: |
| 93 | + continue |
| 94 | + elif before.isalnum() and before not in PIECE_LETTERS + "x": |
| 95 | + continue |
| 96 | + found.append((match.start(), match.end(), chess.parse_square(match.group()))) |
| 97 | + return found |
| 98 | + |
| 99 | + |
| 100 | +TAG_RE = re.compile(r"(<[^>]*>)") |
| 101 | + |
| 102 | + |
| 103 | +def mark_squares(markup): |
| 104 | + """Feldnamen im Pango-Markup blassgelb hinterlegen – wie das goldene Leuchten auf dem Brett.""" |
| 105 | + parts = TAG_RE.split(markup) |
| 106 | + for i in range(0, len(parts), 2): # gerade Stellen sind Text, ungerade Tags |
| 107 | + text = parts[i] |
| 108 | + for start, end, _sq in reversed(squares_in(text)): |
| 109 | + text = (f"{text[:start]}<span bgcolor='#f5c219' bgalpha='35%'>{text[start:end]}</span>" |
| 110 | + f"{text[end:]}") |
| 111 | + parts[i] = text |
| 112 | + return "".join(parts) |
| 113 | + |
| 114 | + |
| 115 | +def square_at(label, x, y): |
| 116 | + """Feld, dessen Name im Label an der Stelle (x, y) steht – oder None.""" |
| 117 | + layout = label.get_layout() |
| 118 | + ox, oy = label.get_layout_offsets() |
| 119 | + inside, index, _trailing = layout.xy_to_index(int((x - ox) * Pango.SCALE), int((y - oy) * Pango.SCALE)) |
| 120 | + if not inside: |
| 121 | + return None |
| 122 | + text = layout.get_text() |
| 123 | + char = len(text.encode()[:index].decode(errors="ignore")) |
| 124 | + return next((square for start, end, square in squares_in(text) if start <= char < end), None) |
| 125 | + |
| 126 | + |
| 80 | 127 | class OpeningMixin: |
| 81 | 128 | def _opening_init(self): |
| 82 | 129 | self.course = None |
| @@ -101,6 +148,11 @@ class OpeningMixin: |
| 101 | 148 | three_lines = self.coach_label.create_pango_layout("Ag\nAg\nAg").get_pixel_size()[1] |
| 102 | 149 | self.coach_label.set_size_request(-1, three_lines) |
| 103 | 150 | self.coach_label.set_valign(Gtk.Align.CENTER) |
| 151 | + # Maus über einem Feldnamen („e4“, auch in „Lc4“): das Feld leuchtet auf dem Brett |
| 152 | + motion = Gtk.EventControllerMotion() |
| 153 | + motion.connect("motion", lambda _c, x, y: self._coach_spotlight(square_at(self.coach_label, x, y))) |
| 154 | + motion.connect("leave", lambda _c: self._coach_spotlight(None)) |
| 155 | + self.coach_label.add_controller(motion) |
| 104 | 156 | self.coach_button = Gtk.Button( |
| 105 | 157 | label="Gegen Maia weiterspielen", valign=Gtk.Align.CENTER, visible=False, margin_end=10, |
| 106 | 158 | ) |
| @@ -139,6 +191,11 @@ class OpeningMixin: |
| 139 | 191 | self.coach = Gtk.Revealer(child=card, transition_type=Gtk.RevealerTransitionType.SLIDE_UP) |
| 140 | 192 | return self.coach |
| 141 | 193 | |
| 194 | + def _coach_spotlight(self, square): |
| 195 | + if self.view.spotlight != square: |
| 196 | + self.view.spotlight = square |
| 197 | + self.view.queue_draw() |
| 198 | + |
| 142 | 199 | def _coach(self, markup, ply=None, replace=False, shapes=(), demos=()): |
| 143 | 200 | """Neue Rückmeldung zur Stellung nach Halbzug ``ply`` (Standard: aktuelle Stellung). |
| 144 | 201 | |
| @@ -167,7 +224,7 @@ class OpeningMixin: |
| 167 | 224 | return |
| 168 | 225 | last = len(self.coach_log) - 1 |
| 169 | 226 | index = last if self.coach_index is None else self.coach_index |
| 170 | | - self.coach_label.set_markup(self.coach_log[index]["markup"]) |
| 227 | + self.coach_label.set_markup(mark_squares(self.coach_log[index]["markup"])) |
| 171 | 228 | self.view.shapes = self.coach_log[index].get("shapes", ()) |
| 172 | 229 | self.view.queue_draw() |
| 173 | 230 | self.coach_nav.set_visible(last > 0) |
| @@ -282,9 +339,9 @@ class OpeningMixin: |
| 282 | 339 | before = demo.board_at(index - 1) |
| 283 | 340 | label = _escape(notation.label(before, move, self._notation())) |
| 284 | 341 | text = _escape(notation.text(comment, self._notation())) |
| 285 | | - self.coach_label.set_markup( |
| 342 | + self.coach_label.set_markup(mark_squares( |
| 286 | 343 | f"<span alpha='70%'>{_escape(self.demo['label'])} · {index + 1}/{len(demo.steps)}</span>\n" |
| 287 | | - f"<b>{label}</b>" + (f" – {text}" if text else "")) |
| 344 | + f"<b>{label}</b>" + (f" – {text}" if text else ""))) |
| 288 | 345 | self.coach_nav.set_visible(True) |
| 289 | 346 | self.coach_prev.set_sensitive(index > 0) |
| 290 | 347 | self.coach_next.set_sensitive(index < len(demo.steps) - 1) |