2026-10-18 01:54:06 +03:00
|
|
|
//! Chessboard widget: drawing, click/drag move input, promotion picker, arrows.
|
|
|
|
|
|
|
|
|
|
use egui::{Align2, Color32, FontId, Pos2, Rect, Sense, Shape, Stroke, Vec2, vec2};
|
2026-10-21 19:57:54 +03:00
|
|
|
use shakmaty::{Board, Chess, Color, File, Move, Piece, Position, Rank, Role, Square};
|
2026-10-18 01:54:06 +03:00
|
|
|
|
|
|
|
|
use crate::assets::{BoardTheme, PieceSet};
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
|
pub struct Arrow {
|
|
|
|
|
pub from: Square,
|
|
|
|
|
pub to: Square,
|
|
|
|
|
pub color: Color32,
|
|
|
|
|
/// Relative thickness, 1.0 = default.
|
|
|
|
|
pub weight: f32,
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 14:00:19 +03:00
|
|
|
/// A round label in the top right corner of a square, as on Lichess.
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
|
pub struct Badge {
|
|
|
|
|
pub square: Square,
|
|
|
|
|
pub text: &'static str,
|
|
|
|
|
pub color: Color32,
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 01:54:06 +03:00
|
|
|
/// Interaction state that lives across frames (per board instance).
|
User comments: an editor in the notation, symbols, arrows on the board
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>
2026-10-25 02:35:54 +03:00
|
|
|
/// 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),
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 01:54:06 +03:00
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct BoardState {
|
|
|
|
|
selected: Option<Square>,
|
|
|
|
|
drag: Option<Square>,
|
|
|
|
|
promotion: Option<(Square, Square)>,
|
User comments: an editor in the notation, symbols, arrows on the board
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>
2026-10-25 02:35:54 +03:00
|
|
|
/// 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>,
|
2026-10-21 19:57:54 +03:00
|
|
|
/// The position drawn last: a different one next frame is animated from it.
|
|
|
|
|
shown: Option<Board>,
|
|
|
|
|
anim: Option<Anim>,
|
|
|
|
|
/// A move was dropped by dragging the piece from this square: that piece is
|
|
|
|
|
/// already in place (a castling rook or a captured pawn still move).
|
|
|
|
|
dropped: Option<Square>,
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
/// A premove set while the opponent thinks (from, to): played at once after their
|
|
|
|
|
/// move if it is legal then (Lichess).
|
|
|
|
|
premove: Option<(Square, Square)>,
|
2026-10-18 01:54:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BoardState {
|
2026-10-21 19:57:54 +03:00
|
|
|
/// Forgets the selection, the drag and the promotion choice.
|
2026-10-18 01:54:06 +03:00
|
|
|
pub fn clear(&mut self) {
|
2026-10-21 19:57:54 +03:00
|
|
|
self.selected = None;
|
|
|
|
|
self.drag = None;
|
|
|
|
|
self.promotion = None;
|
|
|
|
|
}
|
2026-10-22 19:57:53 +03:00
|
|
|
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
/// The opponent has moved: a piece of `own` in the hand (selected or dragged) stays
|
|
|
|
|
/// there, as on Lichess; one that was captured, and a promotion choice, are dropped.
|
|
|
|
|
pub fn opponent_moved(&mut self, board: &Board, own: Color) {
|
|
|
|
|
let mine = |sq: Square| board.piece_at(sq).is_some_and(|p| p.color == own);
|
|
|
|
|
self.selected = self.selected.filter(|&s| mine(s));
|
|
|
|
|
self.drag = self.drag.filter(|&s| mine(s));
|
|
|
|
|
self.promotion = None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The premove set, taken (it is played or dropped after the opponent's move).
|
|
|
|
|
pub fn take_premove(&mut self) -> Option<(Square, Square)> {
|
|
|
|
|
self.premove.take()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets a premove (dev hook `CP_PREMOVE`).
|
|
|
|
|
pub fn set_premove(&mut self, from: Square, to: Square) {
|
|
|
|
|
self.premove = Some((from, to));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Drops the premove (a take-back, the end of the game).
|
|
|
|
|
pub fn clear_premove(&mut self) {
|
|
|
|
|
self.premove = None;
|
|
|
|
|
}
|
|
|
|
|
|
User comments: an editor in the notation, symbols, arrows on the board
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>
2026-10-25 02:35:54 +03:00
|
|
|
/// The shape the user has just drawn, once.
|
|
|
|
|
pub fn take_shape(&mut self) -> Option<Drawn> {
|
|
|
|
|
self.shape.take()
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-22 19:57:53 +03:00
|
|
|
/// 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()
|
|
|
|
|
}
|
2026-10-21 19:57:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Pieces moving from one position to the next, as on Lichess (chessground): each piece
|
|
|
|
|
/// of the new position that was not there slides from the nearest square that held the
|
|
|
|
|
/// same piece; a piece left without a new square fades out.
|
|
|
|
|
struct Anim {
|
|
|
|
|
start: f64,
|
|
|
|
|
duration: f64,
|
|
|
|
|
slides: Vec<(Piece, Square, Square)>,
|
|
|
|
|
fadings: Vec<(Piece, Square)>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Anim {
|
|
|
|
|
fn new(prev: &Board, cur: &Board, start: f64, duration: f64) -> Anim {
|
|
|
|
|
let mut missing: Vec<(Piece, Square)> = Vec::new();
|
|
|
|
|
let mut new: Vec<(Piece, Square)> = Vec::new();
|
|
|
|
|
for sq in Square::ALL {
|
|
|
|
|
match (prev.piece_at(sq), cur.piece_at(sq)) {
|
|
|
|
|
(Some(a), Some(b)) if a == b => {}
|
|
|
|
|
(a, b) => {
|
|
|
|
|
missing.extend(a.map(|p| (p, sq)));
|
|
|
|
|
new.extend(b.map(|p| (p, sq)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let mut slides = Vec::new();
|
|
|
|
|
for (piece, to) in new {
|
|
|
|
|
let nearest = missing
|
|
|
|
|
.iter()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.filter(|(_, (p, _))| *p == piece)
|
|
|
|
|
.min_by_key(|(_, (_, from))| {
|
|
|
|
|
let df = i32::from(from.file()) - i32::from(to.file());
|
|
|
|
|
let dr = i32::from(from.rank()) - i32::from(to.rank());
|
|
|
|
|
df * df + dr * dr
|
|
|
|
|
})
|
|
|
|
|
.map(|(i, _)| i);
|
|
|
|
|
if let Some(i) = nearest {
|
|
|
|
|
let (_, from) = missing.swap_remove(i);
|
|
|
|
|
slides.push((piece, from, to));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Anim {
|
|
|
|
|
start,
|
|
|
|
|
duration,
|
|
|
|
|
slides,
|
|
|
|
|
fadings: missing,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// How much of the way is left: 1 at the start, 0 at the end (eased like chessground).
|
|
|
|
|
fn rest(&self, now: f64) -> Option<f32> {
|
|
|
|
|
let t = 1.0 - (now - self.start) / self.duration;
|
|
|
|
|
(t > 0.0).then(|| {
|
|
|
|
|
let t = t as f32;
|
|
|
|
|
if t < 0.5 {
|
|
|
|
|
4.0 * t * t * t
|
|
|
|
|
} else {
|
|
|
|
|
(t - 1.0) * (2.0 * t - 2.0) * (2.0 * t - 2.0) + 1.0
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-10-18 01:54:06 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct BoardView<'a> {
|
|
|
|
|
pub pos: &'a Chess,
|
|
|
|
|
pub orientation: Color,
|
|
|
|
|
pub last_move: Option<(Square, Square)>,
|
|
|
|
|
pub arrows: &'a [Arrow],
|
|
|
|
|
/// Circled squares (from `[%csl]` comment annotations).
|
|
|
|
|
pub circles: &'a [(Square, Color32)],
|
2026-10-18 14:00:19 +03:00
|
|
|
/// Move assessment shown on the destination square of the last move ("??" in red, …).
|
|
|
|
|
pub badge: Option<Badge>,
|
2026-10-18 01:54:06 +03:00
|
|
|
pub interactive: bool,
|
|
|
|
|
pub show_coords: bool,
|
|
|
|
|
pub theme: BoardTheme,
|
|
|
|
|
pub pieces: PieceSet,
|
2026-10-21 19:57:54 +03:00
|
|
|
/// Seconds a move takes to slide (0: no animation).
|
|
|
|
|
pub animation: f32,
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
/// Not this side's turn, but it may set a premove (a game against the computer).
|
|
|
|
|
pub premove: Option<Color>,
|
|
|
|
|
/// A pawn reaching the last rank becomes a queen without asking.
|
|
|
|
|
pub auto_queen: bool,
|
Paste a line, play on from here, learn the opening
Paste a line from the clipboard (Ctrl+V on a game, the move menu, ☰):
chess-core paste.rs plans where the text fits without touching the game — a
PGN game where its moves part from this one along any branch, or where one of
its positions occurs (another move order, noted); a fragment (SAN, numbered
or not, «…» for black, UCI) after the selected move, instead of it, or at its
move number — and grafts the new moves with their comments, symbols and
variations (the pasted game's alternatives along the way too) as one undo
step, the players' names after the line. A game with no position in common
is refused with a message; a line going elsewhere than the selected move is
asked about; an impossible move inserts nothing.
Play on from here (the move menu, Ctrl+G on a game, ☰): the play dialog shows
the position on a small board and the game it comes from; the new game holds
the moves up to it (Maia sees the history, repetitions count), dimmed during
play, with a comment saying where it comes from; take-backs, abort and resign
count only the new moves; the source game waits under it («⬅ Back to the
game»); the side to move plays by default, a rematch keeps the colour.
Learn the opening up to this move (the move menu, ☰): a training screen over
the game; the user plays both sides; a wrong move is counted, shown for
400 ms in red with a short sound and taken back; from the third mistake on a
move «Show the move» puts an arrow (the run marked with a hint); at the end
the mistakes by move, the line's runs (last, best, clean in a row, recent),
«Again» (Enter). Runs are kept in training.json by the line (start position
and moves), shared between games; an entry or a file that does not read is
kept aside, not lost. New sounds Wrong (Kenney, CC0) and Success (Lichess
Victory, Kenney).
Keys of the game do nothing under a modal question or dialog. Dev hooks
CP_PASTE_LINE, CP_PLAY_FROM, CP_TRAIN, CP_TRAIN_MOVES, CP_TRAIN_KEEP,
CP_TRAINING_FILE. Texts in nine languages.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 14:51:09 +03:00
|
|
|
/// The squares of a wrong move, in red (learning an opening).
|
|
|
|
|
pub alert: Option<(Square, Square)>,
|
2026-10-18 01:54:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BoardView<'_> {
|
|
|
|
|
/// Draws the board into the available square area. Returns a move the user made, if any.
|
|
|
|
|
pub fn show(self, ui: &mut egui::Ui, state: &mut BoardState) -> Option<Move> {
|
|
|
|
|
let avail = ui.available_size();
|
2026-10-26 23:57:45 +03:00
|
|
|
// A board to play on stays usable; one only shown may be a thumbnail.
|
|
|
|
|
let least = if self.interactive || self.premove.is_some() {
|
|
|
|
|
64.0
|
|
|
|
|
} else {
|
|
|
|
|
16.0
|
|
|
|
|
};
|
|
|
|
|
let side = avail.x.min(avail.y).max(least).floor();
|
2026-10-18 01:54:06 +03:00
|
|
|
let (rect, response) = ui.allocate_exact_size(Vec2::splat(side), Sense::click_and_drag());
|
|
|
|
|
let sq_size = side / 8.0;
|
|
|
|
|
let geo = Geometry {
|
|
|
|
|
rect,
|
|
|
|
|
sq: sq_size,
|
|
|
|
|
flipped: self.orientation == Color::Black,
|
|
|
|
|
};
|
|
|
|
|
let painter = ui.painter_at(rect);
|
|
|
|
|
let legal = self.pos.legal_moves();
|
|
|
|
|
let mut result = None;
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
// A premove is set instead of a move: the board takes input for that side.
|
|
|
|
|
let premoving = !self.interactive && self.premove.is_some();
|
|
|
|
|
if !self.interactive && !premoving {
|
|
|
|
|
state.selected = None;
|
|
|
|
|
state.drag = None;
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
|
|
|
|
|
// --- input -----------------------------------------------------------------------
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
if (self.interactive || premoving) && state.promotion.is_none() {
|
2026-10-18 01:54:06 +03:00
|
|
|
let pointer = response.interact_pointer_pos();
|
|
|
|
|
if response.drag_started() {
|
|
|
|
|
let origin = ui.input(|i| i.pointer.press_origin()).or(pointer);
|
|
|
|
|
if let Some(sq) = origin.and_then(|p| geo.square_at(p))
|
|
|
|
|
&& self.own_piece(sq)
|
|
|
|
|
{
|
2026-10-21 19:57:54 +03:00
|
|
|
// The piece is taken from where it stands now, not from mid-flight.
|
|
|
|
|
state.anim = None;
|
2026-10-18 01:54:06 +03:00
|
|
|
state.drag = Some(sq);
|
|
|
|
|
state.selected = Some(sq);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if response.drag_stopped() {
|
|
|
|
|
if let Some(from) = state.drag.take() {
|
|
|
|
|
let target = ui
|
|
|
|
|
.input(|i| i.pointer.latest_pos())
|
|
|
|
|
.and_then(|p| geo.square_at(p));
|
|
|
|
|
if let Some(to) = target.filter(|&t| t != from) {
|
|
|
|
|
result = self.try_move(state, &legal, from, to);
|
2026-10-21 19:57:54 +03:00
|
|
|
state.dropped = result.is_some().then_some(from);
|
2026-10-18 01:54:06 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if response.clicked()
|
|
|
|
|
&& let Some(sq) = pointer.and_then(|p| geo.square_at(p))
|
|
|
|
|
{
|
|
|
|
|
match state.selected {
|
|
|
|
|
// try_move also covers castling by clicking the own rook,
|
|
|
|
|
// and reselects when the target is just another own piece.
|
|
|
|
|
Some(from) if from != sq => {
|
|
|
|
|
result = self.try_move(state, &legal, from, sq);
|
|
|
|
|
}
|
|
|
|
|
Some(from) if from == sq => state.selected = None,
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
_ => {
|
|
|
|
|
state.selected = self.own_piece(sq).then_some(sq);
|
|
|
|
|
// A click beside the own pieces cancels a premove.
|
|
|
|
|
if premoving && state.selected.is_none() {
|
|
|
|
|
state.premove = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
}
|
|
|
|
|
}
|
User comments: an editor in the notation, symbols, arrows on the board
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>
2026-10-25 02:35:54 +03:00
|
|
|
// 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),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
if response.secondary_clicked() {
|
|
|
|
|
state.clear();
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
state.premove = None;
|
User comments: an editor in the notation, symbols, arrows on the board
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>
2026-10-25 02:35:54 +03:00
|
|
|
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));
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 19:57:54 +03:00
|
|
|
// --- animation ---------------------------------------------------------------------
|
|
|
|
|
let now = ui.input(|i| i.time);
|
|
|
|
|
let board = self.pos.board();
|
|
|
|
|
if state.shown.as_ref() != Some(board) {
|
|
|
|
|
state.anim = None;
|
|
|
|
|
if let Some(prev) = &state.shown
|
|
|
|
|
&& self.animation > 0.0
|
|
|
|
|
{
|
|
|
|
|
let mut anim = Anim::new(prev, board, now, f64::from(self.animation));
|
|
|
|
|
if let Some(from) = state.dropped {
|
|
|
|
|
anim.slides.retain(|s| s.1 != from);
|
|
|
|
|
}
|
|
|
|
|
state.anim = Some(anim);
|
|
|
|
|
}
|
|
|
|
|
state.dropped = None;
|
|
|
|
|
state.shown = Some(board.clone());
|
|
|
|
|
}
|
|
|
|
|
let rest = state.anim.as_ref().and_then(|a| a.rest(now));
|
|
|
|
|
if rest.is_none() {
|
|
|
|
|
state.anim = None;
|
|
|
|
|
} else {
|
|
|
|
|
ui.ctx().request_repaint();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 01:54:06 +03:00
|
|
|
// --- squares -----------------------------------------------------------------------
|
|
|
|
|
if let Some(uri) = self.theme.texture {
|
|
|
|
|
egui::Image::new(uri).paint_at(ui, rect);
|
|
|
|
|
}
|
|
|
|
|
for sq in Square::ALL
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter(|_| self.theme.texture.is_none())
|
|
|
|
|
{
|
|
|
|
|
let light = (u32::from(sq.file()) + u32::from(sq.rank())) % 2 == 1;
|
|
|
|
|
let fill = if light {
|
|
|
|
|
self.theme.light
|
|
|
|
|
} else {
|
|
|
|
|
self.theme.dark
|
|
|
|
|
};
|
|
|
|
|
painter.rect_filled(geo.square_rect(sq), 0.0, fill);
|
|
|
|
|
}
|
|
|
|
|
let hl = |painter: &egui::Painter, sq: Square, c: Color32| {
|
|
|
|
|
painter.rect_filled(geo.square_rect(sq), 0.0, c);
|
|
|
|
|
};
|
|
|
|
|
if let Some((from, to)) = self.last_move {
|
|
|
|
|
hl(&painter, from, self.theme.last_move);
|
|
|
|
|
hl(&painter, to, self.theme.last_move);
|
|
|
|
|
}
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
if let Some((from, to)) = state.premove.filter(|_| premoving) {
|
|
|
|
|
hl(&painter, from, PREMOVE);
|
|
|
|
|
hl(&painter, to, PREMOVE);
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
if let Some(sel) = state.selected {
|
|
|
|
|
hl(&painter, sel, self.theme.selected);
|
|
|
|
|
}
|
Paste a line, play on from here, learn the opening
Paste a line from the clipboard (Ctrl+V on a game, the move menu, ☰):
chess-core paste.rs plans where the text fits without touching the game — a
PGN game where its moves part from this one along any branch, or where one of
its positions occurs (another move order, noted); a fragment (SAN, numbered
or not, «…» for black, UCI) after the selected move, instead of it, or at its
move number — and grafts the new moves with their comments, symbols and
variations (the pasted game's alternatives along the way too) as one undo
step, the players' names after the line. A game with no position in common
is refused with a message; a line going elsewhere than the selected move is
asked about; an impossible move inserts nothing.
Play on from here (the move menu, Ctrl+G on a game, ☰): the play dialog shows
the position on a small board and the game it comes from; the new game holds
the moves up to it (Maia sees the history, repetitions count), dimmed during
play, with a comment saying where it comes from; take-backs, abort and resign
count only the new moves; the source game waits under it («⬅ Back to the
game»); the side to move plays by default, a rematch keeps the colour.
Learn the opening up to this move (the move menu, ☰): a training screen over
the game; the user plays both sides; a wrong move is counted, shown for
400 ms in red with a short sound and taken back; from the third mistake on a
move «Show the move» puts an arrow (the run marked with a hint); at the end
the mistakes by move, the line's runs (last, best, clean in a row, recent),
«Again» (Enter). Runs are kept in training.json by the line (start position
and moves), shared between games; an entry or a file that does not read is
kept aside, not lost. New sounds Wrong (Kenney, CC0) and Success (Lichess
Victory, Kenney).
Keys of the game do nothing under a modal question or dialog. Dev hooks
CP_PASTE_LINE, CP_PLAY_FROM, CP_TRAIN, CP_TRAIN_MOVES, CP_TRAIN_KEEP,
CP_TRAINING_FILE. Texts in nine languages.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 14:51:09 +03:00
|
|
|
if let Some((from, to)) = self.alert {
|
|
|
|
|
let red = ui.visuals().error_fg_color.gamma_multiply(0.45);
|
|
|
|
|
hl(&painter, from, red);
|
|
|
|
|
hl(&painter, to, red);
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
if self.pos.is_check()
|
|
|
|
|
&& let Some(k) = self.pos.board().king_of(self.pos.turn())
|
|
|
|
|
{
|
|
|
|
|
let r = geo.square_rect(k);
|
|
|
|
|
painter.circle_filled(
|
|
|
|
|
r.center(),
|
|
|
|
|
sq_size * 0.48,
|
|
|
|
|
Color32::from_rgba_unmultiplied(230, 40, 40, 150),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.show_coords {
|
|
|
|
|
self.draw_coords(&painter, &geo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- pieces --------------------------------------------------------------------------
|
2026-10-21 19:57:54 +03:00
|
|
|
let anim = state.anim.as_ref().zip(rest);
|
|
|
|
|
if let Some((a, rest)) = anim {
|
|
|
|
|
for &(piece, sq) in &a.fadings {
|
|
|
|
|
let r = geo.square_rect(sq).shrink(sq_size * 0.02);
|
|
|
|
|
self.pieces
|
|
|
|
|
.paint_tinted(ui, piece, r, Color32::WHITE.gamma_multiply(rest));
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
for (sq, piece) in self.pos.board().clone() {
|
|
|
|
|
if state.drag == Some(sq) && response.dragged() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-10-21 19:57:54 +03:00
|
|
|
if anim.is_some_and(|(a, _)| a.slides.iter().any(|s| s.2 == sq)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
self.pieces
|
|
|
|
|
.paint(ui, piece, geo.square_rect(sq).shrink(sq_size * 0.02));
|
|
|
|
|
}
|
2026-10-21 19:57:54 +03:00
|
|
|
// Moving pieces over the others.
|
|
|
|
|
if let Some((a, rest)) = anim {
|
|
|
|
|
for &(piece, from, to) in &a.slides {
|
|
|
|
|
let r = geo.square_rect(to);
|
|
|
|
|
let offset = (geo.square_rect(from).center() - r.center()) * rest;
|
|
|
|
|
self.pieces
|
|
|
|
|
.paint(ui, piece, r.translate(offset).shrink(sq_size * 0.02));
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
// where the selected piece may premove to
|
|
|
|
|
if let Some(sel) = state.selected.filter(|_| premoving)
|
|
|
|
|
&& let Some(piece) = self.pos.board().piece_at(sel)
|
|
|
|
|
{
|
|
|
|
|
for to in premove_targets(self.pos.board(), sel, piece) {
|
|
|
|
|
let r = geo.square_rect(to);
|
|
|
|
|
painter.circle_filled(r.center(), sq_size * 0.15, PREMOVE_DOT);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
// legal targets of the selected piece
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
if let Some(sel) = state.selected.filter(|_| !premoving) {
|
2026-10-18 01:54:06 +03:00
|
|
|
for m in legal.iter().filter(|m| m.from() == Some(sel)) {
|
|
|
|
|
let to = ui_target(m);
|
|
|
|
|
let r = geo.square_rect(to);
|
|
|
|
|
if self.pos.board().piece_at(to).is_some() && !m.is_castle() {
|
|
|
|
|
painter.circle_stroke(
|
|
|
|
|
r.center(),
|
|
|
|
|
sq_size * 0.44,
|
|
|
|
|
Stroke::new(sq_size * 0.07, self.theme.dot),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
painter.circle_filled(r.center(), sq_size * 0.15, self.theme.dot);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (sq, c) in self.circles {
|
|
|
|
|
let r = geo.square_rect(*sq);
|
|
|
|
|
painter.circle_stroke(r.center(), sq_size * 0.46, Stroke::new(sq_size * 0.07, *c));
|
|
|
|
|
}
|
|
|
|
|
for a in self.arrows {
|
|
|
|
|
draw_arrow(&painter, &geo, a);
|
|
|
|
|
}
|
User comments: an editor in the notation, symbols, arrows on the board
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>
2026-10-25 02:35:54 +03:00
|
|
|
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),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-18 14:00:19 +03:00
|
|
|
if let Some(b) = self.badge {
|
|
|
|
|
let r = geo.square_rect(b.square);
|
|
|
|
|
let radius = sq_size * 0.19;
|
|
|
|
|
// Over the corner of the square, kept inside the board.
|
|
|
|
|
let c = egui::pos2(
|
|
|
|
|
(r.right() - radius * 0.35).min(rect.right() - radius - 1.0),
|
|
|
|
|
(r.top() + radius * 0.35).max(rect.top() + radius + 1.0),
|
|
|
|
|
);
|
|
|
|
|
painter.circle_filled(
|
|
|
|
|
c + vec2(0.0, sq_size * 0.015),
|
|
|
|
|
radius,
|
|
|
|
|
Color32::from_black_alpha(90),
|
|
|
|
|
);
|
|
|
|
|
painter.circle(
|
|
|
|
|
c,
|
|
|
|
|
radius,
|
|
|
|
|
b.color,
|
|
|
|
|
Stroke::new(sq_size * 0.02, Color32::WHITE),
|
|
|
|
|
);
|
|
|
|
|
painter.text(
|
|
|
|
|
c,
|
|
|
|
|
egui::Align2::CENTER_CENTER,
|
|
|
|
|
b.text,
|
2026-10-28 02:59:14 +03:00
|
|
|
crate::assets::bold(radius * 1.05),
|
2026-10-18 14:00:19 +03:00
|
|
|
Color32::WHITE,
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
|
|
|
|
|
// dragged piece follows the cursor
|
|
|
|
|
if let (Some(from), true) = (state.drag, response.dragged())
|
|
|
|
|
&& let (Some(piece), Some(p)) = (
|
|
|
|
|
self.pos.board().piece_at(from),
|
|
|
|
|
ui.input(|i| i.pointer.latest_pos()),
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
let r = Rect::from_center_size(p, Vec2::splat(sq_size * 1.1));
|
|
|
|
|
self.pieces.paint(ui, piece, r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- promotion picker ---------------------------------------------------------------
|
|
|
|
|
if let Some((from, to)) = state.promotion {
|
|
|
|
|
painter.rect_filled(rect, 0.0, Color32::from_black_alpha(140));
|
|
|
|
|
let color = self.pos.turn();
|
|
|
|
|
let roles = [Role::Queen, Role::Knight, Role::Rook, Role::Bishop];
|
|
|
|
|
let top_down = geo.square_rect(to).center().y < rect.center().y;
|
|
|
|
|
for (i, role) in roles.iter().enumerate() {
|
|
|
|
|
let off = if top_down { i as f32 } else { -(i as f32) };
|
|
|
|
|
let r = geo.square_rect(to).translate(vec2(0.0, off * sq_size));
|
|
|
|
|
let hover = ui.rect_contains_pointer(r);
|
|
|
|
|
painter.circle_filled(
|
|
|
|
|
r.center(),
|
|
|
|
|
sq_size * 0.5,
|
|
|
|
|
if hover {
|
|
|
|
|
Color32::from_gray(240)
|
|
|
|
|
} else {
|
|
|
|
|
Color32::from_gray(190)
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
self.pieces
|
|
|
|
|
.paint(ui, Piece { color, role: *role }, r.shrink(sq_size * 0.08));
|
|
|
|
|
if hover && ui.input(|i| i.pointer.primary_clicked()) {
|
|
|
|
|
result = legal
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|m| {
|
|
|
|
|
m.from() == Some(from) && m.to() == to && m.promotion() == Some(*role)
|
|
|
|
|
})
|
|
|
|
|
.cloned();
|
|
|
|
|
state.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if result.is_none()
|
|
|
|
|
&& ui.input(|i| i.key_pressed(egui::Key::Escape) || i.pointer.secondary_clicked())
|
|
|
|
|
{
|
|
|
|
|
state.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if result.is_some() {
|
|
|
|
|
state.clear();
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
/// The side that moves on this board now: the side to move, or the premoving one.
|
|
|
|
|
fn mover(&self) -> Color {
|
|
|
|
|
match self.premove {
|
|
|
|
|
Some(c) if !self.interactive => c,
|
|
|
|
|
_ => self.pos.turn(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 01:54:06 +03:00
|
|
|
fn own_piece(&self, sq: Square) -> bool {
|
|
|
|
|
self.pos
|
|
|
|
|
.board()
|
|
|
|
|
.piece_at(sq)
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
.is_some_and(|p| p.color == self.mover())
|
2026-10-18 01:54:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_move(
|
|
|
|
|
&self,
|
|
|
|
|
state: &mut BoardState,
|
|
|
|
|
legal: &shakmaty::MoveList,
|
|
|
|
|
from: Square,
|
|
|
|
|
to: Square,
|
|
|
|
|
) -> Option<Move> {
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
if !self.interactive && self.premove.is_some() {
|
|
|
|
|
let piece = self.pos.board().piece_at(from)?;
|
|
|
|
|
if premove_targets(self.pos.board(), from, piece).contains(to) {
|
|
|
|
|
state.premove = Some((from, to));
|
|
|
|
|
state.selected = None;
|
|
|
|
|
} else {
|
|
|
|
|
state.selected = self.own_piece(to).then_some(to);
|
|
|
|
|
if state.selected.is_none() {
|
|
|
|
|
state.premove = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2026-10-18 01:54:06 +03:00
|
|
|
let candidates: Vec<&Move> = legal
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|m| m.from() == Some(from) && (ui_target(m) == to || m.to() == to))
|
|
|
|
|
.collect();
|
|
|
|
|
match candidates.as_slice() {
|
|
|
|
|
[] => {
|
|
|
|
|
state.selected = self.own_piece(to).then_some(to);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
[m] => Some(*(*m)),
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
_ if self.auto_queen && candidates.iter().all(|m| m.is_promotion()) => candidates
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|m| m.promotion() == Some(Role::Queen))
|
|
|
|
|
.map(|m| *(*m)),
|
2026-10-18 01:54:06 +03:00
|
|
|
_ if candidates.iter().all(|m| m.is_promotion()) => {
|
|
|
|
|
state.promotion = Some((from, to));
|
|
|
|
|
state.selected = None;
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
_ => candidates.first().map(|m| *(*m)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw_coords(&self, painter: &egui::Painter, geo: &Geometry) {
|
|
|
|
|
let font = FontId::proportional((geo.sq * 0.17).max(9.0));
|
|
|
|
|
for i in 0..8u32 {
|
|
|
|
|
let file = File::new(i);
|
|
|
|
|
let rank = Rank::new(i);
|
|
|
|
|
let bottom_rank = if geo.flipped {
|
|
|
|
|
Rank::Eighth
|
|
|
|
|
} else {
|
|
|
|
|
Rank::First
|
|
|
|
|
};
|
|
|
|
|
let left_file = if geo.flipped { File::H } else { File::A };
|
|
|
|
|
let fsq = Square::from_coords(file, bottom_rank);
|
|
|
|
|
let rsq = Square::from_coords(left_file, rank);
|
|
|
|
|
let color_for = |sq: Square| {
|
|
|
|
|
let light = (u32::from(sq.file()) + u32::from(sq.rank())) % 2 == 1;
|
|
|
|
|
if light {
|
|
|
|
|
self.theme.dark
|
|
|
|
|
} else {
|
|
|
|
|
self.theme.light
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let fr = geo.square_rect(fsq);
|
|
|
|
|
painter.text(
|
|
|
|
|
fr.right_bottom() + vec2(-geo.sq * 0.06, -geo.sq * 0.02),
|
|
|
|
|
Align2::RIGHT_BOTTOM,
|
|
|
|
|
file.char(),
|
|
|
|
|
font.clone(),
|
|
|
|
|
color_for(fsq),
|
|
|
|
|
);
|
|
|
|
|
let rr = geo.square_rect(rsq);
|
|
|
|
|
painter.text(
|
|
|
|
|
rr.left_top() + vec2(geo.sq * 0.05, geo.sq * 0.03),
|
|
|
|
|
Align2::LEFT_TOP,
|
|
|
|
|
rank.char(),
|
|
|
|
|
font.clone(),
|
|
|
|
|
color_for(rsq),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
/// The premove highlight (Lichess's dark blue) and its target dots.
|
|
|
|
|
const PREMOVE: Color32 = Color32::from_rgba_premultiplied(10, 15, 43, 128);
|
|
|
|
|
const PREMOVE_DOT: Color32 = Color32::from_rgba_premultiplied(10, 15, 43, 140);
|
|
|
|
|
|
|
|
|
|
/// Where a piece may premove: how it moves on an empty board (the position will have
|
|
|
|
|
/// changed), a pawn forward and diagonally, the king at home also to castle with an own
|
|
|
|
|
/// rook of `board` on the a- or h-file — on the king's square or the rook's (chessground
|
|
|
|
|
/// `premove.ts`).
|
|
|
|
|
pub fn premove_targets(board: &Board, from: Square, piece: Piece) -> shakmaty::Bitboard {
|
|
|
|
|
use shakmaty::{Bitboard, attacks};
|
|
|
|
|
match piece.role {
|
|
|
|
|
Role::Pawn => {
|
|
|
|
|
let mut b = attacks::pawn_attacks(piece.color, from);
|
|
|
|
|
let step: i32 = if piece.color == Color::White { 8 } else { -8 };
|
|
|
|
|
let home = if piece.color == Color::White {
|
|
|
|
|
Rank::Second
|
|
|
|
|
} else {
|
|
|
|
|
Rank::Seventh
|
|
|
|
|
};
|
|
|
|
|
if let Some(one) = from.offset(step) {
|
|
|
|
|
b.add(one);
|
|
|
|
|
if from.rank() == home
|
|
|
|
|
&& let Some(two) = from.offset(2 * step)
|
|
|
|
|
{
|
|
|
|
|
b.add(two);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
b
|
|
|
|
|
}
|
|
|
|
|
Role::King => {
|
|
|
|
|
let mut b = attacks::king_attacks(from);
|
|
|
|
|
let back = piece.color.backrank();
|
|
|
|
|
let rook = Piece {
|
|
|
|
|
color: piece.color,
|
|
|
|
|
role: Role::Rook,
|
|
|
|
|
};
|
|
|
|
|
if from == Square::from_coords(File::E, back) {
|
|
|
|
|
for (rook_file, king_file) in [(File::H, File::G), (File::A, File::C)] {
|
|
|
|
|
let r = Square::from_coords(rook_file, back);
|
|
|
|
|
if board.piece_at(r) == Some(rook) {
|
|
|
|
|
b.add(Square::from_coords(king_file, back));
|
|
|
|
|
b.add(r);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
b
|
|
|
|
|
}
|
|
|
|
|
_ => attacks::attacks(from, piece, Bitboard::EMPTY),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The move a premove means in `pos`, if it is legal there (a pawn promotes to a queen).
|
|
|
|
|
pub fn premove_move(pos: &Chess, from: Square, to: Square) -> Option<Move> {
|
|
|
|
|
let legal = pos.legal_moves();
|
|
|
|
|
let mut found = legal
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|m| m.from() == Some(from) && (ui_target(m) == to || m.to() == to));
|
|
|
|
|
let first = *found.next()?;
|
|
|
|
|
if first.is_promotion() {
|
|
|
|
|
return legal
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|m| m.from() == Some(from) && m.to() == to && m.promotion() == Some(Role::Queen))
|
|
|
|
|
.copied();
|
|
|
|
|
}
|
|
|
|
|
Some(first)
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 01:54:06 +03:00
|
|
|
/// Square the user clicks to make a move: for castling, the king's destination.
|
|
|
|
|
pub fn ui_target(m: &Move) -> Square {
|
|
|
|
|
match (m, m.castling_side()) {
|
|
|
|
|
(Move::Castle { king, .. }, Some(side)) => side.king_to(if king.rank() == Rank::First {
|
|
|
|
|
Color::White
|
|
|
|
|
} else {
|
|
|
|
|
Color::Black
|
|
|
|
|
}),
|
|
|
|
|
_ => m.to(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Geometry {
|
|
|
|
|
rect: Rect,
|
|
|
|
|
sq: f32,
|
|
|
|
|
flipped: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Geometry {
|
|
|
|
|
fn square_rect(&self, sq: Square) -> Rect {
|
|
|
|
|
let (f, r) = (u32::from(sq.file()) as f32, u32::from(sq.rank()) as f32);
|
|
|
|
|
let (x, y) = if self.flipped {
|
|
|
|
|
(7.0 - f, r)
|
|
|
|
|
} else {
|
|
|
|
|
(f, 7.0 - r)
|
|
|
|
|
};
|
|
|
|
|
Rect::from_min_size(
|
|
|
|
|
self.rect.min + vec2(x * self.sq, y * self.sq),
|
|
|
|
|
Vec2::splat(self.sq),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn center(&self, sq: Square) -> Pos2 {
|
|
|
|
|
self.square_rect(sq).center()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn square_at(&self, p: Pos2) -> Option<Square> {
|
|
|
|
|
if !self.rect.contains(p) {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
let x = ((p.x - self.rect.min.x) / self.sq).floor().clamp(0.0, 7.0) as u32;
|
|
|
|
|
let y = ((p.y - self.rect.min.y) / self.sq).floor().clamp(0.0, 7.0) as u32;
|
|
|
|
|
let (f, r) = if self.flipped { (7 - x, y) } else { (x, 7 - y) };
|
|
|
|
|
Some(Square::from_coords(File::new(f), Rank::new(r)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
User comments: an editor in the notation, symbols, arrows on the board
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>
2026-10-25 02:35:54 +03:00
|
|
|
/// 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),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 01:54:06 +03:00
|
|
|
fn draw_arrow(painter: &egui::Painter, geo: &Geometry, a: &Arrow) {
|
|
|
|
|
let from = geo.center(a.from);
|
|
|
|
|
let to = geo.center(a.to);
|
|
|
|
|
let dir = (to - from).normalized();
|
|
|
|
|
if !dir.x.is_finite() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let width = geo.sq * 0.16 * a.weight;
|
|
|
|
|
let head_len = geo.sq * 0.42 * a.weight.sqrt();
|
|
|
|
|
let head_w = geo.sq * 0.38 * a.weight.sqrt();
|
|
|
|
|
let tip = to - dir * geo.sq * 0.12;
|
|
|
|
|
let base = tip - dir * head_len;
|
|
|
|
|
let start = from + dir * geo.sq * 0.2;
|
|
|
|
|
let normal = vec2(-dir.y, dir.x);
|
|
|
|
|
painter.line_segment([start, base], Stroke::new(width, a.color));
|
|
|
|
|
painter.add(Shape::convex_polygon(
|
|
|
|
|
vec![tip, base + normal * head_w, base - normal * head_w],
|
|
|
|
|
a.color,
|
|
|
|
|
Stroke::NONE,
|
|
|
|
|
));
|
|
|
|
|
}
|
2026-10-21 19:57:54 +03:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
Premoves, the auto-queen setting, the play dialog redone
Premoves in the game against Maia, as on Lichess: while she thinks, a move of
the user's piece by its pattern on an empty board (a pawn forward and both
diagonals, sliders through pieces, the king at home to castle with an own rook,
by the king's square or the rook's) is set and shown in dark blue; after her
move it is played at once when legal (a promotion as a queen), else dropped.
A right click or a click beside the own pieces cancels it; a take-back, the
leave question and the end drop it. A piece in hand stays there when Maia
moves, unless she took it.
Settings › Board: «Always promote to a queen», off by default — then the
piece is chosen, as before.
The dialog «Play against the computer» redone after the owner's review: You,
Opponent (the strength in the title, the slider edge to edge with 600/2600,
Weaker · As me · Stronger lit when they match, or a line asking for the
rating), Colour, Time; every choice a framed tile of one height, the tiles of a
row sharing its width; time 3+0…30+20 without category captions, Custom with
aligned sliders, Unlimited (no clocks) as a double tile; a solid scroll bar
in a low window, where the introduction is left out. Dev hook CP_PREMOVE.
Adjourning a game recorded as a task.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 12:12:35 +03:00
|
|
|
#[test]
|
|
|
|
|
fn premoves_as_on_lichess() {
|
|
|
|
|
use shakmaty::fen::Fen;
|
|
|
|
|
let sq = |s: &str| s.parse::<Square>().unwrap();
|
|
|
|
|
let wp = Piece {
|
|
|
|
|
color: Color::White,
|
|
|
|
|
role: Role::Pawn,
|
|
|
|
|
};
|
|
|
|
|
// A pawn: forward one or two from home, both diagonals.
|
|
|
|
|
let start = Board::default();
|
|
|
|
|
let t = premove_targets(&start, sq("e2"), wp);
|
|
|
|
|
for s in ["e3", "e4", "d3", "f3"] {
|
|
|
|
|
assert!(t.contains(sq(s)), "{s}");
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(t.count(), 4);
|
|
|
|
|
// A rook through pieces: the position will change.
|
|
|
|
|
let wr = Piece {
|
|
|
|
|
color: Color::White,
|
|
|
|
|
role: Role::Rook,
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(premove_targets(&start, sq("a1"), wr).count(), 14);
|
|
|
|
|
// The king at home: also its castling squares.
|
|
|
|
|
let wk = Piece {
|
|
|
|
|
color: Color::White,
|
|
|
|
|
role: Role::King,
|
|
|
|
|
};
|
|
|
|
|
// The king at home: castling by its square or the rook's, only with the rook.
|
|
|
|
|
let k = premove_targets(&start, sq("e1"), wk);
|
|
|
|
|
assert!(k.contains(sq("g1")) && k.contains(sq("h1")) && k.contains(sq("c1")));
|
|
|
|
|
let no_h_rook: Board = "4k3/8/8/8/8/8/8/R3K3".parse().unwrap();
|
|
|
|
|
let k = premove_targets(&no_h_rook, sq("e1"), wk);
|
|
|
|
|
assert!(!k.contains(sq("g1")) && k.contains(sq("c1")) && k.contains(sq("a1")));
|
|
|
|
|
// Played when legal; a promotion becomes a queen; castling by the king's square.
|
|
|
|
|
let pos: Chess = "4k3/1P6/8/8/8/8/8/4K2R w K - 0 1"
|
|
|
|
|
.parse::<Fen>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.into_position(shakmaty::CastlingMode::Standard)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let m = premove_move(&pos, sq("b7"), sq("b8")).unwrap();
|
|
|
|
|
assert_eq!(m.promotion(), Some(Role::Queen));
|
|
|
|
|
assert!(premove_move(&pos, sq("e1"), sq("g1")).unwrap().is_castle());
|
|
|
|
|
assert!(premove_move(&pos, sq("e1"), sq("h1")).unwrap().is_castle());
|
|
|
|
|
assert!(premove_move(&pos, sq("h1"), sq("h8")).is_some());
|
|
|
|
|
assert!(premove_move(&pos, sq("b7"), sq("c8")).is_none());
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 19:57:54 +03:00
|
|
|
use super::*;
|
|
|
|
|
use shakmaty::fen::Fen;
|
|
|
|
|
use shakmaty::san::San;
|
|
|
|
|
use shakmaty::{CastlingMode, Chess};
|
|
|
|
|
|
|
|
|
|
fn after(fen: &str, moves: &str) -> (Board, Board) {
|
|
|
|
|
let pos: Chess = fen
|
|
|
|
|
.parse::<Fen>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.into_position(CastlingMode::Standard)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let mut next = pos.clone();
|
|
|
|
|
for san in moves.split_whitespace() {
|
|
|
|
|
let m = san.parse::<San>().unwrap().to_move(&next).unwrap();
|
|
|
|
|
next.play_unchecked(m);
|
|
|
|
|
}
|
|
|
|
|
(pos.board().clone(), next.board().clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn plan(fen: &str, moves: &str) -> (Vec<String>, Vec<String>) {
|
|
|
|
|
let (a, b) = after(fen, moves);
|
|
|
|
|
let anim = Anim::new(&a, &b, 0.0, 0.25);
|
|
|
|
|
let mut slides: Vec<String> = anim
|
|
|
|
|
.slides
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(p, f, t)| format!("{}{f}{t}", p.char()))
|
|
|
|
|
.collect();
|
|
|
|
|
let mut fadings: Vec<String> = anim
|
|
|
|
|
.fadings
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(p, s)| format!("{}{s}", p.char()))
|
|
|
|
|
.collect();
|
|
|
|
|
slides.sort();
|
|
|
|
|
fadings.sort();
|
|
|
|
|
(slides, fadings)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const START: &str = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_move_slides_one_piece() {
|
|
|
|
|
assert_eq!(plan(START, "e4"), (vec!["Pe2e4".into()], vec![]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_captured_piece_fades() {
|
|
|
|
|
let fen = "rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
plan(fen, "exd5"),
|
|
|
|
|
(vec!["Pe4d5".into()], vec!["pd5".into()])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn castling_slides_king_and_rook() {
|
|
|
|
|
let fen = "r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
plan(fen, "O-O"),
|
|
|
|
|
(vec!["Ke1g1".into(), "Rh1f1".into()], vec![])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_promoted_pawn_fades_and_the_queen_appears() {
|
|
|
|
|
let fen = "8/4P3/8/8/8/8/k7/4K3 w - - 0 1";
|
|
|
|
|
assert_eq!(plan(fen, "e8=Q"), (vec![], vec!["Pe7".into()]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn en_passant_fades_the_pawn_beside() {
|
|
|
|
|
let fen = "4k3/8/8/3pP3/8/8/8/4K3 w - d6 0 2";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
plan(fen, "exd6"),
|
|
|
|
|
(vec!["Pe5d6".into()], vec!["pd5".into()])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_jump_over_several_moves_slides_each_piece_once() {
|
|
|
|
|
let (slides, fadings) = plan(START, "e4 e5 Nf3 Nc6");
|
|
|
|
|
assert_eq!(
|
|
|
|
|
slides,
|
|
|
|
|
vec!["Ng1f3", "Pe2e4", "nb8c6", "pe7e5"]
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(String::from)
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
);
|
|
|
|
|
assert!(fadings.is_empty());
|
|
|
|
|
}
|
|
|
|
|
}
|