Go back to creating a new game

This commit is contained in:
Savanni D'Gerinel 2023-06-15 00:09:50 -04:00
parent d0db274110
commit ebb7062041
8 changed files with 62 additions and 27 deletions

View File

@ -28,8 +28,8 @@ pub struct PlayStoneRequest {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[typeshare] #[typeshare]
pub struct CreateGameRequest { pub struct CreateGameRequest {
black_player: PlayerInfoRequest, pub black_player: PlayerInfoRequest,
white_player: PlayerInfoRequest, pub white_player: PlayerInfoRequest,
} }
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@ -41,8 +41,8 @@ pub enum PlayerInfoRequest {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[typeshare] #[typeshare]
pub struct HotseatPlayerRequest { pub struct HotseatPlayerRequest {
name: Option<String>, pub name: Option<String>,
rank: Option<Rank>, pub rank: Option<Rank>,
} }
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]

View File

@ -1,5 +1,7 @@
mod api; mod api;
pub use api::{CoreApp, CoreRequest, CoreResponse}; pub use api::{
CoreApp, CoreRequest, CoreResponse, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest,
};
mod types; mod types;
pub use types::{BoardError, Color, Rank, Size}; pub use types::{BoardError, Color, Rank, Size};

View File

@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[typeshare]
pub struct Action<A> {
pub id: String,
pub label: String,
pub action: A,
}

View File

@ -0,0 +1,18 @@
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[typeshare]
pub struct Menu<T: PartialEq + Eq> {
current: Option<T>,
options: Vec<T>,
}
impl<T: PartialEq + Eq> Menu<T> {
pub fn new(options: Vec<T>, current: Option<T>) -> Menu<T> {
if let Some(ref current) = current {
assert!(options.contains(current));
}
Menu { current, options }
}
}

View File

@ -0,0 +1,2 @@
pub mod action;
pub mod menu;

View File

@ -1,3 +1,6 @@
mod elements;
pub use elements::{action::Action, menu::Menu};
mod playing_field; mod playing_field;
pub use playing_field::{playing_field, PlayingFieldView}; pub use playing_field::{playing_field, PlayingFieldView};
@ -5,7 +8,7 @@ pub use playing_field::{playing_field, PlayingFieldView};
// pub use launch_screen::{launch_screen, LaunchScreenView}; // pub use launch_screen::{launch_screen, LaunchScreenView};
mod new_game; mod new_game;
pub use new_game::{new_game, HotseatPlayerElement, Menu, NewGameView, PlayerElement}; pub use new_game::{new_game, HotseatPlayerElement, NewGameView, PlayerElement};
mod types; mod types;
pub use types::{ pub use types::{

View File

@ -1,3 +1,4 @@
use crate::ui::Action;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use typeshare::typeshare; use typeshare::typeshare;
@ -15,22 +16,6 @@ fn rank_strings() -> Vec<String> {
.collect() .collect()
} }
#[derive(Clone, Debug, Serialize, Deserialize)]
#[typeshare]
pub struct Menu<T: PartialEq + Eq> {
current: Option<T>,
options: Vec<T>,
}
impl<T: PartialEq + Eq> Menu<T> {
pub fn new(options: Vec<T>, current: Option<T>) -> Menu<T> {
if let Some(ref current) = current {
assert!(options.contains(current));
}
Menu { current, options }
}
}
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
#[typeshare] #[typeshare]
pub enum PlayerElement { pub enum PlayerElement {
@ -66,6 +51,7 @@ pub struct BotPlayerElement {}
pub struct NewGameView { pub struct NewGameView {
pub black_player: PlayerElement, pub black_player: PlayerElement,
pub white_player: PlayerElement, pub white_player: PlayerElement,
pub start_game: Action<()>,
} }
pub fn new_game() -> NewGameView { pub fn new_game() -> NewGameView {
@ -82,5 +68,10 @@ pub fn new_game() -> NewGameView {
NewGameView { NewGameView {
black_player, black_player,
white_player, white_player,
start_game: Action {
id: "start-game-action".to_owned(),
label: "New Game".to_owned(),
action: (),
},
} }
} }

View File

@ -1,7 +1,10 @@
use crate::CoreApi; use crate::CoreApi;
use glib::{subclass, Object, ParamSpec, Properties, Value}; use glib::{subclass, Object, ParamSpec, Properties, Value};
use gtk::{glib, prelude::*, subclass::prelude::*}; use gtk::{glib, prelude::*, subclass::prelude::*};
use kifu_core::ui::{NewGameView, PlayerElement}; use kifu_core::{
ui::{NewGameView, PlayerElement},
CoreRequest, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest,
};
use std::{cell::Cell, cell::RefCell, rc::Rc}; use std::{cell::Cell, cell::RefCell, rc::Rc};
struct PlayerDataEntryPrivate { struct PlayerDataEntryPrivate {
@ -100,15 +103,21 @@ impl NewGame {
s.attach(&white_player, 2, 1, 1, 1); s.attach(&white_player, 2, 1, 1, 1);
*s.imp().white_player.borrow_mut() = Some(white_player); *s.imp().white_player.borrow_mut() = Some(white_player);
let new_game_button = gtk::Button::builder().label("Start Game").build(); let new_game_button = gtk::Button::builder().label(&view.start_game.label).build();
s.attach(&new_game_button, 2, 2, 1, 1); s.attach(&new_game_button, 2, 2, 1, 1);
/*
new_game_button.connect_clicked(move |_| { new_game_button.connect_clicked(move |_| {
api.dispatch(CoreRequest::CreatGameRequest(CreateGameRequest { api.dispatch(CoreRequest::CreateGame(CreateGameRequest {
black_player: PlayerInfoRequest::Hotseat(HotseatPlayerRequest {
name: None,
rank: None,
}),
white_player: PlayerInfoRequest::Hotseat(HotseatPlayerRequest {
name: None,
rank: None,
}),
})); }));
}); });
*/
s s
} }