From: Thomas Geymayer Date: Thu, 13 Dec 2012 22:37:21 +0000 (+0100) Subject: Rename simgear::Rect to SGRect and make interface more similar to SGBox X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=50688e1159c42347202f3588dae399d7cc3fbd68;p=simgear.git Rename simgear::Rect to SGRect and make interface more similar to SGBox --- diff --git a/simgear/canvas/elements/CanvasImage.cxx b/simgear/canvas/elements/CanvasImage.cxx index ad538dd4..cf9f12f2 100644 --- a/simgear/canvas/elements/CanvasImage.cxx +++ b/simgear/canvas/elements/CanvasImage.cxx @@ -78,7 +78,9 @@ namespace canvas Element* parent ): Element(canvas, node, parent_style, parent), _texture(new osg::Texture2D), - _node_src_rect( node->getNode("source", 0, true) ) + _node_src_rect( node->getNode("source", 0, true) ), + _src_rect(0,0), + _region(0,0) { _geom = new osg::Geometry; _geom->setUseDisplayList(false); @@ -147,7 +149,7 @@ namespace canvas if( !_node_src_rect->getBoolValue("normalized", true) ) { - const Rect& tex_dim = getTextureDimensions(); + const SGRect& tex_dim = getTextureDimensions(); u0 /= tex_dim.width(); u1 /= tex_dim.width(); @@ -216,7 +218,7 @@ namespace canvas } //---------------------------------------------------------------------------- - const Rect& Image::getRegion() const + const SGRect& Image::getRegion() const { return _region; } @@ -317,7 +319,7 @@ namespace canvas { if( !_src_rect.width() || !_src_rect.height() ) { - const Rect& tex_dim = getTextureDimensions(); + const SGRect& tex_dim = getTextureDimensions(); _node_src_rect->setBoolValue("normalized", false); _node_src_rect->setFloatValue("right", tex_dim.width()); @@ -332,16 +334,16 @@ namespace canvas } //---------------------------------------------------------------------------- - Rect Image::getTextureDimensions() const + SGRect Image::getTextureDimensions() const { osg::Texture2D *texture = !_src_canvas.expired() ? _src_canvas.lock()->getTexture() : _texture.get(); if( !texture ) - return Rect(); + return SGRect(); - return Rect + return SGRect ( 0,0, texture->getTextureWidth(), diff --git a/simgear/canvas/elements/CanvasImage.hxx b/simgear/canvas/elements/CanvasImage.hxx index f72ed6f1..8d66fca1 100644 --- a/simgear/canvas/elements/CanvasImage.hxx +++ b/simgear/canvas/elements/CanvasImage.hxx @@ -22,7 +22,7 @@ #include "CanvasElement.hxx" #include -#include +#include #include namespace simgear @@ -55,7 +55,7 @@ namespace canvas void setImage(osg::Image *img); void setFill(const std::string& fill); - const Rect& getRegion() const; + const SGRect& getRegion() const; protected: @@ -68,7 +68,7 @@ namespace canvas virtual void childChanged(SGPropertyNode * child); void setupDefaultDimensions(); - Rect getTextureDimensions() const; + SGRect getTextureDimensions() const; osg::ref_ptr _texture; // TODO optionally forward events to canvas @@ -80,7 +80,7 @@ namespace canvas osg::ref_ptr _colors; SGPropertyNode *_node_src_rect; - Rect _src_rect, + SGRect _src_rect, _region; }; diff --git a/simgear/math/CMakeLists.txt b/simgear/math/CMakeLists.txt index 1308f1f4..0ebe7fd0 100644 --- a/simgear/math/CMakeLists.txt +++ b/simgear/math/CMakeLists.txt @@ -2,7 +2,6 @@ include (SimGearComponent) set(HEADERS - Rect.hxx SGBox.hxx SGCMath.hxx SGGeoc.hxx @@ -21,6 +20,7 @@ set(HEADERS SGPlane.hxx SGQuat.hxx SGRay.hxx + SGRect.hxx SGSphere.hxx SGTriangle.hxx SGVec2.hxx diff --git a/simgear/math/Rect.hxx b/simgear/math/Rect.hxx deleted file mode 100644 index d45d4a0f..00000000 --- a/simgear/math/Rect.hxx +++ /dev/null @@ -1,89 +0,0 @@ -// Class representing a rectangular region -// -// Copyright (C) 2012 Thomas Geymayer -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library General Public -// License as published by the Free Software Foundation; either -// version 2 of the License, or (at your option) any later version. -// -// This library 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 -// Library General Public License for more details. -// -// You should have received a copy of the GNU Library General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - -#ifndef SG_RECT_HXX_ -#define SG_RECT_HXX_ - -namespace simgear -{ - - template - class Rect - { - public: - Rect(): - _x1(0), - _x2(0), - _y1(0), - _y2(0) - {} - - Rect(T x, T y, T w, T h): - _x1(x), - _x2(x + w), - _y1(y), - _y2(y + h) - {} - - void set(T x, T y, T w, T h) - { - _x1 = x; - _x2 = x + w; - _y1 = y; - _y2 = y + h; - } - - T x() const { return _x1; } - T y() const { return _y1; } - T width() const { return _x2 - _x1; } - T height() const { return _y2 - _y1; } - - void setX(T x) { T w = width(); _x1 = x; _x2 = x + w; } - void setY(T y) { T h = height(); _y1 = y; _y2 = y + h; } - void setWidth(T w) { _x2 = _x1 + w; } - void setHeight(T h) { _y2 = _y1 + h; } - - T l() const { return _x1; } - T r() const { return _x2; } - T t() const { return _y1; } - T b() const { return _y2; } - - void setLeft(T l) { _x1 = l; } - void setRight(T r) { _x2 = r; } - void setTop(T t) { _y1 = t; } - void setBottom(T b) { _y2 = b; } - - bool contains(T x, T y) const - { - return _x1 <= x && x <= _x2 - && _y1 <= y && y <= _y2; - } - - bool contains(T x, T y, T margin) const - { - return (_x1 - margin) <= x && x <= (_x2 + margin) - && (_y1 - margin) <= y && y <= (_y2 + margin); - } - - private: - T _x1, _x2, _y1, _y2; - }; - -} // namespace simgear - -#endif /* SG_RECT_HXX_ */ diff --git a/simgear/math/SGRect.hxx b/simgear/math/SGRect.hxx new file mode 100644 index 00000000..20976a0c --- /dev/null +++ b/simgear/math/SGRect.hxx @@ -0,0 +1,130 @@ +// Class representing a rectangular region +// +// Copyright (C) 2012 Thomas Geymayer +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + +#ifndef SG_RECT_HXX_ +#define SG_RECT_HXX_ + +#include "SGLimits.hxx" +#include "SGVec2.hxx" + +template +class SGRect +{ + public: + + SGRect(): + _min(SGLimits::max(), SGLimits::max()), + _max(-SGLimits::max(), -SGLimits::max()) + { + + } + + SGRect(const SGVec2& pt): + _min(pt), + _max(pt) + { + + } + + SGRect(T x, T y): + _min(x, y), + _max(x, y) + { + + } + + SGRect(const SGVec2& min, const SGVec2& max): + _min(min), + _max(max) + { + + } + + SGRect(T x, T y, T w, T h): + _min(x, y), + _max(x + w, y + h) + { + + } + + template + explicit SGRect(const SGRect& rect): + _min(rect.getMin()), + _max(rect.getMax()) + { + + } + + void setMin(const SGVec2& min) { _min = min; } + const SGVec2& getMin() const { return _min; } + + void setMax(const SGVec2& max) { _max = max; } + const SGVec2& getMax() const { return _max; } + + void set(T x, T y, T w, T h) + { + _min.x() = x; + _min.y() = y; + _max.x() = x + w; + _max.y() = y + h; + } + + T x() const { return _min.x(); } + T y() const { return _min.y(); } + T width() const { return _max.x() - _min.x(); } + T height() const { return _max.y() - _min.y(); } + + void setX(T x) { T w = width(); _min.x() = x; _max.x() = x + w; } + void setY(T y) { T h = height(); _min.y() = y; _max.y() = y + h; } + void setWidth(T w) { _max.x() = _min.x() + w; } + void setHeight(T h) { _max.y() = _min.y() + h; } + + T l() const { return _min.x(); } + T r() const { return _max.x(); } + T t() const { return _min.y(); } + T b() const { return _max.y(); } + + void setLeft(T l) { _min.x() = l; } + void setRight(T r) { _max.x() = r; } + void setTop(T t) { _min.y() = t; } + void setBottom(T b) { _max.y() = b; } + + bool contains(T x, T y) const + { + return _min.x() <= x && x <= _max.x() + && _min.y() <= y && y <= _max.y(); + } + + bool contains(T x, T y, T margin) const + { + return (_min.x() - margin) <= x && x <= (_max.x() + margin) + && (_min.y() - margin) <= y && y <= (_max.y() + margin); + } + + private: + SGVec2 _min, + _max; +}; + +template +inline +std::basic_ostream& +operator<<(std::basic_ostream& s, const SGRect& rect) +{ return s << "min = " << rect.getMin() << ", max = " << rect.getMax(); } + +#endif /* SG_RECT_HXX_ */ diff --git a/simgear/props/propertyObject.hxx b/simgear/props/propertyObject.hxx index 6a5e1ae7..753be9f4 100644 --- a/simgear/props/propertyObject.hxx +++ b/simgear/props/propertyObject.hxx @@ -126,8 +126,9 @@ public: T operator op##=(const T rhs)\ {\ SGPropertyNode* n = getOrThrow();\ - n->setValue(n->getValue() op rhs);\ - return *this;\ + T new_val = n->getValue() op rhs;\ + n->setValue(new_val);\ + return new_val;\ } SG_DEF_ASSIGN_OP(+)