Node has a default shape now.

This commit is contained in:
Nomango 2018-03-07 20:14:58 +08:00
parent b583ad562f
commit cddfd5b5cd
8 changed files with 102 additions and 12 deletions

View File

@ -45,6 +45,11 @@ void e2d::ShapeManager::__addShape(Shape * pShape)
{ {
if (pShape) if (pShape)
{ {
if (pShape->m_pParentNode)
{
WARN_IF(true, "ShapeManager::__addShape Failed! The shape is already added.");
return;
}
pShape->retain(); pShape->retain();
s_vShapes.push_back(pShape); s_vShapes.push_back(pShape);
} }

View File

@ -8,6 +8,7 @@
// 默认中心点位置 // 默认中心点位置
static float s_fDefaultPiovtX = 0; static float s_fDefaultPiovtX = 0;
static float s_fDefaultPiovtY = 0; static float s_fDefaultPiovtY = 0;
static bool s_fDefaultShapeEnabled = true;
e2d::Node::Node() e2d::Node::Node()
: m_nOrder(0) : m_nOrder(0)
@ -36,6 +37,11 @@ e2d::Node::Node()
, m_bTransformNeeded(false) , m_bTransformNeeded(false)
, m_bAutoUpdate(true) , m_bAutoUpdate(true)
{ {
if (s_fDefaultShapeEnabled)
{
auto rect = new Rect(this);
this->setShape(rect);
}
} }
e2d::Node::~Node() e2d::Node::~Node()
@ -825,27 +831,24 @@ std::vector<e2d::Action*> e2d::Node::getActions(const String & strActionName)
return std::move(actions); return std::move(actions);
} }
bool e2d::Node::isPointIn(Point point) bool e2d::Node::isPointIn(Point point) const
{ {
if (m_bTransformNeeded)
{
_updateTransform(this);
}
// 为节点创建一个形状 // 为节点创建一个形状
ID2D1RectangleGeometry * rect; ID2D1RectangleGeometry * rect;
Renderer::getID2D1Factory()->CreateRectangleGeometry( Renderer::getID2D1Factory()->CreateRectangleGeometry(
D2D1::RectF(0, 0, m_fWidth * m_fScaleX, m_fHeight * m_fScaleY), D2D1::RectF(0, 0, m_fWidth, m_fHeight),
&rect &rect
); );
// 判断点是否在形状内 // 判断点是否在形状内
BOOL ret; BOOL ret = 0;
rect->FillContainsPoint( rect->FillContainsPoint(
D2D1::Point2F( D2D1::Point2F(
static_cast<float>(point.x), static_cast<float>(point.x),
static_cast<float>(point.y)), static_cast<float>(point.y)),
&m_MatriFinal, m_MatriFinal,
&ret &ret
); );
if (ret) if (ret)
{ {
return true; return true;
@ -868,7 +871,7 @@ bool e2d::Node::isIntersectWith(Node * pNode) const
// 根据自身大小位置创建矩形 // 根据自身大小位置创建矩形
Renderer::getID2D1Factory()->CreateRectangleGeometry( Renderer::getID2D1Factory()->CreateRectangleGeometry(
D2D1::RectF(0, 0, m_fWidth * m_fScaleX, m_fHeight * m_fScaleY), D2D1::RectF(0, 0, m_fWidth, m_fHeight),
&pRect1 &pRect1
); );
// 根据二维矩阵进行转换 // 根据二维矩阵进行转换
@ -879,7 +882,7 @@ bool e2d::Node::isIntersectWith(Node * pNode) const
); );
// 根据相比较节点的大小位置创建矩形 // 根据相比较节点的大小位置创建矩形
Renderer::getID2D1Factory()->CreateRectangleGeometry( 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 &pRect2
); );
// 获取相交状态 // 获取相交状态
@ -903,6 +906,11 @@ void e2d::Node::setDefaultPiovt(double defaultPiovtX, double defaultPiovtY)
s_fDefaultPiovtY = min(max(static_cast<float>(defaultPiovtY), 0), 1); s_fDefaultPiovtY = min(max(static_cast<float>(defaultPiovtY), 0), 1);
} }
void e2d::Node::setDefaultShapeEnable(bool bEnable)
{
s_fDefaultShapeEnabled = bEnable;
}
void e2d::Node::resumeAllActions() void e2d::Node::resumeAllActions()
{ {
ActionManager::resumeAllActionsBindedWith(this); ActionManager::resumeAllActionsBindedWith(this);

View File

@ -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 ID2D1EllipseGeometry * e2d::Circle::_getD2dGeometry() const
{ {
return m_pD2dCircle; return m_pD2dCircle;

View File

@ -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 ID2D1EllipseGeometry * e2d::Ellipse::_getD2dGeometry() const
{ {
return m_pD2dEllipse; return m_pD2dEllipse;

View File

@ -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 ID2D1RectangleGeometry * e2d::Rect::_getD2dGeometry() const
{ {
return m_pD2dRectangle; return m_pD2dRectangle;

View File

@ -11,6 +11,7 @@ e2d::Shape::Shape()
, m_pParentNode(nullptr) , m_pParentNode(nullptr)
, m_pTransformedShape(nullptr) , m_pTransformedShape(nullptr)
, m_bEnable(true) , m_bEnable(true)
, m_bAutoResize(true)
{ {
} }
@ -64,6 +65,11 @@ void e2d::Shape::setOpacity(double opacity)
m_fOpacity = min(max(static_cast<float>(opacity), 0), 1); m_fOpacity = min(max(static_cast<float>(opacity), 0), 1);
} }
void e2d::Shape::setAutoResize(bool bEnable)
{
m_bAutoResize = bEnable;
}
void e2d::Shape::_render() void e2d::Shape::_render()
{ {
if (m_pTransformedShape && m_bEnable) if (m_pTransformedShape && m_bEnable)
@ -105,6 +111,11 @@ void e2d::Shape::_transform()
{ {
if (m_pParentNode && m_bEnable) if (m_pParentNode && m_bEnable)
{ {
if (m_bAutoResize)
{
this->_resize();
}
// ÊÍ·ÅÔ­ÐÎ×´ // ÊÍ·ÅÔ­ÐÎ×´
SafeReleaseInterface(&m_pTransformedShape); SafeReleaseInterface(&m_pTransformedShape);

View File

@ -48,7 +48,7 @@ public:
// 判断点是否在节点内 // 判断点是否在节点内
virtual bool isPointIn( virtual bool isPointIn(
Point point Point point
); ) const;
// 判断两节点是否相交 // 判断两节点是否相交
virtual bool isIntersectWith( virtual bool isIntersectWith(
@ -368,6 +368,11 @@ public:
double defaultPiovtY double defaultPiovtY
); );
// 设置节点是否包含默认形状(默认打开)
static void setDefaultShapeEnable(
bool bEnable
);
protected: protected:
// 更新节点 // 更新节点
void _update(); void _update();

View File

@ -64,10 +64,18 @@ public:
double opacity double opacity
); );
// 设置大小跟随
void setAutoResize(
bool bEnable
);
protected: protected:
// ת»»ÐÎ×´ // ת»»ÐÎ×´
virtual void _transform(); virtual void _transform();
// 重设大小
virtual void _resize() = 0;
// äÖȾÐÎ×´ // äÖȾÐÎ×´
virtual void _render(); virtual void _render();
@ -77,11 +85,12 @@ protected:
protected: protected:
bool m_bEnable; bool m_bEnable;
bool m_bIsVisiable; bool m_bIsVisiable;
bool m_bAutoResize;
UINT32 m_nCategoryBitmask; UINT32 m_nCategoryBitmask;
UINT32 m_nCollisionBitmask; UINT32 m_nCollisionBitmask;
UINT32 m_nColor; UINT32 m_nColor;
float m_fOpacity; float m_fOpacity;
Node * m_pParentNode; Node * m_pParentNode;
ID2D1TransformedGeometry * m_pTransformedShape; ID2D1TransformedGeometry * m_pTransformedShape;
}; };
@ -116,6 +125,9 @@ protected:
double bottom double bottom
); );
// 重设大小
virtual void _resize();
virtual ID2D1RectangleGeometry * _getD2dGeometry() const override; virtual ID2D1RectangleGeometry * _getD2dGeometry() const override;
protected: protected:
@ -149,6 +161,9 @@ protected:
double radius double radius
); );
// 重设大小
virtual void _resize();
virtual ID2D1EllipseGeometry * _getD2dGeometry() const override; virtual ID2D1EllipseGeometry * _getD2dGeometry() const override;
protected: protected:
@ -184,6 +199,9 @@ protected:
double radiusY double radiusY
); );
// 重设大小
virtual void _resize();
virtual ID2D1EllipseGeometry * _getD2dGeometry() const override; virtual ID2D1EllipseGeometry * _getD2dGeometry() const override;
protected: protected: