Skip to content

Commit

Permalink
Add enabled state control to button and knob handlers
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Feb 2, 2025
1 parent cd18a7b commit 6b3d1da
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
8 changes: 7 additions & 1 deletion dgl/EventHandlers.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* DISTRHO Plugin Framework (DPF)
* Copyright (C) 2012-2024 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
Expand Down Expand Up @@ -63,6 +63,9 @@ class ButtonEventHandler
bool isCheckable() const noexcept;
void setCheckable(bool checkable) noexcept;

bool isEnabled() const noexcept;
void setEnabled(bool enabled) noexcept;

Point<double> getLastClickPosition() const noexcept;
Point<double> getLastMotionPosition() const noexcept;

Expand Down Expand Up @@ -121,6 +124,9 @@ class KnobEventHandler
KnobEventHandler& operator=(const KnobEventHandler& other);
virtual ~KnobEventHandler();

bool isEnabled() const noexcept;
void setEnabled(bool enabled) noexcept;

// if setStep(1) has been called before, this returns true
bool isInteger() const noexcept;

Expand Down
80 changes: 79 additions & 1 deletion dgl/src/EventHandlers.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* DISTRHO Plugin Framework (DPF)
* Copyright (C) 2012-2024 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
Expand Down Expand Up @@ -31,6 +31,7 @@ struct ButtonEventHandler::PrivateData {
int state;
bool checkable;
bool checked;
bool enabled;

Point<double> lastClickPos;
Point<double> lastMotionPos;
Expand All @@ -44,11 +45,15 @@ struct ButtonEventHandler::PrivateData {
state(kButtonStateDefault),
checkable(false),
checked(false),
enabled(true),
lastClickPos(0, 0),
lastMotionPos(0, 0) {}

bool mouseEvent(const Widget::MouseEvent& ev)
{
if (! enabled)
return false;

lastClickPos = ev.pos;

// button was released, handle it now
Expand Down Expand Up @@ -98,6 +103,9 @@ struct ButtonEventHandler::PrivateData {

bool motionEvent(const Widget::MotionEvent& ev)
{
if (! enabled)
return false;

// keep pressed
if (button != -1)
{
Expand Down Expand Up @@ -171,6 +179,24 @@ struct ButtonEventHandler::PrivateData {
}
}

void setEnabled(const bool enabled2) noexcept
{
if (enabled == enabled2)
return;

// reset temp vars if disabling
if (! enabled2)
{
button = -1;
state = kButtonStateDefault;
lastClickPos = Point<double>();
lastMotionPos = Point<double>();
}

enabled = enabled2;
widget->repaint();
}

DISTRHO_DECLARE_NON_COPYABLE(PrivateData)
};

Expand Down Expand Up @@ -217,6 +243,16 @@ void ButtonEventHandler::setCheckable(const bool checkable) noexcept
pData->checkable = checkable;
}

bool ButtonEventHandler::isEnabled() const noexcept
{
return pData->enabled;
}

void ButtonEventHandler::setEnabled(const bool enabled) noexcept
{
pData->setEnabled(enabled);
}

Point<double> ButtonEventHandler::getLastClickPosition() const noexcept
{
return pData->lastClickPos;
Expand Down Expand Up @@ -281,6 +317,7 @@ struct KnobEventHandler::PrivateData {
float value;
float valueDef;
float valueTmp;
bool enabled;
bool usingDefault;
bool usingLog;
Orientation orientation;
Expand All @@ -301,6 +338,7 @@ struct KnobEventHandler::PrivateData {
value(0.5f),
valueDef(value),
valueTmp(value),
enabled(true),
usingDefault(false),
usingLog(false),
orientation(Vertical),
Expand All @@ -320,6 +358,7 @@ struct KnobEventHandler::PrivateData {
value(other->value),
valueDef(other->valueDef),
valueTmp(value),
enabled(other->enabled),
usingDefault(other->usingDefault),
usingLog(other->usingLog),
orientation(other->orientation),
Expand All @@ -338,6 +377,7 @@ struct KnobEventHandler::PrivateData {
value = other->value;
valueDef = other->valueDef;
valueTmp = value;
enabled = other->enabled;
usingDefault = other->usingDefault;
usingLog = other->usingLog;
orientation = other->orientation;
Expand All @@ -363,6 +403,9 @@ struct KnobEventHandler::PrivateData {

bool mouseEvent(const Widget::MouseEvent& ev, const double scaleFactor)
{
if (! enabled)
return false;

if (ev.button != 1)
return false;

Expand Down Expand Up @@ -416,6 +459,9 @@ struct KnobEventHandler::PrivateData {

bool motionEvent(const Widget::MotionEvent& ev, const double scaleFactor)
{
if (! enabled)
return false;

if ((state & kKnobStateDragging) == 0x0)
return false;

Expand Down Expand Up @@ -501,6 +547,9 @@ struct KnobEventHandler::PrivateData {

bool scrollEvent(const Widget::ScrollEvent& ev)
{
if (! enabled)
return false;

if (! widget->contains(ev.pos))
return false;

Expand Down Expand Up @@ -541,6 +590,25 @@ struct KnobEventHandler::PrivateData {
return ((usingLog ? invlogscale(value) : value) - minimum) / diff;
}

void setEnabled(const bool enabled2) noexcept
{
if (enabled == enabled2)
return;

// reset temp vars if disabling
if (! enabled2)
{
state = kKnobStateDefault;
lastX = 0.0;
lastY = 0.0;
lastClickTime = 0;
valueTmp = value;
}

enabled = enabled2;
widget->repaint();
}

void setRange(const float min, const float max) noexcept
{
DISTRHO_SAFE_ASSERT_RETURN(max > min,);
Expand Down Expand Up @@ -598,6 +666,16 @@ KnobEventHandler::~KnobEventHandler()
delete pData;
}

bool KnobEventHandler::isEnabled() const noexcept
{
return pData->enabled;
}

void KnobEventHandler::setEnabled(const bool enabled) noexcept
{
pData->setEnabled(enabled);
}

bool KnobEventHandler::isInteger() const noexcept
{
return d_isEqual(pData->step, 1.f);
Expand Down

0 comments on commit 6b3d1da

Please sign in to comment.