resolved clippy issues
This commit is contained in:
@@ -2,7 +2,7 @@ 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::draughts::{DraughtsBoard, DraughtsGame, Move, Piece, Player};
|
||||
use rdraught::position::Position;
|
||||
mod geo2d;
|
||||
use core::f64::consts::PI;
|
||||
@@ -13,8 +13,8 @@ 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");
|
||||
const CROWN_RED: &[u8] = include_bytes!("crown_red.svg");
|
||||
const CROWN_WHITE: &[u8] = include_bytes!("crown_white.svg");
|
||||
|
||||
fn transform_point(p: &Point, m: &Matrix) -> Point {
|
||||
let (x, y) = m.transform_point(p.x(), p.y());
|
||||
@@ -65,7 +65,7 @@ fn draw_piece(
|
||||
crown_white: &SvgHandle,
|
||||
) -> Result<(), Error> {
|
||||
if let Piece::NoPiece = piece {
|
||||
return Ok(());
|
||||
Ok(())
|
||||
} else {
|
||||
let center = square.center();
|
||||
let outer_radius = square.width() * 0.3;
|
||||
@@ -123,8 +123,8 @@ fn draw_piece(
|
||||
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),
|
||||
Some(Player::Red) => rsvg::CairoRenderer::new(crown_red),
|
||||
Some(Player::White) => rsvg::CairoRenderer::new(crown_white),
|
||||
None => panic!("This should never happen"),
|
||||
};
|
||||
let m4 = {
|
||||
@@ -139,7 +139,7 @@ fn draw_piece(
|
||||
cr.set_matrix(Matrix::multiply(&matrix, &m4));
|
||||
renderer
|
||||
.render_document(
|
||||
&cr,
|
||||
cr,
|
||||
&cairo::Rectangle::new(
|
||||
square.tl().x(),
|
||||
square.tl().y(),
|
||||
@@ -179,7 +179,7 @@ fn on_activate(application: >k::Application) {
|
||||
let board_width = SQUARE_SIZE * DraughtsBoard::rows() as f64;
|
||||
let board_height = SQUARE_SIZE * DraughtsBoard::columns() as f64;
|
||||
let board = Rectangle::from_points(Point::new(0.0, 0.0), Point::new(board_width, board_height));
|
||||
let board_clone = board.clone();
|
||||
let board_clone = board;
|
||||
let crown_red_handle = {
|
||||
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_RED));
|
||||
|
||||
@@ -209,10 +209,10 @@ fn on_activate(application: >k::Application) {
|
||||
let draughts_game = draughts_game.clone();
|
||||
let xform = xform.clone();
|
||||
let selected_piece = selected_piece.clone();
|
||||
let board_clone = board.clone();
|
||||
let board_clone = board;
|
||||
let available_moves = available_moves.clone();
|
||||
let get_square_for_position = move |position: &Position, xform: &Matrix| -> Rectangle {
|
||||
let square_size = SQUARE_SIZE as f64;
|
||||
let square_size = SQUARE_SIZE;
|
||||
|
||||
let p1 = Point::new(
|
||||
(position.col() as f64) * square_size,
|
||||
@@ -222,13 +222,12 @@ fn on_activate(application: >k::Application) {
|
||||
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(
|
||||
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()
|
||||
@@ -283,7 +282,6 @@ fn on_activate(application: >k::Application) {
|
||||
}
|
||||
}
|
||||
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();
|
||||
@@ -338,16 +336,13 @@ fn on_activate(application: >k::Application) {
|
||||
|
||||
// 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 = {
|
||||
let mut m = xform.clone();
|
||||
let mut m = *xform;
|
||||
m.invert();
|
||||
m
|
||||
};
|
||||
@@ -384,7 +379,7 @@ fn on_activate(application: >k::Application) {
|
||||
None => selected_piece.set(None),
|
||||
}
|
||||
am.clear();
|
||||
if let None = piece.player() {
|
||||
if piece.player().is_none() {
|
||||
selected_piece.set(None)
|
||||
} else {
|
||||
selected_piece.set(Some(position));
|
||||
|
@@ -106,7 +106,7 @@ impl DraughtsBoard {
|
||||
}
|
||||
|
||||
pub fn pieces<'a, const PIECES: usize>(
|
||||
self: &'a Self,
|
||||
&'a self,
|
||||
pieces: &'a Vec<Piece, PIECES>,
|
||||
) -> Map<
|
||||
Filter<BoardIteratorRef<'a, Piece, 8, 8>, impl FnMut(&(usize, usize, Piece)) -> bool>,
|
||||
@@ -120,14 +120,14 @@ impl DraughtsBoard {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
false
|
||||
})
|
||||
.map(|(row, col, _)| Position::new(row as u8, col as u8))
|
||||
}
|
||||
|
||||
fn is_position_valid(position: Position) -> bool {
|
||||
let position = position.to_index();
|
||||
return position.0 < DraughtsBoard::rows() && position.1 < DraughtsBoard::columns();
|
||||
position.0 < DraughtsBoard::rows() && position.1 < DraughtsBoard::columns()
|
||||
}
|
||||
|
||||
fn get_piece(&self, p: &Position) -> Piece {
|
||||
@@ -174,7 +174,7 @@ impl DraughtsBoard {
|
||||
let start = mv.get_start_position();
|
||||
let piece = self.get_piece(&start);
|
||||
if let Piece::NoPiece = piece {
|
||||
return Err(Error::InvalidMove);
|
||||
Err(Error::InvalidMove)
|
||||
} else {
|
||||
let player = piece.player().unwrap();
|
||||
match mv {
|
||||
@@ -334,7 +334,7 @@ impl DraughtsGame {
|
||||
match mv {
|
||||
Move::Movement { .. } => {
|
||||
if self.next_move == player {
|
||||
self.board.apply_move(&mv)?;
|
||||
self.board.apply_move(mv)?;
|
||||
self.next_turn();
|
||||
} else {
|
||||
return Err(Error::WrongPlayer);
|
||||
@@ -342,9 +342,14 @@ impl DraughtsGame {
|
||||
}
|
||||
Move::Capture { .. } => {
|
||||
if self.next_move == player {
|
||||
self.board.apply_move(&mv)?;
|
||||
self.board.apply_move(mv)?;
|
||||
// Check if more captures are available for the current piece
|
||||
for _ in self.board.moves_for_piece(mv.get_end_position(), true) {
|
||||
if self
|
||||
.board
|
||||
.moves_for_piece(mv.get_end_position(), true)
|
||||
.next()
|
||||
.is_some()
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
self.next_turn();
|
||||
@@ -509,6 +514,6 @@ impl<'a> Iterator for MoveIterator<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
return None;
|
||||
None
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user