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
//! A solid square of color

use sdl2::pixels::Color;
use sdl2::render::Canvas;
use sdl2::video::Window;

use drawable::{DrawSettings, Drawable, Position, State};

/// A solid, fillid with a color
pub struct Solid {
    /// The color
    pub color: Color,
    shown: bool,
}

impl Solid {
    /// Create a Solid from an sdl2 color
    pub fn new_sdl2(color: Color) -> Solid {
        Solid { color, shown: true }
    }

    /// Create a Solid from a rgba values
    pub fn new_rgba(r: u8, g: u8, b: u8, a: u8) -> Solid {
        Solid {
            color: Color::RGBA(r, g, b, a),
            shown: true,
        }
    }
}

impl Drawable for Solid {
    fn content(&self) -> Vec<&dyn Drawable> {
        vec![]
    }
    fn content_mut(&mut self) -> Vec<&mut dyn Drawable> {
        vec![]
    }

    fn step(&mut self) {
        self.shown = false;
    }

    fn state(&self) -> State {
        if self.shown {
            State::Final
        } else {
            State::Hidden
        }
    }

    fn draw(&self, canvas: &mut Canvas<Window>, position: &Position, _settings: DrawSettings) {
        match position {
            Position::Rect(r) => {
                canvas.set_draw_color(self.color);
                canvas.fill_rect(*r).expect("can't draw");
            }
            _ => {}
        }
    }
}