can now run the game

This commit is contained in:
ALEZ-DEV 2024-12-16 21:11:15 +01:00
parent f6247cee5a
commit 85bb1e2c59
6 changed files with 118 additions and 63 deletions

View File

@ -72,6 +72,7 @@ dependencies = [
"log",
"relm4",
"relm4-components",
"wincompatlib",
]
[[package]]
@ -402,7 +403,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d"
dependencies = [
"libc",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@ -1806,7 +1807,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@ -2195,7 +2196,7 @@ dependencies = [
"fastrand",
"once_cell",
"rustix",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@ -2546,7 +2547,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]

View File

@ -9,3 +9,4 @@ anyhow = "1.0.94"
log = "0.4.22"
relm4 = "0.9.1"
relm4-components = "0.9.1"
wincompatlib = "0.7.5"

View File

@ -1,87 +1,112 @@
use babylonia_terminal_sdk::game_state::GameState;
use log::debug;
use relm4::RelmApp;
use manager::run_game;
use relm4::{
gtk::{
self,
prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt},
prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt, WidgetExt},
},
ComponentParts, RelmWidgetExt, SimpleComponent,
loading_widgets::LoadingWidgets,
prelude::{AsyncComponent, AsyncComponentParts, SimpleAsyncComponent},
view, ComponentParts, RelmWidgetExt, SimpleComponent,
};
use relm4::{Component, RelmApp};
mod manager;
pub fn run() {
debug!("Start GUI!");
let app = RelmApp::new("moe.celica.BabyloniaTerminal").with_args(vec![]);
app.run::<AppModel>(0);
app.run_async::<MainWindow>(None);
}
#[derive(Debug)]
pub enum AppMsg {
Increment,
Decrement,
pub enum MainWindowMsg {
RunGame,
}
struct AppModel {
counter: u8,
struct MainWindow {
game_state: GameState,
is_game_running: bool,
}
#[relm4::component]
impl SimpleComponent for AppModel {
type Input = AppMsg;
impl MainWindow {
fn new(game_state: GameState) -> Self {
MainWindow {
game_state,
is_game_running: false,
}
}
}
#[relm4::component(async)]
impl SimpleAsyncComponent for MainWindow {
type Input = MainWindowMsg;
type Output = ();
type Init = u8;
type Init = Option<GameState>;
view! {
#[root]
gtk::Window {
set_title: Some("Babylonia Terminal"),
set_default_width: 700,
set_default_height: 300,
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_spacing: 5,
set_margin_all: 5,
#[name(start_button)]
gtk::Button {
set_label: "Increment",
connect_clicked => AppMsg::Increment,
set_label: "Start game",
connect_clicked => MainWindowMsg::RunGame,
},
gtk::Button {
set_label: "Decrement",
connect_clicked => AppMsg::Decrement,
},
gtk::Label {
#[watch]
set_label: &format!("Counter : {}", model.counter),
set_margin_all: 5,
}
}
}
}
fn init(
counter: Self::Init,
window: Self::Root,
sender: relm4::ComponentSender<Self>,
) -> relm4::ComponentParts<Self> {
let model = AppModel { counter };
async fn init(
game_state: Self::Init,
root: Self::Root,
sender: relm4::AsyncComponentSender<Self>,
) -> AsyncComponentParts<Self> {
let model;
if game_state.is_none() {
model = MainWindow::new(
babylonia_terminal_sdk::game_state::GameState::get_current_state()
.await
.unwrap(),
);
} else {
model = MainWindow::new(game_state.unwrap());
}
let widgets = view_output!();
ComponentParts { model, widgets }
AsyncComponentParts { model, widgets }
}
fn update(&mut self, message: Self::Input, _sender: relm4::ComponentSender<Self>) {
fn init_loading_widgets(root: Self::Root) -> Option<LoadingWidgets> {
view! {
#[local]
root {
set_title: Some("Babylonia Terminal"),
set_default_width: 700,
set_default_height: 300,
#[name(spinner)]
gtk::Spinner {
start: (),
set_halign: gtk::Align::Center,
}
}
}
Some(LoadingWidgets::new(root, spinner))
}
async fn update(&mut self, message: Self::Input, _sender: relm4::AsyncComponentSender<Self>) {
match message {
AppMsg::Increment => {
self.counter = self.counter.wrapping_add(1);
}
AppMsg::Decrement => {
self.counter = self.counter.wrapping_sub(1);
}
MainWindowMsg::RunGame => run_game().await,
}
}
}

View File

@ -0,0 +1,33 @@
use babylonia_terminal_sdk::{
components::proton_component::ProtonComponent, game_config::GameConfig,
game_manager::GameManager,
};
use log::error;
use relm4::tokio::sync::OnceCell;
use wincompatlib::prelude::Proton;
static PROTON: OnceCell<Proton> = OnceCell::const_new();
pub async fn get_proton() -> Proton {
PROTON
.get_or_init(|| async {
let proton_component = ProtonComponent::new(GameConfig::get_config().await.config_dir);
let proton = proton_component.init_proton();
if let Err(ref e) = proton {
error!("Failed to initialize proton : {}", e);
}
proton.unwrap()
})
.await
.clone()
}
pub async fn run_game() {
let proton = get_proton().await;
let game_dir = GameConfig::get_game_dir().await;
if game_dir.is_none() {
error!("Failed to start game, the game directory was not found")
}
GameManager::start_game(&proton, game_dir.unwrap(), None, false).await;
}

View File

@ -150,6 +150,7 @@ dependencies = [
"log",
"relm4",
"relm4-components",
"wincompatlib",
]
[[package]]
@ -406,15 +407,15 @@ dependencies = [
[[package]]
name = "console"
version = "0.15.8"
version = "0.15.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb"
checksum = "ea3c6ecd8059b57859df5c69830340ed3c41d30e3da0c1cbed90a96ac853041b"
dependencies = [
"encode_unicode",
"lazy_static",
"libc",
"unicode-width 0.1.14",
"windows-sys 0.52.0",
"once_cell",
"unicode-width",
"windows-sys 0.59.0",
]
[[package]]
@ -557,9 +558,9 @@ dependencies = [
[[package]]
name = "encode_unicode"
version = "0.3.6"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
[[package]]
name = "encoding_rs"
@ -1435,7 +1436,7 @@ dependencies = [
"console",
"number_prefix",
"portable-atomic",
"unicode-width 0.2.0",
"unicode-width",
"web-time",
]
@ -2739,12 +2740,6 @@ version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "unicode-width"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]]
name = "unicode-width"
version = "0.2.0"

View File

@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"
[dependencies]
babylonia-terminal-cli = { path = "./../babylonia-terminal-cli" }
babylonia-terminal-gui = { path = "./../babylonia-terminal-gui" }
babylonia-terminal-cli = { path = "./../babylonia-terminal-cli" }
log = "0.4.22"
simple_logger = "5.0.0"
tokio = { version = "1.37.0", features = ["full"] }