| 1 | //! Fusion's four-column parameter row — right-aligned label, control, value box(es), keyframe |
| 2 | //! diamond — and one renderer per control kind, built from the Knife `ui` primitives. |
| 3 | |
| 4 | use super::edit::{Cx, NumberEdit}; |
| 5 | use super::layout::{Channel, ControlDef, Kind}; |
| 6 | use super::widgets::{MARK_W, expression_badge, format_param_number, keyframe_button}; |
| 7 | use model::ParamValue; |
| 8 | use ui::{Spec, Ui, fill, fit, px}; |
| 9 | use winit::keyboard::{Key, NamedKey}; |
| 10 | |
| 11 | const LABEL_W: f32 = 115.0; |
| 12 | const ROW_H: f32 = 24.0; |
| 13 | const BOX_H: f32 = 20.0; |
| 14 | |
| 15 | pub(super) fn render(ui: &mut Ui, cx: &mut Cx, def: &ControlDef) { |
| 16 | let channels = def.channels.as_slice(); |
| 17 | let keys = match def.kind { |
| 18 | Kind::Slider | Kind::Screw | Kind::Range | Kind::Color | Kind::Checkbox | Kind::Toggles => true, |
| 19 | Kind::Segmented | Kind::Combo => !def.fuid, |
| 20 | Kind::Point | Kind::Text => false, |
| 21 | }; |
| 22 | match def.kind { |
| 23 | Kind::Slider => { |
| 24 | let ch = &channels[0]; |
| 25 | let sub = sub_labels(&ch.id); |
| 26 | let height = px(if sub.is_some() { ROW_H + 14.0 } else { ROW_H }); |
| 27 | row(ui, cx, &def.label, &ch.id, channels, keys, height, |ui, cx| { |
| 28 | let value = cx.number(ch); |
| 29 | let mut spec = ui::Slider::new(value as f32, ch.scale[0] as f32, ch.scale[1] as f32, cx.default_of(ch) as f32); |
| 30 | spec.step = ch.integer.then_some(1.0); |
| 31 | spec.sub_labels = sub; |
| 32 | spec.label = &def.label; |
| 33 | if let Some(next) = ui::slider(ui, ("sld", cx.node.0, ch.id.as_str()), spec) { |
| 34 | cx.write_numbers(&def.label, &[(ch, f64::from(next))], Some(&ch.id)); |
| 35 | } |
| 36 | number_box(ui, cx, ch, None, value, px(52.0)); |
| 37 | }); |
| 38 | } |
| 39 | Kind::Screw => { |
| 40 | let ch = &channels[0]; |
| 41 | row(ui, cx, &def.label, &ch.id, channels, keys, px(ROW_H), |ui, cx| { |
| 42 | let value = cx.number(ch); |
| 43 | let mut spec = ui::ScrewControl::new(value as f32, cx.default_of(ch) as f32); |
| 44 | spec.sensitivity = ((ch.scale[1] - ch.scale[0]) / 360.0).max(0.001) as f32; |
| 45 | spec.bounds = ch.allowed[0].zip(ch.allowed[1]).map(|(lo, hi)| [lo as f32, hi as f32]); |
| 46 | spec.width = fill(); |
| 47 | spec.label = &def.label; |
| 48 | if let Some(next) = ui::screw_control(ui, ("scw", cx.node.0, ch.id.as_str()), spec) { |
| 49 | cx.write_numbers(&def.label, &[(ch, f64::from(next))], Some(&ch.id)); |
| 50 | } |
| 51 | number_box(ui, cx, ch, None, value, px(52.0)); |
| 52 | }); |
| 53 | } |
| 54 | Kind::Range => { |
| 55 | let [start, end] = [&channels[0], &channels[1]]; |
| 56 | row(ui, cx, &def.label, &start.id, channels, keys, px(ROW_H), |ui, cx| { |
| 57 | let [s, e] = [cx.number(start), cx.number(end)]; |
| 58 | let mut spec = ui::RangeSlider::new( |
| 59 | s as f32, |
| 60 | e as f32, |
| 61 | start.scale[0] as f32, |
| 62 | start.scale[1] as f32, |
| 63 | [cx.default_of(start) as f32, cx.default_of(end) as f32], |
| 64 | ); |
| 65 | spec.step = start.integer.then_some(1.0); |
| 66 | spec.label = &def.label; |
| 67 | if let Some([ns, ne]) = ui::range_slider(ui, ("rng", cx.node.0, start.id.as_str()), spec) { |
| 68 | let moved = |old: f64, new: f32| (f64::from(new) - old).abs() > 1e-6; |
| 69 | let edits: Vec<_> = [(start, s, ns), (end, e, ne)] |
| 70 | .into_iter() |
| 71 | .filter(|&(_, old, new)| moved(old, new)) |
| 72 | .map(|(ch, _, new)| (ch, f64::from(new))) |
| 73 | .collect(); |
| 74 | cx.write_numbers(&def.label, &edits, Some(&start.id)); |
| 75 | } |
| 76 | number_box(ui, cx, start, None, s, px(44.0)); |
| 77 | number_box(ui, cx, end, None, e, px(44.0)); |
| 78 | }); |
| 79 | } |
| 80 | Kind::Color => color(ui, cx, def), |
| 81 | Kind::Checkbox => { |
| 82 | let ch = &channels[0]; |
| 83 | row(ui, cx, &def.label, &ch.id, channels, keys, px(ROW_H), |ui, cx| { |
| 84 | let on = cx.number(ch) >= 0.5; |
| 85 | if checkbox(ui, ("chk", cx.node.0, ch.id.as_str()), on, &def.label) { |
| 86 | cx.write_numbers(&def.label, &[(ch, f64::from(u8::from(!on)))], None); |
| 87 | } |
| 88 | spacer(ui); |
| 89 | }); |
| 90 | } |
| 91 | Kind::Toggles => row(ui, cx, &def.label, &channels[0].id, channels, keys, px(ROW_H), |ui, cx| { |
| 92 | for (ch, name) in channels.iter().zip(&def.entries) { |
| 93 | let on = cx.number(ch) >= 0.5; |
| 94 | if toggle_button(ui, cx, ("tgl", cx.node.0, ch.id.as_str()), name, on) { |
| 95 | cx.write_numbers(name, &[(ch, f64::from(u8::from(!on)))], None); |
| 96 | } |
| 97 | } |
| 98 | spacer(ui); |
| 99 | }), |
| 100 | Kind::Segmented => { |
| 101 | let ch = &channels[0]; |
| 102 | row(ui, cx, &def.label, &ch.id, channels, keys, px(ROW_H + 2.0), |ui, cx| { |
| 103 | let segments: Vec<_> = def.entries.iter().map(|e| ui::MultiSegment::text(e)).collect(); |
| 104 | let mut spec = ui::SegmentedControl::new(&segments, choice_index(cx, def)); |
| 105 | spec.width = fill(); |
| 106 | spec.label = &def.label; |
| 107 | if let Some(next) = ui::segmented_control(ui, ("seg", cx.node.0, ch.id.as_str()), spec) { |
| 108 | write_choice(cx, def, next); |
| 109 | } |
| 110 | }); |
| 111 | } |
| 112 | Kind::Combo => { |
| 113 | let ch = &channels[0]; |
| 114 | row(ui, cx, &def.label, &ch.id, channels, keys, px(ROW_H + 6.0), |ui, cx| { |
| 115 | let current = choice_index(cx, def); |
| 116 | let items: Vec<_> = def |
| 117 | .entries |
| 118 | .iter() |
| 119 | .enumerate() |
| 120 | .map(|(i, e)| ui::popup::Item { text: e, current: i == current, ..Default::default() }) |
| 121 | .collect(); |
| 122 | let shown = def.entries.get(current).map_or("", String::as_str); |
| 123 | let part = ("cmb", cx.node.0, ch.id.as_str()); |
| 124 | if let Some(next) = ui::form::select(ui, part, &def.label, shown, &items, fill(), true) { |
| 125 | write_choice(cx, def, next); |
| 126 | } |
| 127 | }); |
| 128 | } |
| 129 | Kind::Point => { |
| 130 | let ch = &channels[0]; |
| 131 | row(ui, cx, &def.label, &ch.id, channels, keys, px(ROW_H), |ui, cx| { |
| 132 | let point = cx.point(ch); |
| 133 | for (axis, name) in ["X", "Y"].into_iter().enumerate() { |
| 134 | ui.leaf(name, Spec { size: [px(10.0), fill()], text: Some(name), font_size: Some(10.0), color: Some(cx.p.text_dim), center: true, ..Spec::default() }); |
| 135 | number_box(ui, cx, ch, Some(axis), point[axis], fill()); |
| 136 | } |
| 137 | spacer(ui); |
| 138 | }); |
| 139 | } |
| 140 | Kind::Text => text(ui, cx, def), |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /// `(left, right)` words under a slider's track. |
| 145 | fn sub_labels(id: &str) -> Option<(&'static str, &'static str)> { |
| 146 | (id == "SubtractiveAdditive").then_some(("Subtractive", "Additive")) |
| 147 | } |
| 148 | |
| 149 | fn spacer(ui: &mut Ui) { |
| 150 | ui.leaf("pad", Spec { size: [px(0.0), fill()], ..Spec::default() }); |
| 151 | } |
| 152 | |
| 153 | /// Label column, `body`, then the expression badge of `marks` and, when `keys`, their keyframe diamond. |
| 154 | fn row(ui: &mut Ui, cx: &mut Cx, label: &str, key: impl std::hash::Hash, marks: &[Channel], keys: bool, height: ui::Extent, body: impl FnOnce(&mut Ui, &mut Cx)) { |
| 155 | ui.open( |
| 156 | ("row", cx.node.0, key), |
| 157 | Spec { size: [fill(), height], axis: ui::Axis::X, gap: 6.0, pad: [4.0, 1.0], ..Spec::default() }, |
| 158 | ); |
| 159 | let label_h = if height == ui::children() { px(ROW_H - 2.0) } else { fill() }; |
| 160 | ui.open("lbl", Spec { size: [px(LABEL_W), label_h], axis: ui::Axis::X, flags: ui::Flags::CLIP, ..Spec::default() }); |
| 161 | ui.leaf("fill", Spec { size: [fill(), fill()], ..Spec::default() }); |
| 162 | ui.leaf( |
| 163 | "text", |
| 164 | Spec { size: [fit(), label_h], text: Some(label), font_size: Some(10.5), color: Some(cx.p.text_dim), ..Spec::default() }, |
| 165 | ); |
| 166 | ui.close(); |
| 167 | body(ui, cx); |
| 168 | if cx.has_expression(marks) { |
| 169 | expression_badge(ui); |
| 170 | } |
| 171 | if !keys { |
| 172 | ui.leaf("mark-gap", Spec { size: [px(MARK_W), fill()], ..Spec::default() }); |
| 173 | } else { |
| 174 | let state = cx.key_state(marks); |
| 175 | if keyframe_button(ui, cx, state) { |
| 176 | cx.toggle_keyframes(label, marks, state); |
| 177 | } |
| 178 | } |
| 179 | ui.close(); |
| 180 | } |
| 181 | |
| 182 | /// The numeric well beside a control; typing commits when it loses the keys. |
| 183 | fn number_box(ui: &mut Ui, cx: &mut Cx, ch: &Channel, axis: Option<usize>, value: f64, width: ui::Extent) { |
| 184 | let id = ui.id(("num", cx.node.0, ch.id.as_str(), axis)); |
| 185 | let focused = ui.focused() == Some(id); |
| 186 | if focused && cx.edit.number.as_ref().is_none_or(|e| e.id != id) { |
| 187 | cx.edit.number = Some(NumberEdit { id, node: cx.node, buf: format_value(ch, value), channel: ch.clone(), axis }); |
| 188 | ui.focus_all(id); |
| 189 | } |
| 190 | let mut shown; |
| 191 | let buf = match cx.edit.number.as_mut() { |
| 192 | Some(edit) if focused => &mut edit.buf, |
| 193 | _ => { |
| 194 | shown = format_value(ch, value); |
| 195 | &mut shown |
| 196 | } |
| 197 | }; |
| 198 | let signal = ui::text_field( |
| 199 | ui, |
| 200 | id, |
| 201 | buf, |
| 202 | "", |
| 203 | Spec { |
| 204 | size: [width, px(BOX_H)], |
| 205 | font_size: Some(10.5), |
| 206 | fill: Some(cx.p.panel_bg), |
| 207 | border: Some(cx.p.rule), |
| 208 | hover_border: Some(cx.p.accent), |
| 209 | radius: 3.0, |
| 210 | pad: [4.0, 2.0], |
| 211 | ..Spec::default() |
| 212 | }, |
| 213 | ); |
| 214 | for event in &signal.events { |
| 215 | match event { |
| 216 | ui::Event::Key { key: Key::Named(NamedKey::Enter), .. } => ui.set_focus(None), |
| 217 | ui::Event::Key { key: Key::Named(NamedKey::Escape), .. } => { |
| 218 | cx.edit.number = None; |
| 219 | ui.set_focus(None); |
| 220 | } |
| 221 | _ => {} |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | fn format_value(ch: &Channel, value: f64) -> String { |
| 227 | if ch.integer { format!("{}", value.round()) } else { format_param_number(value) } |
| 228 | } |
| 229 | |
| 230 | fn checkbox(ui: &mut Ui, part: impl std::hash::Hash, on: bool, label: &str) -> bool { |
| 231 | let id = ui.id(part); |
| 232 | let signal = ui.signal(id); |
| 233 | ui.open_as( |
| 234 | id, |
| 235 | Spec { |
| 236 | cursor: Some(winit::window::CursorIcon::Pointer), |
| 237 | flags: ui::Flags::CLICKABLE | ui::Flags::FOCUSABLE, |
| 238 | size: [px(BOX_H), px(BOX_H)], |
| 239 | role: Some(accesskit::Role::CheckBox), |
| 240 | ..Spec::default() |
| 241 | }, |
| 242 | ); |
| 243 | ui::check(ui, "mark", Some(on), true, signal.hovered, Some(id)); |
| 244 | ui.close(); |
| 245 | if let Some(node) = ui.access(id) { |
| 246 | node.set_label(label); |
| 247 | } |
| 248 | let space = signal.events.iter().any(|e| matches!(e, ui::Event::Key { key: Key::Named(NamedKey::Space), .. })); |
| 249 | signal.clicked || space |
| 250 | } |
| 251 | |
| 252 | fn toggle_button(ui: &mut Ui, cx: &Cx, part: impl std::hash::Hash, label: &str, on: bool) -> bool { |
| 253 | let p = cx.p; |
| 254 | ui.leaf( |
| 255 | part, |
| 256 | Spec { |
| 257 | cursor: Some(winit::window::CursorIcon::Pointer), |
| 258 | flags: ui::Flags::CLICKABLE, |
| 259 | size: [fit(), px(BOX_H)], |
| 260 | text: Some(label), |
| 261 | font_size: Some(10.5), |
| 262 | medium: on, |
| 263 | color: Some(if on { p.text } else { p.text_dim }), |
| 264 | fill: Some(if on { [p.accent[0], p.accent[1], p.accent[2], 0.28] } else { p.panel_bg }), |
| 265 | border: Some(if on { p.accent } else { p.rule }), |
| 266 | hover_border: Some(p.accent), |
| 267 | radius: 3.0, |
| 268 | pad: [7.0, 0.0], |
| 269 | center: true, |
| 270 | ..Spec::default() |
| 271 | }, |
| 272 | ) |
| 273 | .clicked |
| 274 | } |
| 275 | |
| 276 | fn choice_index(cx: &Cx, def: &ControlDef) -> usize { |
| 277 | let ch = &def.channels[0]; |
| 278 | if def.fuid { |
| 279 | let current = cx.comp.node(cx.node).static_str(&ch.id).unwrap_or(&def.default_text); |
| 280 | def.entries.iter().position(|e| e == current).unwrap_or(def.default_index) |
| 281 | } else { |
| 282 | cx.number(ch).round().max(0.0) as usize |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | fn write_choice(cx: &mut Cx, def: &ControlDef, index: usize) { |
| 287 | let ch = &def.channels[0]; |
| 288 | if def.fuid { |
| 289 | if let Some(entry) = def.entries.get(index) { |
| 290 | cx.write_value(&def.label, &ch.id, ParamValue::FuId(entry.clone())); |
| 291 | } |
| 292 | } else { |
| 293 | cx.write_numbers(&def.label, &[(ch, index as f64)], None); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /// Inline picker over the R/G/B(/A) channels, with a slider row per channel under it. |
| 298 | fn color(ui: &mut Ui, cx: &mut Cx, def: &ControlDef) { |
| 299 | let channels = def.channels.as_slice(); |
| 300 | let current: Vec<f64> = channels.iter().map(|ch| cx.number(ch)).collect(); |
| 301 | row(ui, cx, &def.label, ("group", &channels[0].id), channels, true, ui::children(), |ui, cx| { |
| 302 | let rgba = [0, 1, 2, 3].map(|i| current.get(i).map_or(1.0, |v| v.clamp(0.0, 1.0) as f32)); |
| 303 | let mut spec = ui::ColorPicker::new(rgba); |
| 304 | spec.width = fill(); |
| 305 | spec.label = &def.label; |
| 306 | spec.show_alpha = channels.len() >= 4; |
| 307 | let response = ui::color_picker(ui, ("clr", cx.node.0, channels[0].id.as_str()), spec); |
| 308 | if let Some(next) = response.changed { |
| 309 | let edits: Vec<_> = channels |
| 310 | .iter() |
| 311 | .zip(next) |
| 312 | .zip(rgba) |
| 313 | .filter(|((_, new), old)| (new - old).abs() > 1e-4) |
| 314 | .map(|((ch, new), _)| (ch, f64::from(new))) |
| 315 | .collect(); |
| 316 | cx.write_numbers(&def.label, &edits, Some(&channels[0].id)); |
| 317 | } |
| 318 | }); |
| 319 | for (ch, name) in channels.iter().zip(["Red", "Green", "Blue", "Alpha"]) { |
| 320 | row(ui, cx, name, &ch.id, &[], false, px(ROW_H), |ui, cx| { |
| 321 | let value = cx.number(ch); |
| 322 | let mut spec = ui::Slider::new(value as f32, 0.0, 1.0, cx.default_of(ch) as f32); |
| 323 | spec.label = name; |
| 324 | if let Some(next) = ui::slider(ui, ("clr-sld", cx.node.0, ch.id.as_str()), spec) { |
| 325 | cx.write_numbers(&def.label, &[(ch, f64::from(next))], Some(&ch.id)); |
| 326 | } |
| 327 | number_box(ui, cx, ch, None, value, px(52.0)); |
| 328 | }); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /// A text well; edits apply live and fold into one undo step until it loses the keys. |
| 333 | fn text(ui: &mut Ui, cx: &mut Cx, def: &ControlDef) { |
| 334 | let ch = &def.channels[0]; |
| 335 | row(ui, cx, &def.label, &ch.id, &def.channels, false, px(ROW_H), |ui, cx| { |
| 336 | let id = ui.id(("txt", cx.node.0, ch.id.as_str())); |
| 337 | let focused = ui.focused() == Some(id); |
| 338 | let model = cx.comp.node(cx.node).static_str(&ch.id).unwrap_or(&def.default_text).to_owned(); |
| 339 | if focused && cx.edit.text.as_ref().is_none_or(|(owner, _)| *owner != id) { |
| 340 | cx.edit.text = Some((id, model.clone())); |
| 341 | } |
| 342 | let mut shown = model; |
| 343 | let buf = match cx.edit.text.as_mut() { |
| 344 | Some((_, buf)) if focused => buf, |
| 345 | _ => &mut shown, |
| 346 | }; |
| 347 | let before = buf.clone(); |
| 348 | let signal = ui::text_field( |
| 349 | ui, |
| 350 | id, |
| 351 | buf, |
| 352 | &def.label, |
| 353 | Spec { |
| 354 | size: [fill(), px(BOX_H)], |
| 355 | font_size: Some(10.5), |
| 356 | fill: Some(cx.p.panel_bg), |
| 357 | border: Some(cx.p.rule), |
| 358 | hover_border: Some(cx.p.accent), |
| 359 | radius: 3.0, |
| 360 | pad: [6.0, 2.0], |
| 361 | ..Spec::default() |
| 362 | }, |
| 363 | ); |
| 364 | if *buf != before { |
| 365 | let value = if def.fuid { ParamValue::FuId(buf.clone()) } else { ParamValue::Text(buf.clone()) }; |
| 366 | cx.history.push_coalesced(cx.comp, format!("Edit {}", def.label), format!("text:{}:{}", cx.node.0, ch.id)); |
| 367 | cx.comp.set_input_value(cx.node, &ch.id, value); |
| 368 | cx.actions.push(super::InspectorAction::ParamEdited(cx.node)); |
| 369 | } |
| 370 | if signal.events.iter().any(|e| matches!(e, ui::Event::Key { key: Key::Named(NamedKey::Enter), .. })) { |
| 371 | cx.history.break_coalescing(); |
| 372 | } |
| 373 | }); |
| 374 | } |