| author | |
| committer | |
| log | 0bf5e7056f9c85be4041eded45c23438e71c592a |
| tree | 96f4c72389b67867299c824c36df987eb22d1557 |
| parent | 32cbfb85162699b92240dff7e0f5253f861c4ea3 |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
5 files changed, 1922 insertions(+), 1755 deletions(-)
crates/app/src/inspector/rows.rs created+591| ... | ... | @@ -0,0 +1,591 @@ |
| 1 | //! Parameter row renderers for [`InspectorState`]: float, vec2, bool, enum, int-enum, | |
| 2 | //! combo, RGBA color group, and text rows. | |
| 3 | ||
| 4 | use super::super::{InspectorAction, InspectorState}; | |
| 5 | use super::{ | |
| 6 | KeyframeState, combo_cycle_index, enum_option_index, int_enum_lookup, keyframe_state, | |
| 7 | }; | |
| 8 | use model::{Comp, CompHistory, NodeId, ParamValue}; | |
| 9 | ||
| 10 | impl InspectorState { | |
| 11 | #[allow(clippy::too_many_arguments)] | |
| 12 | pub(super) fn render_float_row( | |
| 13 | &mut self, | |
| 14 | ui: &mut ui::Ui, | |
| 15 | pane_id: ui::Id, | |
| 16 | comp: &mut Comp, | |
| 17 | history: &mut CompHistory, | |
| 18 | node_id: NodeId, | |
| 19 | input_name: &str, | |
| 20 | label: &str, | |
| 21 | min: f64, | |
| 22 | max: f64, | |
| 23 | default: f64, | |
| 24 | current_frame: f64, | |
| 25 | actions: &mut Vec<InspectorAction>, | |
| 26 | ) { | |
| 27 | let cur_val = comp.eval_number(comp.node(node_id), input_name, current_frame, default, &self.expr); | |
| 28 | let has_expr = comp | |
| 29 | .node(node_id) | |
| 30 | .inputs | |
| 31 | .get(input_name) | |
| 32 | .and_then(|b| b.expression.as_ref()) | |
| 33 | .is_some(); | |
| 34 | let kf_state = keyframe_state(comp, node_id, input_name, current_frame); | |
| 35 | let p = self.palette; | |
| 36 | ||
| 37 | ui.open( | |
| 38 | ("row-f", node_id.0, input_name), | |
| 39 | ui::Spec { | |
| 40 | size: [ui::fill(), ui::px(22.0)], | |
| 41 | axis: ui::Axis::X, | |
| 42 | gap: 4.0, | |
| 43 | ..ui::Spec::default() | |
| 44 | }, | |
| 45 | ); | |
| 46 | ||
| 47 | // Label | |
| 48 | ui.leaf( | |
| 49 | "lbl", | |
| 50 | ui::Spec { | |
| 51 | size: [ui::px(86.0), ui::fill()], | |
| 52 | text: Some(label), | |
| 53 | font_size: Some(10.5), | |
| 54 | overflow: ui::Overflow::Ellipsis, | |
| 55 | color: Some(p.text_dim), | |
| 56 | ..ui::Spec::default() | |
| 57 | }, | |
| 58 | ); | |
| 59 | ||
| 60 | // Expression `=` badge if present | |
| 61 | if has_expr { | |
| 62 | render_expr_badge(ui); | |
| 63 | } | |
| 64 | ||
| 65 | // Scrub slider | |
| 66 | let slider_id = pane_id.child(("sld", node_id.0, input_name)); | |
| 67 | let drag_key = format!("{}:{input_name}", node_id.0); | |
| 68 | if let Some(new_val) = self.scrub_slider( | |
| 69 | ui, | |
| 70 | slider_id, | |
| 71 | &drag_key, | |
| 72 | node_id, | |
| 73 | cur_val, | |
| 74 | min, | |
| 75 | max, | |
| 76 | default, | |
| 77 | history, | |
| 78 | comp, | |
| 79 | input_name, | |
| 80 | ) { | |
| 81 | if kf_state != KeyframeState::Static { | |
| 82 | let _ = comp.set_keyframe(node_id, input_name, current_frame, new_val); | |
| 83 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 84 | } else { | |
| 85 | comp.set_input_value(node_id, input_name, ParamValue::Number(new_val)); | |
| 86 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | // Reset button (small dot/arrow when non-default) | |
| 91 | let is_default = (cur_val - default).abs() < 1e-4; | |
| 92 | if !is_default { | |
| 93 | let rst = ui.leaf( | |
| 94 | ("rst", node_id.0, input_name), | |
| 95 | ui::Spec { | |
| 96 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 97 | flags: ui::Flags::CLICKABLE, | |
| 98 | size: [ui::px(18.0), ui::px(20.0)], | |
| 99 | text: Some("↺"), | |
| 100 | font_size: Some(10.5), | |
| 101 | color: Some(p.text_dim), | |
| 102 | hover_fill: Some(p.panel_bg), | |
| 103 | radius: 3.0, | |
| 104 | center: true, | |
| 105 | ..ui::Spec::default() | |
| 106 | }, | |
| 107 | ); | |
| 108 | if rst.clicked { | |
| 109 | history.push_snapshot(comp, format!("Reset {label}")); | |
| 110 | if kf_state != KeyframeState::Static { | |
| 111 | let _ = comp.set_keyframe(node_id, input_name, current_frame, default); | |
| 112 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 113 | } else { | |
| 114 | comp.set_input_value(node_id, input_name, ParamValue::Number(default)); | |
| 115 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 116 | } | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | // Keyframe diamond button (`◆`) | |
| 121 | if self.keyframe_button(ui, ("kf", node_id.0, input_name), kf_state) { | |
| 122 | self.toggle_keyframe(comp, history, node_id, input_name, current_frame, cur_val, kf_state, actions); | |
| 123 | } | |
| 124 | ||
| 125 | ui.close(); | |
| 126 | } | |
| 127 | ||
| 128 | #[allow(clippy::too_many_arguments)] | |
| 129 | pub(super) fn render_vec2_row( | |
| 130 | &mut self, | |
| 131 | ui: &mut ui::Ui, | |
| 132 | pane_id: ui::Id, | |
| 133 | comp: &mut Comp, | |
| 134 | history: &mut CompHistory, | |
| 135 | node_id: NodeId, | |
| 136 | input_name: &str, | |
| 137 | label: &str, | |
| 138 | min: f64, | |
| 139 | max: f64, | |
| 140 | default: [f64; 2], | |
| 141 | current_frame: f64, | |
| 142 | actions: &mut Vec<InspectorAction>, | |
| 143 | ) { | |
| 144 | let cur = comp.eval_point(comp.node(node_id), input_name, current_frame, default, &self.expr); | |
| 145 | let has_expr = comp | |
| 146 | .node(node_id) | |
| 147 | .inputs | |
| 148 | .get(input_name) | |
| 149 | .and_then(|b| b.expression.as_ref()) | |
| 150 | .is_some(); | |
| 151 | ||
| 152 | ui.open( | |
| 153 | ("row-v2", node_id.0, input_name), | |
| 154 | ui::Spec { | |
| 155 | size: [ui::fill(), ui::px(22.0)], | |
| 156 | axis: ui::Axis::X, | |
| 157 | gap: 4.0, | |
| 158 | ..ui::Spec::default() | |
| 159 | }, | |
| 160 | ); | |
| 161 | ui.leaf( | |
| 162 | "lbl", | |
| 163 | ui::Spec { | |
| 164 | size: [ui::px(86.0), ui::fill()], | |
| 165 | text: Some(label), | |
| 166 | font_size: Some(10.5), | |
| 167 | overflow: ui::Overflow::Ellipsis, | |
| 168 | color: Some(self.palette.text_dim), | |
| 169 | ..ui::Spec::default() | |
| 170 | }, | |
| 171 | ); | |
| 172 | if has_expr { | |
| 173 | render_expr_badge(ui); | |
| 174 | } | |
| 175 | ||
| 176 | let x_id = pane_id.child(("v2x", node_id.0, input_name)); | |
| 177 | let x_key = format!("{}:{input_name}:x", node_id.0); | |
| 178 | if let Some(nx) = self.scrub_slider( | |
| 179 | ui, | |
| 180 | x_id, | |
| 181 | &x_key, | |
| 182 | node_id, | |
| 183 | cur[0], | |
| 184 | min, | |
| 185 | max, | |
| 186 | default[0], | |
| 187 | history, | |
| 188 | comp, | |
| 189 | input_name, | |
| 190 | ) { | |
| 191 | comp.set_input_value(node_id, input_name, ParamValue::Point([nx, cur[1]])); | |
| 192 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 193 | } | |
| 194 | ||
| 195 | let y_id = pane_id.child(("v2y", node_id.0, input_name)); | |
| 196 | let y_key = format!("{}:{input_name}:y", node_id.0); | |
| 197 | if let Some(ny) = self.scrub_slider( | |
| 198 | ui, | |
| 199 | y_id, | |
| 200 | &y_key, | |
| 201 | node_id, | |
| 202 | cur[1], | |
| 203 | min, | |
| 204 | max, | |
| 205 | default[1], | |
| 206 | history, | |
| 207 | comp, | |
| 208 | input_name, | |
| 209 | ) { | |
| 210 | comp.set_input_value(node_id, input_name, ParamValue::Point([cur[0], ny])); | |
| 211 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 212 | } | |
| 213 | ||
| 214 | ui.close(); | |
| 215 | } | |
| 216 | ||
| 217 | #[allow(clippy::too_many_arguments)] | |
| 218 | pub(super) fn render_bool_row( | |
| 219 | &mut self, | |
| 220 | ui: &mut ui::Ui, | |
| 221 | comp: &mut Comp, | |
| 222 | history: &mut CompHistory, | |
| 223 | node_id: NodeId, | |
| 224 | input_name: &str, | |
| 225 | label: &str, | |
| 226 | default: bool, | |
| 227 | actions: &mut Vec<InspectorAction>, | |
| 228 | ) { | |
| 229 | let checked = comp | |
| 230 | .node(node_id) | |
| 231 | .static_number(input_name) | |
| 232 | .map(|v| v >= 0.5) | |
| 233 | .unwrap_or(default); | |
| 234 | let p = self.palette; | |
| 235 | ||
| 236 | ui.open( | |
| 237 | ("row-b", node_id.0, input_name), | |
| 238 | ui::Spec { | |
| 239 | size: [ui::fill(), ui::px(22.0)], | |
| 240 | axis: ui::Axis::X, | |
| 241 | gap: 6.0, | |
| 242 | ..ui::Spec::default() | |
| 243 | }, | |
| 244 | ); | |
| 245 | ui.leaf( | |
| 246 | "lbl", | |
| 247 | ui::Spec { | |
| 248 | size: [ui::px(86.0), ui::fill()], | |
| 249 | text: Some(label), | |
| 250 | font_size: Some(10.5), | |
| 251 | overflow: ui::Overflow::Ellipsis, | |
| 252 | color: Some(p.text_dim), | |
| 253 | ..ui::Spec::default() | |
| 254 | }, | |
| 255 | ); | |
| 256 | let fill = if checked { | |
| 257 | [p.accent[0], p.accent[1], p.accent[2], 0.28] | |
| 258 | } else { | |
| 259 | p.panel_bg | |
| 260 | }; | |
| 261 | let border = if checked { p.accent } else { p.rule }; | |
| 262 | let sig = ui.leaf( | |
| 263 | ("chk", node_id.0, input_name), | |
| 264 | ui::Spec { | |
| 265 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 266 | flags: ui::Flags::CLICKABLE, | |
| 267 | size: [ui::px(54.0), ui::px(20.0)], | |
| 268 | text: Some(if checked { "✓ On" } else { "Off" }), | |
| 269 | font_size: Some(10.5), | |
| 270 | medium: checked, | |
| 271 | color: Some(if checked { p.text } else { p.text_dim }), | |
| 272 | fill: Some(fill), | |
| 273 | border: Some(border), | |
| 274 | radius: 4.0, | |
| 275 | center: true, | |
| 276 | ..ui::Spec::default() | |
| 277 | }, | |
| 278 | ); | |
| 279 | if sig.clicked { | |
| 280 | history.push_snapshot(comp, format!("Toggle {label}")); | |
| 281 | let next = if checked { 0.0 } else { 1.0 }; | |
| 282 | comp.set_input_value(node_id, input_name, ParamValue::Number(next)); | |
| 283 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 284 | } | |
| 285 | ui.close(); | |
| 286 | } | |
| 287 | ||
| 288 | #[allow(clippy::too_many_arguments)] | |
| 289 | pub(super) fn render_enum_row( | |
| 290 | &mut self, | |
| 291 | ui: &mut ui::Ui, | |
| 292 | comp: &mut Comp, | |
| 293 | history: &mut CompHistory, | |
| 294 | node_id: NodeId, | |
| 295 | input_name: &str, | |
| 296 | label: &str, | |
| 297 | options: &'static [&'static str], | |
| 298 | default: &'static str, | |
| 299 | actions: &mut Vec<InspectorAction>, | |
| 300 | ) { | |
| 301 | let cur = comp | |
| 302 | .node(node_id) | |
| 303 | .static_str(input_name) | |
| 304 | .unwrap_or(default) | |
| 305 | .to_owned(); | |
| 306 | let idx = enum_option_index(options, &cur); | |
| 307 | if let Some(next_idx) = self.render_combo_row(ui, node_id, input_name, label, &cur, idx, options.len()) { | |
| 308 | history.push_snapshot(comp, format!("Set {label}")); | |
| 309 | comp.set_input_value(node_id, input_name, ParamValue::FuId(options[next_idx].to_owned())); | |
| 310 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 311 | } | |
| 312 | } | |
| 313 | ||
| 314 | #[allow(clippy::too_many_arguments)] | |
| 315 | pub(super) fn render_int_enum_row( | |
| 316 | &mut self, | |
| 317 | ui: &mut ui::Ui, | |
| 318 | comp: &mut Comp, | |
| 319 | history: &mut CompHistory, | |
| 320 | node_id: NodeId, | |
| 321 | input_name: &str, | |
| 322 | label: &str, | |
| 323 | options: &'static [(i32, &'static str)], | |
| 324 | default: i32, | |
| 325 | actions: &mut Vec<InspectorAction>, | |
| 326 | ) { | |
| 327 | let cur_val = comp | |
| 328 | .node(node_id) | |
| 329 | .static_number(input_name) | |
| 330 | .map(|v| v.round() as i32) | |
| 331 | .unwrap_or(default); | |
| 332 | let (idx, cur_label) = int_enum_lookup(options, cur_val); | |
| 333 | if let Some(next_idx) = self.render_combo_row(ui, node_id, input_name, label, &cur_label, idx, options.len()) { | |
| 334 | history.push_snapshot(comp, format!("Set {label}")); | |
| 335 | comp.set_input_value(node_id, input_name, ParamValue::Number(f64::from(options[next_idx].0))); | |
| 336 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 337 | } | |
| 338 | } | |
| 339 | ||
| 340 | #[allow(clippy::too_many_arguments)] | |
| 341 | fn render_combo_row( | |
| 342 | &self, | |
| 343 | ui: &mut ui::Ui, | |
| 344 | node_id: NodeId, | |
| 345 | input_name: &str, | |
| 346 | label: &str, | |
| 347 | cur_label: &str, | |
| 348 | cur_idx: usize, | |
| 349 | option_count: usize, | |
| 350 | ) -> Option<usize> { | |
| 351 | let p = self.palette; | |
| 352 | ||
| 353 | ui.open( | |
| 354 | ("row-e", node_id.0, input_name), | |
| 355 | ui::Spec { | |
| 356 | size: [ui::fill(), ui::px(22.0)], | |
| 357 | axis: ui::Axis::X, | |
| 358 | gap: 6.0, | |
| 359 | ..ui::Spec::default() | |
| 360 | }, | |
| 361 | ); | |
| 362 | ui.leaf( | |
| 363 | "lbl", | |
| 364 | ui::Spec { | |
| 365 | size: [ui::px(86.0), ui::fill()], | |
| 366 | text: Some(label), | |
| 367 | font_size: Some(10.5), | |
| 368 | overflow: ui::Overflow::Ellipsis, | |
| 369 | color: Some(p.text_dim), | |
| 370 | ..ui::Spec::default() | |
| 371 | }, | |
| 372 | ); | |
| 373 | let btn_lbl = format!("{cur_label} ▾"); | |
| 374 | let sig = ui.leaf( | |
| 375 | ("enum", node_id.0, input_name), | |
| 376 | ui::Spec { | |
| 377 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 378 | flags: ui::Flags::CLICKABLE, | |
| 379 | size: [ui::fill(), ui::px(20.0)], | |
| 380 | text: Some(&btn_lbl), | |
| 381 | font_size: Some(10.5), | |
| 382 | color: Some(p.text), | |
| 383 | fill: Some(p.panel_bg), | |
| 384 | border: Some(p.rule), | |
| 385 | hover_border: Some(p.accent), | |
| 386 | radius: 4.0, | |
| 387 | pad: [6.0, 0.0], | |
| 388 | ..ui::Spec::default() | |
| 389 | }, | |
| 390 | ); | |
| 391 | let next = if sig.clicked { | |
| 392 | combo_cycle_index(cur_idx, option_count, ui.modifiers().shift_key()) | |
| 393 | } else { | |
| 394 | None | |
| 395 | }; | |
| 396 | ui.close(); | |
| 397 | next | |
| 398 | } | |
| 399 | ||
| 400 | #[allow(clippy::too_many_arguments)] | |
| 401 | pub(super) fn render_color_group( | |
| 402 | &mut self, | |
| 403 | ui: &mut ui::Ui, | |
| 404 | pane_id: ui::Id, | |
| 405 | comp: &mut Comp, | |
| 406 | history: &mut CompHistory, | |
| 407 | node_id: NodeId, | |
| 408 | label: &str, | |
| 409 | r_key: &'static str, | |
| 410 | g_key: &'static str, | |
| 411 | b_key: &'static str, | |
| 412 | a_key: Option<&'static str>, | |
| 413 | default: [f64; 4], | |
| 414 | current_frame: f64, | |
| 415 | actions: &mut Vec<InspectorAction>, | |
| 416 | ) { | |
| 417 | let rv = comp.eval_number(comp.node(node_id), r_key, current_frame, default[0], &self.expr); | |
| 418 | let gv = comp.eval_number(comp.node(node_id), g_key, current_frame, default[1], &self.expr); | |
| 419 | let bv = comp.eval_number(comp.node(node_id), b_key, current_frame, default[2], &self.expr); | |
| 420 | let _av = a_key | |
| 421 | .map(|ak| comp.eval_number(comp.node(node_id), ak, current_frame, default[3], &self.expr)) | |
| 422 | .unwrap_or(default[3]); | |
| 423 | let swatch = [ | |
| 424 | rv.clamp(0.0, 1.0) as f32, | |
| 425 | gv.clamp(0.0, 1.0) as f32, | |
| 426 | bv.clamp(0.0, 1.0) as f32, | |
| 427 | 1.0, | |
| 428 | ]; | |
| 429 | let p = self.palette; | |
| 430 | ||
| 431 | ui.open( | |
| 432 | ("grp-col", node_id.0, label), | |
| 433 | ui::Spec { | |
| 434 | size: [ui::fill(), ui::children()], | |
| 435 | axis: ui::Axis::Y, | |
| 436 | gap: 4.0, | |
| 437 | pad: [6.0, 5.0], | |
| 438 | fill: Some(p.panel_bg), | |
| 439 | border: Some(p.rule), | |
| 440 | radius: 4.0, | |
| 441 | ..ui::Spec::default() | |
| 442 | }, | |
| 443 | ); | |
| 444 | // Swatch header | |
| 445 | ui.open( | |
| 446 | "col-hdr", | |
| 447 | ui::Spec { | |
| 448 | size: [ui::fill(), ui::px(18.0)], | |
| 449 | axis: ui::Axis::X, | |
| 450 | gap: 6.0, | |
| 451 | ..ui::Spec::default() | |
| 452 | }, | |
| 453 | ); | |
| 454 | ui.leaf( | |
| 455 | "col-lbl", | |
| 456 | ui::Spec { | |
| 457 | size: [ui::px(80.0), ui::fill()], | |
| 458 | text: Some(label), | |
| 459 | font_size: Some(10.5), | |
| 460 | medium: true, | |
| 461 | color: Some(p.text), | |
| 462 | ..ui::Spec::default() | |
| 463 | }, | |
| 464 | ); | |
| 465 | ui.leaf( | |
| 466 | "col-swatch", | |
| 467 | ui::Spec { | |
| 468 | size: [ui::fill(), ui::px(16.0)], | |
| 469 | fill: Some(swatch), | |
| 470 | border: Some(p.rule), | |
| 471 | radius: 3.0, | |
| 472 | ..ui::Spec::default() | |
| 473 | }, | |
| 474 | ); | |
| 475 | ui.close(); | |
| 476 | ||
| 477 | self.render_float_row(ui, pane_id, comp, history, node_id, r_key, "Red", 0.0, 1.0, default[0], current_frame, actions); | |
| 478 | self.render_float_row(ui, pane_id, comp, history, node_id, g_key, "Green", 0.0, 1.0, default[1], current_frame, actions); | |
| 479 | self.render_float_row(ui, pane_id, comp, history, node_id, b_key, "Blue", 0.0, 1.0, default[2], current_frame, actions); | |
| 480 | if let Some(ak) = a_key { | |
| 481 | self.render_float_row(ui, pane_id, comp, history, node_id, ak, "Alpha", 0.0, 1.0, default[3], current_frame, actions); | |
| 482 | } | |
| 483 | ui.close(); | |
| 484 | } | |
| 485 | ||
| 486 | #[allow(clippy::too_many_arguments)] | |
| 487 | pub(super) fn render_text_row( | |
| 488 | &mut self, | |
| 489 | ui: &mut ui::Ui, | |
| 490 | pane_id: ui::Id, | |
| 491 | comp: &mut Comp, | |
| 492 | history: &mut CompHistory, | |
| 493 | node_id: NodeId, | |
| 494 | input_name: &str, | |
| 495 | label: &str, | |
| 496 | default: &str, | |
| 497 | actions: &mut Vec<InspectorAction>, | |
| 498 | ) { | |
| 499 | let field_id = pane_id.child(("txt", node_id.0, input_name)); | |
| 500 | if self.text_focus == Some(field_id) && ui.focused() != Some(field_id) { | |
| 501 | self.text_focus = None; | |
| 502 | history.break_coalescing(); | |
| 503 | } | |
| 504 | let key = (node_id.0, input_name.to_owned()); | |
| 505 | let model_val = comp | |
| 506 | .node(node_id) | |
| 507 | .static_str(input_name) | |
| 508 | .unwrap_or(default) | |
| 509 | .to_owned(); | |
| 510 | let p = self.palette; | |
| 511 | let buf = self.text_bufs.entry(key).or_insert_with(|| model_val.clone()); | |
| 512 | if ui.focused() != Some(field_id) && *buf != model_val { | |
| 513 | *buf = model_val.clone(); | |
| 514 | } | |
| 515 | let prev = buf.clone(); | |
| 516 | ||
| 517 | ui.open( | |
| 518 | ("row-t", node_id.0, input_name), | |
| 519 | ui::Spec { | |
| 520 | size: [ui::fill(), ui::px(24.0)], | |
| 521 | axis: ui::Axis::X, | |
| 522 | gap: 6.0, | |
| 523 | ..ui::Spec::default() | |
| 524 | }, | |
| 525 | ); | |
| 526 | ui.leaf( | |
| 527 | "lbl", | |
| 528 | ui::Spec { | |
| 529 | size: [ui::px(86.0), ui::fill()], | |
| 530 | text: Some(label), | |
| 531 | font_size: Some(10.5), | |
| 532 | overflow: ui::Overflow::Ellipsis, | |
| 533 | color: Some(p.text_dim), | |
| 534 | ..ui::Spec::default() | |
| 535 | }, | |
| 536 | ); | |
| 537 | let sig = ui::text_field( | |
| 538 | ui, | |
| 539 | field_id, | |
| 540 | buf, | |
| 541 | label, | |
| 542 | ui::Spec { | |
| 543 | size: [ui::fill(), ui::px(22.0)], | |
| 544 | font_size: Some(11.0), | |
| 545 | fill: Some(p.panel_bg), | |
| 546 | border: Some(p.rule), | |
| 547 | hover_border: Some(p.accent), | |
| 548 | radius: 4.0, | |
| 549 | pad: [6.0, 2.0], | |
| 550 | ..ui::Spec::default() | |
| 551 | }, | |
| 552 | ); | |
| 553 | if ui.focused() == Some(field_id) { | |
| 554 | self.text_focus = Some(field_id); | |
| 555 | } | |
| 556 | let committed = sig.events.iter().any(|ev| { | |
| 557 | matches!(ev, ui::Event::Key { key: winit::keyboard::Key::Named(winit::keyboard::NamedKey::Enter), .. }) | |
| 558 | }); | |
| 559 | if *buf != prev { | |
| 560 | history.push_coalesced( | |
| 561 | comp, | |
| 562 | format!("Edit {label}"), | |
| 563 | format!("text:{}:{input_name}", node_id.0), | |
| 564 | ); | |
| 565 | comp.set_input_value(node_id, input_name, ParamValue::Text(buf.clone())); | |
| 566 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 567 | } | |
| 568 | if committed { | |
| 569 | history.break_coalescing(); | |
| 570 | } | |
| 571 | ui.close(); | |
| 572 | } | |
| 573 | } | |
| 574 | ||
| 575 | fn render_expr_badge(ui: &mut ui::Ui) { | |
| 576 | ui.leaf( | |
| 577 | "expr-badge", | |
| 578 | ui::Spec { | |
| 579 | size: [ui::px(16.0), ui::px(18.0)], | |
| 580 | text: Some("="), | |
| 581 | font_size: Some(10.5), | |
| 582 | bold: true, | |
| 583 | color: Some([0.36, 0.88, 0.92, 1.0]), | |
| 584 | fill: Some([0.10, 0.24, 0.28, 0.85]), | |
| 585 | border: Some([0.32, 0.78, 0.86, 0.65]), | |
| 586 | radius: 3.0, | |
| 587 | center: true, | |
| 588 | ..ui::Spec::default() | |
| 589 | }, | |
| 590 | ); | |
| 591 | } |
crates/app/src/inspector/widgets.rs+137-596| ... | ... | @@ -1,10 +1,14 @@ |
| 1 | //! Reusable Inspector UI control builders: scrub sliders, Vec2, RGBA color groups, | |
| 2 | //! booleans, FuID enum cyclers, text fields, expression badges, and keyframe diamonds (`◆`). | |
| 1 | //! Reusable Inspector UI control builders: scrub sliders, keyframe diamonds (`◆`), | |
| 2 | //! value formatting/scrubbing math, and parameter row dispatch. | |
| 3 | ||
| 4 | #[path = "rows.rs"] | |
| 5 | mod rows; | |
| 3 | 6 | |
| 4 | 7 | use super::schemas::{ParamKind, ParamSpec, infer_float_range}; |
| 5 | 8 | use super::{InspectorAction, InspectorState}; |
| 6 | 9 | use engine::EvalStats; |
| 7 | 10 | use model::{Comp, CompHistory, NodeId, ParamValue}; |
| 11 | use std::borrow::Cow; | |
| 8 | 12 | use std::time::{Duration, Instant}; |
| 9 | 13 | |
| 10 | 14 | #[derive(Clone, Debug)] |
| ... | ... | @@ -210,582 +214,7 @@ impl InspectorState { |
| 210 | 214 | } |
| 211 | 215 | |
| 212 | 216 | #[allow(clippy::too_many_arguments)] |
| 213 | fn render_float_row( | |
| 214 | &mut self, | |
| 215 | ui: &mut ui::Ui, | |
| 216 | pane_id: ui::Id, | |
| 217 | comp: &mut Comp, | |
| 218 | history: &mut CompHistory, | |
| 219 | node_id: NodeId, | |
| 220 | input_name: &str, | |
| 221 | label: &str, | |
| 222 | min: f64, | |
| 223 | max: f64, | |
| 224 | default: f64, | |
| 225 | current_frame: f64, | |
| 226 | actions: &mut Vec<InspectorAction>, | |
| 227 | ) { | |
| 228 | let cur_val = comp.eval_number(comp.node(node_id), input_name, current_frame, default, &self.expr); | |
| 229 | let has_expr = comp | |
| 230 | .node(node_id) | |
| 231 | .inputs | |
| 232 | .get(input_name) | |
| 233 | .and_then(|b| b.expression.as_ref()) | |
| 234 | .is_some(); | |
| 235 | let kf_state = keyframe_state(comp, node_id, input_name, current_frame); | |
| 236 | let p = self.palette; | |
| 237 | ||
| 238 | ui.open( | |
| 239 | ("row-f", node_id.0, input_name), | |
| 240 | ui::Spec { | |
| 241 | size: [ui::fill(), ui::px(22.0)], | |
| 242 | axis: ui::Axis::X, | |
| 243 | gap: 4.0, | |
| 244 | ..ui::Spec::default() | |
| 245 | }, | |
| 246 | ); | |
| 247 | ||
| 248 | // Label | |
| 249 | ui.leaf( | |
| 250 | "lbl", | |
| 251 | ui::Spec { | |
| 252 | size: [ui::px(86.0), ui::fill()], | |
| 253 | text: Some(label), | |
| 254 | font_size: Some(10.5), | |
| 255 | overflow: ui::Overflow::Ellipsis, | |
| 256 | color: Some(p.text_dim), | |
| 257 | ..ui::Spec::default() | |
| 258 | }, | |
| 259 | ); | |
| 260 | ||
| 261 | // Expression `=` badge if present | |
| 262 | if has_expr { | |
| 263 | render_expr_badge(ui); | |
| 264 | } | |
| 265 | ||
| 266 | // Scrub slider | |
| 267 | let slider_id = pane_id.child(("sld", node_id.0, input_name)); | |
| 268 | let drag_key = format!("{}:{input_name}", node_id.0); | |
| 269 | if let Some(new_val) = self.scrub_slider( | |
| 270 | ui, | |
| 271 | slider_id, | |
| 272 | &drag_key, | |
| 273 | node_id, | |
| 274 | cur_val, | |
| 275 | min, | |
| 276 | max, | |
| 277 | default, | |
| 278 | history, | |
| 279 | comp, | |
| 280 | input_name, | |
| 281 | ) { | |
| 282 | if kf_state != KeyframeState::Static { | |
| 283 | let _ = comp.set_keyframe(node_id, input_name, current_frame, new_val); | |
| 284 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 285 | } else { | |
| 286 | comp.set_input_value(node_id, input_name, ParamValue::Number(new_val)); | |
| 287 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 288 | } | |
| 289 | } | |
| 290 | ||
| 291 | // Reset button (small dot/arrow when non-default) | |
| 292 | let is_default = (cur_val - default).abs() < 1e-4; | |
| 293 | if !is_default { | |
| 294 | let rst = ui.leaf( | |
| 295 | ("rst", node_id.0, input_name), | |
| 296 | ui::Spec { | |
| 297 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 298 | flags: ui::Flags::CLICKABLE, | |
| 299 | size: [ui::px(18.0), ui::px(20.0)], | |
| 300 | text: Some("↺"), | |
| 301 | font_size: Some(10.5), | |
| 302 | color: Some(p.text_dim), | |
| 303 | hover_fill: Some(p.panel_bg), | |
| 304 | radius: 3.0, | |
| 305 | center: true, | |
| 306 | ..ui::Spec::default() | |
| 307 | }, | |
| 308 | ); | |
| 309 | if rst.clicked { | |
| 310 | history.push_snapshot(comp, format!("Reset {label}")); | |
| 311 | if kf_state != KeyframeState::Static { | |
| 312 | let _ = comp.set_keyframe(node_id, input_name, current_frame, default); | |
| 313 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 314 | } else { | |
| 315 | comp.set_input_value(node_id, input_name, ParamValue::Number(default)); | |
| 316 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 317 | } | |
| 318 | } | |
| 319 | } | |
| 320 | ||
| 321 | // Keyframe diamond button (`◆`) | |
| 322 | if self.keyframe_button(ui, ("kf", node_id.0, input_name), kf_state) { | |
| 323 | self.toggle_keyframe(comp, history, node_id, input_name, current_frame, cur_val, kf_state, actions); | |
| 324 | } | |
| 325 | ||
| 326 | ui.close(); | |
| 327 | } | |
| 328 | ||
| 329 | #[allow(clippy::too_many_arguments)] | |
| 330 | fn render_vec2_row( | |
| 331 | &mut self, | |
| 332 | ui: &mut ui::Ui, | |
| 333 | pane_id: ui::Id, | |
| 334 | comp: &mut Comp, | |
| 335 | history: &mut CompHistory, | |
| 336 | node_id: NodeId, | |
| 337 | input_name: &str, | |
| 338 | label: &str, | |
| 339 | min: f64, | |
| 340 | max: f64, | |
| 341 | default: [f64; 2], | |
| 342 | current_frame: f64, | |
| 343 | actions: &mut Vec<InspectorAction>, | |
| 344 | ) { | |
| 345 | let cur = comp.eval_point(comp.node(node_id), input_name, current_frame, default, &self.expr); | |
| 346 | let has_expr = comp | |
| 347 | .node(node_id) | |
| 348 | .inputs | |
| 349 | .get(input_name) | |
| 350 | .and_then(|b| b.expression.as_ref()) | |
| 351 | .is_some(); | |
| 352 | ||
| 353 | ui.open( | |
| 354 | ("row-v2", node_id.0, input_name), | |
| 355 | ui::Spec { | |
| 356 | size: [ui::fill(), ui::px(22.0)], | |
| 357 | axis: ui::Axis::X, | |
| 358 | gap: 4.0, | |
| 359 | ..ui::Spec::default() | |
| 360 | }, | |
| 361 | ); | |
| 362 | ui.leaf( | |
| 363 | "lbl", | |
| 364 | ui::Spec { | |
| 365 | size: [ui::px(86.0), ui::fill()], | |
| 366 | text: Some(label), | |
| 367 | font_size: Some(10.5), | |
| 368 | overflow: ui::Overflow::Ellipsis, | |
| 369 | color: Some(self.palette.text_dim), | |
| 370 | ..ui::Spec::default() | |
| 371 | }, | |
| 372 | ); | |
| 373 | if has_expr { | |
| 374 | render_expr_badge(ui); | |
| 375 | } | |
| 376 | ||
| 377 | let x_id = pane_id.child(("v2x", node_id.0, input_name)); | |
| 378 | let x_key = format!("{}:{input_name}:x", node_id.0); | |
| 379 | if let Some(nx) = self.scrub_slider( | |
| 380 | ui, | |
| 381 | x_id, | |
| 382 | &x_key, | |
| 383 | node_id, | |
| 384 | cur[0], | |
| 385 | min, | |
| 386 | max, | |
| 387 | default[0], | |
| 388 | history, | |
| 389 | comp, | |
| 390 | input_name, | |
| 391 | ) { | |
| 392 | comp.set_input_value(node_id, input_name, ParamValue::Point([nx, cur[1]])); | |
| 393 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 394 | } | |
| 395 | ||
| 396 | let y_id = pane_id.child(("v2y", node_id.0, input_name)); | |
| 397 | let y_key = format!("{}:{input_name}:y", node_id.0); | |
| 398 | if let Some(ny) = self.scrub_slider( | |
| 399 | ui, | |
| 400 | y_id, | |
| 401 | &y_key, | |
| 402 | node_id, | |
| 403 | cur[1], | |
| 404 | min, | |
| 405 | max, | |
| 406 | default[1], | |
| 407 | history, | |
| 408 | comp, | |
| 409 | input_name, | |
| 410 | ) { | |
| 411 | comp.set_input_value(node_id, input_name, ParamValue::Point([cur[0], ny])); | |
| 412 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 413 | } | |
| 414 | ||
| 415 | ui.close(); | |
| 416 | } | |
| 417 | ||
| 418 | #[allow(clippy::too_many_arguments)] | |
| 419 | fn render_bool_row( | |
| 420 | &mut self, | |
| 421 | ui: &mut ui::Ui, | |
| 422 | comp: &mut Comp, | |
| 423 | history: &mut CompHistory, | |
| 424 | node_id: NodeId, | |
| 425 | input_name: &str, | |
| 426 | label: &str, | |
| 427 | default: bool, | |
| 428 | actions: &mut Vec<InspectorAction>, | |
| 429 | ) { | |
| 430 | let checked = comp | |
| 431 | .node(node_id) | |
| 432 | .static_number(input_name) | |
| 433 | .map(|v| v >= 0.5) | |
| 434 | .unwrap_or(default); | |
| 435 | let p = self.palette; | |
| 436 | ||
| 437 | ui.open( | |
| 438 | ("row-b", node_id.0, input_name), | |
| 439 | ui::Spec { | |
| 440 | size: [ui::fill(), ui::px(22.0)], | |
| 441 | axis: ui::Axis::X, | |
| 442 | gap: 6.0, | |
| 443 | ..ui::Spec::default() | |
| 444 | }, | |
| 445 | ); | |
| 446 | ui.leaf( | |
| 447 | "lbl", | |
| 448 | ui::Spec { | |
| 449 | size: [ui::px(86.0), ui::fill()], | |
| 450 | text: Some(label), | |
| 451 | font_size: Some(10.5), | |
| 452 | overflow: ui::Overflow::Ellipsis, | |
| 453 | color: Some(p.text_dim), | |
| 454 | ..ui::Spec::default() | |
| 455 | }, | |
| 456 | ); | |
| 457 | let fill = if checked { | |
| 458 | [p.accent[0], p.accent[1], p.accent[2], 0.28] | |
| 459 | } else { | |
| 460 | p.panel_bg | |
| 461 | }; | |
| 462 | let border = if checked { p.accent } else { p.rule }; | |
| 463 | let sig = ui.leaf( | |
| 464 | ("chk", node_id.0, input_name), | |
| 465 | ui::Spec { | |
| 466 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 467 | flags: ui::Flags::CLICKABLE, | |
| 468 | size: [ui::px(54.0), ui::px(20.0)], | |
| 469 | text: Some(if checked { "✓ On" } else { "Off" }), | |
| 470 | font_size: Some(10.5), | |
| 471 | medium: checked, | |
| 472 | color: Some(if checked { p.text } else { p.text_dim }), | |
| 473 | fill: Some(fill), | |
| 474 | border: Some(border), | |
| 475 | radius: 4.0, | |
| 476 | center: true, | |
| 477 | ..ui::Spec::default() | |
| 478 | }, | |
| 479 | ); | |
| 480 | if sig.clicked { | |
| 481 | history.push_snapshot(comp, format!("Toggle {label}")); | |
| 482 | let next = if checked { 0.0 } else { 1.0 }; | |
| 483 | comp.set_input_value(node_id, input_name, ParamValue::Number(next)); | |
| 484 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 485 | } | |
| 486 | ui.close(); | |
| 487 | } | |
| 488 | ||
| 489 | #[allow(clippy::too_many_arguments)] | |
| 490 | fn render_enum_row( | |
| 491 | &mut self, | |
| 492 | ui: &mut ui::Ui, | |
| 493 | comp: &mut Comp, | |
| 494 | history: &mut CompHistory, | |
| 495 | node_id: NodeId, | |
| 496 | input_name: &str, | |
| 497 | label: &str, | |
| 498 | options: &'static [&'static str], | |
| 499 | default: &'static str, | |
| 500 | actions: &mut Vec<InspectorAction>, | |
| 501 | ) { | |
| 502 | let cur = comp | |
| 503 | .node(node_id) | |
| 504 | .static_str(input_name) | |
| 505 | .unwrap_or(default) | |
| 506 | .to_owned(); | |
| 507 | let idx = options.iter().position(|&o| o == cur).unwrap_or(0); | |
| 508 | if let Some(next_idx) = self.render_combo_row(ui, node_id, input_name, label, &cur, idx, options.len()) { | |
| 509 | history.push_snapshot(comp, format!("Set {label}")); | |
| 510 | comp.set_input_value(node_id, input_name, ParamValue::FuId(options[next_idx].to_owned())); | |
| 511 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 512 | } | |
| 513 | } | |
| 514 | ||
| 515 | #[allow(clippy::too_many_arguments)] | |
| 516 | fn render_int_enum_row( | |
| 517 | &mut self, | |
| 518 | ui: &mut ui::Ui, | |
| 519 | comp: &mut Comp, | |
| 520 | history: &mut CompHistory, | |
| 521 | node_id: NodeId, | |
| 522 | input_name: &str, | |
| 523 | label: &str, | |
| 524 | options: &'static [(i32, &'static str)], | |
| 525 | default: i32, | |
| 526 | actions: &mut Vec<InspectorAction>, | |
| 527 | ) { | |
| 528 | let cur_val = comp | |
| 529 | .node(node_id) | |
| 530 | .static_number(input_name) | |
| 531 | .map(|v| v.round() as i32) | |
| 532 | .unwrap_or(default); | |
| 533 | let idx = options.iter().position(|&(v, _)| v == cur_val).unwrap_or(0); | |
| 534 | let fallback_label; | |
| 535 | let cur_label = match options.iter().find(|&&(v, _)| v == cur_val) { | |
| 536 | Some(&(_, name)) => name, | |
| 537 | None => { | |
| 538 | fallback_label = format!("{cur_val}"); | |
| 539 | &fallback_label | |
| 540 | } | |
| 541 | }; | |
| 542 | if let Some(next_idx) = self.render_combo_row(ui, node_id, input_name, label, cur_label, idx, options.len()) { | |
| 543 | history.push_snapshot(comp, format!("Set {label}")); | |
| 544 | comp.set_input_value(node_id, input_name, ParamValue::Number(f64::from(options[next_idx].0))); | |
| 545 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 546 | } | |
| 547 | } | |
| 548 | ||
| 549 | #[allow(clippy::too_many_arguments)] | |
| 550 | fn render_combo_row( | |
| 551 | &self, | |
| 552 | ui: &mut ui::Ui, | |
| 553 | node_id: NodeId, | |
| 554 | input_name: &str, | |
| 555 | label: &str, | |
| 556 | cur_label: &str, | |
| 557 | cur_idx: usize, | |
| 558 | option_count: usize, | |
| 559 | ) -> Option<usize> { | |
| 560 | let p = self.palette; | |
| 561 | ||
| 562 | ui.open( | |
| 563 | ("row-e", node_id.0, input_name), | |
| 564 | ui::Spec { | |
| 565 | size: [ui::fill(), ui::px(22.0)], | |
| 566 | axis: ui::Axis::X, | |
| 567 | gap: 6.0, | |
| 568 | ..ui::Spec::default() | |
| 569 | }, | |
| 570 | ); | |
| 571 | ui.leaf( | |
| 572 | "lbl", | |
| 573 | ui::Spec { | |
| 574 | size: [ui::px(86.0), ui::fill()], | |
| 575 | text: Some(label), | |
| 576 | font_size: Some(10.5), | |
| 577 | overflow: ui::Overflow::Ellipsis, | |
| 578 | color: Some(p.text_dim), | |
| 579 | ..ui::Spec::default() | |
| 580 | }, | |
| 581 | ); | |
| 582 | let btn_lbl = format!("{cur_label} ▾"); | |
| 583 | let sig = ui.leaf( | |
| 584 | ("enum", node_id.0, input_name), | |
| 585 | ui::Spec { | |
| 586 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 587 | flags: ui::Flags::CLICKABLE, | |
| 588 | size: [ui::fill(), ui::px(20.0)], | |
| 589 | text: Some(&btn_lbl), | |
| 590 | font_size: Some(10.5), | |
| 591 | color: Some(p.text), | |
| 592 | fill: Some(p.panel_bg), | |
| 593 | border: Some(p.rule), | |
| 594 | hover_border: Some(p.accent), | |
| 595 | radius: 4.0, | |
| 596 | pad: [6.0, 0.0], | |
| 597 | ..ui::Spec::default() | |
| 598 | }, | |
| 599 | ); | |
| 600 | let next = if sig.clicked && option_count > 0 { | |
| 601 | Some(if ui.modifiers().shift_key() { | |
| 602 | (cur_idx + option_count - 1) % option_count | |
| 603 | } else { | |
| 604 | (cur_idx + 1) % option_count | |
| 605 | }) | |
| 606 | } else { | |
| 607 | None | |
| 608 | }; | |
| 609 | ui.close(); | |
| 610 | next | |
| 611 | } | |
| 612 | ||
| 613 | #[allow(clippy::too_many_arguments)] | |
| 614 | fn render_color_group( | |
| 615 | &mut self, | |
| 616 | ui: &mut ui::Ui, | |
| 617 | pane_id: ui::Id, | |
| 618 | comp: &mut Comp, | |
| 619 | history: &mut CompHistory, | |
| 620 | node_id: NodeId, | |
| 621 | label: &str, | |
| 622 | r_key: &'static str, | |
| 623 | g_key: &'static str, | |
| 624 | b_key: &'static str, | |
| 625 | a_key: Option<&'static str>, | |
| 626 | default: [f64; 4], | |
| 627 | current_frame: f64, | |
| 628 | actions: &mut Vec<InspectorAction>, | |
| 629 | ) { | |
| 630 | let rv = comp.eval_number(comp.node(node_id), r_key, current_frame, default[0], &self.expr); | |
| 631 | let gv = comp.eval_number(comp.node(node_id), g_key, current_frame, default[1], &self.expr); | |
| 632 | let bv = comp.eval_number(comp.node(node_id), b_key, current_frame, default[2], &self.expr); | |
| 633 | let _av = a_key | |
| 634 | .map(|ak| comp.eval_number(comp.node(node_id), ak, current_frame, default[3], &self.expr)) | |
| 635 | .unwrap_or(default[3]); | |
| 636 | let swatch = [ | |
| 637 | rv.clamp(0.0, 1.0) as f32, | |
| 638 | gv.clamp(0.0, 1.0) as f32, | |
| 639 | bv.clamp(0.0, 1.0) as f32, | |
| 640 | 1.0, | |
| 641 | ]; | |
| 642 | let p = self.palette; | |
| 643 | ||
| 644 | ui.open( | |
| 645 | ("grp-col", node_id.0, label), | |
| 646 | ui::Spec { | |
| 647 | size: [ui::fill(), ui::children()], | |
| 648 | axis: ui::Axis::Y, | |
| 649 | gap: 4.0, | |
| 650 | pad: [6.0, 5.0], | |
| 651 | fill: Some(p.panel_bg), | |
| 652 | border: Some(p.rule), | |
| 653 | radius: 4.0, | |
| 654 | ..ui::Spec::default() | |
| 655 | }, | |
| 656 | ); | |
| 657 | // Swatch header | |
| 658 | ui.open( | |
| 659 | "col-hdr", | |
| 660 | ui::Spec { | |
| 661 | size: [ui::fill(), ui::px(18.0)], | |
| 662 | axis: ui::Axis::X, | |
| 663 | gap: 6.0, | |
| 664 | ..ui::Spec::default() | |
| 665 | }, | |
| 666 | ); | |
| 667 | ui.leaf( | |
| 668 | "col-lbl", | |
| 669 | ui::Spec { | |
| 670 | size: [ui::px(80.0), ui::fill()], | |
| 671 | text: Some(label), | |
| 672 | font_size: Some(10.5), | |
| 673 | medium: true, | |
| 674 | color: Some(p.text), | |
| 675 | ..ui::Spec::default() | |
| 676 | }, | |
| 677 | ); | |
| 678 | ui.leaf( | |
| 679 | "col-swatch", | |
| 680 | ui::Spec { | |
| 681 | size: [ui::fill(), ui::px(16.0)], | |
| 682 | fill: Some(swatch), | |
| 683 | border: Some(p.rule), | |
| 684 | radius: 3.0, | |
| 685 | ..ui::Spec::default() | |
| 686 | }, | |
| 687 | ); | |
| 688 | ui.close(); | |
| 689 | ||
| 690 | self.render_float_row(ui, pane_id, comp, history, node_id, r_key, "Red", 0.0, 1.0, default[0], current_frame, actions); | |
| 691 | self.render_float_row(ui, pane_id, comp, history, node_id, g_key, "Green", 0.0, 1.0, default[1], current_frame, actions); | |
| 692 | self.render_float_row(ui, pane_id, comp, history, node_id, b_key, "Blue", 0.0, 1.0, default[2], current_frame, actions); | |
| 693 | if let Some(ak) = a_key { | |
| 694 | self.render_float_row(ui, pane_id, comp, history, node_id, ak, "Alpha", 0.0, 1.0, default[3], current_frame, actions); | |
| 695 | } | |
| 696 | ui.close(); | |
| 697 | } | |
| 698 | ||
| 699 | #[allow(clippy::too_many_arguments)] | |
| 700 | fn render_text_row( | |
| 701 | &mut self, | |
| 702 | ui: &mut ui::Ui, | |
| 703 | pane_id: ui::Id, | |
| 704 | comp: &mut Comp, | |
| 705 | history: &mut CompHistory, | |
| 706 | node_id: NodeId, | |
| 707 | input_name: &str, | |
| 708 | label: &str, | |
| 709 | default: &str, | |
| 710 | actions: &mut Vec<InspectorAction>, | |
| 711 | ) { | |
| 712 | let field_id = pane_id.child(("txt", node_id.0, input_name)); | |
| 713 | if self.text_focus == Some(field_id) && ui.focused() != Some(field_id) { | |
| 714 | self.text_focus = None; | |
| 715 | history.break_coalescing(); | |
| 716 | } | |
| 717 | let key = (node_id.0, input_name.to_owned()); | |
| 718 | let model_val = comp | |
| 719 | .node(node_id) | |
| 720 | .static_str(input_name) | |
| 721 | .unwrap_or(default) | |
| 722 | .to_owned(); | |
| 723 | let p = self.palette; | |
| 724 | let buf = self.text_bufs.entry(key).or_insert_with(|| model_val.clone()); | |
| 725 | if ui.focused() != Some(field_id) && *buf != model_val { | |
| 726 | *buf = model_val.clone(); | |
| 727 | } | |
| 728 | let prev = buf.clone(); | |
| 729 | ||
| 730 | ui.open( | |
| 731 | ("row-t", node_id.0, input_name), | |
| 732 | ui::Spec { | |
| 733 | size: [ui::fill(), ui::px(24.0)], | |
| 734 | axis: ui::Axis::X, | |
| 735 | gap: 6.0, | |
| 736 | ..ui::Spec::default() | |
| 737 | }, | |
| 738 | ); | |
| 739 | ui.leaf( | |
| 740 | "lbl", | |
| 741 | ui::Spec { | |
| 742 | size: [ui::px(86.0), ui::fill()], | |
| 743 | text: Some(label), | |
| 744 | font_size: Some(10.5), | |
| 745 | overflow: ui::Overflow::Ellipsis, | |
| 746 | color: Some(p.text_dim), | |
| 747 | ..ui::Spec::default() | |
| 748 | }, | |
| 749 | ); | |
| 750 | let sig = ui::text_field( | |
| 751 | ui, | |
| 752 | field_id, | |
| 753 | buf, | |
| 754 | label, | |
| 755 | ui::Spec { | |
| 756 | size: [ui::fill(), ui::px(22.0)], | |
| 757 | font_size: Some(11.0), | |
| 758 | fill: Some(p.panel_bg), | |
| 759 | border: Some(p.rule), | |
| 760 | hover_border: Some(p.accent), | |
| 761 | radius: 4.0, | |
| 762 | pad: [6.0, 2.0], | |
| 763 | ..ui::Spec::default() | |
| 764 | }, | |
| 765 | ); | |
| 766 | if ui.focused() == Some(field_id) { | |
| 767 | self.text_focus = Some(field_id); | |
| 768 | } | |
| 769 | let committed = sig.events.iter().any(|ev| { | |
| 770 | matches!(ev, ui::Event::Key { key: winit::keyboard::Key::Named(winit::keyboard::NamedKey::Enter), .. }) | |
| 771 | }); | |
| 772 | if *buf != prev { | |
| 773 | history.push_coalesced( | |
| 774 | comp, | |
| 775 | format!("Edit {label}"), | |
| 776 | format!("text:{}:{input_name}", node_id.0), | |
| 777 | ); | |
| 778 | comp.set_input_value(node_id, input_name, ParamValue::Text(buf.clone())); | |
| 779 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 780 | } | |
| 781 | if committed { | |
| 782 | history.break_coalescing(); | |
| 783 | } | |
| 784 | ui.close(); | |
| 785 | } | |
| 786 | ||
| 787 | #[allow(clippy::too_many_arguments)] | |
| 788 | fn scrub_slider( | |
| 217 | pub(super) fn scrub_slider( | |
| 789 | 218 | &mut self, |
| 790 | 219 | ui: &mut ui::Ui, |
| 791 | 220 | slider_id: ui::Id, |
| ... | ... | @@ -895,7 +324,7 @@ impl InspectorState { |
| 895 | 324 | updated |
| 896 | 325 | } |
| 897 | 326 | |
| 898 | fn keyframe_button(&self, ui: &mut ui::Ui, part: impl std::hash::Hash, state: KeyframeState) -> bool { | |
| 327 | pub(super) fn keyframe_button(&self, ui: &mut ui::Ui, part: impl std::hash::Hash, state: KeyframeState) -> bool { | |
| 899 | 328 | let (ink, fill, border) = match state { |
| 900 | 329 | KeyframeState::KeyframeAtCurrent => ( |
| 901 | 330 | [1.0, 0.80, 0.22, 1.0], |
| ... | ... | @@ -930,7 +359,7 @@ impl InspectorState { |
| 930 | 359 | } |
| 931 | 360 | |
| 932 | 361 | #[allow(clippy::too_many_arguments)] |
| 933 | fn toggle_keyframe( | |
| 362 | pub(super) fn toggle_keyframe( | |
| 934 | 363 | &self, |
| 935 | 364 | comp: &mut Comp, |
| 936 | 365 | history: &mut CompHistory, |
| ... | ... | @@ -982,22 +411,26 @@ impl InspectorState { |
| 982 | 411 | } |
| 983 | 412 | } |
| 984 | 413 | |
| 985 | fn render_expr_badge(ui: &mut ui::Ui) { | |
| 986 | ui.leaf( | |
| 987 | "expr-badge", | |
| 988 | ui::Spec { | |
| 989 | size: [ui::px(16.0), ui::px(18.0)], | |
| 990 | text: Some("="), | |
| 991 | font_size: Some(10.5), | |
| 992 | bold: true, | |
| 993 | color: Some([0.36, 0.88, 0.92, 1.0]), | |
| 994 | fill: Some([0.10, 0.24, 0.28, 0.85]), | |
| 995 | border: Some([0.32, 0.78, 0.86, 0.65]), | |
| 996 | radius: 3.0, | |
| 997 | center: true, | |
| 998 | ..ui::Spec::default() | |
| 999 | }, | |
| 1000 | ); | |
| 414 | pub(crate) fn enum_option_index(options: &[&str], current: &str) -> usize { | |
| 415 | options.iter().position(|&o| o == current).unwrap_or(0) | |
| 416 | } | |
| 417 | ||
| 418 | pub(crate) fn int_enum_lookup<'a>(options: &[(i32, &'a str)], val: i32) -> (usize, Cow<'a, str>) { | |
| 419 | match options.iter().position(|&(v, _)| v == val) { | |
| 420 | Some(idx) => (idx, Cow::Borrowed(options[idx].1)), | |
| 421 | None => (0, Cow::Owned(val.to_string())), | |
| 422 | } | |
| 423 | } | |
| 424 | ||
| 425 | pub(crate) fn combo_cycle_index(cur_idx: usize, option_count: usize, reverse: bool) -> Option<usize> { | |
| 426 | if option_count == 0 { | |
| 427 | return None; | |
| 428 | } | |
| 429 | Some(if reverse { | |
| 430 | (cur_idx + option_count - 1) % option_count | |
| 431 | } else { | |
| 432 | (cur_idx + 1) % option_count | |
| 433 | }) | |
| 1001 | 434 | } |
| 1002 | 435 | |
| 1003 | 436 | /// Computes whether an input is static, animated on another frame, or has a keyframe at `frame`. |
| ... | ... | @@ -1042,3 +475,111 @@ pub fn format_param_number(v: f64) -> String { |
| 1042 | 475 | format!("{v:.3}") |
| 1043 | 476 | } |
| 1044 | 477 | } |
| 478 | ||
| 479 | #[cfg(test)] | |
| 480 | mod tests { | |
| 481 | use super::*; | |
| 482 | use model::{BezierSpline, Comp, Keyframe}; | |
| 483 | ||
| 484 | #[test] | |
| 485 | fn int_enum_lookup_and_cycling_with_unknown_fallback() { | |
| 486 | const OPTS: &[(i32, &str)] = &[(0, "Normal"), (2, "Screen"), (5, "Multiply")]; | |
| 487 | ||
| 488 | let (idx, lbl) = int_enum_lookup(OPTS, 2); | |
| 489 | assert_eq!(idx, 1); | |
| 490 | assert_eq!(lbl.as_ref(), "Screen"); | |
| 491 | ||
| 492 | // Forward and reverse cycling wrap around | |
| 493 | assert_eq!(combo_cycle_index(idx, OPTS.len(), false), Some(2)); | |
| 494 | assert_eq!(combo_cycle_index(idx, OPTS.len(), true), Some(0)); | |
| 495 | assert_eq!(combo_cycle_index(0, OPTS.len(), true), Some(2)); | |
| 496 | assert_eq!(combo_cycle_index(2, OPTS.len(), false), Some(0)); | |
| 497 | ||
| 498 | // Unknown integer code falls back to index 0 and stringified code | |
| 499 | let (idx, lbl) = int_enum_lookup(OPTS, 99); | |
| 500 | assert_eq!(idx, 0); | |
| 501 | assert_eq!(lbl.as_ref(), "99"); | |
| 502 | assert_eq!(combo_cycle_index(idx, OPTS.len(), false), Some(1)); | |
| 503 | } | |
| 504 | ||
| 505 | #[test] | |
| 506 | fn combo_option_lookup_and_cycling() { | |
| 507 | const OPTS: &[&str] = &["Nearest", "Linear", "Cubic"]; | |
| 508 | ||
| 509 | assert_eq!(enum_option_index(OPTS, "Linear"), 1); | |
| 510 | assert_eq!(enum_option_index(OPTS, "Missing"), 0); | |
| 511 | ||
| 512 | assert_eq!(combo_cycle_index(0, OPTS.len(), false), Some(1)); | |
| 513 | assert_eq!(combo_cycle_index(2, OPTS.len(), false), Some(0)); | |
| 514 | assert_eq!(combo_cycle_index(0, OPTS.len(), true), Some(2)); | |
| 515 | assert_eq!(combo_cycle_index(1, OPTS.len(), true), Some(0)); | |
| 516 | assert_eq!(combo_cycle_index(0, 0, false), None); | |
| 517 | } | |
| 518 | ||
| 519 | #[test] | |
| 520 | fn format_param_number_uses_magnitude_precision() { | |
| 521 | assert_eq!(format_param_number(0.12345), "0.123"); | |
| 522 | assert_eq!(format_param_number(-9.999), "-9.999"); | |
| 523 | assert_eq!(format_param_number(10.0), "10.00"); | |
| 524 | assert_eq!(format_param_number(-42.567), "-42.57"); | |
| 525 | assert_eq!(format_param_number(100.0), "100.0"); | |
| 526 | assert_eq!(format_param_number(-1234.56), "-1234.6"); | |
| 527 | } | |
| 528 | ||
| 529 | #[test] | |
| 530 | fn compute_scrub_value_normal_and_fine_shift_precision() { | |
| 531 | // Full width drag of 100px over [0, 10] adds 10.0 normally, 1.5 with Shift (0.15) | |
| 532 | let normal = compute_scrub_value(2.0, 50.0, 100.0, 0.0, 10.0, 1.0); | |
| 533 | assert!((normal - 7.0).abs() < 1e-9); | |
| 534 | ||
| 535 | let fine = compute_scrub_value(2.0, 50.0, 100.0, 0.0, 10.0, 0.15); | |
| 536 | assert!((fine - 2.75).abs() < 1e-9); | |
| 537 | ||
| 538 | // Clamps to [min, max] and enforces minimum 40px width denominator | |
| 539 | assert_eq!(compute_scrub_value(8.0, 200.0, 100.0, 0.0, 10.0, 1.0), 10.0); | |
| 540 | assert_eq!(compute_scrub_value(1.0, -200.0, 100.0, 0.0, 10.0, 1.0), 0.0); | |
| 541 | let narrow = compute_scrub_value(0.0, 20.0, 10.0, 0.0, 10.0, 1.0); | |
| 542 | assert!((narrow - 5.0).abs() < 1e-9); | |
| 543 | } | |
| 544 | ||
| 545 | #[test] | |
| 546 | fn normalize_slider_fraction_clamps_and_handles_degenerate_span() { | |
| 547 | assert_eq!(normalize_slider_fraction(5.0, 0.0, 10.0), 0.5); | |
| 548 | assert_eq!(normalize_slider_fraction(-5.0, 0.0, 10.0), 0.0); | |
| 549 | assert_eq!(normalize_slider_fraction(25.0, 0.0, 10.0), 1.0); | |
| 550 | assert_eq!(normalize_slider_fraction(0.0, 1.0, 1.0), 0.0); | |
| 551 | } | |
| 552 | ||
| 553 | #[test] | |
| 554 | fn keyframe_state_respects_epsilon() { | |
| 555 | let mut comp = Comp::from_fusion_str( | |
| 556 | r#"Composition { | |
| 557 | Tools = ordered() { | |
| 558 | XF = Transform {}, | |
| 559 | }, | |
| 560 | }"#, | |
| 561 | ) | |
| 562 | .expect("comp"); | |
| 563 | let xf = comp.find_node("XF").unwrap(); | |
| 564 | ||
| 565 | assert_eq!(keyframe_state(&comp, xf, "Size", 10.0), KeyframeState::Static); | |
| 566 | ||
| 567 | comp.set_keyframe(xf, "Size", 10.0, 2.0).expect("set keyframe"); | |
| 568 | assert_eq!(keyframe_state(&comp, xf, "Size", 10.0), KeyframeState::KeyframeAtCurrent); | |
| 569 | assert_eq!(keyframe_state(&comp, xf, "Size", 10.0 + 5e-5), KeyframeState::KeyframeAtCurrent); | |
| 570 | assert_eq!(keyframe_state(&comp, xf, "Size", 10.0 + 2e-4), KeyframeState::AnimatedOtherFrame); | |
| 571 | assert_eq!(keyframe_state(&comp, xf, "Size", 20.0), KeyframeState::AnimatedOtherFrame); | |
| 572 | ||
| 573 | // Empty spline falls back to Static | |
| 574 | let sp_name = comp.node(xf).inputs.get("Size").and_then(|b| b.source_op.clone()).unwrap(); | |
| 575 | comp.splines.insert( | |
| 576 | sp_name, | |
| 577 | BezierSpline { | |
| 578 | name: "XFSize".to_owned(), | |
| 579 | color: [255, 255, 255], | |
| 580 | keyframes: Vec::<Keyframe>::new(), | |
| 581 | }, | |
| 582 | ); | |
| 583 | assert_eq!(keyframe_state(&comp, xf, "Size", 10.0), KeyframeState::Static); | |
| 584 | } | |
| 585 | } |
crates/spline/src/input.rs created+698| ... | ... | @@ -0,0 +1,698 @@ |
| 1 | //! Pointer hit-testing, marquee selection, pan/zoom, popup menus, and keyboard/mouse input | |
| 2 | //! handling for [`SplineView`]. | |
| 3 | ||
| 4 | use super::modal::{AxisLock, TransformMode, ensure_handles, hypot2}; | |
| 5 | use super::presets::{EasingPreset, apply_easing_preset, apply_handle_mode}; | |
| 6 | use super::{ | |
| 7 | HandlePart, POPUP_HEADER_H, POPUP_PAD, POPUP_ROW_H, POPUP_W, PointRef, RULER_H, SIDEBAR_W, | |
| 8 | SplineAction, SplineView, | |
| 9 | }; | |
| 10 | use model::{Comp, CompHistory, HandleMode, Keyframe}; | |
| 11 | use std::collections::HashMap; | |
| 12 | use winit::event::{ElementState, MouseButton}; | |
| 13 | use winit::keyboard::{Key, KeyCode, ModifiersState, NamedKey, PhysicalKey}; | |
| 14 | ||
| 15 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 16 | pub(super) enum PopupKind { | |
| 17 | HandleType, | |
| 18 | Easing, | |
| 19 | } | |
| 20 | ||
| 21 | #[derive(Clone, Debug)] | |
| 22 | pub(super) struct PopupMenu { | |
| 23 | pub(super) kind: PopupKind, | |
| 24 | pub(super) pos: [f32; 2], | |
| 25 | } | |
| 26 | ||
| 27 | pub(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 | ||
| 34 | pub(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)] | |
| 46 | pub(super) struct BoxSelect { | |
| 47 | pub(super) start_local: [f32; 2], | |
| 48 | pub(super) cur_local: [f32; 2], | |
| 49 | } | |
| 50 | ||
| 51 | impl 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 | let plot = self.plot_rect(); | |
| 493 | ||
| 494 | match event { | |
| 495 | ui::Event::PointerMoved(p) => { | |
| 496 | self.pointer_local = [p[0] - rect[0], p[1] - rect[1]]; | |
| 497 | if self.popup.is_some() { | |
| 498 | return actions; | |
| 499 | } | |
| 500 | if self.scrubbing { | |
| 501 | let t = self.local_to_data(self.pointer_local)[0].round(); | |
| 502 | actions.push(SplineAction::ScrubFrame(t)); | |
| 503 | } else if let Some((sp, tr, vr)) = self.panning { | |
| 504 | let dx = f64::from((self.pointer_local[0] - sp[0]) / (plot[2] - plot[0]).max(1.0)); | |
| 505 | let dy = f64::from((self.pointer_local[1] - sp[1]) / (plot[3] - plot[1]).max(1.0)); | |
| 506 | let dt = dx * (tr[1] - tr[0]); | |
| 507 | let dv = dy * (vr[1] - vr[0]); | |
| 508 | self.t_range = [tr[0] - dt, tr[1] - dt]; | |
| 509 | self.v_range = [vr[0] + dv, vr[1] + dv]; | |
| 510 | } else if self.modal.is_some() { | |
| 511 | if self.apply_modal(comp, modifiers.shift_key()) { | |
| 512 | actions.push(SplineAction::SplineEdited); | |
| 513 | } | |
| 514 | } else if let Some((start_l, ref mut dragging)) = self.lmb_drag_point { | |
| 515 | if !*dragging && hypot2(self.pointer_local, start_l) > 3.5 { | |
| 516 | *dragging = true; | |
| 517 | self.begin_modal(comp, history.is_some(), TransformMode::Grab); | |
| 518 | if self.apply_modal(comp, modifiers.shift_key()) { | |
| 519 | actions.push(SplineAction::SplineEdited); | |
| 520 | } | |
| 521 | } | |
| 522 | } else if let Some(bs) = &mut self.box_select { | |
| 523 | bs.cur_local = self.pointer_local; | |
| 524 | } else { | |
| 525 | self.hovered = self.hit_point(comp, self.pointer_local); | |
| 526 | } | |
| 527 | } | |
| 528 | ui::Event::Button { button, pressed, .. } => { | |
| 529 | if let Some(popup) = self.popup.clone() { | |
| 530 | if pressed { | |
| 531 | if button == MouseButton::Left { | |
| 532 | if let Some(idx) = self.popup_item_at(&popup, self.pointer_local) { | |
| 533 | self.popup = None; | |
| 534 | match popup.kind { | |
| 535 | PopupKind::HandleType => { | |
| 536 | if let Some(&(mode, _)) = HANDLE_MENU_ITEMS.get(idx) { | |
| 537 | if self.apply_selected_handle_mode(comp, history, mode) { | |
| 538 | actions.push(SplineAction::SplineEdited); | |
| 539 | } | |
| 540 | } | |
| 541 | } | |
| 542 | PopupKind::Easing => { | |
| 543 | if let Some(&(preset, _)) = EASING_MENU_ITEMS.get(idx) { | |
| 544 | if self.apply_selected_easing(comp, history, preset) { | |
| 545 | actions.push(SplineAction::SplineEdited); | |
| 546 | } | |
| 547 | } | |
| 548 | } | |
| 549 | } | |
| 550 | } else { | |
| 551 | self.popup = None; | |
| 552 | } | |
| 553 | } else { | |
| 554 | self.popup = None; | |
| 555 | } | |
| 556 | } | |
| 557 | return actions; | |
| 558 | } | |
| 559 | ||
| 560 | if button == MouseButton::Middle || (button == MouseButton::Left && modifiers.alt_key()) { | |
| 561 | if pressed { | |
| 562 | self.panning = Some((self.pointer_local, self.t_range, self.v_range)); | |
| 563 | } else { | |
| 564 | self.panning = None; | |
| 565 | } | |
| 566 | } else if button == MouseButton::Right { | |
| 567 | if pressed && self.modal.is_some() { | |
| 568 | self.cancel_modal(comp); | |
| 569 | self.lmb_drag_point = None; | |
| 570 | actions.push(SplineAction::SplineEdited); | |
| 571 | } | |
| 572 | } else if button == MouseButton::Left { | |
| 573 | if pressed { | |
| 574 | if self.modal.is_some() { | |
| 575 | self.confirm_modal(comp, history); | |
| 576 | self.lmb_drag_point = None; | |
| 577 | actions.push(SplineAction::SplineEdited); | |
| 578 | } else if point_in_rect(self.pointer_local, self.normalize_pill_rect()) { | |
| 579 | self.toggle_normalized(comp); | |
| 580 | } else if self.pointer_local[0] < SIDEBAR_W { | |
| 581 | let row = ((self.pointer_local[1] - RULER_H - 6.0) / 22.0).floor() as isize; | |
| 582 | if row >= 0 && (row as usize) < self.channels.len() { | |
| 583 | let sname = self.channels[row as usize].1.clone(); | |
| 584 | if !self.hidden_splines.remove(&sname) { | |
| 585 | self.hidden_splines.insert(sname); | |
| 586 | } | |
| 587 | } | |
| 588 | } else if self.pointer_local[1] < RULER_H { | |
| 589 | self.scrubbing = true; | |
| 590 | let t = self.local_to_data(self.pointer_local)[0].round(); | |
| 591 | actions.push(SplineAction::ScrubFrame(t)); | |
| 592 | } else if let Some(hit) = self.hit_point(comp, self.pointer_local) { | |
| 593 | if modifiers.shift_key() { | |
| 594 | if !self.selected.remove(&hit) { | |
| 595 | self.selected.insert(hit); | |
| 596 | } | |
| 597 | } else if !self.selected.contains(&hit) { | |
| 598 | self.selected.clear(); | |
| 599 | self.selected.insert(hit); | |
| 600 | } | |
| 601 | // Allow direct drag right from mouse down! | |
| 602 | self.lmb_drag_point = Some((self.pointer_local, false)); | |
| 603 | } else { | |
| 604 | if !modifiers.shift_key() { | |
| 605 | self.selected.clear(); | |
| 606 | } | |
| 607 | self.box_select = Some(BoxSelect { | |
| 608 | start_local: self.pointer_local, | |
| 609 | cur_local: self.pointer_local, | |
| 610 | }); | |
| 611 | } | |
| 612 | } else { | |
| 613 | self.scrubbing = false; | |
| 614 | if let Some((_start, dragging)) = self.lmb_drag_point.take() { | |
| 615 | if dragging && self.modal.is_some() { | |
| 616 | self.confirm_modal(comp, history.as_deref_mut()); | |
| 617 | actions.push(SplineAction::SplineEdited); | |
| 618 | } | |
| 619 | } | |
| 620 | if let Some(bs) = self.box_select.take() { | |
| 621 | let x0 = bs.start_local[0].min(bs.cur_local[0]); | |
| 622 | let x1 = bs.start_local[0].max(bs.cur_local[0]); | |
| 623 | let y0 = bs.start_local[1].min(bs.cur_local[1]); | |
| 624 | let y1 = bs.start_local[1].max(bs.cur_local[1]); | |
| 625 | if (x1 - x0) > 4.0 || (y1 - y0) > 4.0 { | |
| 626 | for (_, sname) in &self.channels { | |
| 627 | if self.hidden_splines.contains(sname) { | |
| 628 | continue; | |
| 629 | } | |
| 630 | let Some(sp) = comp.splines.get(sname) else { continue }; | |
| 631 | let bounds = self.spline_bounds(comp, sname); | |
| 632 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 633 | let p = self.spline_to_local(kf.time, kf.value, bounds); | |
| 634 | if p[0] >= x0 && p[0] <= x1 && p[1] >= y0 && p[1] <= y1 { | |
| 635 | self.selected.insert(PointRef { | |
| 636 | spline: sname.clone(), | |
| 637 | key_idx: ki, | |
| 638 | part: HandlePart::Key, | |
| 639 | }); | |
| 640 | } | |
| 641 | } | |
| 642 | } | |
| 643 | } | |
| 644 | } | |
| 645 | } | |
| 646 | } | |
| 647 | } | |
| 648 | ui::Event::Wheel(delta) => { | |
| 649 | // Blender convention: plain scroll zooms toward mouse cursor; Shift zooms Y; Alt pans. | |
| 650 | if modifiers.alt_key() { | |
| 651 | let dx = f64::from(delta[0] / (plot[2] - plot[0]).max(1.0)); | |
| 652 | let dy = f64::from(delta[1] / (plot[3] - plot[1]).max(1.0)); | |
| 653 | let dt = dx * (self.t_range[1] - self.t_range[0]); | |
| 654 | let dv = dy * (self.v_range[1] - self.v_range[0]); | |
| 655 | self.t_range = [self.t_range[0] - dt, self.t_range[1] - dt]; | |
| 656 | self.v_range = [self.v_range[0] + dv, self.v_range[1] + dv]; | |
| 657 | } else if modifiers.shift_key() { | |
| 658 | let f = f64::from((1.0 - delta[1] * 0.004).clamp(0.7, 1.4)); | |
| 659 | self.zoom_at(1.0, f, self.pointer_local); | |
| 660 | } else { | |
| 661 | let f = f64::from((1.0 - delta[1] * 0.0045).clamp(0.7, 1.4)); | |
| 662 | self.zoom_at(f, f, self.pointer_local); | |
| 663 | } | |
| 664 | } | |
| 665 | ui::Event::Pinch(delta) => { | |
| 666 | let f = f64::from((1.0 - delta).clamp(0.7, 1.4)); | |
| 667 | self.zoom_at(f, f, self.pointer_local); | |
| 668 | } | |
| 669 | ui::Event::KeyInput { physical, key, state: ElementState::Pressed, repeat: false, modifiers: kmod, .. } => { | |
| 670 | self.handle_key(comp, history, current_frame, Some(physical), &key, kmod, &mut actions); | |
| 671 | } | |
| 672 | ui::Event::Key { key, modifiers: kmod, .. } => { | |
| 673 | self.handle_key(comp, history, current_frame, None, &key, kmod, &mut actions); | |
| 674 | } | |
| 675 | _ => {} | |
| 676 | } | |
| 677 | ||
| 678 | actions | |
| 679 | } | |
| 680 | ||
| 681 | fn zoom_at(&mut self, ft: f64, fv: f64, pivot_local: [f32; 2]) { | |
| 682 | let p = self.local_to_data(pivot_local); | |
| 683 | let t0 = p[0] - (p[0] - self.t_range[0]) * ft; | |
| 684 | let t1 = p[0] + (self.t_range[1] - p[0]) * ft; | |
| 685 | let v0 = p[1] - (p[1] - self.v_range[0]) * fv; | |
| 686 | let v1 = p[1] + (self.v_range[1] - p[1]) * fv; | |
| 687 | if (t1 - t0) > 2.0 && (t1 - t0) < 100_000.0 { | |
| 688 | self.t_range = [t0, t1]; | |
| 689 | } | |
| 690 | if (v1 - v0) > 1e-4 && (v1 - v0) < 1_000_000.0 { | |
| 691 | self.v_range = [v0, v1]; | |
| 692 | } | |
| 693 | } | |
| 694 | } | |
| 695 | ||
| 696 | fn point_in_rect(p: [f32; 2], r: [f32; 4]) -> bool { | |
| 697 | p[0] >= r[0] && p[0] <= r[2] && p[1] >= r[1] && p[1] <= r[3] | |
| 698 | } |
crates/spline/src/lib.rs+32-1159| ... | ... | @@ -8,19 +8,19 @@ |
| 8 | 8 | //! - `Cmd+F` / Numpad `.` / `F` fit-to-view, `L` Linear/Smooth, `I` Insert key, `X/Del/BS` Delete |
| 9 | 9 | //! - Crisp high-contrast curves (zero XOR!), generous hit targets, live GPU Viewer feedback. |
| 10 | 10 | |
| 11 | mod input; | |
| 12 | mod modal; | |
| 11 | 13 | mod paint; |
| 12 | 14 | pub mod presets; |
| 13 | 15 | |
| 16 | pub use modal::{AxisLock, TransformMode, sort_keyframes}; | |
| 14 | 17 | pub use presets::EasingPreset; |
| 15 | 18 | |
| 16 | use model::{BezierSpline, Comp, CompHistory, HandleMode, Keyframe, NodeId}; | |
| 17 | use presets::{ | |
| 18 | apply_easing_preset, apply_handle_mode, compute_curve_bounds, display_to_value, | |
| 19 | enforce_aligned_handle, value_to_display, | |
| 20 | }; | |
| 19 | use input::{BoxSelect, EASING_MENU_ITEMS, HANDLE_MENU_ITEMS, PopupKind, PopupMenu}; | |
| 20 | use modal::ModalEdit; | |
| 21 | use model::{Comp, NodeId}; | |
| 22 | use presets::{compute_curve_bounds, value_to_display}; | |
| 21 | 23 | use std::collections::{HashMap, HashSet}; |
| 22 | use winit::event::{ElementState, MouseButton}; | |
| 23 | use winit::keyboard::{Key, KeyCode, ModifiersState, NamedKey, PhysicalKey}; | |
| 24 | 24 | |
| 25 | 25 | /// Layout entries kept before the label cache is rebuilt; panning and zooming mint new tick labels. |
| 26 | 26 | const TEXT_CACHE_CAP: usize = 512; |
| ... | ... | @@ -45,75 +45,6 @@ pub struct PointRef { |
| 45 | 45 | pub part: HandlePart, |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 49 | pub enum TransformMode { | |
| 50 | Grab, | |
| 51 | Rotate, | |
| 52 | Scale, | |
| 53 | } | |
| 54 | ||
| 55 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 56 | pub enum AxisLock { | |
| 57 | None, | |
| 58 | X, | |
| 59 | Y, | |
| 60 | } | |
| 61 | ||
| 62 | #[derive(Clone, Debug)] | |
| 63 | struct ModalEdit { | |
| 64 | mode: TransformMode, | |
| 65 | axis: AxisLock, | |
| 66 | /// Pivot in display coordinates `(time, display_value)`. | |
| 67 | pivot: [f64; 2], | |
| 68 | /// Pointer at modal start in display coordinates `(time, display_value)`. | |
| 69 | start_pointer: [f64; 2], | |
| 70 | saved_splines: HashMap<String, Vec<Keyframe>>, | |
| 71 | /// Selection when the modal began; indices refer to `saved_splines`, while `SplineView::selected` | |
| 72 | /// follows the keys through each re-sort. | |
| 73 | orig_selected: HashSet<PointRef>, | |
| 74 | /// Frozen per-spline `(min_v, max_v)` bounds captured when the modal began so normalized | |
| 75 | /// drags stay stable without shifting scale mid-drag. | |
| 76 | saved_bounds: HashMap<String, (f64, f64)>, | |
| 77 | /// Optional pre-edit `Comp` snapshot captured when a `CompHistory` is active. | |
| 78 | pre_edit_snapshot: Option<Comp>, | |
| 79 | } | |
| 80 | ||
| 81 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 82 | enum PopupKind { | |
| 83 | HandleType, | |
| 84 | Easing, | |
| 85 | } | |
| 86 | ||
| 87 | #[derive(Clone, Debug)] | |
| 88 | struct PopupMenu { | |
| 89 | kind: PopupKind, | |
| 90 | pos: [f32; 2], | |
| 91 | } | |
| 92 | ||
| 93 | const HANDLE_MENU_ITEMS: &[(HandleMode, &str)] = &[ | |
| 94 | (HandleMode::Free, "1: Free"), | |
| 95 | (HandleMode::Aligned, "2: Aligned"), | |
| 96 | (HandleMode::Auto, "3: Auto"), | |
| 97 | (HandleMode::Vector, "4: Vector"), | |
| 98 | ]; | |
| 99 | ||
| 100 | const EASING_MENU_ITEMS: &[(EasingPreset, &str)] = &[ | |
| 101 | (EasingPreset::Linear, "1: Linear"), | |
| 102 | (EasingPreset::Smooth, "2: Smooth"), | |
| 103 | (EasingPreset::StepIn, "3: Step In"), | |
| 104 | (EasingPreset::StepOut, "4: Step Out"), | |
| 105 | (EasingPreset::EaseInOut, "5: Ease In-Out"), | |
| 106 | (EasingPreset::EaseOut, "6: Ease Out"), | |
| 107 | (EasingPreset::EaseIn, "7: Ease In"), | |
| 108 | (EasingPreset::KnifeEase, "8: Knife Ease"), | |
| 109 | ]; | |
| 110 | ||
| 111 | #[derive(Clone, Debug)] | |
| 112 | struct BoxSelect { | |
| 113 | start_local: [f32; 2], | |
| 114 | cur_local: [f32; 2], | |
| 115 | } | |
| 116 | ||
| 117 | 48 | #[derive(Clone, Debug)] |
| 118 | 49 | pub enum SplineAction { |
| 119 | 50 | SplineEdited, |
| ... | ... | @@ -162,17 +93,17 @@ pub struct SplineView { |
| 162 | 93 | pub selected: HashSet<PointRef>, |
| 163 | 94 | pub normalized: bool, |
| 164 | 95 | pub palette: SplinePalette, |
| 165 | hovered: Option<PointRef>, | |
| 166 | modal: Option<ModalEdit>, | |
| 167 | popup: Option<PopupMenu>, | |
| 168 | lmb_drag_point: Option<([f32; 2], bool)>, | |
| 169 | box_select: Option<BoxSelect>, | |
| 170 | panning: Option<([f32; 2], [f64; 2], [f64; 2])>, | |
| 171 | scrubbing: bool, | |
| 172 | pointer_local: [f32; 2], | |
| 173 | current_node: Option<NodeId>, | |
| 174 | text_cache: HashMap<String, text::Layout>, | |
| 175 | last_hover_readout: String, | |
| 96 | pub(crate) hovered: Option<PointRef>, | |
| 97 | pub(crate) modal: Option<ModalEdit>, | |
| 98 | pub(crate) popup: Option<PopupMenu>, | |
| 99 | pub(crate) lmb_drag_point: Option<([f32; 2], bool)>, | |
| 100 | pub(crate) box_select: Option<BoxSelect>, | |
| 101 | pub(crate) panning: Option<([f32; 2], [f64; 2], [f64; 2])>, | |
| 102 | pub(crate) scrubbing: bool, | |
| 103 | pub(crate) pointer_local: [f32; 2], | |
| 104 | pub(crate) current_node: Option<NodeId>, | |
| 105 | pub(crate) text_cache: HashMap<String, text::Layout>, | |
| 106 | pub(crate) last_hover_readout: String, | |
| 176 | 107 | } |
| 177 | 108 | |
| 178 | 109 | impl SplineView { |
| ... | ... | @@ -318,7 +249,7 @@ impl SplineView { |
| 318 | 249 | } |
| 319 | 250 | } |
| 320 | 251 | |
| 321 | fn plot_rect(&self) -> [f32; 4] { | |
| 252 | pub(crate) fn plot_rect(&self) -> [f32; 4] { | |
| 322 | 253 | [ |
| 323 | 254 | SIDEBAR_W, |
| 324 | 255 | RULER_H, |
| ... | ... | @@ -327,11 +258,11 @@ impl SplineView { |
| 327 | 258 | ] |
| 328 | 259 | } |
| 329 | 260 | |
| 330 | fn normalize_pill_rect(&self) -> [f32; 4] { | |
| 261 | pub(crate) fn normalize_pill_rect(&self) -> [f32; 4] { | |
| 331 | 262 | [SIDEBAR_W - 46.0, 3.0, SIDEBAR_W - 6.0, RULER_H - 3.0] |
| 332 | 263 | } |
| 333 | 264 | |
| 334 | fn spline_bounds(&self, comp: &Comp, sname: &str) -> (f64, f64) { | |
| 265 | pub(crate) fn spline_bounds(&self, comp: &Comp, sname: &str) -> (f64, f64) { | |
| 335 | 266 | if let Some(m) = &self.modal { |
| 336 | 267 | if let Some(&b) = m.saved_bounds.get(sname) { |
| 337 | 268 | return b; |
| ... | ... | @@ -343,7 +274,7 @@ impl SplineView { |
| 343 | 274 | .unwrap_or((0.0, 1.0)) |
| 344 | 275 | } |
| 345 | 276 | |
| 346 | fn data_to_local(&self, t: f64, v: f64) -> [f32; 2] { | |
| 277 | pub(crate) fn data_to_local(&self, t: f64, v: f64) -> [f32; 2] { | |
| 347 | 278 | let r = self.plot_rect(); |
| 348 | 279 | let tx = (t - self.t_range[0]) / (self.t_range[1] - self.t_range[0]).max(1e-6); |
| 349 | 280 | let ty = (v - self.v_range[0]) / (self.v_range[1] - self.v_range[0]).max(1e-6); |
| ... | ... | @@ -353,12 +284,12 @@ impl SplineView { |
| 353 | 284 | ] |
| 354 | 285 | } |
| 355 | 286 | |
| 356 | fn spline_to_local(&self, t: f64, v: f64, bounds: (f64, f64)) -> [f32; 2] { | |
| 287 | pub(crate) fn spline_to_local(&self, t: f64, v: f64, bounds: (f64, f64)) -> [f32; 2] { | |
| 357 | 288 | let disp_v = value_to_display(v, bounds, self.normalized); |
| 358 | 289 | self.data_to_local(t, disp_v) |
| 359 | 290 | } |
| 360 | 291 | |
| 361 | fn local_to_data(&self, l: [f32; 2]) -> [f64; 2] { | |
| 292 | pub(crate) fn local_to_data(&self, l: [f32; 2]) -> [f64; 2] { | |
| 362 | 293 | let r = self.plot_rect(); |
| 363 | 294 | let tx = f64::from((l[0] - r[0]) / (r[2] - r[0]).max(1.0)); |
| 364 | 295 | let ty = f64::from((r[3] - l[1]) / (r[3] - r[1]).max(1.0)); |
| ... | ... | @@ -368,992 +299,20 @@ impl SplineView { |
| 368 | 299 | ] |
| 369 | 300 | } |
| 370 | 301 | |
| 371 | fn hit_point(&self, comp: &Comp, local: [f32; 2]) -> Option<PointRef> { | |
| 372 | let mut best_handle: Option<(f32, PointRef)> = None; | |
| 373 | let mut best_key: Option<(f32, PointRef)> = None; | |
| 374 | ||
| 375 | for (_, sname) in &self.channels { | |
| 376 | if self.hidden_splines.contains(sname) { | |
| 377 | continue; | |
| 378 | } | |
| 379 | let Some(sp) = comp.splines.get(sname) else { continue }; | |
| 380 | let bounds = self.spline_bounds(comp, sname); | |
| 381 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 382 | let kp = self.spline_to_local(kf.time, kf.value, bounds); | |
| 383 | let kd = hypot2(local, kp); | |
| 384 | if kd < 15.0 && best_key.as_ref().is_none_or(|(d, _)| kd < *d) { | |
| 385 | best_key = Some(( | |
| 386 | kd, | |
| 387 | PointRef { spline: sname.clone(), key_idx: ki, part: HandlePart::Key }, | |
| 388 | )); | |
| 389 | } | |
| 390 | ||
| 391 | if self.is_key_or_handle_selected(sname, ki) { | |
| 392 | if let Some(lh) = kf.lh { | |
| 393 | let lp = self.spline_to_local(lh[0], lh[1], bounds); | |
| 394 | let ld = hypot2(local, lp); | |
| 395 | if ld < 14.0 && best_handle.as_ref().is_none_or(|(d, _)| ld < *d) { | |
| 396 | best_handle = Some(( | |
| 397 | ld, | |
| 398 | PointRef { spline: sname.clone(), key_idx: ki, part: HandlePart::LeftHandle }, | |
| 399 | )); | |
| 400 | } | |
| 401 | } | |
| 402 | if let Some(rh) = kf.rh { | |
| 403 | let rp = self.spline_to_local(rh[0], rh[1], bounds); | |
| 404 | let rd = hypot2(local, rp); | |
| 405 | if rd < 14.0 && best_handle.as_ref().is_none_or(|(d, _)| rd < *d) { | |
| 406 | best_handle = Some(( | |
| 407 | rd, | |
| 408 | PointRef { spline: sname.clone(), key_idx: ki, part: HandlePart::RightHandle }, | |
| 409 | )); | |
| 410 | } | |
| 411 | } | |
| 412 | } | |
| 413 | } | |
| 414 | } | |
| 415 | best_handle.or(best_key).map(|(_, pr)| pr) | |
| 416 | } | |
| 417 | ||
| 418 | fn is_key_or_handle_selected(&self, spline: &str, key_idx: usize) -> bool { | |
| 302 | pub(crate) fn is_key_or_handle_selected(&self, spline: &str, key_idx: usize) -> bool { | |
| 419 | 303 | self.selected.iter().any(|p| p.spline == spline && p.key_idx == key_idx) |
| 420 | 304 | } |
| 421 | ||
| 422 | fn begin_modal(&mut self, comp: &Comp, capture_history: bool, mode: TransformMode) { | |
| 423 | if self.selected.is_empty() { | |
| 424 | return; | |
| 425 | } | |
| 426 | let mut sum_t = 0.0; | |
| 427 | let mut sum_v = 0.0; | |
| 428 | let mut count = 0usize; | |
| 429 | let mut saved = HashMap::new(); | |
| 430 | let mut saved_bounds = HashMap::new(); | |
| 431 | ||
| 432 | for pref in &self.selected { | |
| 433 | if let Some(sp) = comp.splines.get(&pref.spline) { | |
| 434 | saved.entry(pref.spline.clone()).or_insert_with(|| sp.keyframes.clone()); | |
| 435 | let bounds = *saved_bounds | |
| 436 | .entry(pref.spline.clone()) | |
| 437 | .or_insert_with(|| compute_curve_bounds(sp)); | |
| 438 | if let Some(kf) = sp.keyframes.get(pref.key_idx) { | |
| 439 | let pt = match pref.part { | |
| 440 | HandlePart::Key => [kf.time, kf.value], | |
| 441 | HandlePart::LeftHandle => kf.lh.unwrap_or([kf.time, kf.value]), | |
| 442 | HandlePart::RightHandle => kf.rh.unwrap_or([kf.time, kf.value]), | |
| 443 | }; | |
| 444 | let piv_pt = if self.selected.len() == 1 && pref.part != HandlePart::Key { | |
| 445 | [kf.time, kf.value] | |
| 446 | } else { | |
| 447 | pt | |
| 448 | }; | |
| 449 | sum_t += piv_pt[0]; | |
| 450 | sum_v += value_to_display(piv_pt[1], bounds, self.normalized); | |
| 451 | count += 1; | |
| 452 | } | |
| 453 | } | |
| 454 | } | |
| 455 | if count == 0 { | |
| 456 | return; | |
| 457 | } | |
| 458 | self.modal = Some(ModalEdit { | |
| 459 | mode, | |
| 460 | axis: AxisLock::None, | |
| 461 | pivot: [sum_t / count as f64, sum_v / count as f64], | |
| 462 | start_pointer: self.local_to_data(self.pointer_local), | |
| 463 | saved_splines: saved, | |
| 464 | orig_selected: self.selected.clone(), | |
| 465 | saved_bounds, | |
| 466 | pre_edit_snapshot: capture_history.then(|| comp.clone()), | |
| 467 | }); | |
| 468 | } | |
| 469 | ||
| 470 | fn apply_modal(&mut self, comp: &mut Comp, fine: bool) -> bool { | |
| 471 | let Some(m) = &self.modal else { return false }; | |
| 472 | let cur = self.local_to_data(self.pointer_local); | |
| 473 | let dt = cur[0] - m.start_pointer[0]; | |
| 474 | let dv_disp = cur[1] - m.start_pointer[1]; | |
| 475 | let piv = m.pivot; | |
| 476 | let normalized = self.normalized; | |
| 477 | ||
| 478 | for (sname, orig_kfs) in &m.saved_splines { | |
| 479 | if let Some(sp) = comp.splines.get_mut(sname) { | |
| 480 | sp.keyframes = orig_kfs.clone(); | |
| 481 | } | |
| 482 | } | |
| 483 | ||
| 484 | // A selected key carries its handles, so a handle also in the selection must not move twice. | |
| 485 | let selected = &m.orig_selected; | |
| 486 | let moves = |pref: &PointRef| { | |
| 487 | pref.part == HandlePart::Key | |
| 488 | || !selected.contains(&PointRef { spline: pref.spline.clone(), key_idx: pref.key_idx, part: HandlePart::Key }) | |
| 489 | }; | |
| 490 | ||
| 491 | let snap_t = |t: f64| if fine { t } else { t.round() }; | |
| 492 | ||
| 493 | // Helper: convert Auto/Vector handles to Aligned/Free when manually dragged (Blender behavior), | |
| 494 | // and mirror opposite handle if Aligned (and opposite handle isn't also explicitly selected). | |
| 495 | let finalize_handle_drag = |kf: &mut Keyframe, pref: &PointRef, selected: &HashSet<PointRef>| { | |
| 496 | kf.flags.linear = false; | |
| 497 | match kf.handle_mode { | |
| 498 | HandleMode::Auto => kf.handle_mode = HandleMode::Aligned, | |
| 499 | HandleMode::Vector => kf.handle_mode = HandleMode::Free, | |
| 500 | HandleMode::Aligned | HandleMode::Free => {} | |
| 501 | } | |
| 502 | if kf.handle_mode == HandleMode::Aligned { | |
| 503 | let opp_part = match pref.part { | |
| 504 | HandlePart::LeftHandle => HandlePart::RightHandle, | |
| 505 | HandlePart::RightHandle => HandlePart::LeftHandle, | |
| 506 | HandlePart::Key => return, | |
| 507 | }; | |
| 508 | let opp_selected = selected.contains(&PointRef { | |
| 509 | spline: pref.spline.clone(), | |
| 510 | key_idx: pref.key_idx, | |
| 511 | part: opp_part, | |
| 512 | }); | |
| 513 | if !opp_selected { | |
| 514 | enforce_aligned_handle(kf, pref.part); | |
| 515 | } | |
| 516 | } | |
| 517 | }; | |
| 518 | ||
| 519 | match m.mode { | |
| 520 | TransformMode::Grab => { | |
| 521 | let (mt, mv_disp) = match m.axis { | |
| 522 | AxisLock::None => (dt, dv_disp), | |
| 523 | AxisLock::X => (dt, 0.0), | |
| 524 | AxisLock::Y => (0.0, dv_disp), | |
| 525 | }; | |
| 526 | for pref in selected.iter().filter(|p| moves(p)) { | |
| 527 | let Some(sp) = comp.splines.get_mut(&pref.spline) else { continue }; | |
| 528 | let bounds = m.saved_bounds.get(&pref.spline).copied().unwrap_or((0.0, 1.0)); | |
| 529 | let mv = if normalized { | |
| 530 | mv_disp * (bounds.1 - bounds.0).max(1e-9) | |
| 531 | } else { | |
| 532 | mv_disp | |
| 533 | }; | |
| 534 | let Some(kf) = sp.keyframes.get_mut(pref.key_idx) else { continue }; | |
| 535 | match pref.part { | |
| 536 | HandlePart::Key => { | |
| 537 | let new_t = snap_t(kf.time + mt); | |
| 538 | let actual_dt = new_t - kf.time; | |
| 539 | kf.time = new_t; | |
| 540 | kf.value += mv; | |
| 541 | if let Some(lh) = &mut kf.lh { | |
| 542 | lh[0] += actual_dt; | |
| 543 | lh[1] += mv; | |
| 544 | } | |
| 545 | if let Some(rh) = &mut kf.rh { | |
| 546 | rh[0] += actual_dt; | |
| 547 | rh[1] += mv; | |
| 548 | } | |
| 549 | } | |
| 550 | HandlePart::LeftHandle => { | |
| 551 | let mut h = kf.lh.unwrap_or([kf.time - 5.0, kf.value]); | |
| 552 | h[0] = (h[0] + mt).min(kf.time - 0.01); | |
| 553 | h[1] += mv; | |
| 554 | kf.lh = Some(h); | |
| 555 | finalize_handle_drag(kf, pref, selected); | |
| 556 | } | |
| 557 | HandlePart::RightHandle => { | |
| 558 | let mut h = kf.rh.unwrap_or([kf.time + 5.0, kf.value]); | |
| 559 | h[0] = (h[0] + mt).max(kf.time + 0.01); | |
| 560 | h[1] += mv; | |
| 561 | kf.rh = Some(h); | |
| 562 | finalize_handle_drag(kf, pref, selected); | |
| 563 | } | |
| 564 | } | |
| 565 | } | |
| 566 | } | |
| 567 | TransformMode::Scale => { | |
| 568 | let s0 = self.data_to_local(m.start_pointer[0], m.start_pointer[1]); | |
| 569 | let s1 = self.pointer_local; | |
| 570 | let sp_px = self.data_to_local(piv[0], piv[1]); | |
| 571 | let d0 = hypot2(s0, sp_px).max(10.0); | |
| 572 | let d1 = hypot2(s1, sp_px); | |
| 573 | let f = f64::from((d1 / d0).clamp(0.05, 10.0)); | |
| 574 | let (st, sv) = match m.axis { | |
| 575 | AxisLock::None => (f, f), | |
| 576 | AxisLock::X => (f, 1.0), | |
| 577 | AxisLock::Y => (1.0, f), | |
| 578 | }; | |
| 579 | for pref in selected.iter().filter(|p| moves(p)) { | |
| 580 | let Some(sp) = comp.splines.get_mut(&pref.spline) else { continue }; | |
| 581 | let bounds = m.saved_bounds.get(&pref.spline).copied().unwrap_or((0.0, 1.0)); | |
| 582 | let piv_val = display_to_value(piv[1], bounds, normalized); | |
| 583 | let Some(kf) = sp.keyframes.get_mut(pref.key_idx) else { continue }; | |
| 584 | match pref.part { | |
| 585 | HandlePart::Key => { | |
| 586 | let raw_t = piv[0] + (kf.time - piv[0]) * st; | |
| 587 | kf.time = snap_t(raw_t); | |
| 588 | let snap = kf.time - raw_t; | |
| 589 | kf.value = piv_val + (kf.value - piv_val) * sv; | |
| 590 | if let Some(lh) = &mut kf.lh { | |
| 591 | lh[0] = (piv[0] + (lh[0] - piv[0]) * st + snap).min(kf.time - 0.01); | |
| 592 | lh[1] = piv_val + (lh[1] - piv_val) * sv; | |
| 593 | } | |
| 594 | if let Some(rh) = &mut kf.rh { | |
| 595 | rh[0] = (piv[0] + (rh[0] - piv[0]) * st + snap).max(kf.time + 0.01); | |
| 596 | rh[1] = piv_val + (rh[1] - piv_val) * sv; | |
| 597 | } | |
| 598 | } | |
| 599 | HandlePart::LeftHandle => { | |
| 600 | if let Some(lh) = &mut kf.lh { | |
| 601 | lh[0] = (kf.time + (lh[0] - kf.time) * st).min(kf.time - 0.01); | |
| 602 | lh[1] = kf.value + (lh[1] - kf.value) * sv; | |
| 603 | } | |
| 604 | finalize_handle_drag(kf, pref, selected); | |
| 605 | } | |
| 606 | HandlePart::RightHandle => { | |
| 607 | if let Some(rh) = &mut kf.rh { | |
| 608 | rh[0] = (kf.time + (rh[0] - kf.time) * st).max(kf.time + 0.01); | |
| 609 | rh[1] = kf.value + (rh[1] - kf.value) * sv; | |
| 610 | } | |
| 611 | finalize_handle_drag(kf, pref, selected); | |
| 612 | } | |
| 613 | } | |
| 614 | } | |
| 615 | } | |
| 616 | TransformMode::Rotate => { | |
| 617 | let sp_px = self.data_to_local(piv[0], piv[1]); | |
| 618 | let s0_px = self.data_to_local(m.start_pointer[0], m.start_pointer[1]); | |
| 619 | let a0 = (s0_px[1] - sp_px[1]).atan2(s0_px[0] - sp_px[0]); | |
| 620 | let a1 = (self.pointer_local[1] - sp_px[1]).atan2(self.pointer_local[0] - sp_px[0]); | |
| 621 | let da = a1 - a0; | |
| 622 | let (c, s) = (da.cos(), da.sin()); | |
| 623 | ||
| 624 | let rot = |pt: [f64; 2], center_px: [f32; 2], bounds: (f64, f64)| -> [f64; 2] { | |
| 625 | let p = self.spline_to_local(pt[0], pt[1], bounds); | |
| 626 | let rx = p[0] - center_px[0]; | |
| 627 | let ry = p[1] - center_px[1]; | |
| 628 | let np = [center_px[0] + rx * c - ry * s, center_px[1] + rx * s + ry * c]; | |
| 629 | let disp = self.local_to_data(np); | |
| 630 | [disp[0], display_to_value(disp[1], bounds, normalized)] | |
| 631 | }; | |
| 632 | ||
| 633 | for pref in selected.iter().filter(|p| moves(p)) { | |
| 634 | let Some(sp) = comp.splines.get_mut(&pref.spline) else { continue }; | |
| 635 | let bounds = m.saved_bounds.get(&pref.spline).copied().unwrap_or((0.0, 1.0)); | |
| 636 | let Some(kf) = sp.keyframes.get_mut(pref.key_idx) else { continue }; | |
| 637 | match pref.part { | |
| 638 | HandlePart::Key => { | |
| 639 | if selected.len() == 1 { | |
| 640 | let kp = self.spline_to_local(kf.time, kf.value, bounds); | |
| 641 | ensure_handles(kf); | |
| 642 | if let Some(lh) = kf.lh { | |
| 643 | let nl = rot(lh, kp, bounds); | |
| 644 | kf.lh = Some([nl[0].min(kf.time - 0.01), nl[1]]); | |
| 645 | } | |
| 646 | if let Some(rh) = kf.rh { | |
| 647 | let nr = rot(rh, kp, bounds); | |
| 648 | kf.rh = Some([nr[0].max(kf.time + 0.01), nr[1]]); | |
| 649 | } | |
| 650 | kf.flags.linear = false; | |
| 651 | } else { | |
| 652 | let nk = rot([kf.time, kf.value], sp_px, bounds); | |
| 653 | let lh = kf.lh.map(|h| rot(h, sp_px, bounds)); | |
| 654 | let rh = kf.rh.map(|h| rot(h, sp_px, bounds)); | |
| 655 | kf.time = snap_t(nk[0]); | |
| 656 | let snap = kf.time - nk[0]; | |
| 657 | kf.value = nk[1]; | |
| 658 | kf.lh = lh.map(|h| [(h[0] + snap).min(kf.time - 0.01), h[1]]); | |
| 659 | kf.rh = rh.map(|h| [(h[0] + snap).max(kf.time + 0.01), h[1]]); | |
| 660 | } | |
| 661 | } | |
| 662 | HandlePart::LeftHandle => { | |
| 663 | let kp = self.spline_to_local(kf.time, kf.value, bounds); | |
| 664 | if let Some(lh) = kf.lh { | |
| 665 | let nl = rot(lh, kp, bounds); | |
| 666 | kf.lh = Some([nl[0].min(kf.time - 0.01), nl[1]]); | |
| 667 | finalize_handle_drag(kf, pref, selected); | |
| 668 | } | |
| 669 | } | |
| 670 | HandlePart::RightHandle => { | |
| 671 | let kp = self.spline_to_local(kf.time, kf.value, bounds); | |
| 672 | if let Some(rh) = kf.rh { | |
| 673 | let nr = rot(rh, kp, bounds); | |
| 674 | kf.rh = Some([nr[0].max(kf.time + 0.01), nr[1]]); | |
| 675 | finalize_handle_drag(kf, pref, selected); | |
| 676 | } | |
| 677 | } | |
| 678 | } | |
| 679 | } | |
| 680 | } | |
| 681 | } | |
| 682 | // Keep curves sorted while dragging; selection follows its keys to their new slots. | |
| 683 | let remaps: HashMap<&String, Vec<usize>> = m | |
| 684 | .saved_splines | |
| 685 | .keys() | |
| 686 | .filter_map(|sname| Some((sname, sort_keyframes(comp.splines.get_mut(sname)?)))) | |
| 687 | .collect(); | |
| 688 | let new_selected: HashSet<PointRef> = selected | |
| 689 | .iter() | |
| 690 | .map(|p| PointRef { | |
| 691 | key_idx: remaps.get(&p.spline).and_then(|r| r.get(p.key_idx)).copied().unwrap_or(p.key_idx), | |
| 692 | ..p.clone() | |
| 693 | }) | |
| 694 | .collect(); | |
| 695 | self.selected = new_selected; | |
| 696 | comp.revision += 1; | |
| 697 | true | |
| 698 | } | |
| 699 | ||
| 700 | fn cancel_modal(&mut self, comp: &mut Comp) { | |
| 701 | if let Some(m) = self.modal.take() { | |
| 702 | for (sname, orig) in m.saved_splines { | |
| 703 | if let Some(sp) = comp.splines.get_mut(&sname) { | |
| 704 | sp.keyframes = orig; | |
| 705 | } | |
| 706 | } | |
| 707 | self.selected = m.orig_selected; | |
| 708 | comp.revision += 1; | |
| 709 | } | |
| 710 | } | |
| 711 | ||
| 712 | fn confirm_modal(&mut self, comp: &mut Comp, history: Option<&mut CompHistory>) { | |
| 713 | if let Some(m) = self.modal.take() { | |
| 714 | comp.revision += 1; | |
| 715 | if let (Some(h), Some(snap)) = (history, m.pre_edit_snapshot) { | |
| 716 | let label = match m.mode { | |
| 717 | TransformMode::Grab => "Grab Keyframes", | |
| 718 | TransformMode::Rotate => "Rotate Keyframes", | |
| 719 | TransformMode::Scale => "Scale Keyframes", | |
| 720 | }; | |
| 721 | h.push_snapshot(&snap, label); | |
| 722 | } | |
| 723 | } | |
| 724 | } | |
| 725 | ||
| 726 | pub fn is_popup_open(&self) -> bool { | |
| 727 | self.popup.is_some() | |
| 728 | } | |
| 729 | ||
| 730 | fn popup_rect(&self, popup: &PopupMenu) -> [f32; 4] { | |
| 731 | let count = match popup.kind { | |
| 732 | PopupKind::HandleType => HANDLE_MENU_ITEMS.len(), | |
| 733 | PopupKind::Easing => EASING_MENU_ITEMS.len(), | |
| 734 | }; | |
| 735 | let h = POPUP_HEADER_H + count as f32 * POPUP_ROW_H + POPUP_PAD; | |
| 736 | let x = popup.pos[0].clamp(4.0, (self.size[0] - POPUP_W - 4.0).max(4.0)); | |
| 737 | let y = popup.pos[1].clamp(4.0, (self.size[1] - h - 4.0).max(4.0)); | |
| 738 | [x, y, x + POPUP_W, y + h] | |
| 739 | } | |
| 740 | ||
| 741 | fn popup_item_at(&self, popup: &PopupMenu, local: [f32; 2]) -> Option<usize> { | |
| 742 | let r = self.popup_rect(popup); | |
| 743 | if local[0] < r[0] || local[0] > r[2] || local[1] < r[1] + POPUP_HEADER_H || local[1] > r[3] { | |
| 744 | return None; | |
| 745 | } | |
| 746 | let idx = ((local[1] - (r[1] + POPUP_HEADER_H)) / POPUP_ROW_H).floor() as isize; | |
| 747 | let count = match popup.kind { | |
| 748 | PopupKind::HandleType => HANDLE_MENU_ITEMS.len(), | |
| 749 | PopupKind::Easing => EASING_MENU_ITEMS.len(), | |
| 750 | }; | |
| 751 | if idx >= 0 && (idx as usize) < count { | |
| 752 | Some(idx as usize) | |
| 753 | } else { | |
| 754 | None | |
| 755 | } | |
| 756 | } | |
| 757 | ||
| 758 | fn open_popup(&mut self, kind: PopupKind) { | |
| 759 | self.popup = Some(PopupMenu { | |
| 760 | kind, | |
| 761 | pos: [self.pointer_local[0] + 6.0, self.pointer_local[1] + 6.0], | |
| 762 | }); | |
| 763 | } | |
| 764 | ||
| 765 | fn selected_keys_by_spline(&self, comp: &Comp) -> HashMap<String, Vec<usize>> { | |
| 766 | let mut by_spline: HashMap<String, Vec<usize>> = HashMap::new(); | |
| 767 | for pref in &self.selected { | |
| 768 | if let Some(sp) = comp.splines.get(&pref.spline) { | |
| 769 | if pref.key_idx < sp.keyframes.len() { | |
| 770 | by_spline.entry(pref.spline.clone()).or_default().push(pref.key_idx); | |
| 771 | } | |
| 772 | } | |
| 773 | } | |
| 774 | for idxs in by_spline.values_mut() { | |
| 775 | idxs.sort_unstable(); | |
| 776 | idxs.dedup(); | |
| 777 | } | |
| 778 | by_spline | |
| 779 | } | |
| 780 | ||
| 781 | fn apply_selected_handle_mode( | |
| 782 | &mut self, | |
| 783 | comp: &mut Comp, | |
| 784 | history: Option<&mut CompHistory>, | |
| 785 | mode: HandleMode, | |
| 786 | ) -> bool { | |
| 787 | let by_spline = self.selected_keys_by_spline(comp); | |
| 788 | if by_spline.is_empty() { | |
| 789 | return false; | |
| 790 | } | |
| 791 | if let Some(h) = history { | |
| 792 | h.push_snapshot(comp, "Set Handle Type"); | |
| 793 | } | |
| 794 | for (sname, idxs) in by_spline { | |
| 795 | if let Some(sp) = comp.splines.get_mut(&sname) { | |
| 796 | for idx in idxs { | |
| 797 | apply_handle_mode(sp, idx, mode); | |
| 798 | } | |
| 799 | } | |
| 800 | } | |
| 801 | comp.revision += 1; | |
| 802 | true | |
| 803 | } | |
| 804 | ||
| 805 | fn apply_selected_easing( | |
| 806 | &mut self, | |
| 807 | comp: &mut Comp, | |
| 808 | history: Option<&mut CompHistory>, | |
| 809 | preset: EasingPreset, | |
| 810 | ) -> bool { | |
| 811 | let by_spline = self.selected_keys_by_spline(comp); | |
| 812 | if by_spline.is_empty() { | |
| 813 | return false; | |
| 814 | } | |
| 815 | let pre_snap = history.as_ref().map(|_| comp.clone()); | |
| 816 | let mut changed = false; | |
| 817 | for (sname, idxs) in by_spline { | |
| 818 | if let Some(sp) = comp.splines.get_mut(&sname) { | |
| 819 | if apply_easing_preset(sp, &idxs, preset) { | |
| 820 | changed = true; | |
| 821 | } | |
| 822 | } | |
| 823 | } | |
| 824 | if changed { | |
| 825 | if let (Some(h), Some(snap)) = (history, pre_snap) { | |
| 826 | h.push_snapshot(&snap, format!("Easing: {}", preset.label())); | |
| 827 | } | |
| 828 | comp.revision += 1; | |
| 829 | } | |
| 830 | changed | |
| 831 | } | |
| 832 | ||
| 833 | fn handle_key( | |
| 834 | &mut self, | |
| 835 | comp: &mut Comp, | |
| 836 | mut history: Option<&mut CompHistory>, | |
| 837 | current_frame: f64, | |
| 838 | physical: Option<PhysicalKey>, | |
| 839 | key: &Key, | |
| 840 | kmod: ModifiersState, | |
| 841 | actions: &mut Vec<SplineAction>, | |
| 842 | ) { | |
| 843 | if let Some(popup) = self.popup.clone() { | |
| 844 | match key { | |
| 845 | Key::Named(NamedKey::Escape) => { | |
| 846 | self.popup = None; | |
| 847 | } | |
| 848 | Key::Character(c) => { | |
| 849 | let lc = c.to_ascii_lowercase(); | |
| 850 | if lc == "v" { | |
| 851 | self.popup = if popup.kind == PopupKind::HandleType { | |
| 852 | None | |
| 853 | } else { | |
| 854 | Some(PopupMenu { kind: PopupKind::HandleType, pos: popup.pos }) | |
| 855 | }; | |
| 856 | return; | |
| 857 | } | |
| 858 | if lc == "t" { | |
| 859 | self.popup = if popup.kind == PopupKind::Easing { | |
| 860 | None | |
| 861 | } else { | |
| 862 | Some(PopupMenu { kind: PopupKind::Easing, pos: popup.pos }) | |
| 863 | }; | |
| 864 | return; | |
| 865 | } | |
| 866 | if let Ok(num) = lc.parse::<usize>() { | |
| 867 | if num >= 1 { | |
| 868 | let idx = num - 1; | |
| 869 | match popup.kind { | |
| 870 | PopupKind::HandleType => { | |
| 871 | if let Some(&(mode, _)) = HANDLE_MENU_ITEMS.get(idx) { | |
| 872 | self.popup = None; | |
| 873 | if self.apply_selected_handle_mode(comp, history, mode) { | |
| 874 | actions.push(SplineAction::SplineEdited); | |
| 875 | } | |
| 876 | } | |
| 877 | } | |
| 878 | PopupKind::Easing => { | |
| 879 | if let Some(&(preset, _)) = EASING_MENU_ITEMS.get(idx) { | |
| 880 | self.popup = None; | |
| 881 | if self.apply_selected_easing(comp, history, preset) { | |
| 882 | actions.push(SplineAction::SplineEdited); | |
| 883 | } | |
| 884 | } | |
| 885 | } | |
| 886 | } | |
| 887 | } | |
| 888 | } | |
| 889 | } | |
| 890 | _ => {} | |
| 891 | } | |
| 892 | return; | |
| 893 | } | |
| 894 | ||
| 895 | if physical == Some(PhysicalKey::Code(KeyCode::NumpadDecimal)) { | |
| 896 | self.fit_all(comp); | |
| 897 | return; | |
| 898 | } | |
| 899 | let plain = !kmod.super_key() && !kmod.control_key() && !kmod.alt_key(); | |
| 900 | match key { | |
| 901 | Key::Named(NamedKey::Escape) => { | |
| 902 | if self.modal.is_some() { | |
| 903 | self.cancel_modal(comp); | |
| 904 | actions.push(SplineAction::SplineEdited); | |
| 905 | } | |
| 906 | } | |
| 907 | Key::Named(NamedKey::Enter) => { | |
| 908 | if self.modal.is_some() { | |
| 909 | self.confirm_modal(comp, history); | |
| 910 | actions.push(SplineAction::SplineEdited); | |
| 911 | } | |
| 912 | } | |
| 913 | Key::Named(NamedKey::Delete | NamedKey::Backspace) => { | |
| 914 | if self.modal.is_none() && self.delete_selected(comp, history) { | |
| 915 | actions.push(SplineAction::SplineEdited); | |
| 916 | } | |
| 917 | } | |
| 918 | Key::Character(c) => match c.to_ascii_lowercase().as_str() { | |
| 919 | "f" if !kmod.control_key() && !kmod.alt_key() => self.fit_all(comp), | |
| 920 | "." if plain => self.fit_all(comp), | |
| 921 | "n" if plain && self.modal.is_none() => { | |
| 922 | self.toggle_normalized(comp); | |
| 923 | } | |
| 924 | "v" if plain && self.modal.is_none() => { | |
| 925 | self.open_popup(PopupKind::HandleType); | |
| 926 | } | |
| 927 | "t" if plain && self.modal.is_none() => { | |
| 928 | self.open_popup(PopupKind::Easing); | |
| 929 | } | |
| 930 | "g" if plain && self.modal.is_none() => { | |
| 931 | self.begin_modal(comp, history.is_some(), TransformMode::Grab); | |
| 932 | } | |
| 933 | "r" if plain && self.modal.is_none() => { | |
| 934 | self.begin_modal(comp, history.is_some(), TransformMode::Rotate); | |
| 935 | } | |
| 936 | "s" if plain && self.modal.is_none() => { | |
| 937 | self.begin_modal(comp, history.is_some(), TransformMode::Scale); | |
| 938 | } | |
| 939 | "x" if plain && self.modal.is_some() => { | |
| 940 | if let Some(m) = &mut self.modal { | |
| 941 | m.axis = if m.axis == AxisLock::X { AxisLock::None } else { AxisLock::X }; | |
| 942 | } | |
| 943 | if self.apply_modal(comp, kmod.shift_key()) { | |
| 944 | actions.push(SplineAction::SplineEdited); | |
| 945 | } | |
| 946 | } | |
| 947 | "x" if plain && self.modal.is_none() => { | |
| 948 | // Blender `X` deletes selected keys when not in a modal transform! | |
| 949 | if self.delete_selected(comp, history) { | |
| 950 | actions.push(SplineAction::SplineEdited); | |
| 951 | } | |
| 952 | } | |
| 953 | "y" if plain && self.modal.is_some() => { | |
| 954 | if let Some(m) = &mut self.modal { | |
| 955 | m.axis = if m.axis == AxisLock::Y { AxisLock::None } else { AxisLock::Y }; | |
| 956 | } | |
| 957 | if self.apply_modal(comp, kmod.shift_key()) { | |
| 958 | actions.push(SplineAction::SplineEdited); | |
| 959 | } | |
| 960 | } | |
| 961 | "i" if plain && self.modal.is_none() => { | |
| 962 | // Blender `I` inserts keyframe at current integer frame on all visible channels | |
| 963 | let t = current_frame.round(); | |
| 964 | let will_insert = self.channels.iter().any(|(_, sname)| { | |
| 965 | !self.hidden_splines.contains(sname) | |
| 966 | && comp | |
| 967 | .splines | |
| 968 | .get(sname) | |
| 969 | .is_some_and(|sp| !sp.keyframes.iter().any(|k| (k.time - t).abs() < 0.5)) | |
| 970 | }); | |
| 971 | if will_insert { | |
| 972 | if let Some(h) = history.as_deref_mut() { | |
| 973 | h.push_snapshot(comp, "Insert Keyframe"); | |
| 974 | } | |
| 975 | for (_, sname) in &self.channels { | |
| 976 | if self.hidden_splines.contains(sname) { | |
| 977 | continue; | |
| 978 | } | |
| 979 | if let Some(sp) = comp.splines.get_mut(sname) { | |
| 980 | if !sp.keyframes.iter().any(|k| (k.time - t).abs() < 0.5) { | |
| 981 | let v = sp.eval(t); | |
| 982 | let at = sp.keyframes.partition_point(|k| k.time < t); | |
| 983 | sp.keyframes.insert(at, Keyframe::new(t, v)); | |
| 984 | self.selected = self | |
| 985 | .selected | |
| 986 | .drain() | |
| 987 | .map(|mut p| { | |
| 988 | if p.spline == *sname && p.key_idx >= at { | |
| 989 | p.key_idx += 1; | |
| 990 | } | |
| 991 | p | |
| 992 | }) | |
| 993 | .collect(); | |
| 994 | } | |
| 995 | } | |
| 996 | } | |
| 997 | comp.revision += 1; | |
| 998 | actions.push(SplineAction::SplineEdited); | |
| 999 | } | |
| 1000 | } | |
| 1001 | "a" if plain && self.modal.is_none() => { | |
| 1002 | if !self.selected.is_empty() { | |
| 1003 | self.selected.clear(); | |
| 1004 | } else { | |
| 1005 | for (_, sname) in &self.channels { | |
| 1006 | if self.hidden_splines.contains(sname) { | |
| 1007 | continue; | |
| 1008 | } | |
| 1009 | if let Some(sp) = comp.splines.get(sname) { | |
| 1010 | for ki in 0..sp.keyframes.len() { | |
| 1011 | self.selected.insert(PointRef { | |
| 1012 | spline: sname.clone(), | |
| 1013 | key_idx: ki, | |
| 1014 | part: HandlePart::Key, | |
| 1015 | }); | |
| 1016 | } | |
| 1017 | } | |
| 1018 | } | |
| 1019 | } | |
| 1020 | } | |
| 1021 | "l" if plain && self.modal.is_none() => { | |
| 1022 | let by_spline = self.selected_keys_by_spline(comp); | |
| 1023 | if !by_spline.is_empty() { | |
| 1024 | if let Some(h) = history.as_deref_mut() { | |
| 1025 | h.push_snapshot(comp, "Toggle Linear"); | |
| 1026 | } | |
| 1027 | for (sname, idxs) in by_spline { | |
| 1028 | if let Some(sp) = comp.splines.get_mut(&sname) { | |
| 1029 | for idx in idxs { | |
| 1030 | if let Some(kf) = sp.keyframes.get_mut(idx) { | |
| 1031 | kf.flags.linear = !kf.flags.linear; | |
| 1032 | if kf.flags.linear { | |
| 1033 | kf.lh = None; | |
| 1034 | kf.rh = None; | |
| 1035 | kf.handle_mode = HandleMode::Vector; | |
| 1036 | } else { | |
| 1037 | ensure_handles(kf); | |
| 1038 | } | |
| 1039 | } | |
| 1040 | } | |
| 1041 | } | |
| 1042 | } | |
| 1043 | comp.revision += 1; | |
| 1044 | actions.push(SplineAction::SplineEdited); | |
| 1045 | } | |
| 1046 | } | |
| 1047 | _ => {} | |
| 1048 | }, | |
| 1049 | _ => {} | |
| 1050 | } | |
| 1051 | } | |
| 1052 | ||
| 1053 | fn delete_selected(&mut self, comp: &mut Comp, history: Option<&mut CompHistory>) -> bool { | |
| 1054 | let mut by_spline: HashMap<String, Vec<usize>> = HashMap::new(); | |
| 1055 | for pref in &self.selected { | |
| 1056 | if pref.part == HandlePart::Key { | |
| 1057 | by_spline.entry(pref.spline.clone()).or_default().push(pref.key_idx); | |
| 1058 | } | |
| 1059 | } | |
| 1060 | for idxs in by_spline.values_mut() { | |
| 1061 | idxs.sort_unstable(); | |
| 1062 | idxs.dedup(); | |
| 1063 | } | |
| 1064 | let can_delete = by_spline.iter().any(|(sname, idxs)| { | |
| 1065 | comp.splines | |
| 1066 | .get(sname) | |
| 1067 | .is_some_and(|sp| sp.keyframes.len() > 1 && idxs.iter().any(|&i| i < sp.keyframes.len())) | |
| 1068 | }); | |
| 1069 | if !can_delete { | |
| 1070 | return false; | |
| 1071 | } | |
| 1072 | if let Some(h) = history { | |
| 1073 | h.push_snapshot(comp, "Delete Keyframes"); | |
| 1074 | } | |
| 1075 | let mut changed = false; | |
| 1076 | for (sname, idxs) in by_spline { | |
| 1077 | if let Some(sp) = comp.splines.get_mut(&sname) { | |
| 1078 | for &idx in idxs.iter().rev() { | |
| 1079 | if sp.keyframes.len() > 1 && idx < sp.keyframes.len() { | |
| 1080 | sp.keyframes.remove(idx); | |
| 1081 | changed = true; | |
| 1082 | } | |
| 1083 | } | |
| 1084 | } | |
| 1085 | } | |
| 1086 | if changed { | |
| 1087 | self.selected.clear(); | |
| 1088 | comp.revision += 1; | |
| 1089 | } | |
| 1090 | changed | |
| 1091 | } | |
| 1092 | ||
| 1093 | pub fn input( | |
| 1094 | &mut self, | |
| 1095 | comp: &mut Comp, | |
| 1096 | current_frame: f64, | |
| 1097 | event: ui::Event, | |
| 1098 | rect: [f32; 4], | |
| 1099 | pointer_window: [f32; 2], | |
| 1100 | modifiers: ModifiersState, | |
| 1101 | ) -> Vec<SplineAction> { | |
| 1102 | self.input_with_history(comp, None, current_frame, event, rect, pointer_window, modifiers) | |
| 1103 | } | |
| 1104 | ||
| 1105 | #[allow(clippy::too_many_arguments)] | |
| 1106 | pub fn input_with_history( | |
| 1107 | &mut self, | |
| 1108 | comp: &mut Comp, | |
| 1109 | mut history: Option<&mut CompHistory>, | |
| 1110 | current_frame: f64, | |
| 1111 | event: ui::Event, | |
| 1112 | rect: [f32; 4], | |
| 1113 | pointer_window: [f32; 2], | |
| 1114 | modifiers: ModifiersState, | |
| 1115 | ) -> Vec<SplineAction> { | |
| 1116 | let mut actions = Vec::new(); | |
| 1117 | self.pointer_local = [pointer_window[0] - rect[0], pointer_window[1] - rect[1]]; | |
| 1118 | let plot = self.plot_rect(); | |
| 1119 | ||
| 1120 | match event { | |
| 1121 | ui::Event::PointerMoved(p) => { | |
| 1122 | self.pointer_local = [p[0] - rect[0], p[1] - rect[1]]; | |
| 1123 | if self.popup.is_some() { | |
| 1124 | return actions; | |
| 1125 | } | |
| 1126 | if self.scrubbing { | |
| 1127 | let t = self.local_to_data(self.pointer_local)[0].round(); | |
| 1128 | actions.push(SplineAction::ScrubFrame(t)); | |
| 1129 | } else if let Some((sp, tr, vr)) = self.panning { | |
| 1130 | let dx = f64::from((self.pointer_local[0] - sp[0]) / (plot[2] - plot[0]).max(1.0)); | |
| 1131 | let dy = f64::from((self.pointer_local[1] - sp[1]) / (plot[3] - plot[1]).max(1.0)); | |
| 1132 | let dt = dx * (tr[1] - tr[0]); | |
| 1133 | let dv = dy * (vr[1] - vr[0]); | |
| 1134 | self.t_range = [tr[0] - dt, tr[1] - dt]; | |
| 1135 | self.v_range = [vr[0] + dv, vr[1] + dv]; | |
| 1136 | } else if self.modal.is_some() { | |
| 1137 | if self.apply_modal(comp, modifiers.shift_key()) { | |
| 1138 | actions.push(SplineAction::SplineEdited); | |
| 1139 | } | |
| 1140 | } else if let Some((start_l, ref mut dragging)) = self.lmb_drag_point { | |
| 1141 | if !*dragging && hypot2(self.pointer_local, start_l) > 3.5 { | |
| 1142 | *dragging = true; | |
| 1143 | self.begin_modal(comp, history.is_some(), TransformMode::Grab); | |
| 1144 | if self.apply_modal(comp, modifiers.shift_key()) { | |
| 1145 | actions.push(SplineAction::SplineEdited); | |
| 1146 | } | |
| 1147 | } | |
| 1148 | } else if let Some(bs) = &mut self.box_select { | |
| 1149 | bs.cur_local = self.pointer_local; | |
| 1150 | } else { | |
| 1151 | self.hovered = self.hit_point(comp, self.pointer_local); | |
| 1152 | } | |
| 1153 | } | |
| 1154 | ui::Event::Button { button, pressed, .. } => { | |
| 1155 | if let Some(popup) = self.popup.clone() { | |
| 1156 | if pressed { | |
| 1157 | if button == MouseButton::Left { | |
| 1158 | if let Some(idx) = self.popup_item_at(&popup, self.pointer_local) { | |
| 1159 | self.popup = None; | |
| 1160 | match popup.kind { | |
| 1161 | PopupKind::HandleType => { | |
| 1162 | if let Some(&(mode, _)) = HANDLE_MENU_ITEMS.get(idx) { | |
| 1163 | if self.apply_selected_handle_mode(comp, history, mode) { | |
| 1164 | actions.push(SplineAction::SplineEdited); | |
| 1165 | } | |
| 1166 | } | |
| 1167 | } | |
| 1168 | PopupKind::Easing => { | |
| 1169 | if let Some(&(preset, _)) = EASING_MENU_ITEMS.get(idx) { | |
| 1170 | if self.apply_selected_easing(comp, history, preset) { | |
| 1171 | actions.push(SplineAction::SplineEdited); | |
| 1172 | } | |
| 1173 | } | |
| 1174 | } | |
| 1175 | } | |
| 1176 | } else { | |
| 1177 | self.popup = None; | |
| 1178 | } | |
| 1179 | } else { | |
| 1180 | self.popup = None; | |
| 1181 | } | |
| 1182 | } | |
| 1183 | return actions; | |
| 1184 | } | |
| 1185 | ||
| 1186 | if button == MouseButton::Middle || (button == MouseButton::Left && modifiers.alt_key()) { | |
| 1187 | if pressed { | |
| 1188 | self.panning = Some((self.pointer_local, self.t_range, self.v_range)); | |
| 1189 | } else { | |
| 1190 | self.panning = None; | |
| 1191 | } | |
| 1192 | } else if button == MouseButton::Right { | |
| 1193 | if pressed && self.modal.is_some() { | |
| 1194 | self.cancel_modal(comp); | |
| 1195 | self.lmb_drag_point = None; | |
| 1196 | actions.push(SplineAction::SplineEdited); | |
| 1197 | } | |
| 1198 | } else if button == MouseButton::Left { | |
| 1199 | if pressed { | |
| 1200 | if self.modal.is_some() { | |
| 1201 | self.confirm_modal(comp, history); | |
| 1202 | self.lmb_drag_point = None; | |
| 1203 | actions.push(SplineAction::SplineEdited); | |
| 1204 | } else if point_in_rect(self.pointer_local, self.normalize_pill_rect()) { | |
| 1205 | self.toggle_normalized(comp); | |
| 1206 | } else if self.pointer_local[0] < SIDEBAR_W { | |
| 1207 | let row = ((self.pointer_local[1] - RULER_H - 6.0) / 22.0).floor() as isize; | |
| 1208 | if row >= 0 && (row as usize) < self.channels.len() { | |
| 1209 | let sname = self.channels[row as usize].1.clone(); | |
| 1210 | if !self.hidden_splines.remove(&sname) { | |
| 1211 | self.hidden_splines.insert(sname); | |
| 1212 | } | |
| 1213 | } | |
| 1214 | } else if self.pointer_local[1] < RULER_H { | |
| 1215 | self.scrubbing = true; | |
| 1216 | let t = self.local_to_data(self.pointer_local)[0].round(); | |
| 1217 | actions.push(SplineAction::ScrubFrame(t)); | |
| 1218 | } else if let Some(hit) = self.hit_point(comp, self.pointer_local) { | |
| 1219 | if modifiers.shift_key() { | |
| 1220 | if !self.selected.remove(&hit) { | |
| 1221 | self.selected.insert(hit); | |
| 1222 | } | |
| 1223 | } else if !self.selected.contains(&hit) { | |
| 1224 | self.selected.clear(); | |
| 1225 | self.selected.insert(hit); | |
| 1226 | } | |
| 1227 | // Allow direct drag right from mouse down! | |
| 1228 | self.lmb_drag_point = Some((self.pointer_local, false)); | |
| 1229 | } else { | |
| 1230 | if !modifiers.shift_key() { | |
| 1231 | self.selected.clear(); | |
| 1232 | } | |
| 1233 | self.box_select = Some(BoxSelect { | |
| 1234 | start_local: self.pointer_local, | |
| 1235 | cur_local: self.pointer_local, | |
| 1236 | }); | |
| 1237 | } | |
| 1238 | } else { | |
| 1239 | self.scrubbing = false; | |
| 1240 | if let Some((_start, dragging)) = self.lmb_drag_point.take() { | |
| 1241 | if dragging && self.modal.is_some() { | |
| 1242 | self.confirm_modal(comp, history.as_deref_mut()); | |
| 1243 | actions.push(SplineAction::SplineEdited); | |
| 1244 | } | |
| 1245 | } | |
| 1246 | if let Some(bs) = self.box_select.take() { | |
| 1247 | let x0 = bs.start_local[0].min(bs.cur_local[0]); | |
| 1248 | let x1 = bs.start_local[0].max(bs.cur_local[0]); | |
| 1249 | let y0 = bs.start_local[1].min(bs.cur_local[1]); | |
| 1250 | let y1 = bs.start_local[1].max(bs.cur_local[1]); | |
| 1251 | if (x1 - x0) > 4.0 || (y1 - y0) > 4.0 { | |
| 1252 | for (_, sname) in &self.channels { | |
| 1253 | if self.hidden_splines.contains(sname) { | |
| 1254 | continue; | |
| 1255 | } | |
| 1256 | let Some(sp) = comp.splines.get(sname) else { continue }; | |
| 1257 | let bounds = self.spline_bounds(comp, sname); | |
| 1258 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 1259 | let p = self.spline_to_local(kf.time, kf.value, bounds); | |
| 1260 | if p[0] >= x0 && p[0] <= x1 && p[1] >= y0 && p[1] <= y1 { | |
| 1261 | self.selected.insert(PointRef { | |
| 1262 | spline: sname.clone(), | |
| 1263 | key_idx: ki, | |
| 1264 | part: HandlePart::Key, | |
| 1265 | }); | |
| 1266 | } | |
| 1267 | } | |
| 1268 | } | |
| 1269 | } | |
| 1270 | } | |
| 1271 | } | |
| 1272 | } | |
| 1273 | } | |
| 1274 | ui::Event::Wheel(delta) => { | |
| 1275 | // Blender convention: plain scroll zooms toward mouse cursor; Shift zooms Y; Alt pans. | |
| 1276 | if modifiers.alt_key() { | |
| 1277 | let dx = f64::from(delta[0] / (plot[2] - plot[0]).max(1.0)); | |
| 1278 | let dy = f64::from(delta[1] / (plot[3] - plot[1]).max(1.0)); | |
| 1279 | let dt = dx * (self.t_range[1] - self.t_range[0]); | |
| 1280 | let dv = dy * (self.v_range[1] - self.v_range[0]); | |
| 1281 | self.t_range = [self.t_range[0] - dt, self.t_range[1] - dt]; | |
| 1282 | self.v_range = [self.v_range[0] + dv, self.v_range[1] + dv]; | |
| 1283 | } else if modifiers.shift_key() { | |
| 1284 | let f = f64::from((1.0 - delta[1] * 0.004).clamp(0.7, 1.4)); | |
| 1285 | self.zoom_at(1.0, f, self.pointer_local); | |
| 1286 | } else { | |
| 1287 | let f = f64::from((1.0 - delta[1] * 0.0045).clamp(0.7, 1.4)); | |
| 1288 | self.zoom_at(f, f, self.pointer_local); | |
| 1289 | } | |
| 1290 | } | |
| 1291 | ui::Event::Pinch(delta) => { | |
| 1292 | let f = f64::from((1.0 - delta).clamp(0.7, 1.4)); | |
| 1293 | self.zoom_at(f, f, self.pointer_local); | |
| 1294 | } | |
| 1295 | ui::Event::KeyInput { physical, key, state: ElementState::Pressed, repeat: false, modifiers: kmod, .. } => { | |
| 1296 | self.handle_key(comp, history, current_frame, Some(physical), &key, kmod, &mut actions); | |
| 1297 | } | |
| 1298 | ui::Event::Key { key, modifiers: kmod, .. } => { | |
| 1299 | self.handle_key(comp, history, current_frame, None, &key, kmod, &mut actions); | |
| 1300 | } | |
| 1301 | _ => {} | |
| 1302 | } | |
| 1303 | ||
| 1304 | actions | |
| 1305 | } | |
| 1306 | ||
| 1307 | fn zoom_at(&mut self, ft: f64, fv: f64, pivot_local: [f32; 2]) { | |
| 1308 | let p = self.local_to_data(pivot_local); | |
| 1309 | let t0 = p[0] - (p[0] - self.t_range[0]) * ft; | |
| 1310 | let t1 = p[0] + (self.t_range[1] - p[0]) * ft; | |
| 1311 | let v0 = p[1] - (p[1] - self.v_range[0]) * fv; | |
| 1312 | let v1 = p[1] + (self.v_range[1] - p[1]) * fv; | |
| 1313 | if (t1 - t0) > 2.0 && (t1 - t0) < 100_000.0 { | |
| 1314 | self.t_range = [t0, t1]; | |
| 1315 | } | |
| 1316 | if (v1 - v0) > 1e-4 && (v1 - v0) < 1_000_000.0 { | |
| 1317 | self.v_range = [v0, v1]; | |
| 1318 | } | |
| 1319 | } | |
| 1320 | } | |
| 1321 | ||
| 1322 | fn point_in_rect(p: [f32; 2], r: [f32; 4]) -> bool { | |
| 1323 | p[0] >= r[0] && p[0] <= r[2] && p[1] >= r[1] && p[1] <= r[3] | |
| 1324 | } | |
| 1325 | ||
| 1326 | /// Sorts `sp.keyframes` by time (stable) and returns each old index's new index. | |
| 1327 | pub fn sort_keyframes(sp: &mut BezierSpline) -> Vec<usize> { | |
| 1328 | let mut order: Vec<usize> = (0..sp.keyframes.len()).collect(); | |
| 1329 | order.sort_by(|&a, &b| sp.keyframes[a].time.total_cmp(&sp.keyframes[b].time)); | |
| 1330 | let mut new_index = vec![0; order.len()]; | |
| 1331 | for (new, &old) in order.iter().enumerate() { | |
| 1332 | new_index[old] = new; | |
| 1333 | } | |
| 1334 | sp.keyframes = order.iter().map(|&old| sp.keyframes[old].clone()).collect(); | |
| 1335 | new_index | |
| 1336 | } | |
| 1337 | ||
| 1338 | fn ensure_handles(kf: &mut Keyframe) { | |
| 1339 | if kf.lh.is_none() { | |
| 1340 | kf.lh = Some([kf.time - 8.0, kf.value]); | |
| 1341 | } | |
| 1342 | if kf.rh.is_none() { | |
| 1343 | kf.rh = Some([kf.time + 8.0, kf.value]); | |
| 1344 | } | |
| 1345 | } | |
| 1346 | ||
| 1347 | fn hypot2(a: [f32; 2], b: [f32; 2]) -> f32 { | |
| 1348 | ((a[0] - b[0]).powi(2) + (a[1] - b[1]).powi(2)).sqrt() | |
| 1349 | 305 | } |
| 1350 | 306 | |
| 1351 | 307 | #[cfg(test)] |
| 1352 | mod tests { | |
| 308 | pub(crate) mod tests { | |
| 1353 | 309 | use super::*; |
| 1354 | use model::BezierSpline; | |
| 310 | use model::{BezierSpline, CompHistory, HandleMode, Keyframe}; | |
| 311 | use presets::apply_handle_mode; | |
| 312 | use winit::event::MouseButton; | |
| 313 | use winit::keyboard::{Key, ModifiersState, NamedKey}; | |
| 1355 | 314 | |
| 1356 | fn test_comp() -> Comp { | |
| 315 | pub(crate) fn test_comp() -> Comp { | |
| 1357 | 316 | let mut comp = Comp::from_fusion_str("Composition {}").expect("empty comp"); |
| 1358 | 317 | comp.splines.insert( |
| 1359 | 318 | "Gain".to_owned(), |
| ... | ... | @@ -1577,7 +536,7 @@ mod tests { |
| 1577 | 536 | ); |
| 1578 | 537 | } |
| 1579 | 538 | |
| 1580 | fn key_ref(idx: usize, part: HandlePart) -> PointRef { | |
| 539 | pub(crate) fn key_ref(idx: usize, part: HandlePart) -> PointRef { | |
| 1581 | 540 | PointRef { spline: "Gain".to_owned(), key_idx: idx, part } |
| 1582 | 541 | } |
| 1583 | 542 | |
| ... | ... | @@ -1614,49 +573,6 @@ mod tests { |
| 1614 | 573 | assert_eq!(view.current_node, None); |
| 1615 | 574 | } |
| 1616 | 575 | |
| 1617 | #[test] | |
| 1618 | fn dragging_a_key_past_its_neighbour_keeps_curve_sorted_and_selection_on_the_key() { | |
| 1619 | let mut comp = test_comp(); | |
| 1620 | let mut view = SplineView::new(&ui::Theme::dark()); | |
| 1621 | view.resize([800.0, 360.0]); | |
| 1622 | view.set_target_node(&comp, None); | |
| 1623 | view.selected.insert(key_ref(0, HandlePart::Key)); | |
| 1624 | ||
| 1625 | view.pointer_local = view.spline_to_local(0.0, 10.0, (10.0, 110.0)); | |
| 1626 | view.begin_modal(&comp, false, TransformMode::Grab); | |
| 1627 | view.pointer_local = view.spline_to_local(70.0, 10.0, (10.0, 110.0)); | |
| 1628 | assert!(view.apply_modal(&mut comp, false)); | |
| 1629 | ||
| 1630 | let times: Vec<f64> = comp.splines["Gain"].keyframes.iter().map(|k| k.time).collect(); | |
| 1631 | assert_eq!(times, [50.0, 70.0, 100.0]); | |
| 1632 | assert_eq!(view.selected, HashSet::from([key_ref(1, HandlePart::Key)])); | |
| 1633 | ||
| 1634 | // Cancel puts both the keys and the selection back. | |
| 1635 | view.cancel_modal(&mut comp); | |
| 1636 | assert_eq!(comp.splines["Gain"].keyframes[0].time, 0.0); | |
| 1637 | assert_eq!(view.selected, HashSet::from([key_ref(0, HandlePart::Key)])); | |
| 1638 | } | |
| 1639 | ||
| 1640 | #[test] | |
| 1641 | fn grabbing_a_key_and_its_own_handle_moves_the_handle_once() { | |
| 1642 | let mut comp = test_comp(); | |
| 1643 | comp.splines.get_mut("Gain").unwrap().keyframes[1].lh = Some([40.0, 110.0]); | |
| 1644 | let mut view = SplineView::new(&ui::Theme::dark()); | |
| 1645 | view.resize([800.0, 360.0]); | |
| 1646 | view.set_target_node(&comp, None); | |
| 1647 | view.selected.insert(key_ref(1, HandlePart::Key)); | |
| 1648 | view.selected.insert(key_ref(1, HandlePart::LeftHandle)); | |
| 1649 | ||
| 1650 | view.pointer_local = view.spline_to_local(50.0, 110.0, (10.0, 110.0)); | |
| 1651 | view.begin_modal(&comp, false, TransformMode::Grab); | |
| 1652 | view.pointer_local = view.spline_to_local(60.0, 110.0, (10.0, 110.0)); | |
| 1653 | view.apply_modal(&mut comp, false); | |
| 1654 | ||
| 1655 | let kf = &comp.splines["Gain"].keyframes[1]; | |
| 1656 | assert!((kf.time - 60.0).abs() < 1e-6); | |
| 1657 | assert!((kf.lh.unwrap()[0] - 50.0).abs() < 1e-6, "handle shifted {:?}", kf.lh); | |
| 1658 | } | |
| 1659 | ||
| 1660 | 576 | #[test] |
| 1661 | 577 | fn inserting_a_key_before_the_selection_shifts_selected_indices() { |
| 1662 | 578 | let mut comp = test_comp(); |
| ... | ... | @@ -1706,49 +622,6 @@ mod tests { |
| 1706 | 622 | assert_eq!(comp.splines["Gain"].keyframes.len(), 3); |
| 1707 | 623 | } |
| 1708 | 624 | |
| 1709 | #[test] | |
| 1710 | fn multi_key_rotate_carries_handles_with_their_keys() { | |
| 1711 | let mut comp = test_comp(); | |
| 1712 | { | |
| 1713 | let kfs = &mut comp.splines.get_mut("Gain").unwrap().keyframes; | |
| 1714 | kfs[0].lh = Some([-10.0, 10.0]); | |
| 1715 | kfs[0].rh = Some([10.0, 10.0]); | |
| 1716 | kfs[1].lh = Some([40.0, 110.0]); | |
| 1717 | kfs[1].rh = Some([60.0, 110.0]); | |
| 1718 | } | |
| 1719 | let orig = comp.splines["Gain"].keyframes.clone(); | |
| 1720 | let mut view = SplineView::new(&ui::Theme::dark()); | |
| 1721 | view.resize([800.0, 360.0]); | |
| 1722 | view.set_target_node(&comp, None); | |
| 1723 | view.selected.insert(key_ref(0, HandlePart::Key)); | |
| 1724 | view.selected.insert(key_ref(1, HandlePart::Key)); | |
| 1725 | ||
| 1726 | let bounds = (10.0, 110.0); | |
| 1727 | let pivot_data = [25.0, 60.0]; | |
| 1728 | let piv = view.spline_to_local(pivot_data[0], pivot_data[1], bounds); | |
| 1729 | view.pointer_local = [piv[0] + 100.0, piv[1]]; | |
| 1730 | view.begin_modal(&comp, false, TransformMode::Rotate); | |
| 1731 | let theta = 0.1_f32; | |
| 1732 | view.pointer_local = [piv[0] + 100.0 * theta.cos(), piv[1] + 100.0 * theta.sin()]; | |
| 1733 | assert!(view.apply_modal(&mut comp, true)); | |
| 1734 | ||
| 1735 | let rotated = |pt: [f64; 2]| { | |
| 1736 | let p = view.spline_to_local(pt[0], pt[1], bounds); | |
| 1737 | let (rx, ry) = (p[0] - piv[0], p[1] - piv[1]); | |
| 1738 | let np = [piv[0] + rx * theta.cos() - ry * theta.sin(), piv[1] + rx * theta.sin() + ry * theta.cos()]; | |
| 1739 | let d = view.local_to_data(np); | |
| 1740 | [d[0], display_to_value(d[1], bounds, false)] | |
| 1741 | }; | |
| 1742 | let kfs = &comp.splines["Gain"].keyframes; | |
| 1743 | for (i, o) in orig.iter().take(2).enumerate() { | |
| 1744 | for (got, want) in [(kfs[i].lh, o.lh), (kfs[i].rh, o.rh)] { | |
| 1745 | let want = rotated(want.unwrap()); | |
| 1746 | let got = got.unwrap(); | |
| 1747 | assert!((got[0] - want[0]).abs() < 1e-2 && (got[1] - want[1]).abs() < 1e-2, "key {i}: {got:?} vs {want:?}"); | |
| 1748 | } | |
| 1749 | } | |
| 1750 | } | |
| 1751 | ||
| 1752 | 625 | #[test] |
| 1753 | 626 | fn destructive_keys_are_ignored_while_a_modal_is_active() { |
| 1754 | 627 | let mut comp = test_comp(); |
crates/spline/src/modal.rs created+464| ... | ... | @@ -0,0 +1,464 @@ |
| 1 | //! Modal keyframe transforms (`G` grab, `R` rotate, `S` scale) and spline sorting for [`SplineView`]. | |
| 2 | ||
| 3 | use super::{HandlePart, PointRef, SplineView}; | |
| 4 | use model::{BezierSpline, Comp, CompHistory, HandleMode, Keyframe}; | |
| 5 | use presets::{display_to_value, enforce_aligned_handle, value_to_display}; | |
| 6 | use std::collections::{HashMap, HashSet}; | |
| 7 | ||
| 8 | use super::presets; | |
| 9 | ||
| 10 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 11 | pub enum TransformMode { | |
| 12 | Grab, | |
| 13 | Rotate, | |
| 14 | Scale, | |
| 15 | } | |
| 16 | ||
| 17 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 18 | pub enum AxisLock { | |
| 19 | None, | |
| 20 | X, | |
| 21 | Y, | |
| 22 | } | |
| 23 | ||
| 24 | #[derive(Clone, Debug)] | |
| 25 | pub(super) struct ModalEdit { | |
| 26 | pub(super) mode: TransformMode, | |
| 27 | pub(super) axis: AxisLock, | |
| 28 | /// Pivot in display coordinates `(time, display_value)`. | |
| 29 | pub(super) pivot: [f64; 2], | |
| 30 | /// Pointer at modal start in display coordinates `(time, display_value)`. | |
| 31 | pub(super) start_pointer: [f64; 2], | |
| 32 | pub(super) saved_splines: HashMap<String, Vec<Keyframe>>, | |
| 33 | /// Selection when the modal began; indices refer to `saved_splines`, while `SplineView::selected` | |
| 34 | /// follows the keys through each re-sort. | |
| 35 | pub(super) orig_selected: HashSet<PointRef>, | |
| 36 | /// Frozen per-spline `(min_v, max_v)` bounds captured when the modal began so normalized | |
| 37 | /// drags stay stable without shifting scale mid-drag. | |
| 38 | pub(super) saved_bounds: HashMap<String, (f64, f64)>, | |
| 39 | /// Optional pre-edit `Comp` snapshot captured when a `CompHistory` is active. | |
| 40 | pub(super) pre_edit_snapshot: Option<Comp>, | |
| 41 | } | |
| 42 | ||
| 43 | impl SplineView { | |
| 44 | pub(super) fn begin_modal(&mut self, comp: &Comp, capture_history: bool, mode: TransformMode) { | |
| 45 | if self.selected.is_empty() { | |
| 46 | return; | |
| 47 | } | |
| 48 | let mut sum_t = 0.0; | |
| 49 | let mut sum_v = 0.0; | |
| 50 | let mut count = 0usize; | |
| 51 | let mut saved = HashMap::new(); | |
| 52 | let mut saved_bounds = HashMap::new(); | |
| 53 | ||
| 54 | for pref in &self.selected { | |
| 55 | if let Some(sp) = comp.splines.get(&pref.spline) { | |
| 56 | saved.entry(pref.spline.clone()).or_insert_with(|| sp.keyframes.clone()); | |
| 57 | let bounds = *saved_bounds | |
| 58 | .entry(pref.spline.clone()) | |
| 59 | .or_insert_with(|| presets::compute_curve_bounds(sp)); | |
| 60 | if let Some(kf) = sp.keyframes.get(pref.key_idx) { | |
| 61 | let pt = match pref.part { | |
| 62 | HandlePart::Key => [kf.time, kf.value], | |
| 63 | HandlePart::LeftHandle => kf.lh.unwrap_or([kf.time, kf.value]), | |
| 64 | HandlePart::RightHandle => kf.rh.unwrap_or([kf.time, kf.value]), | |
| 65 | }; | |
| 66 | let piv_pt = if self.selected.len() == 1 && pref.part != HandlePart::Key { | |
| 67 | [kf.time, kf.value] | |
| 68 | } else { | |
| 69 | pt | |
| 70 | }; | |
| 71 | sum_t += piv_pt[0]; | |
| 72 | sum_v += value_to_display(piv_pt[1], bounds, self.normalized); | |
| 73 | count += 1; | |
| 74 | } | |
| 75 | } | |
| 76 | } | |
| 77 | if count == 0 { | |
| 78 | return; | |
| 79 | } | |
| 80 | self.modal = Some(ModalEdit { | |
| 81 | mode, | |
| 82 | axis: AxisLock::None, | |
| 83 | pivot: [sum_t / count as f64, sum_v / count as f64], | |
| 84 | start_pointer: self.local_to_data(self.pointer_local), | |
| 85 | saved_splines: saved, | |
| 86 | orig_selected: self.selected.clone(), | |
| 87 | saved_bounds, | |
| 88 | pre_edit_snapshot: capture_history.then(|| comp.clone()), | |
| 89 | }); | |
| 90 | } | |
| 91 | ||
| 92 | pub(super) fn apply_modal(&mut self, comp: &mut Comp, fine: bool) -> bool { | |
| 93 | let Some(m) = &self.modal else { return false }; | |
| 94 | let cur = self.local_to_data(self.pointer_local); | |
| 95 | let dt = cur[0] - m.start_pointer[0]; | |
| 96 | let dv_disp = cur[1] - m.start_pointer[1]; | |
| 97 | let piv = m.pivot; | |
| 98 | let normalized = self.normalized; | |
| 99 | ||
| 100 | for (sname, orig_kfs) in &m.saved_splines { | |
| 101 | if let Some(sp) = comp.splines.get_mut(sname) { | |
| 102 | sp.keyframes = orig_kfs.clone(); | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | // A selected key carries its handles, so a handle also in the selection must not move twice. | |
| 107 | let selected = &m.orig_selected; | |
| 108 | let moves = |pref: &PointRef| { | |
| 109 | pref.part == HandlePart::Key | |
| 110 | || !selected.contains(&PointRef { spline: pref.spline.clone(), key_idx: pref.key_idx, part: HandlePart::Key }) | |
| 111 | }; | |
| 112 | ||
| 113 | let snap_t = |t: f64| if fine { t } else { t.round() }; | |
| 114 | ||
| 115 | // Helper: convert Auto/Vector handles to Aligned/Free when manually dragged (Blender behavior), | |
| 116 | // and mirror opposite handle if Aligned (and opposite handle isn't also explicitly selected). | |
| 117 | let finalize_handle_drag = |kf: &mut Keyframe, pref: &PointRef, selected: &HashSet<PointRef>| { | |
| 118 | kf.flags.linear = false; | |
| 119 | match kf.handle_mode { | |
| 120 | HandleMode::Auto => kf.handle_mode = HandleMode::Aligned, | |
| 121 | HandleMode::Vector => kf.handle_mode = HandleMode::Free, | |
| 122 | HandleMode::Aligned | HandleMode::Free => {} | |
| 123 | } | |
| 124 | if kf.handle_mode == HandleMode::Aligned { | |
| 125 | let opp_part = match pref.part { | |
| 126 | HandlePart::LeftHandle => HandlePart::RightHandle, | |
| 127 | HandlePart::RightHandle => HandlePart::LeftHandle, | |
| 128 | HandlePart::Key => return, | |
| 129 | }; | |
| 130 | let opp_selected = selected.contains(&PointRef { | |
| 131 | spline: pref.spline.clone(), | |
| 132 | key_idx: pref.key_idx, | |
| 133 | part: opp_part, | |
| 134 | }); | |
| 135 | if !opp_selected { | |
| 136 | enforce_aligned_handle(kf, pref.part); | |
| 137 | } | |
| 138 | } | |
| 139 | }; | |
| 140 | ||
| 141 | match m.mode { | |
| 142 | TransformMode::Grab => { | |
| 143 | let (mt, mv_disp) = match m.axis { | |
| 144 | AxisLock::None => (dt, dv_disp), | |
| 145 | AxisLock::X => (dt, 0.0), | |
| 146 | AxisLock::Y => (0.0, dv_disp), | |
| 147 | }; | |
| 148 | for pref in selected.iter().filter(|p| moves(p)) { | |
| 149 | let Some(sp) = comp.splines.get_mut(&pref.spline) else { continue }; | |
| 150 | let bounds = m.saved_bounds.get(&pref.spline).copied().unwrap_or((0.0, 1.0)); | |
| 151 | let mv = if normalized { | |
| 152 | mv_disp * (bounds.1 - bounds.0).max(1e-9) | |
| 153 | } else { | |
| 154 | mv_disp | |
| 155 | }; | |
| 156 | let Some(kf) = sp.keyframes.get_mut(pref.key_idx) else { continue }; | |
| 157 | match pref.part { | |
| 158 | HandlePart::Key => { | |
| 159 | let new_t = snap_t(kf.time + mt); | |
| 160 | let actual_dt = new_t - kf.time; | |
| 161 | kf.time = new_t; | |
| 162 | kf.value += mv; | |
| 163 | if let Some(lh) = &mut kf.lh { | |
| 164 | lh[0] += actual_dt; | |
| 165 | lh[1] += mv; | |
| 166 | } | |
| 167 | if let Some(rh) = &mut kf.rh { | |
| 168 | rh[0] += actual_dt; | |
| 169 | rh[1] += mv; | |
| 170 | } | |
| 171 | } | |
| 172 | HandlePart::LeftHandle => { | |
| 173 | let mut h = kf.lh.unwrap_or([kf.time - 5.0, kf.value]); | |
| 174 | h[0] = (h[0] + mt).min(kf.time - 0.01); | |
| 175 | h[1] += mv; | |
| 176 | kf.lh = Some(h); | |
| 177 | finalize_handle_drag(kf, pref, selected); | |
| 178 | } | |
| 179 | HandlePart::RightHandle => { | |
| 180 | let mut h = kf.rh.unwrap_or([kf.time + 5.0, kf.value]); | |
| 181 | h[0] = (h[0] + mt).max(kf.time + 0.01); | |
| 182 | h[1] += mv; | |
| 183 | kf.rh = Some(h); | |
| 184 | finalize_handle_drag(kf, pref, selected); | |
| 185 | } | |
| 186 | } | |
| 187 | } | |
| 188 | } | |
| 189 | TransformMode::Scale => { | |
| 190 | let s0 = self.data_to_local(m.start_pointer[0], m.start_pointer[1]); | |
| 191 | let s1 = self.pointer_local; | |
| 192 | let sp_px = self.data_to_local(piv[0], piv[1]); | |
| 193 | let d0 = hypot2(s0, sp_px).max(10.0); | |
| 194 | let d1 = hypot2(s1, sp_px); | |
| 195 | let f = f64::from((d1 / d0).clamp(0.05, 10.0)); | |
| 196 | let (st, sv) = match m.axis { | |
| 197 | AxisLock::None => (f, f), | |
| 198 | AxisLock::X => (f, 1.0), | |
| 199 | AxisLock::Y => (1.0, f), | |
| 200 | }; | |
| 201 | for pref in selected.iter().filter(|p| moves(p)) { | |
| 202 | let Some(sp) = comp.splines.get_mut(&pref.spline) else { continue }; | |
| 203 | let bounds = m.saved_bounds.get(&pref.spline).copied().unwrap_or((0.0, 1.0)); | |
| 204 | let piv_val = display_to_value(piv[1], bounds, normalized); | |
| 205 | let Some(kf) = sp.keyframes.get_mut(pref.key_idx) else { continue }; | |
| 206 | match pref.part { | |
| 207 | HandlePart::Key => { | |
| 208 | let raw_t = piv[0] + (kf.time - piv[0]) * st; | |
| 209 | kf.time = snap_t(raw_t); | |
| 210 | let snap = kf.time - raw_t; | |
| 211 | kf.value = piv_val + (kf.value - piv_val) * sv; | |
| 212 | if let Some(lh) = &mut kf.lh { | |
| 213 | lh[0] = (piv[0] + (lh[0] - piv[0]) * st + snap).min(kf.time - 0.01); | |
| 214 | lh[1] = piv_val + (lh[1] - piv_val) * sv; | |
| 215 | } | |
| 216 | if let Some(rh) = &mut kf.rh { | |
| 217 | rh[0] = (piv[0] + (rh[0] - piv[0]) * st + snap).max(kf.time + 0.01); | |
| 218 | rh[1] = piv_val + (rh[1] - piv_val) * sv; | |
| 219 | } | |
| 220 | } | |
| 221 | HandlePart::LeftHandle => { | |
| 222 | if let Some(lh) = &mut kf.lh { | |
| 223 | lh[0] = (kf.time + (lh[0] - kf.time) * st).min(kf.time - 0.01); | |
| 224 | lh[1] = kf.value + (lh[1] - kf.value) * sv; | |
| 225 | } | |
| 226 | finalize_handle_drag(kf, pref, selected); | |
| 227 | } | |
| 228 | HandlePart::RightHandle => { | |
| 229 | if let Some(rh) = &mut kf.rh { | |
| 230 | rh[0] = (kf.time + (rh[0] - kf.time) * st).max(kf.time + 0.01); | |
| 231 | rh[1] = kf.value + (rh[1] - kf.value) * sv; | |
| 232 | } | |
| 233 | finalize_handle_drag(kf, pref, selected); | |
| 234 | } | |
| 235 | } | |
| 236 | } | |
| 237 | } | |
| 238 | TransformMode::Rotate => { | |
| 239 | let sp_px = self.data_to_local(piv[0], piv[1]); | |
| 240 | let s0_px = self.data_to_local(m.start_pointer[0], m.start_pointer[1]); | |
| 241 | let a0 = (s0_px[1] - sp_px[1]).atan2(s0_px[0] - sp_px[0]); | |
| 242 | let a1 = (self.pointer_local[1] - sp_px[1]).atan2(self.pointer_local[0] - sp_px[0]); | |
| 243 | let da = a1 - a0; | |
| 244 | let (c, s) = (da.cos(), da.sin()); | |
| 245 | ||
| 246 | let rot = |pt: [f64; 2], center_px: [f32; 2], bounds: (f64, f64)| -> [f64; 2] { | |
| 247 | let p = self.spline_to_local(pt[0], pt[1], bounds); | |
| 248 | let rx = p[0] - center_px[0]; | |
| 249 | let ry = p[1] - center_px[1]; | |
| 250 | let np = [center_px[0] + rx * c - ry * s, center_px[1] + rx * s + ry * c]; | |
| 251 | let disp = self.local_to_data(np); | |
| 252 | [disp[0], display_to_value(disp[1], bounds, normalized)] | |
| 253 | }; | |
| 254 | ||
| 255 | for pref in selected.iter().filter(|p| moves(p)) { | |
| 256 | let Some(sp) = comp.splines.get_mut(&pref.spline) else { continue }; | |
| 257 | let bounds = m.saved_bounds.get(&pref.spline).copied().unwrap_or((0.0, 1.0)); | |
| 258 | let Some(kf) = sp.keyframes.get_mut(pref.key_idx) else { continue }; | |
| 259 | match pref.part { | |
| 260 | HandlePart::Key => { | |
| 261 | if selected.len() == 1 { | |
| 262 | let kp = self.spline_to_local(kf.time, kf.value, bounds); | |
| 263 | ensure_handles(kf); | |
| 264 | if let Some(lh) = kf.lh { | |
| 265 | let nl = rot(lh, kp, bounds); | |
| 266 | kf.lh = Some([nl[0].min(kf.time - 0.01), nl[1]]); | |
| 267 | } | |
| 268 | if let Some(rh) = kf.rh { | |
| 269 | let nr = rot(rh, kp, bounds); | |
| 270 | kf.rh = Some([nr[0].max(kf.time + 0.01), nr[1]]); | |
| 271 | } | |
| 272 | kf.flags.linear = false; | |
| 273 | } else { | |
| 274 | let nk = rot([kf.time, kf.value], sp_px, bounds); | |
| 275 | let lh = kf.lh.map(|h| rot(h, sp_px, bounds)); | |
| 276 | let rh = kf.rh.map(|h| rot(h, sp_px, bounds)); | |
| 277 | kf.time = snap_t(nk[0]); | |
| 278 | let snap = kf.time - nk[0]; | |
| 279 | kf.value = nk[1]; | |
| 280 | kf.lh = lh.map(|h| [(h[0] + snap).min(kf.time - 0.01), h[1]]); | |
| 281 | kf.rh = rh.map(|h| [(h[0] + snap).max(kf.time + 0.01), h[1]]); | |
| 282 | } | |
| 283 | } | |
| 284 | HandlePart::LeftHandle => { | |
| 285 | let kp = self.spline_to_local(kf.time, kf.value, bounds); | |
| 286 | if let Some(lh) = kf.lh { | |
| 287 | let nl = rot(lh, kp, bounds); | |
| 288 | kf.lh = Some([nl[0].min(kf.time - 0.01), nl[1]]); | |
| 289 | finalize_handle_drag(kf, pref, selected); | |
| 290 | } | |
| 291 | } | |
| 292 | HandlePart::RightHandle => { | |
| 293 | let kp = self.spline_to_local(kf.time, kf.value, bounds); | |
| 294 | if let Some(rh) = kf.rh { | |
| 295 | let nr = rot(rh, kp, bounds); | |
| 296 | kf.rh = Some([nr[0].max(kf.time + 0.01), nr[1]]); | |
| 297 | finalize_handle_drag(kf, pref, selected); | |
| 298 | } | |
| 299 | } | |
| 300 | } | |
| 301 | } | |
| 302 | } | |
| 303 | } | |
| 304 | // Keep curves sorted while dragging; selection follows its keys to their new slots. | |
| 305 | let remaps: HashMap<&String, Vec<usize>> = m | |
| 306 | .saved_splines | |
| 307 | .keys() | |
| 308 | .filter_map(|sname| Some((sname, sort_keyframes(comp.splines.get_mut(sname)?)))) | |
| 309 | .collect(); | |
| 310 | let new_selected: HashSet<PointRef> = selected | |
| 311 | .iter() | |
| 312 | .map(|p| PointRef { | |
| 313 | key_idx: remaps.get(&p.spline).and_then(|r| r.get(p.key_idx)).copied().unwrap_or(p.key_idx), | |
| 314 | ..p.clone() | |
| 315 | }) | |
| 316 | .collect(); | |
| 317 | self.selected = new_selected; | |
| 318 | comp.revision += 1; | |
| 319 | true | |
| 320 | } | |
| 321 | ||
| 322 | pub(super) fn cancel_modal(&mut self, comp: &mut Comp) { | |
| 323 | if let Some(m) = self.modal.take() { | |
| 324 | for (sname, orig) in m.saved_splines { | |
| 325 | if let Some(sp) = comp.splines.get_mut(&sname) { | |
| 326 | sp.keyframes = orig; | |
| 327 | } | |
| 328 | } | |
| 329 | self.selected = m.orig_selected; | |
| 330 | comp.revision += 1; | |
| 331 | } | |
| 332 | } | |
| 333 | ||
| 334 | pub(super) fn confirm_modal(&mut self, comp: &mut Comp, history: Option<&mut CompHistory>) { | |
| 335 | if let Some(m) = self.modal.take() { | |
| 336 | comp.revision += 1; | |
| 337 | if let (Some(h), Some(snap)) = (history, m.pre_edit_snapshot) { | |
| 338 | let label = match m.mode { | |
| 339 | TransformMode::Grab => "Grab Keyframes", | |
| 340 | TransformMode::Rotate => "Rotate Keyframes", | |
| 341 | TransformMode::Scale => "Scale Keyframes", | |
| 342 | }; | |
| 343 | h.push_snapshot(&snap, label); | |
| 344 | } | |
| 345 | } | |
| 346 | } | |
| 347 | } | |
| 348 | ||
| 349 | /// Sorts `sp.keyframes` by time (stable) and returns each old index's new index. | |
| 350 | pub fn sort_keyframes(sp: &mut BezierSpline) -> Vec<usize> { | |
| 351 | let mut order: Vec<usize> = (0..sp.keyframes.len()).collect(); | |
| 352 | order.sort_by(|&a, &b| sp.keyframes[a].time.total_cmp(&sp.keyframes[b].time)); | |
| 353 | let mut new_index = vec![0; order.len()]; | |
| 354 | for (new, &old) in order.iter().enumerate() { | |
| 355 | new_index[old] = new; | |
| 356 | } | |
| 357 | sp.keyframes = order.iter().map(|&old| sp.keyframes[old].clone()).collect(); | |
| 358 | new_index | |
| 359 | } | |
| 360 | ||
| 361 | pub(super) fn ensure_handles(kf: &mut Keyframe) { | |
| 362 | if kf.lh.is_none() { | |
| 363 | kf.lh = Some([kf.time - 8.0, kf.value]); | |
| 364 | } | |
| 365 | if kf.rh.is_none() { | |
| 366 | kf.rh = Some([kf.time + 8.0, kf.value]); | |
| 367 | } | |
| 368 | } | |
| 369 | ||
| 370 | pub(super) fn hypot2(a: [f32; 2], b: [f32; 2]) -> f32 { | |
| 371 | ((a[0] - b[0]).powi(2) + (a[1] - b[1]).powi(2)).sqrt() | |
| 372 | } | |
| 373 | ||
| 374 | #[cfg(test)] | |
| 375 | mod tests { | |
| 376 | use super::*; | |
| 377 | use crate::tests::{key_ref, test_comp}; | |
| 378 | ||
| 379 | #[test] | |
| 380 | fn dragging_a_key_past_its_neighbour_keeps_curve_sorted_and_selection_on_the_key() { | |
| 381 | let mut comp = test_comp(); | |
| 382 | let mut view = SplineView::new(&ui::Theme::dark()); | |
| 383 | view.resize([800.0, 360.0]); | |
| 384 | view.set_target_node(&comp, None); | |
| 385 | view.selected.insert(key_ref(0, HandlePart::Key)); | |
| 386 | ||
| 387 | view.pointer_local = view.spline_to_local(0.0, 10.0, (10.0, 110.0)); | |
| 388 | view.begin_modal(&comp, false, TransformMode::Grab); | |
| 389 | view.pointer_local = view.spline_to_local(70.0, 10.0, (10.0, 110.0)); | |
| 390 | assert!(view.apply_modal(&mut comp, false)); | |
| 391 | ||
| 392 | let times: Vec<f64> = comp.splines["Gain"].keyframes.iter().map(|k| k.time).collect(); | |
| 393 | assert_eq!(times, [50.0, 70.0, 100.0]); | |
| 394 | assert_eq!(view.selected, HashSet::from([key_ref(1, HandlePart::Key)])); | |
| 395 | ||
| 396 | // Cancel puts both the keys and the selection back. | |
| 397 | view.cancel_modal(&mut comp); | |
| 398 | assert_eq!(comp.splines["Gain"].keyframes[0].time, 0.0); | |
| 399 | assert_eq!(view.selected, HashSet::from([key_ref(0, HandlePart::Key)])); | |
| 400 | } | |
| 401 | ||
| 402 | #[test] | |
| 403 | fn grabbing_a_key_and_its_own_handle_moves_the_handle_once() { | |
| 404 | let mut comp = test_comp(); | |
| 405 | comp.splines.get_mut("Gain").unwrap().keyframes[1].lh = Some([40.0, 110.0]); | |
| 406 | let mut view = SplineView::new(&ui::Theme::dark()); | |
| 407 | view.resize([800.0, 360.0]); | |
| 408 | view.set_target_node(&comp, None); | |
| 409 | view.selected.insert(key_ref(1, HandlePart::Key)); | |
| 410 | view.selected.insert(key_ref(1, HandlePart::LeftHandle)); | |
| 411 | ||
| 412 | view.pointer_local = view.spline_to_local(50.0, 110.0, (10.0, 110.0)); | |
| 413 | view.begin_modal(&comp, false, TransformMode::Grab); | |
| 414 | view.pointer_local = view.spline_to_local(60.0, 110.0, (10.0, 110.0)); | |
| 415 | view.apply_modal(&mut comp, false); | |
| 416 | ||
| 417 | let kf = &comp.splines["Gain"].keyframes[1]; | |
| 418 | assert!((kf.time - 60.0).abs() < 1e-6); | |
| 419 | assert!((kf.lh.unwrap()[0] - 50.0).abs() < 1e-6, "handle shifted {:?}", kf.lh); | |
| 420 | } | |
| 421 | ||
| 422 | #[test] | |
| 423 | fn multi_key_rotate_carries_handles_with_their_keys() { | |
| 424 | let mut comp = test_comp(); | |
| 425 | { | |
| 426 | let kfs = &mut comp.splines.get_mut("Gain").unwrap().keyframes; | |
| 427 | kfs[0].lh = Some([-10.0, 10.0]); | |
| 428 | kfs[0].rh = Some([10.0, 10.0]); | |
| 429 | kfs[1].lh = Some([40.0, 110.0]); | |
| 430 | kfs[1].rh = Some([60.0, 110.0]); | |
| 431 | } | |
| 432 | let orig = comp.splines["Gain"].keyframes.clone(); | |
| 433 | let mut view = SplineView::new(&ui::Theme::dark()); | |
| 434 | view.resize([800.0, 360.0]); | |
| 435 | view.set_target_node(&comp, None); | |
| 436 | view.selected.insert(key_ref(0, HandlePart::Key)); | |
| 437 | view.selected.insert(key_ref(1, HandlePart::Key)); | |
| 438 | ||
| 439 | let bounds = (10.0, 110.0); | |
| 440 | let pivot_data = [25.0, 60.0]; | |
| 441 | let piv = view.spline_to_local(pivot_data[0], pivot_data[1], bounds); | |
| 442 | view.pointer_local = [piv[0] + 100.0, piv[1]]; | |
| 443 | view.begin_modal(&comp, false, TransformMode::Rotate); | |
| 444 | let theta = 0.1_f32; | |
| 445 | view.pointer_local = [piv[0] + 100.0 * theta.cos(), piv[1] + 100.0 * theta.sin()]; | |
| 446 | assert!(view.apply_modal(&mut comp, true)); | |
| 447 | ||
| 448 | let rotated = |pt: [f64; 2]| { | |
| 449 | let p = view.spline_to_local(pt[0], pt[1], bounds); | |
| 450 | let (rx, ry) = (p[0] - piv[0], p[1] - piv[1]); | |
| 451 | let np = [piv[0] + rx * theta.cos() - ry * theta.sin(), piv[1] + rx * theta.sin() + ry * theta.cos()]; | |
| 452 | let d = view.local_to_data(np); | |
| 453 | [d[0], display_to_value(d[1], bounds, false)] | |
| 454 | }; | |
| 455 | let kfs = &comp.splines["Gain"].keyframes; | |
| 456 | for (i, o) in orig.iter().take(2).enumerate() { | |
| 457 | for (got, want) in [(kfs[i].lh, o.lh), (kfs[i].rh, o.rh)] { | |
| 458 | let want = rotated(want.unwrap()); | |
| 459 | let got = got.unwrap(); | |
| 460 | assert!((got[0] - want[0]).abs() < 1e-2 && (got[1] - want[1]).abs() < 1e-2, "key {i}: {got:?} vs {want:?}"); | |
| 461 | } | |
| 462 | } | |
| 463 | } | |
| 464 | } |