Fix the constant window updates, move the mouse centering code out of input.rs
This commit is contained in:
parent
db84f03b57
commit
5828d00b74
|
@ -31,6 +31,11 @@ impl<T> Ct<T> {
|
|||
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
|
||||
}
|
||||
}
|
13
src/input.rs
13
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::<Ct<WindowOptions>>().unwrap();
|
||||
if options.cursor_grab == CursorGrabMode::Confined && options.cursor_visible == false {
|
||||
let window = world.get_resource::<Arc<Window>>().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 { .. } => {
|
||||
|
|
|
@ -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::<Arc<Window>>(), world.get_resource_mut::<Ct<WindowOptions>>()) {
|
||||
if let (Some(window), Some(opts)) = (world.get_resource::<Arc<Window>>(), world.get_resource::<Ct<WindowOptions>>()) {
|
||||
// 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::<Ct<WindowOptions>>().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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue