Browse Source

Fix slider

gh-pages
falkTX 9 years ago
parent
commit
402fd5d0ec
3 changed files with 62 additions and 30 deletions
  1. +0
    -3
      dgl/Geometry.hpp
  2. +2
    -0
      dgl/ImageSlider.hpp
  3. +60
    -27
      dgl/src/ImageSlider.cpp

+ 0
- 3
dgl/Geometry.hpp View File

@@ -50,7 +50,6 @@ private:
T fX, fY;
template<typename> friend class Rectangle;

DISTRHO_LEAK_DETECTOR(Point)
DISTRHO_PREVENT_HEAP_ALLOCATION
};

@@ -82,7 +81,6 @@ private:
T fWidth, fHeight;
template<typename> friend class Rectangle;

DISTRHO_LEAK_DETECTOR(Size)
DISTRHO_PREVENT_HEAP_ALLOCATION
};

@@ -131,7 +129,6 @@ private:
Point<T> fPos;
Size<T> fSize;

DISTRHO_LEAK_DETECTOR(Rectangle)
DISTRHO_PREVENT_HEAP_ALLOCATION
};



+ 2
- 0
dgl/ImageSlider.hpp View File

@@ -47,6 +47,7 @@ public:
void setEndPos(const Point<int>& endPos);
void setEndPos(int x, int y);

void setInverted(bool inverted);
void setRange(float min, float max);
void setStep(float step);
void setValue(float value, bool sendCallback = false);
@@ -67,6 +68,7 @@ private:
float fValueTmp;

bool fDragging;
bool fInverted;
int fStartedX;
int fStartedY;



+ 60
- 27
dgl/src/ImageSlider.cpp View File

@@ -31,6 +31,7 @@ ImageSlider::ImageSlider(Window& parent, const Image& image)
fValue(0.5f),
fValueTmp(fValue),
fDragging(false),
fInverted(false),
fStartedX(0),
fStartedY(0),
fCallback(nullptr)
@@ -47,6 +48,7 @@ ImageSlider::ImageSlider(Widget* widget, const Image& image)
fValue(0.5f),
fValueTmp(fValue),
fDragging(false),
fInverted(false),
fStartedX(0),
fStartedY(0),
fCallback(nullptr)
@@ -63,6 +65,7 @@ ImageSlider::ImageSlider(const ImageSlider& imageSlider)
fValue(imageSlider.fValue),
fValueTmp(fValue),
fDragging(false),
fInverted(imageSlider.fInverted),
fStartedX(0),
fStartedY(0),
fCallback(imageSlider.fCallback),
@@ -100,6 +103,15 @@ void ImageSlider::setEndPos(int x, int y)
setEndPos(Point<int>(x, y));
}

void ImageSlider::setInverted(bool inverted)
{
if (fInverted == inverted)
return;

fInverted = inverted;
repaint();
}

void ImageSlider::setRange(float min, float max)
{
if (fValue < min)
@@ -154,24 +166,33 @@ void ImageSlider::onDisplay()
#if 0 // DEBUG, paints slider area
glColor3f(0.4f, 0.5f, 0.1f);
glRecti(fSliderArea.getX(), fSliderArea.getY(), fSliderArea.getX()+fSliderArea.getWidth(), fSliderArea.getY()+fSliderArea.getHeight());
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
#endif

float normValue = (fValue - fMinimum) / (fMaximum - fMinimum);

int x, y;

if (fStartPos.getX() == fEndPos.getX())
if (fStartPos.getY() == fEndPos.getY())
{
x = fStartPos.getX();
y = fEndPos.getY() - static_cast<int>(normValue*static_cast<float>(fEndPos.getY()-fStartPos.getY()));
}
else if (fStartPos.getY() == fEndPos.getY())
{
x = fEndPos.getX() - static_cast<int>(normValue*static_cast<float>(fEndPos.getX()-fStartPos.getX()));
// horizontal
if (fInverted)
x = fEndPos.getX() - static_cast<int>(normValue*static_cast<float>(fEndPos.getX()-fStartPos.getX()));
else
x = fStartPos.getX() + static_cast<int>(normValue*static_cast<float>(fEndPos.getX()-fStartPos.getX()));
y = fStartPos.getY();
}
else
return;
{
// vertical
x = fStartPos.getX();

if (fInverted)
y = fEndPos.getY() - static_cast<int>(normValue*static_cast<float>(fEndPos.getY()-fStartPos.getY()));
else
y = fStartPos.getY() + static_cast<int>(normValue*static_cast<float>(fEndPos.getY()-fStartPos.getY()));
}

fImage.draw(x, y);
}
@@ -188,22 +209,23 @@ bool ImageSlider::onMouse(int button, bool press, int x, int y)

