Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pdvg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "src/Bang.hpp"
#include "src/Canvas.hpp"
#include "src/Jutils.hpp"
#include "src/Common.hpp"
#include "src/Comment.hpp"
#include "src/DragNum.hpp"
Expand All @@ -19,3 +20,4 @@
#include "src/Slider.hpp"
#include "src/Subpatch.hpp"
#include "src/Knob.hpp"
#include "src/Popmenu.hpp"
69 changes: 30 additions & 39 deletions src/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstring>
#include "nanovg.h"
#include "Widget.hpp"
#include "Jutils.hpp"

START_NAMESPACE_DISTRHO

Expand Down Expand Up @@ -64,14 +65,12 @@ inline DGL::Rectangle<float> reduceRectangle(DGL::Rectangle<float> r, float amou
{
// (x + delta, y + delta, w - delta * 2, h - delta * 2)

DGL::Rectangle<float> nR(
return DGL::Rectangle<float>(
r.getX() + amount,
r.getY() + amount,
r.getWidth() - amount * 2,
r.getHeight() - amount * 2
);

return nR;
}

inline DGL::Rectangle<float> subtractBorder(DGL::Rectangle<float> r, Border border)
Expand All @@ -84,52 +83,44 @@ inline DGL::Rectangle<float> subtractBorder(DGL::Rectangle<float> r, Border bord
);
}

inline float valToPropOfLen(float const value, float const length)
static DGL::Rectangle<float> removeFromRight(DGL::Rectangle<float> r, float amount)
{
return value / length;
}


/** Some utility functions taken from JUCE ISC code */
float amountRemove = jmin(amount, r.getWidth());

/** Returns the smaller of two values. */
template <typename Type>
constexpr Type jmin (Type a, Type b) { return b < a ? b : a; }

/** Returns the larger of two values. */
template <typename Type>
constexpr Type jmax (Type a, Type b) { return a < b ? b : a; }

/** Returns the larger of three values. */
template <typename Type>
constexpr Type jmax (Type a, Type b, Type c) { return a < b ? (b < c ? c : b) : (a < c ? c : a); }
return DGL::Rectangle<float>(
r.getX() + r.getWidth() - amountRemove,
r.getY(),
amountRemove,
r.getHeight()
);
}

/** Remaps a normalised value (between 0 and 1) to a target range.
This effectively returns (targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin)).
*/
template <typename Type>
constexpr Type jmap (Type value0To1, Type targetRangeMin, Type targetRangeMax)
static DGL::Rectangle<float> resizeCentered(DGL::Rectangle<float> r, float width, float height)
{
return targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin);
return DGL::Rectangle<float>(
r.getX() + (r.getWidth() - width) / 2.0f,
r.getY() + (r.getHeight() - height) / 2.0f,
width,
height
);
}

/** Remaps a value from a source range to a target range. */
template <typename Type>
Type jmap (Type sourceValue, Type sourceRangeMin, Type sourceRangeMax, Type targetRangeMin, Type targetRangeMax)
static DGL::Rectangle<float> translateRectangle(DGL::Rectangle<float> r, float x, float y)
{
return targetRangeMin + ((targetRangeMax - targetRangeMin) * (sourceValue - sourceRangeMin)) / (sourceRangeMax - sourceRangeMin);
return DGL::Rectangle<float>(
r.getX() + x,
r.getY() + y,
r.getWidth(),
r.getHeight()
);
}

/** Converts an angle in degrees to radians. */
template <typename FloatType>
constexpr FloatType degreesToRadians (FloatType degrees) noexcept { return degrees * (NVG_PI / FloatType (180)); }

/** Converts an angle in radians to degrees. */
template <typename FloatType>
constexpr FloatType radiansToDegrees (FloatType radians) noexcept { return radians * (FloatType (180) / NVG_PI); }

static float valToPropOfLen(float const value, float const length)
{
return value / length;
}

