Browse Source

Add ImageSlider class; misc fixes

tags/1.9.4
falkTX 12 years ago
parent
commit
b82d3403aa
7 changed files with 361 additions and 3 deletions
  1. +2
    -0
      source/libs/distrho/dgl/Geometry.hpp
  2. +1
    -0
      source/libs/distrho/dgl/Image.hpp
  3. +80
    -0
      source/libs/distrho/dgl/ImageSlider.hpp
  4. +12
    -0
      source/libs/distrho/dgl/src/Geometry.cpp
  5. +8
    -3
      source/libs/distrho/dgl/src/Image.cpp
  6. +253
    -0
      source/libs/distrho/dgl/src/ImageSlider.cpp
  7. +5
    -0
      source/libs/distrho/dgl/src/Widget.cpp

+ 2
- 0
source/libs/distrho/dgl/Geometry.hpp View File

@@ -103,6 +103,8 @@ public:

bool contains(T x, T y) const;
bool contains(const Point<T>& pos) const;
bool containsX(T x) const;
bool containsY(T y) const;

void setX(T x);
void setY(T y);


+ 1
- 0
source/libs/distrho/dgl/Image.hpp View File

@@ -43,6 +43,7 @@ public:
GLenum getType() const;

void draw();
void draw(int x, int y);
void draw(const Point<int>& pos);

Image& operator=(const Image& image);


+ 80
- 0
source/libs/distrho/dgl/ImageSlider.hpp View File

@@ -0,0 +1,80 @@
/*
* DISTRHO Plugin Toolkit (DPT)
* Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* For a full copy of the license see the LGPL.txt file
*/

#ifndef __DGL_IMAGE_SLIDER_HPP__
#define __DGL_IMAGE_SLIDER_HPP__

#include "Image.hpp"
#include "Widget.hpp"

START_NAMESPACE_DISTRHO

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

class ImageSlider : public Widget
{
public:
class Callback
{
public:
virtual ~Callback() {}
virtual void imageSliderDragStarted(ImageSlider* imageSlider) = 0;
virtual void imageSliderDragFinished(ImageSlider* imageSlider) = 0;
virtual void imageSliderValueChanged(ImageSlider* imageSlider, float value) = 0;
};

ImageSlider(Window* parent, const Image& image);
ImageSlider(const ImageSlider& imageSlider);

float getValue() const;

void setStartPos(const Point<int>& startPos);
void setEndPos(const Point<int>& endPos);

void setRange(float min, float max);
void setValue(float value, bool sendCallback = false);

void setCallback(Callback* callback);

protected:
void onDisplay();
bool onMouse(int button, bool press, int x, int y);
bool onMotion(int x, int y);

private:
Image fImage;
float fMinimum;
float fMaximum;
float fValue;

bool fDragging;
int fStartedX;
int fStartedY;

Callback* fCallback;

Point<int> fStartPos;
Point<int> fEndPos;
Rectangle<int> fSliderArea;

void _recheckArea();
};

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

END_NAMESPACE_DISTRHO

#endif // __DGL_IMAGE_SLIDER_HPP__

+ 12
- 0
source/libs/distrho/dgl/src/Geometry.cpp View File

@@ -309,6 +309,18 @@ bool Rectangle<T>::contains(const Point<T>& pos) const
return contains(pos.fX, pos.fY);
}

template<typename T>
bool Rectangle<T>::containsX(T x) const
{
return (x >= fPos.fX && x <= fPos.fX + fSize.fWidth);
}

template<typename T>
bool Rectangle<T>::containsY(T y) const
{
return (y >= fPos.fY && y <= fPos.fY + fSize.fHeight);
}

