diff --git a/include/app/Knob.hpp b/include/app/Knob.hpp index 30e5d34f..d7f648a8 100644 --- a/include/app/Knob.hpp +++ b/include/app/Knob.hpp @@ -38,6 +38,10 @@ struct Knob : ParamWidget { void onDragLeave(const DragLeaveEvent& e) override; void onHoverScroll(const HoverScrollEvent& e) override; void onLeave(const LeaveEvent& e) override; + /** Called when user clicks the knob without moving it. + Useful for handling emulating push-knobs in hardware. + */ + void onAction(const ActionEvent& e) override {} }; diff --git a/src/app/Knob.cpp b/src/app/Knob.cpp index 49bc1fd1..16696e89 100644 --- a/src/app/Knob.cpp +++ b/src/app/Knob.cpp @@ -24,6 +24,8 @@ struct Knob::Internal { /** The mouse has once escaped from the knob while dragging. */ bool rotaryDragEnabled = false; float dragAngle = NAN; + + float distDragged = 0.f; }; @@ -83,6 +85,9 @@ void Knob::onDragStart(const DragStartEvent& e) { internal->rotaryDragEnabled = false; internal->dragAngle = NAN; + // Reset distance dragged + internal->distDragged = 0.f; + ParamWidget::onDragStart(e); } @@ -113,6 +118,13 @@ void Knob::onDragEnd(const DragEndEvent& e) { } internal->oldValue = NAN; + // Dispatch Action event if mouse traveled less than a threshold distance + const float actionDistThreshold = 16.f; + if (internal->distDragged < actionDistThreshold) { + ActionEvent eAction; + onAction(eAction); + } + ParamWidget::onDragEnd(e); } @@ -213,6 +225,8 @@ void Knob::onDragMove(const DragMoveEvent& e) { pq->setSmoothValue(value); } + internal->distDragged += e.mouseDelta.norm(); + ParamWidget::onDragMove(e); }