From e78f8795e744638640dda3ba4a7dd9bdd897ba3e Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Sat, 31 Mar 2018 11:59:51 +0800 Subject: [PATCH] update Shape Now you can directly set the shape of nodes by Shape::TYPE. The subclasses of Shape are prefixed with 'Shape'. --- core/Node/Node.cpp | 32 +++- core/Shape/{Circle.cpp => ShapeCircle.cpp} | 20 +-- core/Shape/{Ellipse.cpp => ShapeEllipse.cpp} | 20 +-- core/Shape/{Rect.cpp => ShapeRectangle.cpp} | 20 +-- core/ecommon.h | 98 +++++++++++ core/enodes.h | 9 +- core/eshape.h | 161 +++++-------------- project/vs2017/Easy2D.vcxproj | 6 +- project/vs2017/Easy2D.vcxproj.filters | 18 +-- 9 files changed, 217 insertions(+), 167 deletions(-) rename core/Shape/{Circle.cpp => ShapeCircle.cpp} (69%) rename core/Shape/{Ellipse.cpp => ShapeEllipse.cpp} (64%) rename core/Shape/{Rect.cpp => ShapeRectangle.cpp} (58%) diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index 9b7faedb..007be718 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -39,7 +39,7 @@ e2d::Node::Node() { if (s_fDefaultShapeEnabled) { - auto rect = new Rect(this); + auto rect = new ShapeRectangle(this); this->setShape(rect); } } @@ -542,6 +542,36 @@ void e2d::Node::setSize(Size size) this->setSize(size.width, size.height); } +void e2d::Node::setShape(Shape::TYPE type) +{ + switch (type) + { + case Shape::TYPE::RECTANGLE: + { + auto rect = new ShapeRectangle(this); + this->setShape(rect); + break; + } + + case Shape::TYPE::CIRCLE: + { + auto rect = new ShapeCircle(this); + this->setShape(rect); + break; + } + + case Shape::TYPE::ELLIPSE: + { + auto rect = new ShapeEllipse(this); + this->setShape(rect); + break; + } + + default: + break; + } +} + void e2d::Node::setShape(Shape * pShape) { // 删除旧的形状 diff --git a/core/Shape/Circle.cpp b/core/Shape/ShapeCircle.cpp similarity index 69% rename from core/Shape/Circle.cpp rename to core/Shape/ShapeCircle.cpp index 82f61348..e4ed7ba3 100644 --- a/core/Shape/Circle.cpp +++ b/core/Shape/ShapeCircle.cpp @@ -1,22 +1,22 @@ #include "..\eshape.h" #include "..\enodes.h" -e2d::Circle::Circle() +e2d::ShapeCircle::ShapeCircle() : m_pD2dCircle(nullptr) { } -e2d::Circle::Circle(Point center, double radius) +e2d::ShapeCircle::ShapeCircle(Point center, double radius) : m_pD2dCircle(nullptr) { - this->_setCircle(center, radius); + this->setCircle(center, radius); } -e2d::Circle::Circle(Node * node) +e2d::ShapeCircle::ShapeCircle(Node * node) : m_pD2dCircle(nullptr) { double minSide = min(node->getRealWidth(), node->getRealHeight()); - this->_setCircle( + this->setCircle( Point( node->getRealWidth() / 2, node->getRealHeight() / 2 @@ -25,12 +25,12 @@ e2d::Circle::Circle(Node * node) ); } -e2d::Circle::~Circle() +e2d::ShapeCircle::~ShapeCircle() { SafeReleaseInterface(&m_pD2dCircle); } -void e2d::Circle::_setCircle(Point center, double radius) +void e2d::ShapeCircle::setCircle(Point center, double radius) { SafeReleaseInterface(&m_pD2dCircle); @@ -45,12 +45,12 @@ void e2d::Circle::_setCircle(Point center, double radius) ); } -void e2d::Circle::_resize() +void e2d::ShapeCircle::_resize() { if (m_pParentNode && m_bEnable) { double minSide = min(m_pParentNode->getRealWidth(), m_pParentNode->getRealHeight()); - this->_setCircle( + this->setCircle( Point( m_pParentNode->getRealWidth() / 2, m_pParentNode->getRealHeight() / 2 @@ -60,7 +60,7 @@ void e2d::Circle::_resize() } } -ID2D1EllipseGeometry * e2d::Circle::getD2dGeometry() const +ID2D1EllipseGeometry * e2d::ShapeCircle::getD2dGeometry() const { return m_pD2dCircle; } diff --git a/core/Shape/Ellipse.cpp b/core/Shape/ShapeEllipse.cpp similarity index 64% rename from core/Shape/Ellipse.cpp rename to core/Shape/ShapeEllipse.cpp index bc389750..2d129678 100644 --- a/core/Shape/Ellipse.cpp +++ b/core/Shape/ShapeEllipse.cpp @@ -1,21 +1,21 @@ #include "..\eshape.h" #include "..\enodes.h" -e2d::Ellipse::Ellipse() +e2d::ShapeEllipse::ShapeEllipse() : m_pD2dEllipse(nullptr) { } -e2d::Ellipse::Ellipse(Point center, double radiusX, double radiusY) +e2d::ShapeEllipse::ShapeEllipse(Point center, double radiusX, double radiusY) : m_pD2dEllipse(nullptr) { - this->_setEllipse(center, radiusX, radiusY); + this->setEllipse(center, radiusX, radiusY); } -e2d::Ellipse::Ellipse(Node * node) +e2d::ShapeEllipse::ShapeEllipse(Node * node) : m_pD2dEllipse(nullptr) { - this->_setEllipse( + this->setEllipse( Point( node->getWidth() / 2, node->getHeight() / 2 @@ -25,12 +25,12 @@ e2d::Ellipse::Ellipse(Node * node) ); } -e2d::Ellipse::~Ellipse() +e2d::ShapeEllipse::~ShapeEllipse() { SafeReleaseInterface(&m_pD2dEllipse); } -void e2d::Ellipse::_setEllipse(Point center, double radiusX, double radiusY) +void e2d::ShapeEllipse::setEllipse(Point center, double radiusX, double radiusY) { SafeReleaseInterface(&m_pD2dEllipse); @@ -45,11 +45,11 @@ void e2d::Ellipse::_setEllipse(Point center, double radiusX, double radiusY) ); } -void e2d::Ellipse::_resize() +void e2d::ShapeEllipse::_resize() { if (m_pParentNode && m_bEnable) { - this->_setEllipse( + this->setEllipse( Point( m_pParentNode->getWidth() / 2, m_pParentNode->getHeight() / 2 @@ -60,7 +60,7 @@ void e2d::Ellipse::_resize() } } -ID2D1EllipseGeometry * e2d::Ellipse::getD2dGeometry() const +ID2D1EllipseGeometry * e2d::ShapeEllipse::getD2dGeometry() const { return m_pD2dEllipse; } diff --git a/core/Shape/Rect.cpp b/core/Shape/ShapeRectangle.cpp similarity index 58% rename from core/Shape/Rect.cpp rename to core/Shape/ShapeRectangle.cpp index b90144bf..1d66c391 100644 --- a/core/Shape/Rect.cpp +++ b/core/Shape/ShapeRectangle.cpp @@ -1,21 +1,21 @@ #include "..\eshape.h" #include "..\enodes.h" -e2d::Rect::Rect() +e2d::ShapeRectangle::ShapeRectangle() : m_pD2dRectangle(nullptr) { } -e2d::Rect::Rect(double x, double y, double width, double height) +e2d::ShapeRectangle::ShapeRectangle(double x, double y, double width, double height) : m_pD2dRectangle(nullptr) { - this->_setRect(x, y, x + width, y + height); + this->setRect(x, y, x + width, y + height); } -e2d::Rect::Rect(Node * node) +e2d::ShapeRectangle::ShapeRectangle(Node * node) : m_pD2dRectangle(nullptr) { - this->_setRect( + this->setRect( 0, 0, node->getRealWidth(), @@ -23,12 +23,12 @@ e2d::Rect::Rect(Node * node) ); } -e2d::Rect::~Rect() +e2d::ShapeRectangle::~ShapeRectangle() { SafeReleaseInterface(&m_pD2dRectangle); } -void e2d::Rect::_setRect(double left, double top, double right, double bottom) +void e2d::ShapeRectangle::setRect(double left, double top, double right, double bottom) { SafeReleaseInterface(&m_pD2dRectangle); @@ -42,11 +42,11 @@ void e2d::Rect::_setRect(double left, double top, double right, double bottom) ); } -void e2d::Rect::_resize() +void e2d::ShapeRectangle::_resize() { if (m_pParentNode && m_bEnable) { - this->_setRect( + this->setRect( 0, 0, m_pParentNode->getRealWidth(), @@ -55,7 +55,7 @@ void e2d::Rect::_resize() } } -ID2D1RectangleGeometry * e2d::Rect::getD2dGeometry() const +ID2D1RectangleGeometry * e2d::ShapeRectangle::getD2dGeometry() const { return m_pD2dRectangle; } diff --git a/core/ecommon.h b/core/ecommon.h index 42724f82..7230e3e3 100644 --- a/core/ecommon.h +++ b/core/ecommon.h @@ -648,6 +648,104 @@ protected: }; +class ShapeManager; + +// 形状 +class Shape : + public Object +{ + friend ShapeManager; + friend Node; + +public: + // 形状类别 + enum TYPE + { + RECTANGLE, /* 矩形 */ + CIRCLE, /* 圆形 */ + ELLIPSE /* 椭圆形 */ + }; + +public: + Shape(); + + virtual ~Shape(); + + // 判断两形状的交集关系 + virtual int getRelationWith( + Shape * pShape + ) const; + + // 获取父节点 + Node * getParentNode() const; + + // 获取类别掩码 + UINT32 getCategoryBitmask() const; + + // 获取冲突掩码 + UINT32 getCollisionBitmask() const; + + // 设置类别掩码 + void setCategoryBitmask( + UINT32 mask + ); + + // 设置冲突掩码 + void setCollisionBitmask( + UINT32 mask + ); + + // 启用或关闭该形状 + virtual void setEnable( + bool bEnable + ); + + // 设置形状的可见性 + void setVisiable( + bool bVisiable + ); + + // 设置绘制颜色 + void setColor( + UINT32 color + ); + + // 设置绘制透明度 + void setOpacity( + double opacity + ); + + // 设置大小跟随 + void setAutoResize( + bool bEnable + ); + + // 获取 ID2D1Geometry 对象 + virtual ID2D1Geometry * getD2dGeometry() const = 0; + +protected: + // 转换形状 + virtual void _transform(); + + // 重设大小 + virtual void _resize() = 0; + + // 渲染形状 + virtual void _render(); + +protected: + bool m_bEnable; + bool m_bIsVisiable; + bool m_bAutoResize; + UINT32 m_nCategoryBitmask; + UINT32 m_nCollisionBitmask; + UINT32 m_nColor; + float m_fOpacity; + Node * m_pParentNode; + ID2D1TransformedGeometry * m_pTransformedShape; +}; + + // String 类模板函数定义 template inline e2d::String e2d::String::toString(T value) diff --git a/core/enodes.h b/core/enodes.h index da901cf6..f67d8077 100644 --- a/core/enodes.h +++ b/core/enodes.h @@ -6,7 +6,6 @@ namespace e2d class Action; -class Shape; class Transition; class Node : @@ -307,6 +306,11 @@ public: Size size ); + // 设置节点形状 + virtual void setShape( + Shape::TYPE type + ); + // 设置节点形状 virtual void setShape( Shape * pShape @@ -426,7 +430,7 @@ protected: bool m_bDisplayedInScene; bool m_bSortChildrenNeeded; bool m_bTransformNeeded; - Shape * m_pShape; + Shape * m_pShape; Scene * m_pParentScene; Node * m_pParent; D2D1::Matrix3x2F m_MatriInitial; @@ -704,6 +708,7 @@ public: virtual void onFixedUpdate() override; protected: + // 按钮状态枚举 enum BTN_STATE { NORMAL, MOUSEOVER, SELECTED }; // 设置按钮状态 diff --git a/core/eshape.h b/core/eshape.h index 42a0d4ff..9f9e4bd2 100644 --- a/core/eshape.h +++ b/core/eshape.h @@ -5,105 +5,17 @@ namespace e2d { -class ShapeManager; -class Node; - -class Shape : - public Object -{ - friend ShapeManager; - friend Node; - -public: - Shape(); - - virtual ~Shape(); - - // 判断两形状的交集关系 - virtual int getRelationWith( - Shape * pShape - ) const; - - // 获取父节点 - Node * getParentNode() const; - - // 获取类别掩码 - UINT32 getCategoryBitmask() const; - - // 获取冲突掩码 - UINT32 getCollisionBitmask() const; - - // 设置类别掩码 - void setCategoryBitmask( - UINT32 mask - ); - - // 设置冲突掩码 - void setCollisionBitmask( - UINT32 mask - ); - - // 启用或关闭该形状 - virtual void setEnable( - bool bEnable - ); - - // 设置形状的可见性 - void setVisiable( - bool bVisiable - ); - - // 设置绘制颜色 - void setColor( - UINT32 color - ); - - // 设置绘制透明度 - void setOpacity( - double opacity - ); - - // 设置大小跟随 - void setAutoResize( - bool bEnable - ); - - // 获取 ID2D1Geometry 对象 - virtual ID2D1Geometry * getD2dGeometry() const = 0; - -protected: - // 转换形状 - virtual void _transform(); - - // 重设大小 - virtual void _resize() = 0; - - // 渲染形状 - virtual void _render(); - -protected: - bool m_bEnable; - bool m_bIsVisiable; - bool m_bAutoResize; - UINT32 m_nCategoryBitmask; - UINT32 m_nCollisionBitmask; - UINT32 m_nColor; - float m_fOpacity; - Node * m_pParentNode; - ID2D1TransformedGeometry * m_pTransformedShape; -}; - - -class Rect : +// 矩形 +class ShapeRectangle : public Shape { public: - // 创建一个空矩形 - Rect(); + // 创建一个默认矩形 + ShapeRectangle(); // 根据左上角坐标和宽高创建矩形 - Rect( + ShapeRectangle( double x, double y, double width, @@ -111,23 +23,24 @@ public: ); // 创建一个和节点位置大小相同的矩形 - Rect( + ShapeRectangle( Node * node ); - virtual ~Rect(); + virtual ~ShapeRectangle(); - // 获取 ID2D1Geometry 对象 - virtual ID2D1RectangleGeometry * getD2dGeometry() const override; - -protected: - void _setRect( + // 修改矩形大小 + void setRect( double left, double top, double right, double bottom ); + // 获取 ID2D1Geometry 对象 + virtual ID2D1RectangleGeometry * getD2dGeometry() const override; + +protected: // 重设大小 virtual void _resize(); @@ -136,35 +49,37 @@ protected: }; -class Circle : +// 圆形 +class ShapeCircle : public Shape { public: - // 创建一个空的圆形 - Circle(); + // 创建一个默认圆形 + ShapeCircle(); // 根据圆心和半径创建圆形 - Circle( + ShapeCircle( Point center, double radius ); // 创建一个和节点位置大小相同的圆形 - Circle( + ShapeCircle( Node * node ); - virtual ~Circle(); + virtual ~ShapeCircle(); + + // 修改圆形大小 + void setCircle( + Point center, + double radius + ); // 获取 ID2D1Geometry 对象 virtual ID2D1EllipseGeometry * getD2dGeometry() const override; protected: - void _setCircle( - Point center, - double radius - ); - // 重设大小 virtual void _resize(); @@ -173,37 +88,39 @@ protected: }; -class Ellipse : +// 椭圆形 +class ShapeEllipse : public Shape { public: - // 创建一个空的椭圆 - Ellipse(); + // 创建一个默认椭圆 + ShapeEllipse(); // 根据圆心和半径创建椭圆 - Ellipse( + ShapeEllipse( Point center, double radiusX, double radiusY ); // 创建一个和节点位置大小相同的椭圆 - Ellipse( + ShapeEllipse( Node * node ); - virtual ~Ellipse(); + virtual ~ShapeEllipse(); - // 获取 ID2D1Geometry 对象 - virtual ID2D1EllipseGeometry * getD2dGeometry() const override; - -protected: - void _setEllipse( + // 修改椭圆大小 + void setEllipse( Point center, double radiusX, double radiusY ); + // 获取 ID2D1Geometry 对象 + virtual ID2D1EllipseGeometry * getD2dGeometry() const override; + +protected: // 重设大小 virtual void _resize(); diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index a8f939c4..0daf09c2 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -231,9 +231,9 @@ - - - + + + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 653e91be..4910b170 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -150,18 +150,9 @@ Manager - - Shape - - - Shape - Shape - - Shape - Common @@ -189,6 +180,15 @@ Tool + + Shape + + + Shape + + + Shape +