From 5828d00b742a81692835d102f3cd67df7053ace9 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Tue, 19 Sep 2023 23:05:51 -0400 Subject: [PATCH] Fix the constant window updates, move the mouse centering code out of input.rs --- src/change_tracker.rs | 7 +++++++ src/input.rs | 13 ------------- src/render/window.rs | 43 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/change_tracker.rs b/src/change_tracker.rs index 16ecd9d..ac8215c 100644 --- a/src/change_tracker.rs +++ b/src/change_tracker.rs @@ -31,6 +31,11 @@ impl Ct { before } + /// Returns true if there was a change, will NOT reset back to false. + pub fn peek_changed(&self) -> bool { + self.changed + } + /// Resets the changed tracker to be false pub fn reset(&mut self) { self.changed = false; @@ -78,6 +83,7 @@ mod tests { let mut c = Ct::new_true(100); assert!(c.changed()); + assert!(!c.changed()); // it should've reset itself } #[test] @@ -108,5 +114,6 @@ mod tests { let mut c = Ct::new(100); c.trigger(); assert!(c.changed()); // should changed because of trigger + assert!(!c.changed()); // should've reset itself } } \ No newline at end of file diff --git a/src/input.rs b/src/input.rs index a031c7f..c4dfe21 100755 --- a/src/input.rs +++ b/src/input.rs @@ -255,19 +255,6 @@ impl InputSystem { pos: Vec2::new(position.x as f32, position.y as f32) }; - // if the window is set to confine the cursor, and the cursor is invisible, - // set the cursor position to the center of the screen. - let options = world.get_resource::>().unwrap(); - if options.cursor_grab == CursorGrabMode::Confined && options.cursor_visible == false { - let window = world.get_resource::>().unwrap(); - let size = window.inner_size(); - let middle = PhysicalPosition { - x: size.width / 2, - y: size.height / 2, - }; - window.set_cursor_position(middle).unwrap(); - } - event_queue.trigger_event(exact); }, InputEvent::CursorEntered { .. } => { diff --git a/src/render/window.rs b/src/render/window.rs index 39f8f72..0268206 100644 --- a/src/render/window.rs +++ b/src/render/window.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use glam::{Vec2, IVec2}; use tracing::{warn, error, debug}; -use winit::{window::{Window, Fullscreen}, dpi::{LogicalPosition, LogicalSize}, error::ExternalError}; +use winit::{window::{Window, Fullscreen}, dpi::{LogicalPosition, LogicalSize, PhysicalPosition}, error::ExternalError}; pub use winit::window::{CursorGrabMode, CursorIcon, Icon, Theme, WindowButtons, WindowLevel}; @@ -253,10 +253,28 @@ fn set_cursor_grab(window: &Window, grab: &mut CursorGrabMode) -> anyhow::Result Ok(()) } +/// if the window is set to confine the cursor, and the cursor is invisible, +/// set the cursor position to the center of the screen. +fn center_mouse(window: &Window, options: &WindowOptions) { + if options.cursor_grab == CursorGrabMode::Confined && options.cursor_visible == false { + let size = window.inner_size(); + let middle = PhysicalPosition { + x: size.width / 2, + y: size.height / 2, + }; + window.set_cursor_position(middle).unwrap(); + } +} + fn window_updater_system(world: &mut edict::World) -> anyhow::Result<()> { - if let (Some(window), Some(mut opts)) = (world.get_resource::>(), world.get_resource_mut::>()) { + if let (Some(window), Some(opts)) = (world.get_resource::>(), world.get_resource::>()) { // if the options changed, update the window - if opts.changed() { + if opts.peek_changed() { + drop(opts); // avoid attempting to get a RefMut when we already have a Ref out. + + // now we can get it mutable, this will trigger the ChangeTracker, so it will be reset at the end of this scope. + let mut opts = world.get_resource_mut::>().unwrap(); + window.set_content_protected(opts.content_protected); set_cursor_grab(&window, &mut opts.cursor_grab)?; match window.set_cursor_hittest(opts.cursor_hittest) { @@ -266,10 +284,10 @@ fn window_updater_system(world: &mut edict::World) -> anyhow::Result<()> { error!("OS error when setting cursor hittest: {:?}", e); } } - window.set_cursor_icon(opts.cursor_icon); - window.set_cursor_visible(opts.cursor_visible); - window.set_decorations(opts.decorations); - window.set_enabled_buttons(opts.enabled_buttons); + window.set_cursor_icon(opts.cursor_icon); // TODO: Handle unsupported platforms + window.set_cursor_visible(opts.cursor_visible); // TODO: Handle unsupported platforms + window.set_decorations(opts.decorations); // TODO: Handle unsupported platforms + window.set_enabled_buttons(opts.enabled_buttons); // TODO: Handle unsupported platforms // Update the window mode. can only be done if the monitor is found if let Some(monitor) = window.current_monitor() @@ -293,14 +311,21 @@ fn window_updater_system(world: &mut edict::World) -> anyhow::Result<()> { if opts.min_inner_size.is_some() { window.set_min_inner_size(ivec2_to_logical_size_op(opts.min_inner_size)); } - //window.set_maximized(opts.maximized); // TODO - //window.set_minimized(opts.minimized); // TODO + window.set_maximized(opts.maximized); + window.set_minimized(opts.minimized); window.set_resizable(opts.resizeable); window.set_resize_increments(vec2_to_logical_size_op(opts.resize_increments)); window.set_theme(opts.theme); window.set_title(&opts.title); window.set_window_icon(opts.icon.clone()); window.set_window_level(opts.level); + + // reset the tracker after we mutably used it + opts.reset(); + + center_mouse(&window, &opts); + } else { + center_mouse(&window, &opts); } }