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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
extern crate sdl2;
use sdl2::event::Event;
use sdl2::rect::{Point, Rect};
use sdl2::render::Canvas;
use sdl2::video::Window;
#[allow(unused)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Position {
TopLeftCorner(Point),
Center(Point),
Rect(Rect),
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct DrawSettings {
pub notes_view: bool,
pub background_color: (u8, u8, u8),
}
pub const DSETTINGS_MAIN: DrawSettings = DrawSettings {
notes_view: false,
background_color: (255, 248, 234),
};
pub const DSETTINGS_NOTES: DrawSettings = DrawSettings {
notes_view: true,
..DSETTINGS_MAIN
};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum State {
Working,
Final,
Hidden,
}
impl Position {
pub fn into_rect_with_size(self, width: u32, height: u32) -> Rect {
match self {
Position::TopLeftCorner(point) => Rect::new(point.x, point.y, width, height),
Position::Center(point) => Rect::new(
point.x - width as i32 / 2,
point.y - height as i32 / 2,
width,
height,
),
Position::Rect(rect) => {
let center_x = rect.x() + rect.width() as i32 / 2;
let center_y = rect.y() + rect.height() as i32 / 2;
let x = (center_x - width as i32 / 2).max(rect.x);
let y = (center_y - height as i32 / 2).max(rect.y);
let width = if x + width as i32 > rect.x + rect.width() as i32 {
rect.width()
} else {
width
};
let height = if y + height as i32 > rect.y + rect.height() as i32 {
rect.height()
} else {
height
};
Rect::new(x, y, width, height)
}
}
}
pub fn into_rect_with_size_unbounded(self, width: u32, height: u32) -> Rect {
match self {
Position::TopLeftCorner(point) => Rect::new(point.x, point.y, width, height),
Position::Center(point) => Rect::new(
point.x - width as i32 / 2,
point.y - height as i32 / 2,
width,
height,
),
Position::Rect(rect) => {
let center_x = rect.x() + rect.width() as i32 / 2;
let center_y = rect.y() + rect.height() as i32 / 2;
let x = (center_x - width as i32 / 2).max(rect.x);
let y = (center_y - height as i32 / 2).max(rect.y);
Rect::new(x, y, width, height)
}
}
}
}
pub trait Drawable: Send {
fn content(&self) -> Vec<&dyn Drawable>;
fn content_mut(&mut self) -> Vec<&mut dyn Drawable>;
fn register(&mut self) {
for content in &mut self.content_mut() {
content.register();
}
}
fn load(&mut self) {
for content in &mut self.content_mut() {
content.load();
}
}
fn step(&mut self);
fn event(&mut self, e: Event) {
for x in self.content_mut() {
x.event(e.clone());
}
}
fn state(&self) -> State;
fn update(&mut self, dt: f64) {
for content in &mut self.content_mut() {
content.update(dt);
}
}
fn draw(&self, _canvas: &mut Canvas<Window>, _position: &Position, _settings: DrawSettings);
}
pub trait KnownSize: Drawable {
fn width(&self) -> usize;
fn height(&self) -> usize;
}