Browse Source

Add Internal to app::Switch.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
62a93d099c
2 changed files with 29 additions and 9 deletions
  1. +5
    -3
      include/app/Switch.hpp
  2. +24
    -6
      src/app/Switch.cpp

+ 5
- 3
include/app/Switch.hpp View File

@@ -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. In momentary mode, the value is instead set to maxValue when the mouse is held and minValue when released.
*/ */
struct Switch : ParamWidget { struct Switch : ParamWidget {
struct Internal;
Internal* internal;

/** Return to original position when released */ /** Return to original position when released */
bool momentary = false; bool momentary = false;
/** Hysteresis state for momentary switch */
bool momentaryPressed = false;
bool momentaryReleased = false;


Switch();
~Switch();
void initParamQuantity() override; void initParamQuantity() override;
void step() override; void step() override;
void onDoubleClick(const DoubleClickEvent& e) override; void onDoubleClick(const DoubleClickEvent& e) override;


+ 24
- 6
src/app/Switch.cpp View File

@@ -9,6 +9,21 @@ namespace rack {
namespace app { 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() { void Switch::initParamQuantity() {
ParamWidget::initParamQuantity(); ParamWidget::initParamQuantity();
engine::ParamQuantity* pq = getParamQuantity(); engine::ParamQuantity* pq = getParamQuantity();
@@ -24,12 +39,12 @@ void Switch::initParamQuantity() {


void Switch::step() { void Switch::step() {
engine::ParamQuantity* pq = getParamQuantity(); engine::ParamQuantity* pq = getParamQuantity();
if (momentaryPressed) {
momentaryPressed = false;
if (internal->momentaryPressed) {
internal->momentaryPressed = false;
// Wait another frame. // Wait another frame.
} }
else if (momentaryReleased) {
momentaryReleased = false;
else if (internal->momentaryReleased) {
internal->momentaryReleased = false;
if (pq) { if (pq) {
// Set to minimum value // Set to minimum value
pq->setMin(); pq->setMin();
@@ -40,6 +55,7 @@ void Switch::step() {


void Switch::onDoubleClick(const DoubleClickEvent& e) { void Switch::onDoubleClick(const DoubleClickEvent& e) {
// Don't reset parameter on double-click // Don't reset parameter on double-click
OpaqueWidget::onDoubleClick(e);
} }


void Switch::onDragStart(const DragStartEvent& e) { void Switch::onDragStart(const DragStartEvent& e) {
@@ -50,10 +66,10 @@ void Switch::onDragStart(const DragStartEvent& e) {


engine::ParamQuantity* pq = getParamQuantity(); engine::ParamQuantity* pq = getParamQuantity();
if (momentary) { if (momentary) {
internal->momentaryPressed = true;
if (pq) { if (pq) {
// Set to maximum value // Set to maximum value
pq->setMax(); pq->setMax();
momentaryPressed = true;
} }
} }
else { else {
@@ -84,11 +100,13 @@ void Switch::onDragStart(const DragStartEvent& e) {
} }


void Switch::onDragEnd(const DragEndEvent& e) { void Switch::onDragEnd(const DragEndEvent& e) {
ParamWidget::onDragEnd(e);

if (e.button != GLFW_MOUSE_BUTTON_LEFT) if (e.button != GLFW_MOUSE_BUTTON_LEFT)
return; return;


if (momentary) { if (momentary) {
momentaryReleased = true;
internal->momentaryReleased = true;
} }
} }




Loading…
Cancel
Save