Browse Source

Update to include most of damo22 ImageKnob changes

gh-pages
falkTX 7 years ago
parent
commit
a8aa58f365
3 changed files with 95 additions and 19 deletions
  1. +10
    -2
      dgl/ImageKnob.hpp
  2. +2
    -2
      dgl/Widget.hpp
  3. +83
    -15
      dgl/src/ImageKnob.cpp

+ 10
- 2
dgl/ImageKnob.hpp View File

@@ -51,18 +51,21 @@ public:

float getValue() const noexcept;

void setOrientation(Orientation orientation) noexcept;
void setDefault(float def) noexcept;
void setRange(float min, float max) noexcept;
void setStep(float step) noexcept;
void setValue(float value, bool sendCallback = false) noexcept;
void setRotationAngle(int angle);
void setUsingLogScale(bool yesNo) noexcept;

void setCallback(Callback* callback) noexcept;
void setOrientation(Orientation orientation) noexcept;
void setRotationAngle(int angle);

protected:
void onDisplay() override;
bool onMouse(const MouseEvent&) override;
bool onMotion(const MotionEvent&) override;
bool onScroll(const ScrollEvent&) override;

private:
Image fImage;
@@ -71,7 +74,9 @@ private:
float fMaximum;
float fStep;
float fValue;
float fValueDef;
float fValueTmp;
bool fUsingLog;
Orientation fOrientation;

int fRotationAngle;
@@ -87,6 +92,9 @@ private:
Rectangle<int> fKnobArea;
GLuint fTextureId;

float _logscale(float value) const;
float _invlogscale(float value) const;

DISTRHO_LEAK_DETECTOR(ImageKnob)
};



+ 2
- 2
dgl/Widget.hpp View File

@@ -107,12 +107,12 @@ public:
/**
Mouse scroll event.
@param pos The scroll distance.
@param _ TODO
@param dir The scroll direction.
@see onScroll
*/
struct ScrollEvent : BaseEvent {
Point<int> pos;
Point<float> _;
Point<float> delta;
};

