| author | |
| committer | |
| log | df2e366bf9ed831fec49c63c19a3f1efa4e24550 |
| tree | abf2ee5d53ff98e7a2dbbaccf1db267b54c062d8 |
| parent | a229beeb6bba6ba0d0daf711e287bf2b7052b21c |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
2 files changed, 600 insertions(+), 562 deletions(-)
crates/spline/src/lib.rs+1-562| ... | ... | @@ -8,19 +8,17 @@ |
| 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 paint; | |
| 11 | 12 | pub mod presets; |
| 12 | 13 | |
| 13 | 14 | pub use presets::EasingPreset; |
| 14 | 15 | |
| 15 | use draw::{Glyphs, Primitive}; | |
| 16 | use model::spline::{cubic, segment_control_points}; | |
| 17 | 16 | use model::{Comp, CompHistory, HandleMode, Keyframe, NodeId}; |
| 18 | 17 | use presets::{ |
| 19 | 18 | apply_easing_preset, apply_handle_mode, compute_curve_bounds, display_to_value, |
| 20 | 19 | enforce_aligned_handle, value_to_display, |
| 21 | 20 | }; |
| 22 | 21 | use std::collections::{HashMap, HashSet}; |
| 23 | use std::sync::Arc; | |
| 24 | 22 | use winit::event::{ElementState, MouseButton}; |
| 25 | 23 | use winit::keyboard::{Key, KeyCode, ModifiersState, NamedKey, PhysicalKey}; |
| 26 | 24 | |
| ... | ... | @@ -1271,540 +1269,6 @@ impl SplineView { |
| 1271 | 1269 | self.v_range = [v0, v1]; |
| 1272 | 1270 | } |
| 1273 | 1271 | } |
| 1274 | ||
| 1275 | fn hover_readout_string(&self, comp: &Comp) -> Option<String> { | |
| 1276 | let hov = self.hovered.as_ref()?; | |
| 1277 | let sp = comp.splines.get(&hov.spline)?; | |
| 1278 | let kf = sp.keyframes.get(hov.key_idx)?; | |
| 1279 | let (t, v) = match hov.part { | |
| 1280 | HandlePart::Key => (kf.time, kf.value), | |
| 1281 | HandlePart::LeftHandle => { | |
| 1282 | let h = kf.lh?; | |
| 1283 | (h[0], h[1]) | |
| 1284 | } | |
| 1285 | HandlePart::RightHandle => { | |
| 1286 | let h = kf.rh?; | |
| 1287 | (h[0], h[1]) | |
| 1288 | } | |
| 1289 | }; | |
| 1290 | if (t - t.round()).abs() < 1e-3 { | |
| 1291 | Some(format!("(f{}, {:.3})", t.round() as i64, v)) | |
| 1292 | } else { | |
| 1293 | Some(format!("(f{t:.1}, {v:.3})")) | |
| 1294 | } | |
| 1295 | } | |
| 1296 | ||
| 1297 | pub fn prepare_labels(&mut self, comp: &Comp) { | |
| 1298 | let font_sm = text::Font { | |
| 1299 | families: Arc::from([text::Family::Mono]), | |
| 1300 | size: 10.0, | |
| 1301 | weight: 500, | |
| 1302 | italic: false, | |
| 1303 | }; | |
| 1304 | let font_ch = text::Font { | |
| 1305 | families: Arc::from([text::Family::Sans]), | |
| 1306 | size: 11.0, | |
| 1307 | weight: 500, | |
| 1308 | italic: false, | |
| 1309 | }; | |
| 1310 | let make_layout = |s: &str, font: &text::Font| { | |
| 1311 | let styles = [text::Style::new(font.clone())]; | |
| 1312 | text::Paragraph { | |
| 1313 | text: s, | |
| 1314 | styles: &styles, | |
| 1315 | spans: &[], | |
| 1316 | replacements: &[], | |
| 1317 | width: None, | |
| 1318 | indent: 0.0, | |
| 1319 | line_height: 1.2, | |
| 1320 | align: text::Align::Left, | |
| 1321 | tab: 32.0, | |
| 1322 | } | |
| 1323 | .layout() | |
| 1324 | }; | |
| 1325 | for (label, _) in &self.channels { | |
| 1326 | self.text_cache.entry(label.clone()).or_insert_with(|| make_layout(label, &font_ch)); | |
| 1327 | } | |
| 1328 | let header = self | |
| 1329 | .current_node | |
| 1330 | .map(|id| format!("Splines: {}", comp.node(id).name)) | |
| 1331 | .unwrap_or_else(|| "Splines (Comp)".to_owned()); | |
| 1332 | self.text_cache.entry(header.clone()).or_insert_with(|| make_layout(&header, &font_ch)); | |
| 1333 | self.text_cache | |
| 1334 | .entry("Norm".to_owned()) | |
| 1335 | .or_insert_with(|| make_layout("Norm", &font_sm)); | |
| 1336 | for popup_hdr in ["Set Handle Type", "Set Interpolation"] { | |
| 1337 | self.text_cache | |
| 1338 | .entry(popup_hdr.to_owned()) | |
| 1339 | .or_insert_with(|| make_layout(popup_hdr, &font_sm)); | |
| 1340 | } | |
| 1341 | for &(_, item_lbl) in HANDLE_MENU_ITEMS { | |
| 1342 | self.text_cache | |
| 1343 | .entry(item_lbl.to_owned()) | |
| 1344 | .or_insert_with(|| make_layout(item_lbl, &font_ch)); | |
| 1345 | } | |
| 1346 | for &(_, item_lbl) in EASING_MENU_ITEMS { | |
| 1347 | self.text_cache | |
| 1348 | .entry(item_lbl.to_owned()) | |
| 1349 | .or_insert_with(|| make_layout(item_lbl, &font_ch)); | |
| 1350 | } | |
| 1351 | for hud in [ | |
| 1352 | "Drag or G/R/S X/Y: Axis V: Handle T: Easing N: Norm I: Insert X/Del: Delete L: Linear F: Fit", | |
| 1353 | "GRAB [LMB/Enter confirm, RMB/Esc cancel, X: Frame / Y: Value]", | |
| 1354 | "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", | |
| 1355 | "SCALE [LMB/Enter confirm, RMB/Esc cancel, X: Time / Y: Value]", | |
| 1356 | ] { | |
| 1357 | self.text_cache.entry(hud.to_owned()).or_insert_with(|| make_layout(hud, &font_sm)); | |
| 1358 | } | |
| 1359 | ||
| 1360 | if let Some(readout) = self.hover_readout_string(comp) { | |
| 1361 | if readout != self.last_hover_readout || !self.text_cache.contains_key("__hover_readout") { | |
| 1362 | self.text_cache | |
| 1363 | .insert("__hover_readout".to_owned(), make_layout(&readout, &font_sm)); | |
| 1364 | self.last_hover_readout = readout; | |
| 1365 | } | |
| 1366 | } | |
| 1367 | ||
| 1368 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / 8.0); | |
| 1369 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 1370 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 1371 | for ti in t_start..=t_end { | |
| 1372 | let key = format!("t:{}", (ti as f64 * t_step).round() as i64); | |
| 1373 | self.text_cache.entry(key.clone()).or_insert_with(|| make_layout(&key[2..], &font_sm)); | |
| 1374 | } | |
| 1375 | let v_step = nice_step((self.v_range[1] - self.v_range[0]) / 6.0); | |
| 1376 | let v_start = (self.v_range[0] / v_step).floor() as i64; | |
| 1377 | let v_end = (self.v_range[1] / v_step).ceil() as i64; | |
| 1378 | for vi in v_start..=v_end { | |
| 1379 | let val = vi as f64 * v_step; | |
| 1380 | let s = format!("{val:.2}"); | |
| 1381 | let key = format!("v:{s}"); | |
| 1382 | self.text_cache.entry(key).or_insert_with(|| make_layout(&s, &font_sm)); | |
| 1383 | } | |
| 1384 | } | |
| 1385 | ||
| 1386 | pub fn paint<'a>(&'a self, comp: &'a Comp, current_frame: f64) -> Vec<Primitive<'a>> { | |
| 1387 | let mut out = Vec::with_capacity(512); | |
| 1388 | let [w, h] = self.size; | |
| 1389 | let plot = self.plot_rect(); | |
| 1390 | ||
| 1391 | out.push(Primitive::Rect { rect: [0.0, 0.0, SIDEBAR_W, h], color: self.palette.sidebar }); | |
| 1392 | out.push(Primitive::Segment { from: [SIDEBAR_W, 0.0], to: [SIDEBAR_W, h], width: 1.0, color: self.palette.rule }); | |
| 1393 | out.push(Primitive::Rect { rect: [SIDEBAR_W, 0.0, w, RULER_H], color: self.palette.ruler }); | |
| 1394 | out.push(Primitive::Segment { from: [0.0, RULER_H], to: [w, RULER_H], width: 1.0, color: self.palette.rule }); | |
| 1395 | ||
| 1396 | let header = self | |
| 1397 | .current_node | |
| 1398 | .map(|id| format!("Splines: {}", comp.node(id).name)) | |
| 1399 | .unwrap_or_else(|| "Splines (Comp)".to_owned()); | |
| 1400 | if let Some(hl) = self.text_cache.get(&header) { | |
| 1401 | out.push(Primitive::Text { | |
| 1402 | text: hl as &dyn Glyphs, | |
| 1403 | origin: [10.0, 5.0], | |
| 1404 | clip: Some([8.0, 0.0, SIDEBAR_W - 70.0, RULER_H]), | |
| 1405 | ink: self.palette.text, | |
| 1406 | }); | |
| 1407 | } | |
| 1408 | ||
| 1409 | // Clickable "Normalize" pill in the sidebar header | |
| 1410 | let pill = self.normalize_pill_rect(); | |
| 1411 | let pill_fill = if self.normalized { | |
| 1412 | [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.28] | |
| 1413 | } else { | |
| 1414 | [self.palette.bg[0], self.palette.bg[1], self.palette.bg[2], 0.9] | |
| 1415 | }; | |
| 1416 | let pill_stroke = if self.normalized { self.palette.accent } else { self.palette.rule }; | |
| 1417 | out.push(Primitive::RoundedRect { | |
| 1418 | rect: pill, | |
| 1419 | radius: 4.0, | |
| 1420 | stroke: None, | |
| 1421 | color: pill_fill, | |
| 1422 | }); | |
| 1423 | out.push(Primitive::RoundedRect { | |
| 1424 | rect: pill, | |
| 1425 | radius: 4.0, | |
| 1426 | stroke: Some(1.0), | |
| 1427 | color: pill_stroke, | |
| 1428 | }); | |
| 1429 | if let Some(nl) = self.text_cache.get("Norm") { | |
| 1430 | out.push(Primitive::Text { | |
| 1431 | text: nl as &dyn Glyphs, | |
| 1432 | origin: [pill[0] + 7.0, pill[1] + 3.0], | |
| 1433 | clip: Some(pill), | |
| 1434 | ink: if self.normalized { self.palette.text } else { self.palette.text_dim }, | |
| 1435 | }); | |
| 1436 | } | |
| 1437 | ||
| 1438 | for (i, (label, sname)) in self.channels.iter().enumerate() { | |
| 1439 | let y = RULER_H + 6.0 + i as f32 * 22.0; | |
| 1440 | if y > h - 24.0 { | |
| 1441 | break; | |
| 1442 | } | |
| 1443 | let hidden = self.hidden_splines.contains(sname); | |
| 1444 | let col = spline_color(comp, sname, i); | |
| 1445 | out.push(Primitive::RoundedRect { | |
| 1446 | rect: [10.0, y + 4.0, 20.0, y + 14.0], | |
| 1447 | radius: 3.0, | |
| 1448 | stroke: if hidden { Some(1.2) } else { None }, | |
| 1449 | color: if hidden { self.palette.text_dim } else { col }, | |
| 1450 | }); | |
| 1451 | if let Some(tl) = self.text_cache.get(label) { | |
| 1452 | out.push(Primitive::Text { | |
| 1453 | text: tl as &dyn Glyphs, | |
| 1454 | origin: [26.0, y + 2.0], | |
| 1455 | clip: Some([26.0, y, SIDEBAR_W - 8.0, y + 20.0]), | |
| 1456 | ink: if hidden { self.palette.text_dim } else { self.palette.text }, | |
| 1457 | }); | |
| 1458 | } | |
| 1459 | } | |
| 1460 | ||
| 1461 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / 8.0); | |
| 1462 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 1463 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 1464 | for ti in t_start..=t_end { | |
| 1465 | let t = ti as f64 * t_step; | |
| 1466 | let x = self.data_to_local(t, 0.0)[0]; | |
| 1467 | if x < plot[0] || x > plot[2] { | |
| 1468 | continue; | |
| 1469 | } | |
| 1470 | out.push(Primitive::Segment { from: [x, plot[1]], to: [x, plot[3]], width: 1.0, color: self.palette.grid }); | |
| 1471 | let key = format!("t:{}", t.round() as i64); | |
| 1472 | if let Some(tl) = self.text_cache.get(&key) { | |
| 1473 | out.push(Primitive::Text { | |
| 1474 | text: tl as &dyn Glyphs, | |
| 1475 | origin: [x + 3.0, 6.0], | |
| 1476 | clip: Some([plot[0], 0.0, plot[2], RULER_H]), | |
| 1477 | ink: self.palette.text_dim, | |
| 1478 | }); | |
| 1479 | } | |
| 1480 | } | |
| 1481 | ||
| 1482 | let v_step = nice_step((self.v_range[1] - self.v_range[0]) / 6.0); | |
| 1483 | let v_start = (self.v_range[0] / v_step).floor() as i64; | |
| 1484 | let v_end = (self.v_range[1] / v_step).ceil() as i64; | |
| 1485 | for vi in v_start..=v_end { | |
| 1486 | let v = vi as f64 * v_step; | |
| 1487 | let y = self.data_to_local(0.0, v)[1]; | |
| 1488 | if y < plot[1] || y > plot[3] { | |
| 1489 | continue; | |
| 1490 | } | |
| 1491 | let is_zero = v.abs() < 1e-6; | |
| 1492 | out.push(Primitive::Segment { | |
| 1493 | from: [plot[0], y], | |
| 1494 | to: [plot[2], y], | |
| 1495 | width: if is_zero { 1.3 } else { 1.0 }, | |
| 1496 | color: if is_zero { self.palette.zero_line } else { self.palette.grid }, | |
| 1497 | }); | |
| 1498 | let key = format!("v:{v:.2}"); | |
| 1499 | if let Some(vl) = self.text_cache.get(&key) { | |
| 1500 | out.push(Primitive::Text { | |
| 1501 | text: vl as &dyn Glyphs, | |
| 1502 | origin: [plot[0] + 4.0, y - 12.0], | |
| 1503 | clip: Some(plot), | |
| 1504 | ink: self.palette.text_dim, | |
| 1505 | }); | |
| 1506 | } | |
| 1507 | } | |
| 1508 | ||
| 1509 | out.push(Primitive::PushClip { rect: plot, radius: 0.0 }); | |
| 1510 | ||
| 1511 | if let Some(m) = &self.modal { | |
| 1512 | let piv = self.data_to_local(m.pivot[0], m.pivot[1]); | |
| 1513 | match m.axis { | |
| 1514 | AxisLock::X => out.push(Primitive::Segment { | |
| 1515 | from: [plot[0], piv[1]], | |
| 1516 | to: [plot[2], piv[1]], | |
| 1517 | width: 1.5, | |
| 1518 | color: [0.95, 0.28, 0.34, 0.85], | |
| 1519 | }), | |
| 1520 | AxisLock::Y => out.push(Primitive::Segment { | |
| 1521 | from: [piv[0], plot[1]], | |
| 1522 | to: [piv[0], plot[3]], | |
| 1523 | width: 1.5, | |
| 1524 | color: [0.28, 0.90, 0.42, 0.85], | |
| 1525 | }), | |
| 1526 | AxisLock::None => {} | |
| 1527 | } | |
| 1528 | } | |
| 1529 | ||
| 1530 | let px = self.data_to_local(current_frame, 0.0)[0]; | |
| 1531 | if px >= plot[0] && px <= plot[2] { | |
| 1532 | out.push(Primitive::Segment { | |
| 1533 | from: [px, plot[1]], | |
| 1534 | to: [px, plot[3]], | |
| 1535 | width: 1.6, | |
| 1536 | color: [0.96, 0.36, 0.34, 0.90], | |
| 1537 | }); | |
| 1538 | } | |
| 1539 | ||
| 1540 | for (ci, (_, sname)) in self.channels.iter().enumerate() { | |
| 1541 | if self.hidden_splines.contains(sname) { | |
| 1542 | continue; | |
| 1543 | } | |
| 1544 | let Some(sp) = comp.splines.get(sname) else { continue }; | |
| 1545 | let bounds = self.spline_bounds(comp, sname); | |
| 1546 | let col = spline_color(comp, sname, ci); | |
| 1547 | let any_sel = self.selected.iter().any(|p| p.spline == *sname); | |
| 1548 | let curve_w = if any_sel { 2.4 } else { 1.7 }; | |
| 1549 | let curve_col = if any_sel { | |
| 1550 | [(col[0] + 0.15).min(1.0), (col[1] + 0.15).min(1.0), (col[2] + 0.15).min(1.0), 1.0] | |
| 1551 | } else { | |
| 1552 | col | |
| 1553 | }; | |
| 1554 | ||
| 1555 | if let Some(first) = sp.keyframes.first() { | |
| 1556 | let p = self.spline_to_local(first.time, first.value, bounds); | |
| 1557 | if p[0] > plot[0] { | |
| 1558 | out.push(Primitive::Segment { | |
| 1559 | from: [plot[0], p[1]], | |
| 1560 | to: p, | |
| 1561 | width: 1.1, | |
| 1562 | color: [curve_col[0], curve_col[1], curve_col[2], 0.35], | |
| 1563 | }); | |
| 1564 | } | |
| 1565 | } | |
| 1566 | if let Some(last) = sp.keyframes.last() { | |
| 1567 | let p = self.spline_to_local(last.time, last.value, bounds); | |
| 1568 | if p[0] < plot[2] { | |
| 1569 | out.push(Primitive::Segment { | |
| 1570 | from: p, | |
| 1571 | to: [plot[2], p[1]], | |
| 1572 | width: 1.1, | |
| 1573 | color: [curve_col[0], curve_col[1], curve_col[2], 0.35], | |
| 1574 | }); | |
| 1575 | } | |
| 1576 | } | |
| 1577 | ||
| 1578 | let dense = sp.keyframes.len() > 250; | |
| 1579 | for seg in 0..sp.keyframes.len().saturating_sub(1) { | |
| 1580 | let k0 = &sp.keyframes[seg]; | |
| 1581 | let k1 = &sp.keyframes[seg + 1]; | |
| 1582 | if k1.time < self.t_range[0] || k0.time > self.t_range[1] { | |
| 1583 | continue; | |
| 1584 | } | |
| 1585 | if k0.flags.step_out { | |
| 1586 | // Vertical step at k0.time, then horizontal to k1.time (matches model::spline::eval_segment). | |
| 1587 | let a = self.spline_to_local(k0.time, k0.value, bounds); | |
| 1588 | let m = self.spline_to_local(k0.time, k1.value, bounds); | |
| 1589 | let b = self.spline_to_local(k1.time, k1.value, bounds); | |
| 1590 | out.push(Primitive::Segment { from: a, to: m, width: curve_w, color: curve_col }); | |
| 1591 | out.push(Primitive::Segment { from: m, to: b, width: curve_w, color: curve_col }); | |
| 1592 | } else if k1.flags.step_in { | |
| 1593 | let a = self.spline_to_local(k0.time, k0.value, bounds); | |
| 1594 | let m = self.spline_to_local(k1.time, k0.value, bounds); | |
| 1595 | let b = self.spline_to_local(k1.time, k1.value, bounds); | |
| 1596 | out.push(Primitive::Segment { from: a, to: m, width: curve_w, color: curve_col }); | |
| 1597 | out.push(Primitive::Segment { from: m, to: b, width: curve_w, color: curve_col }); | |
| 1598 | } else if dense || (k1.flags.linear && k0.rh.is_none() && k1.lh.is_none()) { | |
| 1599 | let a = self.spline_to_local(k0.time, k0.value, bounds); | |
| 1600 | let b = self.spline_to_local(k1.time, k1.value, bounds); | |
| 1601 | out.push(Primitive::Segment { from: a, to: b, width: curve_w, color: curve_col }); | |
| 1602 | } else { | |
| 1603 | let [p0, p1, p2, p3] = segment_control_points(k0, k1); | |
| 1604 | const SUBS: usize = 20; | |
| 1605 | let mut prev = self.spline_to_local(p0[0], p0[1], bounds); | |
| 1606 | for s in 1..=SUBS { | |
| 1607 | let u = s as f64 / SUBS as f64; | |
| 1608 | let t = cubic(p0[0], p1[0], p2[0], p3[0], u); | |
| 1609 | let v = cubic(p0[1], p1[1], p2[1], p3[1], u); | |
| 1610 | let cur = self.spline_to_local(t, v, bounds); | |
| 1611 | out.push(Primitive::Segment { from: prev, to: cur, width: curve_w, color: curve_col }); | |
| 1612 | prev = cur; | |
| 1613 | } | |
| 1614 | } | |
| 1615 | } | |
| 1616 | ||
| 1617 | if !dense || (self.t_range[1] - self.t_range[0]) < 180.0 { | |
| 1618 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 1619 | if kf.time < self.t_range[0] - 5.0 || kf.time > self.t_range[1] + 5.0 { | |
| 1620 | continue; | |
| 1621 | } | |
| 1622 | let kp = self.spline_to_local(kf.time, kf.value, bounds); | |
| 1623 | let kref = PointRef { spline: sname.clone(), key_idx: ki, part: HandlePart::Key }; | |
| 1624 | let ksel = self.selected.contains(&kref); | |
| 1625 | let khov = self.hovered.as_ref() == Some(&kref); | |
| 1626 | let show_handles = self.is_key_or_handle_selected(sname, ki); | |
| 1627 | ||
| 1628 | if show_handles { | |
| 1629 | for (part, hopt) in [(HandlePart::LeftHandle, kf.lh), (HandlePart::RightHandle, kf.rh)] { | |
| 1630 | if let Some(h) = hopt { | |
| 1631 | let hp = self.spline_to_local(h[0], h[1], bounds); | |
| 1632 | let href = PointRef { spline: sname.clone(), key_idx: ki, part }; | |
| 1633 | let hsel = self.selected.contains(&href); | |
| 1634 | let hhov = self.hovered.as_ref() == Some(&href); | |
| 1635 | out.push(Primitive::Segment { | |
| 1636 | from: kp, | |
| 1637 | to: hp, | |
| 1638 | width: 1.4, | |
| 1639 | color: if hsel { [1.0, 0.85, 0.35, 1.0] } else { [0.75, 0.78, 0.86, 0.75] }, | |
| 1640 | }); | |
| 1641 | let hr = if hsel || hhov { 5.2 } else { 4.2 }; | |
| 1642 | out.push(Primitive::RoundedRect { | |
| 1643 | rect: [hp[0] - hr, hp[1] - hr, hp[0] + hr, hp[1] + hr], | |
| 1644 | radius: hr, | |
| 1645 | stroke: None, | |
| 1646 | color: if hsel { [1.0, 0.86, 0.28, 1.0] } else if hhov { [1.0, 1.0, 1.0, 1.0] } else { [0.85, 0.50, 0.95, 1.0] }, | |
| 1647 | }); | |
| 1648 | } | |
| 1649 | } | |
| 1650 | } | |
| 1651 | ||
| 1652 | let kr = if ksel || khov { 5.4 } else { 4.2 }; | |
| 1653 | out.push(Primitive::RoundedRect { | |
| 1654 | rect: [kp[0] - kr, kp[1] - kr, kp[0] + kr, kp[1] + kr], | |
| 1655 | radius: 1.5, | |
| 1656 | stroke: None, | |
| 1657 | color: if ksel { [1.0, 0.84, 0.24, 1.0] } else if khov { [1.0, 1.0, 1.0, 1.0] } else { curve_col }, | |
| 1658 | }); | |
| 1659 | out.push(Primitive::RoundedRect { | |
| 1660 | rect: [kp[0] - kr, kp[1] - kr, kp[0] + kr, kp[1] + kr], | |
| 1661 | radius: 1.5, | |
| 1662 | stroke: Some(1.2), | |
| 1663 | color: [0.02, 0.02, 0.03, 1.0], | |
| 1664 | }); | |
| 1665 | } | |
| 1666 | } | |
| 1667 | } | |
| 1668 | ||
| 1669 | // Hover readout pill showing real `(f<t>, <val>)` for the hovered keyframe/handle | |
| 1670 | if let (Some(hov), Some(rl)) = (self.hovered.as_ref(), self.text_cache.get("__hover_readout")) { | |
| 1671 | if let Some(sp) = comp.splines.get(&hov.spline) { | |
| 1672 | if let Some(kf) = sp.keyframes.get(hov.key_idx) { | |
| 1673 | let bounds = self.spline_bounds(comp, &hov.spline); | |
| 1674 | let pt = match hov.part { | |
| 1675 | HandlePart::Key => Some([kf.time, kf.value]), | |
| 1676 | HandlePart::LeftHandle => kf.lh, | |
| 1677 | HandlePart::RightHandle => kf.rh, | |
| 1678 | }; | |
| 1679 | if let Some([t, v]) = pt { | |
| 1680 | let p = self.spline_to_local(t, v, bounds); | |
| 1681 | let pill_w = (self.last_hover_readout.len() as f32 * 6.4 + 12.0).max(76.0); | |
| 1682 | let pill_h = 18.0; | |
| 1683 | let rx = (p[0] + 10.0).clamp(plot[0] + 4.0, (plot[2] - pill_w - 4.0).max(plot[0] + 4.0)); | |
| 1684 | let ry = (p[1] - pill_h - 8.0).clamp(plot[1] + 4.0, (plot[3] - pill_h - 4.0).max(plot[1] + 4.0)); | |
| 1685 | let r = [rx, ry, rx + pill_w, ry + pill_h]; | |
| 1686 | out.push(Primitive::RoundedRect { | |
| 1687 | rect: r, | |
| 1688 | radius: 4.0, | |
| 1689 | stroke: None, | |
| 1690 | color: [0.08, 0.09, 0.11, 0.94], | |
| 1691 | }); | |
| 1692 | out.push(Primitive::RoundedRect { | |
| 1693 | rect: r, | |
| 1694 | radius: 4.0, | |
| 1695 | stroke: Some(1.0), | |
| 1696 | color: self.palette.accent, | |
| 1697 | }); | |
| 1698 | out.push(Primitive::Text { | |
| 1699 | text: rl as &dyn Glyphs, | |
| 1700 | origin: [rx + 6.0, ry + 3.0], | |
| 1701 | clip: Some(r), | |
| 1702 | ink: self.palette.text, | |
| 1703 | }); | |
| 1704 | } | |
| 1705 | } | |
| 1706 | } | |
| 1707 | } | |
| 1708 | ||
| 1709 | if let Some(bs) = &self.box_select { | |
| 1710 | let x0 = bs.start_local[0].min(bs.cur_local[0]); | |
| 1711 | let x1 = bs.start_local[0].max(bs.cur_local[0]); | |
| 1712 | let y0 = bs.start_local[1].min(bs.cur_local[1]); | |
| 1713 | let y1 = bs.start_local[1].max(bs.cur_local[1]); | |
| 1714 | out.push(Primitive::RoundedRect { | |
| 1715 | rect: [x0, y0, x1, y1], | |
| 1716 | radius: 2.0, | |
| 1717 | stroke: None, | |
| 1718 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], | |
| 1719 | }); | |
| 1720 | out.push(Primitive::RoundedRect { | |
| 1721 | rect: [x0, y0, x1, y1], | |
| 1722 | radius: 2.0, | |
| 1723 | stroke: Some(1.2), | |
| 1724 | color: self.palette.accent, | |
| 1725 | }); | |
| 1726 | } | |
| 1727 | ||
| 1728 | out.push(Primitive::PopClip); | |
| 1729 | ||
| 1730 | let hud_key = match self.modal.as_ref().map(|m| m.mode) { | |
| 1731 | Some(TransformMode::Grab) => "GRAB [LMB/Enter confirm, RMB/Esc cancel, X: Frame / Y: Value]", | |
| 1732 | Some(TransformMode::Rotate) => "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", | |
| 1733 | Some(TransformMode::Scale) => "SCALE [LMB/Enter confirm, RMB/Esc cancel, X: Time / Y: Value]", | |
| 1734 | None => "Drag or G/R/S X/Y: Axis V: Handle T: Easing N: Norm I: Insert X/Del: Delete L: Linear F: Fit", | |
| 1735 | }; | |
| 1736 | out.push(Primitive::Rect { rect: [SIDEBAR_W, h - 24.0, w, h], color: self.palette.ruler }); | |
| 1737 | if let Some(hl) = self.text_cache.get(hud_key) { | |
| 1738 | out.push(Primitive::Text { | |
| 1739 | text: hl as &dyn Glyphs, | |
| 1740 | origin: [SIDEBAR_W + 10.0, h - 18.0], | |
| 1741 | clip: Some([SIDEBAR_W + 6.0, h - 24.0, w - 6.0, h]), | |
| 1742 | ink: if self.modal.is_some() { [0.98, 0.82, 0.30, 1.0] } else { self.palette.text_dim }, | |
| 1743 | }); | |
| 1744 | } | |
| 1745 | ||
| 1746 | // Floating Blender-style popup menu (`V` Handle Type / `T` Interpolation & Easing) | |
| 1747 | if let Some(popup) = &self.popup { | |
| 1748 | let pr = self.popup_rect(popup); | |
| 1749 | let hov_idx = self.popup_item_at(popup, self.pointer_local); | |
| 1750 | out.push(Primitive::RoundedRect { | |
| 1751 | rect: pr, | |
| 1752 | radius: 6.0, | |
| 1753 | stroke: None, | |
| 1754 | color: [0.09, 0.10, 0.12, 0.96], | |
| 1755 | }); | |
| 1756 | out.push(Primitive::RoundedRect { | |
| 1757 | rect: pr, | |
| 1758 | radius: 6.0, | |
| 1759 | stroke: Some(1.0), | |
| 1760 | color: self.palette.rule, | |
| 1761 | }); | |
| 1762 | let hdr_text = match popup.kind { | |
| 1763 | PopupKind::HandleType => "Set Handle Type", | |
| 1764 | PopupKind::Easing => "Set Interpolation", | |
| 1765 | }; | |
| 1766 | if let Some(hl) = self.text_cache.get(hdr_text) { | |
| 1767 | out.push(Primitive::Text { | |
| 1768 | text: hl as &dyn Glyphs, | |
| 1769 | origin: [pr[0] + 10.0, pr[1] + 5.0], | |
| 1770 | clip: Some([pr[0], pr[1], pr[2], pr[1] + POPUP_HEADER_H]), | |
| 1771 | ink: self.palette.text_dim, | |
| 1772 | }); | |
| 1773 | } | |
| 1774 | out.push(Primitive::Segment { | |
| 1775 | from: [pr[0] + 6.0, pr[1] + POPUP_HEADER_H - 1.0], | |
| 1776 | to: [pr[2] - 6.0, pr[1] + POPUP_HEADER_H - 1.0], | |
| 1777 | width: 1.0, | |
| 1778 | color: self.palette.rule, | |
| 1779 | }); | |
| 1780 | let labels: Vec<&'static str> = match popup.kind { | |
| 1781 | PopupKind::HandleType => HANDLE_MENU_ITEMS.iter().map(|&(_, l)| l).collect(), | |
| 1782 | PopupKind::Easing => EASING_MENU_ITEMS.iter().map(|&(_, l)| l).collect(), | |
| 1783 | }; | |
| 1784 | for (idx, lbl) in labels.into_iter().enumerate() { | |
| 1785 | let ry0 = pr[1] + POPUP_HEADER_H + idx as f32 * POPUP_ROW_H; | |
| 1786 | let ry1 = ry0 + POPUP_ROW_H; | |
| 1787 | if hov_idx == Some(idx) { | |
| 1788 | out.push(Primitive::RoundedRect { | |
| 1789 | rect: [pr[0] + 4.0, ry0 + 1.0, pr[2] - 4.0, ry1 - 1.0], | |
| 1790 | radius: 4.0, | |
| 1791 | stroke: None, | |
| 1792 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.25], | |
| 1793 | }); | |
| 1794 | } | |
| 1795 | if let Some(il) = self.text_cache.get(lbl) { | |
| 1796 | out.push(Primitive::Text { | |
| 1797 | text: il as &dyn Glyphs, | |
| 1798 | origin: [pr[0] + 10.0, ry0 + 4.0], | |
| 1799 | clip: Some([pr[0] + 4.0, ry0, pr[2] - 4.0, ry1]), | |
| 1800 | ink: self.palette.text, | |
| 1801 | }); | |
| 1802 | } | |
| 1803 | } | |
| 1804 | } | |
| 1805 | ||
| 1806 | out | |
| 1807 | } | |
| 1808 | 1272 | } |
| 1809 | 1273 | |
| 1810 | 1274 | fn point_in_rect(p: [f32; 2], r: [f32; 4]) -> bool { |
| ... | ... | @@ -1820,31 +1284,6 @@ fn ensure_handles(kf: &mut Keyframe) { |
| 1820 | 1284 | } |
| 1821 | 1285 | } |
| 1822 | 1286 | |
| 1823 | fn spline_color(comp: &Comp, name: &str, idx: usize) -> [f32; 4] { | |
| 1824 | if let Some(sp) = comp.splines.get(name) { | |
| 1825 | let [r, g, b] = sp.color; | |
| 1826 | if r as u16 + g as u16 + b as u16 > 120 { | |
| 1827 | return [r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0]; | |
| 1828 | } | |
| 1829 | } | |
| 1830 | const PALETTE: &[[f32; 4]] = &[ | |
| 1831 | [0.98, 0.44, 0.62, 1.0], | |
| 1832 | [0.36, 0.84, 0.96, 1.0], | |
| 1833 | [0.50, 0.92, 0.46, 1.0], | |
| 1834 | [0.98, 0.78, 0.28, 1.0], | |
| 1835 | [0.74, 0.56, 0.98, 1.0], | |
| 1836 | ]; | |
| 1837 | PALETTE[idx % PALETTE.len()] | |
| 1838 | } | |
| 1839 | ||
| 1840 | fn nice_step(raw: f64) -> f64 { | |
| 1841 | let raw = raw.max(1e-6); | |
| 1842 | let pow = 10.0_f64.powf(raw.log10().floor()); | |
| 1843 | let frac = raw / pow; | |
| 1844 | let nice = if frac < 1.5 { 1.0 } else if frac < 3.5 { 2.0 } else if frac < 7.5 { 5.0 } else { 10.0 }; | |
| 1845 | nice * pow | |
| 1846 | } | |
| 1847 | ||
| 1848 | 1287 | fn hypot2(a: [f32; 2], b: [f32; 2]) -> f32 { |
| 1849 | 1288 | ((a[0] - b[0]).powi(2) + (a[1] - b[1]).powi(2)).sqrt() |
| 1850 | 1289 | } |
crates/spline/src/paint.rs created+599| ... | ... | @@ -0,0 +1,599 @@ |
| 1 | //! Label layout and primitive painting for [`SplineView`]. | |
| 2 | ||
| 3 | use super::{ | |
| 4 | AxisLock, EASING_MENU_ITEMS, HANDLE_MENU_ITEMS, HandlePart, POPUP_HEADER_H, POPUP_ROW_H, | |
| 5 | PointRef, PopupKind, RULER_H, SIDEBAR_W, SplineView, TransformMode, | |
| 6 | }; | |
| 7 | use draw::{Glyphs, Primitive}; | |
| 8 | use model::Comp; | |
| 9 | use model::spline::{cubic, segment_control_points}; | |
| 10 | use std::sync::Arc; | |
| 11 | ||
| 12 | impl SplineView { | |
| 13 | fn hover_readout_string(&self, comp: &Comp) -> Option<String> { | |
| 14 | let hov = self.hovered.as_ref()?; | |
| 15 | let sp = comp.splines.get(&hov.spline)?; | |
| 16 | let kf = sp.keyframes.get(hov.key_idx)?; | |
| 17 | let (t, v) = match hov.part { | |
| 18 | HandlePart::Key => (kf.time, kf.value), | |
| 19 | HandlePart::LeftHandle => { | |
| 20 | let h = kf.lh?; | |
| 21 | (h[0], h[1]) | |
| 22 | } | |
| 23 | HandlePart::RightHandle => { | |
| 24 | let h = kf.rh?; | |
| 25 | (h[0], h[1]) | |
| 26 | } | |
| 27 | }; | |
| 28 | if (t - t.round()).abs() < 1e-3 { | |
| 29 | Some(format!("(f{}, {:.3})", t.round() as i64, v)) | |
| 30 | } else { | |
| 31 | Some(format!("(f{t:.1}, {v:.3})")) | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | pub fn prepare_labels(&mut self, comp: &Comp) { | |
| 36 | let font_sm = text::Font { | |
| 37 | families: Arc::from([text::Family::Mono]), | |
| 38 | size: 10.0, | |
| 39 | weight: 500, | |
| 40 | italic: false, | |
| 41 | }; | |
| 42 | let font_ch = text::Font { | |
| 43 | families: Arc::from([text::Family::Sans]), | |
| 44 | size: 11.0, | |
| 45 | weight: 500, | |
| 46 | italic: false, | |
| 47 | }; | |
| 48 | let make_layout = |s: &str, font: &text::Font| { | |
| 49 | let styles = [text::Style::new(font.clone())]; | |
| 50 | text::Paragraph { | |
| 51 | text: s, | |
| 52 | styles: &styles, | |
| 53 | spans: &[], | |
| 54 | replacements: &[], | |
| 55 | width: None, | |
| 56 | indent: 0.0, | |
| 57 | line_height: 1.2, | |
| 58 | align: text::Align::Left, | |
| 59 | tab: 32.0, | |
| 60 | } | |
| 61 | .layout() | |
| 62 | }; | |
| 63 | for (label, _) in &self.channels { | |
| 64 | self.text_cache.entry(label.clone()).or_insert_with(|| make_layout(label, &font_ch)); | |
| 65 | } | |
| 66 | let header = self | |
| 67 | .current_node | |
| 68 | .map(|id| format!("Splines: {}", comp.node(id).name)) | |
| 69 | .unwrap_or_else(|| "Splines (Comp)".to_owned()); | |
| 70 | self.text_cache.entry(header.clone()).or_insert_with(|| make_layout(&header, &font_ch)); | |
| 71 | self.text_cache | |
| 72 | .entry("Norm".to_owned()) | |
| 73 | .or_insert_with(|| make_layout("Norm", &font_sm)); | |
| 74 | for popup_hdr in ["Set Handle Type", "Set Interpolation"] { | |
| 75 | self.text_cache | |
| 76 | .entry(popup_hdr.to_owned()) | |
| 77 | .or_insert_with(|| make_layout(popup_hdr, &font_sm)); | |
| 78 | } | |
| 79 | for &(_, item_lbl) in HANDLE_MENU_ITEMS { | |
| 80 | self.text_cache | |
| 81 | .entry(item_lbl.to_owned()) | |
| 82 | .or_insert_with(|| make_layout(item_lbl, &font_ch)); | |
| 83 | } | |
| 84 | for &(_, item_lbl) in EASING_MENU_ITEMS { | |
| 85 | self.text_cache | |
| 86 | .entry(item_lbl.to_owned()) | |
| 87 | .or_insert_with(|| make_layout(item_lbl, &font_ch)); | |
| 88 | } | |
| 89 | for hud in [ | |
| 90 | "Drag or G/R/S X/Y: Axis V: Handle T: Easing N: Norm I: Insert X/Del: Delete L: Linear F: Fit", | |
| 91 | "GRAB [LMB/Enter confirm, RMB/Esc cancel, X: Frame / Y: Value]", | |
| 92 | "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", | |
| 93 | "SCALE [LMB/Enter confirm, RMB/Esc cancel, X: Time / Y: Value]", | |
| 94 | ] { | |
| 95 | self.text_cache.entry(hud.to_owned()).or_insert_with(|| make_layout(hud, &font_sm)); | |
| 96 | } | |
| 97 | ||
| 98 | if let Some(readout) = self.hover_readout_string(comp) { | |
| 99 | if readout != self.last_hover_readout || !self.text_cache.contains_key("__hover_readout") { | |
| 100 | self.text_cache | |
| 101 | .insert("__hover_readout".to_owned(), make_layout(&readout, &font_sm)); | |
| 102 | self.last_hover_readout = readout; | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / 8.0); | |
| 107 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 108 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 109 | for ti in t_start..=t_end { | |
| 110 | let key = format!("t:{}", (ti as f64 * t_step).round() as i64); | |
| 111 | self.text_cache.entry(key.clone()).or_insert_with(|| make_layout(&key[2..], &font_sm)); | |
| 112 | } | |
| 113 | let v_step = nice_step((self.v_range[1] - self.v_range[0]) / 6.0); | |
| 114 | let v_start = (self.v_range[0] / v_step).floor() as i64; | |
| 115 | let v_end = (self.v_range[1] / v_step).ceil() as i64; | |
| 116 | for vi in v_start..=v_end { | |
| 117 | let val = vi as f64 * v_step; | |
| 118 | let s = format!("{val:.2}"); | |
| 119 | let key = format!("v:{s}"); | |
| 120 | self.text_cache.entry(key).or_insert_with(|| make_layout(&s, &font_sm)); | |
| 121 | } | |
| 122 | } | |
| 123 | ||
| 124 | pub fn paint<'a>(&'a self, comp: &'a Comp, current_frame: f64) -> Vec<Primitive<'a>> { | |
| 125 | let mut out = Vec::with_capacity(512); | |
| 126 | let [w, h] = self.size; | |
| 127 | let plot = self.plot_rect(); | |
| 128 | ||
| 129 | self.paint_chrome_and_sidebar(comp, w, h, &mut out); | |
| 130 | self.paint_grid_and_rulers(plot, &mut out); | |
| 131 | ||
| 132 | out.push(Primitive::PushClip { rect: plot, radius: 0.0 }); | |
| 133 | ||
| 134 | self.paint_modal_guides(plot, &mut out); | |
| 135 | ||
| 136 | let px = self.data_to_local(current_frame, 0.0)[0]; | |
| 137 | if px >= plot[0] && px <= plot[2] { | |
| 138 | out.push(Primitive::Segment { | |
| 139 | from: [px, plot[1]], | |
| 140 | to: [px, plot[3]], | |
| 141 | width: 1.6, | |
| 142 | color: [0.96, 0.36, 0.34, 0.90], | |
| 143 | }); | |
| 144 | } | |
| 145 | ||
| 146 | self.paint_curves_and_handles(comp, plot, &mut out); | |
| 147 | self.paint_hover_readout(comp, plot, &mut out); | |
| 148 | self.paint_box_select(&mut out); | |
| 149 | ||
| 150 | out.push(Primitive::PopClip); | |
| 151 | ||
| 152 | self.paint_hud(w, h, &mut out); | |
| 153 | self.paint_popup(&mut out); | |
| 154 | ||
| 155 | out | |
| 156 | } | |
| 157 | ||
| 158 | fn paint_chrome_and_sidebar<'a>(&'a self, comp: &'a Comp, w: f32, h: f32, out: &mut Vec<Primitive<'a>>) { | |
| 159 | out.push(Primitive::Rect { rect: [0.0, 0.0, SIDEBAR_W, h], color: self.palette.sidebar }); | |
| 160 | out.push(Primitive::Segment { from: [SIDEBAR_W, 0.0], to: [SIDEBAR_W, h], width: 1.0, color: self.palette.rule }); | |
| 161 | out.push(Primitive::Rect { rect: [SIDEBAR_W, 0.0, w, RULER_H], color: self.palette.ruler }); | |
| 162 | out.push(Primitive::Segment { from: [0.0, RULER_H], to: [w, RULER_H], width: 1.0, color: self.palette.rule }); | |
| 163 | ||
| 164 | let header = self | |
| 165 | .current_node | |
| 166 | .map(|id| format!("Splines: {}", comp.node(id).name)) | |
| 167 | .unwrap_or_else(|| "Splines (Comp)".to_owned()); | |
| 168 | if let Some(hl) = self.text_cache.get(&header) { | |
| 169 | out.push(Primitive::Text { | |
| 170 | text: hl as &dyn Glyphs, | |
| 171 | origin: [10.0, 5.0], | |
| 172 | clip: Some([8.0, 0.0, SIDEBAR_W - 70.0, RULER_H]), | |
| 173 | ink: self.palette.text, | |
| 174 | }); | |
| 175 | } | |
| 176 | ||
| 177 | // Clickable "Normalize" pill in the sidebar header | |
| 178 | let pill = self.normalize_pill_rect(); | |
| 179 | let pill_fill = if self.normalized { | |
| 180 | [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.28] | |
| 181 | } else { | |
| 182 | [self.palette.bg[0], self.palette.bg[1], self.palette.bg[2], 0.9] | |
| 183 | }; | |
| 184 | let pill_stroke = if self.normalized { self.palette.accent } else { self.palette.rule }; | |
| 185 | out.push(Primitive::RoundedRect { | |
| 186 | rect: pill, | |
| 187 | radius: 4.0, | |
| 188 | stroke: None, | |
| 189 | color: pill_fill, | |
| 190 | }); | |
| 191 | out.push(Primitive::RoundedRect { | |
| 192 | rect: pill, | |
| 193 | radius: 4.0, | |
| 194 | stroke: Some(1.0), | |
| 195 | color: pill_stroke, | |
| 196 | }); | |
| 197 | if let Some(nl) = self.text_cache.get("Norm") { | |
| 198 | out.push(Primitive::Text { | |
| 199 | text: nl as &dyn Glyphs, | |
| 200 | origin: [pill[0] + 7.0, pill[1] + 3.0], | |
| 201 | clip: Some(pill), | |
| 202 | ink: if self.normalized { self.palette.text } else { self.palette.text_dim }, | |
| 203 | }); | |
| 204 | } | |
| 205 | ||
| 206 | for (i, (label, sname)) in self.channels.iter().enumerate() { | |
| 207 | let y = RULER_H + 6.0 + i as f32 * 22.0; | |
| 208 | if y > h - 24.0 { | |
| 209 | break; | |
| 210 | } | |
| 211 | let hidden = self.hidden_splines.contains(sname); | |
| 212 | let col = spline_color(comp, sname, i); | |
| 213 | out.push(Primitive::RoundedRect { | |
| 214 | rect: [10.0, y + 4.0, 20.0, y + 14.0], | |
| 215 | radius: 3.0, | |
| 216 | stroke: if hidden { Some(1.2) } else { None }, | |
| 217 | color: if hidden { self.palette.text_dim } else { col }, | |
| 218 | }); | |
| 219 | if let Some(tl) = self.text_cache.get(label) { | |
| 220 | out.push(Primitive::Text { | |
| 221 | text: tl as &dyn Glyphs, | |
| 222 | origin: [26.0, y + 2.0], | |
| 223 | clip: Some([26.0, y, SIDEBAR_W - 8.0, y + 20.0]), | |
| 224 | ink: if hidden { self.palette.text_dim } else { self.palette.text }, | |
| 225 | }); | |
| 226 | } | |
| 227 | } | |
| 228 | } | |
| 229 | ||
| 230 | fn paint_grid_and_rulers<'a>(&'a self, plot: [f32; 4], out: &mut Vec<Primitive<'a>>) { | |
| 231 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / 8.0); | |
| 232 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 233 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 234 | for ti in t_start..=t_end { | |
| 235 | let t = ti as f64 * t_step; | |
| 236 | let x = self.data_to_local(t, 0.0)[0]; | |
| 237 | if x < plot[0] || x > plot[2] { | |
| 238 | continue; | |
| 239 | } | |
| 240 | out.push(Primitive::Segment { from: [x, plot[1]], to: [x, plot[3]], width: 1.0, color: self.palette.grid }); | |
| 241 | let key = format!("t:{}", t.round() as i64); | |
| 242 | if let Some(tl) = self.text_cache.get(&key) { | |
| 243 | out.push(Primitive::Text { | |
| 244 | text: tl as &dyn Glyphs, | |
| 245 | origin: [x + 3.0, 6.0], | |
| 246 | clip: Some([plot[0], 0.0, plot[2], RULER_H]), | |
| 247 | ink: self.palette.text_dim, | |
| 248 | }); | |
| 249 | } | |
| 250 | } | |
| 251 | ||
| 252 | let v_step = nice_step((self.v_range[1] - self.v_range[0]) / 6.0); | |
| 253 | let v_start = (self.v_range[0] / v_step).floor() as i64; | |
| 254 | let v_end = (self.v_range[1] / v_step).ceil() as i64; | |
| 255 | for vi in v_start..=v_end { | |
| 256 | let v = vi as f64 * v_step; | |
| 257 | let y = self.data_to_local(0.0, v)[1]; | |
| 258 | if y < plot[1] || y > plot[3] { | |
| 259 | continue; | |
| 260 | } | |
| 261 | let is_zero = v.abs() < 1e-6; | |
| 262 | out.push(Primitive::Segment { | |
| 263 | from: [plot[0], y], | |
| 264 | to: [plot[2], y], | |
| 265 | width: if is_zero { 1.3 } else { 1.0 }, | |
| 266 | color: if is_zero { self.palette.zero_line } else { self.palette.grid }, | |
| 267 | }); | |
| 268 | let key = format!("v:{v:.2}"); | |
| 269 | if let Some(vl) = self.text_cache.get(&key) { | |
| 270 | out.push(Primitive::Text { | |
| 271 | text: vl as &dyn Glyphs, | |
| 272 | origin: [plot[0] + 4.0, y - 12.0], | |
| 273 | clip: Some(plot), | |
| 274 | ink: self.palette.text_dim, | |
| 275 | }); | |
| 276 | } | |
| 277 | } | |
| 278 | } | |
| 279 | ||
| 280 | fn paint_modal_guides<'a>(&'a self, plot: [f32; 4], out: &mut Vec<Primitive<'a>>) { | |
| 281 | if let Some(m) = &self.modal { | |
| 282 | let piv = self.data_to_local(m.pivot[0], m.pivot[1]); | |
| 283 | match m.axis { | |
| 284 | AxisLock::X => out.push(Primitive::Segment { | |
| 285 | from: [plot[0], piv[1]], | |
| 286 | to: [plot[2], piv[1]], | |
| 287 | width: 1.5, | |
| 288 | color: [0.95, 0.28, 0.34, 0.85], | |
| 289 | }), | |
| 290 | AxisLock::Y => out.push(Primitive::Segment { | |
| 291 | from: [piv[0], plot[1]], | |
| 292 | to: [piv[0], plot[3]], | |
| 293 | width: 1.5, | |
| 294 | color: [0.28, 0.90, 0.42, 0.85], | |
| 295 | }), | |
| 296 | AxisLock::None => {} | |
| 297 | } | |
| 298 | } | |
| 299 | } | |
| 300 | ||
| 301 | fn paint_curves_and_handles<'a>(&'a self, comp: &'a Comp, plot: [f32; 4], out: &mut Vec<Primitive<'a>>) { | |
| 302 | for (ci, (_, sname)) in self.channels.iter().enumerate() { | |
| 303 | if self.hidden_splines.contains(sname) { | |
| 304 | continue; | |
| 305 | } | |
| 306 | let Some(sp) = comp.splines.get(sname) else { continue }; | |
| 307 | let bounds = self.spline_bounds(comp, sname); | |
| 308 | let col = spline_color(comp, sname, ci); | |
| 309 | let any_sel = self.selected.iter().any(|p| p.spline == *sname); | |
| 310 | let curve_w = if any_sel { 2.4 } else { 1.7 }; | |
| 311 | let curve_col = if any_sel { | |
| 312 | [(col[0] + 0.15).min(1.0), (col[1] + 0.15).min(1.0), (col[2] + 0.15).min(1.0), 1.0] | |
| 313 | } else { | |
| 314 | col | |
| 315 | }; | |
| 316 | ||
| 317 | if let Some(first) = sp.keyframes.first() { | |
| 318 | let p = self.spline_to_local(first.time, first.value, bounds); | |
| 319 | if p[0] > plot[0] { | |
| 320 | out.push(Primitive::Segment { | |
| 321 | from: [plot[0], p[1]], | |
| 322 | to: p, | |
| 323 | width: 1.1, | |
| 324 | color: [curve_col[0], curve_col[1], curve_col[2], 0.35], | |
| 325 | }); | |
| 326 | } | |
| 327 | } | |
| 328 | if let Some(last) = sp.keyframes.last() { | |
| 329 | let p = self.spline_to_local(last.time, last.value, bounds); | |
| 330 | if p[0] < plot[2] { | |
| 331 | out.push(Primitive::Segment { | |
| 332 | from: p, | |
| 333 | to: [plot[2], p[1]], | |
| 334 | width: 1.1, | |
| 335 | color: [curve_col[0], curve_col[1], curve_col[2], 0.35], | |
| 336 | }); | |
| 337 | } | |
| 338 | } | |
| 339 | ||
| 340 | let dense = sp.keyframes.len() > 250; | |
| 341 | for seg in 0..sp.keyframes.len().saturating_sub(1) { | |
| 342 | let k0 = &sp.keyframes[seg]; | |
| 343 | let k1 = &sp.keyframes[seg + 1]; | |
| 344 | if k1.time < self.t_range[0] || k0.time > self.t_range[1] { | |
| 345 | continue; | |
| 346 | } | |
| 347 | if k0.flags.step_out { | |
| 348 | // Vertical step at k0.time, then horizontal to k1.time (matches model::spline::eval_segment). | |
| 349 | let a = self.spline_to_local(k0.time, k0.value, bounds); | |
| 350 | let m = self.spline_to_local(k0.time, k1.value, bounds); | |
| 351 | let b = self.spline_to_local(k1.time, k1.value, bounds); | |
| 352 | out.push(Primitive::Segment { from: a, to: m, width: curve_w, color: curve_col }); | |
| 353 | out.push(Primitive::Segment { from: m, to: b, width: curve_w, color: curve_col }); | |
| 354 | } else if k1.flags.step_in { | |
| 355 | let a = self.spline_to_local(k0.time, k0.value, bounds); | |
| 356 | let m = self.spline_to_local(k1.time, k0.value, bounds); | |
| 357 | let b = self.spline_to_local(k1.time, k1.value, bounds); | |
| 358 | out.push(Primitive::Segment { from: a, to: m, width: curve_w, color: curve_col }); | |
| 359 | out.push(Primitive::Segment { from: m, to: b, width: curve_w, color: curve_col }); | |
| 360 | } else if dense || (k1.flags.linear && k0.rh.is_none() && k1.lh.is_none()) { | |
| 361 | let a = self.spline_to_local(k0.time, k0.value, bounds); | |
| 362 | let b = self.spline_to_local(k1.time, k1.value, bounds); | |
| 363 | out.push(Primitive::Segment { from: a, to: b, width: curve_w, color: curve_col }); | |
| 364 | } else { | |
| 365 | let [p0, p1, p2, p3] = segment_control_points(k0, k1); | |
| 366 | const SUBS: usize = 20; | |
| 367 | let mut prev = self.spline_to_local(p0[0], p0[1], bounds); | |
| 368 | for s in 1..=SUBS { | |
| 369 | let u = s as f64 / SUBS as f64; | |
| 370 | let t = cubic(p0[0], p1[0], p2[0], p3[0], u); | |
| 371 | let v = cubic(p0[1], p1[1], p2[1], p3[1], u); | |
| 372 | let cur = self.spline_to_local(t, v, bounds); | |
| 373 | out.push(Primitive::Segment { from: prev, to: cur, width: curve_w, color: curve_col }); | |
| 374 | prev = cur; | |
| 375 | } | |
| 376 | } | |
| 377 | } | |
| 378 | ||
| 379 | if !dense || (self.t_range[1] - self.t_range[0]) < 180.0 { | |
| 380 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 381 | if kf.time < self.t_range[0] - 5.0 || kf.time > self.t_range[1] + 5.0 { | |
| 382 | continue; | |
| 383 | } | |
| 384 | let kp = self.spline_to_local(kf.time, kf.value, bounds); | |
| 385 | let kref = PointRef { spline: sname.clone(), key_idx: ki, part: HandlePart::Key }; | |
| 386 | let ksel = self.selected.contains(&kref); | |
| 387 | let khov = self.hovered.as_ref() == Some(&kref); | |
| 388 | let show_handles = self.is_key_or_handle_selected(sname, ki); | |
| 389 | ||
| 390 | if show_handles { | |
| 391 | for (part, hopt) in [(HandlePart::LeftHandle, kf.lh), (HandlePart::RightHandle, kf.rh)] { | |
| 392 | if let Some(h) = hopt { | |
| 393 | let hp = self.spline_to_local(h[0], h[1], bounds); | |
| 394 | let href = PointRef { spline: sname.clone(), key_idx: ki, part }; | |
| 395 | let hsel = self.selected.contains(&href); | |
| 396 | let hhov = self.hovered.as_ref() == Some(&href); | |
| 397 | out.push(Primitive::Segment { | |
| 398 | from: kp, | |
| 399 | to: hp, | |
| 400 | width: 1.4, | |
| 401 | color: if hsel { [1.0, 0.85, 0.35, 1.0] } else { [0.75, 0.78, 0.86, 0.75] }, | |
| 402 | }); | |
| 403 | let hr = if hsel || hhov { 5.2 } else { 4.2 }; | |
| 404 | out.push(Primitive::RoundedRect { | |
| 405 | rect: [hp[0] - hr, hp[1] - hr, hp[0] + hr, hp[1] + hr], | |
| 406 | radius: hr, | |
| 407 | stroke: None, | |
| 408 | color: if hsel { [1.0, 0.86, 0.28, 1.0] } else if hhov { [1.0, 1.0, 1.0, 1.0] } else { [0.85, 0.50, 0.95, 1.0] }, | |
| 409 | }); | |
| 410 | } | |
| 411 | } | |
| 412 | } | |
| 413 | ||
| 414 | let kr = if ksel || khov { 5.4 } else { 4.2 }; | |
| 415 | out.push(Primitive::RoundedRect { | |
| 416 | rect: [kp[0] - kr, kp[1] - kr, kp[0] + kr, kp[1] + kr], | |
| 417 | radius: 1.5, | |
| 418 | stroke: None, | |
| 419 | color: if ksel { [1.0, 0.84, 0.24, 1.0] } else if khov { [1.0, 1.0, 1.0, 1.0] } else { curve_col }, | |
| 420 | }); | |
| 421 | out.push(Primitive::RoundedRect { | |
| 422 | rect: [kp[0] - kr, kp[1] - kr, kp[0] + kr, kp[1] + kr], | |
| 423 | radius: 1.5, | |
| 424 | stroke: Some(1.2), | |
| 425 | color: [0.02, 0.02, 0.03, 1.0], | |
| 426 | }); | |
| 427 | } | |
| 428 | } | |
| 429 | } | |
| 430 | } | |
| 431 | ||
| 432 | fn paint_hover_readout<'a>(&'a self, comp: &'a Comp, plot: [f32; 4], out: &mut Vec<Primitive<'a>>) { | |
| 433 | // Hover readout pill showing real `(f<t>, <val>)` for the hovered keyframe/handle | |
| 434 | if let (Some(hov), Some(rl)) = (self.hovered.as_ref(), self.text_cache.get("__hover_readout")) { | |
| 435 | if let Some(sp) = comp.splines.get(&hov.spline) { | |
| 436 | if let Some(kf) = sp.keyframes.get(hov.key_idx) { | |
| 437 | let bounds = self.spline_bounds(comp, &hov.spline); | |
| 438 | let pt = match hov.part { | |
| 439 | HandlePart::Key => Some([kf.time, kf.value]), | |
| 440 | HandlePart::LeftHandle => kf.lh, | |
| 441 | HandlePart::RightHandle => kf.rh, | |
| 442 | }; | |
| 443 | if let Some([t, v]) = pt { | |
| 444 | let p = self.spline_to_local(t, v, bounds); | |
| 445 | let pill_w = (self.last_hover_readout.len() as f32 * 6.4 + 12.0).max(76.0); | |
| 446 | let pill_h = 18.0; | |
| 447 | let rx = (p[0] + 10.0).clamp(plot[0] + 4.0, (plot[2] - pill_w - 4.0).max(plot[0] + 4.0)); | |
| 448 | let ry = (p[1] - pill_h - 8.0).clamp(plot[1] + 4.0, (plot[3] - pill_h - 4.0).max(plot[1] + 4.0)); | |
| 449 | let r = [rx, ry, rx + pill_w, ry + pill_h]; | |
| 450 | out.push(Primitive::RoundedRect { | |
| 451 | rect: r, | |
| 452 | radius: 4.0, | |
| 453 | stroke: None, | |
| 454 | color: [0.08, 0.09, 0.11, 0.94], | |
| 455 | }); | |
| 456 | out.push(Primitive::RoundedRect { | |
| 457 | rect: r, | |
| 458 | radius: 4.0, | |
| 459 | stroke: Some(1.0), | |
| 460 | color: self.palette.accent, | |
| 461 | }); | |
| 462 | out.push(Primitive::Text { | |
| 463 | text: rl as &dyn Glyphs, | |
| 464 | origin: [rx + 6.0, ry + 3.0], | |
| 465 | clip: Some(r), | |
| 466 | ink: self.palette.text, | |
| 467 | }); | |
| 468 | } | |
| 469 | } | |
| 470 | } | |
| 471 | } | |
| 472 | } | |
| 473 | ||
| 474 | fn paint_box_select<'a>(&'a self, out: &mut Vec<Primitive<'a>>) { | |
| 475 | if let Some(bs) = &self.box_select { | |
| 476 | let x0 = bs.start_local[0].min(bs.cur_local[0]); | |
| 477 | let x1 = bs.start_local[0].max(bs.cur_local[0]); | |
| 478 | let y0 = bs.start_local[1].min(bs.cur_local[1]); | |
| 479 | let y1 = bs.start_local[1].max(bs.cur_local[1]); | |
| 480 | out.push(Primitive::RoundedRect { | |
| 481 | rect: [x0, y0, x1, y1], | |
| 482 | radius: 2.0, | |
| 483 | stroke: None, | |
| 484 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], | |
| 485 | }); | |
| 486 | out.push(Primitive::RoundedRect { | |
| 487 | rect: [x0, y0, x1, y1], | |
| 488 | radius: 2.0, | |
| 489 | stroke: Some(1.2), | |
| 490 | color: self.palette.accent, | |
| 491 | }); | |
| 492 | } | |
| 493 | } | |
| 494 | ||
| 495 | fn paint_hud<'a>(&'a self, w: f32, h: f32, out: &mut Vec<Primitive<'a>>) { | |
| 496 | let hud_key = match self.modal.as_ref().map(|m| m.mode) { | |
| 497 | Some(TransformMode::Grab) => "GRAB [LMB/Enter confirm, RMB/Esc cancel, X: Frame / Y: Value]", | |
| 498 | Some(TransformMode::Rotate) => "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", | |
| 499 | Some(TransformMode::Scale) => "SCALE [LMB/Enter confirm, RMB/Esc cancel, X: Time / Y: Value]", | |
| 500 | None => "Drag or G/R/S X/Y: Axis V: Handle T: Easing N: Norm I: Insert X/Del: Delete L: Linear F: Fit", | |
| 501 | }; | |
| 502 | out.push(Primitive::Rect { rect: [SIDEBAR_W, h - 24.0, w, h], color: self.palette.ruler }); | |
| 503 | if let Some(hl) = self.text_cache.get(hud_key) { | |
| 504 | out.push(Primitive::Text { | |
| 505 | text: hl as &dyn Glyphs, | |
| 506 | origin: [SIDEBAR_W + 10.0, h - 18.0], | |
| 507 | clip: Some([SIDEBAR_W + 6.0, h - 24.0, w - 6.0, h]), | |
| 508 | ink: if self.modal.is_some() { [0.98, 0.82, 0.30, 1.0] } else { self.palette.text_dim }, | |
| 509 | }); | |
| 510 | } | |
| 511 | } | |
| 512 | ||
| 513 | fn paint_popup<'a>(&'a self, out: &mut Vec<Primitive<'a>>) { | |
| 514 | // Floating Blender-style popup menu (`V` Handle Type / `T` Interpolation & Easing) | |
| 515 | if let Some(popup) = &self.popup { | |
| 516 | let pr = self.popup_rect(popup); | |
| 517 | let hov_idx = self.popup_item_at(popup, self.pointer_local); | |
| 518 | out.push(Primitive::RoundedRect { | |
| 519 | rect: pr, | |
| 520 | radius: 6.0, | |
| 521 | stroke: None, | |
| 522 | color: [0.09, 0.10, 0.12, 0.96], | |
| 523 | }); | |
| 524 | out.push(Primitive::RoundedRect { | |
| 525 | rect: pr, | |
| 526 | radius: 6.0, | |
| 527 | stroke: Some(1.0), | |
| 528 | color: self.palette.rule, | |
| 529 | }); | |
| 530 | let hdr_text = match popup.kind { | |
| 531 | PopupKind::HandleType => "Set Handle Type", | |
| 532 | PopupKind::Easing => "Set Interpolation", | |
| 533 | }; | |
| 534 | if let Some(hl) = self.text_cache.get(hdr_text) { | |
| 535 | out.push(Primitive::Text { | |
| 536 | text: hl as &dyn Glyphs, | |
| 537 | origin: [pr[0] + 10.0, pr[1] + 5.0], | |
| 538 | clip: Some([pr[0], pr[1], pr[2], pr[1] + POPUP_HEADER_H]), | |
| 539 | ink: self.palette.text_dim, | |
| 540 | }); | |
| 541 | } | |
| 542 | out.push(Primitive::Segment { | |
| 543 | from: [pr[0] + 6.0, pr[1] + POPUP_HEADER_H - 1.0], | |
| 544 | to: [pr[2] - 6.0, pr[1] + POPUP_HEADER_H - 1.0], | |
| 545 | width: 1.0, | |
| 546 | color: self.palette.rule, | |
| 547 | }); | |
| 548 | let labels: Vec<&'static str> = match popup.kind { | |
| 549 | PopupKind::HandleType => HANDLE_MENU_ITEMS.iter().map(|&(_, l)| l).collect(), | |
| 550 | PopupKind::Easing => EASING_MENU_ITEMS.iter().map(|&(_, l)| l).collect(), | |
| 551 | }; | |
| 552 | for (idx, lbl) in labels.into_iter().enumerate() { | |
| 553 | let ry0 = pr[1] + POPUP_HEADER_H + idx as f32 * POPUP_ROW_H; | |
| 554 | let ry1 = ry0 + POPUP_ROW_H; | |
| 555 | if hov_idx == Some(idx) { | |
| 556 | out.push(Primitive::RoundedRect { | |
| 557 | rect: [pr[0] + 4.0, ry0 + 1.0, pr[2] - 4.0, ry1 - 1.0], | |
| 558 | radius: 4.0, | |
| 559 | stroke: None, | |
| 560 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.25], | |
| 561 | }); | |
| 562 | } | |
| 563 | if let Some(il) = self.text_cache.get(lbl) { | |
| 564 | out.push(Primitive::Text { | |
| 565 | text: il as &dyn Glyphs, | |
| 566 | origin: [pr[0] + 10.0, ry0 + 4.0], | |
| 567 | clip: Some([pr[0] + 4.0, ry0, pr[2] - 4.0, ry1]), | |
| 568 | ink: self.palette.text, | |
| 569 | }); | |
| 570 | } | |
| 571 | } | |
| 572 | } | |
| 573 | } | |
| 574 | } | |
| 575 | ||
| 576 | fn spline_color(comp: &Comp, name: &str, idx: usize) -> [f32; 4] { | |
| 577 | if let Some(sp) = comp.splines.get(name) { | |
| 578 | let [r, g, b] = sp.color; | |
| 579 | if r as u16 + g as u16 + b as u16 > 120 { | |
| 580 | return [r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0]; | |
| 581 | } | |
| 582 | } | |
| 583 | const PALETTE: &[[f32; 4]] = &[ | |
| 584 | [0.98, 0.44, 0.62, 1.0], | |
| 585 | [0.36, 0.84, 0.96, 1.0], | |
| 586 | [0.50, 0.92, 0.46, 1.0], | |
| 587 | [0.98, 0.78, 0.28, 1.0], | |
| 588 | [0.74, 0.56, 0.98, 1.0], | |
| 589 | ]; | |
| 590 | PALETTE[idx % PALETTE.len()] | |
| 591 | } | |
| 592 | ||
| 593 | fn nice_step(raw: f64) -> f64 { | |
| 594 | let raw = raw.max(1e-6); | |
| 595 | let pow = 10.0_f64.powf(raw.log10().floor()); | |
| 596 | let frac = raw / pow; | |
| 597 | let nice = if frac < 1.5 { 1.0 } else if frac < 3.5 { 2.0 } else if frac < 7.5 { 5.0 } else { 10.0 }; | |
| 598 | nice * pow | |
| 599 | } |