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
use std::collections::HashMap;
use std::collections::HashSet;
use crate::core::point::*;
use crate::core::widget_store::*;
use crate::widget::widget::*;
use piston_window::*;
pub struct Pushrod {
window: PistonWindow,
pub widget_store: WidgetStore,
}
impl Pushrod {
pub fn new(window: PistonWindow) -> Self {
Self {
window,
widget_store: WidgetStore::new(),
}
}
pub fn get_factory(&mut self) -> &mut GfxFactory {
&mut self.window.factory
}
pub fn add_widget(&mut self, widget: Box<dyn Widget>) -> i32 {
self.widget_store.add_widget(widget)
}
pub fn add_widget_to_parent(&mut self, widget: Box<dyn Widget>, parent_id: i32) -> i32 {
self.widget_store.add_widget_to_parent(widget, parent_id)
}
fn handle_draw(&mut self, event: &Event) {
let widgets = &mut self.widget_store;
self.window.draw_2d(event, |c, g| widgets.draw(0, c, g));
}
pub fn run(&mut self) {
let mut last_widget_id = -1;
let mut previous_mouse_position: Point = make_origin_point();
let mut button_map: HashMap<i32, HashSet<Button>> = HashMap::new();
while let Some(ref event) = &self.window.next() {
event.mouse_cursor(|x, y| {
let mouse_point = make_point_f64(x, y);
if mouse_point.x != previous_mouse_position.x
|| mouse_point.y != previous_mouse_position.y
{
previous_mouse_position = mouse_point.clone();
let current_widget_id = self
.widget_store
.get_widget_id_for_point(mouse_point.clone());
let current_parent_for_widget =
self.widget_store.get_parent_of(current_widget_id);
if current_widget_id != -1 {
self.widget_store
.mouse_moved_for_id(current_widget_id, mouse_point.clone());
}
if current_widget_id != last_widget_id {
if last_widget_id != -1 {
self.widget_store.mouse_exited_for_id(last_widget_id);
}
last_widget_id = current_widget_id;
if last_widget_id != -1 {
self.widget_store.mouse_entered_for_id(last_widget_id);
}
eprintln!(
"Widget IDs: current={} parent={} children={:?}",
current_widget_id,
current_parent_for_widget,
self.widget_store.get_children_of(current_widget_id)
);
}
}
});
event.mouse_scroll(|x, y| {
let mouse_point = make_point_f64(x, y);
if last_widget_id != -1 {
self.widget_store
.mouse_scrolled_for_id(last_widget_id, mouse_point.clone());
}
});
event.button(|args| match args.state {
ButtonState::Press => {
button_map
.entry(last_widget_id)
.or_insert(HashSet::new())
.insert(args.button);
self.widget_store.button_down(last_widget_id, args.button);
}
ButtonState::Release => {
let button_set = button_map.entry(last_widget_id).or_insert(HashSet::new());
if button_set.contains(&args.button) {
button_set.remove(&args.button);
self.widget_store
.button_up_inside(last_widget_id, args.button);
} else {
for (widget_id, button_set) in button_map.iter_mut() {
if button_set.contains(&args.button) {
self.widget_store.button_up_outside(*widget_id, args.button);
button_set.remove(&args.button);
}
}
}
}
});
event.resize(|w, h| {
self.widget_store.handle_resize(w as u32, h as u32);
});
event.focus(|focused| {
self.widget_store.handle_focus(focused);
});
match event {
Event::Input(Input::Button(ButtonArgs {
state,
button: Button::Keyboard(key),
scancode: _,
})) => {
self.widget_store
.keypress_for_id(last_widget_id, &key, &state);
}
_ => {}
};
event.resize(|_, _| {
self.widget_store.invalidate_all_widgets();
});
event.render(|_| {
self.handle_draw(&event);
self.widget_store.invalidate_all_widgets();
});
}
}
}