diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ff3e28f..5f645cb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,3 +66,6 @@ Nothing has been released yet. The first release, 0.1.0, will cover what is on ` ladder), 3D (a waterfall of the last 32 to 256 frames) and Split (the spectrum over a spectrogram), with Hold view, Reset, History and Color in its top bar and every analyser setting in one card. Curve drawing in every expanded window is about three times cheaper (coordinates are written without the float formatter). +- An expanded window for Trance Gate: the step ring (step count, rate) and the pattern track on a panel down the + left, the envelope with its draggable handles across the display, and a card with Attack, Decay, Sustain, + Release, Swing and Mix. The ring and envelope are the card's, drawn and hit tested in the window's geometry. diff --git a/README.md b/README.md index b76d87f7..56af70d0 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,8 @@ axis with draggable crossovers, and Delay, Echo and Reverb, whose window draws t EQ at full size with every control in a row below, Distortion, Tape Saturation and Soft Clipper, whose window draws the transfer curve beside a sine through the device, Bit Crusher, whose window draws one crushed cycle, Utility, and Stereo Widener, whose window draws the stereo field over a live correlation lane, Tuner, and Spectrum, whose window shows the spectrum flat, as a 3D waterfall of its -history, or split over a spectrogram; the other devices open their card in a window for now. +history, or split over a spectrogram, and Trance Gate, whose window draws its step ring and pattern track on a +panel beside the envelope; the other devices open their card in a window for now. [docs/devices.md](docs/devices.md) explains how they work and how to add one. | Kind | Devices | diff --git a/crates/dstudio-app/src/device_editors.rs b/crates/dstudio-app/src/device_editors.rs index 52ca84b2..4afc9890 100644 --- a/crates/dstudio-app/src/device_editors.rs +++ b/crates/dstudio-app/src/device_editors.rs @@ -54,7 +54,7 @@ pub struct CanvasEvent { impl CanvasEvent { /// Card-relative coordinates (pt at 100%). fn pt(&self) -> (f32, f32) { - (self.x / Z, (self.y + FIELD_Y) / Z) + (self.x / Z, (self.y + tg_geo().origin_y) / Z) } } @@ -177,7 +177,7 @@ fn fx(x: f32) -> f32 { x * Z } fn fy(y: f32) -> f32 { - y * Z - FIELD_Y + y * Z - tg_geo().origin_y } /// SVG path data in field px from card coordinates (pt at 100%). @@ -411,22 +411,116 @@ fn env_span(e: &Adsr, step_ms: f32) -> (f32, f32) { (gate, ((raw / grid).ceil() * grid).max(300.0)) } -// Trance Gate geometry (pt at 100%, card relative). +// Trance Gate geometry (pt at 100%): the card's, card relative, and the expanded +// window's, relative to the top left of the window's canvas. const TG_W: f32 = 528.0; -const TG_CX: f32 = 98.0; -const TG_CY: f32 = 157.5; -const TG_R: f32 = 83.0; -const TG_RI: f32 = 0.6 * TG_R; -const TG_PAT: (f32, f32, f32, f32) = (8.0, 285.0, 180.0, 18.0); -const TG_ENV: (f32, f32, f32, f32) = (200.0, 85.0, 320.0, 104.0); -const TG_PLOT: (f32, f32, f32, f32) = (210.0, 95.0, 510.0, 173.0); // x0, y0 (level 1), x1, y1 (level 0) const TG_KNOB_X: f32 = 229.0; +#[derive(Debug, Clone, Copy)] +pub(crate) struct TgGeo { + /// Ring centre and outer radius; the inner radius is 0.6 of it. + cx: f32, + cy: f32, + r: f32, + /// Scale of the ring's centre (step counter, chevrons, rate) and of small text. + s: f32, + ts: f32, + /// Pattern track, and its caption (x, y, h). + pat: (f32, f32, f32, f32), + pat_label: (f32, f32, f32), + /// Envelope well and its fill, its caption (x, y), and the plot: x0, y0 (level 1), x1, y1 (level 0). + env: (f32, f32, f32, f32), + env_fill: bool, + env_label: (f32, f32), + plot: (f32, f32, f32, f32), + /// Field y (px) of y = 0: the card's canvas starts below its header. + origin_y: f32, +} + +const TG_CARD: TgGeo = TgGeo { + cx: 98.0, + cy: 157.5, + r: 83.0, + s: 1.0, + ts: 1.0, + pat: (8.0, 285.0, 180.0, 18.0), + pat_label: (8.0, 266.0, 19.0), + env: (200.0, 85.0, 320.0, 104.0), + env_fill: true, + env_label: (200.0, 66.0), + plot: (210.0, 95.0, 510.0, 173.0), + origin_y: FIELD_Y, +}; + +/// The window: the ring and pattern track on the left panel, the envelope across the display. +pub(crate) const TG_WIN: TgGeo = TgGeo { + cx: 137.0, + cy: 128.0, + r: 127.0, + s: 1.53, + ts: 1.1, + pat: (60.0, 278.0, 204.0, 26.0), + pat_label: (10.0, 278.0, 26.0), + env: (288.0, 0.0, 880.0, 516.0), + env_fill: false, + env_label: (303.0, 3.0), + plot: (303.0, 25.0, 1152.0, 488.0), + origin_y: 0.0, +}; + +#[cfg(test)] +impl TgGeo { + pub(crate) fn ring_cx(&self) -> f32 { + self.cx + } + pub(crate) fn ring_cy(&self) -> f32 { + self.cy + } + pub(crate) fn ring_r(&self) -> f32 { + self.r + } +} + +thread_local! { + static TG_GEO: std::cell::Cell = const { std::cell::Cell::new(TG_CARD) }; +} + +/// The Trance Gate geometry being drawn or hit tested. +fn tg_geo() -> TgGeo { + TG_GEO.with(|g| g.get()) +} + +/// Draws and hit tests with `g` until dropped. +pub(crate) struct TgScope(TgGeo); + +impl TgScope { + pub(crate) fn new(g: TgGeo) -> Self { + Self(TG_GEO.with(|c| c.replace(g))) + } +} + +impl Drop for TgScope { + fn drop(&mut self) { + TG_GEO.with(|c| c.set(self.0)); + } +} + +fn tg_cursor(hit: Hit) -> i32 { + match hit { + Hit::Step(_) | Hit::Pattern(_) | Hit::StepMore | Hit::StepFewer | Hit::Rate => CURSOR_POINTER, + Hit::StepCount => CURSOR_NS, + Hit::EnvA | Hit::EnvR => CURSOR_EW, + Hit::EnvD => CURSOR_MOVE, + _ => 0, + } +} + fn step_angle(i: f32, steps: u32) -> f32 { std::f32::consts::TAU * i / steps.max(1) as f32 } fn polar(r: f32, a: f32) -> (f32, f32) { - (TG_CX + r * a.sin(), TG_CY - r * a.cos()) + let g = tg_geo(); + (g.cx + r * a.sin(), g.cy - r * a.cos()) } fn sector(r0: f32, r1: f32, a0: f32, a1: f32) -> PathData { let (o0, o1, i1, i0) = (polar(r1, a0), polar(r1, a1), polar(r0, a1), polar(r0, a0)); @@ -580,17 +674,47 @@ impl Controller { } fn trance_gate_card(&self, d: &Device, def: &'static DeviceDef, card: &mut DeviceCard) { - let accent = card.accent; + let (hit, _) = self.hover_of(d.id); + let mut c = Canvas::default(); + self.tg_draw(d, def, card.accent, &mut c); + // Knobs: Attack, Decay, Sustain, Release, Swing, Mix (cells 50 wide, gap 4). + let knobs: Vec = (2..8).map(|i| knob(def, self.fv(d, def, i), i, TG_KNOB_X + 54.0 * (i - 2) as f32)).collect(); + card.knobs = ModelRc::from(Rc::new(VecModel::from(knobs))); + card.canvas = c.into_canvas(TG_W, tg_cursor(hit)); + } + + /// The Trance Gate's expanded window picture (src/device_window.rs places it): the ring + /// and pattern track on the left panel, the envelope across the display. `w` x `h` pt. + pub(crate) fn trance_gate_window_canvas(&self, d: &Device, def: &'static DeviceDef, accent: Color, w: f32, h: f32) -> PanelCanvas { + let _g = TgScope::new(TG_WIN); + let (hit, _) = self.hover_of(d.id); + let mut c = Canvas::default(); + self.tg_draw(d, def, accent, &mut c); + PanelCanvas { + visible: true, + w: w * Z, + h: h * Z, + paths: ModelRc::from(Rc::new(VecModel::from(c.paths))), + texts: ModelRc::from(Rc::new(VecModel::from(c.texts))), + interactive: true, + cursor: tg_cursor(hit), + ..Default::default() + } + } + + /// The ring, pattern track and envelope in the current geometry ([`TgScope`]). + fn tg_draw(&self, d: &Device, def: &'static DeviceDef, accent: Color, c: &mut Canvas) { + let g = tg_geo(); + let (s, ri) = (g.s, 0.6 * g.r); let (hit, _) = self.hover_of(d.id); let (pattern, steps, mask) = self.tg_pattern(d, def); let rate = (self.fv(d, def, 1).round() as usize).min(tg::RATES.len() - 1); let swing = self.fv(d, def, 6) / 100.0; let env = self.adsr(d, def); let step_ms = self.step_ms(rate); - let mut c = Canvas::default(); // Ring: one annular sector per step (swing-warped), then the gate-shape preview. - let gap = 0.6 / TG_R; + let gap = 0.6 / g.r; for i in 0..steps as usize { let a0 = step_angle(step_start(i, swing), steps) + gap; let a1 = step_angle(if i + 1 == steps as usize { steps as f32 } else { step_start(i + 1, swing) }, steps) - gap; @@ -600,7 +724,7 @@ impl Controller { let on = mask >> i & 1 == 1; let hot = hit == Hit::Step(i); let fill = if on { alpha(accent, if hot { 0.16 } else { 0.08 }) } else { ink(if hot { 0.10 } else { 0.045 }) }; - c.fill(sector(TG_RI, TG_R, a0, a1), fill); + c.fill(sector(ri, g.r, a0, a1), fill); } let levels = ring_levels(&env, steps, mask, swing, step_ms); let n = levels.len(); @@ -613,7 +737,7 @@ impl Controller { let j0 = ((a0 / std::f32::consts::TAU) * n as f32).ceil() as usize; let j1 = (((a1 / std::f32::consts::TAU) * n as f32).floor() as usize).min(n - 1); let mut outer: Vec<(f32, f32)> = Vec::new(); - let r_of = |lv: f32| TG_RI + (TG_R - TG_RI) * lv; + let r_of = |lv: f32| ri + (g.r - ri) * lv; outer.push(polar(r_of(levels[j0.min(n - 1)]), a0)); for (j, lv) in levels.iter().enumerate().take(j1 + 1).skip(j0) { outer.push(polar(r_of(*lv), std::f32::consts::TAU * j as f32 / n as f32)); @@ -622,19 +746,19 @@ impl Controller { if levels[j0.min(n - 1)..=j1].iter().all(|l| *l < 0.004) { continue; } - let (i1, i0) = (polar(TG_RI, a1), polar(TG_RI, a0)); - let shape = PathData::default().poly(&outer).l(i1.0, i1.1).a(TG_RI, a1 - a0 > std::f32::consts::PI, false, i0.0, i0.1).z(); + let (i1, i0) = (polar(ri, a1), polar(ri, a0)); + let shape = PathData::default().poly(&outer).l(i1.0, i1.1).a(ri, a1 - a0 > std::f32::consts::PI, false, i0.0, i0.1).z(); c.fill(shape, alpha(accent, 0.34)); c.stroke(PathData::default().poly(&outer), accent, 1.5); } - c.stroke(PathData::default().circle(TG_CX, TG_CY, TG_RI - 4.2), ink(0.05), 1.0); + c.stroke(PathData::default().circle(g.cx, g.cy, ri - 4.2), ink(0.05), 1.0); // Playhead while the transport plays. if self.engine.transport().playing { let beats = self.playhead_ticks() as f64 / PPQ as f64; let pos = (beats / tg::RATE_QUARTERS[rate]).rem_euclid(steps as f64) as f32; if pos > 0.01 { let a1 = step_angle(pos, steps); - let r = TG_RI - 4.2; + let r = ri - 4.2; let (sx, sy) = polar(r, 0.0); let (ex, ey) = polar(r, a1); c.stroke(PathData::default().m(sx, sy).a(r, a1 > std::f32::consts::PI, true, ex, ey), alpha(accent, 0.7), 2.0); @@ -642,12 +766,22 @@ impl Controller { } // Centre: step counter with chevrons, rate pill. let count_hot = matches!(hit, Hit::StepCount); - c.text(TG_CX - 32.0, TG_CY - 26.0, 48.0, 28.0, steps.to_string(), ink(if count_hot { 1.0 } else { 0.92 }), 28.0, 600, 1); + c.text( + g.cx - 32.0 * s, + g.cy - 26.0 * s, + 48.0 * s, + 28.0 * s, + steps.to_string(), + ink(if count_hot { 1.0 } else { 0.92 }), + 28.0 * s, + 600, + 1, + ); for (k, h) in [(0, Hit::StepMore), (1, Hit::StepFewer)] { let enabled = if k == 0 { steps < tg::MAX_STEPS } else { steps > 1 }; - let cy = TG_CY - 18.0 + 14.0 * k as f32; + let cy = g.cy + (-18.0 + 14.0 * k as f32) * s; c.chevron( - TG_CX + 19.0, + g.cx + 19.0 * s, cy, k == 1, ink(if !enabled { @@ -661,29 +795,29 @@ impl Controller { } let rate_hot = hit == Hit::Rate; c.text( - TG_CX - 26.0, - TG_CY + 5.0, - 44.0, - 18.0, + g.cx - 26.0 * s, + g.cy + 5.0 * s, + 44.0 * s, + 18.0 * s, tg::RATES[rate], if rate_hot { Color::from_rgb_u8(255, 255, 255) } else { accent.brighter(0.2) }, - 13.0, + 13.0 * g.ts, 600, 2, ); - c.chevron(TG_CX + 23.0, TG_CY + 14.0, true, rgba(0xa6, 0xa8, 0xad, 1.0)); + c.chevron(g.cx + 23.0 * s, g.cy + 14.0 * s, true, rgba(0xa6, 0xa8, 0xad, 1.0)); // Pattern choice track. - c.text(TG_PAT.0, TG_PAT.1 - 19.0, 80.0, 19.0, "Pattern", ink(0.4), 12.0, 600, 0); - c.fill(PathData::default().rrect(TG_PAT.0, TG_PAT.1, TG_PAT.2, TG_PAT.3, TG_PAT.3 / 2.0), rgba(0x29, 0x2a, 0x2f, 1.0)); - let cell = TG_PAT.2 / tg::PATTERN_COUNT as f32; + c.text(g.pat_label.0, g.pat_label.1, 80.0, g.pat_label.2, "Pattern", ink(0.4), 12.0, 600, 0); + c.fill(PathData::default().rrect(g.pat.0, g.pat.1, g.pat.2, g.pat.3, g.pat.3 / 2.0), rgba(0x29, 0x2a, 0x2f, 1.0)); + let cell = g.pat.2 / tg::PATTERN_COUNT as f32; for i in 0..tg::PATTERN_COUNT { - let x = TG_PAT.0 + cell * i as f32; + let x = g.pat.0 + cell * i as f32; let sel = i == pattern; if sel { - c.fill(PathData::default().rrect(x + 2.0, TG_PAT.1 + 2.0, cell - 4.0, TG_PAT.3 - 4.0, (TG_PAT.3 - 4.0) / 2.0), accent); + c.fill(PathData::default().rrect(x + 2.0, g.pat.1 + 2.0, cell - 4.0, g.pat.3 - 4.0, (g.pat.3 - 4.0) / 2.0), accent); } else if hit == Hit::Pattern(i) { - c.fill(PathData::default().rrect(x + 2.0, TG_PAT.1 + 2.0, cell - 4.0, TG_PAT.3 - 4.0, (TG_PAT.3 - 4.0) / 2.0), ink(0.04)); + c.fill(PathData::default().rrect(x + 2.0, g.pat.1 + 2.0, cell - 4.0, g.pat.3 - 4.0, (g.pat.3 - 4.0) / 2.0), ink(0.04)); } let col = if sel { rgba(0x17, 0x18, 0x1b, 1.0) @@ -692,17 +826,19 @@ impl Controller { } else { ink(0.7) }; - c.text(x, TG_PAT.1, cell, TG_PAT.3, (i + 1).to_string(), col, 11.0, 600, 1); + c.text(x, g.pat.1, cell, g.pat.3, (i + 1).to_string(), col, 11.0 * g.ts, 600, 1); } // Envelope. - c.text(TG_ENV.0, TG_ENV.1 - 19.0, 120.0, 19.0, "Envelope", ink(0.4), 12.0, 600, 0); - c.fill(PathData::default().rrect(TG_ENV.0, TG_ENV.1, TG_ENV.2, TG_ENV.3, 6.0), rgba(0, 0, 0, 0.26)); + c.text(g.env_label.0, g.env_label.1, 120.0, 19.0, "Envelope", ink(0.4), 12.0, 600, 0); + if g.env_fill { + c.fill(PathData::default().rrect(g.env.0, g.env.1, g.env.2, g.env.3, 6.0), rgba(0, 0, 0, 0.26)); + } let (gate_ms, span) = match &self.devices.panel.editors.gesture { Some(Gesture::Env { device, span, gate_ms, .. }) if *device == d.id => (*gate_ms, *span), _ => env_span(&env, step_ms), }; - let (x0, y0, x1, y1) = TG_PLOT; + let (x0, y0, x1, y1) = g.plot; let tx = |t: f32| x0 + (t / span).clamp(0.0, 1.0) * (x1 - x0); let ly = |l: f32| y1 - l.clamp(0.0, 1.0) * (y1 - y0); let mut grid = PathData::default(); @@ -758,54 +894,44 @@ impl Controller { } c.text(x0, y1 + 1.0, 80.0, 15.0, "Gate", ink(0.55), 10.5, 600, 0); c.text(x1 - 100.0, y1 + 1.0, 100.0, 15.0, format!("{} ms", span.round()), ink(0.55), 10.5, 600, 2); - - // Knobs: Attack, Decay, Sustain, Release, Swing, Mix (cells 50 wide, gap 4). - let knobs: Vec = (2..8).map(|i| knob(def, self.fv(d, def, i), i, TG_KNOB_X + 54.0 * (i - 2) as f32)).collect(); - card.knobs = ModelRc::from(Rc::new(VecModel::from(knobs))); - let cursor = match hit { - Hit::Step(_) | Hit::Pattern(_) | Hit::StepMore | Hit::StepFewer | Hit::Rate => CURSOR_POINTER, - Hit::StepCount => CURSOR_NS, - Hit::EnvA | Hit::EnvR => CURSOR_EW, - Hit::EnvD => CURSOR_MOVE, - _ => 0, - }; - card.canvas = c.into_canvas(TG_W, cursor); } fn tg_hit(&self, d: &Device, def: &DeviceDef, p: (f32, f32)) -> Hit { let (_, steps, _) = self.tg_pattern(d, def); let swing = self.fv(d, def, 6) / 100.0; - let (dx, dy) = (p.0 - TG_CX, p.1 - TG_CY); + let g = tg_geo(); + let (s, ri) = (g.s, 0.6 * g.r); + let (dx, dy) = (p.0 - g.cx, p.1 - g.cy); let dist = (dx * dx + dy * dy).sqrt(); - if in_rect(p, TG_CX + 11.0, TG_CY - 25.0, 16.0, 13.0) { + if in_rect(p, g.cx + 11.0 * s, g.cy - 25.0 * s, 16.0 * s, 13.0 * s) { return Hit::StepMore; } - if in_rect(p, TG_CX + 11.0, TG_CY - 11.0, 16.0, 13.0) { + if in_rect(p, g.cx + 11.0 * s, g.cy - 11.0 * s, 16.0 * s, 13.0 * s) { return Hit::StepFewer; } - if in_rect(p, TG_CX - 32.0, TG_CY - 26.0, 43.0, 28.0) { + if in_rect(p, g.cx - 32.0 * s, g.cy - 26.0 * s, 43.0 * s, 28.0 * s) { return Hit::StepCount; } - if in_rect(p, TG_CX - 26.0, TG_CY + 5.0, 54.0, 18.0) { + if in_rect(p, g.cx - 26.0 * s, g.cy + 5.0 * s, 54.0 * s, 18.0 * s) { return Hit::Rate; } - if (TG_RI - 4.0..=TG_R + 6.0).contains(&dist) { + if (ri - 4.0..=g.r + 6.0).contains(&dist) { let a = dx.atan2(-dy).rem_euclid(std::f32::consts::TAU); let u = a / std::f32::consts::TAU * steps as f32; let k = swung_step(u as f64, swing as f64).rem_euclid(steps as i64) as usize; return Hit::Step(k); } - if dist < TG_RI - 4.0 { + if dist < ri - 4.0 { return Hit::Hole; } - if in_rect(p, TG_PAT.0, TG_PAT.1, TG_PAT.2, TG_PAT.3) { - return Hit::Pattern((((p.0 - TG_PAT.0) / (TG_PAT.2 / 8.0)) as usize).min(7)); + if in_rect(p, g.pat.0, g.pat.1, g.pat.2, g.pat.3) { + return Hit::Pattern((((p.0 - g.pat.0) / (g.pat.2 / 8.0)) as usize).min(7)); } - if in_rect(p, TG_ENV.0, TG_ENV.1, TG_ENV.2, TG_ENV.3) { + if in_rect(p, g.env.0, g.env.1, g.env.2, g.env.3) { let env = self.adsr(d, def); let rate = (self.fv(d, def, 1).round() as usize).min(tg::RATES.len() - 1); let (gate, span) = env_span(&env, self.step_ms(rate)); - let (x0, y0, x1, y1) = TG_PLOT; + let (x0, y0, x1, y1) = g.plot; let tx = |t: f32| x0 + (t / span).clamp(0.0, 1.0) * (x1 - x0); let ly = |l: f32| y1 - l * (y1 - y0); let mut best = (Hit::None, 12.0f32); @@ -1287,12 +1413,16 @@ impl Controller { // ---------------------------------------------------------------- events - pub fn panel_editor(&mut self, device: DeviceId, e: CanvasEvent) { + pub fn panel_editor(&mut self, device: DeviceId, mut e: CanvasEvent) { + // Kind + 16: the event comes from the device's expanded window, drawn in its geometry. + let win = e.kind >= 16; + e.kind %= 16; let Some((d, def)) = self.panel_def(device) else { return }; let name = match def.layout.view { Some(dstudio_core::builtin::View::Custom(n)) => n, _ => return, }; + let _geo = (win && name == "trance-gate").then(|| TgScope::new(TG_WIN)); let p = e.pt(); let hit = match name { "trance-gate" => self.tg_hit(d, def, p), @@ -1350,7 +1480,7 @@ impl Controller { self.devices.panel.editors.gesture = Some(Gesture::Paint { device, on, last: k }); } Gesture::Env { device, handle, span, gate_ms } => { - let (x0, y0, x1, y1) = TG_PLOT; + let (x0, y0, x1, y1) = tg_geo().plot; let t = ((p.0 - x0) / (x1 - x0)).clamp(0.0, 1.0) * span; let Some((d, _)) = self.panel_def(device) else { return }; let attack = self.fv(d, def, 2); diff --git a/crates/dstudio-app/src/device_window.rs b/crates/dstudio-app/src/device_window.rs index 441bfcef..002e82b8 100644 --- a/crates/dstudio-app/src/device_window.rs +++ b/crates/dstudio-app/src/device_window.rs @@ -60,6 +60,8 @@ const BOX_RULE: i32 = 1; const BOX_TRACK: i32 = 2; const BOX_HEADER_RULE: i32 = 3; const BOX_DISPLAY: i32 = 4; +/// Left edge of the Trance Gate's envelope display and card (its ring panel is left of it). +const TG_DISPLAY_X: f32 = 304.0; /// Window actions (Studio.win-action, MenuPick::Window). pub(crate) const ACT_SECTION: i32 = 1; @@ -128,7 +130,7 @@ pub(crate) fn has_window(def: &DeviceDef) -> bool { def.id, "auto-filter" | "channeleq" | "character-eq" | "transient-shaper" | "multiband-compressor" | "delay" | "reverb" | "echo" ) || is_wave_window(def) - || matches!(def.id, "utility" | "widener" | "tuner" | "spectrum") + || matches!(def.id, "utility" | "widener" | "tuner" | "spectrum" | "trance-gate") || def.id == "bitcrusher" || is_field_window(def) || is_dynamics_window(def) @@ -194,6 +196,10 @@ pub(crate) fn kit(def: &DeviceDef) -> Kit { // Stereo Widener: the field over the correlation lane. // Spectrum: the analyser across the display, one Level meter beside it. "spectrum" => Kit { w: 1200.0, display: (INSET, 52.0, 1108.0, 514.0), plot: (36.0, 32.0, 42.0, 34.0), narrow: true, meters_h: 0.0 }, + // Trance Gate: the envelope across the display right of the ring panel; no meters. + "trance-gate" => { + Kit { w: 1200.0, display: (TG_DISPLAY_X, 56.0, 880.0, 516.0), plot: (0.0, 0.0, 0.0, 0.0), narrow: true, meters_h: 0.0 } + } // Tuner: the bar across a tall display, one In meter beside it. "tuner" => Kit { w: 1200.0, display: (INSET, 52.0, 1108.0, 550.0), plot: (0.0, 0.0, 0.0, 0.0), narrow: true, meters_h: 0.0 }, // Its meters run down past the card, as the Bit Crusher's. @@ -493,6 +499,8 @@ struct Builder { accent: Color, /// Window content width. w: f32, + /// Left edge of the control cards (INSET; the Trance Gate's start right of its ring panel). + left: f32, boxes: Vec, pills: Vec, knobs: Vec, @@ -506,7 +514,16 @@ fn accent_of(b: &Builder) -> Color { impl Builder { fn new(accent: Color, w: f32) -> Self { - Builder { accent, w, boxes: Vec::new(), pills: Vec::new(), knobs: Vec::new(), texts: Vec::new(), square: PanelCanvas::default() } + Builder { + accent, + w, + left: INSET, + boxes: Vec::new(), + pills: Vec::new(), + knobs: Vec::new(), + texts: Vec::new(), + square: PanelCanvas::default(), + } } fn rect(&mut self, kind: i32, x: f32, y: f32, w: f32, h: f32) { @@ -665,6 +682,8 @@ impl Controller { _ if def.id == "bitcrusher" => self.crush_window_canvas(d, def, &k, accent), _ if def.id == "widener" => self.widener_window_canvas(d, def, &k, accent), _ if def.id == "spectrum" => self.spectrum_window_canvas(d, def, &k, accent), + // The ring panel and the envelope display share one canvas from the panel's corner. + _ if def.id == "trance-gate" => self.trance_gate_window_canvas(d, def, accent, dx + dw - INSET, dh), // The card's tuner at twice its scale, its bar across the middle of the display. _ if def.id == "tuner" => { PanelCanvas { well: false, ..self.tuner_canvas_at(d, def, k.display.2 / 2.0, TUNER_WIN_H, 2.0 * Z, accent) } @@ -674,6 +693,9 @@ impl Controller { }; display.x = (dx + k.plot.0) * Z; display.y = (dy + k.plot.1) * Z; + if def.id == "trance-gate" { + display.x = INSET * Z; + } let height = match def.id { "auto-filter" => self.auto_filter_window(d, def, &mut b), "channeleq" => self.channel_eq_window(d, def, &mut b), @@ -689,9 +711,14 @@ impl Controller { "widener" => self.widener_window(d, def, &mut b, dy + dh + 12.0), "tuner" => self.tuner_window(d, def, &mut b, dy + dh + 14.0), "spectrum" => self.spectrum_window(d, def, &mut b, dy + dh + 12.0), + "trance-gate" => self.trance_gate_window(d, def, &mut b, dy + dh + 12.0), _ => self.field_window(d, def, &mut b, dy + dh + 12.0), }; - let (mut meters, ticks, mut ticks_x) = self.window_meters(d.id, &k); + let (mut meters, mut ticks, mut ticks_x) = self.window_meters(d.id, &k); + if def.id == "trance-gate" { + meters.clear(); + ticks.clear(); + } // The Tuner meters its input only; the Spectrum its level. if matches!(def.id, "tuner" | "spectrum") { meters.truncate(1); @@ -942,12 +969,12 @@ impl Controller { /// A row of clusters in a card: places every item, the card and its rules. fn win_card(&self, d: &Device, def: &'static DeviceDef, b: &mut Builder, row: Row, clusters: Vec<(Vec, f32)>) { - b.rect(BOX_CARD, INSET, row.y, b.w - 2.0 * INSET, row.h); + b.rect(BOX_CARD, b.left, row.y, b.w - b.left - INSET, row.h); let widths: Vec = clusters .iter() .map(|(items, gap)| items.iter().map(Item::width).sum::() + gap * (items.len().saturating_sub(1)) as f32) .collect(); - let (xs, rules) = gutter(INSET, b.w - 2.0 * INSET, &widths); + let (xs, rules) = gutter(b.left, b.w - b.left - INSET, &widths); let rule_h = row.h - 24.0; for rx in rules { b.rect(BOX_RULE, rx, row.y + 12.0, 1.0, rule_h); @@ -1785,6 +1812,28 @@ impl Controller { row.y + row.h + 16.0 } + /// Trance Gate: the ring panel down the left; under the envelope, one card with the + /// envelope's Attack, Decay, Sustain and Release, Swing, and Mix. + fn trance_gate_window(&self, d: &Device, def: &'static DeviceDef, b: &mut Builder, y: f32) -> f32 { + let knob = |id, name| Item::Knob(id, name, 1, 62.0, false); + let row = Row { y, h: 100.0, dial: 26.0, value: 52.0, pill: 28.0, picker: 44.0 }; + b.rect(BOX_CARD, INSET, 56.0, TG_DISPLAY_X - INSET - 14.0, row.y + row.h - 56.0); + b.left = TG_DISPLAY_X; + self.win_card( + d, + def, + b, + row, + vec![ + (vec![knob("attack", "Attack"), knob("decay", "Decay"), knob("sustain", "Sustain"), knob("release", "Release")], 0.0), + (vec![knob("swing", "Swing")], 0.0), + (vec![knob("mix", "Mix")], 0.0), + ], + ); + b.left = INSET; + row.y + row.h + 16.0 + } + /// Stereo Widener: one card under the display: Width and Low Cut, Mid and Side Gain, Mix. fn widener_window(&self, d: &Device, def: &'static DeviceDef, b: &mut Builder, y: f32) -> f32 { let knob = |id, name, size, w| Item::Knob(id, name, size, w, false); @@ -3376,6 +3425,39 @@ mod tests { } } + /// Trance Gate: the ring panel and envelope share one interactive canvas, no meters, and + /// a press on the window's ring toggles the step under it (window geometry, not the card's). + #[test] + fn trance_gate_window_toggles_steps_on_its_own_ring() { + use slint::Model; + let (mut c, did, def) = controller_with("trance-gate"); + assert!(has_window(def)); + let v = view(&c, did, def); + assert!((v.w / Z - 1200.0).abs() < 0.5 && (v.h / Z - 700.0).abs() < 0.5, "{} x {}", v.w / Z, v.h / Z); + assert_eq!(v.meters.row_count(), 0); + assert!(v.display.interactive && v.display.paths.row_count() > 10); + assert_eq!(knob_names(&v), ["Attack", "Decay", "Sustain", "Release", "Swing", "Mix"]); + assert!(v.display.texts.iter().any(|t| t.text == "Envelope")); + let bits = |c: &Controller| { + let d = c.project().device(did).unwrap(); + c.panel_value(d, def, def.param("p1_lo").unwrap()).round() as u32 + }; + let before = bits(&c); + // The middle of step 1 on the window's ring (16 steps), in canvas px. + let g = crate::device_editors::TG_WIN; + let (r, a) = (0.8 * g.ring_r(), std::f32::consts::TAU * 0.5 / 16.0); + let (x, y) = (g.ring_cx() + r * a.sin(), g.ring_cy() - r * a.cos()); + let ev = + |kind| crate::device_editors::CanvasEvent { kind, x: x * Z, y: y * Z, wx: 0.0, wy: 0.0, shift: false, alt: false, wheel: 0.0 }; + c.panel_editor(did, ev(16)); + c.panel_editor(did, ev(18)); + assert_eq!(bits(&c) ^ before, 1, "step 1 toggled"); + // The same point on the card misses the ring (its ring is smaller and higher). + c.panel_editor(did, ev(0)); + c.panel_editor(did, ev(2)); + assert_eq!(bits(&c) ^ before, 1, "the card's geometry did not take the window's point"); + } + /// Spectrum: three views from one history; Reset clears it, Hold view stops taking /// frames, the History picker is inert in 2D. #[test] @@ -3453,6 +3535,7 @@ mod tests { "widener", "tuner", "spectrum", + "trance-gate", ] { let (mut c, did, def) = controller_with(id); let h: std::collections::VecDeque<_> = diff --git a/crates/dstudio-app/ui/device_panel.slint b/crates/dstudio-app/ui/device_panel.slint index 733b4be8..d61e56c6 100644 --- a/crates/dstudio-app/ui/device_panel.slint +++ b/crates/dstudio-app/ui/device_panel.slint @@ -437,6 +437,8 @@ export component DeviceCanvas inherits Rectangle { in property accent; // In a device window: Rust maps the well with the window's geometry (src/device_window.rs). in property window; + // Editor events from a device window carry kind + 16 (src/device_editors.rs). + property ekind: root.window ? 16 : 0; property zm: PanelStyle.z; property mouse-cursor: root.f.cursor == 1 ? MouseCursor.move : root.f.cursor == 2 ? MouseCursor.ew-resize : root.f.cursor == 3 ? MouseCursor.ns-resize : root.f.cursor == 4 ? MouseCursor.pointer : root.f.cursor == 5 ? MouseCursor.crosshair : MouseCursor.default; @@ -526,27 +528,27 @@ export component DeviceCanvas inherits Rectangle { pointer-event(e) => { if (e.kind == PointerEventKind.down && e.button == PointerEventButton.left) { keys.focus(); - Studio.panel-editor(root.device, 0, self.mouse-x, self.mouse-y, self.absolute-position.x + self.mouse-x, (self.absolute-position.y - Studio.menu-dy) + self.mouse-y, e.modifiers.shift, e.modifiers.alt, 0); + Studio.panel-editor(root.device, root.ekind + 0, self.mouse-x, self.mouse-y, self.absolute-position.x + self.mouse-x, (self.absolute-position.y - Studio.menu-dy) + self.mouse-y, e.modifiers.shift, e.modifiers.alt, 0); } else if (e.kind == PointerEventKind.down && e.button == PointerEventButton.right) { - Studio.panel-editor(root.device, 5, self.mouse-x, self.mouse-y, self.absolute-position.x + self.mouse-x, (self.absolute-position.y - Studio.menu-dy) + self.mouse-y, e.modifiers.shift, e.modifiers.alt, 0); + Studio.panel-editor(root.device, root.ekind + 5, self.mouse-x, self.mouse-y, self.absolute-position.x + self.mouse-x, (self.absolute-position.y - Studio.menu-dy) + self.mouse-y, e.modifiers.shift, e.modifiers.alt, 0); } else if (e.kind == PointerEventKind.move) { - Studio.panel-editor(root.device, self.pressed ? 1 : 4, self.mouse-x, self.mouse-y, self.absolute-position.x + self.mouse-x, (self.absolute-position.y - Studio.menu-dy) + self.mouse-y, e.modifiers.shift, e.modifiers.alt, 0); + Studio.panel-editor(root.device, root.ekind + (self.pressed ? 1 : 4), self.mouse-x, self.mouse-y, self.absolute-position.x + self.mouse-x, (self.absolute-position.y - Studio.menu-dy) + self.mouse-y, e.modifiers.shift, e.modifiers.alt, 0); } else if (e.kind == PointerEventKind.up && e.button == PointerEventButton.left) { - Studio.panel-editor(root.device, 2, self.mouse-x, self.mouse-y, self.absolute-position.x + self.mouse-x, (self.absolute-position.y - Studio.menu-dy) + self.mouse-y, e.modifiers.shift, e.modifiers.alt, 0); + Studio.panel-editor(root.device, root.ekind + 2, self.mouse-x, self.mouse-y, self.absolute-position.x + self.mouse-x, (self.absolute-position.y - Studio.menu-dy) + self.mouse-y, e.modifiers.shift, e.modifiers.alt, 0); } else if (e.kind == PointerEventKind.cancel) { - Studio.panel-editor(root.device, 3, self.mouse-x, self.mouse-y, 0, 0, false, false, 0); + Studio.panel-editor(root.device, root.ekind + 3, self.mouse-x, self.mouse-y, 0, 0, false, false, 0); } } double-clicked => { - Studio.panel-editor(root.device, 6, self.mouse-x, self.mouse-y, self.absolute-position.x + self.mouse-x, (self.absolute-position.y - Studio.menu-dy) + self.mouse-y, false, false, 0); + Studio.panel-editor(root.device, root.ekind + 6, self.mouse-x, self.mouse-y, self.absolute-position.x + self.mouse-x, (self.absolute-position.y - Studio.menu-dy) + self.mouse-y, false, false, 0); } scroll-event(e) => { if (e.delta-y == 0) { return reject; } - Studio.panel-editor(root.device, 7, self.mouse-x, self.mouse-y, 0, 0, e.modifiers.shift, e.modifiers.alt, e.delta-y / 1px); + Studio.panel-editor(root.device, root.ekind + 7, self.mouse-x, self.mouse-y, 0, 0, e.modifiers.shift, e.modifiers.alt, e.delta-y / 1px); accept } changed has-hover => { - if (!self.has-hover) { Studio.panel-editor(root.device, 8, 0, 0, 0, 0, false, false, 0); } + if (!self.has-hover) { Studio.panel-editor(root.device, root.ekind + 8, 0, 0, 0, 0, false, false, 0); } } } } diff --git a/docs/devices.md b/docs/devices.md index 4a0dd5b4..27db2b5f 100644 --- a/docs/devices.md +++ b/docs/devices.md @@ -67,7 +67,7 @@ flowchart LR Flanger, Phaser, Ring Mod and Character Mod; and the dynamics family: Compressor, Limiter, Gate, Glue Compressor, Character Compressor and Mastering Limiter; Transient Shaper; Multiband Compressor; Delay, Echo and Reverb; Distortion, Tape Saturation, - Soft Clipper and Bit Crusher; Utility, Stereo Widener, Tuner and Spectrum), otherwise the card itself in a window. A + Soft Clipper and Bit Crusher; Utility, Stereo Widener, Tuner and Spectrum; Trance Gate), otherwise the card itself in a window. A window is a face 960 pt wide (Ring Mod 860): a top bar (power, presets, the device's header pills), a large display (the card's canvas drawn at window size with its axis and legend), In / Out meters, and rows of controls laid out with the same @@ -132,7 +132,11 @@ flowchart LR its own history of analyser frames (`SpecHistory`, as deep as the History picker, frozen by Hold view, cleared by Reset) and draws it as a waterfall (up to 32 lines) or a spectrogram (up to 64 rows), each frame placed at its - age so a history still filling up starts at the front. The + age so a history still filling up starts at the front. The Trance Gate + window reuses the card's editor (src/device_editors.rs): `tg_draw` and + `tg_hit` read their geometry from `TgScope` (`TG_CARD` or `TG_WIN`), and + the window's canvas sends its editor events with kind + 16 so they are hit + tested in the window's geometry. The meters read `se_builtin_set_metering` / `se_builtin_levels` (`Engine::builtin_levels(id, &mut [f32; 4])`): while a window is open the audio thread keeps the input and output peak of each block, and the app