Highlight the shown move of an engine better line in the notation
Each move of the "Best:" line is its own button: clicking opens the line up to it, and stepping through the preview highlights the current move. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6cf639bb24
commit
6db8d67821
4 changed files with 88 additions and 29 deletions
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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<GameAction>) {
|
||||
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));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<NodeId, Mark>>,
|
||||
/// 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<NodeId>,
|
||||
pub context: Option<(NodeId, NotationCommand)>,
|
||||
/// The better line of this marked move was clicked.
|
||||
pub best: Option<NodeId>,
|
||||
/// 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));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue