1mod events;
4
5use super::*;
6
7impl Ui {
8 pub fn pointer_move(&mut self, x: i32, y: i32, root: NodeId) {
10 self.input_root = Some(root);
11 self.pointer = (x, y);
12 let popup = self
14 .open_lists(root)
15 .into_iter()
16 .rev()
17 .find_map(|l| self.hit(l, x, y, None));
18 self.hot = popup.or_else(|| self.hit(root, x, y, None));
19 if self.pointer_down {
20 if let Some(id) = self.pressed {
21 self.drag_control(id, x, y);
22 let button = matches!(
25 self.nodes[id].kind,
26 Kind::Button { .. }
27 | Kind::Control(Control::Check { .. } | Control::Combo { .. })
28 ) || self.nodes[id].flags & unbanished_formats::ui::flags::MOUSE != 0;
29 if button && self.hot != Some(id) {
30 self.pressed = None;
31 }
32 }
33 }
34 for n in &mut self.nodes {
35 if let Kind::Control(Control::Slider(s)) = &mut n.kind {
36 let k = s.knob(n.rect);
37 s.over_knob = k[0] <= x && x <= k[2] && k[1] <= y && y <= k[3];
38 }
39 }
40 self.update_states(root);
41 for n in &mut self.nodes {
42 if let Kind::List(l) = &mut n.kind {
43 l.hovered = None;
44 }
45 if let Kind::Control(Control::Document(d)) = &mut n.kind {
46 d.hovered_link = None;
47 }
48 }
49 if let Some(h) = self.hot {
50 let rect = self.nodes[h].rect;
51 if let Kind::List(l) = &mut self.nodes[h].kind {
52 let row = ((y - rect[1] + l.scroll) / l.row_height.max(1)).max(0) as usize;
53 l.hovered = (row < l.items.len()).then_some(row);
54 }
55 if let Kind::Control(Control::Document(d)) = &mut self.nodes[h].kind {
56 d.hovered_link = d
57 .links
58 .iter()
59 .position(|(_, r)| contains(*r, x - rect[0], y - rect[1]));
60 }
61 }
62 self.update_tooltip(root);
63 }
64
65 pub fn pointer_button(&mut self, down: bool, root: NodeId) -> Option<Event> {
68 let open: Vec<_> = self
70 .active_nodes(root)
71 .into_iter()
72 .filter(|&id| {
73 matches!(
74 self.nodes[id].kind,
75 Kind::Control(Control::Combo { open: true, .. })
76 )
77 })
78 .collect();
79 if down {
80 for id in open {
81 let Kind::Control(Control::Combo { button, list, .. }) = self.nodes[id].kind else {
82 continue;
83 };
84 if self.hot != Some(id) && self.hot != Some(button) && self.hot != Some(list) {
85 if let Kind::Control(Control::Combo { open, .. }) = &mut self.nodes[id].kind {
86 *open = false;
87 }
88 }
89 }
90 }
91 self.pointer_down = down;
92 let mut event = None;
93 if down {
94 self.pressed = self.hot;
95 if let Some(f) = self.focus.filter(|&f| Some(f) != self.hot) {
96 if matches!(self.nodes[f].kind, Kind::Control(Control::Edit(_))) {
97 self.commit_edit(f);
98 }
99 }
100 self.focus = self.hot;
101 if self.hot.is_some_and(|id| {
104 self.interactive(id)
105 && self.nodes[id].usable()
106 && !matches!(
107 self.nodes[id].kind,
108 Kind::List(_)
109 | Kind::Control(
110 Control::Slider(_) | Control::Scroll { .. } | Control::Edit(_)
111 )
112 )
113 }) {
114 self.sounds.push(4);
115 }
116 if let Some(id) = self.hot {
117 let rect = self.nodes[id].rect;
118 match &mut self.nodes[id].kind {
119 Kind::List(l) => {
120 if let Some(row) = l.hovered {
121 l.selected = Some(row);
122 event = Some(Event::Selected(id, row));
123 }
124 }
125 Kind::Control(Control::Edit(e)) => {
129 let target = self.pointer.0 - rect[0];
130 let len = e.text.text.chars().count();
131 e.cursor = (0..len)
132 .find(|&i| {
133 let prefix = e.text.text.chars().take(i + 1).collect::<String>();
134 let x = e.text.font.map_or(0, |f| {
135 TextLayout::compute(&self.res, f, &prefix, None, 0).width
136 });
137 0 <= target && target <= x
138 })
139 .unwrap_or(len);
140 e.caret = true;
141 e.blink = 0.0;
142 }
143 Kind::Control(Control::Document(d)) => d.pressed_link = d.hovered_link,
144 Kind::Control(Control::Scroll {
148 sprites,
149 vertical,
150 value,
151 page,
152 inset,
153 drag,
154 ..
155 }) if *page < 1.0 => {
156 let (x, y) = self.pointer;
157 let thumb = scroll_thumb(rect, *vertical, *value, *page, *inset, sprites);
158 let step = *page / (1.0 - *page);
159 if thumb[0] <= x && x <= thumb[2] && thumb[1] <= y && y <= thumb[3] {
160 *drag = Some((*value, if *vertical { y } else { x }));
161 self.sounds.push(4);
162 } else if (*vertical && thumb[3] < y) || thumb[2] < x {
163 let v = *value + step;
164 self.set_value(id, v);
165 self.sounds.push(4);
166 self.pending.push(Event::Changed(id));
167 self.scroll_parent(id);
168 } else if (*vertical && y < thumb[1]) || x < thumb[0] {
169 let v = *value - step;
170 self.set_value(id, v);
171 self.sounds.push(4);
172 self.pending.push(Event::Changed(id));
173 self.scroll_parent(id);
174 }
175 }
176 Kind::Control(Control::Slider(sl)) => {
180 let (x, y) = self.pointer;
181 let knob = sl.knob(rect);
182 if knob[0] <= x && x <= knob[2] && knob[1] <= y && y <= knob[3] {
183 sl.drag = Some((sl.value, x));
184 self.sounds.push(4);
185 } else if knob[0] <= x && x <= knob[2] {
186 } else {
187 let step = if knob[2] < x { sl.step() } else { -sl.step() };
188 let value = sl.value + step;
189 self.set_value(id, value);
190 self.sounds.push(4);
191 event = Some(Event::Changed(id));
192 }
193 }
194 _ => {
195 self.drag_control(id, self.pointer.0, self.pointer.1);
196 }
197 }
198 }
199 } else {
200 if let Some(id) = self.pressed {
203 let rect = self.nodes[id].rect;
204 let (x, y) = self.pointer;
205 let drag = match &mut self.nodes[id].kind {
206 Kind::Control(Control::Slider(sl)) => sl.drag.take(),
207 Kind::Control(Control::Scroll { drag, .. }) => drag.take(),
208 _ => None,
209 };
210 if let Some((start, _)) = drag {
211 let near = rect[0] - 32 <= x
212 && x <= rect[2] + 32
213 && rect[1] - 32 <= y
214 && y <= rect[3] + 32;
215 if near {
216 self.sounds.push(5);
217 } else {
218 self.set_value(id, start);
221 self.scroll_parent(id);
222 }
223 }
224 }
225 if let Some(id) = self.pressed.filter(|p| Some(*p) == self.hot) {
226 event = self.activate(id).or(event);
227 if event.is_some() {
229 self.sounds.push(5);
230 }
231 }
232 self.pressed = None;
233 }
234 if let Some(e) = event {
235 event = Some(self.route_event(root, e));
236 }
237 if !down {
238 for n in &mut self.nodes {
239 if let Kind::Control(Control::Document(d)) = &mut n.kind {
240 d.pressed_link = None;
241 }
242 }
243 }
244 self.update_states(root);
245 event
246 }
247
248 pub fn wheel(&mut self, delta: i32) -> bool {
251 if delta == 0 {
252 return false;
253 }
254 let (x, y) = self.pointer;
255 let active = self
257 .input_root
258 .map(|r| self.active_nodes(r))
259 .unwrap_or_default();
260 let hovered = active
261 .into_iter()
262 .filter_map(|id| {
263 let n = &self.nodes[id];
264 matches!(n.kind, Kind::Control(Control::Container { .. }))
265 .then_some(n.rect)
266 .filter(|r| contains(*r, x, y))
267 .map(|r| (id, (r[2] - r[0]) * (r[3] - r[1])))
268 })
269 .min_by_key(|c| c.1);
270 if let Some((id, _)) = hovered {
271 if let Kind::Control(Control::Container { wheel, .. }) = &mut self.nodes[id].kind {
272 *wheel += if 0 < delta { -7 } else { 7 };
273 }
274 return true;
275 }
276 if let Some(h) = self.hot {
278 let height = self.nodes[h].rect[3] - self.nodes[h].rect[1];
279 if let Kind::List(l) = &mut self.nodes[h].kind {
280 let max = (l.items.len() as i32 * l.row_height - height).max(0);
281 l.scroll = (l.scroll - delta).clamp(0, max);
282 return true;
283 }
284 }
285 false
286 }
287
288 pub fn update_containers(&mut self) {
291 for id in 0..self.nodes.len() {
292 let Kind::Control(Control::Container {
293 content,
294 bars,
295 view,
296 wheel,
297 ..
298 }) = self.nodes[id].kind
299 else {
300 continue;
301 };
302 if wheel == 0 || bars[1].is_none() {
303 continue;
304 }
305 let height = (view[3] - view[1]) as f32;
306 let max = content.map_or(0.0, |n| self.nodes[n].size[1] as f32 - height);
307 if max <= 0.0 {
308 continue;
309 }
310 if let Kind::Control(Control::Container {
311 offset,
312 wheel,
313 fine,
314 ..
315 }) = &mut self.nodes[id].kind
316 {
317 if *fine as i32 != offset[1] {
318 *fine = offset[1] as f32;
319 }
320 let mut step = (max / (max / height)) / 7.0 * 0.33;
321 if *wheel < 0 {
322 step = -step;
323 *wheel += 1;
324 } else {
325 *wheel -= 1;
326 }
327 *fine = (*fine + step).clamp(0.0, max);
328 offset[1] = *fine as i32;
329 }
330 }
331 }
332
333 pub(crate) fn active_nodes(&self, root: NodeId) -> Vec<NodeId> {
334 fn visit(ui: &Ui, id: NodeId, out: &mut Vec<NodeId>) {
335 let n = &ui.nodes[id];
336 if !n.visible() || !n.enabled {
337 return;
338 }
339 if n.usable() {
341 out.push(id);
342 }
343 let children = match &n.kind {
344 Kind::Control(c) => c.active_children(),
345 Kind::Button { .. } => Vec::new(),
346 _ => n.children.clone(),
347 };
348 for c in children {
349 visit(ui, c, out);
350 }
351 }
352 let mut out = Vec::new();
353 visit(self, root, &mut out);
354 out
355 }
356
357 pub(super) fn interactive(&self, id: NodeId) -> bool {
358 if self.nodes[id].flags & unbanished_formats::ui::flags::MOUSE != 0 {
359 return true;
360 }
361 match &self.nodes[id].kind {
362 Kind::Button { .. } | Kind::List(_) => true,
363 Kind::Control(
364 Control::Slider(_)
365 | Control::Scroll { .. }
366 | Control::Check { .. }
367 | Control::Combo { .. },
368 ) => true,
369 Kind::Control(Control::Edit(e)) => e.max_length > 0,
370 Kind::Control(Control::Document(d)) => !d.links.is_empty(),
371 _ => false,
372 }
373 }
374
375 pub(super) fn hit(&self, id: NodeId, x: i32, y: i32, clip: Option<[i32; 4]>) -> Option<NodeId> {
376 let n = &self.nodes[id];
377 if !n.visible() || !n.enabled || clip.is_some_and(|r| !contains(r, x, y)) {
378 return None;
379 }
380 let mut child_clip = clip;
381 if let Kind::Control(Control::Document(d)) = &n.kind {
382 return d
383 .links
384 .iter()
385 .any(|(_, r)| contains(*r, x - n.rect[0], y - n.rect[1]))
386 .then_some(id);
387 }
388 let children = match &n.kind {
389 Kind::Control(Control::Container {
390 content,
391 bars,
392 view,
393 ..
394 }) => {
395 for c in bars.iter().flatten().rev() {
396 if let Some(h) = self.hit(*c, x, y, clip) {
397 return Some(h);
398 }
399 }
400 if let Some(c) = content {
401 return self.hit(*c, x, y, intersect(clip, *view));
402 }
403 return None;
404 }
405 Kind::Control(Control::Clip { .. }) => {
406 child_clip = intersect(clip, n.rect);
407 if let Kind::Control(c) = &n.kind {
408 c.active_children()
409 } else {
410 Vec::new()
411 }
412 }
413 Kind::Control(Control::Check { .. } | Control::Combo { .. }) => {
414 if let Kind::Control(Control::Combo {
415 list, open: true, ..
416 }) = &n.kind
417 {
418 if let Some(h) = self.hit(*list, x, y, None) {
419 return Some(h);
420 }
421 }
422 return n.contains(x, y).then_some(id);
423 }
424 Kind::Control(c) => c.active_children(),
425 _ => n.children.clone(),
426 };
427 for child in children.into_iter().rev() {
428 if let Some(h) = self.hit(child, x, y, child_clip) {
429 return Some(h);
430 }
431 }
432 (self.interactive(id) && n.contains(x, y)).then_some(id)
433 }
434}