simplified UI code

This commit is contained in:
2025-06-27 08:38:28 +08:00
parent d326c615cd
commit 01799ea4a0

View File

@@ -63,7 +63,6 @@ fn draw_piece(
piece: Piece,
crown_red: &SvgHandle,
crown_white: &SvgHandle,
current_player: Player,
) -> Result<(), Error> {
if let Piece::NoPiece = piece {
Ok(())
@@ -116,10 +115,7 @@ fn draw_piece(
cr.set_source_rgb(color.0, color.1, color.2);
cr.arc(
center.x(),
match current_player {
Player::White => center.y() + thickness / 2.0,
Player::Red => center.y() - thickness / 2.0,
},
center.y() - thickness / 2.0,
radius,
0.0,
2.0 * PI,
@@ -134,21 +130,11 @@ fn draw_piece(
};
let m4 = {
let mut m1 = Matrix::identity();
match current_player {
Player::Red => m1.translate(-center.x(), -(center.y() - thickness / 1.0)),
Player::White => m1.translate(-center.x(), -(center.y() + thickness / 2.0)),
}
m1.translate(-center.x(), -(center.y() - thickness / 1.0));
let mut m2 = Matrix::identity();
m2.scale(0.5, 0.5);
if Player::White == current_player {
let m = Matrix::new(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
m2 = Matrix::multiply(&m2, &m);
}
let mut m3 = Matrix::identity();
match current_player {
Player::Red => m3.translate(center.x(), center.y() - thickness / 1.0),
Player::White => m3.translate(center.x(), center.y() + thickness / 2.0),
}
m3.translate(center.x(), center.y() - thickness / 1.0);
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
};
cr.set_matrix(Matrix::multiply(
@@ -263,7 +249,7 @@ fn on_activate(application: &gtk::Application) {
)
.unwrap()
};
let current_player = Rc::<Player>::new(Player::White);
let current_player = Rc::<Player>::new(Player::Red);
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).
@@ -289,13 +275,7 @@ fn on_activate(application: &gtk::Application) {
let mut xform = xform.borrow_mut();
*xform = Matrix::multiply(
&Matrix::multiply(
&Matrix::multiply(
&Matrix::new(1.0, 0.0, 0.0, 1.0, -board_center.x(), -board_center.y()),
&(match *current_player {
Player::White => Matrix::new(1.0, 0.0, 0.0, -1.0, 0.0, 0.0),
Player::Red => Matrix::new(1.0, 0.0, 0.0, 1.0, 0.0, 0.0),
}),
),
&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()),
@@ -305,10 +285,13 @@ fn on_activate(application: &gtk::Application) {
// 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 position = match *current_player {
Player::White => Position::new((8 - row - 1) as u8, col as u8),
Player::Red => Position::new(row as u8, col as u8),
};
let square = Rectangle::new(
position.col() as f64 * SQUARE_SIZE,
position.row() as f64 * SQUARE_SIZE,
col as f64 * SQUARE_SIZE,
row as f64 * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE,
);
@@ -334,16 +317,21 @@ fn on_activate(application: &gtk::Application) {
draughts_game.borrow().piece_at(position),
&crown_red_handle,
&crown_white_handle,
*current_player,
)
.unwrap();
cr.restore().unwrap();
}
}
if let Some(selected_position) = selected_piece.get() {
let screen_position = match *current_player {
Player::White => {
Position::new(8 - 1 - selected_position.row(), selected_position.col())
}
Player::Red => selected_position,
};
let square = Rectangle::new(
selected_position.col() as f64 * SQUARE_SIZE,
selected_position.row() as f64 * SQUARE_SIZE,
screen_position.col() as f64 * SQUARE_SIZE,
screen_position.row() as f64 * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE,
);
@@ -371,9 +359,13 @@ fn on_activate(application: &gtk::Application) {
if !am.is_empty() {
for mv in am.iter() {
let end_pos = mv.get_end_position();
let screen_position = match *current_player {
Player::White => Position::new(8 - 1 - end_pos.row(), end_pos.col()),
Player::Red => end_pos,
};
let square = Rectangle::new(
end_pos.col() as f64 * SQUARE_SIZE,
end_pos.row() as f64 * SQUARE_SIZE,
screen_position.col() as f64 * SQUARE_SIZE,
screen_position.row() as f64 * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE,
);
@@ -421,9 +413,16 @@ fn on_activate(application: &gtk::Application) {
if board_clone.contains(&p) {
let p = &p - &board_clone.tl();
// println!("Point: {:?}", p);
let position =
Position::new((p.y() / SQUARE_SIZE) as u8, (p.x() / SQUARE_SIZE) as u8);
// println!("Selected position: {:?}", position);
let position = match *current_player {
Player::White => Position::new(
(8.0 - (p.y() / SQUARE_SIZE)) as u8,
(p.x() / SQUARE_SIZE) as u8,
),
Player::Red => {
Position::new((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);
// println!("Selected piece: {:?}", piece);
@@ -433,9 +432,9 @@ fn on_activate(application: &gtk::Application) {
for mv in am.iter() {
if mv.get_end_position() == position {
draughts_game.apply_move(mv).unwrap();
if let Some(mv) = draughts_game.get_best_move(10) {
println!("Next best move: {:?}", mv);
}
// if let Some(mv) = draughts_game.get_best_move(10) {
// println!("Next best move: {:?}", mv);
// }
move_applied = true;
break;
}