removed dependency from rmath

This commit is contained in:
2025-06-25 10:23:00 +08:00
parent 3cfbe4d87a
commit b2d7b3449e
3 changed files with 94 additions and 170 deletions

View File

@@ -1,13 +1,12 @@
use gdk4::cairo::{Context as CairoContext, Matrix};
use gdk4::cairo::{Context as CairoContext, Matrix, Rectangle};
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 geo2d::Point;
use rsvg::SvgHandle;
use std::cell::{Cell, RefCell};
use std::rc::Rc;
@@ -17,9 +16,50 @@ 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 transform_point(p: &Point, m: &Matrix) -> Point {
let (x, y) = m.transform_point(p.x(), p.y());
Point::new(x, y)
}
trait AugmentedRect {
fn from_points(tl: Point, br: Point) -> Rectangle;
fn center(&self) -> Point;
fn tl(&self) -> Point;
fn br(&self) -> Point;
fn contains(&self, p: &Point) -> bool;
}
impl AugmentedRect for Rectangle {
fn center(&self) -> Point {
Point::new(
self.x() + self.width() / 2.0,
self.y() + self.height() / 2.0,
)
}
fn tl(&self) -> Point {
Point::new(self.x(), self.y())
}
fn br(&self) -> Point {
Point::new(self.x() + self.width(), self.y() + self.height())
}
fn from_points(tl: Point, br: Point) -> Rectangle {
Rectangle::new(tl.x(), tl.y(), br.x() - tl.x(), br.y() - tl.y())
}
fn contains(&self, p: &Point) -> bool {
self.x() < p.x()
&& self.x() + self.width() > p.x()
&& self.y() < p.y()
&& self.y() + self.height() > p.y()
}
}
fn draw_piece(
cr: &CairoContext,
square: &Rect2d,
square: &Rectangle,
piece: Piece,
crown_red: &SvgHandle,
crown_white: &SvgHandle,
@@ -138,7 +178,7 @@ fn on_activate(application: &gtk::Application) {
// 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 = Rectangle::from_points(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));
@@ -162,7 +202,7 @@ fn on_activate(application: &gtk::Application) {
)
.unwrap()
};
let xform = Rc::<RefCell<Xform>>::new(RefCell::new(Xform::identity(3)));
let xform = Rc::<RefCell<Matrix>>::new(RefCell::new(Matrix::identity()));
// 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).
{
@@ -171,27 +211,29 @@ fn on_activate(application: &gtk::Application) {
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 get_square_for_position = move |position: &Position, xform: &Matrix| -> Rectangle {
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,
),
let p1 = Point::new(
(position.col() as f64) * square_size,
((8 - 1 - position.row()) as f64) * square_size,
);
&square * &xform
let p2 = &p1 + &Point::new(square_size, square_size);
let square = Rectangle::from_points(&board_clone.tl() + &p1, &board_clone.tl() + &p2);
let tl = transform_point(&square.tl(), xform);
let br = transform_point(&square.br(), xform);
let result = Rectangle::new(
f64::min(tl.x(), br.x()),
f64::min(tl.y(), br.y()),
f64::abs(tl.x() - br.x()),
f64::abs(tl.y() - br.y()),
);
result
};
drawing_area
.borrow_mut()
.set_draw_func(move |_widget, cr, width, height| {
let screen = Rect2d::new(
let screen = Rectangle::from_points(
Point::new(0.0, 0.0),
Point::new(width as f64, height as f64),
);
@@ -202,9 +244,13 @@ fn on_activate(application: &gtk::Application) {
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());
*xform = Matrix::multiply(
&Matrix::multiply(
&Matrix::new(1.0, 0.0, 0.0, 1.0, -board_center.x(), -board_center.y()),
&Matrix::new(f, 0.0, 0.0, f, 0.0, 0.0),
),
&Matrix::new(1.0, 0.0, 0.0, 1.0, screen_center.x(), screen_center.y()),
);
// Loop over rows and columns to draw each chessboard cell.
for row in 0..DraughtsBoard::rows() {
@@ -300,8 +346,12 @@ fn on_activate(application: &gtk::Application) {
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;
let inverse = {
let mut m = xform.clone();
m.invert();
m
};
let p = transform_point(&Point::new(x, y), &inverse);
if board_clone.contains(&p) {
let p = &p - &board_clone.tl();
// println!("Point: {:?}", p);