Initial commit

This commit is contained in:
2025-06-20 16:12:40 +02:00
commit 4f01e08be5
18 changed files with 1536 additions and 0 deletions

27
rdraught-ui/src/crown.svg Normal file
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

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

@@ -0,0 +1,211 @@
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>;
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)
}
}
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()
}
}
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);
}
}

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

@@ -0,0 +1,180 @@
use gdk4::cairo::{Context as CairoContext, Matrix};
use gtk4::cairo::Error;
use gtk4::prelude::*;
use gtk4::{self as gtk};
use rdraught::draughts::{self, DraughtsBoard, Piece};
use rdraught::position::Position;
mod geo2d;
use core::f64::consts::PI;
use geo2d::{Point, Rect2d, Xform, scale, xlate};
use rsvg::{Handle, HandleExt};
const SQUARE_SIZE: f64 = 1.0;
const CROWN: &'static [u8] = include_bytes!("crown.svg");
fn draw_piece(cr: &CairoContext, square: &Rect2d, piece: Piece) -> 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, 0.0, 0.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 handle = Handle::new_from_data(CROWN).unwrap();
handle.render_cairo(cr as &cairo::Context);
cr.move_to(center.x(), center.y());
cr.set_source_rgb(1.0, 1.0, 0.0);
cr.set_font_size(20.0);
cr.show_text("A♔")?;
}
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 = gtk::DrawingArea::new();
// Add the drawing area to the window.
window.set_child(Some(&drawing_area));
let draughts_board = DraughtsBoard::default();
println!("{:?}", draughts_board[Position::new(0, 0)]);
// 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));
// 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).
drawing_area.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 xform = xlate(-board_center.x(), -board_center.y())
* scale(f, f)
* xlate(screen_center.x(), screen_center.y());
//let xlation = screen.center() - board.center();
//let xform = xform * xlate(xlation.x(), xlation.y());
let square_size = SQUARE_SIZE as f64;
// Loop over rows and columns to draw each chessboard cell.
for row in 0..DraughtsBoard::rows() {
for col in 0..DraughtsBoard::columns() {
let square = Rect2d::new(
board.tl() + Point::new((col as f64) * square_size, (row as f64) * square_size),
board.tl()
+ Point::new(
((col + 1) as f64) * square_size,
((row + 1) as f64) * square_size,
),
);
let square = square * &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); // white
} else {
cr.set_source_rgb(0.4, 0.4, 0.2); // black
}
// 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_board[Position::new(col as u8, (8 - row - 1) as u8)],
)
.unwrap();
}
}
});
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();
}