From cddfd5b5cdbbd57b3f49351f6eda853e95635a6e Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Wed, 7 Mar 2018 20:14:58 +0800 Subject: [PATCH] Node has a default shape now. --- core/Manager/ShapeManager.cpp | 5 +++++ core/Node/Node.cpp | 28 ++++++++++++++++++---------- core/Shape/Circle.cpp | 15 +++++++++++++++ core/Shape/Ellipse.cpp | 15 +++++++++++++++ core/Shape/Rect.cpp | 13 +++++++++++++ core/Shape/Shape.cpp | 11 +++++++++++ core/enodes.h | 7 ++++++- core/eshape.h | 20 +++++++++++++++++++- 8 files changed, 102 insertions(+), 12 deletions(-) diff --git a/core/Manager/ShapeManager.cpp b/core/Manager/ShapeManager.cpp index 26bfe511..70bc1f35 100644 --- a/core/Manager/ShapeManager.cpp +++ b/core/Manager/ShapeManager.cpp @@ -45,6 +45,11 @@ void e2d::ShapeManager::__addShape(Shape * pShape) { if (pShape) { + if (pShape->m_pParentNode) + { + WARN_IF(true, "ShapeManager::__addShape Failed! The shape is already added."); + return; + } pShape->retain(); s_vShapes.push_back(pShape); } diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index ae0e0199..bdc854ab 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -8,6 +8,7 @@ // 默认中心点位置 static float s_fDefaultPiovtX = 0; static float s_fDefaultPiovtY = 0; +static bool s_fDefaultShapeEnabled = true; e2d::Node::Node() : m_nOrder(0) @@ -36,6 +37,11 @@ e2d::Node::Node() , m_bTransformNeeded(false) , m_bAutoUpdate(true) { + if (s_fDefaultShapeEnabled) + { + auto rect = new Rect(this); + this->setShape(rect); + } } e2d::Node::~Node() @@ -825,27 +831,24 @@ std::vector e2d::Node::getActions(const String & strActionName) return std::move(actions); } -bool e2d::Node::isPointIn(Point point) +bool e2d::Node::isPointIn(Point point) const { - if (m_bTransformNeeded) - { - _updateTransform(this); - } // 为节点创建一个形状 ID2D1RectangleGeometry * rect; Renderer::getID2D1Factory()->CreateRectangleGeometry( - D2D1::RectF(0, 0, m_fWidth * m_fScaleX, m_fHeight * m_fScaleY), + D2D1::RectF(0, 0, m_fWidth, m_fHeight), &rect ); // 判断点是否在形状内 - BOOL ret; + BOOL ret = 0; rect->FillContainsPoint( D2D1::Point2F( static_cast(point.x), static_cast(point.y)), - &m_MatriFinal, + m_MatriFinal, &ret ); + if (ret) { return true; @@ -868,7 +871,7 @@ bool e2d::Node::isIntersectWith(Node * pNode) const // 根据自身大小位置创建矩形 Renderer::getID2D1Factory()->CreateRectangleGeometry( - D2D1::RectF(0, 0, m_fWidth * m_fScaleX, m_fHeight * m_fScaleY), + D2D1::RectF(0, 0, m_fWidth, m_fHeight), &pRect1 ); // 根据二维矩阵进行转换 @@ -879,7 +882,7 @@ bool e2d::Node::isIntersectWith(Node * pNode) const ); // 根据相比较节点的大小位置创建矩形 Renderer::getID2D1Factory()->CreateRectangleGeometry( - D2D1::RectF(0, 0, pNode->m_fWidth * pNode->m_fScaleX, pNode->m_fHeight * pNode->m_fScaleY), + D2D1::RectF(0, 0, pNode->m_fWidth, pNode->m_fHeight), &pRect2 ); // 获取相交状态 @@ -903,6 +906,11 @@ void e2d::Node::setDefaultPiovt(double defaultPiovtX, double defaultPiovtY) s_fDefaultPiovtY = min(max(static_cast(defaultPiovtY), 0), 1); } +void e2d::Node::setDefaultShapeEnable(bool bEnable) +{ + s_fDefaultShapeEnabled = bEnable; +} + void e2d::Node::resumeAllActions() { ActionManager::resumeAllActionsBindedWith(this); diff --git a/core/Shape/Circle.cpp b/core/Shape/Circle.cpp index 89fd9433..56138179 100644 --- a/core/Shape/Circle.cpp +++ b/core/Shape/Circle.cpp @@ -45,6 +45,21 @@ void e2d::Circle::_setCircle(Point center, double radius) ); } +void e2d::Circle::_resize() +{ + if (m_pParentNode && m_bEnable) + { + double minSide = min(m_pParentNode->getRealWidth(), m_pParentNode->getRealHeight()); + this->_setCircle( + Point( + m_pParentNode->getRealWidth() / 2, + m_pParentNode->getRealHeight() / 2 + ), + minSide / 2 + ); + } +} + ID2D1EllipseGeometry * e2d::Circle::_getD2dGeometry() const { return m_pD2dCircle; diff --git a/core/Shape/Ellipse.cpp b/core/Shape/Ellipse.cpp index 6b8ec12d..6bf14fd2 100644 --- a/core/Shape/Ellipse.cpp +++ b/core/Shape/Ellipse.cpp @@ -45,6 +45,21 @@ void e2d::Ellipse::_setEllipse(Point center, double radiusX, double radiusY) ); } +void e2d::Ellipse::_resize() +{ + if (m_pParentNode && m_bEnable) + { + this->_setEllipse( + Point( + m_pParentNode->getWidth() / 2, + m_pParentNode->getHeight() / 2 + ), + m_pParentNode->getWidth() / 2, + m_pParentNode->getHeight() / 2 + ); + } +} + ID2D1EllipseGeometry * e2d::Ellipse::_getD2dGeometry() const { return m_pD2dEllipse; diff --git a/core/Shape/Rect.cpp b/core/Shape/Rect.cpp index 5190cc26..7cfebafa 100644 --- a/core/Shape/Rect.cpp +++ b/core/Shape/Rect.cpp @@ -42,6 +42,19 @@ void e2d::Rect::_setRect(double left, double top, double right, double bottom) ); } +void e2d::Rect::_resize() +{ + if (m_pParentNode && m_bEnable) + { + this->_setRect( + 0, + 0, + m_pParentNode->getRealWidth(), + m_pParentNode->getRealHeight() + ); + } +} + ID2D1RectangleGeometry * e2d::Rect::_getD2dGeometry() const { return m_pD2dRectangle; diff --git a/core/Shape/Shape.cpp b/core/Shape/Shape.cpp index 6163e28f..beaf915c 100644 --- a/core/Shape/Shape.cpp +++ b/core/Shape/Shape.cpp @@ -11,6 +11,7 @@ e2d::Shape::Shape() , m_pParentNode(nullptr) , m_pTransformedShape(nullptr) , m_bEnable(true) + , m_bAutoResize(true) { } @@ -64,6 +65,11 @@ void e2d::Shape::setOpacity(double opacity) m_fOpacity = min(max(static_cast(opacity), 0), 1); } +void e2d::Shape::setAutoResize(bool bEnable) +{ + m_bAutoResize = bEnable; +} + void e2d::Shape::_render() { if (m_pTransformedShape && m_bEnable) @@ -105,6 +111,11 @@ void e2d::Shape::_transform() { if (m_pParentNode && m_bEnable) { + if (m_bAutoResize) + { + this->_resize(); + } + // 释放原形状 SafeReleaseInterface(&m_pTransformedShape); diff --git a/core/enodes.h b/core/enodes.h index 7a8d929c..3f7ae8cd 100644 --- a/core/enodes.h +++ b/core/enodes.h @@ -48,7 +48,7 @@ public: // 判断点是否在节点内 virtual bool isPointIn( Point point - ); + ) const; // 判断两节点是否相交 virtual bool isIntersectWith( @@ -368,6 +368,11 @@ public: double defaultPiovtY ); + // 设置节点是否包含默认形状(默认打开) + static void setDefaultShapeEnable( + bool bEnable + ); + protected: // 更新节点 void _update(); diff --git a/core/eshape.h b/core/eshape.h index 03378239..10dfcd56 100644 --- a/core/eshape.h +++ b/core/eshape.h @@ -64,10 +64,18 @@ public: double opacity ); + // 设置大小跟随 + void setAutoResize( + bool bEnable + ); + protected: // 转换形状 virtual void _transform(); + // 重设大小 + virtual void _resize() = 0; + // 渲染形状 virtual void _render(); @@ -77,11 +85,12 @@ protected: 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; + Node * m_pParentNode; ID2D1TransformedGeometry * m_pTransformedShape; }; @@ -116,6 +125,9 @@ protected: double bottom ); + // 重设大小 + virtual void _resize(); + virtual ID2D1RectangleGeometry * _getD2dGeometry() const override; protected: @@ -149,6 +161,9 @@ protected: double radius ); + // 重设大小 + virtual void _resize(); + virtual ID2D1EllipseGeometry * _getD2dGeometry() const override; protected: @@ -184,6 +199,9 @@ protected: double radiusY ); + // 重设大小 + virtual void _resize(); + virtual ID2D1EllipseGeometry * _getD2dGeometry() const override; protected: