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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use crate::core::point::*;
use crate::widget::widget::*;
use piston_window::*;
pub struct WidgetContainer {
pub widget: Box<dyn Widget>,
widget_id: i32,
parent_id: i32,
}
pub struct WidgetStore {
pub widgets: Vec<WidgetContainer>,
}
impl WidgetStore {
pub fn new() -> Self {
let mut widgets_list: Vec<WidgetContainer> = Vec::new();
let mut base_widget = CanvasWidget::new();
base_widget.set_size(800, 600);
widgets_list.push(WidgetContainer {
widget: Box::new(base_widget),
widget_id: 0,
parent_id: 0,
});
Self {
widgets: widgets_list,
}
}
pub fn handle_resize(&mut self, width: u32, height: u32) {
self.widgets[0].widget.set_size(width as i32, height as i32);
for pos in 0..self.widgets.len() {
self.window_resized_for_id(
pos as i32,
crate::core::point::Size {
w: width as i32,
h: height as i32,
},
);
}
}
pub fn invalidate_all_widgets(&mut self) {
self.widgets.iter_mut().for_each(|x| x.widget.invalidate());
}
pub fn needs_repaint(&mut self) -> bool {
self.widgets
.iter_mut()
.map(|x| x.widget.is_invalidated())
.find(|x| x == &true)
.unwrap_or(false)
}
pub fn add_widget(&mut self, widget: Box<dyn Widget>) -> i32 {
let widget_size = self.widgets.len() as i32;
self.widgets.push(WidgetContainer {
widget,
widget_id: widget_size,
parent_id: 0,
});
widget_size
}
pub fn add_widget_to_parent(&mut self, widget: Box<dyn Widget>, parent_id: i32) -> i32 {
let widget_size = self.widgets.len() as i32;
self.widgets.push(WidgetContainer {
widget,
widget_id: widget_size,
parent_id,
});
widget_size
}
pub fn get_parent_of(&mut self, widget_id: i32) -> i32 {
if widget_id <= 0 {
0
} else {
self.widgets[widget_id as usize].parent_id
}
}
pub fn get_children_of(&self, parent_id: i32) -> Vec<i32> {
self.widgets
.iter()
.filter(|x| x.parent_id == parent_id)
.map(|x| x.widget_id)
.collect()
}
pub fn get_widget_id_for_point(&mut self, point: Point) -> i32 {
let mut found_id = -1;
for (pos, obj) in self.widgets.iter_mut().enumerate() {
let widget_point = &obj.widget.get_origin();
let widget_size: crate::core::point::Size = obj.widget.get_size();
if widget_size.w > 0 && widget_size.h > 0 {
if point.x >= widget_point.x
&& point.x <= widget_point.x + widget_size.w
&& point.y >= widget_point.y
&& point.y <= widget_point.y + widget_size.h
{
found_id = pos as i32;
}
}
}
found_id
}
pub fn draw(&mut self, widget_id: i32, c: Context, g: &mut G2d) {
let parents_of_widget = self.get_children_of(widget_id);
if parents_of_widget.len() == 0 {
return;
}
for pos in 0..parents_of_widget.len() {
c.reset();
let paint_id = parents_of_widget[pos];
let paint_widget = &mut self.widgets[paint_id as usize];
if &paint_widget.widget.is_invalidated() == &true {
let origin: Point = paint_widget.widget.get_origin().clone();
let size: crate::core::point::Size = paint_widget.widget.get_size().clone();
let new_context: Context = Context {
viewport: c.viewport,
view: c.view,
transform: c.transform.trans(origin.x as f64, origin.y as f64),
draw_state: c.draw_state,
};
let clip: DrawState = c.draw_state.scissor([
origin.x as u32 * 2,
origin.y as u32 * 2,
size.w as u32 * 2,
size.h as u32 * 2,
]);
&paint_widget.widget.draw(new_context, g, &clip);
}
if parents_of_widget[pos] != widget_id {
self.draw(paint_id, c, g);
}
}
}
pub fn keypress_for_id(&mut self, id: i32, key: &Key, state: &ButtonState) {
&self.widgets[id as usize].widget.key_pressed(id, key, state);
}
pub fn mouse_entered_for_id(&mut self, id: i32) {
&self.widgets[id as usize].widget.mouse_entered(id);
}
pub fn mouse_exited_for_id(&mut self, id: i32) {
&self.widgets[id as usize].widget.mouse_exited(id);
}
pub fn mouse_scrolled_for_id(&mut self, id: i32, point: Point) {
&self.widgets[id as usize].widget.mouse_scrolled(id, point);
}
pub fn mouse_moved_for_id(&mut self, id: i32, point: Point) {
let origin = &self.widgets[id as usize].widget.get_origin();
let new_point = Point {
x: point.x - origin.x,
y: point.y - origin.y,
};
&self.widgets[id as usize].widget.mouse_moved(id, new_point);
}
pub fn window_resized_for_id(&mut self, id: i32, size: crate::core::point::Size) {
&self.widgets[id as usize].widget.window_resized(id, size);
}
pub fn handle_focus(&mut self, focus: bool) {
for id in 0..self.widgets.len() as i32 {
&self.widgets[id as usize].widget.window_focused(id, focus);
}
}
pub fn button_down(&mut self, id: i32, button: Button) {
&self.widgets[id as usize].widget.button_down(id, button);
}
pub fn button_up_inside(&mut self, id: i32, button: Button) {
&self.widgets[id as usize].widget.button_up_inside(id, button);
}
pub fn button_up_outside(&mut self, id: i32, button: Button) {
&self.widgets[id as usize].widget.button_up_outside(id, button);
}
pub fn get_widget_for_id(&mut self, id: i32) -> &Box<dyn Widget> {
&self.widgets[id as usize].widget
}
}