| @@ -78,6 +78,7 @@ class OpeningMixin: |
| 78 | 78 | self._name_cache = {} |
| 79 | 79 | self.coach_log = [] # Rückmeldungen: {"ply": Halbzug der zugehörigen Stellung, "markup": Text} |
| 80 | 80 | self.coach_index = None # angezeigte Rückmeldung; None = die neueste |
| 81 | + self.demo = None # laufende „Warum?“-Vorführung: {"demo": Demo, "label": "h3", "index": Schritt} |
| 81 | 82 | |
| 82 | 83 | # --- Oberfläche -------------------------------------------------------- |
| 83 | 84 | |
| @@ -95,6 +96,9 @@ class OpeningMixin: |
| 95 | 96 | label="Gegen Maia weiterspielen", valign=Gtk.Align.CENTER, visible=False, margin_end=10, |
| 96 | 97 | ) |
| 97 | 98 | self.coach_button.set_action_name("win.opening-continue") |
| 99 | + self.coach_why = Gtk.Button(valign=Gtk.Align.CENTER, visible=False, margin_end=10, |
| 100 | + tooltip_text="Zeigt auf dem Brett, was hinter dem Zug steckt") |
| 101 | + self.coach_why.connect("clicked", lambda *_: self._demo_start()) |
| 98 | 102 | # Blättern durch die letzten Rückmeldungen; das Brett zeigt die Stellung dazu |
| 99 | 103 | self.coach_nav = Gtk.Box(spacing=2, valign=Gtk.Align.CENTER, margin_end=6) |
| 100 | 104 | self.coach_prev = Gtk.Button(icon_name="go-previous-symbolic", tooltip_text="Vorherige Rückmeldung", |
| @@ -112,17 +116,19 @@ class OpeningMixin: |
| 112 | 116 | card.append(Gtk.Image(icon_name="dialog-information-symbolic", margin_start=14, valign=Gtk.Align.CENTER)) |
| 113 | 117 | card.append(self.coach_label) |
| 114 | 118 | card.append(self.coach_nav) |
| 119 | + card.append(self.coach_why) |
| 115 | 120 | card.append(self.coach_button) |
| 116 | 121 | self.coach = Gtk.Revealer(child=card, transition_type=Gtk.RevealerTransitionType.SLIDE_UP) |
| 117 | 122 | return self.coach |
| 118 | 123 | |
| 119 | | - def _coach(self, markup, ply=None, replace=False, shapes=()): |
| 124 | + def _coach(self, markup, ply=None, replace=False, shapes=(), demo=None): |
| 120 | 125 | """Neue Rückmeldung zur Stellung nach Halbzug ``ply`` (Standard: aktuelle Stellung). |
| 121 | 126 | |
| 122 | 127 | ``shapes``: Pfeile und Felder aus dem Kurskommentar, solange diese Rückmeldung zu sehen ist. |
| 123 | 128 | """ |
| 124 | 129 | entry = {"ply": len(self.board.move_stack) - 1 if ply is None else ply, "markup": markup, |
| 125 | | - "shapes": tuple(shapes)} |
| 130 | + "shapes": tuple(shapes), "demo": demo} |
| 131 | + self.demo = None |
| 126 | 132 | if replace and self.coach_log: |
| 127 | 133 | self.coach_log[-1] = entry |
| 128 | 134 | else: |
| @@ -132,6 +138,10 @@ class OpeningMixin: |
| 132 | 138 | self._coach_show() |
| 133 | 139 | |
| 134 | 140 | def _coach_show(self): |
| 141 | + if self.demo is not None: |
| 142 | + self._demo_show() |
| 143 | + return |
| 144 | + self.coach_why.set_visible(False) |
| 135 | 145 | if not self.coach_log: |
| 136 | 146 | self.coach.set_reveal_child(False) |
| 137 | 147 | self.view.shapes = () |
| @@ -146,9 +156,17 @@ class OpeningMixin: |
| 146 | 156 | self.coach_next.set_sensitive(index < last) |
| 147 | 157 | self.coach_last.set_sensitive(index < last) |
| 148 | 158 | self.coach_button.set_visible(self.final_text is not None and index == last) |
| 159 | + self.coach_last.set_tooltip_text("Zum aktuellen Zug") |
| 160 | + why = self.coach_log[index].get("demo") |
| 161 | + if why is not None: |
| 162 | + self.coach_why.set_label(f"Warum {why['label']}?") |
| 163 | + self.coach_why.set_visible(why is not None) |
| 149 | 164 | self.coach.set_reveal_child(True) |
| 150 | 165 | |
| 151 | 166 | def _coach_step(self, delta): |
| 167 | + if self.demo is not None: |
| 168 | + self._demo_step(delta) |
| 169 | + return |
| 152 | 170 | last = len(self.coach_log) - 1 |
| 153 | 171 | index = (last if self.coach_index is None else self.coach_index) + delta |
| 154 | 172 | index = max(0, min(last, index)) |
| @@ -161,11 +179,84 @@ class OpeningMixin: |
| 161 | 179 | self._coach_show() |
| 162 | 180 | |
| 163 | 181 | def _coach_latest(self): |
| 182 | + if self.demo is not None: |
| 183 | + self._demo_end() |
| 184 | + return |
| 164 | 185 | self.coach_index = None |
| 165 | 186 | self.browse_ply = None |
| 166 | 187 | self._refresh() |
| 167 | 188 | self._coach_show() |
| 168 | 189 | |
| 190 | + # --- „Warum?“-Vorführung ---------------------------------------------- |
| 191 | + |
| 192 | + def _demo_entry(self, before, move): |
| 193 | + demo = self.course.demo(before, move) if self.course else None |
| 194 | + return {"demo": demo, "label": notation.move(before, move, self._notation())} if demo else None |
| 195 | + |
| 196 | + def _demo_start(self): |
| 197 | + last = len(self.coach_log) - 1 |
| 198 | + index = last if self.coach_index is None else self.coach_index |
| 199 | + why = self.coach_log[index].get("demo") if self.coach_log else None |
| 200 | + if why is None: |
| 201 | + return |
| 202 | + self.demo = dict(why, index=0) |
| 203 | + demo = why["demo"] |
| 204 | + self.view.animate_move(chess.Board(demo.start_fen), demo.steps[0][0]) |
| 205 | + self._refresh() |
| 206 | + self._coach_show() |
| 207 | + |
| 208 | + def _demo_step(self, delta): |
| 209 | + demo = self.demo["demo"] |
| 210 | + index = max(0, min(len(demo.steps) - 1, self.demo["index"] + delta)) |
| 211 | + if index == self.demo["index"]: |
| 212 | + return |
| 213 | + if index > self.demo["index"]: |
| 214 | + self.view.animate_move(demo.board_at(index - 1), demo.steps[index][0]) |
| 215 | + self.demo["index"] = index |
| 216 | + self._refresh() |
| 217 | + self._coach_show() |
| 218 | + |
| 219 | + def _demo_end(self): |
| 220 | + if self.demo is None: |
| 221 | + return False |
| 222 | + self.demo = None |
| 223 | + self._refresh() |
| 224 | + self._coach_show() |
| 225 | + return True |
| 226 | + |
| 227 | + def _demo_refresh(self): |
| 228 | + """Brett zeigt die Vorführung; ziehen kann man erst wieder danach.""" |
| 229 | + v = self.view |
| 230 | + demo, index = self.demo["demo"], self.demo["index"] |
| 231 | + board = demo.board_at(index) |
| 232 | + v.board = board |
| 233 | + v.last_move = board.peek() |
| 234 | + v.interactive = False |
| 235 | + v.selected = None |
| 236 | + v.badge = None |
| 237 | + v.hint_from = v.hint_to = v.threat_from = v.threat_to = None |
| 238 | + v.shapes = demo.steps[index][2] |
| 239 | + v.queue_draw() |
| 240 | + self._refresh_chrome() |
| 241 | + |
| 242 | + def _demo_show(self): |
| 243 | + demo, index = self.demo["demo"], self.demo["index"] |
| 244 | + move, comment, _shapes = demo.steps[index] |
| 245 | + before = demo.board_at(index - 1) |
| 246 | + label = _escape(notation.label(before, move, self._notation())) |
| 247 | + text = _escape(notation.text(comment, self._notation())) |
| 248 | + self.coach_label.set_markup( |
| 249 | + f"<span alpha='70%'>Warum {_escape(self.demo['label'])}? · {index + 1}/{len(demo.steps)}</span>\n" |
| 250 | + f"<b>{label}</b>" + (f" – {text}" if text else "")) |
| 251 | + self.coach_nav.set_visible(True) |
| 252 | + self.coach_prev.set_sensitive(index > 0) |
| 253 | + self.coach_next.set_sensitive(index < len(demo.steps) - 1) |
| 254 | + self.coach_last.set_sensitive(True) |
| 255 | + self.coach_last.set_tooltip_text("Zurück zur Partie") |
| 256 | + self.coach_why.set_visible(False) |
| 257 | + self.coach_button.set_visible(False) |
| 258 | + self.coach.set_reveal_child(True) |
| 259 | + |
| 169 | 260 | def _opening_title(self): |
| 170 | 261 | return self.course.title if self.course else "Eröffnung" |
| 171 | 262 | |
| @@ -268,6 +359,7 @@ class OpeningMixin: |
| 268 | 359 | self._opening_next_step() |
| 269 | 360 | |
| 270 | 361 | def _opening_leave(self): |
| 362 | + self.demo = None |
| 271 | 363 | self.course = None |
| 272 | 364 | self.opening_busy = False |
| 273 | 365 | self.coach_log = [] |
| @@ -334,12 +426,15 @@ class OpeningMixin: |
| 334 | 426 | if gen != self.gen or self.mode != "opening" or option.move not in self.board.legal_moves: |
| 335 | 427 | return False |
| 336 | 428 | label = notation.label(self.board, option.move, self._notation()) |
| 429 | + # „Warum?“ zum eigenen Zug davor bleibt erreichbar, auch wenn der Gegner schon geantwortet hat |
| 430 | + why = self._demo_entry(self.board, option.move) or ( |
| 431 | + self.coach_log[-1].get("demo") if self.coach_log else None) |
| 337 | 432 | self.view.animate_move(self.board, option.move) |
| 338 | 433 | self.board.push(option.move) |
| 339 | 434 | self.opening_busy = False |
| 340 | 435 | self._changed() |
| 341 | 436 | comment = f" – {_escape(notation.text(option.comment, self._notation()))}" if option.comment else "" |
| 342 | | - self._coach(f"<b>{_escape(label)}</b>{comment}", shapes=option.shapes) |
| 437 | + self._coach(f"<b>{_escape(label)}</b>{comment}", shapes=option.shapes, demo=why) |
| 343 | 438 | self._refresh() |
| 344 | 439 | if not self.course.options(self.board): |
| 345 | 440 | self._opening_finished() |
| @@ -361,7 +456,8 @@ class OpeningMixin: |
| 361 | 456 | else: |
| 362 | 457 | comment = "Zugumstellung – auch so kommst du zu unserem Ziel." |
| 363 | 458 | self._coach(f"✓ <b>{label}</b> – {_escape(notation.text(comment, self._notation()))}", |
| 364 | | - shapes=verdict.option.shapes if verdict.option else ()) |
| 459 | + shapes=verdict.option.shapes if verdict.option else (), |
| 460 | + demo=self._demo_entry(before, move)) |
| 365 | 461 | self._refresh() |
| 366 | 462 | self._opening_next_step() |
| 367 | 463 | return |