Rename simgear::Rect to SGRect and make interface more similar to SGBox

This commit is contained in:
Thomas Geymayer
2012-12-13 23:37:21 +01:00
parent 8417fea3bf
commit 50688e1159
6 changed files with 147 additions and 103 deletions

View File

@@ -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<int>& tex_dim = getTextureDimensions();
const SGRect<int>& tex_dim = getTextureDimensions();
u0 /= tex_dim.width();
u1 /= tex_dim.width();
@@ -216,7 +218,7 @@ namespace canvas
}
//----------------------------------------------------------------------------
const Rect<float>& Image::getRegion() const
const SGRect<float>& Image::getRegion() const
{
return _region;
}
@@ -317,7 +319,7 @@ namespace canvas
{
if( !_src_rect.width() || !_src_rect.height() )
{
const Rect<int>& tex_dim = getTextureDimensions();
const SGRect<int>& tex_dim = getTextureDimensions();
_node_src_rect->setBoolValue("normalized", false);
_node_src_rect->setFloatValue("right", tex_dim.width());
@@ -332,16 +334,16 @@ namespace canvas
}
//----------------------------------------------------------------------------
Rect<int> Image::getTextureDimensions() const
SGRect<int> Image::getTextureDimensions() const
{
osg::Texture2D *texture = !_src_canvas.expired()
? _src_canvas.lock()->getTexture()
: _texture.get();
if( !texture )
return Rect<int>();
return SGRect<int>();
return Rect<int>
return SGRect<int>
(
0,0,
texture->getTextureWidth(),

View File

@@ -22,7 +22,7 @@
#include "CanvasElement.hxx"
#include <simgear/canvas/canvas_fwd.hxx>
#include <simgear/math/Rect.hxx>
#include <simgear/math/SGRect.hxx>
#include <osg/Texture2D>
namespace simgear
@@ -55,7 +55,7 @@ namespace canvas
void setImage(osg::Image *img);
void setFill(const std::string& fill);
const Rect<float>& getRegion() const;
const SGRect<float>& getRegion() const;
protected:
@@ -68,7 +68,7 @@ namespace canvas
virtual void childChanged(SGPropertyNode * child);
void setupDefaultDimensions();
Rect<int> getTextureDimensions() const;
SGRect<int> getTextureDimensions() const;
osg::ref_ptr<osg::Texture2D> _texture;
// TODO optionally forward events to canvas
@@ -80,7 +80,7 @@ namespace canvas
osg::ref_ptr<osg::Vec4Array> _colors;
SGPropertyNode *_node_src_rect;
Rect<float> _src_rect,
SGRect<float> _src_rect,
_region;
};

View File

@@ -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

View File

@@ -1,89 +0,0 @@
// Class representing a rectangular region
//
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
//
// 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<typename T>
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_ */

130
simgear/math/SGRect.hxx Normal file
View File

@@ -0,0 +1,130 @@
// Class representing a rectangular region
//
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
//
// 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<typename T>
class SGRect
{
public:
SGRect():
_min(SGLimits<T>::max(), SGLimits<T>::max()),
_max(-SGLimits<T>::max(), -SGLimits<T>::max())
{
}
SGRect(const SGVec2<T>& pt):
_min(pt),
_max(pt)
{
}
SGRect(T x, T y):
_min(x, y),
_max(x, y)
{
}
SGRect(const SGVec2<T>& min, const SGVec2<T>& max):
_min(min),
_max(max)
{
}
SGRect(T x, T y, T w, T h):
_min(x, y),
_max(x + w, y + h)
{
}
template<typename S>
explicit SGRect(const SGRect<S>& rect):
_min(rect.getMin()),
_max(rect.getMax())
{
}
void setMin(const SGVec2<T>& min) { _min = min; }
const SGVec2<T>& getMin() const { return _min; }
void setMax(const SGVec2<T>& max) { _max = max; }
const SGVec2<T>& 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<T> _min,
_max;
};
template<typename char_type, typename traits_type, typename T>
inline
std::basic_ostream<char_type, traits_type>&
operator<<(std::basic_ostream<char_type, traits_type>& s, const SGRect<T>& rect)
{ return s << "min = " << rect.getMin() << ", max = " << rect.getMax(); }
#endif /* SG_RECT_HXX_ */

View File

@@ -126,8 +126,9 @@ public:
T operator op##=(const T rhs)\
{\
SGPropertyNode* n = getOrThrow();\
n->setValue<T>(n->getValue<T>() op rhs);\
return *this;\
T new_val = n->getValue<T>() op rhs;\
n->setValue<T>(new_val);\
return new_val;\
}
SG_DEF_ASSIGN_OP(+)