diff --git a/apps/desktop/src/app.rs b/apps/desktop/src/app.rs index c9e5518..b380073 100644 --- a/apps/desktop/src/app.rs +++ b/apps/desktop/src/app.rs @@ -195,6 +195,9 @@ impl App { if std::env::var("CP_ENGINE_PANEL").is_ok() { g.debug_open_engine_settings(); } + if let Some(k) = std::env::var("CP_BEST").ok().and_then(|v| v.parse().ok()) { + g.debug_best_line(k); + } // CP_REVIEW=dialog opens the game-analysis dialog, any other value starts the analysis. if let Ok(v) = std::env::var("CP_REVIEW") { g.debug_review(&self.settings, ctx, v != "dialog"); diff --git a/apps/desktop/src/game_view.rs b/apps/desktop/src/game_view.rs index ccf5823..9df5fb3 100644 --- a/apps/desktop/src/game_view.rs +++ b/apps/desktop/src/game_view.rs @@ -613,12 +613,16 @@ impl GameView { cursor: self.cursor, scroll_to_cursor: self.scroll_to_cursor, marks: self.review.visible.then_some(&self.review.marks), + preview: self + .preview + .as_ref() + .map(|p| (p.base, p.san.as_slice(), p.shown)), } .show(ui); if let Some(id) = resp.clicked { self.goto(id); } - if let Some(id) = resp.best + if let Some((id, shown)) = resp.best && let Some(mark) = self.review.marks.get(&id) && let Some(root) = self.game.position(mark.base) && !mark.best.is_empty() @@ -631,7 +635,7 @@ impl GameView { root: root.clone(), moves: mark.best.clone(), san: mark.best_san.clone(), - shown: 1, + shown: shown.min(mark.best.len()), }); } if let Some((id, cmd)) = resp.context { @@ -954,6 +958,28 @@ impl GameView { self.engine_settings_open = true; } + /// Opens the better line of the first engine-marked move up to its `shown`-th move. + pub fn debug_best_line(&mut self, shown: usize) { + let mark = self + .game + .mainline() + .into_iter() + .find_map(|id| self.review.marks.get(&id).cloned()); + if let Some(mark) = mark + && let Some(root) = self.game.position(mark.base) + && !mark.best.is_empty() + { + self.cursor = mark.base; + self.preview = Some(Preview { + base: mark.base, + root: root.clone(), + shown: shown.clamp(1, mark.best.len()), + moves: mark.best, + san: mark.best_san, + }); + } + } + pub fn debug_review(&mut self, settings: &Settings, ctx: &egui::Context, start: bool) { if start { self.start_review(settings, false, ctx, &mut Vec::new()); @@ -978,6 +1004,7 @@ impl GameView { fn step_back(&mut self) { if let Some(p) = &mut self.preview { + self.scroll_to_cursor = true; p.shown = p.shown.saturating_sub(1); if p.shown == 0 { self.preview = None; @@ -992,6 +1019,7 @@ impl GameView { fn step_forward(&mut self, actions: &mut Vec) { if let Some(p) = &mut self.preview { if p.shown < p.moves.len() { + self.scroll_to_cursor = true; p.shown += 1; actions.push(GameAction::Sound(Sound::Move)); } diff --git a/apps/desktop/src/notation.rs b/apps/desktop/src/notation.rs index ef148e3..b782e8a 100644 --- a/apps/desktop/src/notation.rs +++ b/apps/desktop/src/notation.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use crate::i18n::tr; use crate::review::{Mark, judgement_color, judgement_name, judgement_symbol}; -use chess_core::shakmaty::Color; +use chess_core::shakmaty::{Color, Position}; use chess_core::{CommentPart, Game, Nag, NodeId, parse_comment}; use egui::{Align2, Color32, FontId, RichText, Sense, Stroke, Ui, vec2}; @@ -15,6 +15,8 @@ pub struct Notation<'a> { pub scroll_to_cursor: bool, /// Engine assessments of main-line moves (whole-game analysis), shown apart from NAGs. pub marks: Option<&'a HashMap>, + /// Engine line shown on the board: (start node, SAN moves, moves played so far). + pub preview: Option<(NodeId, &'a [String], usize)>, } /// Result of a frame of the notation panel. @@ -22,8 +24,8 @@ pub struct Notation<'a> { pub struct NotationResponse { pub clicked: Option, pub context: Option<(NodeId, NotationCommand)>, - /// The better line of this marked move was clicked. - pub best: Option, + /// A move of the better line of this marked move was clicked: (marked node, moves to show). + pub best: Option<(NodeId, usize)>, } #[derive(Clone, Copy, Debug)] @@ -76,7 +78,7 @@ impl Notation<'_> { if interrupted || color == Color::Black { row.flush(ui, &self, &mut out); if let Some(mark) = mark { - mark_block(ui, child, mark, &mut out); + mark_block(ui, &self, child, mark, &mut out); } if !after.is_empty() { comment_block(ui, &after); @@ -239,9 +241,18 @@ fn paint_badge(ui: &Ui, cell: egui::Rect, mark: &Mark) { .galley(rect.center() - galley.size() / 2.0, galley, Color32::WHITE); } -/// "Mistake. Best: 23…Nf6 24.Nd5 …" under a marked main-line move; the line opens as a preview. -fn mark_block(ui: &mut Ui, id: NodeId, mark: &Mark, out: &mut NotationResponse) { +/// "Mistake. Best: 23…Nf6 24.Nd5 …" under a marked main-line move. Each move opens the line +/// on the board up to it; the move shown on the board is highlighted like the game cursor. +fn mark_block(ui: &mut Ui, n: &Notation, id: NodeId, mark: &Mark, out: &mut NotationResponse) { let color = judgement_color(mark.judgement); + let start = n + .game + .position(mark.base) + .map(|p| (p.fullmoves().get(), p.turn())); + let shown = n + .preview + .filter(|(base, san, _)| *base == mark.base && *san == mark.best_san.as_slice()) + .map(|(_, _, shown)| shown); egui::Frame::new() .fill(ui.visuals().extreme_bg_color) .inner_margin(egui::Margin { @@ -254,33 +265,50 @@ fn mark_block(ui: &mut Ui, id: NodeId, mark: &Mark, out: &mut NotationResponse) .show(ui, |ui| { ui.set_width(ui.available_width()); ui.horizontal_wrapped(|ui| { - ui.spacing_mut().item_spacing.x = 5.0; + ui.spacing_mut().item_spacing = vec2(3.0, 2.0); ui.label( RichText::new(format!("{}.", judgement_name(mark.judgement))) .color(color) .strong(), ); - let Some(first) = &mark.best_label else { + if mark.best_san.is_empty() { return; - }; + } ui.label(RichText::new(tr("Best:")).weak()); - let mut line = first.clone(); - for san in mark.best_san.iter().skip(1).take(4) { - line.push(' '); - line.push_str(san); - } - if mark.best_san.len() > 5 { - line.push_str(" …"); - } - if ui - .add( - egui::Button::new(RichText::new(line).size(14.5)) - .frame_when_inactive(false), - ) - .on_hover_text(tr("Show this line on the board")) - .clicked() - { - out.best = Some(id); + for (i, san) in mark.best_san.iter().enumerate() { + let label = match start { + Some((num, turn)) => { + let ply = i as u32 + u32::from(turn == Color::Black); + let num = num + ply / 2; + if ply.is_multiple_of(2) { + format!("{num}.{san}") + } else if i == 0 { + format!("{num}…{san}") + } else { + san.clone() + } + } + None => san.clone(), + }; + let text = RichText::new(label).size(14.5); + let current = shown == Some(i + 1); + let button = if current { + let sel = ui.visuals().selection; + egui::Button::new(text.color(sel.stroke.color).strong()) + .fill(sel.bg_fill) + .stroke(Stroke::new(1.0, sel.stroke.color)) + } else { + egui::Button::new(text).frame_when_inactive(false) + }; + let resp = ui + .add(button) + .on_hover_text(tr("Show this line on the board")); + if current && n.scroll_to_cursor { + resp.scroll_to_me(Some(egui::Align::Center)); + } + if resp.clicked() { + out.best = Some((id, i + 1)); + } } }); }); diff --git a/docs/dev/desktop.md b/docs/dev/desktop.md index 33c4c67..cc3c976 100644 --- a/docs/dev/desktop.md +++ b/docs/dev/desktop.md @@ -37,7 +37,7 @@ XDG_CONFIG_HOME=/tmp/cfg CP_OPEN=11 CP_PLY=3 CP_ANALYZE=1 ./target/debug/chesspe ``` `CP_OPEN` — индекс партии (с 0) в первой базе списка, `CP_PLY` — шагов по основной линии, -`CP_ANALYZE` — запустить активный движок, `CP_SETTINGS` — открыть настройки, `CP_ENGINE_PANEL` — раскрыть панель настроек движка, `CP_VAR` — перейти в первый вариант после `CP_PLY`, `CP_REVIEW=1` — запустить анализ партии (`=dialog` — только открыть окно), `CP_SCREENSHOT=file.png` (+ `CP_SCREENSHOT_DELAY=сек`) — полноэкранный снимок окна и выход; так сделаны `docs/screenshots/`. +`CP_ANALYZE` — запустить активный движок, `CP_SETTINGS` — открыть настройки, `CP_ENGINE_PANEL` — раскрыть панель настроек движка, `CP_VAR` — перейти в первый вариант после `CP_PLY`, `CP_REVIEW=1` — запустить анализ партии (`=dialog` — только открыть окно), `CP_BEST=k` — показать на доске k-й ход лучшей линии у первой отметки движка (нужен готовый анализ), `CP_SCREENSHOT=file.png` (+ `CP_SCREENSHOT_DELAY=сек`) — полноэкранный снимок окна и выход; так сделаны `docs/screenshots/`. ## Языки