diff --git a/Cargo.lock b/Cargo.lock index 5070395..e1ab834 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2084,7 +2084,7 @@ dependencies = [ [[package]] name = "maolan-widgets" -version = "0.1.1" +version = "0.1.2" dependencies = [ "iced", "iced_aw", diff --git a/Cargo.toml b/Cargo.toml index 3bef881..6aff488 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "maolan-widgets" -version = "0.1.1" +version = "0.1.2" edition = "2024" description = "Widgets used for Maolan DAW" license = "BSD-2-Clause" diff --git a/src/adsr.rs b/src/adsr.rs index 8e04dc8..00bfd6d 100644 --- a/src/adsr.rs +++ b/src/adsr.rs @@ -2,20 +2,23 @@ use iced::{ Color, Element, Event, Length, Point, Rectangle, Renderer, Theme, mouse, widget::{ - button, canvas, + canvas, canvas::{Action, Frame, Geometry, Path, Program, Stroke, Text}, - column, row, text, + column, container, mouse_area, row, text, text_input, }, }; /// Edits emitted by [`adsr_envelope`]. Stage indices follow attack, decay, /// sustain, release order. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub enum AdsrMessage { Change(usize, f32), Release(usize), Reset(usize), Assign(usize), + Edit(usize), + EditChanged(usize, String), + EditSubmit(usize), } /// Draw an interactive ADSR envelope. @@ -28,6 +31,27 @@ pub fn adsr_envelope<'a>( ranges: [(f32, f32); 4], steps: [f32; 4], assigned: [bool; 4], +) -> Element<'a, AdsrMessage> { + adsr_envelope_with_edit( + values, + ranges, + steps, + assigned, + None, + "", + iced::widget::Id::unique(), + ) +} + +/// Draw an envelope with an optional editable stage value. +pub fn adsr_envelope_with_edit<'a>( + values: [f32; 4], + ranges: [(f32, f32); 4], + steps: [f32; 4], + assigned: [bool; 4], + editing: Option, + edit_value: &'a str, + input_id: iced::widget::Id, ) -> Element<'a, AdsrMessage> { let graph = Envelope { values, @@ -41,18 +65,30 @@ pub fn adsr_envelope<'a>( } else { format!("{:.2}s", values[i]) }; - button(column![ - text(["Attack", "Decay", "Sustain", "Release"][i]).size(10), - text(value).size(10) - ]) - .padding(2) - .width(Length::Fill) - .style(button::text) - .on_press(AdsrMessage::Assign(i)) - .into() + let label: Element<'a, AdsrMessage> = if editing == Some(i) { + text_input::("", edit_value) + .id(input_id.clone()) + .size(10) + .on_input(move |value| AdsrMessage::EditChanged(i, value)) + .on_submit(AdsrMessage::EditSubmit(i)) + .width(Length::Fill) + .into() + } else { + let content = container(column![ + text(["Attack", "Decay", "Sustain", "Release"][i]).size(10), + text(value).size(10) + ]) + .padding(2) + .width(Length::Fill); + mouse_area(content) + .on_press(AdsrMessage::Assign(i)) + .on_double_click(AdsrMessage::Edit(i)) + .into() + }; + label })) .width(210); - column![canvas(graph).width(210).height(115), labels] + column![canvas(graph).width(210).height(96), labels] .spacing(3) .into() } @@ -209,7 +245,7 @@ impl Program for Envelope { } for (label, position) in [ ("level", Point::new(0.0, 0.0)), - ("time", Point::new(183.0, 102.0)), + ("time", Point::new(183.0, 78.0)), ] { frame.fill_text(Text { content: label.into(), diff --git a/src/knob.rs b/src/knob.rs new file mode 100644 index 0000000..3a8e11f --- /dev/null +++ b/src/knob.rs @@ -0,0 +1,149 @@ +use iced::{ + Alignment, Background, Border, Color, Element, Length, Theme, + widget::{column, container, text}, +}; + +use crate::arch_range_slider::arch_range_slider; +use crate::arch_slider::arch_slider; + +/// Data used to render a labeled knob and its value readout. +pub struct Knob { + pub label: String, + pub value: f32, + pub range: std::ops::RangeInclusive, + pub default: f32, + pub step: f32, + pub value_text: String, + pub assigned: bool, +} + +/// A compact labeled knob with an optional amber modulation-assignment state. +pub fn knob( + knob: Knob, + on_change: impl Fn(f32) -> Message + 'static, + on_release: Message, +) -> Element<'static, Message> { + let min = *knob.range.start(); + let max = *knob.range.end(); + let mut slider = arch_slider(knob.range, knob.value.clamp(min, max), on_change) + .step(knob.step) + .double_click_reset(knob.default) + .on_release(on_release) + .fill_from_start() + .width(Length::Fixed(41.0)) + .height(Length::Fixed(41.0)); + + if knob.assigned { + slider = slider + .filled_color(Color::from_rgb(0.82, 0.58, 0.08)) + .handle_color(Color::from_rgb(1.0, 0.83, 0.10)); + } + + let assigned = knob.assigned; + container( + column![ + text(knob.label) + .size(11) + .width(Length::Fill) + .align_x(iced::alignment::Horizontal::Center), + slider, + text(knob.value_text) + .size(10) + .width(Length::Fill) + .align_x(iced::alignment::Horizontal::Center) + ] + .spacing(2) + .align_x(Alignment::Center), + ) + .width(Length::Fixed(50.0)) + .padding(2) + .style(move |_theme: &Theme| { + let border_color = if assigned { + Color::from_rgb(1.0, 0.83, 0.10) + } else { + Color::TRANSPARENT + }; + container::Style { + background: assigned + .then(|| Background::Color(Color::from_rgba(1.0, 0.83, 0.10, 0.10))), + border: Border { + color: border_color, + width: if assigned { 1.0 } else { 0.0 }, + radius: 3.0.into(), + }, + ..container::Style::default() + } + }) + .into() +} + +/// Data used to render a knob with two independently adjustable range handles. +pub struct RangeKnob { + pub label: String, + pub low: f32, + pub high: f32, + pub range: std::ops::RangeInclusive, + pub step: f32, + pub value_text: String, + pub assigned: bool, +} + +/// A compact labeled range knob, styled like [`knob`]. +pub fn range_knob( + knob: RangeKnob, + on_change: impl Fn(f32, f32) -> Message + 'static, + on_release: Message, +) -> Element<'static, Message> { + let min = *knob.range.start(); + let max = *knob.range.end(); + let low = knob.low.clamp(min, max); + let high = knob.high.clamp(min, max); + let mut slider = arch_range_slider(knob.range, low.min(high), low.max(high), on_change) + .step(knob.step) + .on_release(on_release) + .width(Length::Fixed(41.0)) + .height(Length::Fixed(41.0)); + + if knob.assigned { + slider = slider + .filled_color(Color::from_rgb(0.82, 0.58, 0.08)) + .handle_color(Color::from_rgb(1.0, 0.83, 0.10)); + } + + let assigned = knob.assigned; + container( + column![ + text(knob.label) + .size(11) + .width(Length::Fill) + .align_x(iced::alignment::Horizontal::Center), + slider, + text(knob.value_text) + .size(10) + .width(Length::Fill) + .align_x(iced::alignment::Horizontal::Center) + ] + .spacing(2) + .align_x(Alignment::Center), + ) + .width(Length::Fixed(50.0)) + .padding(2) + .style(move |_theme: &Theme| { + let border_color = if assigned { + Color::from_rgb(1.0, 0.83, 0.10) + } else { + Color::TRANSPARENT + }; + container::Style { + background: assigned + .then(|| Background::Color(Color::from_rgba(1.0, 0.83, 0.10, 0.10))), + border: Border { + color: border_color, + width: if assigned { 1.0 } else { 0.0 }, + radius: 3.0.into(), + }, + ..container::Style::default() + } + }) + .into() +} diff --git a/src/lib.rs b/src/lib.rs index 9d9c049..8636191 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ pub mod drum; pub mod horizontal_meter; pub mod horizontal_scrollbar; pub mod horizontal_slider; +pub mod knob; pub mod menu; pub mod meters; pub mod midi;