增加Rect抽象矩形类

This commit is contained in:
Nomango 2018-05-22 22:55:06 +08:00
parent d8ea376ceb
commit 0d066e131a
10 changed files with 164 additions and 32 deletions

View File

@ -41,4 +41,9 @@ e2d::Point::operator e2d::Size() const
e2d::Point e2d::Point::operator-() const e2d::Point e2d::Point::operator-() const
{ {
return Point(-x, -y); return Point(-x, -y);
} }
bool e2d::Point::operator==(const Point & point) const
{
return (x == point.x) && (y == point.y);
}

59
core/Common/Rect.cpp Normal file
View File

@ -0,0 +1,59 @@
#include "..\e2dcommon.h"
e2d::Rect::Rect(void)
{
setRect(0.0, 0.0, 0.0, 0.0);
}
e2d::Rect::Rect(double x, double y, double width, double height)
{
setRect(x, y, width, height);
}
e2d::Rect::Rect(const Point& pos, const Size& size)
{
setRect(pos.x, pos.y, size.width, size.height);
}
e2d::Rect::Rect(const Rect& other)
{
setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
}
e2d::Rect& e2d::Rect::operator= (const Rect& other)
{
setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
return *this;
}
bool e2d::Rect::operator==(const Rect & rect) const
{
return (origin == rect.origin) && (size == rect.size);
}
void e2d::Rect::setRect(double x, double y, double width, double height)
{
origin.x = x;
origin.y = y;
size.width = width;
size.height = height;
}
bool e2d::Rect::containsPoint(const Point& point) const
{
if (point.x >= origin.x && point.x <= (origin.y + size.height)
&& point.y >= origin.y && point.y <= (origin.y + size.height))
{
return true;
}
return false;
}
bool e2d::Rect::intersects(const Rect& rect) const
{
return !((origin.x + size.width) < rect.origin.x ||
(rect.origin.x + rect.size.width) < origin.x ||
(origin.y + size.height) < rect.origin.y ||
(rect.origin.y + rect.size.height) < origin.y);
}

View File

@ -40,4 +40,9 @@ e2d::Size::operator e2d::Point() const
e2d::Size e2d::Size::operator-() const e2d::Size e2d::Size::operator-() const
{ {
return Size(-width, -height); return Size(-width, -height);
} }
bool e2d::Size::operator==(const Size & size) const
{
return (width == size.width) && (height == size.height);
}

View File

@ -129,7 +129,7 @@ e2d::String & e2d::String::operator=(const String &str)
return (*this); return (*this);
} }
bool e2d::String::operator==(const wchar_t *str) bool e2d::String::operator==(const wchar_t *str) const
{ {
if (str) if (str)
{ {
@ -141,7 +141,7 @@ bool e2d::String::operator==(const wchar_t *str)
} }
} }
bool e2d::String::operator==(const char *str) bool e2d::String::operator==(const char *str) const
{ {
if (str) if (str)
{ {
@ -154,12 +154,12 @@ bool e2d::String::operator==(const char *str)
} }
} }
bool e2d::String::operator ==(const e2d::String &str) bool e2d::String::operator ==(const e2d::String &str) const
{ {
return _str == str._str; return _str == str._str;
} }
bool e2d::String::operator!=(const wchar_t *str) bool e2d::String::operator!=(const wchar_t *str) const
{ {
if (str) if (str)
{ {
@ -171,7 +171,7 @@ bool e2d::String::operator!=(const wchar_t *str)
} }
} }
bool e2d::String::operator!=(const char *str) bool e2d::String::operator!=(const char *str) const
{ {
if (str) if (str)
{ {
@ -184,7 +184,7 @@ bool e2d::String::operator!=(const char *str)
} }
} }
bool e2d::String::operator!=(const e2d::String &str) bool e2d::String::operator!=(const e2d::String &str) const
{ {
return _str != str._str; return _str != str._str;
} }

