Initial commit

This commit is contained in:
2025-06-20 16:12:40 +02:00
commit f50e741929
20 changed files with 1843 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="45" height="45">
<g style="fill:none; fill-opacity:0; fill-rule:evenodd; stroke:#ffc837; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(0,0)">
<path
d="M 22.5,11.63 L 22.5,6"
style="fill:none; stroke:#ffc837; stroke-linejoin:miter;" />
<path
d="M 20,8 L 25,8"
style="fill:none; stroke:#ffc837; stroke-linejoin:miter;" />
<path
d="M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25"
style="fill:#ffffff; stroke:#ffc837; stroke-linecap:butt; stroke-linejoin:miter;" />
<path
d="M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z "
style="fill:#ffffff; stroke:#ffc837;" />
<path
d="M 11.5,30 C 17,27 27,27 32.5,30"
style="fill:none; stroke:#ffc837;" />
<path
d="M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5"
style="fill:none; stroke:#ffc837;" />
<path
d="M 11.5,37 C 17,34 27,34 32.5,37"
style="fill:none; stroke:#ffc837;" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="45" height="45">
<g style="fill:none; fill-opacity:0; fill-rule:evenodd; stroke:#776b00; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(0,0)">
<path
d="M 22.5,11.63 L 22.5,6"
style="fill:none; stroke:#776b00; stroke-linejoin:miter;" />
<path
d="M 20,8 L 25,8"
style="fill:none; stroke:#776b00; stroke-linejoin:miter;" />
<path
d="M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25"
style="fill:#ffffff; stroke:#776b00; stroke-linecap:butt; stroke-linejoin:miter;" />
<path
d="M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z "
style="fill:#ffffff; stroke:#776b00;" />
<path
d="M 11.5,30 C 17,27 27,27 32.5,30"
style="fill:none; stroke:#776b00;" />
<path
d="M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5"
style="fill:none; stroke:#776b00;" />
<path
d="M 11.5,37 C 17,34 27,34 32.5,37"
style="fill:none; stroke:#776b00;" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

221
rdraught-ui/src/geo2d.rs Normal file
View File

@@ -0,0 +1,221 @@
use rmath::SMatrix;
use std::clone::Clone;
use std::cmp::Eq;
use std::cmp::PartialEq;
use std::fmt::Display;
use std::marker::Copy;
use std::ops::Add;
use std::ops::Div;
use std::ops::Mul;
use std::ops::Neg;
use std::ops::Sub;
pub type Xform = SMatrix<f64, 3, 3>;
#[derive(Debug)]
pub struct Point(SMatrix<f64, 1, 3>);
impl Point {
pub fn x(&self) -> f64 {
self.0[(0, 0)]
}
pub fn y(&self) -> f64 {
self.0[(0, 1)]
}
pub fn new(x: f64, y: f64) -> Point {
Point(SMatrix::new(|pos| match pos {
(0, 0) => x,
(0, 1) => y,
(0, 2) => 1f64,
_ => 0f64,
}))
}
}
impl Add<&Point> for &Point {
fn add(self, rhs: &Point) -> Self::Output {
self * &xlate(rhs.x(), rhs.y())
}
type Output = Point;
}
impl Sub<&Point> for &Point {
fn sub(self, rhs: &Point) -> Self::Output {
self * &xlate(-rhs.x(), -rhs.y())
}
type Output = Point;
}
impl Mul<f64> for &Point {
fn mul(self, rhs: f64) -> Self::Output {
Point::new(self.0[(0, 0)] * rhs, self.0[(0, 1)] * rhs)
}
type Output = Point;
}
impl Div<f64> for &Point {
fn div(self, rhs: f64) -> Self::Output {
Point::new(self.0[(0, 0)] / rhs, self.0[(0, 1)] / rhs)
}
type Output = Point;
}
impl Neg for &Point {
fn neg(self) -> Self::Output {
Point::new(-self.x(), -self.y())
}
type Output = Point;
}
impl PartialEq for Point {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}
impl Eq for Point {}
impl Mul<&Xform> for &Point {
fn mul(self, rhs: &Xform) -> Self::Output {
Point(self.0 * rhs)
}
type Output = Point;
}
impl Clone for Point {
fn clone(&self) -> Self {
*self
}
}
impl Copy for Point {}
impl Display for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug, Clone)]
pub struct Rect2d {
tl: Point,
br: Point,
}
impl Rect2d {
pub fn new(tl: Point, br: Point) -> Rect2d {
Rect2d { tl, br }
}
pub fn center(&self) -> Point {
&(&self.tl + &self.br) / 2.0
}
pub fn tl(&self) -> Point {
self.tl
}
pub fn br(&self) -> Point {
self.br
}
pub fn width(&self) -> f64 {
(self.br.x() - self.tl.x()).abs()
}
pub fn height(&self) -> f64 {
(self.br.y() - self.tl.y()).abs()
}
pub fn contains(&self, point: &Point) -> bool {
self.tl().x() < point.x()
&& self.tl().y() < point.y()
&& self.br().x() > point.x()
&& self.br().y() > point.y()
}
}
impl Mul<&Xform> for &Rect2d {
fn mul(self, rhs: &Xform) -> Self::Output {
Rect2d {
tl: Point(self.tl.0 * rhs),
br: Point(self.br.0 * rhs),
}
}
type Output = Rect2d;
}
pub fn rot(alpha: f64) -> Xform {
let sa = alpha.sin();
let ca = alpha.cos();
Xform::new(|position| match position {
(0, 0) => ca,
(1, 1) => ca,
(1, 0) => -sa,
(0, 1) => sa,
(2, 2) => 1f64,
_ => 0f64,
})
}
impl Point {}
pub fn scale(x: f64, y: f64) -> Xform {
Xform::new(|position| match position {
(0, 0) => x,
(1, 1) => y,
(2, 2) => 1f64,
_ => 0f64,
})
}
pub fn xlate(x: f64, y: f64) -> Xform {
Xform::new(|position| match position {
(0, 0) => 1f64,
(1, 1) => 1f64,
(2, 2) => 1f64,
(2, 0) => x,
(2, 1) => y,
_ => 0f64,
})
}
#[cfg(test)]
mod tests {
use std::f64::consts::PI;
use super::Point;
use super::rot;
use super::scale;
use super::xlate;
#[test]
fn test_xlate() {
let p = Point::new(1.0, 3.0);
let xform = xlate(-1.0, 0.0);
let p2 = &(&(&p * &xform) * &rot(-PI / 2.0)) * &xlate(-2.0, 3.0);
assert!(p == p2);
}
#[test]
fn test_rotate() {
let p = Point::new(0.0, 3.0);
let p2 = &p * &rot(-PI / 2.0);
assert!((p2.x() - 3.0).abs() < 1e-3);
assert!((p2.y() - 0.0).abs() < 1e-3);
}
#[test]
fn test_scale() {
let p = Point::new(1.0, 3.0);
let p2 = &p * &scale(2.0, 3.0);
assert!((p2.x() - 2.0).abs() < 1e-3);
assert!((p2.y() - 9.0).abs() < 1e-3);
}
}

