use std::*;
use crate::types::*;
use super::buffer::Buff;
use super::layer::Layer;
use crate::graphics::{Graphics, Renderable};
pub struct Window<'a, Pixel> {
base: Layer<'a, Pixel>,
on_load: Option<&'a mut (OnLoad + 'a)>,
on_unload: Option<&'a mut (OnUnload + 'a)>,
on_event: Option<&'a mut (OnEvent + 'a)>
}
pub trait OnLoad {
fn on_load(&mut self);
}
pub trait OnUnload {
fn on_unload(&mut self);
}
pub trait OnEvent {
fn on_event(&mut self, e: &Event);
}
impl <'a, Pixel>Window<'a, Pixel> {
pub fn new(w: usize, h: usize, renderer: Option<&'a mut (Renderable<Pixel> + 'a)>) -> Self {
let bounds = Rect::new(0, 0, w, h);
let base = Layer::new(bounds, renderer);
return Window{base: base, on_load: None, on_event: None, on_unload: None}
}
pub fn bind_handlers(&mut self, on_load: Option<&'a mut OnLoad>, on_unload: Option<&'a mut OnUnload>, on_event: Option<&'a mut OnEvent>) {
self.on_load = on_load;
self.on_unload = on_unload;
self.on_event = on_event;
}
pub fn render(&mut self, graphics: &mut Graphics<Pixel>, buffer: &mut Buff<Pixel>) {
self.base.render(graphics, buffer);
}
}
impl <'a, Pixel>OnLoad for Window<'a, Pixel> {
fn on_load(&mut self) {
match self.on_load {
Some(ref mut h) => h.on_load(),
None => ()
}
}
}
impl <'a, Pixel>OnUnload for Window<'a, Pixel> {
fn on_unload(&mut self) {
match self.on_unload {
Some(ref mut h) => h.on_unload(),
None => ()
}
}
}
impl <'a, Pixel>OnEvent for Window<'a, Pixel> {
fn on_event(&mut self, e: &events::Event) {
match self.on_event {
Some(ref mut h) => h.on_event(e),
None => ()
}
}
}