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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use std::{
cell::{Cell, RefCell},
collections::BTreeMap,
rc::Rc,
};
use dces::prelude::{Entity, EntityComponentManager, System};
use crate::{css_engine::*, prelude::*, shell::WindowShell, tree::Tree, utils::*};
pub struct EventStateSystem {
pub shell: Rc<RefCell<WindowShell<WindowAdapter>>>,
pub handlers: EventHandlerMap,
pub update: Rc<Cell<bool>>,
pub running: Rc<Cell<bool>>,
pub mouse_down_nodes: RefCell<Vec<Entity>>,
pub states: Rc<RefCell<BTreeMap<Entity, Rc<dyn State>>>>,
pub render_objects: Rc<RefCell<BTreeMap<Entity, Box<dyn RenderObject>>>>,
pub layouts: Rc<RefCell<BTreeMap<Entity, Box<dyn Layout>>>>,
}
impl EventStateSystem {
fn process_top_down_event(
&self,
_event: &EventBox,
_ecm: &mut EntityComponentManager<Tree, StringComponentStore>,
) {
}
fn process_bottom_up_event(
&self,
mouse_position: Point,
event: &EventBox,
ecm: &mut EntityComponentManager<Tree, StringComponentStore>,
) {
let mut matching_nodes = vec![];
let mut current_node = event.source;
let root = ecm.entity_store().root;
let theme = ecm
.component_store()
.get::<Theme>("theme", root)
.unwrap()
.clone();
if let Ok(WindowEvent::Resize { width, height }) = event.downcast_ref::<WindowEvent>() {
if let Ok(bounds) = ecm
.component_store_mut()
.get_mut::<Rectangle>("bounds", root)
{
bounds.set_width(*width);
bounds.set_height(*height);
}
if let Ok(constraint) = ecm
.component_store_mut()
.get_mut::<Constraint>("constraint", root)
{
constraint.set_width(*width);
constraint.set_height(*height);
}
self.update.set(true);
}
if let Ok(event) = event.downcast_ref::<KeyDownEvent>() {
if let Ok(global) = ecm.component_store_mut().get_mut::<Global>("global", root) {
global.keyboard_state.set_key_state(event.event.key, true);
}
}
if let Ok(event) = event.downcast_ref::<KeyUpEvent>() {
if let Ok(global) = ecm.component_store_mut().get_mut::<Global>("global", root) {
global.keyboard_state.set_key_state(event.event.key, false);
}
}
let mut unknown_event = true;
let mut clipped_parent = vec![];
loop {
if let Some(cp) = clipped_parent.last() {
if ecm.entity_store().parent[¤t_node] == Some(*cp) {
clipped_parent.push(current_node);
} else {
clipped_parent.pop();
}
}
if event.downcast_ref::<KeyDownEvent>().is_ok() {
if let Some(focused) = ecm
.component_store()
.get::<Global>("global", root)
.unwrap()
.focused_widget
{
if current_node == focused {
matching_nodes.push(current_node);
}
}
unknown_event = false;
}
if event.downcast_ref::<KeyUpEvent>().is_ok() {
if let Some(focused) = ecm
.component_store()
.get::<Global>("global", root)
.unwrap()
.focused_widget
{
if current_node == focused {
matching_nodes.push(current_node);
}
}
unknown_event = false;
}
if event.downcast_ref::<ScrollEvent>().is_ok() {
if check_mouse_condition(
mouse_position,
&WidgetContainer::new(current_node, ecm, &theme),
) {
matching_nodes.push(current_node);
}
unknown_event = false;
}
if let Ok(event) = event.downcast_ref::<ClickEvent>() {
if check_mouse_condition(
event.position,
&WidgetContainer::new(current_node, ecm, &theme),
) {
let mut add = true;
if let Some(op) = clipped_parent.get(0) {
if !check_mouse_condition(
event.position,
&WidgetContainer::new(*op, ecm, &theme),
) {
add = false;
}
}
if add {
matching_nodes.push(current_node);
self.mouse_down_nodes.borrow_mut().push(current_node);
}
}
unknown_event = false;
}
if let Ok(event) = event.downcast_ref::<MouseDownEvent>() {
if check_mouse_condition(
Point::new(event.x, event.y),
&WidgetContainer::new(current_node, ecm, &theme),
) {
let mut add = true;
if let Some(op) = clipped_parent.get(0) {
if !check_mouse_condition(
Point::new(event.x, event.y),
&WidgetContainer::new(*op, ecm, &theme),
) {
add = false;
}
}
if add {
matching_nodes.push(current_node);
self.mouse_down_nodes.borrow_mut().push(current_node);
}
}
unknown_event = false;
}
if event.downcast_ref::<MouseUpEvent>().is_ok() {
if self.mouse_down_nodes.borrow().contains(¤t_node) {
matching_nodes.push(current_node);
let index = self
.mouse_down_nodes
.borrow()
.iter()
.position(|x| *x == current_node)
.unwrap();
self.mouse_down_nodes.borrow_mut().remove(index);
}
unknown_event = false;
}
if unknown_event
&& *WidgetContainer::new(current_node, ecm, &theme).get::<bool>("enabled")
{
if let Some(handlers) = self.handlers.borrow().get(¤t_node) {
if handlers.iter().any(|handler| handler.handles_event(&event)) {
matching_nodes.push(current_node);
}
}
}
if let Ok(clip) = ecm.component_store().get::<bool>("clip", current_node) {
if *clip {
clipped_parent.clear();
clipped_parent.push(current_node);
}
}
let mut it = ecm.entity_store().start_node(current_node).into_iter();
it.next();
if let Some(node) = it.next() {
current_node = node;
} else {
break;
}
}
let mut handled = false;
let mut disabled_parent = None;
for node in matching_nodes.iter().rev() {
if let Some(dp) = disabled_parent {
if ecm.entity_store().parent[&node] == Some(dp) {
disabled_parent = Some(*node);
continue;
} else {
disabled_parent = None;
}
}
if let Ok(enabled) = ecm.component_store().get::<bool>("enabled", *node) {
if !enabled {
disabled_parent = Some(*node);
continue;
}
}
if let Some(handlers) = self.handlers.borrow().get(node) {
handled = handlers.iter().any(|handler| handler.handle_event(event));
self.update.set(true);
}
if handled {
break;
}
}
}
}
impl System<Tree, StringComponentStore> for EventStateSystem {
fn run(&self, ecm: &mut EntityComponentManager<Tree, StringComponentStore>) {
let mut shell = self.shell.borrow_mut();
loop {
{
let adapter = shell.adapter();
let mouse_position = adapter.mouse_position;
for event in adapter.event_queue.into_iter() {
if let Ok(event) = event.downcast_ref::<SystemEvent>() {
match event {
SystemEvent::Quit => {
self.running.set(false);
return;
}
}
}
match event.strategy {
EventStrategy::TopDown => {
self.process_top_down_event(&event, ecm);
}
EventStrategy::BottomUp => {
self.process_bottom_up_event(mouse_position, &event, ecm);
}
_ => {}
}
}
}
let root = ecm.entity_store().root;
let theme = ecm
.component_store()
.get::<Theme>("theme", root)
.unwrap()
.clone();
let mut current_node = root;
loop {
let mut skip = false;
{
let mut ctx = Context::new(
(current_node, ecm),
&mut shell,
&theme,
self.render_objects.clone(),
self.layouts.clone(),
self.handlers.clone(),
self.states.clone(),
);
if !self.states.borrow().contains_key(¤t_node) {
skip = true;
}
if !skip {
if let Some(state) = self.states.borrow().get(¤t_node) {
state.update(&mut ctx);
}
}
}
let mut it = ecm.entity_store().start_node(current_node).into_iter();
it.next();
if let Some(node) = it.next() {
current_node = node;
} else {
break;
}
}
if shell.adapter().event_queue.is_empty() {
break;
}
}
}
}