From 317ddd62e01501fa302db1a846a9bc939b538ece Mon Sep 17 00:00:00 2001 From: iWas-Coder Date: Fri, 17 May 2024 01:31:23 +0200 Subject: [PATCH] Modularized `sk_launcher` in multiple files --- src/sk_launcher.rs | 103 ++++---------------------------- src/sk_launcher_base.rs | 54 +++++++++++++++++ src/sk_launcher_centralpanel.rs | 38 ++++++++++++ src/sk_launcher_clientwindow.rs | 65 ++++++++++++++++++++ src/sk_launcher_logtab.rs | 36 +++++++++++ src/sk_launcher_modulestab.rs | 39 ++++++++++++ src/sk_launcher_serverwindow.rs | 48 +++++++++++++++ src/sk_launcher_topbar.rs | 47 +++++++++++++++ 8 files changed, 337 insertions(+), 93 deletions(-) create mode 100644 src/sk_launcher_base.rs create mode 100644 src/sk_launcher_centralpanel.rs create mode 100644 src/sk_launcher_clientwindow.rs create mode 100644 src/sk_launcher_logtab.rs create mode 100644 src/sk_launcher_modulestab.rs create mode 100644 src/sk_launcher_serverwindow.rs create mode 100644 src/sk_launcher_topbar.rs diff --git a/src/sk_launcher.rs b/src/sk_launcher.rs index 2d9d828..51fb192 100644 --- a/src/sk_launcher.rs +++ b/src/sk_launcher.rs @@ -21,105 +21,22 @@ extern crate libc; extern crate eframe; -extern crate image; -use std::{env, thread}; -use std::sync::Arc; -use std::sync::atomic::{AtomicBool, Ordering}; -use libc::c_char; -use eframe::egui; - -struct Launcher { - curr_tab: String, - online_mode: bool, - ip: String, - is_client_running: Arc, - is_server_running: Arc -} +mod sk_launcher_base; +mod sk_launcher_topbar; +mod sk_launcher_centralpanel; +mod sk_launcher_modulestab; +mod sk_launcher_logtab; +mod sk_launcher_clientwindow; +mod sk_launcher_serverwindow; -extern "C" { - fn sk_client_run(ip: *const c_char) -> u8; - fn sk_server_run() -> u8; -} +use std::env; +use eframe::egui; static SK_LAUNCHER_NAME: &str = "sparky::launcher"; static SK_LAUNCHER_TAB_MODULES: &str = "Modules"; static SK_LAUNCHER_TAB_LOG: &str = "Console Log"; -impl Default for Launcher { - fn default() -> Self { - Self { - curr_tab: SK_LAUNCHER_TAB_MODULES.to_owned(), - online_mode: false, - ip: "".to_owned(), - is_client_running: Arc::new(AtomicBool::new(false)), - is_server_running: Arc::new(AtomicBool::new(false)) - } - } -} - -impl eframe::App for Launcher { - fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { - egui::TopBottomPanel::top(SK_LAUNCHER_NAME).show(ctx, |ui| { - ui.horizontal(|ui| { - egui::widgets::global_dark_light_mode_switch(ui); - ui.separator(); - if ui.selectable_label(self.curr_tab == SK_LAUNCHER_TAB_MODULES, SK_LAUNCHER_TAB_MODULES).clicked() { - self.curr_tab = SK_LAUNCHER_TAB_MODULES.to_owned(); - } - if ui.selectable_label(self.curr_tab == SK_LAUNCHER_TAB_LOG, SK_LAUNCHER_TAB_LOG).clicked() { - self.curr_tab = SK_LAUNCHER_TAB_LOG.to_owned(); - } - }); - }); - egui::CentralPanel::default().show(ctx, |_| { - if self.curr_tab == SK_LAUNCHER_TAB_MODULES { - egui::Window::new("Client").show(ctx, |ui| { - if self.is_client_running.load(Ordering::SeqCst) { ui.set_enabled(false); } - ui.checkbox(&mut self.online_mode, "Online mode"); - if self.online_mode { - ui.horizontal(|ui| { - let ip_label = ui.label("IP: "); - ui.text_edit_singleline(&mut self.ip).labelled_by(ip_label.id); - }); - } - if ui.button("Play").clicked() { - self.is_client_running.store(true, Ordering::SeqCst); - let is_client_running = Arc::clone(&self.is_client_running); - if self.ip.is_empty() { - thread::spawn(move || { - unsafe { sk_client_run(std::ptr::null()); } - is_client_running.store(false, Ordering::SeqCst); - }); - } - else { - let ip_addr = std::ffi::CString::new(self.ip.clone()).unwrap(); - thread::spawn(move || { - unsafe { sk_client_run(ip_addr.as_ptr()); } - is_client_running.store(false, Ordering::SeqCst); - }); - } - } - }); - egui::Window::new("Server").show(ctx, |ui| { - if self.is_server_running.load(Ordering::SeqCst) { ui.set_enabled(false); } - if ui.button("Start").clicked() { - self.is_server_running.store(true, Ordering::SeqCst); - let is_server_running = Arc::clone(&self.is_server_running); - thread::spawn(move || { - unsafe { sk_server_run(); } - is_server_running.store(false, Ordering::SeqCst); - }); - } - }); - } - else if self.curr_tab == SK_LAUNCHER_TAB_LOG { - // TODO: Add a section where all stdout/stderr output is shown - } - }); - } -} - #[no_mangle] pub extern "C" fn sk_launcher_run() -> u8 { println!("INFO: Initializing {}", SK_LAUNCHER_NAME); @@ -141,7 +58,7 @@ pub extern "C" fn sk_launcher_run() -> u8 { }), ..Default::default() }, - Box::new(|_| Box::new(Launcher::default()) as Box) + Box::new(|_| Box::new(sk_launcher_base::Launcher::default()) as Box) ).expect("Unexpected error. Shutting down..."); println!("INFO: {} closed successfully", SK_LAUNCHER_NAME); return 0; diff --git a/src/sk_launcher_base.rs b/src/sk_launcher_base.rs new file mode 100644 index 0000000..41e96a8 --- /dev/null +++ b/src/sk_launcher_base.rs @@ -0,0 +1,54 @@ +/* + * GNU Sparky --- A 5v5 character-based libre tactical shooter + * Copyright (C) 2024 Wasym A. Alonso + * + * This file is part of Sparky. + * + * Sparky is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Sparky is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Sparky. If not, see . + */ + + +use egui; +use std::sync::Arc; +use sk_launcher_topbar; +use SK_LAUNCHER_TAB_MODULES; +use sk_launcher_centralpanel; +use std::sync::atomic::AtomicBool; + +pub struct Launcher { + pub curr_tab: String, + pub online_mode: bool, + pub ip: String, + pub is_client_running: Arc, + pub is_server_running: Arc +} + +impl Default for Launcher { + fn default() -> Self { + Self { + curr_tab: SK_LAUNCHER_TAB_MODULES.to_owned(), + online_mode: false, + ip: "".to_owned(), + is_client_running: Arc::new(AtomicBool::new(false)), + is_server_running: Arc::new(AtomicBool::new(false)) + } + } +} + +impl eframe::App for Launcher { + fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + ::update(self, ctx); + ::update(self, ctx); + } +} diff --git a/src/sk_launcher_centralpanel.rs b/src/sk_launcher_centralpanel.rs new file mode 100644 index 0000000..d2c7a78 --- /dev/null +++ b/src/sk_launcher_centralpanel.rs @@ -0,0 +1,38 @@ +/* + * GNU Sparky --- A 5v5 character-based libre tactical shooter + * Copyright (C) 2024 Wasym A. Alonso + * + * This file is part of Sparky. + * + * Sparky is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Sparky is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Sparky. If not, see . + */ + + +use egui; +use sk_launcher_base; +use sk_launcher_logtab; +use sk_launcher_modulestab; + +pub trait CentralPanel { + fn update(&mut self, ctx: &egui::Context); +} + +impl CentralPanel for sk_launcher_base::Launcher { + fn update(&mut self, ctx: &egui::Context) { + egui::CentralPanel::default().show(ctx, |_| { + ::update(self, ctx); + ::update(self, ctx); + }); + } +} diff --git a/src/sk_launcher_clientwindow.rs b/src/sk_launcher_clientwindow.rs new file mode 100644 index 0000000..7aa79a8 --- /dev/null +++ b/src/sk_launcher_clientwindow.rs @@ -0,0 +1,65 @@ +/* + * GNU Sparky --- A 5v5 character-based libre tactical shooter + * Copyright (C) 2024 Wasym A. Alonso + * + * This file is part of Sparky. + * + * Sparky is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Sparky is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Sparky. If not, see . + */ + + +use egui; +use std::thread; +use libc::c_char; +use std::sync::Arc; +use sk_launcher_base; +use std::sync::atomic::Ordering; + +extern "C" { fn sk_client_run(ip: *const c_char) -> u8; } + +pub trait ClientWindow { + fn update(&mut self, ctx: &egui::Context); +} + +impl ClientWindow for sk_launcher_base::Launcher { + fn update(&mut self, ctx: &egui::Context) { + egui::Window::new("Client").show(ctx, |ui| { + if self.is_client_running.load(Ordering::SeqCst) { ui.set_enabled(false); } + ui.checkbox(&mut self.online_mode, "Online mode"); + if self.online_mode { + ui.horizontal(|ui| { + let ip_label = ui.label("IP: "); + ui.text_edit_singleline(&mut self.ip).labelled_by(ip_label.id); + }); + } + if ui.button("Play").clicked() { + self.is_client_running.store(true, Ordering::SeqCst); + let is_client_running = Arc::clone(&self.is_client_running); + if self.ip.is_empty() { + thread::spawn(move || { + unsafe { sk_client_run(std::ptr::null()); } + is_client_running.store(false, Ordering::SeqCst); + }); + } + else { + let ip_addr = std::ffi::CString::new(self.ip.clone()).unwrap(); + thread::spawn(move || { + unsafe { sk_client_run(ip_addr.as_ptr()); } + is_client_running.store(false, Ordering::SeqCst); + }); + } + } + }); + } +} diff --git a/src/sk_launcher_logtab.rs b/src/sk_launcher_logtab.rs new file mode 100644 index 0000000..bee4aea --- /dev/null +++ b/src/sk_launcher_logtab.rs @@ -0,0 +1,36 @@ +/* + * GNU Sparky --- A 5v5 character-based libre tactical shooter + * Copyright (C) 2024 Wasym A. Alonso + * + * This file is part of Sparky. + * + * Sparky is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Sparky is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Sparky. If not, see . + */ + + +use egui; +use sk_launcher_base; +use SK_LAUNCHER_TAB_LOG; + +pub trait LogTab { + fn update(&mut self, _ctx: &egui::Context); +} + +impl LogTab for sk_launcher_base::Launcher { + fn update(&mut self, _ctx: &egui::Context) { + if self.curr_tab == SK_LAUNCHER_TAB_LOG { + // TODO: Add a section where all stdout/stderr output is shown + } + } +} diff --git a/src/sk_launcher_modulestab.rs b/src/sk_launcher_modulestab.rs new file mode 100644 index 0000000..35fdc66 --- /dev/null +++ b/src/sk_launcher_modulestab.rs @@ -0,0 +1,39 @@ +/* + * GNU Sparky --- A 5v5 character-based libre tactical shooter + * Copyright (C) 2024 Wasym A. Alonso + * + * This file is part of Sparky. + * + * Sparky is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Sparky is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Sparky. If not, see . + */ + + +use egui; +use sk_launcher_base; +use SK_LAUNCHER_TAB_MODULES; +use sk_launcher_clientwindow; +use sk_launcher_serverwindow; + +pub trait ModulesTab { + fn update(&mut self, ctx: &egui::Context); +} + +impl ModulesTab for sk_launcher_base::Launcher { + fn update(&mut self, ctx: &egui::Context) { + if self.curr_tab == SK_LAUNCHER_TAB_MODULES { + ::update(self, ctx); + ::update(self, ctx); + } + } +} diff --git a/src/sk_launcher_serverwindow.rs b/src/sk_launcher_serverwindow.rs new file mode 100644 index 0000000..03cd80e --- /dev/null +++ b/src/sk_launcher_serverwindow.rs @@ -0,0 +1,48 @@ +/* + * GNU Sparky --- A 5v5 character-based libre tactical shooter + * Copyright (C) 2024 Wasym A. Alonso + * + * This file is part of Sparky. + * + * Sparky is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Sparky is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Sparky. If not, see . + */ + + +use egui; +use std::thread; +use std::sync::Arc; +use sk_launcher_base; +use std::sync::atomic::Ordering; + +extern "C" { fn sk_server_run() -> u8; } + +pub trait ServerWindow { + fn update(&mut self, ctx: &egui::Context); +} + +impl ServerWindow for sk_launcher_base::Launcher { + fn update(&mut self, ctx: &egui::Context) { + egui::Window::new("Server").show(ctx, |ui| { + if self.is_server_running.load(Ordering::SeqCst) { ui.set_enabled(false); } + if ui.button("Start").clicked() { + self.is_server_running.store(true, Ordering::SeqCst); + let is_server_running = Arc::clone(&self.is_server_running); + thread::spawn(move || { + unsafe { sk_server_run(); } + is_server_running.store(false, Ordering::SeqCst); + }); + } + }); + } +} diff --git a/src/sk_launcher_topbar.rs b/src/sk_launcher_topbar.rs new file mode 100644 index 0000000..2d49cf7 --- /dev/null +++ b/src/sk_launcher_topbar.rs @@ -0,0 +1,47 @@ +/* + * GNU Sparky --- A 5v5 character-based libre tactical shooter + * Copyright (C) 2024 Wasym A. Alonso + * + * This file is part of Sparky. + * + * Sparky is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Sparky is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Sparky. If not, see . + */ + + +use egui; +use sk_launcher_base; +use SK_LAUNCHER_NAME; +use SK_LAUNCHER_TAB_LOG; +use SK_LAUNCHER_TAB_MODULES; + +pub trait TopBar { + fn update(&mut self, ctx: &egui::Context); +} + +impl TopBar for sk_launcher_base::Launcher { + fn update(&mut self, ctx: &egui::Context) { + egui::TopBottomPanel::top(SK_LAUNCHER_NAME).show(ctx, |ui| { + ui.horizontal(|ui| { + egui::widgets::global_dark_light_mode_switch(ui); + ui.separator(); + if ui.selectable_label(self.curr_tab == SK_LAUNCHER_TAB_MODULES, SK_LAUNCHER_TAB_MODULES).clicked() { + self.curr_tab = SK_LAUNCHER_TAB_MODULES.to_owned(); + } + if ui.selectable_label(self.curr_tab == SK_LAUNCHER_TAB_LOG, SK_LAUNCHER_TAB_LOG).clicked() { + self.curr_tab = SK_LAUNCHER_TAB_LOG.to_owned(); + } + }); + }); + } +}