From d9e32ad4a7371aa1660d8dc221fbebe448f0de7f Mon Sep 17 00:00:00 2001 From: Umut Date: Tue, 9 Jul 2024 22:15:18 +0300 Subject: [PATCH] feat: bump bevy to v0.14 --- Cargo.toml | 8 +-- README.md | 2 +- examples/controlling.rs | 2 +- examples/setup.rs | 2 +- examples/spawning.rs | 2 +- src/components.rs | 8 --- src/plugins.rs | 39 ++-------- src/prelude.rs | 19 +---- src/systems.rs | 2 +- src/utils.rs | 154 +++------------------------------------- 10 files changed, 28 insertions(+), 210 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2ecb1aa..c66e3e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,14 +12,14 @@ authors = [ ] [dependencies] -bevy = { version = "0.13.1", features = ["serialize"] } -bevy-persistent = { version = "0.5" } +bevy = { version = "0.14", features = ["serialize"] } +bevy-persistent = { version = "0.6" } serde = { version = "1.0", features = ["derive"] } -winit = { version = "0.29", default-features = false } +winit = { version = "0.30", default-features = false } [dev-dependencies] dirs = { version = "5.0" } -bevy-persistent = { version = "0.5", features = ["toml"] } +bevy-persistent = { version = "0.6", features = ["toml"] } [features] default = [] diff --git a/README.md b/README.md index 8bf6f11..d3d581f 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ let state_directory = dirs::data_dir() Time to create the primary window: ```rust -app.world.spawn(( +app.world_mut().spawn(( PrimaryWindow, PersistentWindowBundle { window: Window { title: "I am the primary window!".to_owned(), ..Default::default() }, diff --git a/examples/controlling.rs b/examples/controlling.rs index ad7e8bc..aac2cff 100644 --- a/examples/controlling.rs +++ b/examples/controlling.rs @@ -20,7 +20,7 @@ fn main() { .unwrap_or(Path::new("session").join("data").join("state")) .join("primary"); - app.world.spawn(( + app.world_mut().spawn(( PrimaryWindow, PersistentWindowBundle { window: Window { diff --git a/examples/setup.rs b/examples/setup.rs index fb70e63..478f45d 100644 --- a/examples/setup.rs +++ b/examples/setup.rs @@ -17,7 +17,7 @@ fn main() { .unwrap_or(Path::new("session").join("data").join("state")) .join("setup"); - app.world.spawn(( + app.world_mut().spawn(( PrimaryWindow, PersistentWindowBundle { window: Window { title: "I persist!".to_owned(), ..Default::default() }, diff --git a/examples/spawning.rs b/examples/spawning.rs index e9e5c72..61bad19 100644 --- a/examples/spawning.rs +++ b/examples/spawning.rs @@ -25,7 +25,7 @@ fn main() { .unwrap_or(Path::new("session").join("data").join("state")) .join("primary"); - app.world.spawn(( + app.world_mut().spawn(( PrimaryWindow, PersistentWindowBundle { window: Window { title: "I am primary...".to_owned(), ..Default::default() }, diff --git a/src/components.rs b/src/components.rs index 377f921..2899ccc 100644 --- a/src/components.rs +++ b/src/components.rs @@ -9,10 +9,6 @@ pub struct WindowState { /// Mode of the window. pub mode: WindowMode, - /// Name of the monitor that the window is in. - /// (`None` means pick the best monitor) - pub monitor: Option, - /// Resolution of the window. /// (`None` means pick the best resolution) pub resolution: Option<(u32, u32)>, @@ -34,7 +30,6 @@ impl WindowState { pub fn borderless_fullscreen() -> WindowState { WindowState { mode: WindowMode::BorderlessFullscreen, - monitor: None, resolution: None, scale: None, position: None, @@ -46,7 +41,6 @@ impl WindowState { pub fn fullscreen() -> WindowState { WindowState { mode: WindowMode::Fullscreen, - monitor: None, resolution: None, scale: None, position: None, @@ -57,7 +51,6 @@ impl WindowState { pub fn sized_fullscreen() -> WindowState { WindowState { mode: WindowMode::SizedFullscreen, - monitor: None, resolution: None, scale: None, position: None, @@ -69,7 +62,6 @@ impl WindowState { pub fn windowed(width: u32, height: u32) -> WindowState { WindowState { mode: WindowMode::Windowed, - monitor: None, resolution: Some((width, height)), scale: None, position: None, diff --git a/src/plugins.rs b/src/plugins.rs index 7045fbc..bb271ef 100644 --- a/src/plugins.rs +++ b/src/plugins.rs @@ -10,46 +10,19 @@ use crate::prelude::*; /// # Panics /// /// - Panics if it's added to the [App] -/// before [WinitPlugin](bevy::winit::WinitPlugin), -/// which is in the [DefaultPlugins]. +/// before [WinitPlugin](bevy::winit::WinitPlugin), +/// which is in the [DefaultPlugins]. pub struct PersistentWindowsPlugin; impl Plugin for PersistentWindowsPlugin { fn build(&self, app: &mut App) { - let event_loop = app - .world - .get_non_send_resource::>() - .expect("persistent windows plugin is added before winit plugin"); + let mut persistent_windows = + app.world_mut().query::<(&mut Window, &Persistent)>(); - match utils::available_monitors(event_loop) { - Some(available_monitors) => { - let best_monitor = utils::best_monitor(&available_monitors); - - let mut persistent_windows = - app.world.query::<(&mut Window, &mut Persistent)>(); - - for (mut window, mut state) in persistent_windows.iter_mut(&mut app.world) { - utils::adjust_to_monitor( - &available_monitors, - best_monitor, - &mut window, - &mut state, - ); - } - }, - None => { - log::error!("unable to persist windows as no monitors are available"); - }, + for (mut window, state) in persistent_windows.iter_mut(app.world_mut()) { + utils::apply_state_to_window(&mut window, state); } - let event_loop = app.world.remove_non_send_resource::>().unwrap(); - - let mut create_window = SystemState::::from_world(&mut app.world); - create_windows(event_loop.deref(), create_window.get_mut(&mut app.world)); - create_window.apply(&mut app.world); - - app.insert_non_send_resource(event_loop); - app.add_systems( PreUpdate, ( diff --git a/src/prelude.rs b/src/prelude.rs index 8ed9884..b37014b 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -5,36 +5,21 @@ pub(crate) use crate::{ utils, }; pub(crate) use bevy::{ - ecs::system::SystemState, - log, prelude::*, - utils::HashMap, window::{ - RequestRedraw, WindowMode, WindowResized, WindowResolution, WindowScaleFactorChanged, }, - winit::{ - create_windows, - CreateWindowParams, - WinitWindows, - }, + winit::WinitWindows, }; pub(crate) use bevy_persistent::prelude::*; pub(crate) use serde::{ Deserialize, Serialize, }; -pub(crate) use std::ops::{ - Deref, - DerefMut, -}; -pub(crate) use winit::{ - event_loop::EventLoop, - monitor::MonitorHandle, -}; +pub(crate) use std::ops::DerefMut; pub use crate::{ PersistentWindowBundle, diff --git a/src/systems.rs b/src/systems.rs index 3b7ad4f..e29862b 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -82,6 +82,6 @@ pub fn on_persistent_window_state_changed( } state.persist().ok(); - utils::apply_state_to_window(window.deref_mut(), state.deref_mut(), None); + utils::apply_state_to_window(window.deref_mut(), state.deref_mut()); } } diff --git a/src/utils.rs b/src/utils.rs index 15a409b..a64be07 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,152 +2,21 @@ use crate::prelude::*; -/// Gets available monitors from the event loop. -/// -/// Result is a mapping from monitor name to monitor index and monitor handle. -/// -/// Returns [None] if no monitors are available. -pub fn available_monitors( - event_loop: &EventLoop, -) -> Option> { - let available_monitors = event_loop - .available_monitors() - .enumerate() - .filter_map(|(index, monitor)| monitor.name().map(|name| (name, (index, monitor)))) - .collect::>(); - - if !available_monitors.is_empty() { Some(available_monitors) } else { None } -} - -/// Gets the best monitor from available monitors. -/// -/// Best monitor is selected according to: -/// - Width `(Bigger is Better)` -/// - Height `(Bigger is Better)` -/// - Refresh rate `(Bigger is Better)` -/// - Monitor index `(Smaller is Better)` -/// in that order. -/// -/// If refresh rate information is not available, monitor is not preferred. -/// -/// Result is a tuple of `monitor name` and a tuple of `monitor index` and `monitor handle`. -/// -/// # Panics -/// -/// - Panics if `available_monitors` is empty. -pub fn best_monitor( - available_monitors: &HashMap, -) -> (&String, &(usize, MonitorHandle)) { - let mut available_monitors = available_monitors.iter(); - - let mut selected_monitor = available_monitors.next().unwrap(); - for current_monitor in available_monitors { - let selected_monitor_handle = &selected_monitor.1.1; - let current_monitor_handle = ¤t_monitor.1.1; - - let selected_monitor_size = selected_monitor_handle.size(); - let current_monitor_size = current_monitor_handle.size(); - - if current_monitor_size.width < selected_monitor_size.width { - continue; - } - if current_monitor_size.height < selected_monitor_size.height { - continue; - } - - let selected_monitor_refresh_rate = selected_monitor_handle.refresh_rate_millihertz(); - let current_monitor_refresh_rate = current_monitor_handle.refresh_rate_millihertz(); - - match (current_monitor_refresh_rate, selected_monitor_refresh_rate) { - (Some(_), None) => {}, - (None, Some(_)) => continue, - - (Some(current_monitor_refresh_rate), Some(selected_monitor_refresh_rate)) - if current_monitor_refresh_rate != selected_monitor_refresh_rate => - { - if current_monitor_refresh_rate < selected_monitor_refresh_rate { - continue; - } - }, - - _ => { - let selected_monitor_index = &selected_monitor.1.0; - let current_monitor_index = ¤t_monitor.1.0; +/// Propagates the changes made to the state to the window. +pub fn apply_state_to_window(window: &mut Window, state: &Persistent) { + window.mode = state.mode; - if current_monitor_index > selected_monitor_index { - continue; - } - }, + if let Some((width, height)) = state.resolution { + window.resolution = WindowResolution::default(); + if let Some(scale) = state.scale { + window.resolution.set_scale_factor_override(Some(scale as f32)); } - - selected_monitor = current_monitor; + window.resolution.set_physical_resolution(width, height); } - selected_monitor -} -/// Adjusts a persistent window for a monitor. -/// -/// If state already has an existing monitor, it's used. -/// Otherwise, the best monitor is used. -/// -/// Changes made by this function persist immediately. -pub fn adjust_to_monitor( - available_monitors: &HashMap, - best_monitor: (&String, &(usize, MonitorHandle)), - window: &mut Window, - state: &mut Persistent, -) { - let (name, (index, handle)) = state - .monitor - .as_ref() - .and_then(|name| available_monitors.get(name).map(|monitor| (name, monitor))) - .unwrap_or(best_monitor); - - let new_state = WindowState { - monitor: Some(name.to_string()), - mode: state.mode, - resolution: Some(state.resolution.unwrap_or_else(|| { - let best_video_mode = bevy::winit::get_best_videomode(handle); - let best_resolution = best_video_mode.size(); - (best_resolution.width, best_resolution.height) - })), - scale: Some(state.scale.unwrap_or_else(|| handle.scale_factor())), - position: state.position, - sync: state.sync, - }; - - if &new_state != state.get() { - state.set(new_state).ok(); - } - - apply_state_to_window(window, state, Some(*index)) -} - -/// Propagates the changes made to the state to the window. -pub fn apply_state_to_window( - window: &mut Window, - state: &Persistent, - monitor_index: Option, -) { - let mode = state.mode; - - let mut resolution = WindowResolution::default(); - if let Some(scale) = state.scale { - resolution.set_scale_factor_override(Some(scale as f32)); + if let Some(position) = state.position { + window.position = WindowPosition::new(position.into()); } - resolution.set_physical_resolution(state.resolution.unwrap().0, state.resolution.unwrap().1); - - let position = if let Some(position) = state.position { - WindowPosition::new(position.into()) - } else { - monitor_index - .map(|monitor_index| WindowPosition::Centered(MonitorSelection::Index(monitor_index))) - .unwrap_or(WindowPosition::Automatic) - }; - - window.mode = mode; - window.resolution = resolution; - window.position = position; } /// Propagates the changes made to the window to the state. @@ -156,7 +25,6 @@ pub fn apply_window_to_state( state: &mut Persistent, winit_window: &winit::window::Window, ) { - let monitor = winit_window.current_monitor().and_then(|monitor| monitor.name()); let mode = window.mode; let resolution = Some((winit_window.inner_size().width, winit_window.inner_size().height)); let scale = Some(window.scale_factor() as f64); @@ -170,7 +38,7 @@ pub fn apply_window_to_state( } }); - let new_state = WindowState { monitor, mode, resolution, scale, position, sync: state.sync }; + let new_state = WindowState { mode, resolution, scale, position, sync: state.sync }; if new_state != *state.get() { state.set(new_state).ok(); state.sync = false;