commit 3562ad248c032f7906bde90b81d127009c7f3b43
parent b4ac0311058bd1626d00e83824ec98a23651d873
Author: falkTX <falktx@gmail.com>
Date: Sat, 17 May 2014 01:34:32 +0100
Make widget coordinates relative
Diffstat:
5 files changed, 37 insertions(+), 23 deletions(-)
diff --git a/dgl/Widget.hpp b/dgl/Widget.hpp
@@ -57,14 +57,22 @@ public:
virtual void setSize(int width, int height) noexcept;
virtual void setSize(const Size<int>& size) noexcept;
- const Rectangle<int>& getArea() const noexcept;
-
uint getEventTimestamp() const noexcept;
int getModifiers() const noexcept;
App& getParentApp() const noexcept;
Window& getParentWindow() const noexcept;
+ /**
+ Check if this widget contains the point defined by @a X and @a Y.
+ */
+ bool contains(int x, int y) const noexcept;
+
+ /**
+ Check if this widget contains the point @a pos.
+ */
+ bool contains(const Point<int>& pos) const noexcept;
+
void repaint() noexcept;
protected:
diff --git a/dgl/src/ImageButton.cpp b/dgl/src/ImageButton.cpp
@@ -104,7 +104,7 @@ bool ImageButton::onMouse(int button, bool press, int x, int y)
repaint();
}
- if (! getArea().contains(x, y))
+ if (! contains(x, y))
{
fCurButton = -1;
return false;
@@ -113,7 +113,7 @@ bool ImageButton::onMouse(int button, bool press, int x, int y)
if (fCallback != nullptr)
fCallback->imageButtonClicked(this, fCurButton);
- //if (getArea().contains(x, y))
+ //if (contains(x, y))
//{
// fCurImage = &fImageHover;
// repaint();
@@ -124,7 +124,7 @@ bool ImageButton::onMouse(int button, bool press, int x, int y)
return true;
}
- if (press && getArea().contains(x, y))
+ if (press && contains(x, y))
{
if (fCurImage != &fImageDown)
{
@@ -144,7 +144,7 @@ bool ImageButton::onMotion(int x, int y)
if (fCurButton != -1)
return true;
- if (getArea().contains(x, y))
+ if (contains(x, y))
{
if (fCurImage != &fImageHover)
{
diff --git a/dgl/src/ImageKnob.cpp b/dgl/src/ImageKnob.cpp
@@ -248,7 +248,7 @@ void ImageKnob::onDisplay()
const GLint w2 = getWidth()/2;
const GLint h2 = getHeight()/2;
- glTranslatef(static_cast<float>(getX()+w2), static_cast<float>(getY()+h2), 0.0f);
+ glTranslatef(static_cast<float>(w2), static_cast<float>(h2), 0.0f);
glRotatef(normValue*static_cast<float>(fRotationAngle), 0.0f, 0.0f, 1.0f);
Rectangle<int>(-w2, -h2, getWidth(), getHeight()).draw();
@@ -268,7 +268,8 @@ void ImageKnob::onDisplay()
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glRasterPos2i(getX(), getY()+getHeight());
+ glRasterPos2i(0, getHeight());
+ //glRasterPos2i(getX(), getY()+getHeight());
glDrawPixels(fImgLayerSize, fImgLayerSize, fImage.getFormat(), fImage.getType(), fImage.getRawData() + imageDataOffset);
}
}
@@ -280,7 +281,7 @@ bool ImageKnob::onMouse(int button, bool press, int x, int y)
if (press)
{
- if (! getArea().contains(x, y))
+ if (! contains(x, y))
return false;
fDragging = true;
diff --git a/dgl/src/Widget.cpp b/dgl/src/Widget.cpp
@@ -127,6 +127,7 @@ void Widget::setWidth(int width) noexcept
fArea.setWidth(width);
fParent.repaint();
+ onReshape(width, fArea.getHeight());
}
void Widget::setHeight(int height) noexcept
@@ -136,6 +137,7 @@ void Widget::setHeight(int height) noexcept
fArea.setHeight(height);
fParent.repaint();
+ onReshape(fArea.getWidth(), height);
}
void Widget::setSize(int width, int height) noexcept
@@ -150,11 +152,7 @@ void Widget::setSize(const Size<int>& size) noexcept
fArea.setSize(size);
fParent.repaint();
-}
-
-const Rectangle<int>& Widget::getArea() const noexcept
-{
- return fArea;
+ onReshape(fArea.getWidth(), fArea.getHeight());
}
uint Widget::getEventTimestamp() const noexcept
@@ -177,6 +175,16 @@ Window& Widget::getParentWindow() const noexcept
return fParent;
}
+bool Widget::contains(int x, int y) const noexcept
+{
+ return (x >= 0 && y >= 0 && x < fArea.getWidth() && y < fArea.getHeight());
+}
+
+bool Widget::contains(const Point<int>& pos) const noexcept
+{
+ return contains(pos.getX(), pos.getY());
+}
+
void Widget::repaint() noexcept
{
fParent.repaint();
diff --git a/dgl/src/Window.cpp b/dgl/src/Window.cpp
@@ -582,7 +582,10 @@ struct Window::PrivateData {
Widget* const widget(*it);
if (widget->isVisible())
+ {
+ glViewport(widget->getX(), -widget->getY(), fView->width, fView->height);
widget->onDisplay();
+ }
}
fSelf->onDisplayAfter();
@@ -615,7 +618,7 @@ struct Window::PrivateData {
{
Widget* const widget(*rit);
- if (widget->isVisible() && widget->onMouse(button, press, x, y))
+ if (widget->isVisible() && widget->onMouse(button, press, x-widget->getX(), y-widget->getY()))
break;
}
}
@@ -631,7 +634,7 @@ struct Window::PrivateData {
{
Widget* const widget(*rit);
- if (widget->isVisible() && widget->onMotion(x, y))
+ if (widget->isVisible() && widget->onMotion(x-widget->getX(), y-widget->getY()))
break;
}
}
@@ -647,7 +650,7 @@ struct Window::PrivateData {
{
Widget* const widget(*rit);
- if (widget->isVisible() && widget->onScroll(x, y, dx, dy))
+ if (widget->isVisible() && widget->onScroll(x-widget->getX(), y-widget->getY(), dx, dy))
break;
}
}
@@ -673,12 +676,6 @@ struct Window::PrivateData {
DBGp("PUGL: onReshape : %i %i\n", width, height);
fSelf->onReshape(width, height);
-
- FOR_EACH_WIDGET(it)
- {
- Widget* const widget(*it);
- widget->onReshape(width, height);
- }
}
void onClose()