From 62a93d099c569bb39f79a1200e1d35d430f9682b Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Wed, 1 Sep 2021 03:59:01 -0400 Subject: [PATCH] Add Internal to app::Switch. --- include/app/Switch.hpp | 8 +++++--- src/app/Switch.cpp | 30 ++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/include/app/Switch.hpp b/include/app/Switch.hpp index e26005f0..b8053d56 100644 --- a/include/app/Switch.hpp +++ b/include/app/Switch.hpp @@ -13,12 +13,14 @@ When maxValue is reached, the next click resets to minValue. In momentary mode, the value is instead set to maxValue when the mouse is held and minValue when released. */ struct Switch : ParamWidget { + struct Internal; + Internal* internal; + /** Return to original position when released */ bool momentary = false; - /** Hysteresis state for momentary switch */ - bool momentaryPressed = false; - bool momentaryReleased = false; + Switch(); + ~Switch(); void initParamQuantity() override; void step() override; void onDoubleClick(const DoubleClickEvent& e) override; diff --git a/src/app/Switch.cpp b/src/app/Switch.cpp index b0c3d84b..e770c583 100644 --- a/src/app/Switch.cpp +++ b/src/app/Switch.cpp @@ -9,6 +9,21 @@ namespace rack { namespace app { +struct Switch::Internal { + /** Hysteresis state for momentary switch */ + bool momentaryPressed = false; + bool momentaryReleased = false; +}; + + +Switch::Switch() { + internal = new Internal; +} + +Switch::~Switch() { + delete internal; +} + void Switch::initParamQuantity() { ParamWidget::initParamQuantity(); engine::ParamQuantity* pq = getParamQuantity(); @@ -24,12 +39,12 @@ void Switch::initParamQuantity() { void Switch::step() { engine::ParamQuantity* pq = getParamQuantity(); - if (momentaryPressed) { - momentaryPressed = false; + if (internal->momentaryPressed) { + internal->momentaryPressed = false; // Wait another frame. } - else if (momentaryReleased) { - momentaryReleased = false; + else if (internal->momentaryReleased) { + internal->momentaryReleased = false; if (pq) { // Set to minimum value pq->setMin(); @@ -40,6 +55,7 @@ void Switch::step() { void Switch::onDoubleClick(const DoubleClickEvent& e) { // Don't reset parameter on double-click + OpaqueWidget::onDoubleClick(e); } void Switch::onDragStart(const DragStartEvent& e) { @@ -50,10 +66,10 @@ void Switch::onDragStart(const DragStartEvent& e) { engine::ParamQuantity* pq = getParamQuantity(); if (momentary) { + internal->momentaryPressed = true; if (pq) { // Set to maximum value pq->setMax(); - momentaryPressed = true; } } else { @@ -84,11 +100,13 @@ void Switch::onDragStart(const DragStartEvent& e) { } void Switch::onDragEnd(const DragEndEvent& e) { + ParamWidget::onDragEnd(e); + if (e.button != GLFW_MOUSE_BUTTON_LEFT) return; if (momentary) { - momentaryReleased = true; + internal->momentaryReleased = true; } }