use std::any::TypeId;
use std::fmt::Debug;
use crate::BBox;
use crate::Cap;
use crate::Renderer;
pub trait Renderable: 'static + Debug {
fn type_id(&self) -> TypeId;
fn render(&self, cap: &dyn Cap, renderer: &dyn Renderer, bbox: BBox) -> BBox;
#[allow(unused_variables)]
fn render_done(&self, cap: &dyn Cap, renderer: &dyn Renderer, bbox: BBox) {}
}
impl dyn Renderable {
pub fn is<T>(&self) -> bool
where
T: Renderable,
{
let t = TypeId::of::<T>();
let own_t = self.type_id();
t == own_t
}
pub fn downcast_ref<T>(&self) -> Option<&T>
where
T: Renderable,
{
if self.is::<T>() {
unsafe { Some(&*(self as *const dyn Renderable as *const T)) }
} else {
None
}
}
pub fn downcast_mut<T>(&mut self) -> Option<&mut T>
where
T: Renderable,
{
if self.is::<T>() {
unsafe { Some(&mut *(self as *mut dyn Renderable as *mut T)) }
} else {
None
}
}
}