summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-11-27 23:31:54 +0100
committerHampusM <hampus@hampusmat.com>2023-11-27 23:31:54 +0100
commitc04ff88876e48cdf09223fe99b77e4724f2024aa (patch)
treed6f143688a903937c40cf6eb618d2f3bec27cba5
parente95f5445a18deb48b535fa9c3cb00b46f5f4014c (diff)
feat(engine): add input mode & cursor mode window settings
-rw-r--r--engine/src/lib.rs101
1 files changed, 98 insertions, 3 deletions
diff --git a/engine/src/lib.rs b/engine/src/lib.rs
index 2016ec1..f84c0ad 100644
--- a/engine/src/lib.rs
+++ b/engine/src/lib.rs
@@ -71,6 +71,15 @@ where
Renderer::<CameraT>::set_viewport(&Vec2::ZERO, &new_window_size);
});
+ Self::set_window_input_modes(&window, window_settings)?;
+
+ window
+ .set_cursor_mode(window_settings.cursor_mode)
+ .map_err(|err| Error::SetWindowCursorModeFailed {
+ source: err,
+ cursor_mode: window_settings.cursor_mode,
+ })?;
+
let renderer =
Renderer::new(&window, camera).map_err(Error::InitializeRendererFailed)?;
@@ -213,6 +222,24 @@ where
*prev_frame_start = Some(frame_start_time);
}
+
+ fn set_window_input_modes(
+ window: &Window,
+ window_settings: &WindowSettings,
+ ) -> Result<(), Error>
+ {
+ for (input_mode, enabled) in &window_settings.input_modes {
+ window
+ .set_input_mode(*input_mode, *enabled)
+ .map_err(|err| Error::SetWindowInputModeFailed {
+ source: err,
+ input_mode: *input_mode,
+ enabled: *enabled,
+ })?;
+ }
+
+ Ok(())
+ }
}
/// Engine Error
@@ -234,6 +261,23 @@ pub enum Error
#[error("Failed to get window size")]
GetWindowSizeFailed(#[source] glfw::Error),
+ #[error("Failed to set window input mode {input_mode:?} to {enabled}")]
+ SetWindowInputModeFailed
+ {
+ #[source]
+ source: glfw::Error,
+ input_mode: InputMode,
+ enabled: bool,
+ },
+
+ #[error("Failed to set window cursor mode to {cursor_mode:?}")]
+ SetWindowCursorModeFailed
+ {
+ #[source]
+ source: glfw::Error,
+ cursor_mode: CursorMode,
+ },
+
#[error("Failed to get key state")]
GetKeyStateFailed(#[source] glfw::Error),
@@ -249,10 +293,16 @@ pub struct WindowSettings
{
size: WindowSize,
title: Box<str>,
+ input_modes: BTreeMap<InputMode, bool>,
+ cursor_mode: CursorMode,
}
-#[derive(Debug, Default)]
-pub struct WindowSettingsBuilder {}
+#[derive(Debug)]
+pub struct WindowSettingsBuilder
+{
+ input_modes: BTreeMap<InputMode, bool>,
+ cursor_mode: CursorMode,
+}
impl WindowSettingsBuilder
{
@@ -262,8 +312,53 @@ impl WindowSettingsBuilder
Self::default()
}
+ #[must_use]
+ pub fn input_mode(mut self, input_mode: InputMode, enabled: bool) -> Self
+ {
+ self.input_modes.insert(input_mode, enabled);
+
+ self
+ }
+
+ #[must_use]
+ pub fn input_modes(
+ mut self,
+ input_modes: impl IntoIterator<Item = (InputMode, bool)>,
+ ) -> Self
+ {
+ self.input_modes.extend(input_modes);
+
+ self
+ }
+
+ #[must_use]
+ pub fn cursor_mode(mut self, cursor_mode: CursorMode) -> Self
+ {
+ self.cursor_mode = cursor_mode;
+
+ self
+ }
+
pub fn build(&self, size: WindowSize, title: impl Into<Box<str>>) -> WindowSettings
{
- WindowSettings { size, title: title.into() }
+ WindowSettings {
+ size,
+ title: title.into(),
+ input_modes: self.input_modes.clone(),
+ cursor_mode: self.cursor_mode,
+ }
}
}
+
+impl Default for WindowSettingsBuilder
+{
+ fn default() -> Self
+ {
+ Self {
+ input_modes: BTreeMap::new(),
+ cursor_mode: CursorMode::Normal,
+ }
+ }
+}
+
+pub use glfw::window::{CursorMode, InputMode};