From 3ec241f04c4850be3c39049fa6ad53653ab24c4c Mon Sep 17 00:00:00 2001 From: falkTX Date: Tue, 6 May 2014 18:58:34 +0200 Subject: [PATCH] More base changes --- dgl/Geometry.hpp | 23 ++++++--- dgl/Widget.hpp | 10 ++-- dgl/src/Geometry.cpp | 108 ++++++++++++++++++++++++++++++++----------- dgl/src/Widget.cpp | 18 ++++++-- examples/color.cpp | 10 ++-- examples/images.cpp | 34 +++++++------- 6 files changed, 138 insertions(+), 65 deletions(-) diff --git a/dgl/Geometry.hpp b/dgl/Geometry.hpp index e4495b57..c259d941 100644 --- 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& size) noexcept; void growBy(const T& multiplier) noexcept; void shrinkBy(const T& divider) noexcept; @@ -110,27 +112,34 @@ public: const Point& getPos() const noexcept; const Size& getSize() const noexcept; - bool contains(const T& x, const T& y) const noexcept; - bool contains(const Point& 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& pos) noexcept; - void move(const T& x, const T& y) noexcept; - void move(const Point& pos) noexcept; + void moveBy(const T& x, const T& y) noexcept; + void moveBy(const Point& 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& 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& pos) const noexcept; + bool containsX(const T& x) const noexcept; + bool containsY(const T& y) const noexcept; + void draw(); Rectangle& operator=(const Rectangle& rect) noexcept; + Rectangle& operator*=(const T& m) noexcept; + Rectangle& operator/=(const T& d) noexcept; + bool operator==(const Rectangle& size) const noexcept; + bool operator!=(const Rectangle& size) const noexcept; private: Point fPos; diff --git a/dgl/Widget.hpp b/dgl/Widget.hpp index 8345621e..084687f4 100644 --- a/dgl/Widget.hpp +++ b/dgl/Widget.hpp @@ -53,8 +53,8 @@ public: void setPos(int x, int y); void setPos(const Point& pos); - void move(int x, int y); - void move(const Point& pos); + void moveBy(int x, int y); + void moveBy(const Point& 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& size); - void setSize(int width, int height) - { - setSize(Size(width, height)); - } - const Rectangle& getArea() const noexcept; uint32_t getEventTimestamp(); diff --git a/dgl/src/Geometry.cpp b/dgl/src/Geometry.cpp index 97a049e8..2529c5cd 100644 --- a/dgl/src/Geometry.cpp +++ b/dgl/src/Geometry.cpp @@ -178,6 +178,20 @@ void Size::setHeight(const T& height) noexcept fHeight = height; } +template +void Size::setSize(const T& width, const T& height) noexcept +{ + fWidth = width; + fHeight = height; +} + +template +void Size::setSize(const Size& size) noexcept +{ + fWidth = size.fWidth; + fHeight = size.fHeight; +} + template void Size::growBy(const T& multiplier) noexcept { @@ -325,30 +339,6 @@ const Size& Rectangle::getSize() const noexcept return fSize; } -template -bool Rectangle::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 -bool Rectangle::contains(const Point& pos) const noexcept -{ - return contains(pos.fX, pos.fY); -} - -template -bool Rectangle::containsX(const T& x) const noexcept -{ - return (x >= fPos.fX && x <= fPos.fX + fSize.fWidth); -} - -template -bool Rectangle::containsY(const T& y) const noexcept -{ - return (y >= fPos.fY && y <= fPos.fY + fSize.fHeight); -} - template void Rectangle::setX(const T& x) noexcept { @@ -375,14 +365,14 @@ void Rectangle::setPos(const Point& pos) noexcept } template -void Rectangle::move(const T& x, const T& y) noexcept +void Rectangle::moveBy(const T& x, const T& y) noexcept { fPos.fX += x; fPos.fY += y; } template -void Rectangle::move(const Point& pos) noexcept +void Rectangle::moveBy(const Point& pos) noexcept { fPos += pos; } @@ -412,6 +402,44 @@ void Rectangle::setSize(const Size& size) noexcept fSize = size; } +template +void Rectangle::growBy(const T& multiplier) noexcept +{ + fSize.fWidth *= multiplier; + fSize.fHeight *= multiplier; +} + +template +void Rectangle::shrinkBy(const T& divider) noexcept +{ + fSize.fWidth /= divider; + fSize.fHeight /= divider; +} + +template +bool Rectangle::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 +bool Rectangle::contains(const Point& pos) const noexcept +{ + return contains(pos.fX, pos.fY); +} + +template +bool Rectangle::containsX(const T& x) const noexcept +{ + return (x >= fPos.fX && x <= fPos.fX + fSize.fWidth); +} + +template +bool Rectangle::containsY(const T& y) const noexcept +{ + return (y >= fPos.fY && y <= fPos.fY + fSize.fHeight); +} + template void Rectangle::draw() { @@ -444,6 +472,34 @@ Rectangle& Rectangle::operator=(const Rectangle& rect) noexcept return *this; } +template +Rectangle& Rectangle::operator*=(const T& m) noexcept +{ + fSize.fWidth *= m; + fSize.fHeight *= m; + return *this; +} + +template +Rectangle& Rectangle::operator/=(const T& d) noexcept +{ + fSize.fWidth /= d; + fSize.fHeight /= d; + return *this; +} + +template +bool Rectangle::operator==(const Rectangle& rect) const noexcept +{ + return (fPos == rect.fPos && fSize == rect.fSize); +} + +template +bool Rectangle::operator!=(const Rectangle& rect) const noexcept +{ + return !operator==(rect); +} + // ----------------------------------------------------------------------- // Possible template data types diff --git a/dgl/src/Widget.cpp b/dgl/src/Widget.cpp index 0aa46f7a..da4fb139 100644 --- a/dgl/src/Widget.cpp +++ b/dgl/src/Widget.cpp @@ -108,15 +108,20 @@ void Widget::setPos(const Point& 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& pos) +void Widget::moveBy(const Point& pos) { - fArea.move(pos); + Point 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(width, height)); +} + void Widget::setSize(const Size& size) { if (fArea.getSize() == size) diff --git a/examples/color.cpp b/examples/color.cpp index e0dbba9b..b110e71d 100644 --- 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(0, 0, width, height); // small bg, centered 2/3 size bgSmall = Rectangle(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 index 8e8201d0..b113abb0 100644 --- 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;