View File

@ -188,7 +188,7 @@ void e2d::Button::_fixedUpdate()
{ {
// 鼠标左键抬起时,判断鼠标坐标是否在按钮内部 // 鼠标左键抬起时,判断鼠标坐标是否在按钮内部
if (_isSelected && if (_isSelected &&
_normal->isPointIn(Input::getMousePos())) _normal->containsPoint(Input::getMousePos()))
{ {
_runCallback(); _runCallback();
} }
@ -198,7 +198,7 @@ void e2d::Button::_fixedUpdate()
if (Input::isPress(Input::Mouse::LEFT)) if (Input::isPress(Input::Mouse::LEFT))
{ {
if (_normal->isPointIn(Input::getMousePos())) if (_normal->containsPoint(Input::getMousePos()))
{ {
// 鼠标左键按下,且位于按钮内时,标记 _isSelected 为 true // 鼠标左键按下,且位于按钮内时,标记 _isSelected 为 true
_isSelected = true; _isSelected = true;
@ -208,14 +208,14 @@ void e2d::Button::_fixedUpdate()
if (_isSelected && Input::isDown(Input::Mouse::LEFT)) if (_isSelected && Input::isDown(Input::Mouse::LEFT))
{ {
if (_normal->isPointIn(Input::getMousePos())) if (_normal->containsPoint(Input::getMousePos()))
{ {
_setState(ButtonState::SELECTED); _setState(ButtonState::SELECTED);
Window::setCursor(Window::Cursor::HAND); Window::setCursor(Window::Cursor::HAND);
return; return;
} }
} }
else if (_normal->isPointIn(Input::getMousePos())) else if (_normal->containsPoint(Input::getMousePos()))
{ {
_setState(ButtonState::MOUSEOVER); _setState(ButtonState::MOUSEOVER);
Window::setCursor(Window::Cursor::HAND); Window::setCursor(Window::Cursor::HAND);
@ -225,7 +225,7 @@ void e2d::Button::_fixedUpdate()
_setState(ButtonState::NORMAL); _setState(ButtonState::NORMAL);
} }
if (_visiable && !_enable && _normal && _normal->isPointIn(Input::getMousePos())) if (_visiable && !_enable && _normal && _normal->containsPoint(Input::getMousePos()))
{ {
Window::setCursor(Window::Cursor::NO); Window::setCursor(Window::Cursor::NO);
} }

View File

@ -825,7 +825,7 @@ e2d::Action * e2d::Node::getAction(const String& name)
return nullptr; return nullptr;
} }
bool e2d::Node::isPointIn(Point point) const bool e2d::Node::containsPoint(const Point& point) const
{ {
BOOL ret = 0; BOOL ret = 0;
// 如果存在碰撞体,用碰撞体判断 // 如果存在碰撞体,用碰撞体判断
@ -867,7 +867,7 @@ bool e2d::Node::isPointIn(Point point) const
return false; return false;
} }
bool e2d::Node::isOverlappedWith(const Node * node) const bool e2d::Node::intersects(Node * node) const
{ {
// 如果存在碰撞体,用碰撞体判断 // 如果存在碰撞体,用碰撞体判断
if (this->_collider && node->_collider) if (this->_collider && node->_collider)

View File

@ -23,6 +23,7 @@ enum class LineJoin : int
ROUND = 2 /* 圆角 */ ROUND = 2 /* 圆角 */
}; };
class Size; class Size;
// 坐标 // 坐标
@ -37,18 +38,21 @@ public:
Point(double x, double y); Point(double x, double y);
Point operator + (Point const & p) const; Point operator + (Point const & point) const;
Point operator - (Point const & p) const; Point operator - (Point const & point) const;
Point operator * (double const & value) const; Point operator * (double const & point) const;
Point operator / (double const & value) const; Point operator / (double const & point) const;
Point operator - () const; Point operator - () const;
bool operator== (const Point& point) const;
operator e2d::Size() const; operator e2d::Size() const;
}; };
// 二维向量 // 二维向量
typedef Point Vector; typedef Point Vector;
// 大小 // 大小
class Size class Size
{ {
@ -63,14 +67,69 @@ public:
Size operator + (Size const & size) const; Size operator + (Size const & size) const;
Size operator - (Size const & size) const; Size operator - (Size const & size) const;
Size operator * (double const & value) const; Size operator * (double const & size) const;
Size operator / (double const & value) const; Size operator / (double const & size) const;
Size operator - () const; Size operator - () const;
bool operator== (const Size& size) const;
operator e2d::Point() const; operator e2d::Point() const;
}; };
// 矩形
class Rect
{
public:
Point origin; // 原点坐标
Size size; // 宽度和高度
public:
Rect();
Rect(
double x,
double y,
double width,
double height
);
Rect(
const Point& pos,
const Size& size
);
Rect(
const Rect& other
);
Rect& operator= (
const Rect& other
);
bool operator== (
const Rect& rect
) const;
// 设置矩形
void setRect(
double x,
double y,
double width,
double height
);
// 判断点是否在矩形内
bool containsPoint(
const Point& point
) const;
// 判断两矩形是否相交
bool intersects(
const Rect& rect
) const;
};
// 字符串 // 字符串
class String class String
{ {
@ -186,12 +245,12 @@ public:
operator wchar_t* () const; operator wchar_t* () const;
// 比较运算符 // 比较运算符
bool operator== (const String &); bool operator== (const String &) const;
bool operator== (const char *); bool operator== (const char *) const;
bool operator== (const wchar_t *); bool operator== (const wchar_t *) const;
bool operator!= (const String &); bool operator!= (const String &) const;
bool operator!= (const char *); bool operator!= (const char *) const;
bool operator!= (const wchar_t *); bool operator!= (const wchar_t *) const;
bool operator> (const String &) const; bool operator> (const String &) const;
bool operator>= (const String &) const; bool operator>= (const String &) const;
bool operator< (const String &) const; bool operator< (const String &) const;

View File

@ -52,13 +52,13 @@ public:
virtual bool isVisiable() const; virtual bool isVisiable() const;
// 判断点是否在节点内 // 判断点是否在节点内
virtual bool isPointIn( virtual bool containsPoint(
Point point const Point& point
) const; ) const;
// 判断两节点是否重叠 // 判断两物体是否相交
virtual bool isOverlappedWith( virtual bool intersects(
const Node * node Node * node
) const; ) const;
// 获取节点名称 // 获取节点名称

View File

@ -231,6 +231,7 @@
<ClCompile Include="..\..\core\Common\Function.cpp" /> <ClCompile Include="..\..\core\Common\Function.cpp" />
<ClCompile Include="..\..\core\Common\Object.cpp" /> <ClCompile Include="..\..\core\Common\Object.cpp" />
<ClCompile Include="..\..\core\Common\Point.cpp" /> <ClCompile Include="..\..\core\Common\Point.cpp" />
<ClCompile Include="..\..\core\Common\Rect.cpp" />
<ClCompile Include="..\..\core\Common\Scene.cpp" /> <ClCompile Include="..\..\core\Common\Scene.cpp" />
<ClCompile Include="..\..\core\Common\Size.cpp" /> <ClCompile Include="..\..\core\Common\Size.cpp" />
<ClCompile Include="..\..\core\Common\String.cpp" /> <ClCompile Include="..\..\core\Common\String.cpp" />

View File

@ -234,6 +234,9 @@
<ClCompile Include="..\..\core\Node\ToggleButton.cpp"> <ClCompile Include="..\..\core\Node\ToggleButton.cpp">
<Filter>Node</Filter> <Filter>Node</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\core\Common\Rect.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\core\easy2d.h" /> <ClInclude Include="..\..\core\easy2d.h" />