Skip to content

Commit

Permalink
migrated: int -> enum parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Weichart committed Sep 9, 2022
1 parent ac00a1b commit 51a056a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 161 deletions.
8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,4 @@ nih_plug = { git = "https://github.com/robbert-vdh/nih-plug.git" }
embedded-time = "0.12.1"

# gui
nih_plug_egui = { git = "https://github.com/robbert-vdh/nih-plug.git" }

# utility
strum = "0.24"
strum_macros = "0.24"
num-derive = "0.3"
num-traits = "0.2"
nih_plug_egui = { git = "https://github.com/robbert-vdh/nih-plug.git" }
61 changes: 26 additions & 35 deletions src/editor/gui.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::sync::{Arc};
use std::sync::Arc;
use std::sync::atomic::AtomicBool;

use nih_plug::context::ParamSetter;
use nih_plug::param::Param;
use nih_plug::prelude::Enum;
use nih_plug_egui::{egui, widgets};
use nih_plug_egui::egui::{Context, DragValue, popup_below_widget, Response, Rounding, Slider, Ui, Vec2, Widget};
use nih_plug_egui::egui::{Context, DragValue, popup_below_widget, Vec2};
use nih_plug_egui::egui::style::Margin;
use crate::{CategoricalIntParam, Clockwork, FrequencyType, PluginParams, TriggerMode};

use crate::{Clockwork, FrequencyType, PluginParams, TriggerMode};
use crate::editor::numpad::Numpad;

pub trait GuiEditor {
Expand All @@ -17,7 +21,6 @@ pub trait GuiEditor {
params: &Arc<PluginParams>,
is_typing: &Arc<AtomicBool>,
);

}

