From c04ff88876e48cdf09223fe99b77e4724f2024aa Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 27 Nov 2023 23:31:54 +0100 Subject: feat(engine): add input mode & cursor mode window settings --- engine/src/lib.rs | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 3 deletions(-) (limited to 'engine/src') 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::::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, + input_modes: BTreeMap, + cursor_mode: CursorMode, } -#[derive(Debug, Default)] -pub struct WindowSettingsBuilder {} +#[derive(Debug)] +pub struct WindowSettingsBuilder +{ + input_modes: BTreeMap, + 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, + ) -> 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>) -> 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}; -- cgit v1.2.3-18-g5258