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
mod button;
mod text;

pub use self::button::{Button, ButtonState, Image, Flat};
pub use self::text::Label;

use std::any::Any;

use super::resources::*;

/// Enum used to check what events should happen on certain controls.
pub enum ControlType {
    Button,
    Label
}

/// Data that needs to be supplied to the `update` function.
pub struct ControlState {
    /// The position of the mouse cursor. Is not required to be inside the bounds of the screen.
    pub mouse_pos: (i32, i32),
    /// If the left mouse button is pressed or not.
    pub mouse_down: bool
}

impl ControlState {
    /// Determines if the mouse is inside a rectangle. Mostly used internally.
    pub fn mouse_collision(&self, pos: (i32, i32), size: (i32, i32)) -> bool {
        self.mouse_pos.0 >= pos.0 && self.mouse_pos.1 >= pos.1
            && self.mouse_pos.0 < pos.0 + size.0
            && self.mouse_pos.1 < pos.1 + size.1
    }
}

impl Default for ControlState {
    fn default() -> Self {
        ControlState {
            mouse_pos: (0, 0),
            mouse_down: false
        }
    }
}

pub trait Control {
    /// Update the control.
    fn update(&mut self, args: &ControlState, res: &Resources);

    /// Draw the control on the output buffer.
    fn draw(&self, buffer: &mut Vec<u32>, buffer_width: usize, res: &Resources);

    /// Retrieve what type of control this is.
    fn control_type(&self) -> ControlType;

    /// For downcasting.
    fn as_any(&self) -> &Any;

    /// For downcasting.
    fn as_any_mut(&mut self) -> &mut Any;
}