added greeting window
This commit is contained in:
62
rdraught-ui/src/greeting_dialog.rs
Normal file
62
rdraught-ui/src/greeting_dialog.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use gtk4::{Align, Application, Box, CheckButton, Label, Orientation, Window, prelude::*};
|
||||
|
||||
use crate::types::SharedMutable;
|
||||
use rdraught::draughts::Player;
|
||||
|
||||
pub(crate) fn create(application: &Application, current_player: SharedMutable<Player>) -> Window {
|
||||
let label = Label::builder().label("Main player:").build();
|
||||
let red_player_button = CheckButton::builder().label("Red").active(true).build();
|
||||
let white_player_button = CheckButton::builder()
|
||||
.label("White")
|
||||
.active(false)
|
||||
.group(&red_player_button)
|
||||
.build();
|
||||
let current_player_1 = current_player.clone();
|
||||
red_player_button.connect_toggled(move |b| {
|
||||
if b.is_active() {
|
||||
current_player_1.set(Player::Red);
|
||||
}
|
||||
});
|
||||
let current_player_2 = current_player.clone();
|
||||
white_player_button.connect_toggled(move |b| {
|
||||
if b.is_active() {
|
||||
current_player_2.set(Player::White);
|
||||
}
|
||||
});
|
||||
let main_player_selector = Box::builder()
|
||||
.valign(Align::Center)
|
||||
.halign(Align::Center)
|
||||
.spacing(10)
|
||||
.orientation(Orientation::Horizontal)
|
||||
.build();
|
||||
|
||||
main_player_selector.append(&label);
|
||||
main_player_selector.append(&red_player_button);
|
||||
main_player_selector.append(&white_player_button);
|
||||
let layout = Box::builder()
|
||||
.valign(Align::Center)
|
||||
.halign(Align::Center)
|
||||
.spacing(10)
|
||||
.orientation(Orientation::Vertical)
|
||||
.build();
|
||||
layout.append(&main_player_selector);
|
||||
layout.set_margin_top(10);
|
||||
layout.set_margin_bottom(10);
|
||||
layout.set_margin_start(10);
|
||||
layout.set_margin_end(10);
|
||||
let player_select_dialog = Window::builder()
|
||||
.application(application)
|
||||
.title("Rdraught")
|
||||
.modal(true)
|
||||
.child(&layout)
|
||||
.visible(true)
|
||||
.build();
|
||||
|
||||
player_select_dialog.connect_close_request(move |window| {
|
||||
if let Some(application) = window.application() {
|
||||
application.remove_window(window);
|
||||
}
|
||||
glib::Propagation::Proceed
|
||||
});
|
||||
player_select_dialog
|
||||
}
|
@@ -1,18 +1,22 @@
|
||||
use gdk4::cairo::{Context as CairoContext, Matrix, Rectangle};
|
||||
use gtk4::cairo::Error;
|
||||
use gtk4::glib::Propagation;
|
||||
use gtk4::{self as gtk, gdk::ffi::GDK_BUTTON_PRIMARY};
|
||||
use gtk4::{DrawingArea, prelude::*};
|
||||
use gtk4::{Application, DrawingArea, prelude::*};
|
||||
use rdraught::draughts::{DraughtsBoard, DraughtsGame, Move, Piece, Player};
|
||||
use rdraught::position::Position;
|
||||
mod geo2d;
|
||||
use core::f64::consts::PI;
|
||||
use geo2d::Point;
|
||||
use rsvg::SvgHandle;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
use std::thread;
|
||||
const SQUARE_SIZE: f64 = 1.0;
|
||||
|
||||
mod greeting_dialog;
|
||||
mod types;
|
||||
|
||||
use types::{SharedMutable, SharedMutableRef, new_shared_mut, new_shared_mut_ref};
|
||||
|
||||
const CROWN_RED: &[u8] = include_bytes!("crown_red.svg");
|
||||
const CROWN_WHITE: &[u8] = include_bytes!("crown_white.svg");
|
||||
|
||||
@@ -186,11 +190,7 @@ fn draw_score_bar(cr: &CairoContext, board: &Rectangle, draughts_game: &Draughts
|
||||
cr.restore().unwrap();
|
||||
}
|
||||
|
||||
fn on_activate(application: >k::Application) {
|
||||
// Initialize GTK before using any GTK functions.
|
||||
if gtk::init().is_err() {
|
||||
panic!("Failed to initialize GTK.");
|
||||
}
|
||||
fn create_game_window(application: &Application, current_player: Player) {
|
||||
// Create a new window.
|
||||
let window = gtk::ApplicationWindow::builder()
|
||||
.application(application)
|
||||
@@ -200,13 +200,13 @@ fn on_activate(application: >k::Application) {
|
||||
.build();
|
||||
|
||||
// Create a DrawingArea widget where we will draw the chessboard.
|
||||
let drawing_area = Rc::new(RefCell::new(gtk::DrawingArea::new()));
|
||||
let drawing_area = DrawingArea::new();
|
||||
// Add the drawing area to the window.
|
||||
window.set_child(Some(drawing_area.borrow().as_ref() as &DrawingArea));
|
||||
window.set_child(Some(&drawing_area));
|
||||
|
||||
let draughts_game = Rc::new(RefCell::new(DraughtsGame::default()));
|
||||
let selected_piece: Rc<Cell<Option<Position>>> = Rc::new(Cell::new(None));
|
||||
let available_moves: Rc<RefCell<Vec<Move>>> = Rc::new(RefCell::new(Vec::new()));
|
||||
let draughts_game = new_shared_mut_ref(DraughtsGame::default());
|
||||
let selected_piece: SharedMutable<Option<Position>> = new_shared_mut(None);
|
||||
let available_moves = new_shared_mut_ref(Vec::<Move>::new());
|
||||
// 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;
|
||||
@@ -238,8 +238,7 @@ fn on_activate(application: >k::Application) {
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
let current_player = Rc::<Player>::new(Player::Red);
|
||||
let xform = Rc::<RefCell<Matrix>>::new(RefCell::new(Matrix::identity()));
|
||||
let xform = new_shared_mut_ref(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).
|
||||
{
|
||||
@@ -247,76 +246,107 @@ fn on_activate(application: >k::Application) {
|
||||
let xform = xform.clone();
|
||||
let selected_piece = selected_piece.clone();
|
||||
let available_moves = available_moves.clone();
|
||||
let current_player = current_player.clone();
|
||||
drawing_area
|
||||
.borrow_mut()
|
||||
.set_draw_func(move |_widget, cr, width, height| {
|
||||
let screen = Rectangle::from_points(
|
||||
Point::new(0.0, 0.0),
|
||||
Point::new(width as f64, height as f64),
|
||||
);
|
||||
let f = f64::min(
|
||||
screen.width() / board_with_bar.width(),
|
||||
screen.height() / board_with_bar.height(),
|
||||
);
|
||||
let screen_center = screen.center();
|
||||
let board_center = board_with_bar.center();
|
||||
let mut xform = xform.borrow_mut();
|
||||
*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()),
|
||||
);
|
||||
cr.set_matrix(*xform);
|
||||
drawing_area.set_draw_func(move |_widget, cr, width, height| {
|
||||
let screen = Rectangle::from_points(
|
||||
Point::new(0.0, 0.0),
|
||||
Point::new(width as f64, height as f64),
|
||||
);
|
||||
let f = f64::min(
|
||||
screen.width() / board_with_bar.width(),
|
||||
screen.height() / board_with_bar.height(),
|
||||
);
|
||||
let screen_center = screen.center();
|
||||
let board_center = board_with_bar.center();
|
||||
let mut xform = xform.borrow_mut();
|
||||
*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()),
|
||||
);
|
||||
cr.set_matrix(*xform);
|
||||
|
||||
// Loop over rows and columns to draw each chessboard cell.
|
||||
for row in 0..DraughtsBoard::rows() {
|
||||
for col in 0..DraughtsBoard::columns() {
|
||||
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(
|
||||
col as f64 * SQUARE_SIZE,
|
||||
row as f64 * SQUARE_SIZE,
|
||||
SQUARE_SIZE,
|
||||
SQUARE_SIZE,
|
||||
);
|
||||
cr.save().unwrap();
|
||||
// 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);
|
||||
} else {
|
||||
cr.set_source_rgb(0.4, 0.4, 0.2);
|
||||
}
|
||||
|
||||
// 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_game.borrow().piece_at(position),
|
||||
&crown_red_handle,
|
||||
&crown_white_handle,
|
||||
)
|
||||
.unwrap();
|
||||
cr.restore().unwrap();
|
||||
// Loop over rows and columns to draw each chessboard cell.
|
||||
for row in 0..DraughtsBoard::rows() {
|
||||
for col in 0..DraughtsBoard::columns() {
|
||||
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(
|
||||
col as f64 * SQUARE_SIZE,
|
||||
row as f64 * SQUARE_SIZE,
|
||||
SQUARE_SIZE,
|
||||
SQUARE_SIZE,
|
||||
);
|
||||
cr.save().unwrap();
|
||||
// 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);
|
||||
} else {
|
||||
cr.set_source_rgb(0.4, 0.4, 0.2);
|
||||
}
|
||||
|
||||
// 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_game.borrow().piece_at(position),
|
||||
&crown_red_handle,
|
||||
&crown_white_handle,
|
||||
)
|
||||
.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,
|
||||
}
|
||||
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(
|
||||
screen_position.col() as f64 * SQUARE_SIZE,
|
||||
screen_position.row() as f64 * SQUARE_SIZE,
|
||||
SQUARE_SIZE,
|
||||
SQUARE_SIZE,
|
||||
);
|
||||
cr.save().unwrap();
|
||||
cr.new_path();
|
||||
cr.move_to(square.tl().x(), square.tl().y());
|
||||
cr.line_to(square.tl().x(), square.br().y());
|
||||
cr.line_to(square.br().x(), square.br().y());
|
||||
cr.line_to(square.br().x(), square.tl().y());
|
||||
cr.line_to(square.tl().x(), square.tl().y());
|
||||
cr.clip();
|
||||
cr.new_path();
|
||||
cr.set_source_rgb(0.0, 0.0, 1.0);
|
||||
cr.set_line_width((square.width() + square.height()) * 0.05);
|
||||
cr.move_to(square.tl().x(), square.tl().y());
|
||||
cr.line_to(square.tl().x(), square.br().y());
|
||||
cr.line_to(square.br().x(), square.br().y());
|
||||
cr.line_to(square.br().x(), square.tl().y());
|
||||
cr.line_to(square.tl().x(), square.tl().y());
|
||||
cr.stroke().unwrap();
|
||||
cr.restore().unwrap();
|
||||
}
|
||||
|
||||
let am = available_moves.borrow();
|
||||
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(
|
||||
screen_position.col() as f64 * SQUARE_SIZE,
|
||||
@@ -333,7 +363,7 @@ fn on_activate(application: >k::Application) {
|
||||
cr.line_to(square.tl().x(), square.tl().y());
|
||||
cr.clip();
|
||||
cr.new_path();
|
||||
cr.set_source_rgb(0.0, 0.0, 1.0);
|
||||
cr.set_source_rgb(0.0, 1.0, 0.0);
|
||||
cr.set_line_width((square.width() + square.height()) * 0.05);
|
||||
cr.move_to(square.tl().x(), square.tl().y());
|
||||
cr.line_to(square.tl().x(), square.br().y());
|
||||
@@ -343,43 +373,9 @@ fn on_activate(application: >k::Application) {
|
||||
cr.stroke().unwrap();
|
||||
cr.restore().unwrap();
|
||||
}
|
||||
|
||||
let am = available_moves.borrow();
|
||||
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(
|
||||
screen_position.col() as f64 * SQUARE_SIZE,
|
||||
screen_position.row() as f64 * SQUARE_SIZE,
|
||||
SQUARE_SIZE,
|
||||
SQUARE_SIZE,
|
||||
);
|
||||
cr.save().unwrap();
|
||||
cr.new_path();
|
||||
cr.move_to(square.tl().x(), square.tl().y());
|
||||
cr.line_to(square.tl().x(), square.br().y());
|
||||
cr.line_to(square.br().x(), square.br().y());
|
||||
cr.line_to(square.br().x(), square.tl().y());
|
||||
cr.line_to(square.tl().x(), square.tl().y());
|
||||
cr.clip();
|
||||
cr.new_path();
|
||||
cr.set_source_rgb(0.0, 1.0, 0.0);
|
||||
cr.set_line_width((square.width() + square.height()) * 0.05);
|
||||
cr.move_to(square.tl().x(), square.tl().y());
|
||||
cr.line_to(square.tl().x(), square.br().y());
|
||||
cr.line_to(square.br().x(), square.br().y());
|
||||
cr.line_to(square.br().x(), square.tl().y());
|
||||
cr.line_to(square.tl().x(), square.tl().y());
|
||||
cr.stroke().unwrap();
|
||||
cr.restore().unwrap();
|
||||
}
|
||||
}
|
||||
draw_score_bar(cr, &board, &draughts_game.borrow());
|
||||
});
|
||||
}
|
||||
draw_score_bar(cr, &board, &draughts_game.borrow());
|
||||
});
|
||||
}
|
||||
let gesture = gtk::GestureClick::new();
|
||||
|
||||
@@ -402,7 +398,7 @@ fn on_activate(application: >k::Application) {
|
||||
if board_clone.contains(&p) {
|
||||
let p = &p - &board_clone.tl();
|
||||
// println!("Point: {:?}", p);
|
||||
let position = match *current_player {
|
||||
let position = match current_player {
|
||||
Player::White => Position::new(
|
||||
(8.0 - (p.y() / SQUARE_SIZE)) as u8,
|
||||
(p.x() / SQUARE_SIZE) as u8,
|
||||
@@ -411,7 +407,7 @@ fn on_activate(application: >k::Application) {
|
||||
Position::new((p.y() / SQUARE_SIZE) as u8, (p.x() / SQUARE_SIZE) as u8)
|
||||
}
|
||||
};
|
||||
println!("Selected position: {:?}", position);
|
||||
// println!("Selected position: {:?}", position);
|
||||
let mut draughts_game = draughts_game.borrow_mut();
|
||||
let piece = draughts_game.piece_at(position);
|
||||
// println!("Selected piece: {:?}", piece);
|
||||
@@ -421,9 +417,15 @@ fn on_activate(application: >k::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);
|
||||
// }
|
||||
let game_copy = draughts_game.clone();
|
||||
thread::spawn(move || {
|
||||
if let (Some(mv), analyzed_moves) = game_copy.get_best_move(10) {
|
||||
println!(
|
||||
"Next best move: {:?}, analyzed moves: {}",
|
||||
mv, analyzed_moves
|
||||
);
|
||||
}
|
||||
});
|
||||
move_applied = true;
|
||||
break;
|
||||
}
|
||||
@@ -452,17 +454,33 @@ fn on_activate(application: >k::Application) {
|
||||
} else {
|
||||
selected_piece.set(None);
|
||||
}
|
||||
drawing_area.borrow_mut().queue_draw();
|
||||
drawing_area.queue_draw();
|
||||
});
|
||||
}
|
||||
// Assign the gesture to the treeview
|
||||
drawing_area.borrow_mut().add_controller(gesture);
|
||||
drawing_area.add_controller(gesture);
|
||||
window.present();
|
||||
}
|
||||
|
||||
fn on_activate(application: &Application) {
|
||||
// Initialize GTK before using any GTK functions.
|
||||
if gtk::init().is_err() {
|
||||
panic!("Failed to initialize GTK.");
|
||||
}
|
||||
|
||||
let current_player = new_shared_mut(Player::Red);
|
||||
let dialog = greeting_dialog::create(application, current_player.clone());
|
||||
let application = application.clone();
|
||||
dialog.connect_close_request(move |w| {
|
||||
application.remove_window(w);
|
||||
create_game_window(&application, current_player.get());
|
||||
Propagation::Proceed
|
||||
});
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Create a new application with the builder pattern
|
||||
let app = gtk::Application::builder()
|
||||
let app = Application::builder()
|
||||
.application_id("net.woggioni.rdraught")
|
||||
.build();
|
||||
app.connect_activate(on_activate);
|
||||
|
12
rdraught-ui/src/types.rs
Normal file
12
rdraught-ui/src/types.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub(crate) type SharedMutable<T> = Rc<Cell<T>>;
|
||||
pub(crate) type SharedMutableRef<T> = Rc<RefCell<T>>;
|
||||
pub(crate) fn new_shared_mut_ref<T>(obj: T) -> SharedMutableRef<T> {
|
||||
Rc::new(RefCell::new(obj))
|
||||
}
|
||||
|
||||
pub(crate) fn new_shared_mut<T>(obj: T) -> SharedMutable<T> {
|
||||
Rc::new(Cell::new(obj))
|
||||
}
|
Reference in New Issue
Block a user