diff options
Diffstat (limited to 'engine/src/window.rs')
-rw-r--r-- | engine/src/window.rs | 86 |
1 files changed, 82 insertions, 4 deletions
diff --git a/engine/src/window.rs b/engine/src/window.rs index ad239a1..00c360e 100644 --- a/engine/src/window.rs +++ b/engine/src/window.rs @@ -4,16 +4,22 @@ use std::ffi::{CStr, CString}; use bitflags::bitflags; use ecs::actions::Actions; use ecs::extension::Collector as ExtensionCollector; +use ecs::phase::{Phase, PRESENT as PRESENT_PHASE, START as START_PHASE}; +use ecs::relationship::{ChildOf, Relationship}; use ecs::sole::Single; -use ecs::Sole; +use ecs::{static_entity, Sole}; use glfw::window::{Hint as WindowCreationHint, HintValue as WindowCreationHintValue}; use glfw::WindowSize; use util_macros::VariantArr; use crate::data_types::dimens::Dimens; -use crate::event::{Conclude as ConcludeEvent, Start as StartEvent}; use crate::vector::Vec2; +static_entity!( + pub UPDATE_PHASE, + (Phase, <Relationship<ChildOf, Phase>>::new(*PRESENT_PHASE)) +); + #[derive(Debug, Sole)] /// Has to be dropped last since it holds the OpenGL context. #[sole(drop_last)] @@ -169,6 +175,24 @@ impl Window .set_cursor_pos_callback(move |pos| callback(Vec2 { x: pos.x, y: pos.y })); } + /// Sets the window's mouse button callback. The given function is called when a mouse + /// button enters a new state. + pub fn set_mouse_button_callback( + &self, + callback: impl Fn(MouseButton, MouseButtonState, KeyModifiers) + 'static, + ) + { + self.inner.set_mouse_button_callback( + move |mouse_button, mouse_button_state, key_modifiers| { + callback( + MouseButton::from_glfw_mouse_button(mouse_button), + MouseButtonState::from_glfw_mouse_button_state(mouse_button_state), + KeyModifiers::from_bits_truncate(key_modifiers.bits()), + ) + }, + ); + } + /// Sets the window's close callback. pub fn set_close_callback(&self, callback: impl Fn() + 'static) { @@ -514,6 +538,60 @@ impl KeyState } } +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum MouseButton +{ + One, + Two, + Three, + Four, + Five, + Six, + Seven, + Eight, +} + +impl MouseButton +{ + pub const LEFT: Self = Self::One; + pub const MIDDLE: Self = Self::Three; + pub const RIGHT: Self = Self::Two; + + fn from_glfw_mouse_button(mouse_button: glfw::window::MouseButton) -> Self + { + match mouse_button { + glfw::window::MouseButton::One => Self::One, + glfw::window::MouseButton::Two => Self::Two, + glfw::window::MouseButton::Three => Self::Three, + glfw::window::MouseButton::Four => Self::Four, + glfw::window::MouseButton::Five => Self::Five, + glfw::window::MouseButton::Six => Self::Six, + glfw::window::MouseButton::Seven => Self::Seven, + glfw::window::MouseButton::Eight => Self::Eight, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum MouseButtonState +{ + Pressed, + Released, +} + +impl MouseButtonState +{ + fn from_glfw_mouse_button_state( + mouse_button_state: glfw::window::MouseButtonState, + ) -> Self + { + match mouse_button_state { + glfw::window::MouseButtonState::Pressed => Self::Pressed, + glfw::window::MouseButtonState::Released => Self::Released, + } + } +} + bitflags! { #[derive(Debug, Clone, Copy)] pub struct KeyModifiers: i32 { @@ -613,8 +691,8 @@ impl ecs::extension::Extension for Extension { fn collect(self, mut collector: ExtensionCollector<'_>) { - collector.add_system(StartEvent, initialize); - collector.add_system(ConcludeEvent, update); + collector.add_system(*START_PHASE, initialize); + collector.add_system(*UPDATE_PHASE, update); let window = self .window_builder |