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
use std::rc::Rc;
use crate::{
prelude::*,
proc_macros::{Event, IntoHandler},
shell::MouseButton,
utils::*,
};
pub fn check_mouse_condition(mouse_position: Point, widget: &WidgetContainer<'_>) -> bool {
let enabled = widget.get::<bool>("enabled");
if !enabled {
return false;
}
let bounds = widget.get::<Rectangle>("bounds");
let position = widget.get::<Point>("position");
let mut rect = Rectangle::new(0.0, 0.0, bounds.width(), bounds.height());
rect.set_x(position.x);
rect.set_y(position.y);
rect.contains((mouse_position.x, mouse_position.y))
}
#[derive(Event)]
pub struct MouseMoveEvent {
pub x: f64,
pub y: f64,
}
#[derive(Event)]
pub struct ScrollEvent {
pub delta: Point,
}
#[derive(Event)]
pub struct MouseUpEvent {
pub button: MouseButton,
pub x: f64,
pub y: f64,
}
#[derive(Event)]
pub struct ClickEvent {
pub position: Point,
}
#[derive(Event)]
pub struct MouseDownEvent {
pub button: MouseButton,
pub x: f64,
pub y: f64,
}
#[derive(Event)]
pub struct GlobalMouseUpEvent {
pub button: MouseButton,
pub x: f64,
pub y: f64,
}
pub type MouseHandlerFunction = dyn Fn(&mut StatesContext, Point) -> bool + 'static;
pub type GlobalMouseHandlerFunction = dyn Fn(&mut StatesContext, Point) + 'static;
pub struct ClickEventHandler {
handler: Rc<MouseHandlerFunction>,
}
impl Into<Rc<dyn EventHandler>> for ClickEventHandler {
fn into(self) -> Rc<dyn EventHandler> {
Rc::new(self)
}
}
impl EventHandler for ClickEventHandler {
fn handle_event(&self, state_context: &mut StatesContext, event: &EventBox) -> bool {
event
.downcast_ref::<ClickEvent>()
.ok()
.map_or(false, |event| (self.handler)(state_context, event.position))
}
fn handles_event(&self, event: &EventBox) -> bool {
event.is_type::<ClickEvent>()
}
}
#[derive(IntoHandler)]
pub struct MouseDownEventHandler {
handler: Rc<MouseHandlerFunction>,
}
impl EventHandler for MouseDownEventHandler {
fn handle_event(&self, state_context: &mut StatesContext, event: &EventBox) -> bool {
event
.downcast_ref::<MouseDownEvent>()
.ok()
.map_or(false, |event| {
(self.handler)(state_context, Point::new(event.x, event.y))
})
}
fn handles_event(&self, event: &EventBox) -> bool {
event.is_type::<MouseDownEvent>()
}
}
#[derive(IntoHandler)]
pub struct GlobalMouseUpEventHandler {
handler: Rc<GlobalMouseHandlerFunction>,
}
impl EventHandler for GlobalMouseUpEventHandler {
fn handle_event(&self, state_context: &mut StatesContext, event: &EventBox) -> bool {
event
.downcast_ref::<GlobalMouseUpEvent>()
.ok()
.map_or(false, |event| {
(self.handler)(state_context, Point::new(event.x, event.y));
false
})
}
fn handles_event(&self, event: &EventBox) -> bool {
event.is_type::<GlobalMouseUpEvent>()
}
}
#[derive(IntoHandler)]
pub struct MouseUpEventHandler {
handler: Rc<MouseHandlerFunction>,
}
impl EventHandler for MouseUpEventHandler {
fn handle_event(&self, state_context: &mut StatesContext, event: &EventBox) -> bool {
event
.downcast_ref::<MouseUpEvent>()
.ok()
.map_or(false, |event| {
(self.handler)(state_context, Point::new(event.x, event.y))
})
}
fn handles_event(&self, event: &EventBox) -> bool {
event.is_type::<MouseUpEvent>()
}
}
#[derive(IntoHandler)]
pub struct MouseMoveEventHandler {
handler: Rc<MouseHandlerFunction>,
}
impl EventHandler for MouseMoveEventHandler {
fn handle_event(&self, state_context: &mut StatesContext, event: &EventBox) -> bool {
event
.downcast_ref::<MouseMoveEvent>()
.ok()
.map_or(false, |event| {
(self.handler)(state_context, Point::new(event.x, event.y))
})
}
fn handles_event(&self, event: &EventBox) -> bool {
event.is_type::<MouseMoveEvent>()
}
}
#[derive(IntoHandler)]
pub struct ScrollEventHandler {
handler: Rc<MouseHandlerFunction>,
}
impl EventHandler for ScrollEventHandler {
fn handle_event(&self, state_context: &mut StatesContext, event: &EventBox) -> bool {
event
.downcast_ref::<ScrollEvent>()
.ok()
.map_or(false, |event| {
(self.handler)(state_context, Point::new(event.delta.x, event.delta.y))
})
}
fn handles_event(&self, event: &EventBox) -> bool {
event.is_type::<ScrollEvent>()
}
}
pub trait MouseHandler: Sized + Widget {
fn on_click<H: Fn(&mut StatesContext, Point) -> bool + 'static>(self, handler: H) -> Self {
self.insert_handler(ClickEventHandler {
handler: Rc::new(handler),
})
}
fn on_mouse_down<H: Fn(&mut StatesContext, Point) -> bool + 'static>(self, handler: H) -> Self {
self.insert_handler(MouseDownEventHandler {
handler: Rc::new(handler),
})
}
fn on_mouse_up<H: Fn(&mut StatesContext, Point) -> bool + 'static>(self, handler: H) -> Self {
self.insert_handler(MouseUpEventHandler {
handler: Rc::new(handler),
})
}
fn on_global_mouse_up<H: Fn(&mut StatesContext, Point) + 'static>(self, handler: H) -> Self {
self.insert_handler(GlobalMouseUpEventHandler {
handler: Rc::new(handler),
})
}
fn on_mouse_move<H: Fn(&mut StatesContext, Point) -> bool + 'static>(self, handler: H) -> Self {
self.insert_handler(MouseMoveEventHandler {
handler: Rc::new(handler),
})
}
fn on_scroll<H: Fn(&mut StatesContext, Point) -> bool + 'static>(self, handler: H) -> Self {
self.insert_handler(ScrollEventHandler {
handler: Rc::new(handler),
})
}
}