use egui::{ InnerResponse, Pos2, Ui, Vec2, }; use serde::{ Deserialize, Serialize, }; use std::ops::RangeInclusive; use crate::{ visuals::{ templates::WidgetTemplate, VisualTheme, }, Tooltipable, }; use self::{ connector::ports::Port, fader::Fader, knob::Knob, toggle::Toggle, }; pub mod connector; pub mod fader; pub mod knob; pub mod scope; pub mod toggle; #[derive(Clone, Copy, PartialEq, Serialize, Deserialize, Debug)] pub struct KnobRange { pub start: f32, pub end: f32, } impl From> for KnobRange { fn from(range: RangeInclusive) -> Self { Self { start: *range.start(), end: *range.end(), } } } impl From for RangeInclusive { fn from(v: KnobRange) -> Self { v.start..=v.end } } impl From<(f32, f32)> for KnobRange { fn from(range: (f32, f32)) -> Self { Self { start: range.0, end: range.1, } } } pub enum WidgetResponse { None, Changed, AttemptConnection, AttemptDisconnect, } pub enum SlotWidget { Knob(Knob), Fader(Fader), Toggle(Toggle), Port(Port), } impl SlotWidget { pub fn pos(&self) -> Pos2 { match self { SlotWidget::Knob(k) => k.pos, SlotWidget::Fader(_f) => todo!(), SlotWidget::Toggle(t) => t.pos, SlotWidget::Port(p) => p.pos, } } pub fn size(&self) -> Vec2 { match self { SlotWidget::Knob(k) => k.size, SlotWidget::Fader(_f) => todo!(), SlotWidget::Toggle(t) => t.size, SlotWidget::Port(p) => p.size, } } pub fn value(&self) -> f32 { match self { SlotWidget::Knob(k) => k.value, SlotWidget::Fader(_f) => todo!(), SlotWidget::Toggle(t) => t.value(), SlotWidget::Port(_) => 0.0, } } pub fn show(&mut self, ui: &mut Ui, theme: VisualTheme) -> InnerResponse { match self { SlotWidget::Knob(k) => k.show(ui, theme), SlotWidget::Fader(_f) => todo!(), SlotWidget::Toggle(t) => t.show(ui, theme), SlotWidget::Port(p) => p.show(ui, theme), } } fn name(&self) -> String { match self { SlotWidget::Knob(k) => k.name.clone(), SlotWidget::Fader(_f) => todo!(), SlotWidget::Toggle(t) => t.name.clone(), SlotWidget::Port(p) => p.name.clone(), } } pub fn from_template(template: WidgetTemplate) -> Option { template.into_slot_widget() } } impl Tooltipable for SlotWidget { fn tooltip(&self) -> String { match self { SlotWidget::Knob(_) | SlotWidget::Fader(_) | SlotWidget::Toggle(_) => { format!("{}: {}", self.name(), self.value()) } SlotWidget::Port(_) => self.name(), } } }