365
rdraught-ui/src/main.rs Normal file
View File

@@ -0,0 +1,365 @@
use gdk4::cairo::{Context as CairoContext, Matrix};
use gtk4::cairo::Error;
use gtk4::{self as gtk, gdk::ffi::GDK_BUTTON_PRIMARY};
use gtk4::{DrawingArea, prelude::*};
use rdraught::draughts::{self, DraughtsBoard, DraughtsGame, Move, Piece, Player};
use rdraught::position::Position;
use rmath::NumericalMatrix;
mod geo2d;
use core::f64::consts::PI;
use geo2d::{Point, Rect2d, Xform, scale, xlate};
use rsvg::SvgHandle;
use std::cell::{Cell, RefCell};
use std::rc::Rc;
const SQUARE_SIZE: f64 = 1.0;
const CROWN_RED: &'static [u8] = include_bytes!("crown_red.svg");
const CROWN_WHITE: &'static [u8] = include_bytes!("crown_white.svg");
fn draw_piece(
cr: &CairoContext,
square: &Rect2d,
piece: Piece,
crown_red: &SvgHandle,
crown_white: &SvgHandle,
) -> Result<(), Error> {
if let Piece::NoPiece = piece {
return Ok(());
} else {
let center = square.center();
let outer_radius = square.width() * 0.3;
let vertical_scale_factor = 0.8;
let matrix = {
let mut m1 = Matrix::identity();
m1.translate(0.0, -(center.y() - outer_radius));
let mut m2 = Matrix::identity();
m2.scale(1.0, vertical_scale_factor);
let mut m3 = Matrix::identity();
m3.translate(0.0, center.y() - outer_radius * vertical_scale_factor);
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
};
cr.save()?;
cr.set_matrix(matrix);
cr.set_source_rgb(0.0, 0.0, 0.0);
let thickness = outer_radius * 0.3;
cr.arc(
center.x(),
center.y() + thickness / 2.0,
outer_radius,
0.0,
2.0 * PI,
);
cr.rectangle(
center.x() - outer_radius,
center.y() - thickness / 2.0,
outer_radius * 2.0,
thickness,
);
cr.arc(
center.x(),
center.y() - thickness / 2.0,
outer_radius,
0.0,
2.0 * PI,
);
cr.fill().unwrap();
let (color, crowned) = match piece {
Piece::NoPiece => return Ok(()),
Piece::SimpleRedPawn => ((1.0, 0.0, 0.0), false),
Piece::SimpleWhitePawn => ((1.0, 1.0, 1.0), false),
Piece::CrownedRedPawn => ((1.0, 0.0, 0.0), true),
Piece::CrownedWhitePawn => ((1.0, 1.0, 1.0), true),
};
let radius = square.width() * 0.275;
cr.set_source_rgb(color.0, color.1, color.2);
cr.arc(
center.x(),
center.y() - thickness / 2.0,
radius,
0.0,
2.0 * PI,
);
cr.fill()?;
if crowned {
let renderer = match piece.player() {
Some(Player::Red) => rsvg::CairoRenderer::new(&crown_red),
Some(Player::White) => rsvg::CairoRenderer::new(&crown_white),
None => panic!("This should never happen"),
};
let m4 = {
let mut m1 = Matrix::identity();
m1.translate(-center.x(), -(center.y() - thickness / 1.0));
let mut m2 = Matrix::identity();
m2.scale(0.5, 0.5);
let mut m3 = Matrix::identity();
m3.translate(center.x(), center.y() - thickness / 1.0);
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
};
cr.set_matrix(Matrix::multiply(&matrix, &m4));
renderer
.render_document(
&cr,
&cairo::Rectangle::new(
square.tl().x(),
square.tl().y(),
square.width(),
square.height(),
),
)
.unwrap();
}
cr.restore()?;
Ok(())
}
}
fn on_activate(application: &gtk::Application) {
// Initialize GTK before using any GTK functions.
if gtk::init().is_err() {
panic!("Failed to initialize GTK.");
}
// Create a new window.
let window = gtk::ApplicationWindow::builder()
.application(application)
.title("Rdraught")
.default_width(800)
.default_height(800)
.build();
// Create a DrawingArea widget where we will draw the chessboard.
let drawing_area = Rc::new(RefCell::new(gtk::DrawingArea::new()));
// Add the drawing area to the window.
window.set_child(Some(drawing_area.borrow().as_ref() as &DrawingArea));
let draughts_game = Rc::new(RefCell::new(DraughtsGame::default()));
let selected_piece: Rc<Cell<Option<Position>>> = Rc::new(Cell::new(None));
let available_moves: Rc<RefCell<Vec<Move>>> = Rc::new(RefCell::new(Vec::new()));
// Get the allocation information for the widget.
let board_width = SQUARE_SIZE * DraughtsBoard::rows() as f64;
let board_height = SQUARE_SIZE * DraughtsBoard::columns() as f64;
let board = Rect2d::new(Point::new(0.0, 0.0), Point::new(board_width, board_height));
let board_clone = board.clone();
let crown_red_handle = {
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_RED));
rsvg::Loader::new()
.read_stream(
&stream,
None::<&gio::File>, // no base file as this document has no references
None::<&gio::Cancellable>, // no cancellable
)
.unwrap()
};
let crown_white_handle = {
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_WHITE));
rsvg::Loader::new()
.read_stream(
&stream,
None::<&gio::File>, // no base file as this document has no references
None::<&gio::Cancellable>, // no cancellable
)
.unwrap()
};
let xform = Rc::<RefCell<Xform>>::new(RefCell::new(Xform::identity(3)));
// Set the "draw" function of the drawing area. This callback is called
// whenever GTK needs to redraw this widget (for example, on first display or when resized).
{
let draughts_game = draughts_game.clone();
let xform = xform.clone();
let selected_piece = selected_piece.clone();
let board_clone = board.clone();
let available_moves = available_moves.clone();
let get_square_for_position = move |position: &Position, xform: &Xform| -> Rect2d {
let square_size = SQUARE_SIZE as f64;
let square = Rect2d::new(
&board_clone.tl()
+ &Point::new(
(position.col() as f64) * square_size,
((8 - 1 - position.row()) as f64) * square_size,
),
&board_clone.tl()
+ &Point::new(
((position.col() + 1) as f64) * square_size,
((8 - 1 - position.row() + 1) as f64) * square_size,
),
);
&square * &xform
};
drawing_area
.borrow_mut()
.set_draw_func(move |_widget, cr, width, height| {
let screen = Rect2d::new(
Point::new(0.0, 0.0),
Point::new(width as f64, height as f64),
);
let f = f64::min(
screen.width() / board.width(),
screen.height() / board.height(),
);
let screen_center = screen.center();
let board_center = board.center();
let mut xform = xform.borrow_mut();
*xform = xlate(-board_center.x(), -board_center.y())
* scale(f, f)
* xlate(screen_center.x(), screen_center.y());
// Loop over rows and columns to draw each chessboard cell.
for row in 0..DraughtsBoard::rows() {
for col in 0..DraughtsBoard::columns() {
let position = Position::new((8 - row - 1) as u8, col as u8);
let square = get_square_for_position(&position, &xform);
// Alternate colors based on the sum of row and column indices.
if (row + col) % 2 == 0 {
cr.set_source_rgb(0.8, 0.8, 0.6);
} else {
cr.set_source_rgb(0.4, 0.4, 0.2);
}
// Draw and fill the square.
cr.rectangle(
square.tl().x(),
square.tl().y(),
square.width(),
square.height(),
);
cr.fill().unwrap();
draw_piece(
cr,
&square,
draughts_game.borrow().piece_at(position),
&crown_red_handle,
&crown_white_handle,
)
.unwrap();
}
}
if let Some(selected_postion) = selected_piece.get() {
let piece = draughts_game.borrow().piece_at(selected_postion);
let square = get_square_for_position(&selected_postion, &xform);
cr.save().unwrap();
cr.new_path();
cr.move_to(square.tl().x(), square.tl().y());
cr.line_to(square.tl().x(), square.br().y());
cr.line_to(square.br().x(), square.br().y());
cr.line_to(square.br().x(), square.tl().y());
cr.line_to(square.tl().x(), square.tl().y());
cr.clip();
cr.new_path();
cr.set_source_rgb(0.0, 0.0, 1.0);
cr.set_line_width((square.width() + square.height()) * 0.05);
cr.move_to(square.tl().x(), square.tl().y());
cr.line_to(square.tl().x(), square.br().y());
cr.line_to(square.br().x(), square.br().y());
cr.line_to(square.br().x(), square.tl().y());
cr.line_to(square.tl().x(), square.tl().y());
cr.stroke().unwrap();
cr.restore().unwrap();
}
let am = available_moves.borrow();
if !am.is_empty() {
for mv in am.iter() {
let square = get_square_for_position(&mv.get_end_position(), &xform);
cr.save().unwrap();
cr.new_path();
cr.move_to(square.tl().x(), square.tl().y());
cr.line_to(square.tl().x(), square.br().y());
cr.line_to(square.br().x(), square.br().y());
cr.line_to(square.br().x(), square.tl().y());
cr.line_to(square.tl().x(), square.tl().y());
cr.clip();
cr.new_path();
cr.set_source_rgb(0.0, 1.0, 0.0);
cr.set_line_width((square.width() + square.height()) * 0.05);
cr.move_to(square.tl().x(), square.tl().y());
cr.line_to(square.tl().x(), square.br().y());
cr.line_to(square.br().x(), square.br().y());
cr.line_to(square.br().x(), square.tl().y());
cr.line_to(square.tl().x(), square.tl().y());
cr.stroke().unwrap();
cr.restore().unwrap();
}
}
});
}
let gesture = gtk::GestureClick::new();
// Set the gestures button to the right mouse button (=3)
gesture.set_button(GDK_BUTTON_PRIMARY as u32);
// Assign your handler to an event of the gesture (e.g. the `pressed` event)
{
let selected_piece = selected_piece;
let draughts_game = draughts_game;
let xform = xform;
let drawing_area = drawing_area.clone();
let available_moves = available_moves.clone();
gesture.connect_pressed(move |gesture, _, x, y| {
gesture.set_state(gtk::EventSequenceState::Claimed);
let xform = xform.borrow();
let inverse = xform.clone().invert();
let p = &Point::new(x, y) * &inverse;
if board_clone.contains(&p) {
let p = &p - &board_clone.tl();
// println!("Point: {:?}", p);
let position = Position::new(
8 - 1 - (p.y() / SQUARE_SIZE) as u8,
(p.x() / SQUARE_SIZE) as u8,
);
// println!("Selected position: {:?}", position);
let mut draughts_game = draughts_game.borrow_mut();
let piece = draughts_game.piece_at(position);
let mut am = available_moves.borrow_mut();
let mut move_applied = false;
if !am.is_empty() {
for mv in am.iter() {
if mv.get_end_position() == position {
draughts_game.apply_move(mv).unwrap();
move_applied = true;
break;
}
}
if move_applied {
selected_piece.set(None);
am.clear();
}
}
if !move_applied {
match piece.player() {
Some(Player::Red) => selected_piece.set(Some(position)),
Some(Player::White) => selected_piece.set(Some(position)),
None => selected_piece.set(None),
}
am.clear();
if let None = piece.player() {
selected_piece.set(None)
} else {
selected_piece.set(Some(position));
for mv in draughts_game.moves_for_piece(position) {
am.push(mv);
}
}
}
} else {
selected_piece.set(None);
}
drawing_area.borrow_mut().queue_draw();
});
}
// Assign the gesture to the treeview
drawing_area.borrow_mut().add_controller(gesture);
window.present();
}
fn main() {
// Create a new application with the builder pattern
let app = gtk::Application::builder()
.application_id("net.woggioni.rdraught")
.build();
app.connect_activate(on_activate);
// Run the application
app.run();
}