2022-11-08 04:11:59 +11:00
|
|
|
// Diopser: a phase rotation plugin
|
|
|
|
// Copyright (C) 2021-2022 Robbert van der Helm
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-11-05 04:52:35 +11:00
|
|
|
use nih_plug_vizia::vizia::prelude::*;
|
|
|
|
|
2022-11-23 11:00:45 +11:00
|
|
|
use super::SafeModeClamper;
|
|
|
|
|
|
|
|
/// A custom toggleable button that toggles safe mode whenever it is Alt+clicked. Otherwise this is
|
|
|
|
/// very similar to the param button.
|
2022-11-05 04:52:35 +11:00
|
|
|
#[derive(Lens)]
|
2022-11-23 11:00:45 +11:00
|
|
|
pub struct SafeModeButton<L: Lens<Target = SafeModeClamper>> {
|
2022-11-05 04:52:35 +11:00
|
|
|
lens: L,
|
2022-11-09 07:25:24 +11:00
|
|
|
|
|
|
|
/// The number of (fractional) scrolled lines that have not yet been turned into parameter
|
|
|
|
/// change events. This is needed to support trackpads with smooth scrolling.
|
|
|
|
scrolled_lines: f32,
|
2022-11-05 04:52:35 +11:00
|
|
|
}
|
|
|
|
|
2022-11-23 11:00:45 +11:00
|
|
|
impl<L: Lens<Target = SafeModeClamper>> SafeModeButton<L> {
|
|
|
|
/// Creates a new button bound to the [`SafeModeClamper`].
|
2022-11-05 04:52:35 +11:00
|
|
|
pub fn new<T>(cx: &mut Context, lens: L, label: impl Res<T>) -> Handle<Self>
|
|
|
|
where
|
|
|
|
T: ToString,
|
|
|
|
{
|
2022-11-09 07:25:24 +11:00
|
|
|
Self {
|
|
|
|
lens: lens.clone(),
|
|
|
|
scrolled_lines: 0.0,
|
|
|
|
}
|
|
|
|
.build(cx, move |cx| {
|
|
|
|
Label::new(cx, label);
|
|
|
|
})
|
2022-11-23 11:00:45 +11:00
|
|
|
.checked(lens.map(|v| v.status()))
|
2022-11-09 07:25:24 +11:00
|
|
|
// We'll pretend this is a param-button, so this class is used for assigning a unique color
|
|
|
|
.class("safe-mode")
|
2022-11-05 04:52:35 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 11:00:45 +11:00
|
|
|
impl<L: Lens<Target = SafeModeClamper>> View for SafeModeButton<L> {
|
2022-11-05 04:52:35 +11:00
|
|
|
fn element(&self) -> Option<&'static str> {
|
|
|
|
// Reuse the styling from param-button
|
|
|
|
Some("param-button")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-11-09 07:25:24 +11:00
|
|
|
WindowEvent::MouseDown(MouseButton::Left)
|
2022-11-05 04:52:35 +11:00
|
|
|
| WindowEvent::MouseDoubleClick(MouseButton::Left)
|
|
|
|
| WindowEvent::MouseTripleClick(MouseButton::Left) => {
|
2022-11-23 11:00:45 +11:00
|
|
|
// We can just unconditionally toggle the boolean here. When safe mode is enabled
|
|
|
|
// this immediately clamps the affected parameters to their new range.
|
|
|
|
let safe_mode_clamper = self.lens.get(cx);
|
|
|
|
safe_mode_clamper.toggle(cx);
|
2022-11-05 04:52:35 +11:00
|
|
|
|
|
|
|
meta.consume();
|
|
|
|
}
|
2022-11-09 07:25:24 +11:00
|
|
|
WindowEvent::MouseScroll(_scroll_x, scroll_y) => {
|
|
|
|
self.scrolled_lines += scroll_y;
|
|
|
|
|
|
|
|
if self.scrolled_lines.abs() >= 1.0 {
|
2022-11-23 11:00:45 +11:00
|
|
|
let safe_mode_clamper = self.lens.get(cx);
|
2022-11-09 07:25:24 +11:00
|
|
|
|
|
|
|
if self.scrolled_lines >= 1.0 {
|
2022-11-23 11:00:45 +11:00
|
|
|
safe_mode_clamper.enable(cx);
|
2022-11-09 07:25:24 +11:00
|
|
|
self.scrolled_lines -= 1.0;
|
|
|
|
} else {
|
2022-11-23 11:00:45 +11:00
|
|
|
safe_mode_clamper.disable(cx);
|
2022-11-09 07:25:24 +11:00
|
|
|
self.scrolled_lines += 1.0;
|
|
|
|
}
|
|
|
|
}
|
2022-11-09 07:41:29 +11:00
|
|
|
|
|
|
|
meta.consume();
|
2022-11-09 07:25:24 +11:00
|
|
|
}
|
2022-11-05 04:52:35 +11:00
|
|
|
_ => {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|