inline float getColorBrightness(NVGcolor c)
static float getColorBrightness(NVGcolor c)
{
float brightness = 0.0f;

Expand Down
126 changes: 124 additions & 2 deletions src/ExtraEventHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ struct PDToggleEventHandler::PrivateData
widget(w),
callback(other->callback),
isDown(other->isDown)

{
}

Expand Down Expand Up @@ -682,7 +681,6 @@ struct PDRadioEventHandler::PrivateData

bool mouseEvent(const Widget::MouseEvent &ev)
{

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

Expand Down Expand Up @@ -1655,5 +1653,129 @@ bool PDKnobEventHandler::scrollEvent(const Widget::ScrollEvent &ev)
return pData->scrollEvent(ev);
}
// end knob
// --------------------------------------------------------------------------------------------------------------------

// begin popmenu
struct PDPopmenuEventHandler::PrivateData
{
PDPopmenuEventHandler *const self;
SubWidget *const widget;
PDPopmenuEventHandler::Callback *callback;

uint length;
uint value;

PrivateData(PDPopmenuEventHandler *const s, SubWidget *const w)
: self(s),
widget(w),
callback(nullptr),
length(0),
value(0)
{
}

PrivateData(PDPopmenuEventHandler *const s, SubWidget *const w, PrivateData *const other)
: self(s),
widget(w),
callback(other->callback),
length(other->length),
value(other->value)
{
}

void assignFrom(PrivateData *const other)
{
callback = other->callback;
length = other->length;
value = other->value;
}

bool mouseEvent(const Widget::MouseEvent &ev)
{
if (ev.button != 1)
return false;

PDWidget* pdWidget = dynamic_cast<PDWidget*>(widget);
if (ev.press && pdWidget->contains(ev.pos))
{
return true;
}
return false;
}

bool motionEvent(const Widget::MotionEvent &ev)
{
return false;
}

bool setValue(const uint value2, const bool sendCallback)
{
if (value != value2)
{
value = value2;
if (sendCallback && callback != nullptr)
{
try
{
callback->popmenuClicked(widget, value);
}
DISTRHO_SAFE_EXCEPTION("PDPopmenuEventHandler::setValue");
}
return true;
}
return false;
}
};

// --------------------------------------------------------------------------------------------------------------------

PDPopmenuEventHandler::PDPopmenuEventHandler(SubWidget *const self)
: pData(new PrivateData(this, self)) {}

PDPopmenuEventHandler::PDPopmenuEventHandler(SubWidget *const self, const PDPopmenuEventHandler &other)
: pData(new PrivateData(this, self, other.pData)) {}

PDPopmenuEventHandler &PDPopmenuEventHandler::operator=(const PDPopmenuEventHandler &other)
{
pData->assignFrom(other.pData);
return *this;
}

PDPopmenuEventHandler::~PDPopmenuEventHandler()
{
delete pData;
}

uint PDPopmenuEventHandler::getValue() const noexcept
{
return pData->value;
}

bool PDPopmenuEventHandler::setValue(const int value, const bool sendCallback) noexcept
{
return pData->setValue(static_cast<uint>(value), sendCallback);
}

void PDPopmenuEventHandler::setLength(int length) noexcept
{
pData->length = length;
}

void PDPopmenuEventHandler::setCallback(Callback *const callback) noexcept
{
pData->callback = callback;
}

bool PDPopmenuEventHandler::mouseEvent(const Widget::MouseEvent &ev)
{
return pData->mouseEvent(ev);
}

bool PDPopmenuEventHandler::motionEvent(const Widget::MotionEvent &ev)
{
return pData->motionEvent(ev);
}

// end popmenu

END_NAMESPACE_DGL
33 changes: 33 additions & 0 deletions src/ExtraEventHandlers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,39 @@ class PDKnobEventHandler
DISTRHO_LEAK_DETECTOR(PDKnobEventHandler)
};

class PDPopmenuEventHandler
{
public:
class Callback
{
public:
virtual ~Callback() {}
virtual void popmenuClicked(SubWidget *widget, uint index) = 0;
};

explicit PDPopmenuEventHandler(SubWidget *self);
explicit PDPopmenuEventHandler(SubWidget *self, const PDPopmenuEventHandler &other);
PDPopmenuEventHandler &operator=(const PDPopmenuEventHandler &other);
~PDPopmenuEventHandler();

uint getValue() const noexcept;

virtual bool setValue(int value, bool sendCallback = false) noexcept;

void setLength(int length) noexcept;
void setCallback(Callback *callback) noexcept;

bool mouseEvent(const Widget::MouseEvent &ev);
bool motionEvent(const Widget::MotionEvent &ev);

protected:
private:
struct PrivateData;
PrivateData *const pData;

DISTRHO_LEAK_DETECTOR(PDPopmenuEventHandler)
};

// --------------------------------------------------------------------------------------------------------------------

END_NAMESPACE_DGL
50 changes: 50 additions & 0 deletions src/Jutils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022 - Raw Material Software Limited
* SPDX-License-Identifier: ISC
*/

#pragma once

#include "nanovg.h"

START_NAMESPACE_DISTRHO

/** Some utility functions taken from JUCE ISC code */

/** Returns the smaller of two values. */
template <typename Type>
constexpr Type jmin (Type a, Type b) { return b < a ? b : a; }

/** Returns the larger of two values. */
template <typename Type>
constexpr Type jmax (Type a, Type b) { return a < b ? b : a; }

/** Returns the larger of three values. */
template <typename Type>
constexpr Type jmax (Type a, Type b, Type c) { return a < b ? (b < c ? c : b) : (a < c ? c : a); }

/** Remaps a normalised value (between 0 and 1) to a target range.
This effectively returns (targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin)).
*/
template <typename Type>
constexpr Type jmap (Type value0To1, Type targetRangeMin, Type targetRangeMax)
{
return targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin);
}

/** Remaps a value from a source range to a target range. */
template <typename Type>
Type jmap (Type sourceValue, Type sourceRangeMin, Type sourceRangeMax, Type targetRangeMin, Type targetRangeMax)
{
return targetRangeMin + ((targetRangeMax - targetRangeMin) * (sourceValue - sourceRangeMin)) / (sourceRangeMax - sourceRangeMin);
}

/** Converts an angle in degrees to radians. */
template <typename FloatType>
constexpr FloatType degreesToRadians (FloatType degrees) noexcept { return degrees * (NVG_PI / FloatType (180)); }

/** Converts an angle in radians to degrees. */
template <typename FloatType>
constexpr FloatType radiansToDegrees (FloatType radians) noexcept { return radians * (FloatType (180) / NVG_PI); }

END_NAMESPACE_DISTRHO
Loading