/**


+ 83
- 15
dgl/src/ImageKnob.cpp View File

@@ -30,7 +30,9 @@ ImageKnob::ImageKnob(Window& parent, const Image& image, Orientation orientation
fMaximum(1.0f),
fStep(0.0f),
fValue(0.5f),
fValueDef(fValue),
fValueTmp(fValue),
fUsingLog(false),
fOrientation(orientation),
fRotationAngle(0),
fDragging(false),
@@ -54,7 +56,9 @@ ImageKnob::ImageKnob(Widget* widget, const Image& image, Orientation orientation
fMaximum(1.0f),
fStep(0.0f),
fValue(0.5f),
fValueDef(fValue),
fValueTmp(fValue),
fUsingLog(false),
fOrientation(orientation),
fRotationAngle(0),
fDragging(false),
@@ -78,7 +82,9 @@ ImageKnob::ImageKnob(const ImageKnob& imageKnob)
fMaximum(imageKnob.fMaximum),
fStep(imageKnob.fStep),
fValue(imageKnob.fValue),
fValueDef(imageKnob.fValueDef),
fValueTmp(fValue),
fUsingLog(imageKnob.fUsingLog),
fOrientation(imageKnob.fOrientation),
fRotationAngle(imageKnob.fRotationAngle),
fDragging(false),
@@ -122,12 +128,9 @@ float ImageKnob::getValue() const noexcept
return fValue;
}

void ImageKnob::setOrientation(Orientation orientation) noexcept
void ImageKnob::setDefault(float value) noexcept
{
if (fOrientation == orientation)
return;

fOrientation = orientation;
fValueDef = value;
}

void ImageKnob::setRange(float min, float max) noexcept
@@ -171,7 +174,7 @@ void ImageKnob::setValue(float value, bool sendCallback) noexcept
if (fValue == value)
return;

fValue = value;
fValue = fUsingLog ? _logscale(value) : value;

if (fStep == 0.0f)
fValueTmp = value;
@@ -186,6 +189,24 @@ void ImageKnob::setValue(float value, bool sendCallback) noexcept
}
}

void ImageKnob::setUsingLogScale(bool yesNo) noexcept
{
fUsingLog = yesNo;
}

void ImageKnob::setCallback(Callback* callback) noexcept
{
fCallback = callback;
}

void ImageKnob::setOrientation(Orientation orientation) noexcept
{
if (fOrientation == orientation)
return;

fOrientation = orientation;
}

void ImageKnob::setRotationAngle(int angle)
{
if (fRotationAngle == angle)
@@ -206,8 +227,8 @@ void ImageKnob::setRotationAngle(int angle)
glGenTextures(1, &fTextureId);
glBindTexture(GL_TEXTURE_2D, fTextureId);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWidth(), getHeight(), 0, fImage.getFormat(), fImage.getType(), fImage.getRawData());

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -223,14 +244,9 @@ void ImageKnob::setRotationAngle(int angle)
}
}

void ImageKnob::setCallback(Callback* callback) noexcept
{
fCallback = callback;
}

void ImageKnob::onDisplay()
{
const float normValue = (fValue - fMinimum) / (fMaximum - fMinimum);
const float normValue = (fUsingLog ? _invlogscale(fValue) : fValue - fMinimum) / (fMaximum - fMinimum);

if (fRotationAngle != 0)
{
@@ -290,6 +306,13 @@ bool ImageKnob::onMouse(const MouseEvent& ev)
if (! contains(ev.pos))
return false;

if (ev.mod & MODIFIER_SHIFT)
{
setValue(fValueDef);
fValueTmp = fValue;
return true;
}

fDragging = true;
fLastX = ev.pos.getX();
fLastY = ev.pos.getY();
@@ -321,7 +344,7 @@ bool ImageKnob::onMotion(const MotionEvent& ev)

if (fOrientation == ImageKnob::Horizontal)
{
if (int movX = ev.pos.getX() - fLastX)
if (const int movX = ev.pos.getX() - fLastX)
{
d = (ev.mod & MODIFIER_SHIFT) ? 2000.0f : 200.0f;
value = fValueTmp + (float(fMaximum - fMinimum) / d * float(movX));
@@ -330,7 +353,7 @@ bool ImageKnob::onMotion(const MotionEvent& ev)
}
else if (fOrientation == ImageKnob::Vertical)
{
if (int movY = fLastY - ev.pos.getY())
if (const int movY = fLastY - ev.pos.getY())
{
d = (ev.mod & MODIFIER_SHIFT) ? 2000.0f : 200.0f;
value = fValueTmp + (float(fMaximum - fMinimum) / d * float(movY));
@@ -366,6 +389,51 @@ bool ImageKnob::onMotion(const MotionEvent& ev)
return true;
}

bool ImageKnob::onScroll(const ScrollEvent& ev)
{
if (! contains(ev.pos))
return false;

const float d = (ev.mod & MODIFIER_CTRL) ? 2000.0f : 200.0f;
float value = (fValueTmp) + (float(fMaximum - fMinimum) / d * 10.f * ev.delta.getY());

if (value < fMinimum)
{
value = fMinimum;
fValueTmp = value;
}
else if (value > fMaximum)
{
value = fMaximum;
fValueTmp = value;
}
else if (fStep != 0.0f)
{
fValueTmp = value;
const float rest = std::fmod(value, fStep);
value = value - rest + (rest > fStep/2.0f ? fStep : 0.0f);
}

setValue(value, true);
return true;
}

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

float ImageKnob::_logscale(float value) const
{
const float b = std::log(fMaximum/fMinimum)/(fMaximum-fMinimum);
const float a = fMaximum/std::exp(fMaximum*b);
return a * std::exp(b*value);
}

float ImageKnob::_invlogscale(float value) const
{
const float b = std::log(fMaximum/fMinimum)/(fMaximum-fMinimum);
const float a = fMaximum/std::exp(fMaximum*b);
return std::log(value/a)/b;
}

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

END_NAMESPACE_DGL

Loading…
Cancel
Save