2023-08-20 03:45:19 +00:00
|
|
|
use crate::CoreApi;
|
|
|
|
use glib::Object;
|
2023-06-15 03:49:03 +00:00
|
|
|
use gtk::{glib, prelude::*, subclass::prelude::*};
|
2023-06-15 04:09:50 +00:00
|
|
|
use kifu_core::{
|
2023-08-20 03:45:19 +00:00
|
|
|
ui::{HomeView, PlayerElement},
|
2023-06-15 04:09:50 +00:00
|
|
|
CoreRequest, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest,
|
|
|
|
};
|
2023-08-20 03:45:19 +00:00
|
|
|
use std::{cell::RefCell, rc::Rc};
|
2023-08-20 00:46:43 +00:00
|
|
|
|
|
|
|
use super::GameDatabase;
|
2023-06-15 03:49:03 +00:00
|
|
|
|
|
|
|
struct PlayerDataEntryPrivate {
|
2023-06-16 02:20:59 +00:00
|
|
|
name: gtk::Text,
|
2023-06-15 03:49:03 +00:00
|
|
|
rank: gtk::DropDown,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PlayerDataEntryPrivate {
|
|
|
|
fn default() -> Self {
|
|
|
|
let rank = gtk::DropDown::builder().build();
|
|
|
|
Self {
|
2023-06-16 02:20:59 +00:00
|
|
|
name: gtk::Text::builder().build(),
|
2023-06-15 03:49:03 +00:00
|
|
|
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<PlayerDataEntryPrivate>) @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 {
|
2023-06-16 02:20:59 +00:00
|
|
|
s.imp().name.set_placeholder_text(Some(&placeholder));
|
2023-06-15 03:49:03 +00:00
|
|
|
}
|
|
|
|
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"),
|
|
|
|
}
|
|
|
|
|
2023-06-16 02:20:59 +00:00
|
|
|
s.append(&s.imp().name);
|
2023-06-15 03:49:03 +00:00
|
|
|
s.append(&s.imp().rank);
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
2023-06-16 02:20:59 +00:00
|
|
|
|
|
|
|
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<String> {
|
|
|
|
self.imp().rank.selected_item().and_then(|obj| {
|
|
|
|
let str_obj = obj.downcast::<gtk::StringObject>().ok()?;
|
|
|
|
Some(str_obj.string().clone().to_string())
|
|
|
|
})
|
|
|
|
}
|
2023-06-15 03:49:03 +00:00
|
|
|
}
|
2023-05-26 04:16:40 +00:00
|
|
|
|
2023-07-24 23:43:22 +00:00
|
|
|
pub struct HomePrivate {
|
2023-06-15 03:49:03 +00:00
|
|
|
black_player: Rc<RefCell<Option<PlayerDataEntry>>>,
|
|
|
|
white_player: Rc<RefCell<Option<PlayerDataEntry>>>,
|
2023-05-26 04:16:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 23:43:22 +00:00
|
|
|
impl Default for HomePrivate {
|
2023-05-26 04:16:40 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2023-06-15 03:49:03 +00:00
|
|
|
black_player: Rc::new(RefCell::new(None)),
|
|
|
|
white_player: Rc::new(RefCell::new(None)),
|
2023-05-26 04:16:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
2023-07-24 23:43:22 +00:00
|
|
|
impl ObjectSubclass for HomePrivate {
|
|
|
|
const NAME: &'static str = "Home";
|
|
|
|
type Type = Home;
|
2023-08-20 03:45:19 +00:00
|
|
|
type ParentType = gtk::Box;
|
2023-05-26 04:16:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 23:43:22 +00:00
|
|
|
impl ObjectImpl for HomePrivate {}
|
|
|
|
impl WidgetImpl for HomePrivate {}
|
2023-08-20 03:45:19 +00:00
|
|
|
impl BoxImpl for HomePrivate {}
|
2023-05-26 04:16:40 +00:00
|
|
|
|
|
|
|
glib::wrapper! {
|
2023-08-20 03:45:19 +00:00
|
|
|
pub struct Home(ObjectSubclass<HomePrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
|
2023-05-26 04:16:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 23:43:22 +00:00
|
|
|
impl Home {
|
|
|
|
pub fn new(api: CoreApi, view: HomeView) -> Home {
|
2023-05-26 04:16:40 +00:00
|
|
|
let s: Self = Object::builder().build();
|
2023-08-20 03:45:19 +00:00
|
|
|
s.set_spacing(4);
|
|
|
|
s.set_homogeneous(false);
|
|
|
|
s.set_orientation(gtk::Orientation::Vertical);
|
|
|
|
|
|
|
|
let players = gtk::Box::builder()
|
|
|
|
.spacing(4)
|
|
|
|
.orientation(gtk::Orientation::Horizontal)
|
|
|
|
.build();
|
|
|
|
s.append(&players);
|
2023-05-26 04:16:40 +00:00
|
|
|
|
2023-06-15 03:49:03 +00:00
|
|
|
let black_player = PlayerDataEntry::new(view.black_player);
|
2023-08-20 03:45:19 +00:00
|
|
|
players.append(&black_player);
|
2023-06-16 02:20:59 +00:00
|
|
|
*s.imp().black_player.borrow_mut() = Some(black_player.clone());
|
2023-06-15 03:49:03 +00:00
|
|
|
let white_player = PlayerDataEntry::new(view.white_player);
|
2023-08-20 03:45:19 +00:00
|
|
|
players.append(&white_player);
|
2023-06-16 02:20:59 +00:00
|
|
|
*s.imp().white_player.borrow_mut() = Some(white_player.clone());
|
2023-06-15 03:49:03 +00:00
|
|
|
|
2023-06-15 04:09:50 +00:00
|
|
|
let new_game_button = gtk::Button::builder().label(&view.start_game.label).build();
|
2023-08-20 03:45:19 +00:00
|
|
|
s.append(&new_game_button);
|
2023-06-15 03:49:03 +00:00
|
|
|
|
2023-08-20 03:24:01 +00:00
|
|
|
let library = GameDatabase::new();
|
|
|
|
let library_view = gtk::ScrolledWindow::builder()
|
|
|
|
.hscrollbar_policy(gtk::PolicyType::Never)
|
|
|
|
.min_content_width(360)
|
2023-08-20 03:45:19 +00:00
|
|
|
.vexpand(true)
|
|
|
|
.hexpand(true)
|
2023-08-20 03:24:01 +00:00
|
|
|
.child(&library)
|
|
|
|
.build();
|
2023-08-20 03:45:19 +00:00
|
|
|
s.append(&library_view);
|
2023-08-20 03:24:01 +00:00
|
|
|
|
|
|
|
library.set_games(view.games);
|
2023-08-20 00:46:43 +00:00
|
|
|
|
2023-06-16 02:20:59 +00:00
|
|
|
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()),
|
|
|
|
}));
|
|
|
|
}
|
2023-06-15 03:49:03 +00:00
|
|
|
});
|
2023-05-26 04:16:40 +00:00
|
|
|
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
2023-06-16 02:20:59 +00:00
|
|
|
|
|
|
|
fn player_info(player: PlayerDataEntry) -> PlayerInfoRequest {
|
|
|
|
PlayerInfoRequest::Hotseat(HotseatPlayerRequest {
|
|
|
|
name: player.text(),
|
|
|
|
rank: player.rank(),
|
|
|
|
})
|
|
|
|
}
|