User comments: an editor in the notation, symbols, arrows on the board
Some checks are pending
CI / macos-build (push) Waiting to run
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run

Enter on a move (the move's menu, a double click on a comment, the ☰ menu) opens
the editor where the comment stands: after any move, in a variation too (the
flow is cut at the move), or at the start of the game; Enter — done, Esc —
cancel, Shift+Enter — a paragraph; another move writes the draft; one edit —
one undo step. Symbols of the move (! ? !! ?? !? ?!, Ctrl+1…6) and of the
position in the editor and in the menu. The right mouse button draws arrows
and circles on the board ([%cal]/[%csl] of the current move; colours by
modifiers). Existing comments are edited and deleted; their commands and
untouched bytes are kept. chess_core: with_text, with_shapes, shapes,
Game::set_comments_before. Dev hooks CP_COMMENT_EDIT, CP_TYPE, CP_SHAPE.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-25 02:35:54 +03:00
parent 9967d6b759
commit dc7d9f88b3
15 changed files with 1592 additions and 71 deletions

View file

@ -23,11 +23,24 @@ pub struct Badge {
}
/// Interaction state that lives across frames (per board instance).
/// A shape the user drew with the right mouse button (docs/design/user-comments.md
/// §10): an arrow, or a circle on one square. The colour is the modifier held when the
/// button was released: none green, Shift red, Alt blue, Shift+Alt yellow (Lichess).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Drawn {
Arrow(chess_core::ShapeColor, Square, Square),
Circle(chess_core::ShapeColor, Square),
}
#[derive(Default)]
pub struct BoardState {
selected: Option<Square>,
drag: Option<Square>,
promotion: Option<(Square, Square)>,
/// The square the right button went down on: a shape is being drawn.
shape_from: Option<Square>,
/// A shape drawn this frame, for the game to take (`take_shape`).
shape: Option<Drawn>,
/// The position drawn last: a different one next frame is animated from it.
shown: Option<Board>,
anim: Option<Anim>,
@ -44,6 +57,11 @@ impl BoardState {
self.promotion = None;
}
/// The shape the user has just drawn, once.
pub fn take_shape(&mut self) -> Option<Drawn> {
self.shape.take()
}
/// A piece is selected or dragged, or a promotion is being chosen.
pub fn is_busy(&self) -> bool {
self.selected.is_some() || self.drag.is_some() || self.promotion.is_some()
@ -182,8 +200,32 @@ impl BoardView<'_> {
_ => state.selected = self.own_piece(sq).then_some(sq),
}
}
// The right button draws: a drag — an arrow, a click — a circle on the square.
// Pressing it also drops a picked-up piece, as before.
if response.drag_started_by(egui::PointerButton::Secondary) {
let origin = ui.input(|i| i.pointer.press_origin()).or(pointer);
state.shape_from = origin.and_then(|p| geo.square_at(p));
state.selected = None;
state.drag = None;
}
if response.drag_stopped_by(egui::PointerButton::Secondary) {
let to = ui
.input(|i| i.pointer.latest_pos())
.and_then(|p| geo.square_at(p));
let color = shape_color_for(ui.input(|i| i.modifiers));
if let Some(from) = state.shape_from.take() {
state.shape = Some(match to {
Some(to) if to != from => Drawn::Arrow(color, from, to),
_ => Drawn::Circle(color, from),
});
}
}
if response.secondary_clicked() {
state.clear();
if let Some(sq) = pointer.and_then(|p| geo.square_at(p)) {
let color = shape_color_for(ui.input(|i| i.modifiers));
state.shape = Some(Drawn::Circle(color, sq));
}
}
}
@ -305,6 +347,35 @@ impl BoardView<'_> {
for a in self.arrows {
draw_arrow(&painter, &geo, a);
}
if let Some(from) = state.shape_from
&& response.dragged_by(egui::PointerButton::Secondary)
{
let color = shape_color_for(ui.input(|i| i.modifiers));
let c = shape_preview_color(color);
match ui
.input(|i| i.pointer.latest_pos())
.and_then(|p| geo.square_at(p))
{
Some(to) if to != from => draw_arrow(
&painter,
&geo,
&Arrow {
from,
to,
color: c,
weight: 0.9,
},
),
_ => {
let r = geo.square_rect(from);
painter.circle_stroke(
r.center(),
sq_size * 0.46,
Stroke::new(sq_size * 0.07, c),
);
}
}
}
if let Some(b) = self.badge {
let r = geo.square_rect(b.square);
let radius = sq_size * 0.19;
@ -509,6 +580,27 @@ impl Geometry {
}
}
/// The colour of a shape from the modifiers held (as on Lichess).
fn shape_color_for(m: egui::Modifiers) -> chess_core::ShapeColor {
match (m.shift, m.alt) {
(false, false) => chess_core::ShapeColor::Green,
(true, false) => chess_core::ShapeColor::Red,
(false, true) => chess_core::ShapeColor::Blue,
(true, true) => chess_core::ShapeColor::Yellow,
}
}
/// How a shape being drawn looks (the game draws the finished ones).
pub fn shape_preview_color(c: chess_core::ShapeColor) -> Color32 {
match c {
chess_core::ShapeColor::Red => Color32::from_rgba_unmultiplied(136, 32, 32, 170),
chess_core::ShapeColor::Green => Color32::from_rgba_unmultiplied(21, 120, 27, 170),
chess_core::ShapeColor::Blue => Color32::from_rgba_unmultiplied(0, 48, 136, 170),
chess_core::ShapeColor::Yellow => Color32::from_rgba_unmultiplied(230, 143, 0, 170),
chess_core::ShapeColor::Other(_) => Color32::from_rgba_unmultiplied(120, 120, 120, 170),
}
}
fn draw_arrow(painter: &egui::Painter, geo: &Geometry, a: &Arrow) {
let from = geo.center(a.from);
let to = geo.center(a.to);