MyGUI_TRect.h
Go to the documentation of this file.
/*
* This source file is part of MyGUI. For the latest info, see http://mygui.info/
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#ifndef MYGUI_TRECT_H_
#define MYGUI_TRECT_H_
#include "MyGUI_Prerequest.h"
#include "MyGUI_StringUtility.h"
namespace MyGUI::types
{
template<typename T>
struct TRect
{
T left{};
T top{};
T right{};
T bottom{};
TRect() = default;
TRect(T const& _left, T const& _top, T const& _right, T const& _bottom) :
left(_left),
top(_top),
right(_right),
bottom(_bottom)
{
}
TRect& operator-=(TRect const& _obj)
{
left -= _obj.left;
top -= _obj.top;
right -= _obj.right;
bottom -= _obj.bottom;
return *this;
}
TRect& operator+=(TRect const& _obj)
{
left += _obj.left;
top += _obj.top;
right += _obj.right;
bottom += _obj.bottom;
return *this;
}
TRect operator-(TRect const& _obj) const
{
return TRect(left - _obj.left, top - _obj.top, right - _obj.right, bottom - _obj.bottom);
}
TRect operator+(TRect const& _obj) const
{
return TRect(left + _obj.left, top + _obj.top, right + _obj.right, bottom + _obj.bottom);
}
template<typename U>
TRect& operator=(TRect<U> const& _obj)
{
left = _obj.left;
top = _obj.top;
right = _obj.right;
bottom = _obj.bottom;
return *this;
}
bool operator==(TRect const& _obj) const
{
return ((left == _obj.left) && (top == _obj.top) && (right == _obj.right) && (bottom == _obj.bottom));
}
bool operator!=(TRect const& _obj) const
{
return !((left == _obj.left) && (top == _obj.top) && (right == _obj.right) && (bottom == _obj.bottom));
}
T width() const
{
return right - left;
}
T height() const
{
return bottom - top;
}
void clear()
{
left = top = right = bottom = 0;
}
void set(T const& _left, T const& _top, T const& _right, T const& _bottom)
{
left = _left;
top = _top;
right = _right;
bottom = _bottom;
}
void swap(TRect& _value)
{
TRect tmp = _value;
_value = *this;
*this = tmp;
}
bool empty() const
{
return ((left == 0) && (top == 0) && (right == 0) && (bottom == 0));
}
bool inside(const TRect<T>& _value) const
{
return (
(_value.left >= left) && (_value.right <= right) && (_value.top >= top) && (_value.bottom <= bottom));
}
bool intersect(const TRect<T>& _value) const
{
return (
(_value.left <= right) && (_value.right >= left) && (_value.top <= bottom) && (_value.bottom >= top));
}
bool inside(const TPoint<T>& _value) const
{
return ((_value.left >= left) && (_value.left <= right) && (_value.top >= top) && (_value.top <= bottom));
}
std::string print() const
{
std::ostringstream stream;
stream << *this;
return stream.str();
}
static TRect<T> parse(std::string_view _value)
{
return utility::parseValue<TRect<T>>(_value);
}
friend std::ostream& operator<<(std::ostream& _stream, const TRect<T>& _value)
{
_stream << _value.left << " " << _value.top << " " << _value.right << " " << _value.bottom;
return _stream;
}
friend std::istream& operator>>(std::istream& _stream, TRect<T>& _value)
{
_stream >> _value.left >> _value.top >> _value.right >> _value.bottom;
if (_stream.fail())
_value.clear();
return _stream;
}
};
} // namespace MyGUI
#endif // MYGUI_TRECT_H_
MyGUI_Prerequest.h
MyGUI_StringUtility.h
MyGUI::types
Definition MyGUI_TCoord.h:15
MyGUI::utility::parseValue
T parseValue(std::string_view _value)
Definition MyGUI_StringUtility.h:60
MyGUI::types::TPoint
Definition MyGUI_TPoint.h:18
MyGUI::types::TPoint::left
T left
Definition MyGUI_TPoint.h:19
MyGUI::types::TPoint::top
T top
Definition MyGUI_TPoint.h:20
MyGUI::types::TRect::inside
bool inside(const TPoint< T > &_value) const
Definition MyGUI_TRect.h:130
MyGUI::types::TRect< int >::right
int right
Definition MyGUI_TRect.h:21
MyGUI::types::TRect::print
std::string print() const
Definition MyGUI_TRect.h:135
MyGUI::types::TRect::set
void set(T const &_left, T const &_top, T const &_right, T const &_bottom)
Definition MyGUI_TRect.h:98
MyGUI::types::TRect::operator!=
bool operator!=(TRect const &_obj) const
Definition MyGUI_TRect.h:78
MyGUI::types::TRect::operator-=
TRect & operator-=(TRect const &_obj)
Definition MyGUI_TRect.h:35
MyGUI::types::TRect::height
T height() const
Definition MyGUI_TRect.h:88
MyGUI::types::TRect::swap
void swap(TRect &_value)
Definition MyGUI_TRect.h:106
MyGUI::types::TRect::TRect
TRect(T const &_left, T const &_top, T const &_right, T const &_bottom)
Definition MyGUI_TRect.h:26
MyGUI::types::TRect::empty
bool empty() const
Definition MyGUI_TRect.h:113
MyGUI::types::TRect::inside
bool inside(const TRect< T > &_value) const
Definition MyGUI_TRect.h:118
MyGUI::types::TRect::operator+
TRect operator+(TRect const &_obj) const
Definition MyGUI_TRect.h:58
MyGUI::types::TRect::operator==
bool operator==(TRect const &_obj) const
Definition MyGUI_TRect.h:73
MyGUI::types::TRect::clear
void clear()
Definition MyGUI_TRect.h:93
MyGUI::types::TRect::operator>>
friend std::istream & operator>>(std::istream &_stream, TRect< T > &_value)
Definition MyGUI_TRect.h:153
MyGUI::types::TRect< int >::left
int left
Definition MyGUI_TRect.h:19
MyGUI::types::TRect::intersect
bool intersect(const TRect< T > &_value) const
Definition MyGUI_TRect.h:124
MyGUI::types::TRect::operator-
TRect operator-(TRect const &_obj) const
Definition MyGUI_TRect.h:53
MyGUI::types::TRect::operator<<
friend std::ostream & operator<<(std::ostream &_stream, const TRect< T > &_value)
Definition MyGUI_TRect.h:147
MyGUI::types::TRect< int >::bottom
int bottom
Definition MyGUI_TRect.h:22
MyGUI::types::TRect::width
T width() const
Definition MyGUI_TRect.h:83
MyGUI::types::TRect::TRect
TRect()=default
MyGUI::types::TRect< int >::top
int top
Definition MyGUI_TRect.h:20
MyGUI::types::TRect::operator+=
TRect & operator+=(TRect const &_obj)
Definition MyGUI_TRect.h:44
MyGUI::types::TRect::operator=
TRect & operator=(TRect< U > const &_obj)
Definition MyGUI_TRect.h:64
MyGUI::types::TRect::parse
static TRect< T > parse(std::string_view _value)
Definition MyGUI_TRect.h:142