Add button callbacks

This commit is contained in:
yaw-man 2022-08-18 11:21:03 -03:00
parent 5846220601
commit 25d54ad976
2 changed files with 33 additions and 30 deletions

10
.gitignore vendored
View File

@ -1,4 +1,6 @@
out out/
.vs .vs/
doc doc/
releases build/
releases/
lib/DPF/khronos/

View File

@ -1,12 +1,15 @@
#include "DistrhoUI.hpp" #include "DistrhoUI.hpp"
#include "tablet.h" #include "tablet.h"
#ifdef DEBUG
#include <format> #include <format>
#include <optional> #include <optional>
#endif
START_NAMESPACE_DISTRHO START_NAMESPACE_DISTRHO
//UI element for mapping the currently pressed pen button combination to a parameter. //UI element for mapping the currently pressed pen button combination to a parameter.
class ButtonMappingWidget : public NanoSubWidget class ButtonMappingWidget : public NanoSubWidget,
public ButtonEventHandler
{ {
public: public:
ButtonMappingWidget( ButtonMappingWidget(
@ -14,7 +17,8 @@ public:
float initialSize, float initialSize,
float initialX, float initialX,
float initialY, float initialY,
Parameters associatedParameter) : Parameters associatedParameter,
ButtonEventHandler::Callback* const callback) :
x(initialX), x(initialX),
y(initialY), y(initialY),
size(initialSize), size(initialSize),
@ -22,41 +26,33 @@ public:
isClicked(false), isClicked(false),
isPenPressed(false), isPenPressed(false),
mask(0), mask(0),
NanoSubWidget(parent) NanoSubWidget(parent),
ButtonEventHandler(this)
{ {
setNeedsFullViewportDrawing(true);
setSize(Size<uint>(static_cast<uint>(size), static_cast<uint>(size))); setSize(Size<uint>(static_cast<uint>(size), static_cast<uint>(size)));
setAbsolutePos((int)x, (int)y); setAbsolutePos((int)x, (int)y);
ButtonEventHandler::setCallback(callback);
} }
void onNanoDisplay() override void onNanoDisplay() override
{ {
beginPath(); beginPath();
strokeColor(200, 200, 200); strokeColor(200, 200, 200);
//if (isClicked) fillColor(0.5f, 0.5f, 0.5f, 0.5f * (isClicked + isPenPressed));
fillColor(0.5f, 0.5f, 0.5f, 0.5f); roundedRect(0.f, 0.f, size, size, 0.5f * size);
//roundedRect(x, y, size, size, 0.f); stroke();
circle(0.5 * getWidth(), 0.5 * getHeight(), 10.f); fill();
closePath(); closePath();
} }
bool onMouse(const MouseEvent& ev) override bool onMouse(const MouseEvent& ev) override
{ {
isClicked = ev.press && contains(ev.pos); isClicked = ev.press;
repaint(); return ButtonEventHandler::mouseEvent(ev);
return false;
} }
void setMask(const ButtonMask m) void setMask(const ButtonMask m) { mask = m; }
{ bool matchesMask(const ButtonMask m) { return isPenPressed = (m && mask == m); }
mask = m;
}
bool matchesMask(const ButtonMask m)
{
isPenPressed = (m && mask == m);
return isPenPressed;
}
bool isClicked; bool isClicked;
bool isPenPressed; bool isPenPressed;
@ -71,7 +67,8 @@ private:
}; };
class TabUI : public UI class TabUI : public UI,
public ButtonEventHandler::Callback
{ {
static const uint kInitialWidth = 800; static const uint kInitialWidth = 800;
static const uint kInitialHeight = 600; static const uint kInitialHeight = 600;
@ -80,8 +77,8 @@ public:
TabUI() TabUI()
: UI(kInitialWidth, kInitialHeight), : UI(kInitialWidth, kInitialHeight),
tab(getWindow().getNativeWindowHandle()), tab(getWindow().getNativeWindowHandle()),
AButtonWidget(this, 5.f, 0.f, 0.f, kParameterButtonA), AButtonWidget(this, 75.f, 700.f, 500.f, kParameterButtonA, this),
BButtonWidget(this, 50.f, 0.f, 0.f, kParameterButtonB) BButtonWidget(this, 75.f, 700.f, 400.f, kParameterButtonB, this)
{ {
#ifdef DGL_NO_SHARED_RESOURCES #ifdef DGL_NO_SHARED_RESOURCES
@ -118,8 +115,12 @@ protected:
setParameterValue(kParameterButtonB, 1.f); setParameterValue(kParameterButtonB, 1.f);
else setParameterValue(kParameterButtonB, 0.f); else setParameterValue(kParameterButtonB, 0.f);
if (AButtonWidget.isClicked) AButtonWidget.setMask(buttonMask); }
if (BButtonWidget.isClicked) BButtonWidget.setMask(buttonMask);
void buttonClicked(SubWidget* const widget, int ) override
{
if (widget == &AButtonWidget) AButtonWidget.setMask(pkt.buttons);
if (widget == &BButtonWidget) BButtonWidget.setMask(pkt.buttons);
} }
void parameterChanged(uint32_t index, float value) override void parameterChanged(uint32_t index, float value) override