Moved all panels onto their own struct.

This commit is contained in:
Filipe Rodrigues 2022-02-01 22:27:50 +00:00
parent 53b19d4f0e
commit 1324b3d7c0
7 changed files with 60 additions and 31 deletions

View File

@ -10,10 +10,9 @@ mod renderer;
// Imports
use {
self::{event_handler::EventHandler, renderer::Renderer},
crate::{paths, util, Args, Egui, ImageLoader, Panel, PanelState, PanelsProfile, PanelsRenderer, Wgpu},
crate::{paths, util, Args, Egui, ImageLoader, Panel, PanelState, Panels, PanelsProfile, PanelsRenderer, Wgpu},
anyhow::Context,
crossbeam::atomic::AtomicCell,
parking_lot::Mutex,
std::{num::NonZeroUsize, thread, time::Duration},
winit::{
dpi::{PhysicalPosition, PhysicalSize},
@ -45,9 +44,8 @@ pub fn run(args: &Args) -> Result<(), anyhow::Error> {
let panels = args
.panel_geometries
.iter()
.map(|&geometry| Panel::new(geometry, PanelState::Empty, args.image_duration, args.fade_point))
.collect::<Vec<_>>();
let panels = Mutex::new(panels);
.map(|&geometry| Panel::new(geometry, PanelState::Empty, args.image_duration, args.fade_point));
let panels = Panels::new(panels);
// Create the panels renderer
let panels_renderer = PanelsRenderer::new(wgpu.device(), wgpu.surface_texture_format())

View File

@ -6,10 +6,9 @@ mod settings_window;
// Imports
use {
self::settings_window::SettingsWindow,
crate::{paths, Egui, ImageLoader, Panel, PanelsRenderer, Wgpu},
crate::{paths, Egui, ImageLoader, Panels, PanelsRenderer, Wgpu},
anyhow::Context,
crossbeam::atomic::AtomicCell,
parking_lot::Mutex,
std::{thread, time::Duration},
winit::{dpi::PhysicalPosition, window::Window},
};
@ -32,7 +31,7 @@ pub struct Renderer<'a> {
panels_renderer: &'a PanelsRenderer,
/// Panels
panels: &'a Mutex<Vec<Panel>>,
panels: &'a Panels,
/// Egui
egui: &'a Egui,
@ -49,7 +48,7 @@ impl<'a> Renderer<'a> {
paths_distributer: &'a paths::Distributer,
image_loader: &'a ImageLoader,
panels_renderer: &'a PanelsRenderer,
panels: &'a Mutex<Vec<Panel>>,
panels: &'a Panels,
egui: &'a Egui,
queued_settings_window_open_click: &'a AtomicCell<Option<PhysicalPosition<f64>>>,
) -> Self {
@ -97,14 +96,13 @@ impl<'a> Renderer<'a> {
/// Updates all panels
fn update(&mut self) -> Result<(), anyhow::Error> {
let mut panels = self.panels.lock();
for panel in &mut *panels {
self.panels.for_each_mut(|panel| {
if let Err(err) = panel.update(self.wgpu, self.panels_renderer, self.image_loader) {
log::warn!("Unable to update panel: {err:?}");
}
}
Ok(())
Ok(())
})
}
/// Renders
@ -130,9 +128,8 @@ impl<'a> Renderer<'a> {
self.wgpu.render(|encoder, surface_view, surface_size| {
// Render the panels
let mut panels = self.panels.lock();
self.panels_renderer
.render(&mut *panels, self.wgpu.queue(), encoder, surface_view, surface_size)
.render(self.panels, self.wgpu.queue(), encoder, surface_view, surface_size)
.context("Unable to render panels")?;
// Render egui

View File

@ -5,12 +5,11 @@
// Imports
use {
crate::{paths, Panel, PanelState, Rect},
crate::{paths, Panel, PanelState, Panels, Rect},
cgmath::{Point2, Vector2},
crossbeam::atomic::AtomicCell,
egui::Widget,
parking_lot::Mutex,
std::{mem, time::Duration},
std::time::Duration,
winit::{
dpi::{PhysicalPosition, PhysicalSize},
window::Window,
@ -49,7 +48,7 @@ impl<'a> SettingsWindow<'a> {
_frame: &epi::Frame,
surface_size: PhysicalSize<u32>,
window: &Window,
panels: &Mutex<Vec<Panel>>,
panels: &Panels,
paths_distributer: &paths::Distributer,
) -> Result<(), anyhow::Error> {
// Create the base settings window
@ -68,12 +67,14 @@ impl<'a> SettingsWindow<'a> {
// Then render it
settings_window.open(&mut self.open).show(ctx, |ui| {
let mut panels = panels.lock();
for (idx, panel) in panels.iter_mut().enumerate() {
ui.collapsing(format!("Panel {idx}"), |ui| {
let mut panel_idx = 0;
panels.for_each_mut::<_, ()>(|panel| {
ui.collapsing(format!("Panel {panel_idx}"), |ui| {
ui.add(PanelWidget::new(panel, surface_size));
});
}
panel_idx += 1;
});
ui.collapsing("Add panel", |ui| {
ui.horizontal(|ui| {
ui.label("Geometry");
@ -91,7 +92,7 @@ impl<'a> SettingsWindow<'a> {
});
if ui.button("Add").clicked() {
panels.push(Panel::new(
panels.add_panel(Panel::new(
self.new_panel_state.geometry,
PanelState::Empty,
Duration::from_secs_f32(self.new_panel_state.duration_secs),
@ -99,7 +100,6 @@ impl<'a> SettingsWindow<'a> {
));
}
});
mem::drop(panels);
ui.horizontal(|ui| {
let cur_root_path = paths_distributer.root_path();

View File

@ -10,7 +10,8 @@
try_trait_v2,
backtrace,
thread_id_value,
unwrap_infallible
unwrap_infallible,
explicit_generic_args_with_impl_trait
)]
// Lints
#![warn(
@ -74,7 +75,7 @@ pub use self::{
args::Args,
egui::Egui,
img::ImageLoader,
panel::{Panel, PanelState, PanelsProfile, PanelsRenderer},
panel::{Panel, PanelState, Panels, PanelsProfile, PanelsRenderer},
rect::Rect,
wgpu::Wgpu,
};

View File

@ -4,12 +4,14 @@
// Modules
mod image;
mod panels;
mod profile;
mod renderer;
// Exports
pub use self::{
image::PanelImage,
panels::Panels,
profile::PanelsProfile,
renderer::{PanelImageId, PanelUniforms, PanelVertex, PanelsRenderer},
};

31
src/panel/panels.rs Normal file
View File

@ -0,0 +1,31 @@
//! Panels
// Imports
use {crate::Panel, parking_lot::Mutex};
/// All panels
#[derive(Debug)]
pub struct Panels {
/// All of the panels
panels: Mutex<Vec<Panel>>,
}
impl Panels {
/// Creates the panel
pub fn new(panels: impl IntoIterator<Item = Panel>) -> Self {
Self {
panels: Mutex::new(panels.into_iter().collect()),
}
}
/// Adds a new panel
pub fn add_panel(&self, panel: Panel) {
self.panels.lock().push(panel);
}
/// Iterates over all panels mutably
pub fn for_each_mut<T, C: FromIterator<T>>(&self, f: impl FnMut(&mut Panel) -> T) -> C {
let mut panels = self.panels.lock();
panels.iter_mut().map(f).collect()
}
}

View File

@ -10,7 +10,7 @@ pub use self::{uniform::PanelUniforms, vertex::PanelVertex};
// Imports
use {
super::PanelImage,
crate::{img::Image, Panel, Wgpu},
crate::{img::Image, Panels, Wgpu},
parking_lot::Mutex,
wgpu::util::DeviceExt,
winit::dpi::PhysicalSize,
@ -117,7 +117,7 @@ impl PanelsRenderer {
/// Renders panels
pub fn render(
&self,
panels: &mut [Panel],
panels: &Panels,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
surface_view: &wgpu::TextureView,
@ -156,7 +156,7 @@ impl PanelsRenderer {
render_pass.set_vertex_buffer(0, self.vertices.slice(..));
// And draw each panel
for panel in panels {
panels.for_each_mut::<_, ()>(|panel| {
// Calculate the matrix for the panel
let matrix = panel.matrix(surface_size);
@ -192,7 +192,7 @@ impl PanelsRenderer {
render_pass.set_bind_group(1, image.image_bind_group(), &[]);
render_pass.draw_indexed(0..6, 0, 0..1);
}
}
});
Ok(())
}