diff --git a/Makefile b/Makefile index 940003b..ef03c40 100644 --- a/Makefile +++ b/Makefile @@ -36,13 +36,13 @@ ifc-test: cd ifc && make test kifu-core/dev: - cd kifu/kifu-core && make test + cd kifu/core && make test kifu-core/test: - cd kifu/kifu-core && make test + cd kifu/core && make test kifu-core/test-oneshot: - cd kifu/kifu-core && make test-oneshot + cd kifu/core && make test-oneshot kifu-gtk: cd kifu/gtk && make release diff --git a/kifu/core/src/api.rs b/kifu/core/src/api.rs index 8635268..7e4e0d6 100644 --- a/kifu/core/src/api.rs +++ b/kifu/core/src/api.rs @@ -1,15 +1,21 @@ -use crate::types::AppState; -use crate::ui::{playing_field, PlayingFieldView}; +use crate::{ + types::{AppState, GameState, Player, Rank}, + ui::{new_game, playing_field, NewGameView, PlayingFieldView}, +}; use serde::{Deserialize, Serialize}; use std::sync::{Arc, RwLock}; use typeshare::typeshare; -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[typeshare] #[serde(tag = "type", content = "content")] pub enum CoreRequest { + CreateGame(CreateGameRequest), + LaunchScreen, + NewGame, PlayingField, - PlayStoneRequest(PlayStoneRequest), + PlayStone(PlayStoneRequest), + StartGame, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -19,10 +25,40 @@ pub struct PlayStoneRequest { pub row: u8, } +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[typeshare] +pub struct CreateGameRequest { + pub black_player: PlayerInfoRequest, + pub white_player: PlayerInfoRequest, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[typeshare] +pub enum PlayerInfoRequest { + Hotseat(HotseatPlayerRequest), +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[typeshare] +pub struct HotseatPlayerRequest { + pub name: String, + pub rank: Option, +} + +impl From for Player { + fn from(p: HotseatPlayerRequest) -> Self { + Self { + name: p.name, + rank: p.rank.and_then(|r| Rank::try_from(r.as_ref()).ok()), + } + } +} + #[derive(Clone, Debug, Serialize, Deserialize)] #[typeshare] #[serde(tag = "type", content = "content")] pub enum CoreResponse { + NewGameView(NewGameView), PlayingFieldView(PlayingFieldView), } @@ -40,18 +76,57 @@ impl CoreApp { pub async fn dispatch(&self, request: CoreRequest) -> CoreResponse { match request { + /* + CoreRequest::LaunchScreen => { + let app_state = self.state.read().unwrap(); + + At launch, I want to either show a list of games in progress, the current game, or the game creation screen. + - if a live game is in progress, immmediately go to that game. Such a game will be classified at game creation, so it should be persisted to the state. + - if no live games are in progress, but there are slow games in progress, show a list of the slow games and let the player choose which one to jump into. + - if no games are in progress, show only the game creation screen + - game creation menu should be present both when there are only slow games and when there are no games + - the UI returned here will always be available in other places, such as when the user is viewing a game and wants to return to this page + + For the initial version, I want only to show the game creation screen. Then I will backtrack record application state so that the only decisions can be made. + } + */ + CoreRequest::CreateGame(create_request) => { + let mut app_state = self.state.write().unwrap(); + let white_player = { + match create_request.white_player { + PlayerInfoRequest::Hotseat(request) => Player::from(request), + } + }; + let black_player = { + match create_request.black_player { + PlayerInfoRequest::Hotseat(request) => Player::from(request), + } + }; + app_state.game = Some(GameState { + white_player, + black_player, + ..GameState::new() + }); + let game_state = app_state.game.as_ref().unwrap(); + CoreResponse::PlayingFieldView(playing_field(game_state)) + } + CoreRequest::LaunchScreen => CoreResponse::NewGameView(new_game()), + CoreRequest::NewGame => CoreResponse::NewGameView(new_game()), CoreRequest::PlayingField => { let app_state = self.state.read().unwrap(); let game = app_state.game.as_ref().unwrap(); CoreResponse::PlayingFieldView(playing_field(game)) } - CoreRequest::PlayStoneRequest(request) => { + CoreRequest::PlayStone(request) => { let mut app_state = self.state.write().unwrap(); app_state.place_stone(request); let game = app_state.game.as_ref().unwrap(); CoreResponse::PlayingFieldView(playing_field(game)) } + CoreRequest::StartGame => { + unimplemented!() + } } } diff --git a/kifu/core/src/lib.rs b/kifu/core/src/lib.rs index bcdd2a0..fe0d321 100644 --- a/kifu/core/src/lib.rs +++ b/kifu/core/src/lib.rs @@ -1,8 +1,10 @@ mod api; -pub use api::{CoreApp, CoreRequest, CoreResponse}; +pub use api::{ + CoreApp, CoreRequest, CoreResponse, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest, +}; mod types; -pub use types::{BoardError, Color, Size}; +pub use types::{BoardError, Color, Rank, Size}; pub mod ui; mod board; diff --git a/kifu/core/src/types.rs b/kifu/core/src/types.rs index 3a1b3f3..3249ea4 100644 --- a/kifu/core/src/types.rs +++ b/kifu/core/src/types.rs @@ -55,7 +55,7 @@ impl AppState { pub fn place_stone(&mut self, req: PlayStoneRequest) { match self.game { Some(ref mut game) => { - game.place_stone(Coordinate { + let _ = game.place_stone(Coordinate { column: req.column, row: req.row, }); @@ -65,8 +65,9 @@ impl AppState { } } -#[derive(Debug)] -enum Rank { +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[typeshare] +pub enum Rank { Kyu(u8), Dan(u8), Pro(u8), @@ -92,8 +93,8 @@ impl From for String { #[derive(Debug)] pub struct Player { - name: String, - rank: Rank, + pub name: String, + pub rank: Option, } #[derive(Debug)] @@ -112,19 +113,19 @@ pub struct GameState { } impl GameState { - fn new() -> GameState { + pub fn new() -> GameState { GameState { board: Board::new(), past_positions: vec![], conversation: vec![], current_player: Color::Black, white_player: Player { - name: "Savanni".to_owned(), - rank: Rank::Kyu(10), + name: "".to_owned(), + rank: None, }, black_player: Player { - name: "Opal".to_owned(), - rank: Rank::Kyu(10), + name: "".to_owned(), + rank: None, }, white_clock: Duration::from_secs(600), black_clock: Duration::from_secs(600), diff --git a/kifu/core/src/ui/elements/action.rs b/kifu/core/src/ui/elements/action.rs new file mode 100644 index 0000000..69c5514 --- /dev/null +++ b/kifu/core/src/ui/elements/action.rs @@ -0,0 +1,10 @@ +use serde::{Deserialize, Serialize}; +use typeshare::typeshare; + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[typeshare] +pub struct Action { + pub id: String, + pub label: String, + pub action: A, +} diff --git a/kifu/core/src/ui/elements/menu.rs b/kifu/core/src/ui/elements/menu.rs new file mode 100644 index 0000000..25b06ed --- /dev/null +++ b/kifu/core/src/ui/elements/menu.rs @@ -0,0 +1,18 @@ +use serde::{Deserialize, Serialize}; +use typeshare::typeshare; + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[typeshare] +pub struct Menu { + current: Option, + options: Vec, +} + +impl Menu { + pub fn new(options: Vec, current: Option) -> Menu { + if let Some(ref current) = current { + assert!(options.contains(current)); + } + Menu { current, options } + } +} diff --git a/kifu/core/src/ui/elements/mod.rs b/kifu/core/src/ui/elements/mod.rs new file mode 100644 index 0000000..c5aac34 --- /dev/null +++ b/kifu/core/src/ui/elements/mod.rs @@ -0,0 +1,2 @@ +pub mod action; +pub mod menu; diff --git a/kifu/core/src/ui/launch_screen.rs b/kifu/core/src/ui/launch_screen.rs new file mode 100644 index 0000000..3696423 --- /dev/null +++ b/kifu/core/src/ui/launch_screen.rs @@ -0,0 +1,14 @@ +use crate::{ + ui::types; +}; +use serde::{Deserialize, Serialize}; +use typeshare::typeshare; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum LaunchScreenView { + CreateGame(CreateGameView) +} + +// This will be called when the Kifu application starts. +pub fn launch_screen() -> LaunchScreenView { +} diff --git a/kifu/core/src/ui/mod.rs b/kifu/core/src/ui/mod.rs index abe5612..6ae201e 100644 --- a/kifu/core/src/ui/mod.rs +++ b/kifu/core/src/ui/mod.rs @@ -1,6 +1,15 @@ +mod elements; +pub use elements::{action::Action, menu::Menu}; + mod playing_field; pub use playing_field::{playing_field, PlayingFieldView}; +// mod launch_screen; +// pub use launch_screen::{launch_screen, LaunchScreenView}; + +mod new_game; +pub use new_game::{new_game, HotseatPlayerElement, NewGameView, PlayerElement}; + mod types; pub use types::{ BoardElement, ChatElement, IntersectionElement, PlayerCardElement, StoneElement, diff --git a/kifu/core/src/ui/new_game.rs b/kifu/core/src/ui/new_game.rs new file mode 100644 index 0000000..afe913a --- /dev/null +++ b/kifu/core/src/ui/new_game.rs @@ -0,0 +1,77 @@ +use crate::ui::Action; +use serde::{Deserialize, Serialize}; +use typeshare::typeshare; + +fn rank_strings() -> Vec { + vec![ + "", "30 kyu", "29 kyu", "28 kyu", "27 kyu", "26 kyu", "25 kyu", "24 kyu", "23 kyu", + "22 kyu", "21 kyu", "20 kyu", "19 kyu", "18 kyu", "17 kyu", "16 kyu", "15 kyu", "14 kyu", + "13 kyu", "12 kyu", "11 kyu", "10 kyu", "9 kyu", "8 kyu", "7 kyu", "6 kyu", "5 kyu", + "4 kyu", "3 kyu", "2 kyu", "1 kyu", "1 dan", "2 dan", "3 dan", "4 dan", "5 dan", "6 dan", + "7 dan", "8 dan", "9 dan", "1 pro", "2 pro", "3 pro", "4 pro", "5 pro", "6 pro", "7 pro", + "8 pro", "9 pro", + ] + .into_iter() + .map(|s| s.to_owned()) + .collect() +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[typeshare] +pub enum PlayerElement { + Hotseat(HotseatPlayerElement), + // Remote(RemotePlayerElement), + // Bot(BotPlayerElement), +} + +impl Default for PlayerElement { + fn default() -> PlayerElement { + PlayerElement::Hotseat(HotseatPlayerElement::default()) + } +} + +#[derive(Clone, Debug, Serialize, Deserialize, Default)] +#[typeshare] +pub struct HotseatPlayerElement { + pub placeholder: Option, + pub default_rank: Option, + pub ranks: Vec, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[typeshare] +pub struct RemotePlayerElement {} + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[typeshare] +pub struct BotPlayerElement {} + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[typeshare] +pub struct NewGameView { + pub black_player: PlayerElement, + pub white_player: PlayerElement, + pub start_game: Action<()>, +} + +pub fn new_game() -> NewGameView { + let black_player = PlayerElement::Hotseat(HotseatPlayerElement { + placeholder: Some("black player".to_owned()), + default_rank: None, + ranks: rank_strings(), + }); + let white_player = PlayerElement::Hotseat(HotseatPlayerElement { + placeholder: Some("white player".to_owned()), + default_rank: None, + ranks: rank_strings(), + }); + NewGameView { + black_player, + white_player, + start_game: Action { + id: "start-game-action".to_owned(), + label: "New Game".to_owned(), + action: (), + }, + } +} diff --git a/kifu/core/src/ui/playing_field.rs b/kifu/core/src/ui/playing_field.rs index 2e8592c..d5ed200 100644 --- a/kifu/core/src/ui/playing_field.rs +++ b/kifu/core/src/ui/playing_field.rs @@ -21,16 +21,24 @@ pub fn playing_field(game: &GameState) -> PlayingFieldView { let player_card_black = types::PlayerCardElement { color: Color::Black, - name: "Savanni".to_owned(), - rank: "10k".to_owned(), - clock: "24:53".to_owned(), + name: game.black_player.name.clone(), + rank: game + .black_player + .rank + .map(String::from) + .unwrap_or("".to_owned()), + clock: "".to_owned(), }; let player_card_white = types::PlayerCardElement { color: Color::White, - name: "Opal".to_owned(), - rank: "10k".to_owned(), - clock: "25:00".to_owned(), + name: game.white_player.name.clone(), + rank: game + .black_player + .rank + .map(String::from) + .unwrap_or("".to_owned()), + clock: "".to_owned(), }; let chat = types::ChatElement { diff --git a/kifu/core/src/ui/types.rs b/kifu/core/src/ui/types.rs index c12c416..1454a31 100644 --- a/kifu/core/src/ui/types.rs +++ b/kifu/core/src/ui/types.rs @@ -31,7 +31,7 @@ impl StoneElement { } } -#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[typeshare] #[serde(tag = "type", content = "content")] pub enum IntersectionElement { @@ -53,9 +53,10 @@ impl BoardElement { .map(|row| { (0..size.width) .map(|column| { - IntersectionElement::Empty(CoreRequest::PlayStoneRequest( - PlayStoneRequest { column, row }, - )) + IntersectionElement::Empty(CoreRequest::PlayStone(PlayStoneRequest { + column, + row, + })) }) .collect::>() }) @@ -66,7 +67,12 @@ impl BoardElement { pub fn stone(&self, column: u8, row: u8) -> IntersectionElement { let addr = self.addr(column, row); - self.spaces[addr] + self.spaces[addr].clone() + } + + pub fn stone_mut(&mut self, column: u8, row: u8) -> &mut IntersectionElement { + let addr = self.addr(column, row); + &mut self.spaces[addr] } fn addr(&self, column: u8, row: u8) -> usize { @@ -76,23 +82,24 @@ impl BoardElement { impl From<&Board> for BoardElement { fn from(board: &Board) -> Self { - let spaces: Vec = (0..board.size.height) - .map(|row| { - (0..board.size.width) - .map(|column| match board.stone(&Coordinate { column, row }) { - Some(color) => IntersectionElement::Filled(StoneElement { - jitter: Jitter { x: 0, y: 0 }, - liberties: None, - color, - }), - None => IntersectionElement::Empty(CoreRequest::PlayStoneRequest( - PlayStoneRequest { column, row }, - )), - }) - .collect::>() - }) - .collect::>>() - .concat(); + let spaces: Vec = + (0..board.size.height) + .map(|row| { + (0..board.size.width) + .map(|column| match board.stone(&Coordinate { column, row }) { + Some(color) => IntersectionElement::Filled(StoneElement { + jitter: Jitter { x: 0, y: 0 }, + liberties: None, + color, + }), + None => IntersectionElement::Empty(CoreRequest::PlayStone( + PlayStoneRequest { column, row }, + )), + }) + .collect::>() + }) + .collect::>>() + .concat(); Self { size: board.size, spaces, diff --git a/kifu/gtk/Makefile b/kifu/gtk/Makefile index 2297add..be968cd 100644 --- a/kifu/gtk/Makefile +++ b/kifu/gtk/Makefile @@ -3,3 +3,7 @@ release: dev: cargo watch -x 'run --bin kifu-gtk' + +screenplay: + cargo watch -x 'run --bin screenplay --features="screenplay"' + diff --git a/kifu/gtk/src/lib.rs b/kifu/gtk/src/lib.rs index b0ef459..94c88d6 100644 --- a/kifu/gtk/src/lib.rs +++ b/kifu/gtk/src/lib.rs @@ -19,3 +19,14 @@ impl CoreApi { }); } } + +pub fn perftrace(trace_name: &str, f: F) -> A +where + F: FnOnce() -> A, +{ + let start = std::time::Instant::now(); + let result = f(); + let end = std::time::Instant::now(); + println!("[Trace: {}] {:?}", trace_name, end - start); + return result; +} diff --git a/kifu/gtk/src/main.rs b/kifu/gtk/src/main.rs index 931aae5..3e34e3e 100644 --- a/kifu/gtk/src/main.rs +++ b/kifu/gtk/src/main.rs @@ -1,8 +1,38 @@ use gtk::prelude::*; use kifu_core::{CoreApp, CoreRequest, CoreResponse}; -use kifu_gtk::{ui::PlayingField, CoreApi}; +use kifu_gtk::{ + perftrace, + ui::{NewGame, PlayingField}, + CoreApi, +}; use std::sync::{Arc, RwLock}; +fn handle_response(api: CoreApi, window: gtk::ApplicationWindow, message: CoreResponse) { + let playing_field = Arc::new(RwLock::new(None)); + match message { + CoreResponse::NewGameView(view) => perftrace("NewGameView", || { + let api = api.clone(); + + let new_game = NewGame::new(api, view); + window.set_child(Some(&new_game)); + }), + CoreResponse::PlayingFieldView(view) => perftrace("PlayingFieldView", || { + let api = api.clone(); + + let mut playing_field = playing_field.write().unwrap(); + if playing_field.is_none() { + perftrace("creating a new playing field", || { + let field = PlayingField::new(api, view); + window.set_child(Some(&field)); + *playing_field = Some(field); + }) + } else { + playing_field.as_ref().map(|field| field.update_view(view)); + } + }), + } +} + fn main() { gio::resources_register_include!("com.luminescent-dreams.kifu-gtk.gresource") .expect("Failed to register resources"); @@ -44,30 +74,15 @@ fn main() { gtk_rx.attach(None, { let api = api.clone(); - let playing_field = Arc::new(RwLock::new(None)); move |message| { - match message { - CoreResponse::PlayingFieldView(view) => { - let api = api.clone(); - - let start = std::time::Instant::now(); - let mut playing_field = playing_field.write().unwrap(); - if playing_field.is_none() { - let field = PlayingField::new(api, view); - window.set_child(Some(&field)); - *playing_field = Some(field); - } else { - playing_field.as_ref().map(|field| field.update_view(view)); - } - let end = std::time::Instant::now(); - println!("Time to render the playing field: {:?}", end - start); - } - } + perftrace("handle_response", || { + handle_response(api.clone(), window.clone(), message) + }); Continue(true) } }); - api.dispatch(CoreRequest::PlayingField); + api.dispatch(CoreRequest::NewGame); } }); diff --git a/kifu/gtk/src/ui/board.rs b/kifu/gtk/src/ui/board.rs index 2940066..fa3e67d 100644 --- a/kifu/gtk/src/ui/board.rs +++ b/kifu/gtk/src/ui/board.rs @@ -1,4 +1,4 @@ -use crate::CoreApi; +use crate::{perftrace, CoreApi}; use gio::resources_lookup_data; use glib::Object; use gtk::{ @@ -83,86 +83,88 @@ impl ObjectImpl for BoardPrivate { self.drawing_area .set_draw_func(move |_, context, width, height| { - let render_start = std::time::Instant::now(); - let board = board.borrow(); + perftrace("render drawing area", || { + let render_start = std::time::Instant::now(); + let board = board.borrow(); - match background { - Ok(Some(ref background)) => { - context.set_source_pixbuf(&background, 0., 0.); - context.paint().expect("paint should succeed"); - } - Ok(None) | Err(_) => context.set_source_rgb(0.7, 0.7, 0.7), - }; - let _ = context.paint(); + match background { + Ok(Some(ref background)) => { + context.set_source_pixbuf(&background, 0., 0.); + context.paint().expect("paint should succeed"); + } + Ok(None) | Err(_) => context.set_source_rgb(0.7, 0.7, 0.7), + }; + let _ = context.paint(); - context.set_source_rgb(0.1, 0.1, 0.1); - context.set_line_width(2.); - let hspace_between = ((width - 40) as f64) / ((board.size.width - 1) as f64); - let vspace_between = ((height - 40) as f64) / ((board.size.height - 1) as f64); + context.set_source_rgb(0.1, 0.1, 0.1); + context.set_line_width(2.); + let hspace_between = ((width - 40) as f64) / ((board.size.width - 1) as f64); + let vspace_between = ((height - 40) as f64) / ((board.size.height - 1) as f64); - let pen = Pen { - x_offset: MARGIN as f64, - y_offset: MARGIN as f64, - hspace_between, - vspace_between, - }; + let pen = Pen { + x_offset: MARGIN as f64, + y_offset: MARGIN as f64, + hspace_between, + vspace_between, + }; - (0..board.size.width).for_each(|col| { - context.move_to( - (MARGIN as f64) + (col as f64) * hspace_between, - MARGIN as f64, - ); - context.line_to( - (MARGIN as f64) + (col as f64) * hspace_between, - (height as f64) - (MARGIN as f64), - ); - let _ = context.stroke(); - }); - (0..board.size.height).for_each(|row| { - context.move_to( - MARGIN as f64, - (MARGIN as f64) + (row as f64) * vspace_between, - ); - context.line_to( - (width - MARGIN) as f64, - (MARGIN as f64) + (row as f64) * vspace_between, - ); - let _ = context.stroke(); - }); - - context.set_source_rgb(0.1, 0.1, 0.0); - vec![3, 9, 15].into_iter().for_each(|col| { - vec![3, 9, 15].into_iter().for_each(|row| { - pen.star_point(context, col, row); + (0..board.size.width).for_each(|col| { + context.move_to( + (MARGIN as f64) + (col as f64) * hspace_between, + MARGIN as f64, + ); + context.line_to( + (MARGIN as f64) + (col as f64) * hspace_between, + (height as f64) - (MARGIN as f64), + ); + let _ = context.stroke(); + }); + (0..board.size.height).for_each(|row| { + context.move_to( + MARGIN as f64, + (MARGIN as f64) + (row as f64) * vspace_between, + ); + context.line_to( + (width - MARGIN) as f64, + (MARGIN as f64) + (row as f64) * vspace_between, + ); + let _ = context.stroke(); }); - }); - (0..19).for_each(|col| { - (0..19).for_each(|row| { - match board.stone(row, col) { - IntersectionElement::Filled(stone) => { - pen.stone(&context, row, col, stone.color, stone.liberties); - } + context.set_source_rgb(0.1, 0.1, 0.0); + vec![3, 9, 15].into_iter().for_each(|col| { + vec![3, 9, 15].into_iter().for_each(|row| { + pen.star_point(context, col, row); + }); + }); + + (0..19).for_each(|col| { + (0..19).for_each(|row| { + match board.stone(row, col) { + IntersectionElement::Filled(stone) => { + pen.stone(&context, row, col, stone.color, stone.liberties); + } + _ => {} + }; + }) + }); + + let cursor = cursor_location.borrow(); + match *cursor { + None => {} + Some(ref cursor) => match board.stone(cursor.row, cursor.column) { + IntersectionElement::Empty(_) => pen.ghost_stone( + context, + cursor.row, + cursor.column, + *current_player.borrow(), + ), _ => {} - }; - }) - }); - - let cursor = cursor_location.borrow(); - match *cursor { - None => {} - Some(ref cursor) => match board.stone(cursor.row, cursor.column) { - IntersectionElement::Empty(_) => pen.ghost_stone( - context, - cursor.row, - cursor.column, - *current_player.borrow(), - ), - _ => {} - }, - } - let render_end = std::time::Instant::now(); - println!("board rendering time: {:?}", render_end - render_start); + }, + } + let render_end = std::time::Instant::now(); + println!("board rendering time: {:?}", render_end - render_start); + }) }); let motion_controller = gtk::EventControllerMotion::new(); diff --git a/kifu/gtk/src/ui/mod.rs b/kifu/gtk/src/ui/mod.rs index 6a469b3..7fa21fe 100644 --- a/kifu/gtk/src/ui/mod.rs +++ b/kifu/gtk/src/ui/mod.rs @@ -7,6 +7,9 @@ pub use chat::Chat; mod playing_field; pub use playing_field::PlayingField; +mod new_game; +pub use new_game::NewGame; + mod board; pub use board::Board; diff --git a/kifu/gtk/src/ui/new_game.rs b/kifu/gtk/src/ui/new_game.rs new file mode 100644 index 0000000..d037ef2 --- /dev/null +++ b/kifu/gtk/src/ui/new_game.rs @@ -0,0 +1,150 @@ +use crate::CoreApi; +use glib::Object; +use gtk::{glib, prelude::*, subclass::prelude::*}; +use kifu_core::{ + ui::{NewGameView, PlayerElement}, + CoreRequest, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest, +}; +use std::{cell::RefCell, rc::Rc}; + +struct PlayerDataEntryPrivate { + name: gtk::Text, + rank: gtk::DropDown, +} + +impl Default for PlayerDataEntryPrivate { + fn default() -> Self { + let rank = gtk::DropDown::builder().build(); + Self { + name: gtk::Text::builder().build(), + rank, + } + } +} + +#[glib::object_subclass] +impl ObjectSubclass for PlayerDataEntryPrivate { + const NAME: &'static str = "PlayerDataEntry"; + type Type = PlayerDataEntry; + type ParentType = gtk::Box; +} + +impl ObjectImpl for PlayerDataEntryPrivate {} +impl WidgetImpl for PlayerDataEntryPrivate {} +impl BoxImpl for PlayerDataEntryPrivate {} + +glib::wrapper! { + struct PlayerDataEntry(ObjectSubclass) @extends gtk::Box, gtk::Widget; +} + +impl PlayerDataEntry { + pub fn new(element: PlayerElement) -> PlayerDataEntry { + let s: Self = Object::builder().build(); + + let rank_model = gio::ListStore::new(gtk::StringObject::static_type()); + s.imp().rank.set_model(Some(&rank_model)); + + match element { + PlayerElement::Hotseat(player) => { + if let Some(placeholder) = player.placeholder { + s.imp().name.set_placeholder_text(Some(&placeholder)); + } + player.ranks.iter().for_each(|rank| rank_model.append(>k::StringObject::new(rank))); + } + // PlayerElement::Remote(_) => s.imp().placeholder.set_text("remote player"), + // PlayerElement::Bot(_) => s.imp().placeholder.set_text("bot player"), + } + + s.append(&s.imp().name); + s.append(&s.imp().rank); + + s + } + + pub fn text(&self) -> String { + let name = self.imp().name.buffer().text().to_string(); + if name.is_empty() { + self.imp() + .name + .placeholder_text() + .map(|s| s.to_string()) + .unwrap_or("".to_owned()) + } else { + name + } + } + + pub fn rank(&self) -> Option { + self.imp().rank.selected_item().and_then(|obj| { + let str_obj = obj.downcast::().ok()?; + Some(str_obj.string().clone().to_string()) + }) + } +} + +pub struct NewGamePrivate { + black_player: Rc>>, + white_player: Rc>>, +} + +impl Default for NewGamePrivate { + fn default() -> Self { + Self { + black_player: Rc::new(RefCell::new(None)), + white_player: Rc::new(RefCell::new(None)), + } + } +} + +#[glib::object_subclass] +impl ObjectSubclass for NewGamePrivate { + const NAME: &'static str = "NewGame"; + type Type = NewGame; + type ParentType = gtk::Grid; +} + +impl ObjectImpl for NewGamePrivate {} +impl WidgetImpl for NewGamePrivate {} +impl GridImpl for NewGamePrivate {} + +glib::wrapper! { + pub struct NewGame(ObjectSubclass) @extends gtk::Grid, gtk::Widget; +} + +impl NewGame { + pub fn new(api: CoreApi, view: NewGameView) -> NewGame { + let s: Self = Object::builder().build(); + + let black_player = PlayerDataEntry::new(view.black_player); + s.attach(&black_player, 1, 1, 1, 1); + *s.imp().black_player.borrow_mut() = Some(black_player.clone()); + + let white_player = PlayerDataEntry::new(view.white_player); + s.attach(&white_player, 2, 1, 1, 1); + *s.imp().white_player.borrow_mut() = Some(white_player.clone()); + + let new_game_button = gtk::Button::builder().label(&view.start_game.label).build(); + s.attach(&new_game_button, 2, 2, 1, 1); + + new_game_button.connect_clicked({ + move |_| { + let black_player = black_player.clone(); + let white_player = white_player.clone(); + + api.dispatch(CoreRequest::CreateGame(CreateGameRequest { + black_player: player_info(black_player.clone()), + white_player: player_info(white_player.clone()), + })); + } + }); + + s + } +} + +fn player_info(player: PlayerDataEntry) -> PlayerInfoRequest { + PlayerInfoRequest::Hotseat(HotseatPlayerRequest { + name: player.text(), + rank: player.rank(), + }) +} diff --git a/kifu/gtk/src/ui/playing_field.rs b/kifu/gtk/src/ui/playing_field.rs index afcb93d..dce713e 100644 --- a/kifu/gtk/src/ui/playing_field.rs +++ b/kifu/gtk/src/ui/playing_field.rs @@ -1,5 +1,8 @@ -use crate::ui::{Board, Chat, PlayerCard}; -use crate::CoreApi; +use crate::{ + perftrace, + ui::{Board, Chat, PlayerCard}, + CoreApi, +}; use glib::Object; use gtk::{prelude::*, subclass::prelude::*}; use kifu_core::ui::PlayingFieldView; @@ -69,10 +72,12 @@ impl PlayingField { } pub fn update_view(&self, view: PlayingFieldView) { - self.imp().board.borrow().as_ref().map(|board| { - board.set_board(view.board); - board.set_current_player(view.current_player); - }); + perftrace("update_view", || { + self.imp().board.borrow().as_ref().map(|board| { + board.set_board(view.board); + board.set_current_player(view.current_player); + }); + }) } } @@ -82,8 +87,8 @@ pub fn playing_field_view() -> PlayingFieldView { width: 19, height: 19, }); - *board.stone_mut(4, 4) = IntersectionElement::Filled(StoneElement::new(Color::White)); - *board.stone_mut(15, 15) = IntersectionElement::Filled(StoneElement::new(Color::Black)); + *board.stone_mut(4, 4) = IntersectionElement::Filled(StoneElement::new(Color::White, None)); + *board.stone_mut(15, 15) = IntersectionElement::Filled(StoneElement::new(Color::Black, None)); *board.stone_mut(18, 18) = IntersectionElement::Unplayable; let player_card_black = PlayerCardElement { color: Color::Black,