template<typename T>
void Rectangle<T>::setX(T x)
{


+ 8
- 3
source/libs/distrho/dgl/src/Image.cpp View File

@@ -89,18 +89,23 @@ GLenum Image::getType() const

void Image::draw()
{
draw(Point<int>(0, 0));
draw(0, 0);
}

void Image::draw(const Point<int>& pos)
void Image::draw(int x, int y)
{
if (! isValid())
return;

glRasterPos2i(pos.getX(), fSize.getHeight()+pos.getY());
glRasterPos2i(x, fSize.getHeight()+y);
glDrawPixels(fSize.getWidth(), fSize.getHeight(), fFormat, fType, fRawData);
}

void Image::draw(const Point<int>& pos)
{
draw(pos.getX(), pos.getY());
}

Image& Image::operator=(const Image& image)
{
fRawData = image.fRawData;


+ 253
- 0
source/libs/distrho/dgl/src/ImageSlider.cpp View File

@@ -0,0 +1,253 @@
/*
* DISTRHO Plugin Toolkit (DPT)
* Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* For a full copy of the license see the LGPL.txt file
*/

#include "../ImageSlider.hpp"

START_NAMESPACE_DISTRHO

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

ImageSlider::ImageSlider(Window* parent, const Image& image)
: Widget(parent),
fImage(image),
fMinimum(0.0f),
fMaximum(1.0f),
fValue(0.5f),
fDragging(false),
fStartedX(0),
fStartedY(0),
fCallback(nullptr)
{
setSize(fImage.getSize());
}

ImageSlider::ImageSlider(const ImageSlider& imageSlider)
: Widget(imageSlider.getParent()),
fImage(imageSlider.fImage),
fMinimum(imageSlider.fMinimum),
fMaximum(imageSlider.fMaximum),
fValue(imageSlider.fValue),
fDragging(false),
fStartedX(0),
fStartedY(0),
fCallback(nullptr)
{
setSize(fImage.getSize());
}

float ImageSlider::getValue() const
{
return fValue;
}

void ImageSlider::setStartPos(const Point<int>& startPos)
{
fStartPos = startPos;
_recheckArea();
}

void ImageSlider::setEndPos(const Point<int>& endPos)
{
fEndPos = endPos;
_recheckArea();
}

void ImageSlider::setRange(float min, float max)
{
if (fValue < min)
{
fValue = min;
repaint();

if (fCallback != nullptr)
fCallback->imageSliderValueChanged(this, fValue);
}
else if (fValue > max)
{
fValue = max;
repaint();

if (fCallback != nullptr)
fCallback->imageSliderValueChanged(this, fValue);
}

fMinimum = min;
fMaximum = max;
}

void ImageSlider::setValue(float value, bool sendCallback)
{
if (fValue == value)
return;

fValue = value;
repaint();

if (sendCallback && fCallback != nullptr)
fCallback->imageSliderValueChanged(this, fValue);
}

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

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());
#endif

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

int x, y;

if (fStartPos.getX() == fEndPos.getX())
{
x = fStartPos.getX();
y = fStartPos.getY() + normValue*(fEndPos.getY()-fStartPos.getY());
}
else if (fStartPos.getY() == fEndPos.getY())
{
x = fStartPos.getX() + normValue*(fEndPos.getX()-fStartPos.getX());
y = fStartPos.getY();
}
else
return;

fImage.draw(x, y);
}

bool ImageSlider::onMouse(int button, bool press, int x, int y)
{
if (button != 1)
return false;

if (press)
{
if (! fSliderArea.contains(x, y))
return false;

float vper;

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

float value = vper * (fMaximum - fMinimum) + fMinimum;

if (value < fMinimum)
value = fMinimum;
else if (value > fMaximum)
value = fMaximum;

fDragging = true;
fStartedX = x;
fStartedY = y;

if (fCallback != nullptr)
fCallback->imageSliderDragStarted(this);

setValue(value, true);

return true;
}
else if (fDragging)
{
if (fCallback != nullptr)
fCallback->imageSliderDragFinished(this);

fDragging = false;
return true;
}

return false;
}

bool ImageSlider::onMotion(int x, int y)
{
if (! fDragging)
return false;

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

if ((horizontal && fSliderArea.containsX(x)) || (fSliderArea.containsY(y) && ! horizontal))
{
float vper;

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

float value = vper * (fMaximum - fMinimum) + fMinimum;

if (value < fMinimum)
value = fMinimum;
else if (value > fMaximum)
value = fMaximum;

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

return true;
}

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

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

END_NAMESPACE_DISTRHO

+ 5
- 0
source/libs/distrho/dgl/src/Widget.cpp View File

@@ -177,22 +177,27 @@ void Widget::onDisplay()

bool Widget::onKeyboard(bool, uint32_t)
{
return false;
}

bool Widget::onMouse(int, bool, int, int)
{
return false;
}

bool Widget::onMotion(int, int)
{
return false;
}

bool Widget::onScroll(float, float)
{
return false;
}

bool Widget::onSpecial(bool, Key)
{
return false;
}

void Widget::onReshape(int width, int height)


Loading…
Cancel
Save