float vper;

if (fStartPos.getX() == fEndPos.getX())
{
// vertical
vper = float(y - fSliderArea.getY()) / float(fSliderArea.getHeight());
}
else if (fStartPos.getY() == fEndPos.getY())
if (fStartPos.getY() == fEndPos.getY())
{
// horizontal
vper = float(x - fSliderArea.getX()) / float(fSliderArea.getWidth());
}
else
return false;
{
// vertical
vper = float(y - fSliderArea.getY()) / float(fSliderArea.getHeight());
}

float value;

value = fMaximum - vper * (fMaximum - fMinimum);
if (fInverted)
value = fMaximum - vper * (fMaximum - fMinimum);
else
value = fMinimum + vper * (fMaximum - fMinimum);

if (value < fMinimum)
{
@@ -250,7 +272,7 @@ bool ImageSlider::onMotion(int x, int y)
if (! fDragging)
return false;

bool horizontal = fStartPos.getY() == fEndPos.getY();
const bool horizontal = fStartPos.getY() == fEndPos.getY();

if ((horizontal && fSliderArea.containsX(x)) || (fSliderArea.containsY(y) && ! horizontal))
{
@@ -269,7 +291,10 @@ bool ImageSlider::onMotion(int x, int y)

float value;

value = fMaximum - vper * (fMaximum - fMinimum);
if (fInverted)
value = fMaximum - vper * (fMaximum - fMinimum);
else
value = fMinimum + vper * (fMaximum - fMinimum);

if (value < fMinimum)
{
@@ -290,13 +315,19 @@ bool ImageSlider::onMotion(int x, int y)

setValue(value, true);
}
else if (y < fSliderArea.getY())
else if (horizontal)
{
setValue(fMaximum, true);
if (x < fSliderArea.getX())
setValue(fInverted ? fMaximum : fMinimum, true);
else
setValue(fInverted ? fMinimum : fMaximum, true);
}
else
{
setValue(fMinimum, true);
if (y < fSliderArea.getY())
setValue(fInverted ? fMaximum : fMinimum, true);
else
setValue(fInverted ? fMinimum : fMaximum, true);
}

return true;
@@ -304,19 +335,21 @@ bool ImageSlider::onMotion(int x, int y)

void ImageSlider::_recheckArea()
{
if (fStartPos.getX() == fEndPos.getX())
if (fStartPos.getY() == fEndPos.getY())
{
// horizontal
fSliderArea = Rectangle<int>(fStartPos.getX(),
fStartPos.getY(),
fImage.getWidth(),
fEndPos.getY() + fImage.getHeight() - fStartPos.getY());
fEndPos.getX() + fImage.getWidth() - fStartPos.getX(),
fImage.getHeight());
}
else if (fStartPos.getY() == fEndPos.getY())
else
{
// vertical
fSliderArea = Rectangle<int>(fStartPos.getX(),
fStartPos.getY(),
fEndPos.getX() + fImage.getWidth() - fStartPos.getX(),
fImage.getHeight());
fImage.getWidth(),
fEndPos.getY() + fImage.getHeight() - fStartPos.getY());
}
}



Loading…
Cancel
Save