1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::*;
use crate::core::buffer::Buff;
use crate::types::rect::Rect;
use crate::graphics::{Graphics, Renderable};
pub struct Layer<'a, Pixel> {
bounds: Rect,
visible: bool,
renderer: Option<&'a mut (Renderable<Pixel> + 'a)>,
children: vec::Vec<&'a mut Layer<'a, Pixel>>
}
impl <'a, Pixel>Layer<'a, Pixel> {
pub fn new(bounds: Rect, renderer: Option<&'a mut (Renderable<Pixel> + 'a)>) -> Layer<'a, Pixel> {
return Layer{bounds: bounds, visible: true, renderer, children: Vec::new()};
}
pub fn bounds(&self) -> Rect {
return self.bounds;
}
pub fn set_visible(&mut self, visible: bool) {
self.visible = visible;
}
}
impl <'a, Pixel>Renderable<Pixel> for Layer<'a, Pixel> {
fn render(&mut self, graphics: &mut Graphics<Pixel>, buffer: &mut Buff<Pixel>) {
if !self.visible {
return;
}
let gfx_bounds = graphics.get_bounds();
let mut new_bounds = gfx_bounds;
new_bounds.x += self.bounds.x;
new_bounds.y += self.bounds.y;
new_bounds.w = if gfx_bounds.w > self.bounds.w { self.bounds.w } else { gfx_bounds.w };
new_bounds.h = if gfx_bounds.h > self.bounds.h { self.bounds.h } else { gfx_bounds.h };
graphics.set_bounds(&new_bounds);
for child in self.children.iter_mut() {
child.render(graphics, buffer);
}
match self.renderer {
Some(ref mut r) => r.render(graphics, buffer),
None => ()
}
graphics.set_bounds(&gfx_bounds);
}
}