commit 3ec241f04c4850be3c39049fa6ad53653ab24c4c
parent 142c6ddebd4f7bd8c8507591de7768d09792d588
Author: falkTX <falktx@gmail.com>
Date: Tue, 6 May 2014 18:58:09 +0200
More base changes
Diffstat:
6 files changed, 138 insertions(+), 65 deletions(-)
diff --git a/dgl/Geometry.hpp b/dgl/Geometry.hpp
@@ -70,6 +70,8 @@ public:
void setWidth(const T& width) noexcept;
void setHeight(const T& height) noexcept;
+ void setSize(const T& width, const T& height) noexcept;
+ void setSize(const Size<T>& size) noexcept;
void growBy(const T& multiplier) noexcept;
void shrinkBy(const T& divider) noexcept;
@@ -110,27 +112,34 @@ public:
const Point<T>& getPos() const noexcept;
const Size<T>& getSize() const noexcept;
- bool contains(const T& x, const T& y) const noexcept;
- bool contains(const Point<T>& pos) const noexcept;
- bool containsX(const T& x) const noexcept;
- bool containsY(const T& y) const noexcept;
-
void setX(const T& x) noexcept;
void setY(const T& y) noexcept;
void setPos(const T& x, const T& y) noexcept;
void setPos(const Point<T>& pos) noexcept;
- void move(const T& x, const T& y) noexcept;
- void move(const Point<T>& pos) noexcept;
+ void moveBy(const T& x, const T& y) noexcept;
+ void moveBy(const Point<T>& pos) noexcept;
void setWidth(const T& width) noexcept;
void setHeight(const T& height) noexcept;
void setSize(const T& width, const T& height) noexcept;
void setSize(const Size<T>& size) noexcept;
+ void growBy(const T& multiplier) noexcept;
+ void shrinkBy(const T& divider) noexcept;
+
+ bool contains(const T& x, const T& y) const noexcept;
+ bool contains(const Point<T>& pos) const noexcept;
+ bool containsX(const T& x) const noexcept;
+ bool containsY(const T& y) const noexcept;
+
void draw();
Rectangle<T>& operator=(const Rectangle<T>& rect) noexcept;
+ Rectangle<T>& operator*=(const T& m) noexcept;
+ Rectangle<T>& operator/=(const T& d) noexcept;
+ bool operator==(const Rectangle<T>& size) const noexcept;
+ bool operator!=(const Rectangle<T>& size) const noexcept;
private:
Point<T> fPos;
diff --git a/dgl/Widget.hpp b/dgl/Widget.hpp
@@ -53,8 +53,8 @@ public:
void setPos(int x, int y);
void setPos(const Point<int>& pos);
- void move(int x, int y);
- void move(const Point<int>& pos);
+ void moveBy(int x, int y);
+ void moveBy(const Point<int>& pos);
int getWidth() const noexcept;
int getHeight() const noexcept;
@@ -63,13 +63,9 @@ public:
// virtual needed by cairo
virtual void setWidth(int width);
virtual void setHeight(int height);
+ virtual void setSize(int width, int height);
virtual void setSize(const Size<int>& size);
- void setSize(int width, int height)
- {
- setSize(Size<int>(width, height));
- }
-
const Rectangle<int>& getArea() const noexcept;
uint32_t getEventTimestamp();
diff --git a/dgl/src/Geometry.cpp b/dgl/src/Geometry.cpp
@@ -179,6 +179,20 @@ void Size<T>::setHeight(const T& height) noexcept
}
template<typename T>
+void Size<T>::setSize(const T& width, const T& height) noexcept
+{
+ fWidth = width;
+ fHeight = height;
+}
+
+template<typename T>
+void Size<T>::setSize(const Size<T>& size) noexcept
+{
+ fWidth = size.fWidth;
+ fHeight = size.fHeight;
+}
+
+template<typename T>
void Size<T>::growBy(const T& multiplier) noexcept
{
fWidth *= multiplier;
@@ -326,30 +340,6 @@ const Size<T>& Rectangle<T>::getSize() const noexcept
}
template<typename T>
-bool Rectangle<T>::contains(const T& x, const T& y) const noexcept
-{
- return (x >= fPos.fX && y >= fPos.fY && x <= fPos.fX+fSize.fWidth && y <= fPos.fY+fSize.fHeight);
-}
-
-template<typename T>
-bool Rectangle<T>::contains(const Point<T>& pos) const noexcept
-{
- return contains(pos.fX, pos.fY);
-}
-
-template<typename T>
-bool Rectangle<T>::containsX(const T& x) const noexcept
-{
- return (x >= fPos.fX && x <= fPos.fX + fSize.fWidth);
-}
-
-template<typename T>
-bool Rectangle<T>::containsY(const T& y) const noexcept
-{
- return (y >= fPos.fY && y <= fPos.fY + fSize.fHeight);
-}
-
-template<typename T>
void Rectangle<T>::setX(const T& x) noexcept
{
fPos.fX = x;
@@ -375,14 +365,14 @@ void Rectangle<T>::setPos(const Point<T>& pos) noexcept
}
template<typename T>
-void Rectangle<T>::move(const T& x, const T& y) noexcept
+void Rectangle<T>::moveBy(const T& x, const T& y) noexcept
{
fPos.fX += x;
fPos.fY += y;
}
template<typename T>
-void Rectangle<T>::move(const Point<T>& pos) noexcept
+void Rectangle<T>::moveBy(const Point<T>& pos) noexcept
{
fPos += pos;
}
@@ -413,6 +403,44 @@ void Rectangle<T>::setSize(const Size<T>& size) noexcept
}
template<typename T>
+void Rectangle<T>::growBy(const T& multiplier) noexcept
+{
+ fSize.fWidth *= multiplier;
+ fSize.fHeight *= multiplier;
+}
+
+template<typename T>
+void Rectangle<T>::shrinkBy(const T& divider) noexcept
+{
+ fSize.fWidth /= divider;
+ fSize.fHeight /= divider;
+}
+
+template<typename T>
+bool Rectangle<T>::contains(const T& x, const T& y) const noexcept
+{
+ return (x >= fPos.fX && y >= fPos.fY && x <= fPos.fX+fSize.fWidth && y <= fPos.fY+fSize.fHeight);
+}
+
+template<typename T>
+bool Rectangle<T>::contains(const Point<T>& pos) const noexcept
+{
+ return contains(pos.fX, pos.fY);
+}
+
+template<typename T>
+bool Rectangle<T>::containsX(const T& x) const noexcept
+{
+ return (x >= fPos.fX && x <= fPos.fX + fSize.fWidth);
+}
+
+template<typename T>
+bool Rectangle<T>::containsY(const T& y) const noexcept
+{
+ return (y >= fPos.fY && y <= fPos.fY + fSize.fHeight);
+}
+
+template<typename T>
void Rectangle<T>::draw()
{
// TODO - use glVexter2 d/f/i/s according to T type
@@ -444,6 +472,34 @@ Rectangle<T>& Rectangle<T>::operator=(const Rectangle<T>& rect) noexcept
return *this;
}
+template<typename T>
+Rectangle<T>& Rectangle<T>::operator*=(const T& m) noexcept
+{
+ fSize.fWidth *= m;
+ fSize.fHeight *= m;
+ return *this;
+}
+
+template<typename T>
+Rectangle<T>& Rectangle<T>::operator/=(const T& d) noexcept
+{
+ fSize.fWidth /= d;
+ fSize.fHeight /= d;
+ return *this;
+}
+
+template<typename T>
+bool Rectangle<T>::operator==(const Rectangle<T>& rect) const noexcept
+{
+ return (fPos == rect.fPos && fSize == rect.fSize);
+}
+
+template<typename T>
+bool Rectangle<T>::operator!=(const Rectangle<T>& rect) const noexcept
+{
+ return !operator==(rect);
+}
+
// -----------------------------------------------------------------------
// Possible template data types
diff --git a/dgl/src/Widget.cpp b/dgl/src/Widget.cpp
@@ -108,15 +108,20 @@ void Widget::setPos(const Point<int>& pos)
fParent.repaint();
}
-void Widget::move(int x, int y)
+void Widget::moveBy(int x, int y)
{
- fArea.move(x, y);
+ fArea.moveBy(x, y);
fParent.repaint();
}
-void Widget::move(const Point<int>& pos)
+void Widget::moveBy(const Point<int>& pos)
{
- fArea.move(pos);
+ Point<int> movedPos = fArea.getPos() + pos;
+
+ if (fArea.getPos() == movedPos)
+ return;
+
+ fArea.moveBy(pos);
fParent.repaint();
}
@@ -153,6 +158,11 @@ void Widget::setHeight(int height)
fParent.repaint();
}
+void Widget::setSize(int width, int height)
+{
+ setSize(Size<int>(width, height));
+}
+
void Widget::setSize(const Size<int>& size)
{
if (fArea.getSize() == size)
diff --git a/examples/color.cpp b/examples/color.cpp
@@ -110,15 +110,17 @@ private:
void onReshape(int width, int height) override
{
- // make widget same size as window
- setSize(width, height);
- Widget::onReshape(width, height);
-
// full bg
bgFull = Rectangle<int>(0, 0, width, height);
// small bg, centered 2/3 size
bgSmall = Rectangle<int>(width/6, height/6, width*2/3, height*2/3);
+
+ // make widget same size as window
+ setSize(width, height);
+
+ // default reshape implementation
+ Widget::onReshape(width, height);
}
char cur;
diff --git a/examples/images.cpp b/examples/images.cpp
@@ -126,23 +126,6 @@ private:
repaint();
}
- void setNewTopImg(const int imgId)
- {
- if (fImgTop1st == imgId)
- return;
-
- if (fImgTop2nd == imgId)
- {
- fImgTop2nd = fImgTop1st;
- fImgTop1st = imgId;
- return;
- }
-
- fImgTop3rd = fImgTop2nd;
- fImgTop2nd = fImgTop1st;
- fImgTop1st = imgId;
- }
-
void onDisplay() override
{
switch (fImgTop3rd)
@@ -185,6 +168,23 @@ private:
};
}
+ void setNewTopImg(const int imgId)
+ {
+ if (fImgTop1st == imgId)
+ return;
+
+ if (fImgTop2nd == imgId)
+ {
+ fImgTop2nd = fImgTop1st;
+ fImgTop1st = imgId;
+ return;
+ }
+
+ fImgTop3rd = fImgTop2nd;
+ fImgTop2nd = fImgTop1st;
+ fImgTop1st = imgId;
+ }
+
int fImgTop1st, fImgTop2nd, fImgTop3rd;
int fImg1x, fImg2x, fImg3y;
bool fImg1rev, fImg2rev, fImg3rev;