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::*;
pub enum ControlType {
Button,
Label
}
pub struct ControlState {
pub mouse_pos: (i32, i32),
pub mouse_down: bool
}
impl ControlState {
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 {
fn update(&mut self, args: &ControlState, res: &Resources);
fn draw(&self, buffer: &mut Vec<u32>, buffer_width: usize, res: &Resources);
fn control_type(&self) -> ControlType;
fn as_any(&self) -> &Any;
fn as_any_mut(&mut self) -> &mut Any;
}