From 0d066e131a7010ebfea736c45ee1f3a770104b11 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 22 May 2018 22:55:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Rect=E6=8A=BD=E8=B1=A1?= =?UTF-8?q?=E7=9F=A9=E5=BD=A2=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Common/Point.cpp | 7 ++- core/Common/Rect.cpp | 59 +++++++++++++++++++ core/Common/Size.cpp | 7 ++- core/Common/String.cpp | 12 ++-- core/Node/Button.cpp | 10 ++-- core/Node/Node.cpp | 4 +- core/e2dcommon.h | 83 +++++++++++++++++++++++---- core/e2dnode.h | 10 ++-- project/vs2017/Easy2D.vcxproj | 1 + project/vs2017/Easy2D.vcxproj.filters | 3 + 10 files changed, 164 insertions(+), 32 deletions(-) create mode 100644 core/Common/Rect.cpp diff --git a/core/Common/Point.cpp b/core/Common/Point.cpp index f78246ce..1a05f43b 100644 --- a/core/Common/Point.cpp +++ b/core/Common/Point.cpp @@ -41,4 +41,9 @@ e2d::Point::operator e2d::Size() const e2d::Point e2d::Point::operator-() const { return Point(-x, -y); -} \ No newline at end of file +} + +bool e2d::Point::operator==(const Point & point) const +{ + return (x == point.x) && (y == point.y); +} diff --git a/core/Common/Rect.cpp b/core/Common/Rect.cpp new file mode 100644 index 00000000..f2e7a4ae --- /dev/null +++ b/core/Common/Rect.cpp @@ -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); +} diff --git a/core/Common/Size.cpp b/core/Common/Size.cpp index e626418d..c7ae6e69 100644 --- a/core/Common/Size.cpp +++ b/core/Common/Size.cpp @@ -40,4 +40,9 @@ e2d::Size::operator e2d::Point() const e2d::Size e2d::Size::operator-() const { return Size(-width, -height); -} \ No newline at end of file +} + +bool e2d::Size::operator==(const Size & size) const +{ + return (width == size.width) && (height == size.height); +} diff --git a/core/Common/String.cpp b/core/Common/String.cpp index 8ac9f374..53bd6d55 100644 --- a/core/Common/String.cpp +++ b/core/Common/String.cpp @@ -129,7 +129,7 @@ e2d::String & e2d::String::operator=(const String &str) return (*this); } -bool e2d::String::operator==(const wchar_t *str) +bool e2d::String::operator==(const wchar_t *str) const { 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) { @@ -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; } -bool e2d::String::operator!=(const wchar_t *str) +bool e2d::String::operator!=(const wchar_t *str) const { 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) { @@ -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; } diff --git a/core/Node/Button.cpp b/core/Node/Button.cpp index 297a3786..914eafa9 100644 --- a/core/Node/Button.cpp +++ b/core/Node/Button.cpp @@ -188,7 +188,7 @@ void e2d::Button::_fixedUpdate() { // 鼠标左键抬起时,判断鼠标坐标是否在按钮内部 if (_isSelected && - _normal->isPointIn(Input::getMousePos())) + _normal->containsPoint(Input::getMousePos())) { _runCallback(); } @@ -198,7 +198,7 @@ void e2d::Button::_fixedUpdate() if (Input::isPress(Input::Mouse::LEFT)) { - if (_normal->isPointIn(Input::getMousePos())) + if (_normal->containsPoint(Input::getMousePos())) { // 鼠标左键按下,且位于按钮内时,标记 _isSelected 为 true _isSelected = true; @@ -208,14 +208,14 @@ void e2d::Button::_fixedUpdate() if (_isSelected && Input::isDown(Input::Mouse::LEFT)) { - if (_normal->isPointIn(Input::getMousePos())) + if (_normal->containsPoint(Input::getMousePos())) { _setState(ButtonState::SELECTED); Window::setCursor(Window::Cursor::HAND); return; } } - else if (_normal->isPointIn(Input::getMousePos())) + else if (_normal->containsPoint(Input::getMousePos())) { _setState(ButtonState::MOUSEOVER); Window::setCursor(Window::Cursor::HAND); @@ -225,7 +225,7 @@ void e2d::Button::_fixedUpdate() _setState(ButtonState::NORMAL); } - if (_visiable && !_enable && _normal && _normal->isPointIn(Input::getMousePos())) + if (_visiable && !_enable && _normal && _normal->containsPoint(Input::getMousePos())) { Window::setCursor(Window::Cursor::NO); } diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index fff9430a..5c501059 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -825,7 +825,7 @@ e2d::Action * e2d::Node::getAction(const String& name) return nullptr; } -bool e2d::Node::isPointIn(Point point) const +bool e2d::Node::containsPoint(const Point& point) const { BOOL ret = 0; // 如果存在碰撞体,用碰撞体判断 @@ -867,7 +867,7 @@ bool e2d::Node::isPointIn(Point point) const return false; } -bool e2d::Node::isOverlappedWith(const Node * node) const +bool e2d::Node::intersects(Node * node) const { // 如果存在碰撞体,用碰撞体判断 if (this->_collider && node->_collider) diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 15942404..de7de2c1 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -23,6 +23,7 @@ enum class LineJoin : int ROUND = 2 /* 圆角 */ }; + class Size; // 坐标 @@ -37,18 +38,21 @@ public: Point(double x, double y); - Point operator + (Point const & p) const; - Point operator - (Point const & p) const; - Point operator * (double const & value) const; - Point operator / (double const & value) const; + Point operator + (Point const & point) const; + Point operator - (Point const & point) const; + Point operator * (double const & point) const; + Point operator / (double const & point) const; Point operator - () const; + bool operator== (const Point& point) const; operator e2d::Size() const; }; + // 二维向量 typedef Point Vector; + // 大小 class Size { @@ -63,14 +67,69 @@ public: Size operator + (Size const & size) const; Size operator - (Size const & size) const; - Size operator * (double const & value) const; - Size operator / (double const & value) const; + Size operator * (double const & size) const; + Size operator / (double const & size) const; Size operator - () const; + bool operator== (const Size& size) 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 { @@ -186,12 +245,12 @@ public: operator wchar_t* () const; // 比较运算符 - bool operator== (const String &); - bool operator== (const char *); - bool operator== (const wchar_t *); - bool operator!= (const String &); - bool operator!= (const char *); - bool operator!= (const wchar_t *); + bool operator== (const String &) const; + bool operator== (const char *) const; + bool operator== (const wchar_t *) const; + bool operator!= (const String &) const; + bool operator!= (const char *) const; + bool operator!= (const wchar_t *) const; bool operator> (const String &) const; bool operator>= (const String &) const; bool operator< (const String &) const; diff --git a/core/e2dnode.h b/core/e2dnode.h index 3ac70948..40088992 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -52,13 +52,13 @@ public: virtual bool isVisiable() const; // 判断点是否在节点内 - virtual bool isPointIn( - Point point + virtual bool containsPoint( + const Point& point ) const; - // 判断两节点是否重叠 - virtual bool isOverlappedWith( - const Node * node + // 判断两物体是否相交 + virtual bool intersects( + Node * node ) const; // 获取节点名称 diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index c33e5e67..d4a49414 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -231,6 +231,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 1a5b1a18..8a9b77fd 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -234,6 +234,9 @@ Node + + Common +