1//! Pointer hit-testing, marquee selection, pan/zoom, popup menus, and keyboard/mouse input
2//! handling for [`SplineView`].
3
4use super::modal::{AxisLock, TransformMode, ensure_handles, hypot2};
5use super::presets::{EasingPreset, apply_easing_preset, apply_handle_mode};
6use super::{
7 HandlePart, POPUP_HEADER_H, POPUP_PAD, POPUP_ROW_H, POPUP_W, PointRef, RULER_H, SIDEBAR_W,
8 SplineAction, SplineView,
9};
10use model::{Comp, CompHistory, HandleMode, Keyframe};
11use std::collections::HashMap;
12use winit::event::{ElementState, MouseButton};
13use winit::keyboard::{Key, KeyCode, ModifiersState, NamedKey, PhysicalKey};
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub(super) enum PopupKind {
17 HandleType,
18 Easing,
19}
20
21#[derive(Clone, Debug)]
22pub(super) struct PopupMenu {
23 pub(super) kind: PopupKind,
24 pub(super) pos: [f32; 2],
25}
26
27pub(super) const HANDLE_MENU_ITEMS: &[(HandleMode, &str)] = &[
28 (HandleMode::Free, "1: Free"),
29 (HandleMode::Aligned, "2: Aligned"),
30 (HandleMode::Auto, "3: Auto"),
31 (HandleMode::Vector, "4: Vector"),
32];
33
34pub(super) const EASING_MENU_ITEMS: &[(EasingPreset, &str)] = &[
35 (EasingPreset::Linear, "1: Linear"),
36 (EasingPreset::Smooth, "2: Smooth"),
37 (EasingPreset::StepIn, "3: Step In"),
38 (EasingPreset::StepOut, "4: Step Out"),
39 (EasingPreset::EaseInOut, "5: Ease In-Out"),
40 (EasingPreset::EaseOut, "6: Ease Out"),
41 (EasingPreset::EaseIn, "7: Ease In"),
42 (EasingPreset::KnifeEase, "8: Knife Ease"),
43];
44
45#[derive(Clone, Debug)]
46pub(super) struct BoxSelect {
47 pub(super) start_local: [f32; 2],
48 pub(super) cur_local: [f32; 2],
49}
50
51impl SplineView {
52 pub(super) fn hit_point(&self, comp: &Comp, local: [f32; 2]) -> Option<PointRef> {
53 let mut best_handle: Option<(f32, PointRef)> = None;
54 let mut best_key: Option<(f32, PointRef)> = None;
55
56 for (_, sname) in &self.channels {
57 if self.hidden_splines.contains(sname) {
58 continue;
59 }
60 let Some(sp) = comp.splines.get(sname) else { continue };
61 let bounds = self.spline_bounds(comp, sname);
62 for (ki, kf) in sp.keyframes.iter().enumerate() {
63 let kp = self.spline_to_local(kf.time, kf.value, bounds);
64 let kd = hypot2(local, kp);
65 if kd < 15.0 && best_key.as_ref().is_none_or(|(d, _)| kd < *d) {
66 best_key = Some((
67 kd,
68 PointRef { spline: sname.clone(), key_idx: ki, part: HandlePart::Key },
69 ));
70 }
71
72 if self.is_key_or_handle_selected(sname, ki) {
73 if let Some(lh) = kf.lh {
74 let lp = self.spline_to_local(lh[0], lh[1], bounds);
75 let ld = hypot2(local, lp);
76 if ld < 14.0 && best_handle.as_ref().is_none_or(|(d, _)| ld < *d) {
77 best_handle = Some((
78 ld,
79 PointRef { spline: sname.clone(), key_idx: ki, part: HandlePart::LeftHandle },
80 ));
81 }
82 }
83 if let Some(rh) = kf.rh {
84 let rp = self.spline_to_local(rh[0], rh[1], bounds);
85 let rd = hypot2(local, rp);
86 if rd < 14.0 && best_handle.as_ref().is_none_or(|(d, _)| rd < *d) {
87 best_handle = Some((
88 rd,
89 PointRef { spline: sname.clone(), key_idx: ki, part: HandlePart::RightHandle },
90 ));
91 }
92 }
93 }
94 }
95 }
96 best_handle.or(best_key).map(|(_, pr)| pr)
97 }
98
99 pub fn is_popup_open(&self) -> bool {
100 self.popup.is_some()
101 }
102
103 pub(super) fn popup_rect(&self, popup: &PopupMenu) -> [f32; 4] {
104 let count = match popup.kind {
105 PopupKind::HandleType => HANDLE_MENU_ITEMS.len(),
106 PopupKind::Easing => EASING_MENU_ITEMS.len(),
107 };
108 let h = POPUP_HEADER_H + count as f32 * POPUP_ROW_H + POPUP_PAD;
109 let x = popup.pos[0].clamp(4.0, (self.size[0] - POPUP_W - 4.0).max(4.0));
110 let y = popup.pos[1].clamp(4.0, (self.size[1] - h - 4.0).max(4.0));
111 [x, y, x + POPUP_W, y + h]
112 }
113
114 pub(super) fn popup_item_at(&self, popup: &PopupMenu, local: [f32; 2]) -> Option<usize> {
115 let r = self.popup_rect(popup);
116 if local[0] < r[0] || local[0] > r[2] || local[1] < r[1] + POPUP_HEADER_H || local[1] > r[3] {
117 return None;
118 }
119 let idx = ((local[1] - (r[1] + POPUP_HEADER_H)) / POPUP_ROW_H).floor() as isize;
120 let count = match popup.kind {
121 PopupKind::HandleType => HANDLE_MENU_ITEMS.len(),
122 PopupKind::Easing => EASING_MENU_ITEMS.len(),
123 };
124 if idx >= 0 && (idx as usize) < count {
125 Some(idx as usize)
126 } else {
127 None
128 }
129 }
130
131 fn open_popup(&mut self, kind: PopupKind) {
132 self.popup = Some(PopupMenu {
133 kind,
134 pos: [self.pointer_local[0] + 6.0, self.pointer_local[1] + 6.0],
135 });
136 }
137
138 fn selected_keys_by_spline(&self, comp: &Comp) -> HashMap<String, Vec<usize>> {
139 let mut by_spline: HashMap<String, Vec<usize>> = HashMap::new();
140 for pref in &self.selected {
141 if let Some(sp) = comp.splines.get(&pref.spline) {
142 if pref.key_idx < sp.keyframes.len() {
143 by_spline.entry(pref.spline.clone()).or_default().push(pref.key_idx);
144 }
145 }
146 }
147 for idxs in by_spline.values_mut() {
148 idxs.sort_unstable();
149 idxs.dedup();
150 }
151 by_spline
152 }
153
154 fn apply_selected_handle_mode(
155 &mut self,
156 comp: &mut Comp,
157 history: Option<&mut CompHistory>,
158 mode: HandleMode,
159 ) -> bool {
160 let by_spline = self.selected_keys_by_spline(comp);
161 if by_spline.is_empty() {
162 return false;
163 }
164 if let Some(h) = history {
165 h.push_snapshot(comp, "Set Handle Type");
166 }
167 for (sname, idxs) in by_spline {
168 if let Some(sp) = comp.splines.get_mut(&sname) {
169 for idx in idxs {
170 apply_handle_mode(sp, idx, mode);
171 }
172 }
173 }
174 comp.revision += 1;
175 true
176 }
177
178 fn apply_selected_easing(
179 &mut self,
180 comp: &mut Comp,
181 history: Option<&mut CompHistory>,
182 preset: EasingPreset,
183 ) -> bool {
184 let by_spline = self.selected_keys_by_spline(comp);
185 if by_spline.is_empty() {
186 return false;
187 }
188 let pre_snap = history.as_ref().map(|_| comp.clone());
189 let mut changed = false;
190 for (sname, idxs) in by_spline {
191 if let Some(sp) = comp.splines.get_mut(&sname) {
192 if apply_easing_preset(sp, &idxs, preset) {
193 changed = true;
194 }
195 }
196 }
197 if changed {
198 if let (Some(h), Some(snap)) = (history, pre_snap) {
199 h.push_snapshot(&snap, format!("Easing: {}", preset.label()));
200 }
201 comp.revision += 1;
202 }
203 changed
204 }
205
206 #[allow(clippy::too_many_arguments)]
207 fn handle_key(
208 &mut self,
209 comp: &mut Comp,
210 mut history: Option<&mut CompHistory>,
211 current_frame: f64,
212 physical: Option<PhysicalKey>,
213 key: &Key,
214 kmod: ModifiersState,
215 actions: &mut Vec<SplineAction>,
216 ) {
217 if let Some(popup) = self.popup.clone() {
218 match key {
219 Key::Named(NamedKey::Escape) => {
220 self.popup = None;
221 }
222 Key::Character(c) => {
223 let lc = c.to_ascii_lowercase();
224 if lc == "v" {
225 self.popup = if popup.kind == PopupKind::HandleType {
226 None
227 } else {
228 Some(PopupMenu { kind: PopupKind::HandleType, pos: popup.pos })
229 };
230 return;
231 }
232 if lc == "t" {
233 self.popup = if popup.kind == PopupKind::Easing {
234 None
235 } else {
236 Some(PopupMenu { kind: PopupKind::Easing, pos: popup.pos })
237 };
238 return;
239 }
240 if let Ok(num) = lc.parse::<usize>() {
241 if num >= 1 {
242 let idx = num - 1;
243 match popup.kind {
244 PopupKind::HandleType => {
245 if let Some(&(mode, _)) = HANDLE_MENU_ITEMS.get(idx) {
246 self.popup = None;
247 if self.apply_selected_handle_mode(comp, history, mode) {
248 actions.push(SplineAction::SplineEdited);
249 }
250 }
251 }
252 PopupKind::Easing => {
253 if let Some(&(preset, _)) = EASING_MENU_ITEMS.get(idx) {
254 self.popup = None;
255 if self.apply_selected_easing(comp, history, preset) {
256 actions.push(SplineAction::SplineEdited);
257 }
258 }
259 }
260 }
261 }
262 }
263 }
264 _ => {}
265 }
266 return;
267 }
268
269 if physical == Some(PhysicalKey::Code(KeyCode::NumpadDecimal)) {
270 self.fit_all(comp);
271 return;
272 }
273 let plain = !kmod.super_key() && !kmod.control_key() && !kmod.alt_key();
274 match key {
275 Key::Named(NamedKey::Escape) => {
276 if self.modal.is_some() {
277 self.cancel_modal(comp);
278 actions.push(SplineAction::SplineEdited);
279 }
280 }
281 Key::Named(NamedKey::Enter) => {
282 if self.modal.is_some() {
283 self.confirm_modal(comp, history);
284 actions.push(SplineAction::SplineEdited);
285 }
286 }
287 Key::Named(NamedKey::Delete | NamedKey::Backspace) => {
288 if self.modal.is_none() && self.delete_selected(comp, history) {
289 actions.push(SplineAction::SplineEdited);
290 }
291 }
292 Key::Character(c) => match c.to_ascii_lowercase().as_str() {
293 "f" if !kmod.control_key() && !kmod.alt_key() => self.fit_all(comp),
294 "." if plain => self.fit_all(comp),
295 "n" if plain && self.modal.is_none() => {
296 self.toggle_normalized(comp);
297 }
298 "v" if plain && self.modal.is_none() => {
299 self.open_popup(PopupKind::HandleType);
300 }
301 "t" if plain && self.modal.is_none() => {
302 self.open_popup(PopupKind::Easing);
303 }
304 "g" if plain && self.modal.is_none() => {
305 self.begin_modal(comp, history.is_some(), TransformMode::Grab);
306 }
307 "r" if plain && self.modal.is_none() => {
308 self.begin_modal(comp, history.is_some(), TransformMode::Rotate);
309 }
310 "s" if plain && self.modal.is_none() => {
311 self.begin_modal(comp, history.is_some(), TransformMode::Scale);
312 }
313 "x" if plain && self.modal.is_some() => {
314 if let Some(m) = &mut self.modal {
315 m.axis = if m.axis == AxisLock::X { AxisLock::None } else { AxisLock::X };
316 }
317 if self.apply_modal(comp, kmod.shift_key()) {
318 actions.push(SplineAction::SplineEdited);
319 }
320 }
321 "x" if plain && self.modal.is_none() => {
322 // Blender `X` deletes selected keys when not in a modal transform!
323 if self.delete_selected(comp, history) {
324 actions.push(SplineAction::SplineEdited);
325 }
326 }
327 "y" if plain && self.modal.is_some() => {
328 if let Some(m) = &mut self.modal {
329 m.axis = if m.axis == AxisLock::Y { AxisLock::None } else { AxisLock::Y };
330 }
331 if self.apply_modal(comp, kmod.shift_key()) {
332 actions.push(SplineAction::SplineEdited);
333 }
334 }
335 "i" if plain && self.modal.is_none() => {
336 // Blender `I` inserts keyframe at current integer frame on all visible channels
337 let t = current_frame.round();
338 let will_insert = self.channels.iter().any(|(_, sname)| {
339 !self.hidden_splines.contains(sname)
340 && comp
341 .splines
342 .get(sname)
343 .is_some_and(|sp| !sp.keyframes.iter().any(|k| (k.time - t).abs() < 0.5))
344 });
345 if will_insert {
346 if let Some(h) = history.as_deref_mut() {
347 h.push_snapshot(comp, "Insert Keyframe");
348 }
349 for (_, sname) in &self.channels {
350 if self.hidden_splines.contains(sname) {
351 continue;
352 }
353 if let Some(sp) = comp.splines.get_mut(sname) {
354 if !sp.keyframes.iter().any(|k| (k.time - t).abs() < 0.5) {
355 let v = sp.eval(t);
356 let at = sp.keyframes.partition_point(|k| k.time < t);
357 sp.keyframes.insert(at, Keyframe::new(t, v));
358 self.selected = self
359 .selected
360 .drain()
361 .map(|mut p| {
362 if p.spline == *sname && p.key_idx >= at {
363 p.key_idx += 1;
364 }
365 p
366 })
367 .collect();
368 }
369 }
370 }
371 comp.revision += 1;
372 actions.push(SplineAction::SplineEdited);
373 }
374 }
375 "a" if plain && self.modal.is_none() => {
376 if !self.selected.is_empty() {
377 self.selected.clear();
378 } else {
379 for (_, sname) in &self.channels {
380 if self.hidden_splines.contains(sname) {
381 continue;
382 }
383 if let Some(sp) = comp.splines.get(sname) {
384 for ki in 0..sp.keyframes.len() {
385 self.selected.insert(PointRef {
386 spline: sname.clone(),
387 key_idx: ki,
388 part: HandlePart::Key,
389 });
390 }
391 }
392 }
393 }
394 }
395 "l" if plain && self.modal.is_none() => {
396 let by_spline = self.selected_keys_by_spline(comp);
397 if !by_spline.is_empty() {
398 if let Some(h) = history.as_deref_mut() {
399 h.push_snapshot(comp, "Toggle Linear");
400 }
401 for (sname, idxs) in by_spline {
402 if let Some(sp) = comp.splines.get_mut(&sname) {
403 for idx in idxs {
404 if let Some(kf) = sp.keyframes.get_mut(idx) {
405 kf.flags.linear = !kf.flags.linear;
406 if kf.flags.linear {
407 kf.lh = None;
408 kf.rh = None;
409 kf.handle_mode = HandleMode::Vector;
410 } else {
411 ensure_handles(kf);
412 }
413 }
414 }
415 }
416 }
417 comp.revision += 1;
418 actions.push(SplineAction::SplineEdited);
419 }
420 }
421 _ => {}
422 },
423 _ => {}
424 }
425 }
426
427 fn delete_selected(&mut self, comp: &mut Comp, history: Option<&mut CompHistory>) -> bool {
428 let mut by_spline: HashMap<String, Vec<usize>> = HashMap::new();
429 for pref in &self.selected {
430 if pref.part == HandlePart::Key {
431 by_spline.entry(pref.spline.clone()).or_default().push(pref.key_idx);
432 }
433 }
434 for idxs in by_spline.values_mut() {
435 idxs.sort_unstable();
436 idxs.dedup();
437 }
438 let can_delete = by_spline.iter().any(|(sname, idxs)| {
439 comp.splines
440 .get(sname)
441 .is_some_and(|sp| sp.keyframes.len() > 1 && idxs.iter().any(|&i| i < sp.keyframes.len()))
442 });
443 if !can_delete {
444 return false;
445 }
446 if let Some(h) = history {
447 h.push_snapshot(comp, "Delete Keyframes");
448 }
449 let mut changed = false;
450 for (sname, idxs) in by_spline {
451 if let Some(sp) = comp.splines.get_mut(&sname) {
452 for &idx in idxs.iter().rev() {
453 if sp.keyframes.len() > 1 && idx < sp.keyframes.len() {
454 sp.keyframes.remove(idx);
455 changed = true;
456 }
457 }
458 }
459 }
460 if changed {
461 self.selected.clear();
462 comp.revision += 1;
463 }
464 changed
465 }
466
467 pub fn input(
468 &mut self,
469 comp: &mut Comp,
470 current_frame: f64,
471 event: ui::Event,
472 rect: [f32; 4],
473 pointer_window: [f32; 2],
474 modifiers: ModifiersState,
475 ) -> Vec<SplineAction> {
476 self.input_with_history(comp, None, current_frame, event, rect, pointer_window, modifiers)
477 }
478
479 #[allow(clippy::too_many_arguments)]
480 pub fn input_with_history(
481 &mut self,
482 comp: &mut Comp,
483 mut history: Option<&mut CompHistory>,
484 current_frame: f64,
485 event: ui::Event,
486 rect: [f32; 4],
487 pointer_window: [f32; 2],
488 modifiers: ModifiersState,
489 ) -> Vec<SplineAction> {
490 let mut actions = Vec::new();
491 self.pointer_local = [pointer_window[0] - rect[0], pointer_window[1] - rect[1]];
492 self.sanitize_stale(comp);
493 let plot = self.plot_rect();
494
495 match event {
496 ui::Event::PointerMoved(p) => {
497 self.pointer_local = [p[0] - rect[0], p[1] - rect[1]];
498 if self.popup.is_some() {
499 return actions;
500 }
501 if self.scrubbing {
502 let t = self.local_to_data(self.pointer_local)[0].round();
503 actions.push(SplineAction::ScrubFrame(t));
504 } else if let Some((sp, tr, vr)) = self.panning {
505 let dx = f64::from((self.pointer_local[0] - sp[0]) / (plot[2] - plot[0]).max(1.0));
506 let dy = f64::from((self.pointer_local[1] - sp[1]) / (plot[3] - plot[1]).max(1.0));
507 let dt = dx * (tr[1] - tr[0]);
508 let dv = dy * (vr[1] - vr[0]);
509 self.t_range = [tr[0] - dt, tr[1] - dt];
510 self.v_range = [vr[0] + dv, vr[1] + dv];
511 } else if self.modal.is_some() {
512 if self.apply_modal(comp, modifiers.shift_key()) {
513 actions.push(SplineAction::SplineEdited);
514 }
515 } else if let Some((start_l, ref mut dragging)) = self.lmb_drag_point {
516 if !*dragging && hypot2(self.pointer_local, start_l) > 3.5 {
517 *dragging = true;
518 self.begin_modal(comp, history.is_some(), TransformMode::Grab);
519 if self.apply_modal(comp, modifiers.shift_key()) {
520 actions.push(SplineAction::SplineEdited);
521 }
522 }
523 } else if let Some(bs) = &mut self.box_select {
524 bs.cur_local = self.pointer_local;
525 } else {
526 self.hovered = self.hit_point(comp, self.pointer_local);
527 }
528 }
529 ui::Event::Button { button, pressed, .. } => {
530 if let Some(popup) = self.popup.clone() {
531 if pressed {
532 if button == MouseButton::Left {
533 if let Some(idx) = self.popup_item_at(&popup, self.pointer_local) {
534 self.popup = None;
535 match popup.kind {
536 PopupKind::HandleType => {
537 if let Some(&(mode, _)) = HANDLE_MENU_ITEMS.get(idx) {
538 if self.apply_selected_handle_mode(comp, history, mode) {
539 actions.push(SplineAction::SplineEdited);
540 }
541 }
542 }
543 PopupKind::Easing => {
544 if let Some(&(preset, _)) = EASING_MENU_ITEMS.get(idx) {
545 if self.apply_selected_easing(comp, history, preset) {
546 actions.push(SplineAction::SplineEdited);
547 }
548 }
549 }
550 }
551 } else {
552 self.popup = None;
553 }
554 } else {
555 self.popup = None;
556 }
557 }
558 return actions;
559 }
560
561 if button == MouseButton::Middle || (button == MouseButton::Left && modifiers.alt_key()) {
562 if pressed {
563 self.panning = Some((self.pointer_local, self.t_range, self.v_range));
564 } else {
565 self.panning = None;
566 }
567 } else if button == MouseButton::Right {
568 if pressed && self.modal.is_some() {
569 self.cancel_modal(comp);
570 self.lmb_drag_point = None;
571 actions.push(SplineAction::SplineEdited);
572 }
573 } else if button == MouseButton::Left {
574 if pressed {
575 if self.modal.is_some() {
576 self.confirm_modal(comp, history);
577 self.lmb_drag_point = None;
578 actions.push(SplineAction::SplineEdited);
579 } else if point_in_rect(self.pointer_local, self.normalize_pill_rect()) {
580 self.toggle_normalized(comp);
581 } else if self.pointer_local[0] < SIDEBAR_W {
582 let row = ((self.pointer_local[1] - RULER_H - 6.0) / 22.0).floor() as isize;
583 if row >= 0 && (row as usize) < self.channels.len() {
584 let sname = self.channels[row as usize].1.clone();
585 if !self.hidden_splines.remove(&sname) {
586 self.hidden_splines.insert(sname);
587 }
588 }
589 } else if self.pointer_local[1] < RULER_H {
590 self.scrubbing = true;
591 let t = self.local_to_data(self.pointer_local)[0].round();
592 actions.push(SplineAction::ScrubFrame(t));
593 } else if let Some(hit) = self.hit_point(comp, self.pointer_local) {
594 if modifiers.shift_key() {
595 if !self.selected.remove(&hit) {
596 self.selected.insert(hit);
597 }
598 } else if !self.selected.contains(&hit) {
599 self.selected.clear();
600 self.selected.insert(hit);
601 }
602 // Allow direct drag right from mouse down!
603 self.lmb_drag_point = Some((self.pointer_local, false));
604 } else {
605 if !modifiers.shift_key() {
606 self.selected.clear();
607 }
608 self.box_select = Some(BoxSelect {
609 start_local: self.pointer_local,
610 cur_local: self.pointer_local,
611 });
612 }
613 } else {
614 self.scrubbing = false;
615 if let Some((_start, dragging)) = self.lmb_drag_point.take() {
616 if dragging && self.modal.is_some() {
617 self.confirm_modal(comp, history.as_deref_mut());
618 actions.push(SplineAction::SplineEdited);
619 }
620 }
621 if let Some(bs) = self.box_select.take() {
622 let x0 = bs.start_local[0].min(bs.cur_local[0]);
623 let x1 = bs.start_local[0].max(bs.cur_local[0]);
624 let y0 = bs.start_local[1].min(bs.cur_local[1]);
625 let y1 = bs.start_local[1].max(bs.cur_local[1]);
626 if (x1 - x0) > 4.0 || (y1 - y0) > 4.0 {
627 for (_, sname) in &self.channels {
628 if self.hidden_splines.contains(sname) {
629 continue;
630 }
631 let Some(sp) = comp.splines.get(sname) else { continue };
632 let bounds = self.spline_bounds(comp, sname);
633 for (ki, kf) in sp.keyframes.iter().enumerate() {
634 let p = self.spline_to_local(kf.time, kf.value, bounds);
635 if p[0] >= x0 && p[0] <= x1 && p[1] >= y0 && p[1] <= y1 {
636 self.selected.insert(PointRef {
637 spline: sname.clone(),
638 key_idx: ki,
639 part: HandlePart::Key,
640 });
641 }
642 }
643 }
644 }
645 }
646 }
647 }
648 }
649 ui::Event::Wheel(delta) => {
650 // Blender convention: plain scroll zooms toward mouse cursor; Shift zooms Y; Alt pans.
651 if modifiers.alt_key() {
652 let dx = f64::from(delta[0] / (plot[2] - plot[0]).max(1.0));
653 let dy = f64::from(delta[1] / (plot[3] - plot[1]).max(1.0));
654 let dt = dx * (self.t_range[1] - self.t_range[0]);
655 let dv = dy * (self.v_range[1] - self.v_range[0]);
656 self.t_range = [self.t_range[0] - dt, self.t_range[1] - dt];
657 self.v_range = [self.v_range[0] + dv, self.v_range[1] + dv];
658 } else if modifiers.shift_key() {
659 let f = f64::from((1.0 - delta[1] * 0.004).clamp(0.7, 1.4));
660 self.zoom_at(1.0, f, self.pointer_local);
661 } else {
662 let f = f64::from((1.0 - delta[1] * 0.0045).clamp(0.7, 1.4));
663 self.zoom_at(f, f, self.pointer_local);
664 }
665 }
666 ui::Event::Pinch(delta) => {
667 let f = f64::from((1.0 - delta).clamp(0.7, 1.4));
668 self.zoom_at(f, f, self.pointer_local);
669 }
670 ui::Event::KeyInput { physical, key, state: ElementState::Pressed, repeat: false, modifiers: kmod, .. } => {
671 self.handle_key(comp, history, current_frame, Some(physical), &key, kmod, &mut actions);
672 }
673 ui::Event::Key { key, modifiers: kmod, .. } => {
674 self.handle_key(comp, history, current_frame, None, &key, kmod, &mut actions);
675 }
676 _ => {}
677 }
678
679 actions
680 }
681
682 fn zoom_at(&mut self, ft: f64, fv: f64, pivot_local: [f32; 2]) {
683 let p = self.local_to_data(pivot_local);
684 let t0 = p[0] - (p[0] - self.t_range[0]) * ft;
685 let t1 = p[0] + (self.t_range[1] - p[0]) * ft;
686 let v0 = p[1] - (p[1] - self.v_range[0]) * fv;
687 let v1 = p[1] + (self.v_range[1] - p[1]) * fv;
688 if (t1 - t0) > 2.0 && (t1 - t0) < 100_000.0 {
689 self.t_range = [t0, t1];
690 }
691 if (v1 - v0) > 1e-4 && (v1 - v0) < 1_000_000.0 {
692 self.v_range = [v0, v1];
693 }
694 }
695}
696
697fn point_in_rect(p: [f32; 2], r: [f32; 4]) -> bool {
698 p[0] >= r[0] && p[0] <= r[2] && p[1] >= r[1] && p[1] <= r[3]
699}