Skip to content

Commit

Permalink
wip ActionTrigger
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Oct 21, 2024
1 parent 41ff367 commit 3285776
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 50 deletions.
44 changes: 21 additions & 23 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn create(editor_data: Data, editor_state: Arc<ViziaState>) -> Option<Box<dy
cx,
Data::is_learning,
Data::learning_index,
0,
255,
Data::learned_notes,
);

Expand Down Expand Up @@ -553,6 +553,7 @@ impl ActionTrigger {
// delay_data: delay_data.get(cx),
}
.build(cx, |_cx| {
// Label::new(cx, "XXX").class("global-title");
// put other widgets here
})
}
Expand All @@ -561,22 +562,14 @@ impl ActionTrigger {
self.is_learning.store(true, Ordering::SeqCst);
self.learning_index.store(self.own_index, Ordering::SeqCst);
}

pub fn stop_learning(&self) {
self.is_learning.store(false, Ordering::SeqCst);
}

pub fn set_learning_index(&self, index: usize) {
self.learning_index.store(index, Ordering::SeqCst);
}

pub fn get_learning_index(&self) -> usize {
self.learning_index.load(Ordering::SeqCst)
}

// Checks if learning is active for this trigger
pub fn is_learning(&self) -> bool {
self.is_learning.load(Ordering::SeqCst) && self.get_learning_index() == self.own_index
self.is_learning.load(Ordering::SeqCst)
&& self.learning_index.load(Ordering::SeqCst) == self.own_index
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -601,20 +594,11 @@ impl ActionTrigger {
} else {
vg::Paint::color(background_color)
};

// Fill the path using the selected paint
canvas.fill_path(&path, &paint);
// }

// Drawing the border around the rectangle

let mut path = vg::Path::new();
path.rect(
bounds.x + border_width * 0.5,
bounds.y,
bounds.w - border_width,
bounds.h - border_width * 0.5,
);
path.rect(bounds.x, bounds.y, bounds.w, bounds.h);
path.close();

canvas.stroke_path(
Expand All @@ -630,6 +614,22 @@ impl View for ActionTrigger {
Some("action-trigger")
}

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)
| WindowEvent::MouseDoubleClick(MouseButton::Left)
| WindowEvent::MouseTripleClick(MouseButton::Left) => {
if self.is_learning() {
self.stop_learning();
} else {
self.start_learning();
}
meta.consume();
}
_ => {}
});
}
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();
Expand All @@ -640,7 +640,5 @@ impl View for ActionTrigger {
let path_line_width = draw_context.outline_width();

self.draw_background(canvas, bounds, background_color, border_color, border_width);
// Example of using internal state in a simplistic way
if self.is_learning() {}
}
}
54 changes: 27 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,30 +247,28 @@ impl GainParams {
.with_unit(" dB")
.with_value_to_string(formatters::v2s_f32_gain_to_db(1))
.with_string_to_value(formatters::s2v_f32_gain_to_db()),
mute_in_learn: BoolParam::new("mute in", false)
.with_value_to_string(Arc::new(|value| {
String::from(if value { "learning" } else { "not learning" })
}))
.with_callback({
let learning_index = learning_index.clone();
let is_learning = is_learning.clone();
Arc::new(move |_| {
learning_index.store(0, Ordering::Release);
is_learning.store(true, Ordering::Release);
})
}),
mute_out_learn: BoolParam::new("mute out", false)
.with_value_to_string(Arc::new(|value| {
String::from(if value { "learning" } else { "not learning" })
}))
.with_callback({
let learning_index = learning_index.clone();
let is_learning = is_learning.clone();
Arc::new(move |_| {
learning_index.store(1, Ordering::Release);
is_learning.store(true, Ordering::Release);
})
}),
mute_in_learn: BoolParam::new("mute in", false).with_value_to_string(Arc::new(
|value| String::from(if value { "learning" } else { "not learning" }),
)),
// .with_callback({
// let learning_index = learning_index.clone();
// let is_learning = is_learning.clone();
// Arc::new(move |_| {
// learning_index.store(0, Ordering::Release);
// is_learning.store(true, Ordering::Release);
// })
// }),
mute_out_learn: BoolParam::new("mute out", false).with_value_to_string(Arc::new(
|value| String::from(if value { "learning" } else { "not learning" }),
)),
// .with_callback({
// let learning_index = learning_index.clone();
// let is_learning = is_learning.clone();
// Arc::new(move |_| {
// learning_index.store(1, Ordering::Release);
// is_learning.store(true, Ordering::Release);
// })
// }),
}
}
}
Expand Down Expand Up @@ -592,10 +590,11 @@ impl Plugin for Del2 {
context: &mut impl ProcessContext<Self>,
) -> ProcessStatus {
self.update_timing_params();
// TODO: put back?
// if self.is_learning {
if Del2::compare_exchange(&self.is_learning) {
self.counting_state = CountingState::MidiLearn;
}
// if Del2::compare_exchange(&self.is_learning) {
// self.counting_state = CountingState::MidiLearn;
// }

self.process_midi_events(context);
self.prepare_for_delay(buffer.samples());
Expand Down Expand Up @@ -680,6 +679,7 @@ impl Del2 {
}
CountingState::MidiLearn => {
let is_learning = self.is_learning.clone();
println!("midi learn ");
is_learning.store(false, Ordering::Release);
self.learned_notes.store(
self.learning_index.load(Ordering::SeqCst),
Expand Down

0 comments on commit 3285776

Please sign in to comment.