PC Music Generator - a Virtual Modular Synthesizer
at main 137 lines 3.0 kB view raw
1use egui::{ 2 InnerResponse, 3 Pos2, 4 Ui, 5 Vec2, 6}; 7use serde::{ 8 Deserialize, 9 Serialize, 10}; 11use std::ops::RangeInclusive; 12 13use crate::{ 14 visuals::{ 15 templates::WidgetTemplate, 16 VisualTheme, 17 }, 18 Tooltipable, 19}; 20 21use self::{ 22 connector::ports::Port, 23 fader::Fader, 24 knob::Knob, 25 toggle::Toggle, 26}; 27 28pub mod connector; 29pub mod fader; 30pub mod knob; 31pub mod scope; 32pub mod toggle; 33 34#[derive(Clone, Copy, PartialEq, Serialize, Deserialize, Debug)] 35pub struct KnobRange { 36 pub start: f32, 37 pub end: f32, 38} 39 40impl From<RangeInclusive<f32>> for KnobRange { 41 fn from(range: RangeInclusive<f32>) -> Self { 42 Self { 43 start: *range.start(), 44 end: *range.end(), 45 } 46 } 47} 48 49impl From<KnobRange> for RangeInclusive<f32> { 50 fn from(v: KnobRange) -> Self { 51 v.start..=v.end 52 } 53} 54 55impl From<(f32, f32)> for KnobRange { 56 fn from(range: (f32, f32)) -> Self { 57 Self { 58 start: range.0, 59 end: range.1, 60 } 61 } 62} 63 64pub enum WidgetResponse { 65 None, 66 Changed, 67 AttemptConnection, 68 AttemptDisconnect, 69} 70 71pub enum SlotWidget { 72 Knob(Knob), 73 Fader(Fader), 74 Toggle(Toggle), 75 Port(Port), 76} 77 78impl SlotWidget { 79 pub fn pos(&self) -> Pos2 { 80 match self { 81 SlotWidget::Knob(k) => k.pos, 82 SlotWidget::Fader(_f) => todo!(), 83 SlotWidget::Toggle(t) => t.pos, 84 SlotWidget::Port(p) => p.pos, 85 } 86 } 87 88 pub fn size(&self) -> Vec2 { 89 match self { 90 SlotWidget::Knob(k) => k.size, 91 SlotWidget::Fader(_f) => todo!(), 92 SlotWidget::Toggle(t) => t.size, 93 SlotWidget::Port(p) => p.size, 94 } 95 } 96 97 pub fn value(&self) -> f32 { 98 match self { 99 SlotWidget::Knob(k) => k.value, 100 SlotWidget::Fader(_f) => todo!(), 101 SlotWidget::Toggle(t) => t.value(), 102 SlotWidget::Port(_) => 0.0, 103 } 104 } 105 106 pub fn show(&mut self, ui: &mut Ui, theme: VisualTheme) -> InnerResponse<WidgetResponse> { 107 match self { 108 SlotWidget::Knob(k) => k.show(ui, theme), 109 SlotWidget::Fader(_f) => todo!(), 110 SlotWidget::Toggle(t) => t.show(ui, theme), 111 SlotWidget::Port(p) => p.show(ui, theme), 112 } 113 } 114 115 fn name(&self) -> String { 116 match self { 117 SlotWidget::Knob(k) => k.name.clone(), 118 SlotWidget::Fader(_f) => todo!(), 119 SlotWidget::Toggle(t) => t.name.clone(), 120 SlotWidget::Port(p) => p.name.clone(), 121 } 122 } 123 pub fn from_template(template: WidgetTemplate) -> Option<Self> { 124 template.into_slot_widget() 125 } 126} 127 128impl Tooltipable for SlotWidget { 129 fn tooltip(&self) -> String { 130 match self { 131 SlotWidget::Knob(_) | SlotWidget::Fader(_) | SlotWidget::Toggle(_) => { 132 format!("{}: {}", self.name(), self.value()) 133 } 134 SlotWidget::Port(_) => self.name(), 135 } 136 } 137}