Skip to content

Commit

Permalink
try to get the midi label to update using events
Browse files Browse the repository at this point in the history
the LabelChange event does not happen
  • Loading branch information
magnetophon committed Oct 22, 2024
1 parent 60a4c48 commit 67d5c08
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
49 changes: 42 additions & 7 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use nih_plug_vizia::{assets, create_vizia_editor, ViziaState, ViziaTheming};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

// use crate::editor::action_trigger_derived_lenses::learned_notes;
// use crate::editor::data_derived_lenses::learned_notes;
use crate::AtomicByteArray;
use crate::Del2Params;
use crate::DelayData;
Expand All @@ -23,7 +25,17 @@ pub(crate) struct Data {
pub learned_notes: Arc<AtomicByteArray>,
}

impl Model for Data {}
impl Model for Data {
fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
event.map(|event, meta| match event {
ActionTriggerEvent::LabelChange(learned_notes) => {
// self.set_label_string();
println!("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
meta.consume();
}
});
}
}

// Makes sense to also define this here, makes it a bit easier to keep track of
pub fn default_state() -> Arc<ViziaState> {
Expand Down Expand Up @@ -87,9 +99,14 @@ pub fn create(editor_data: Data, editor_state: Arc<ViziaState>) -> Option<Box<dy
cx,
Data::is_learning,
Data::learning_index,
0,
1,
Data::learned_notes,
);
)
.on_change(|ex, learned_notes| {
ex.emit(ActionTriggerEvent::LabelChange(learned_notes))
});

// Label::new(cx, learned_string).class("slider-label");

HStack::new(cx, |cx| {
HStack::new(cx, |cx| {
Expand Down Expand Up @@ -532,6 +549,7 @@ pub struct ActionTrigger {
own_index: usize,
learned_notes: Arc<AtomicByteArray>,
label_string: String,
on_change: Option<Box<dyn Fn(&mut EventContext, AtomicByteArray)>>,
}
impl ActionTrigger {
pub fn new<IsLearningL, LearningIndexL, LearnedNoteL>(
Expand All @@ -553,6 +571,7 @@ impl ActionTrigger {
own_index,
learned_notes: learned_notes.get(cx),
label_string: String::from("click to learn"),
on_change: None,
}
.build(cx, |cx| {
Label::new(cx, Self::label_string).class("action-label");
Expand Down Expand Up @@ -623,7 +642,7 @@ impl View for ActionTrigger {
Some("action-trigger")
}

fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
fn event(&mut self, _cx: &mut EventContext, event: &mut Event) {
event.map(|window_event, meta| match window_event {
// We don't need special double and triple click handling
WindowEvent::MouseDown(MouseButton::Left)
Expand All @@ -640,15 +659,31 @@ impl View for ActionTrigger {
_ => {}
});
}

fn draw(&self, draw_context: &mut DrawContext, canvas: &mut Canvas) {
let bounds = draw_context.bounds();
let background_color: vg::Color = draw_context.background_color().into();
let border_color: vg::Color = draw_context.border_color().into();
let outline_color: vg::Color = draw_context.outline_color().into();
let selection_color: vg::Color = draw_context.selection_color().into();
// let outline_color: vg::Color = draw_context.outline_color().into();
// let selection_color: vg::Color = draw_context.selection_color().into();
let border_width = draw_context.border_width();
let path_line_width = draw_context.outline_width();
// let path_line_width = draw_context.outline_width();

self.draw_background(canvas, bounds, background_color, border_color, border_width);
}
}
pub trait ActionTriggerModifiers {
fn on_change<F: Fn(&mut EventContext, AtomicByteArray) + 'static>(self, callback: F) -> Self;
}

impl<'a> ActionTriggerModifiers for Handle<'a, ActionTrigger> {
fn on_change<F: Fn(&mut EventContext, AtomicByteArray) + 'static>(self, callback: F) -> Self {
self.modify(|actiontrigger| actiontrigger.on_change = Some(Box::new(callback)))
}
}

pub enum ActionTriggerEvent {
// LabelChange(AtomicByteArray, usize),
LabelChange(AtomicByteArray),
// ColorChange,
}
34 changes: 33 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
use array_init::array_init;
use bit_mask_ring_buf::BMRingBuf;
use nih_plug::prelude::*;
use nih_plug_vizia::vizia::binding::Lens;
use nih_plug_vizia::vizia::binding::LensExt;
use nih_plug_vizia::vizia::prelude::*;
use nih_plug_vizia::ViziaState;
use std::hash::{Hash, Hasher};
use std::simd::f32x4;
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -82,6 +85,7 @@ struct Del2 {
delay_buffer_size: u32,
counting_state: CountingState,
should_update_filter: Arc<AtomicBool>,
learned_string: String,
}

// for use in graph
Expand Down Expand Up @@ -461,6 +465,7 @@ impl Default for Del2 {
delay_buffer_size: 0,
counting_state: CountingState::TimeOut,
should_update_filter,
learned_string: String::from("no trigger"),
}
}
}
Expand Down Expand Up @@ -1115,7 +1120,8 @@ impl Enum for MyLadderMode {
})
}
}
// #[derive(Debug)]

#[derive(Debug)]
pub struct AtomicByteArray {
data: AtomicU64,
}
Expand All @@ -1141,5 +1147,31 @@ impl AtomicByteArray {
}
}

// impl Lens for AtomicByteArray {
// type Source = Self; // Adjust based on your actual source type
// type Target = Self; // Adjust based on your actual target type

// fn view<F: FnOnce(&Self::Target)>(&self, f: F) {
// // Implement logic how to view the lens
// f(self);
// }
// // Implement more logic here if necessary
// }

// impl Hash for AtomicByteArray {
// fn hash<H: Hasher>(&self, state: &mut H) {
// // Hash the stored u64 value
// self.data.load(Ordering::SeqCst).hash(state);
// }
// }

// impl Clone for AtomicByteArray {
// fn clone(&self) -> Self {
// AtomicByteArray {
// data: AtomicU64::new(self.data.load(Ordering::SeqCst)),
// }
// }
// }

nih_export_clap!(Del2);
nih_export_vst3!(Del2);

0 comments on commit 67d5c08

Please sign in to comment.