micro_gui/
gui.rs

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
10/// Top level Gui object
11pub 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    /// Create a new Gui instance of the provided size with the specified buffer
18    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    /// Push a window to the top of the window stack
26    /// This will cause the window to be rendered
27    pub fn push_window(&mut self, w: &'a mut Window<'a, Pixel>) {
28        w.on_load();
29        self.windows.push(w);
30    }
31
32    /// Pop a window from the top of the window stack
33    /// This will cause the previous window to be re-rendered
34    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    /// Render will render the current window
44    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    /// Event passes a ugui event to the current window
54    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}