1
2use std::vec::Vec;
3
4use crate::types::*;
5use crate::core::buffer::{Buff};
6use crate::core::window::{Window, OnEvent, OnLoad, OnUnload};
7
8use crate::graphics::Graphics;
9
10pub struct Gui<'a, Pixel> {
12 graphics: Graphics<Pixel>,
13 windows: Vec<&'a mut Window<'a, Pixel>>
14}
15
16impl <'a, Pixel> Gui <'a, Pixel> {
17 pub fn new(w: usize, h: usize) -> Self {
19 let graphics = Graphics::new(0, 0, w, h);
20 let windows = Vec::new();
21
22 return Self{graphics, windows};
23 }
24
25 pub fn push_window(&mut self, w: &'a mut Window<'a, Pixel>) {
28 w.on_load();
29 self.windows.push(w);
30 }
31
32 pub fn pop_window(&mut self) -> Option<&mut Window<'a, Pixel>> {
35 let mut window = self.windows.pop();
36 match window {
37 Some(ref mut w) => w.on_unload(),
38 None => ()
39 }
40 return window;
41 }
42
43 pub fn render(&mut self, buff: &mut Buff<Pixel>) {
45 let windows = self.windows.as_mut_slice();
46 let len = windows.len();
47 if len <= 0 { return; }
48
49 let active = &mut windows[len-1];
50 active.render(&mut self.graphics, buff);
51 }
52
53 pub fn event(&mut self, e: &events::Event) {
55 let windows = self.windows.as_mut_slice();
56 let len = windows.len();
57 if len <= 0 { return; }
58
59 let active = &mut windows[len-1];
60 active.on_event(e);
61 }
62}