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};

/// Layers combine graphics functions to provide reusable blocks for rendering
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> {
    /// Create a new layer with the provided bounds
    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()};
    }

    /// Fetch the bounds of a given layer
    pub fn bounds(&self) -> Rect {
        return self.bounds;
    }

    /// Set layer visible state
    pub fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }
}

impl <'a, Pixel>Renderable<Pixel> for Layer<'a, Pixel> {
    /// Render a layer using the provided graphics context and buffer
    fn render(&mut self, graphics: &mut Graphics<Pixel>, buffer: &mut Buff<Pixel>) {

        if !self.visible {
            return;
        }

        // Update graphics context with new layer bounds
        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);

        // Render children
        for child in self.children.iter_mut() {
            child.render(graphics, buffer);
        }

        // Render parent
        match self.renderer {
            Some(ref mut r) => r.render(graphics, buffer),
            None => ()
        }

        // Revert graphics context
        graphics.set_bounds(&gfx_bounds);

    }
}