1//! Small Inspector widgets: the keyframe diamond (`◆`), the expression badge, and the keyframe
2//! state / number formatting they share.
3
4use super::edit::Cx;
5use model::{Comp, NodeId};
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum KeyframeState {
9 Static,
10 AnimatedOtherFrame,
11 KeyframeAtCurrent,
12}
13
14/// Computes whether an input is static, animated on another frame, or has a keyframe at `frame`.
15pub fn keyframe_state(comp: &Comp, node_id: NodeId, input_name: &str, frame: f64) -> KeyframeState {
16 let Some(sp_name) = comp
17 .nodes
18 .get(node_id.0)
19 .and_then(|n| n.inputs.get(input_name))
20 .and_then(|b| b.source_op.as_deref())
21 else {
22 return KeyframeState::Static;
23 };
24 let Some(spline) = comp.splines.get(sp_name) else {
25 return KeyframeState::Static;
26 };
27 if spline.keyframes.iter().any(|kf| (kf.time - frame).abs() < 1e-4) {
28 KeyframeState::KeyframeAtCurrent
29 } else if !spline.keyframes.is_empty() {
30 KeyframeState::AnimatedOtherFrame
31 } else {
32 KeyframeState::Static
33 }
34}
35
36pub fn format_param_number(v: f64) -> String {
37 if v.abs() >= 100.0 {
38 format!("{v:.1}")
39 } else if v.abs() >= 10.0 {
40 format!("{v:.2}")
41 } else {
42 format!("{v:.3}")
43 }
44}
45
46/// The diamond column's width, kept by rows with nothing to animate so controls line up.
47pub(super) const MARK_W: f32 = 18.0;
48
49/// Whether the diamond was clicked.
50pub(super) fn keyframe_button(ui: &mut ui::Ui, cx: &Cx, state: KeyframeState) -> bool {
51 let (ink, fill, border) = match state {
52 KeyframeState::KeyframeAtCurrent => (
53 [1.0, 0.80, 0.22, 1.0],
54 Some([0.98, 0.72, 0.18, 0.24]),
55 Some([0.98, 0.76, 0.20, 0.85]),
56 ),
57 KeyframeState::AnimatedOtherFrame => (
58 [0.92, 0.64, 0.24, 0.90],
59 Some([0.92, 0.64, 0.24, 0.12]),
60 Some([0.92, 0.64, 0.24, 0.45]),
61 ),
62 KeyframeState::Static => (cx.p.text_dim, None, None),
63 };
64 ui.leaf(
65 "kf",
66 ui::Spec {
67 cursor: Some(winit::window::CursorIcon::Pointer),
68 flags: ui::Flags::CLICKABLE,
69 size: [ui::px(MARK_W), ui::px(20.0)],
70 text: Some("◆"),
71 font_size: Some(10.0),
72 color: Some(ink),
73 fill,
74 border,
75 hover_fill: Some([0.98, 0.76, 0.22, 0.18]),
76 radius: 4.0,
77 center: true,
78 ..ui::Spec::default()
79 },
80 )
81 .clicked
82}
83
84pub(super) fn expression_badge(ui: &mut ui::Ui) {
85 ui.leaf(
86 "expr",
87 ui::Spec {
88 size: [ui::px(16.0), ui::px(18.0)],
89 text: Some("="),
90 font_size: Some(10.5),
91 bold: true,
92 color: Some([0.36, 0.88, 0.92, 1.0]),
93 fill: Some([0.10, 0.24, 0.28, 0.85]),
94 border: Some([0.32, 0.78, 0.86, 0.65]),
95 radius: 3.0,
96 center: true,
97 ..ui::Spec::default()
98 },
99 );
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105 use model::{BezierSpline, Comp, Keyframe};
106
107 #[test]
108 fn format_param_number_uses_magnitude_precision() {
109 assert_eq!(format_param_number(0.12345), "0.123");
110 assert_eq!(format_param_number(-9.999), "-9.999");
111 assert_eq!(format_param_number(10.0), "10.00");
112 assert_eq!(format_param_number(-42.567), "-42.57");
113 assert_eq!(format_param_number(100.0), "100.0");
114 assert_eq!(format_param_number(-1234.56), "-1234.6");
115 }
116
117 #[test]
118 fn keyframe_state_respects_epsilon() {
119 let mut comp = Comp::from_fusion_str(
120 r#"Composition {
121 Tools = ordered() {
122 XF = Transform {},
123 },
124 }"#,
125 )
126 .expect("comp");
127 let xf = comp.find_node("XF").unwrap();
128
129 assert_eq!(keyframe_state(&comp, xf, "Size", 10.0), KeyframeState::Static);
130
131 comp.set_keyframe(xf, "Size", 10.0, 2.0).expect("set keyframe");
132 assert_eq!(keyframe_state(&comp, xf, "Size", 10.0), KeyframeState::KeyframeAtCurrent);
133 assert_eq!(keyframe_state(&comp, xf, "Size", 10.0 + 5e-5), KeyframeState::KeyframeAtCurrent);
134 assert_eq!(keyframe_state(&comp, xf, "Size", 10.0 + 2e-4), KeyframeState::AnimatedOtherFrame);
135 assert_eq!(keyframe_state(&comp, xf, "Size", 20.0), KeyframeState::AnimatedOtherFrame);
136
137 // Empty spline falls back to Static
138 let sp_name = comp.node(xf).inputs.get("Size").and_then(|b| b.source_op.clone()).unwrap();
139 comp.splines.insert(
140 sp_name,
141 BezierSpline {
142 name: "XFSize".to_owned(),
143 color: [255, 255, 255],
144 keyframes: Vec::<Keyframe>::new(),
145 },
146 );
147 assert_eq!(keyframe_state(&comp, xf, "Size", 10.0), KeyframeState::Static);
148 }
149}