/* ----------------------------------------------------------------------------- GSFramework Copyright 2001-2013 Emmanuel Julien. All Rights Reserved. ----------------------------------------------------------------------------- */ #ifndef __NRECT__ #define __NRECT__ #include "ntypes.h" namespace GS { namespace NML { class File; class Tag; } /*! @short Point. @author Emmanuel Julien (ejulien@nworks.fr) */ template struct Point { T x, y; T operator [] (size_t n) const { return *(&x + n); } void Set(T _x, T _y) { x = _x; y = _y; } Point(T ux, T uy) : x(ux), y(uy) {} Point() : x(0), y(0) {} }; typedef Point iPoint; typedef Point fPoint; /*! @short Rectangle. @author Emmanuel Julien (ejulien@nworks.fr) */ template struct Rect { T sx, sy, ex, ey; void SetWidth(T w) { ex = sx + w; } void SetHeight(T h) { ey = sy + h; } T GetWidth() const { return ex - sx; } T GetHeight() const { return ey - sy; } Rect operator * (T v) const { return Rect (sx * v, sy * v, ex * v, ey * v); } Rect operator / (T v) const { return Rect (sx / v, sy / v, ex / v, ey / v); } bool Inside(T x, T y) const { return (x > sx) && (y > sy) && (x < ex) && (y < ey); } bool FitsInside(const Rect &b) const { return (GetWidth() <= b.GetWidth()) && (GetHeight() <= b.GetHeight()); } bool Intersect(const Rect &b) const { return ((ex < b.sx) || (ey < b.sy) || (sx > b.ex) || (sy > b.ey)) ? false : true; } Rect Intersection(const Rect &b) const { T _sx = Types::Max(sx, b.sx), _sy = Types::Max(sy, b.sy), _ex = Types::Min(ex, b.ex), _ey = Types::Min(ey, b.ey); T n_sx = Types::Min(_sx, _ex), n_sy = Types::Min(_sy, _ey), n_ex = Types::Max(_sx, _ex), n_ey = Types::Max(_sy, _ey); return Rect (_sx = n_sx, _sy = n_sy, _ex = n_ex, _ey = n_ey); } Rect Grow(T border) const { return Rect (sx - border, sy - border, ex + border, ey + border); } void Set(T usx, T usy, T uex, T uey) { sx = usx; sy = usy; ex = uex; ey = uey; } void Set(T ux = 0, T uy = 0) { sx = ux; sy = uy; ex = ux; ey = uy; } Rect Offset(T x, T y) const { return Rect (sx + x, sy + y, ex + x, ey + y); } Rect AsFloat() const { return Rect (float(sx), float(sy), float(ex), float(ey)); } Rect AsInt() const { return Rect (int(sx), int(sy), int(ex), int(ey)); } NML::Tag *AsMetaTag(const char *id) const; bool FromMetaTag(NML::Tag &); static Rect FromWidthHeight(T sx, T sy, T w, T h) { return Rect (sx, sy, sx + w, sy + h); } Rect(const Rect &b) : sx(b.sx), sy(b.sy), ex(b.ex), ey(b.ey) {} Rect(T usx, T usy, T uex, T uey) : sx(usx), sy(usy), ex(uex), ey(uey) {} Rect(T usx, T usy) : sx(usx), ex(usx), sy(usy), ey(usy) {} Rect() : sx(0), sy(0), ex(0), ey(0) {} }; typedef Rect iRect; typedef Rect fRect; } // GS #endif // __NRECT__