simplified UI code
This commit is contained in:
@@ -63,7 +63,6 @@ fn draw_piece(
|
|||||||
piece: Piece,
|
piece: Piece,
|
||||||
crown_red: &SvgHandle,
|
crown_red: &SvgHandle,
|
||||||
crown_white: &SvgHandle,
|
crown_white: &SvgHandle,
|
||||||
current_player: Player,
|
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
if let Piece::NoPiece = piece {
|
if let Piece::NoPiece = piece {
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -116,10 +115,7 @@ fn draw_piece(
|
|||||||
cr.set_source_rgb(color.0, color.1, color.2);
|
cr.set_source_rgb(color.0, color.1, color.2);
|
||||||
cr.arc(
|
cr.arc(
|
||||||
center.x(),
|
center.x(),
|
||||||
match current_player {
|
center.y() - thickness / 2.0,
|
||||||
Player::White => center.y() + thickness / 2.0,
|
|
||||||
Player::Red => center.y() - thickness / 2.0,
|
|
||||||
},
|
|
||||||
radius,
|
radius,
|
||||||
0.0,
|
0.0,
|
||||||
2.0 * PI,
|
2.0 * PI,
|
||||||
@@ -134,21 +130,11 @@ fn draw_piece(
|
|||||||
};
|
};
|
||||||
let m4 = {
|
let m4 = {
|
||||||
let mut m1 = Matrix::identity();
|
let mut m1 = Matrix::identity();
|
||||||
match current_player {
|
m1.translate(-center.x(), -(center.y() - thickness / 1.0));
|
||||||
Player::Red => m1.translate(-center.x(), -(center.y() - thickness / 1.0)),
|
|
||||||
Player::White => m1.translate(-center.x(), -(center.y() + thickness / 2.0)),
|
|
||||||
}
|
|
||||||
let mut m2 = Matrix::identity();
|
let mut m2 = Matrix::identity();
|
||||||
m2.scale(0.5, 0.5);
|
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();
|
let mut m3 = Matrix::identity();
|
||||||
match current_player {
|
m3.translate(center.x(), center.y() - thickness / 1.0);
|
||||||
Player::Red => m3.translate(center.x(), center.y() - thickness / 1.0),
|
|
||||||
Player::White => m3.translate(center.x(), center.y() + thickness / 2.0),
|
|
||||||
}
|
|
||||||
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
|
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
|
||||||
};
|
};
|
||||||
cr.set_matrix(Matrix::multiply(
|
cr.set_matrix(Matrix::multiply(
|
||||||
@@ -263,7 +249,7 @@ fn on_activate(application: >k::Application) {
|
|||||||
)
|
)
|
||||||
.unwrap()
|
.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()));
|
let xform = Rc::<RefCell<Matrix>>::new(RefCell::new(Matrix::identity()));
|
||||||
// Set the "draw" function of the drawing area. This callback is called
|
// 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).
|
// whenever GTK needs to redraw this widget (for example, on first display or when resized).
|
||||||
@@ -289,13 +275,7 @@ fn on_activate(application: >k::Application) {
|
|||||||
let mut xform = xform.borrow_mut();
|
let mut xform = xform.borrow_mut();
|
||||||
*xform = Matrix::multiply(
|
*xform = Matrix::multiply(
|
||||||
&Matrix::multiply(
|
&Matrix::multiply(
|
||||||
&Matrix::multiply(
|
&Matrix::new(1.0, 0.0, 0.0, 1.0, -board_center.x(), -board_center.y()),
|
||||||
&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(f, 0.0, 0.0, f, 0.0, 0.0),
|
&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()),
|
&Matrix::new(1.0, 0.0, 0.0, 1.0, screen_center.x(), screen_center.y()),
|
||||||
@@ -305,10 +285,13 @@ fn on_activate(application: >k::Application) {
|
|||||||
// Loop over rows and columns to draw each chessboard cell.
|
// Loop over rows and columns to draw each chessboard cell.
|
||||||
for row in 0..DraughtsBoard::rows() {
|
for row in 0..DraughtsBoard::rows() {
|
||||||
for col in 0..DraughtsBoard::columns() {
|
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(
|
let square = Rectangle::new(
|
||||||
position.col() as f64 * SQUARE_SIZE,
|
col as f64 * SQUARE_SIZE,
|
||||||
position.row() as f64 * SQUARE_SIZE,
|
row as f64 * SQUARE_SIZE,
|
||||||
SQUARE_SIZE,
|
SQUARE_SIZE,
|
||||||
SQUARE_SIZE,
|
SQUARE_SIZE,
|
||||||
);
|
);
|
||||||
@@ -334,16 +317,21 @@ fn on_activate(application: >k::Application) {
|
|||||||
draughts_game.borrow().piece_at(position),
|
draughts_game.borrow().piece_at(position),
|
||||||
&crown_red_handle,
|
&crown_red_handle,
|
||||||
&crown_white_handle,
|
&crown_white_handle,
|
||||||
*current_player,
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
cr.restore().unwrap();
|
cr.restore().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(selected_position) = selected_piece.get() {
|
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(
|
let square = Rectangle::new(
|
||||||
selected_position.col() as f64 * SQUARE_SIZE,
|
screen_position.col() as f64 * SQUARE_SIZE,
|
||||||
selected_position.row() as f64 * SQUARE_SIZE,
|
screen_position.row() as f64 * SQUARE_SIZE,
|
||||||
SQUARE_SIZE,
|
SQUARE_SIZE,
|
||||||
SQUARE_SIZE,
|
SQUARE_SIZE,
|
||||||
);
|
);
|
||||||
@@ -371,9 +359,13 @@ fn on_activate(application: >k::Application) {
|
|||||||
if !am.is_empty() {
|
if !am.is_empty() {
|
||||||
for mv in am.iter() {
|
for mv in am.iter() {
|
||||||
let end_pos = mv.get_end_position();
|
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(
|
let square = Rectangle::new(
|
||||||
end_pos.col() as f64 * SQUARE_SIZE,
|
screen_position.col() as f64 * SQUARE_SIZE,
|
||||||
end_pos.row() as f64 * SQUARE_SIZE,
|
screen_position.row() as f64 * SQUARE_SIZE,
|
||||||
SQUARE_SIZE,
|
SQUARE_SIZE,
|
||||||
SQUARE_SIZE,
|
SQUARE_SIZE,
|
||||||
);
|
);
|
||||||
@@ -421,9 +413,16 @@ fn on_activate(application: >k::Application) {
|
|||||||
if board_clone.contains(&p) {
|
if board_clone.contains(&p) {
|
||||||
let p = &p - &board_clone.tl();
|
let p = &p - &board_clone.tl();
|
||||||
// println!("Point: {:?}", p);
|
// println!("Point: {:?}", p);
|
||||||
let position =
|
let position = match *current_player {
|
||||||
Position::new((p.y() / SQUARE_SIZE) as u8, (p.x() / SQUARE_SIZE) as u8);
|
Player::White => Position::new(
|
||||||
// println!("Selected position: {:?}", position);
|
(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 mut draughts_game = draughts_game.borrow_mut();
|
||||||
let piece = draughts_game.piece_at(position);
|
let piece = draughts_game.piece_at(position);
|
||||||
// println!("Selected piece: {:?}", piece);
|
// println!("Selected piece: {:?}", piece);
|
||||||
@@ -433,9 +432,9 @@ fn on_activate(application: >k::Application) {
|
|||||||
for mv in am.iter() {
|
for mv in am.iter() {
|
||||||
if mv.get_end_position() == position {
|
if mv.get_end_position() == position {
|
||||||
draughts_game.apply_move(mv).unwrap();
|
draughts_game.apply_move(mv).unwrap();
|
||||||
if let Some(mv) = draughts_game.get_best_move(10) {
|
// if let Some(mv) = draughts_game.get_best_move(10) {
|
||||||
println!("Next best move: {:?}", mv);
|
// println!("Next best move: {:?}", mv);
|
||||||
}
|
// }
|
||||||
move_applied = true;
|
move_applied = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user