summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engine/src/window.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/engine/src/window.rs b/engine/src/window.rs
index ad239a1..008c09b 100644
--- a/engine/src/window.rs
+++ b/engine/src/window.rs
@@ -169,6 +169,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 +532,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 {