draw/event cleanup and gitignore
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,6 @@
|
||||
/target
|
||||
target
|
||||
|
||||
Cargo.lock
|
||||
|
||||
imgui.ini
|
||||
|
||||
71
src/draw.rs
71
src/draw.rs
@@ -1,71 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::entity::EntityHandle;
|
||||
use crate::loaders::mesh::Mesh;
|
||||
use crate::render::{DrawCall, Pipeline};
|
||||
|
||||
pub type DrawHandle = usize;
|
||||
|
||||
struct DrawEntry
|
||||
{
|
||||
mesh: Rc<Mesh>,
|
||||
entity: EntityHandle,
|
||||
pipeline: Pipeline,
|
||||
}
|
||||
|
||||
pub struct DrawManager
|
||||
{
|
||||
entries: HashMap<DrawHandle, DrawEntry>,
|
||||
next_handle: DrawHandle,
|
||||
}
|
||||
|
||||
impl DrawManager
|
||||
{
|
||||
pub fn new() -> Self
|
||||
{
|
||||
Self {
|
||||
entries: HashMap::new(),
|
||||
next_handle: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_mesh_internal(
|
||||
&mut self,
|
||||
mesh: Rc<Mesh>,
|
||||
entity: EntityHandle,
|
||||
pipeline: Pipeline,
|
||||
) -> DrawHandle
|
||||
{
|
||||
let handle = self.next_handle;
|
||||
self.next_handle += 1;
|
||||
|
||||
self.entries.insert(
|
||||
handle,
|
||||
DrawEntry {
|
||||
mesh,
|
||||
entity,
|
||||
pipeline,
|
||||
},
|
||||
);
|
||||
|
||||
handle
|
||||
}
|
||||
|
||||
pub fn clear_mesh_internal(&mut self, handle: DrawHandle)
|
||||
{
|
||||
self.entries.remove(&handle);
|
||||
}
|
||||
|
||||
pub fn collect_draw_calls(&self) -> Vec<DrawCall>
|
||||
{
|
||||
vec![]
|
||||
}
|
||||
|
||||
pub fn draw_mesh(_mesh: Rc<Mesh>, _entity: EntityHandle, _pipeline: Pipeline) -> DrawHandle
|
||||
{
|
||||
0
|
||||
}
|
||||
|
||||
pub fn clear_mesh(_handle: DrawHandle) {}
|
||||
}
|
||||
70
src/event.rs
70
src/event.rs
@@ -1,70 +0,0 @@
|
||||
use std::any::{Any, TypeId};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub trait Event: std::fmt::Debug {}
|
||||
|
||||
type EventHandler<T> = Box<dyn FnMut(&T)>;
|
||||
|
||||
pub struct EventBus
|
||||
{
|
||||
handlers: HashMap<TypeId, Box<dyn Any>>,
|
||||
}
|
||||
|
||||
impl EventBus
|
||||
{
|
||||
fn new() -> Self
|
||||
{
|
||||
Self {
|
||||
handlers: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn subscribe_internal<T: Event + 'static, F: FnMut(&T) + 'static>(&mut self, handler: F)
|
||||
{
|
||||
let type_id = TypeId::of::<T>();
|
||||
let handlers: &mut Vec<EventHandler<T>> = self
|
||||
.handlers
|
||||
.entry(type_id)
|
||||
.or_insert_with(|| Box::new(Vec::<EventHandler<T>>::new()))
|
||||
.downcast_mut()
|
||||
.unwrap();
|
||||
|
||||
handlers.push(Box::new(handler));
|
||||
}
|
||||
|
||||
fn publish_internal<T: Event + 'static>(&mut self, event: &T)
|
||||
{
|
||||
let type_id = TypeId::of::<T>();
|
||||
|
||||
if let Some(handlers) = self.handlers.get_mut(&type_id)
|
||||
{
|
||||
let typed_handlers = handlers.downcast_mut::<Vec<EventHandler<T>>>().unwrap();
|
||||
for handler in typed_handlers
|
||||
{
|
||||
handler(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subscribe<T: Event + 'static, F: FnMut(&T) + 'static>(handler: F)
|
||||
{
|
||||
EVENT_BUS.with(|bus| bus.borrow_mut().subscribe_internal(handler));
|
||||
}
|
||||
|
||||
pub fn publish<T: Event + 'static>(event: &T)
|
||||
{
|
||||
EVENT_BUS.with(|bus| bus.borrow_mut().publish_internal(event));
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static EVENT_BUS: RefCell<EventBus> = RefCell::new(EventBus::new());
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UpdateEvent
|
||||
{
|
||||
pub delta: f32,
|
||||
}
|
||||
impl Event for UpdateEvent {}
|
||||
@@ -3,7 +3,6 @@ mod components;
|
||||
mod debug;
|
||||
mod editor;
|
||||
mod entity;
|
||||
mod event;
|
||||
mod loaders;
|
||||
mod physics;
|
||||
mod postprocess;
|
||||
|
||||
Reference in New Issue
Block a user