Refactor system registration to use TypeIds
This commit is contained in:
parent
0c0d5cdb2a
commit
75fd59a504
5 changed files with 68 additions and 73 deletions
|
|
@ -1,12 +1,12 @@
|
|||
use std::any::TypeId;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use raidillon_core::scene::Scene;
|
||||
use indexmap::IndexMap;
|
||||
use glium::{Display, Frame};
|
||||
use glium::glutin::surface::WindowSurface;
|
||||
use indexmap::IndexMap;
|
||||
use raidillon_assets::ModelManagerRef;
|
||||
use raidillon_core::DebugUIBuffer;
|
||||
use crate::GliumAssetManager;
|
||||
use raidillon_core::scene::Scene;
|
||||
|
||||
pub struct RenderingContext<'a> {
|
||||
pub scene: &'a Scene,
|
||||
|
|
@ -19,16 +19,21 @@ pub struct RenderingContext<'a> {
|
|||
/// The internal "rendering system" trait of glium_platform.
|
||||
/// This is unrelated to the main System trait in core.
|
||||
pub trait RenderingSystem {
|
||||
fn handle_event(&mut self, window: &mut glium::winit::window::Window, event: winit::event::Event<()>) {}
|
||||
fn prepare_frame(&mut self, window: &mut glium::winit::window::Window) {}
|
||||
fn handle_event(
|
||||
&mut self,
|
||||
_window: &mut glium::winit::window::Window,
|
||||
_event: winit::event::Event<()>,
|
||||
) {
|
||||
}
|
||||
fn prepare_frame(&mut self, _window: &mut glium::winit::window::Window) {}
|
||||
fn render(&mut self, ctx: &mut RenderingContext);
|
||||
fn initialize(display: &Display<WindowSurface>, window: &glium::winit::window::Window) -> Self where Self: Sized;
|
||||
fn initialize(display: &Display<WindowSurface>, window: &glium::winit::window::Window) -> Self
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub type SystemID = &'static str;
|
||||
|
||||
pub struct RenderingSystemManager {
|
||||
pub systems: IndexMap<SystemID, Box<dyn RenderingSystem>>,
|
||||
pub systems: IndexMap<TypeId, Box<dyn RenderingSystem>>,
|
||||
}
|
||||
|
||||
impl RenderingSystemManager {
|
||||
|
|
@ -37,11 +42,16 @@ impl RenderingSystemManager {
|
|||
systems: IndexMap::default(),
|
||||
}
|
||||
}
|
||||
pub fn add_system(&mut self, id: SystemID, system: Box<dyn RenderingSystem>) {
|
||||
self.systems.insert(id, system);
|
||||
|
||||
pub fn add<R>(&mut self, display: &Display<WindowSurface>, window: &glium::winit::window::Window)
|
||||
where
|
||||
R: RenderingSystem + 'static,
|
||||
{
|
||||
let system = R::initialize(display, window);
|
||||
self.systems.insert(TypeId::of::<R>(), Box::new(system));
|
||||
}
|
||||
|
||||
pub fn remove_system(&mut self, id: SystemID) {
|
||||
self.systems.shift_remove(&id);
|
||||
pub fn remove<R: 'static>(&mut self) {
|
||||
self.systems.shift_remove(&TypeId::of::<R>());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue