diff --git a/Cargo.lock b/Cargo.lock index a0af6df..9f592a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -503,6 +503,7 @@ name = "moonhare_window" version = "0.1.0" dependencies = [ "gtk4", + "moonhare_event", "moonhare_log", ] diff --git a/crates/moonhare_event/Cargo.toml b/crates/moonhare_event/Cargo.toml index 3fff50a..5e64993 100644 --- a/crates/moonhare_event/Cargo.toml +++ b/crates/moonhare_event/Cargo.toml @@ -4,4 +4,4 @@ edition = "2024" version.workspace = true authors.workspace = true -[dependencies] +[dependencies] \ No newline at end of file diff --git a/crates/moonhare_event/src/event.rs b/crates/moonhare_event/src/event.rs index acaba4b..ddd4bae 100644 --- a/crates/moonhare_event/src/event.rs +++ b/crates/moonhare_event/src/event.rs @@ -9,4 +9,8 @@ pub enum EventType { pub trait Event { fn get_event_name() -> &'static str; fn get_event_type() -> EventType; -} \ No newline at end of file + + /// Emits the Event + fn emit(); +} + diff --git a/crates/moonhare_event/src/events/key_events/key_pressed_event.rs b/crates/moonhare_event/src/events/key_events/key_pressed_event.rs index 75bd104..019615e 100644 --- a/crates/moonhare_event/src/events/key_events/key_pressed_event.rs +++ b/crates/moonhare_event/src/events/key_events/key_pressed_event.rs @@ -1,5 +1,6 @@ -use crate::{event::Event,}; +use crate::{event::{Event, EventType}, events::key_events::KeyEvent}; +#[derive(Debug)] struct KeyPressedEvent{} const KEY_PRESSED_EVENT_NAME: &str = "KeyPressedEvent"; @@ -9,8 +10,12 @@ impl Event for KeyPressedEvent { KEY_PRESSED_EVENT_NAME } - fn get_event_type() -> crate::event::EventType { - crate::event::EventType::KeyPressed + fn get_event_type() -> EventType { + EventType::KeyPressed + } + + fn emit(&self) -> &impl Event{ + self } } diff --git a/crates/moonhare_event/src/events/window_events/mod.rs b/crates/moonhare_event/src/events/window_events/mod.rs index c52e95c..edd30b6 100644 --- a/crates/moonhare_event/src/events/window_events/mod.rs +++ b/crates/moonhare_event/src/events/window_events/mod.rs @@ -1,3 +1,5 @@ +pub mod window_close_event; + use crate::event::Event; pub trait WindowEvent: Event { diff --git a/crates/moonhare_event/src/events/window_events/window_close_event.rs b/crates/moonhare_event/src/events/window_events/window_close_event.rs new file mode 100644 index 0000000..3ae2ab3 --- /dev/null +++ b/crates/moonhare_event/src/events/window_events/window_close_event.rs @@ -0,0 +1,23 @@ +use crate::{event::Event, events::window_events::WindowEvent}; + +#[derive(Debug)] +pub struct WindowCloseEvent { + +} + +impl Event for WindowCloseEvent { + fn get_event_name() -> &'static str { + todo!() + } + + fn get_event_type() -> crate::event::EventType { + todo!() + } + + fn emit(){ + } +} + +impl WindowEvent for WindowCloseEvent { + +} \ No newline at end of file diff --git a/crates/moonhare_game/src/lib.rs b/crates/moonhare_game/src/lib.rs index d8f295c..4d52811 100644 --- a/crates/moonhare_game/src/lib.rs +++ b/crates/moonhare_game/src/lib.rs @@ -8,6 +8,7 @@ pub mod basic; /// Only one Game may exist per project #[derive(Debug)] pub struct Game { + pub is_running: bool, pub name: String, pub context: WindowRenderContext, } @@ -15,6 +16,7 @@ pub struct Game { impl Default for Game { fn default() -> Self { Self { + is_running: true, name: default_game_name(), context: WindowRenderContext::OPENGLGTK } @@ -28,7 +30,7 @@ impl Game { pub fn run(&self) { info("Running Game..."); - loop { + while self.is_running { } } diff --git a/crates/moonhare_window/Cargo.toml b/crates/moonhare_window/Cargo.toml index 048c16c..8d840b5 100644 --- a/crates/moonhare_window/Cargo.toml +++ b/crates/moonhare_window/Cargo.toml @@ -6,4 +6,5 @@ authors.workspace = true [dependencies] moonhare_log = { path = "../moonhare_log" } +moonhare_event = { path = "../moonhare_event" } gtk4 = "*" \ No newline at end of file diff --git a/crates/moonhare_window/src/lib.rs b/crates/moonhare_window/src/lib.rs index 86c805f..9ffa372 100644 --- a/crates/moonhare_window/src/lib.rs +++ b/crates/moonhare_window/src/lib.rs @@ -1,12 +1,5 @@ //! Provides functionality to create either a vulkan or opengl window - -use std::marker; - -#[cfg(target_os = "linux")] -use crate::platforms::gtk_window::GTKWindow; - pub mod window_config; - pub mod platforms; #[derive(Debug, Clone, Copy)] @@ -30,7 +23,8 @@ pub struct Window { impl Window { - /// creates a gtk4 window + /// creates a gtk4 window while spaning a new thread that the window runs on. + /// here: gtk sends engine events when _things happen_ with the window that other engine parts can interact with #[cfg(target_os = "linux")] pub fn create(context: WindowRenderContext) { match context { @@ -51,7 +45,6 @@ impl Window { }); }, } - } #[cfg(not(target_os = "linux"))] diff --git a/crates/moonhare_window/src/platforms/gtk_window.rs b/crates/moonhare_window/src/platforms/gtk_window.rs index efd84fb..af21ae3 100644 --- a/crates/moonhare_window/src/platforms/gtk_window.rs +++ b/crates/moonhare_window/src/platforms/gtk_window.rs @@ -1,4 +1,5 @@ -use gtk4::{gio::prelude::ApplicationExt, glib, prelude::{GtkWindowExt, WidgetExt}, Application, ApplicationWindow}; +use gtk4::{gio::{prelude::{ActionMapExtManual, ApplicationExt}, ActionEntry}, glib, prelude::{GtkWindowExt, WidgetExt}, Application, ApplicationWindow}; +use moonhare_event::{event::Event, events::window_events::window_close_event::WindowCloseEvent}; use crate::{window_config, MoonhareWindow}; @@ -21,8 +22,20 @@ impl GTKWindow { window.set_default_size(window_config.width as i32, window_config.height as i32); window.set_visible(window_config.visble); + // Add action "close" to `window` taking no parameter + let action_close = ActionEntry::builder("close") + .activate(|window: &ApplicationWindow, _, _| { + GTKWindow::shutdown(); + window.close(); + }) + .build(); + + window.add_action_entries([action_close]); + window.show(); } + + } impl MoonhareWindow for GTKWindow { @@ -43,6 +56,8 @@ impl MoonhareWindow for GTKWindow { } fn shutdown() { + // todo: emit WindowCloseEvent + } }