impl GuiEditor for Clockwork {
Expand All @@ -27,13 +30,13 @@ impl GuiEditor for Clockwork {
fn draw_ui(ctx: &Context, setter: &ParamSetter, params: &Arc<PluginParams>, is_typing: &Arc<AtomicBool>) {
egui::CentralPanel::default()
.show(ctx, |ui| {
let freq_type = FrequencyType::from_int_param(&params.freq_type);
let freq_type = params.freq_type.value();
let freq_param = match freq_type {
FrequencyType::Hertz => &params.freq_hz,
FrequencyType::Milliseconds => &params.freq_ms,
FrequencyType::Bpm => &params.freq_bpm,
};
let trigger_mode = TriggerMode::from_int_param(&params.trigger_mode);
let trigger_mode = params.trigger_mode.value();

ui.style_mut().spacing.window_margin = Margin::from(Vec2::from([
Clockwork::WINDOW_HEIGHT as f32 * 0.05,
Expand All @@ -46,7 +49,7 @@ impl GuiEditor for Clockwork {
ui.horizontal(|ui| {
ui.add(widgets::ParamSlider::for_param(
freq_param,
setter
setter,
)
.without_value()
.with_width(Clockwork::WINDOW_WIDTH as f32 * 0.8)
Expand All @@ -63,12 +66,12 @@ impl GuiEditor for Clockwork {
) {
is_typing.store(
false,
std::sync::atomic::Ordering::Relaxed
std::sync::atomic::Ordering::Relaxed,
);
} else {
is_typing.store(
true,
std::sync::atomic::Ordering::Relaxed
std::sync::atomic::Ordering::Relaxed,
);
}
}
Expand All @@ -85,7 +88,7 @@ impl GuiEditor for Clockwork {
&dragval_widget,
|ui| {
ui.add(numpad);
}
},
);

if is_typing.load(
Expand All @@ -96,22 +99,16 @@ impl GuiEditor for Clockwork {
};

// Toggle for frequency type

let text = FrequencyType::variants()[freq_type.clone().to_index()];
if ui
.add(egui::Button::new(freq_type.to_string()))
.add(egui::Button::new(text))
.clicked()
{
match freq_type {
FrequencyType::Hertz => {
setter.set_parameter(&params.freq_type, FrequencyType::Milliseconds as i32);
},
FrequencyType::Milliseconds => {
setter.set_parameter(&params.freq_type, FrequencyType::Bpm as i32);
},
FrequencyType::Bpm => {
setter.set_parameter(&params.freq_type, FrequencyType::Hertz as i32);
}
}
if freq_type.clone().to_index() == FrequencyType::variants().len() - 1 {
setter.set_parameter(&params.freq_type, FrequencyType::from_index(0));
} else {
setter.set_parameter(&params.freq_type, params.freq_type.next_step(freq_type.clone()))
}
}
});
});
Expand All @@ -120,22 +117,17 @@ impl GuiEditor for Clockwork {
ui.separator();
ui.horizontal(
|ui| {
let text = TriggerMode::variants()[trigger_mode.clone().to_index()];
if ui
.add(egui::Button::new(
trigger_mode.to_string()
text
))
.clicked()
{
match trigger_mode {
TriggerMode::Continue => {
setter.set_parameter(&params.trigger_mode, TriggerMode::ReTrigger as i32);
},
TriggerMode::ReTrigger => {
setter.set_parameter(&params.trigger_mode, TriggerMode::ReTriggerDelayed as i32);
},
TriggerMode::ReTriggerDelayed => {
setter.set_parameter(&params.trigger_mode, TriggerMode::Continue as i32);
}
if trigger_mode.clone().to_index() == TriggerMode::variants().len() - 1 {
setter.set_parameter(&params.trigger_mode, TriggerMode::from_index(0));
} else {
setter.set_parameter(&params.trigger_mode, params.trigger_mode.next_step(trigger_mode.clone()));
}
}
let label = egui::Label::new(trigger_mode.description()).wrap(true);
Expand All @@ -145,6 +137,5 @@ impl GuiEditor for Clockwork {
})
});
}

}

33 changes: 18 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use std::time::SystemTime;
use nih_plug_egui::{create_egui_editor, EguiState};
use crate::params::freq_type::{ FrequencyType };
use crate::params::trigger_mode::{TriggerMode};
use crate::params::categorical_int_param::CategoricalIntParam;
use num_traits::FromPrimitive;
use crate::editor::gui::{ GuiEditor };

// This is a shortened version of the gain example with most comments removed, check out
Expand Down Expand Up @@ -38,9 +36,9 @@ pub struct PluginParams {
#[id = "freq-bpm"]
pub freq_bpm: FloatParam,
#[id = "freq-type"]
pub freq_type: IntParam,
pub freq_type: EnumParam<FrequencyType>,
#[id = "trigger-mode"]
pub trigger_mode: IntParam,
pub trigger_mode: EnumParam<TriggerMode>,

#[persist = "editor-state"]
editor_state: Arc<EguiState>,
Expand Down Expand Up @@ -98,9 +96,15 @@ impl Default for PluginParams {
.with_unit(" bpm"),

// Frequency Type
freq_type: FrequencyType::new_int_param(),
freq_type: EnumParam::new(
"Frequency Type",
FrequencyType::Hertz,
),
// Trigger Mode
trigger_mode: TriggerMode::new_int_param(),
trigger_mode: EnumParam::new(
"Trigger Mode",
TriggerMode::Continue,
),
}
}
}
Expand All @@ -111,7 +115,7 @@ impl Plugin for Clockwork {
const URL: &'static str = "https://github.com/AlexW00/clockwork";
const EMAIL: &'static str = "alexanderweichart@icloud.com";

const VERSION: &'static str = "1.0.0";
const VERSION: &'static str = "1.0.2";

const DEFAULT_INPUT_CHANNELS: u32 = 0;
const DEFAULT_OUTPUT_CHANNELS: u32 = 0;
Expand Down Expand Up @@ -176,11 +180,11 @@ impl Plugin for Clockwork {
impl Clockwork {

fn on_note_on (&mut self, note_event: NoteEvent) {
match TriggerMode::from_i32(self.params.trigger_mode.value) {
Some(TriggerMode::ReTrigger) => {
match self.params.trigger_mode.value() {
TriggerMode::ReTrigger => {
self.last_note_on_send = SystemTime::UNIX_EPOCH;
}
Some(TriggerMode::ReTriggerDelayed) => {
TriggerMode::ReTriggerDelayed=> {
self.last_note_on_send = SystemTime::now();
}
_ => (),
Expand Down Expand Up @@ -225,17 +229,16 @@ fn on_note_on (&mut self, note_event: NoteEvent) {
}

fn do_send_midi (&self) -> bool {
match FrequencyType::from_i32(self.params.freq_type.value) {
Some(FrequencyType::Milliseconds) => self.ms_since_last_midi_send() as f32 > self.params.freq_ms.value,
Some(FrequencyType::Hertz) => {
match self.params.freq_type.value() {
FrequencyType::Milliseconds => self.ms_since_last_midi_send() as f32 > self.params.freq_ms.value,
FrequencyType::Hertz => {
let ms = 1000.0 / self.params.freq_hz.value;
self.ms_since_last_midi_send() as f32 > ms
}
Some(FrequencyType::Bpm) => {
FrequencyType::Bpm => {
let ms = 60000.0 / self.params.freq_bpm.value;
self.ms_since_last_midi_send() as f32 > ms
}
_ => false
}
}

Expand Down
52 changes: 0 additions & 52 deletions src/params/categorical_int_param.rs

This file was deleted.

31 changes: 5 additions & 26 deletions src/params/freq_type.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
use std::borrow::Cow;
use num_derive::FromPrimitive;
use num_derive::ToPrimitive;
use strum_macros::{EnumCount, EnumIter};
use strum::{EnumCount, IntoEnumIterator};
use nih_plug::prelude::Enum;

use std::fmt::Display;
use crate::CategoricalIntParam;

#[derive(FromPrimitive, ToPrimitive, Clone, Copy, EnumCount, EnumIter, Default)]
#[derive(PartialEq, Enum, Clone)]
pub enum FrequencyType {
#[name="Hz"]
Hertz,
#[default]
#[name="ms"]
Milliseconds,
#[name="bpm"]
Bpm
}

impl Display for FrequencyType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FrequencyType::Hertz => write!(f, "Hz"),
FrequencyType::Milliseconds => write!(f, "ms"),
FrequencyType::Bpm => write!(f, "bpm"),
}
}
}

impl CategoricalIntParam<FrequencyType> for FrequencyType {
fn title() -> String {
"Frequency type".to_string()
}
}
3 changes: 1 addition & 2 deletions src/params/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod freq_type;
pub mod trigger_mode;
pub mod categorical_int_param;
pub mod trigger_mode;
29 changes: 5 additions & 24 deletions src/params/trigger_mode.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
use num_derive::FromPrimitive;
use num_derive::ToPrimitive;
use strum_macros::{EnumCount, EnumIter};
use nih_plug::prelude::Enum;

use std::fmt::Display;
use crate::params::categorical_int_param::{CategoricalIntParam};

#[derive(FromPrimitive, ToPrimitive, Clone, Copy, EnumCount, EnumIter, Default)]
#[derive(PartialEq, Enum, Clone)]
pub enum TriggerMode {
#[default]
#[name="Continue"]
Continue,
#[name="Re-trigger"]
ReTrigger,
#[name="Re-trigger delayed"]
ReTriggerDelayed,
}

impl Display for TriggerMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TriggerMode::Continue => write!(f, "Continue"),
TriggerMode::ReTrigger => write!(f, "Re-trigger"),
TriggerMode::ReTriggerDelayed => write!(f, "Re-trigger delayed"),
}
}
}

impl TriggerMode {
pub fn description(&self) -> &'static str {
match self {
Expand All @@ -32,9 +19,3 @@ impl TriggerMode {
}
}
}

impl CategoricalIntParam<TriggerMode> for TriggerMode {
fn title() -> String {
"Trigger mode".to_string()
}
}

0 comments on commit 51a056a

Please sign in to comment.