Piece animation, as on Lichess
A move slides with chessground's easing: each piece of the new position slides from the nearest square that held the same piece, a piece left over fades out, so castling moves king and rook and a jump over several moves slides each piece once. Settings → Board: Off, Fast 120 ms, Normal 250 ms (default), Slow 500 ms. A piece dropped by dragging does not slide (the castling rook still does); picking up a piece in flight takes it from its square. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c7a8f0d453
commit
d250ddf2cc
9 changed files with 284 additions and 2 deletions
|
|
@ -2,6 +2,13 @@
|
|||
|
||||
All notable changes are listed here. Versions follow [Semantic Versioning](https://semver.org/).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
- Piece animation, as on Lichess: a move slides on the board (castling moves both king and
|
||||
rook; jumping several moves slides every piece once), a captured piece fades out. Speed in
|
||||
Settings → Board: Off, Fast (120 ms), Normal (250 ms, the default), Slow (500 ms). A piece
|
||||
dropped by dragging is already in place and does not slide.
|
||||
|
||||
## [0.8.2] — 2026-10-21
|
||||
|
||||
- Windows: ChessPenguin no longer quits silently on a machine whose graphics do not
|
||||
|
|
|
|||
|
|
@ -134,6 +134,12 @@ impl PieceSet {
|
|||
pub fn paint(self, ui: &egui::Ui, piece: Piece, rect: Rect) {
|
||||
egui::Image::new(self.uri(piece)).paint_at(ui, rect);
|
||||
}
|
||||
|
||||
pub fn paint_tinted(self, ui: &egui::Ui, piece: Piece, rect: Rect, tint: egui::Color32) {
|
||||
egui::Image::new(self.uri(piece))
|
||||
.tint(tint)
|
||||
.paint_at(ui, rect);
|
||||
}
|
||||
}
|
||||
|
||||
/// Registers all bundled SVGs with egui's byte loader.
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
//! Chessboard widget: drawing, click/drag move input, promotion picker, arrows.
|
||||
|
||||
use egui::{Align2, Color32, FontId, Pos2, Rect, Sense, Shape, Stroke, Vec2, vec2};
|
||||
use shakmaty::{Chess, Color, File, Move, Piece, Position, Rank, Role, Square};
|
||||
use shakmaty::{Board, Chess, Color, File, Move, Piece, Position, Rank, Role, Square};
|
||||
|
||||
use crate::assets::{BoardTheme, PieceSet};
|
||||
|
||||
|
|
@ -28,11 +28,82 @@ pub struct BoardState {
|
|||
selected: Option<Square>,
|
||||
drag: Option<Square>,
|
||||
promotion: Option<(Square, Square)>,
|
||||
/// 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>,
|
||||
}
|
||||
|
||||
impl BoardState {
|
||||
/// Forgets the selection, the drag and the promotion choice.
|
||||
pub fn clear(&mut self) {
|
||||
*self = Self::default();
|
||||
self.selected = None;
|
||||
self.drag = None;
|
||||
self.promotion = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -49,6 +120,8 @@ pub struct BoardView<'a> {
|
|||
pub show_coords: bool,
|
||||
pub theme: BoardTheme,
|
||||
pub pieces: PieceSet,
|
||||
/// Seconds a move takes to slide (0: no animation).
|
||||
pub animation: f32,
|
||||
}
|
||||
|
||||
impl BoardView<'_> {
|
||||
|
|
@ -75,6 +148,8 @@ impl BoardView<'_> {
|
|||
if let Some(sq) = origin.and_then(|p| geo.square_at(p))
|
||||
&& self.own_piece(sq)
|
||||
{
|
||||
// The piece is taken from where it stands now, not from mid-flight.
|
||||
state.anim = None;
|
||||
state.drag = Some(sq);
|
||||
state.selected = Some(sq);
|
||||
}
|
||||
|
|
@ -86,6 +161,7 @@ impl BoardView<'_> {
|
|||
.and_then(|p| geo.square_at(p));
|
||||
if let Some(to) = target.filter(|&t| t != from) {
|
||||
result = self.try_move(state, &legal, from, to);
|
||||
state.dropped = result.is_some().then_some(from);
|
||||
}
|
||||
}
|
||||
} else if response.clicked()
|
||||
|
|
@ -106,6 +182,30 @@ impl BoardView<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
// --- 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();
|
||||
}
|
||||
|
||||
// --- squares -----------------------------------------------------------------------
|
||||
if let Some(uri) = self.theme.texture {
|
||||
egui::Image::new(uri).paint_at(ui, rect);
|
||||
|
|
@ -148,13 +248,33 @@ impl BoardView<'_> {
|
|||
}
|
||||
|
||||
// --- pieces --------------------------------------------------------------------------
|
||||
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));
|
||||
}
|
||||
}
|
||||
for (sq, piece) in self.pos.board().clone() {
|
||||
if state.drag == Some(sq) && response.dragged() {
|
||||
continue;
|
||||
}
|
||||
if anim.is_some_and(|(a, _)| a.slides.iter().any(|s| s.2 == sq)) {
|
||||
continue;
|
||||
}
|
||||
self.pieces
|
||||
.paint(ui, piece, geo.square_rect(sq).shrink(sq_size * 0.02));
|
||||
}
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
|
||||
// legal targets of the selected piece
|
||||
if let Some(sel) = state.selected {
|
||||
|
|
@ -405,3 +525,96 @@ fn draw_arrow(painter: &egui::Painter, geo: &Geometry, a: &Arrow) {
|
|||
Stroke::NONE,
|
||||
));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -498,6 +498,7 @@ impl SearchBoard {
|
|||
show_coords: settings.show_coords,
|
||||
theme: settings.board_theme.theme(),
|
||||
pieces: settings.piece_set,
|
||||
animation: settings.animation.seconds(),
|
||||
};
|
||||
let played = ui
|
||||
.allocate_ui(vec2(side, side), |ui| view.show(ui, &mut self.board))
|
||||
|
|
|
|||
|
|
@ -704,6 +704,7 @@ impl GameView {
|
|||
show_coords: settings.show_coords,
|
||||
theme: settings.board_theme.theme(),
|
||||
pieces: settings.piece_set,
|
||||
animation: settings.animation.seconds(),
|
||||
};
|
||||
if let Some(m) = view.show(ui, &mut self.board) {
|
||||
self.play_move(m, actions);
|
||||
|
|
|
|||
|
|
@ -341,6 +341,16 @@ const ROWS: &[[&str; 6]] = &[
|
|||
"Detalles",
|
||||
"Dettagli",
|
||||
],
|
||||
[
|
||||
"Piece animation",
|
||||
"Анимация фигур",
|
||||
"Animation des pièces",
|
||||
"Figurenanimation",
|
||||
"Animación de piezas",
|
||||
"Animazione dei pezzi",
|
||||
],
|
||||
["Off", "Нет", "Aucune", "Aus", "No", "No"],
|
||||
["Slow", "Медленно", "Lent", "Langsam", "Lento", "Lento"],
|
||||
[
|
||||
"ChessPenguin could not start.",
|
||||
"ChessPenguin не удалось запустить.",
|
||||
|
|
|
|||
|
|
@ -1767,6 +1767,7 @@ fn chip(ui: &mut Ui, c: &Condition) -> (bool, bool) {
|
|||
show_coords: false,
|
||||
theme: theme.theme(),
|
||||
pieces: *pieces,
|
||||
animation: 0.0,
|
||||
};
|
||||
ui.allocate_ui(vec2(160.0, 160.0), |ui| {
|
||||
view.show(ui, &mut crate::board::BoardState::default())
|
||||
|
|
|
|||
|
|
@ -62,12 +62,46 @@ impl EngineConfig {
|
|||
}
|
||||
}
|
||||
|
||||
/// Piece animation, with Lichess's durations.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
pub enum Animation {
|
||||
Off,
|
||||
Fast,
|
||||
#[default]
|
||||
Normal,
|
||||
Slow,
|
||||
}
|
||||
|
||||
impl Animation {
|
||||
pub const ALL: [Animation; 4] = [Self::Off, Self::Fast, Self::Normal, Self::Slow];
|
||||
|
||||
pub fn seconds(self) -> f32 {
|
||||
match self {
|
||||
Self::Off => 0.0,
|
||||
Self::Fast => 0.12,
|
||||
Self::Normal => 0.25,
|
||||
Self::Slow => 0.5,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn label(self) -> &'static str {
|
||||
crate::i18n::tr(match self {
|
||||
Self::Off => "Off",
|
||||
Self::Fast => "Fast",
|
||||
Self::Normal => "Normal",
|
||||
Self::Slow => "Slow",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct Settings {
|
||||
pub piece_set: PieceSet,
|
||||
pub board_theme: BoardThemeId,
|
||||
pub show_coords: bool,
|
||||
/// How fast moves slide on the board.
|
||||
pub animation: Animation,
|
||||
pub sound_enabled: bool,
|
||||
pub sound_set: SoundSet,
|
||||
pub volume: f32,
|
||||
|
|
@ -133,6 +167,7 @@ impl Default for Settings {
|
|||
piece_set: PieceSet::default(),
|
||||
board_theme: BoardThemeId::default(),
|
||||
show_coords: true,
|
||||
animation: Animation::default(),
|
||||
sound_enabled: true,
|
||||
sound_set: SoundSet::default(),
|
||||
volume: 0.7,
|
||||
|
|
|
|||
|
|
@ -509,6 +509,14 @@ fn board_tab(ui: &mut Ui, s: &mut Settings, changed: &mut bool) {
|
|||
}
|
||||
});
|
||||
ui.add_space(8.0);
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(tr("Piece animation"));
|
||||
for a in crate::settings::Animation::ALL {
|
||||
*changed |= ui
|
||||
.selectable_value(&mut s.animation, a, a.label())
|
||||
.changed();
|
||||
}
|
||||
});
|
||||
*changed |= ui
|
||||
.checkbox(&mut s.show_coords, tr("Show coordinates"))
|
||||
.changed();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue