From 94deff330a11b3b8553afcdc77e6d720fc9fd92a Mon Sep 17 00:00:00 2001 From: Speak2Erase Date: Wed, 18 Oct 2023 10:00:00 -0700 Subject: [PATCH] refactor: :recycle: don't take an &'static reference to graphics state --- crates/luminol-components/src/map_view.rs | 45 ++++++++++++--------- crates/luminol-components/src/tilepicker.rs | 14 ++++++- crates/luminol-core/src/lib.rs | 8 ++-- crates/luminol-filesystem/src/archiver.rs | 2 +- crates/luminol-graphics/src/event.rs | 9 ++++- crates/luminol-graphics/src/map.rs | 9 ++++- 6 files changed, 61 insertions(+), 26 deletions(-) diff --git a/crates/luminol-components/src/map_view.rs b/crates/luminol-components/src/map_view.rs index fdbc0184..95f2493a 100644 --- a/crates/luminol-components/src/map_view.rs +++ b/crates/luminol-components/src/map_view.rs @@ -59,36 +59,38 @@ pub enum SelectedLayer { } impl MapView { - pub fn new( - graphics_state: &luminol_graphics::GraphicsState, - filesystem: &impl luminol_filesystem::FileSystem, + pub fn new( + update_state: &mut luminol_core::UpdateState<'_, W, T>, map: &luminol_data::rpg::Map, tileset: &luminol_data::rpg::Tileset, ) -> Result { // Get tilesets. - let use_push_constants = graphics_state + let use_push_constants = update_state + .graphics .render_state .device .features() .contains(wgpu::Features::PUSH_CONSTANTS); - let atlas = graphics_state - .atlas_cache - .load_atlas(graphics_state, filesystem, tileset)?; + let atlas = update_state.graphics.atlas_cache.load_atlas( + &update_state.graphics, + update_state.filesystem, + tileset, + )?; let events = map .events .iter() .map(|(id, e)| { let sprite = luminol_graphics::Event::new( - graphics_state, - filesystem, + &update_state.graphics, + update_state.filesystem, e, &atlas, use_push_constants, ); let preview_sprite = luminol_graphics::Event::new( - graphics_state, - filesystem, + &update_state.graphics, + update_state.filesystem, e, &atlas, use_push_constants, @@ -108,8 +110,8 @@ impl MapView { .flatten_ok() .try_collect()?; let map = luminol_graphics::Map::new( - graphics_state, - filesystem, + &update_state.graphics, + update_state.filesystem, map, tileset, use_push_constants, @@ -141,10 +143,10 @@ impl MapView { }) } - pub fn ui( + pub fn ui( &mut self, ui: &mut egui::Ui, - graphics_state: &'static luminol_graphics::GraphicsState, + update_state: &mut luminol_core::UpdateState<'_, W, T>, map: &luminol_data::rpg::Map, tilepicker: &crate::Tilepicker, dragging_event: bool, @@ -257,6 +259,9 @@ impl MapView { let proj_center_y = height2 * 32. - self.pan.y / scale; let proj_width2 = canvas_rect.width() / scale / 2.; let proj_height2 = canvas_rect.height() / scale / 2.; + + let graphics_state = update_state.graphics.clone(); + self.map.set_proj( &graphics_state.render_state, glam::Mat4::orthographic_rh( @@ -269,7 +274,7 @@ impl MapView { ), ); self.map.paint( - graphics_state, + graphics_state.clone(), ui.painter(), match self.selected_layer { SelectedLayer::Events => None, @@ -366,7 +371,7 @@ impl MapView { 1., ), ); - sprite.paint(graphics_state, ui.painter(), canvas_rect); + sprite.paint(graphics_state.clone(), ui.painter(), canvas_rect); } if matches!(self.selected_layer, SelectedLayer::Events) @@ -419,7 +424,11 @@ impl MapView { ); if let Some((_, preview_sprite)) = sprites { if ui.ctx().screen_rect().contains_rect(response.rect) { - preview_sprite.paint(graphics_state, &painter, response.rect); + preview_sprite.paint( + graphics_state.clone(), + &painter, + response.rect, + ); } } match self.selected_event_id { diff --git a/crates/luminol-components/src/tilepicker.rs b/crates/luminol-components/src/tilepicker.rs index 11ee8846..f14cffe9 100644 --- a/crates/luminol-components/src/tilepicker.rs +++ b/crates/luminol-components/src/tilepicker.rs @@ -138,13 +138,15 @@ impl Tilepicker { } } - pub fn ui( + pub fn ui( &mut self, + update_state: &mut luminol_core::UpdateState<'_, W, T>, ui: &mut egui::Ui, - graphics_state: &'static luminol_graphics::GraphicsState, scroll_rect: egui::Rect, ) -> egui::Response { let time = ui.ctx().input(|i| i.time); + let graphics_state = update_state.graphics.clone(); + if let Some(ani_time) = self.ani_time { if time - ani_time >= 16. / 60. { self.ani_time = Some(time); @@ -179,6 +181,7 @@ impl Tilepicker { 1., ), ); + // FIXME: move this into graphics ui.painter().add(egui::PaintCallback { rect: scroll_rect.translate(canvas_rect.min.to_vec2()), callback: Arc::new( @@ -190,6 +193,8 @@ impl Tilepicker { let id = res_hash.insert(resources.clone()); prepare_id.set(id).expect("resources id already set?"); + paint_callback_resources.insert(graphics_state.clone()); + vec![] }) .paint(move |_info, render_pass, paint_callback_resources| { @@ -200,6 +205,11 @@ impl Tilepicker { tiles, viewport, .. } = resources.as_ref(); + let graphics_state: &Arc = + paint_callback_resources + .get() + .expect("graphics state unset"); + viewport.bind(render_pass); tiles.draw(graphics_state, viewport, &[true], None, render_pass); }), diff --git a/crates/luminol-core/src/lib.rs b/crates/luminol-core/src/lib.rs index cd67dd24..23a502dd 100644 --- a/crates/luminol-core/src/lib.rs +++ b/crates/luminol-core/src/lib.rs @@ -22,6 +22,8 @@ // terms of the Steamworks API by Valve Corporation, the licensors of this // Program grant you additional permission to convey the resulting work. +use std::sync::Arc; + mod tab; pub use tab::{Tab, Tabs}; @@ -38,7 +40,7 @@ pub use toasts::Toasts; pub struct UpdateState<'res, W, T> { pub audio: &'res mut luminol_audio::Audio, - pub graphics: &'res mut luminol_graphics::GraphicsState, // FIXME: certain functions were written with this being a static in mind + pub graphics: Arc, pub filesystem: &'res mut luminol_filesystem::project::FileSystem, // FIXME: this is probably wrong pub data: &'res mut luminol_data::data_cache::Cache, @@ -79,7 +81,7 @@ impl<'res, W, T> UpdateState<'res, W, T> { ) -> UpdateState<'this, O, T> { UpdateState { audio: &mut *self.audio, - graphics: &mut *self.graphics, + graphics: self.graphics.clone(), filesystem: &mut *self.filesystem, data: &mut *self.data, edit_tabs: &mut *self.edit_tabs, @@ -97,7 +99,7 @@ impl<'res, W, T> UpdateState<'res, W, T> { ) -> UpdateState<'this, W, O> { UpdateState { audio: &mut *self.audio, - graphics: &mut *self.graphics, + graphics: self.graphics.clone(), filesystem: &mut *self.filesystem, data: &mut *self.data, edit_tabs, diff --git a/crates/luminol-filesystem/src/archiver.rs b/crates/luminol-filesystem/src/archiver.rs index a9ff891b..ece985fd 100644 --- a/crates/luminol-filesystem/src/archiver.rs +++ b/crates/luminol-filesystem/src/archiver.rs @@ -245,7 +245,7 @@ impl crate::File for File { impl crate::FileSystem for FileSystem where - T: Read + Write + Seek + Send + Sync, + T: Read + Write + Seek + Send + Sync + 'static, { type File = File; diff --git a/crates/luminol-graphics/src/event.rs b/crates/luminol-graphics/src/event.rs index ed15064e..db8f6bf3 100644 --- a/crates/luminol-graphics/src/event.rs +++ b/crates/luminol-graphics/src/event.rs @@ -124,7 +124,7 @@ impl Event { pub fn paint( &self, - graphics_state: &'static crate::GraphicsState, + graphics_state: Arc, painter: &egui::Painter, rect: egui::Rect, ) { @@ -140,6 +140,9 @@ impl Event { .or_insert_with(Default::default); let id = res_hash.insert(resources.clone()); prepare_id.set(id).expect("resources id already set?"); + + paint_callback_resources.insert(graphics_state.clone()); + vec![] }) .paint(move |_info, render_pass, paint_callback_resources| { @@ -148,6 +151,10 @@ impl Event { let resources = &res_hash[id]; let Resources { viewport, sprite } = resources.as_ref(); + let graphics_state: &Arc = paint_callback_resources + .get() + .expect("graphics state is unset"); + viewport.bind(render_pass); sprite.draw(graphics_state, viewport, render_pass); }); diff --git a/crates/luminol-graphics/src/map.rs b/crates/luminol-graphics/src/map.rs index 87cd4bfa..85dd1ecb 100644 --- a/crates/luminol-graphics/src/map.rs +++ b/crates/luminol-graphics/src/map.rs @@ -16,6 +16,7 @@ // along with Luminol. If not, see . use crate::Plane; +use std::sync::Arc; use std::time::Duration; @@ -139,7 +140,7 @@ impl Map { pub fn paint( &mut self, - graphics_state: &'static crate::GraphicsState, + graphics_state: Arc, painter: &egui::Painter, selected_layer: Option, rect: egui::Rect, @@ -179,6 +180,8 @@ impl Map { let id = res_hash.insert(resources.clone()); prepare_id.set(id).expect("resources id already set?"); + paint_callback_resources.insert(graphics_state.clone()); // ugh + vec![] }) .paint(move |_info, render_pass, paint_callback_resources| { @@ -193,6 +196,10 @@ impl Map { .. } = resources.as_ref(); + let graphics_state: &Arc = paint_callback_resources + .get() + .expect("graphics state is unset"); + viewport.bind(render_pass); if pano_enabled {