Move egui layer to a separate DebugGuiStack structure

This commit is contained in:
Tony Klink 2024-03-31 14:09:38 -06:00
parent 3f367e19ac
commit 92c0278ef0
Signed by: klink
GPG key ID: 85175567C4D19231
5 changed files with 104 additions and 9 deletions

50
src/core/debug_gui.rs Normal file
View file

@ -0,0 +1,50 @@
use std::collections::HashMap;
use vulkano_util::renderer::VulkanoWindowRenderer;
use winit::{event_loop::EventLoopWindowTarget, window::WindowId};
use crate::modules::graphics::egui::{Gui, GuiConfig};
#[derive(Default)]
pub struct DebugGuiStack {
guis: HashMap<WindowId, Gui>,
}
impl DebugGuiStack {
pub fn add_gui<T>(
&mut self,
window_id: WindowId,
event_loop: &EventLoopWindowTarget<T>,
renderer: &VulkanoWindowRenderer,
is_overlay: bool,
allow_srgb_render_target: bool,
) where
T: Clone + Send + Sync,
{
let gui = Gui::new(
event_loop,
renderer.surface().clone(),
renderer.graphics_queue().clone(),
renderer.swapchain_format(),
GuiConfig {
is_overlay,
allow_srgb_render_target,
..Default::default()
},
);
self.guis.insert(window_id, gui);
}
pub fn remove_gui(&mut self, window_id: WindowId) {
self.guis.remove(&window_id).unwrap();
}
pub fn get(&mut self, window_id: WindowId) -> Option<&Gui> {
self.guis.get(&window_id)
}
pub fn get_mut(&mut self, window_id: WindowId) -> Option<&mut Gui> {
self.guis.get_mut(&window_id)
}
}

View file

@ -1,2 +1,4 @@
pub mod events;
pub mod module;
pub mod time;
pub mod debug_gui;

View file

@ -1,10 +1,11 @@
use std::{collections::HashMap, time::Duration};
use std::time::Duration;
use anyhow::Result;
use flax::World;
use winit::window::WindowId;
use crate::{core::events::Events, modules::graphics::egui::Gui};
use crate::core::events::Events;
use super::debug_gui::DebugGuiStack;
pub trait Module {
fn on_update(&mut self, world: &mut World, events: &mut Events, frame_time: Duration) -> Result<()>;
@ -67,7 +68,7 @@ impl<'a> IntoIterator for &'a mut ModulesStack {
// THREAD LOCAL STUFF
pub trait RenderModule {
fn on_update(&mut self, gui: &mut HashMap<WindowId, Gui>, vk_context: &mut vulkano_util::context::VulkanoContext, vk_windows: &mut vulkano_util::window::VulkanoWindows, world: &mut World, events: &mut Events, frame_time: Duration) -> Result<()>;
fn on_update(&mut self, gui_stack: &mut DebugGuiStack, vk_context: &mut vulkano_util::context::VulkanoContext, vk_windows: &mut vulkano_util::window::VulkanoWindows, world: &mut World, events: &mut Events, frame_time: Duration) -> Result<()>;
}
pub struct RenderModulesStack {

43
src/core/time.rs Normal file
View file

@ -0,0 +1,43 @@
//! Provides time related functionality for Clocks.
use std::time::{Duration, Instant};
use flax::component;
component! {
pub clock: Clock,
}
/// Measures high precision time
#[allow(dead_code)]
pub struct Clock {
start: Instant,
}
#[allow(dead_code)]
impl Clock {
// Creates and starts a new clock
pub fn new() -> Self {
Clock {
start: Instant::now(),
}
}
// Returns the elapsed time
pub fn elapsed(&self) -> Duration {
Instant::now() - self.start
}
// Resets the clock and returns the elapsed time
pub fn reset(&mut self) -> Duration {
let elapsed = self.elapsed();
self.start = Instant::now();
elapsed
}
}
impl Default for Clock {
fn default() -> Self {
Self::new()
}
}

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, sync::Arc};
use std::sync::Arc;
use flax::{entity_ids, BoxedSystem, Query, QueryBorrow, Schedule, System, World};
use vulkano::{
@ -15,9 +15,8 @@ use vulkano::{
use vulkano_util::{
context::VulkanoContext, renderer::VulkanoWindowRenderer, window::VulkanoWindows,
};
use winit::window::WindowId;
use crate::core::module::RenderModule as ThreadLocalModule;
use crate::core::{debug_gui::DebugGuiStack, module::RenderModule as ThreadLocalModule};
use self::{egui::Gui, test_pipeline::test_pipeline};
@ -65,7 +64,7 @@ impl RenderModule {
impl ThreadLocalModule for RenderModule {
fn on_update(
&mut self,
guis: &mut HashMap<WindowId, Gui>,
gui_stack: &mut DebugGuiStack,
vk_context: &mut VulkanoContext,
vk_windows: &mut vulkano_util::window::VulkanoWindows,
world: &mut World,
@ -77,7 +76,7 @@ impl ThreadLocalModule for RenderModule {
let viewport = &mut self.viewport;
for (window_id, renderer) in vk_windows.iter_mut() {
let gui = guis.get_mut(window_id).unwrap();
let gui = gui_stack.get_mut(*window_id).unwrap();
draw(
self.command_buffer_allocator.clone(),
viewport,