Magic_Game/core/Node/Node.cpp

870 lines
15 KiB
C++
Raw Normal View History

#include "..\enodes.h"
#include "..\emanagers.h"
2017-10-17 21:22:25 +08:00
#include "..\etools.h"
2017-10-19 00:50:04 +08:00
#include "..\eactions.h"
2018-02-03 22:04:43 +08:00
#include "..\eshape.h"
2017-10-14 18:43:32 +08:00
#include <algorithm>
// Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>λ<EFBFBD><CEBB>
static float s_fDefaultPiovtX = 0;
static float s_fDefaultPiovtY = 0;
e2d::Node::Node()
2017-10-14 18:43:32 +08:00
: m_nOrder(0)
2018-02-27 21:07:43 +08:00
, m_fPosX(0)
, m_fPosY(0)
, m_fWidth(0)
, m_fHeight(0)
, m_fScaleX(1.0f)
, m_fScaleY(1.0f)
2017-10-15 02:46:24 +08:00
, m_fRotation(0)
, m_fSkewAngleX(0)
, m_fSkewAngleY(0)
, m_fDisplayOpacity(1.0f)
, m_fRealOpacity(1.0f)
, m_fPivotX(s_fDefaultPiovtX)
, m_fPivotY(s_fDefaultPiovtY)
, m_MatriInitial(D2D1::Matrix3x2F::Identity())
, m_MatriFinal(D2D1::Matrix3x2F::Identity())
, m_bVisiable(true)
2017-10-17 21:22:25 +08:00
, m_bDisplayedInScene(false)
2018-02-03 22:04:43 +08:00
, m_pShape(nullptr)
2017-10-14 11:40:47 +08:00
, m_pParent(nullptr)
, m_pParentScene(nullptr)
, m_nHashName(0)
, m_bSortChildrenNeeded(false)
, m_bTransformNeeded(false)
2018-02-03 22:04:43 +08:00
, m_bAutoUpdate(true)
{
}
e2d::Node::~Node()
{
ActionManager::__clearAllActionsBindedWith(this);
ShapeManager::__delShape(m_pShape);
2018-02-03 22:04:43 +08:00
for (auto child : m_vChildren)
2017-10-21 19:53:38 +08:00
{
2018-02-03 22:04:43 +08:00
SafeRelease(&child);
2017-10-21 19:53:38 +08:00
}
}
void e2d::Node::_update()
2017-10-14 18:43:32 +08:00
{
if (m_bTransformNeeded)
{
_updateTransform(this);
}
2017-10-14 18:43:32 +08:00
if (!m_vChildren.empty())
{
2018-02-03 22:04:43 +08:00
if (m_bSortChildrenNeeded)
{
2018-02-11 20:48:56 +08:00
// <20>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
auto sortFunc = [](Node * n1, Node * n2) {
return n1->getOrder() < n2->getOrder();
};
2018-02-03 22:04:43 +08:00
std::sort(
std::begin(m_vChildren),
std::end(m_vChildren),
2018-02-11 20:48:56 +08:00
sortFunc
2018-02-03 22:04:43 +08:00
);
2017-10-14 18:43:32 +08:00
2018-02-03 22:04:43 +08:00
m_bSortChildrenNeeded = false;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>
2017-12-15 23:15:06 +08:00
size_t size = m_vChildren.size();
size_t i;
2017-10-14 18:43:32 +08:00
for (i = 0; i < size; i++)
{
auto child = m_vChildren[i];
// <20><><EFBFBD><EFBFBD> Order С<><D0A1><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD>
if (child->getOrder() < 0)
{
2018-02-06 21:11:54 +08:00
child->_update();
2017-10-14 18:43:32 +08:00
}
else
{
break;
}
}
2018-02-06 21:11:54 +08:00
if (m_bAutoUpdate)
2018-02-04 21:24:27 +08:00
{
if (!Game::isPaused())
2018-02-06 21:11:54 +08:00
{
this->onUpdate();
}
this->onFixedUpdate();
2018-02-03 22:04:43 +08:00
}
2017-10-14 18:43:32 +08:00
// <20><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ڵ<EFBFBD>
for (; i < size; i++)
2018-02-06 21:11:54 +08:00
m_vChildren[i]->_update();
2017-10-14 18:43:32 +08:00
}
else
{
2018-02-06 21:11:54 +08:00
if (m_bAutoUpdate)
2018-02-04 21:24:27 +08:00
{
if (!Game::isPaused())
2018-02-06 21:11:54 +08:00
{
this->onUpdate();
}
this->onFixedUpdate();
2018-02-04 21:24:27 +08:00
}
2017-10-14 18:43:32 +08:00
}
}
void e2d::Node::_render()
2017-10-13 11:42:36 +08:00
{
2018-01-30 16:45:38 +08:00
if (!m_bVisiable)
{
return;
}
if (!m_vChildren.empty())
{
size_t size = m_vChildren.size();
size_t i;
for (i = 0; i < size; i++)
{
auto child = m_vChildren[i];
// <20><><EFBFBD><EFBFBD> Order С<><D0A1><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD>
if (child->getOrder() < 0)
{
child->_render();
}
else
{
break;
}
}
// ת<><D7AA><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD>Ķ<EFBFBD>ά<EFBFBD><CEAC><EFBFBD><EFBFBD>
Renderer::getRenderTarget()->SetTransform(m_MatriFinal);
2018-01-30 16:45:38 +08:00
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
this->onRender();
// <20><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ڵ<EFBFBD>
for (; i < size; i++)
m_vChildren[i]->_render();
}
else
{
// ת<><D7AA><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD>Ķ<EFBFBD>ά<EFBFBD><CEAC><EFBFBD><EFBFBD>
Renderer::getRenderTarget()->SetTransform(m_MatriFinal);
2018-01-30 16:45:38 +08:00
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
this->onRender();
}
}
void e2d::Node::_drawShape()
2017-10-29 23:48:32 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>״
2018-02-03 22:04:43 +08:00
if (m_pShape && m_pShape->m_bIsVisiable)
2017-10-29 23:48:32 +08:00
{
2018-02-03 22:04:43 +08:00
m_pShape->_render();
2017-10-29 23:48:32 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>״
2018-02-03 22:04:43 +08:00
for (auto child : m_vChildren)
2017-10-29 23:48:32 +08:00
{
2018-02-03 22:04:43 +08:00
child->_drawShape();
2017-10-29 23:48:32 +08:00
}
}
void e2d::Node::_onEnter()
2017-10-17 21:22:25 +08:00
{
2018-02-03 22:04:43 +08:00
if (!this->m_bDisplayedInScene)
2017-10-17 21:22:25 +08:00
{
this->m_bDisplayedInScene = true;
this->onEnter();
2018-02-03 22:04:43 +08:00
for (auto child : m_vChildren)
2017-10-17 21:22:25 +08:00
{
2018-02-03 22:04:43 +08:00
child->_onEnter();
2017-10-17 21:22:25 +08:00
}
}
}
void e2d::Node::_onExit()
2017-10-17 21:22:25 +08:00
{
if (this->m_bDisplayedInScene)
{
this->m_bDisplayedInScene = false;
this->onExit();
2018-02-03 22:04:43 +08:00
for (auto child : m_vChildren)
2017-10-17 21:22:25 +08:00
{
2018-02-03 22:04:43 +08:00
child->_onExit();
2017-10-17 21:22:25 +08:00
}
}
}
void e2d::Node::_updateTransform()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD>
D2D1_POINT_2F pivot = D2D1::Point2F(
2018-02-27 21:07:43 +08:00
m_fWidth * m_fPivotX,
m_fHeight * m_fPivotY
);
// <20>任 Initial <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ㽫<DAB5><E3BDAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б
m_MatriInitial = D2D1::Matrix3x2F::Scale(
m_fScaleX,
m_fScaleY,
pivot
) * D2D1::Matrix3x2F::Skew(
m_fSkewAngleX,
m_fSkewAngleY,
pivot
2017-10-17 21:22:25 +08:00
) * D2D1::Matrix3x2F::Rotation(
m_fRotation,
pivot
) * D2D1::Matrix3x2F::Translation(
2018-02-27 21:07:43 +08:00
m_fPosX,
m_fPosY
);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5>任 Final <20><><EFBFBD><EFBFBD>
2018-02-11 20:48:56 +08:00
m_MatriFinal = m_MatriInitial * D2D1::Matrix3x2F::Translation(-pivot.x, -pivot.y);
// <20>͸<EFBFBD><CDB8>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (m_pParent)
{
m_MatriInitial = m_MatriInitial * m_pParent->m_MatriInitial;
2018-02-11 20:48:56 +08:00
m_MatriFinal = m_MatriFinal * m_pParent->m_MatriInitial;
}
}
void e2d::Node::_updateChildrenTransform()
{
2018-02-03 22:04:43 +08:00
for (auto child : m_vChildren)
{
2018-02-03 22:04:43 +08:00
_updateTransform(child);
}
}
void e2d::Node::_updateTransform(Node * node)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
node->_updateTransform();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״Ҳ<D7B4><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧת<D3A6><D7AA>
2018-02-03 22:04:43 +08:00
if (node->m_pShape)
2017-10-29 23:48:32 +08:00
{
2018-02-03 22:04:43 +08:00
node->m_pShape->_transform();
2017-10-29 23:48:32 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>нڵ<D0BD>
node->_updateChildrenTransform();
2017-10-29 23:48:32 +08:00
// <20><>־<EFBFBD><D6BE>ִ<EFBFBD>й<EFBFBD><D0B9>
node->m_bTransformNeeded = false;
}
2017-10-14 18:43:32 +08:00
void e2d::Node::_updateChildrenOpacity()
{
2018-02-03 22:04:43 +08:00
for (auto child : m_vChildren)
{
2018-02-03 22:04:43 +08:00
_updateOpacity(child);
}
}
2017-10-14 18:43:32 +08:00
void e2d::Node::_updateOpacity(Node * node)
{
if (node->m_pParent)
{
node->m_fDisplayOpacity = node->m_fRealOpacity * node->m_pParent->m_fDisplayOpacity;
}
node->_updateChildrenOpacity();
2017-10-14 18:43:32 +08:00
}
bool e2d::Node::isVisiable() const
2017-10-14 18:43:32 +08:00
{
return m_bVisiable;
}
e2d::String e2d::Node::getName() const
2017-10-17 21:22:25 +08:00
{
return m_sName;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getPosX() const
2017-10-17 21:22:25 +08:00
{
2018-02-27 21:07:43 +08:00
return m_fPosX;
2017-10-17 21:22:25 +08:00
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getPosY() const
2017-10-15 02:46:24 +08:00
{
2018-02-27 21:07:43 +08:00
return m_fPosY;
2017-10-15 02:46:24 +08:00
}
e2d::Point e2d::Node::getPos() const
2017-10-15 02:46:24 +08:00
{
2018-02-27 21:07:43 +08:00
return Point(m_fPosX, m_fPosY);
2017-10-15 02:46:24 +08:00
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getWidth() const
2017-10-14 18:43:32 +08:00
{
2018-02-27 21:07:43 +08:00
return m_fWidth * m_fScaleX;
2017-10-14 18:43:32 +08:00
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getHeight() const
2017-10-14 18:43:32 +08:00
{
2018-02-27 21:07:43 +08:00
return m_fHeight * m_fScaleY;
2017-10-17 21:22:25 +08:00
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getRealWidth() const
2017-10-17 21:22:25 +08:00
{
2018-02-27 21:07:43 +08:00
return m_fWidth;
2017-10-17 21:22:25 +08:00
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getRealHeight() const
2017-10-17 21:22:25 +08:00
{
2018-02-27 21:07:43 +08:00
return m_fHeight;
2017-10-17 21:22:25 +08:00
}
e2d::Size e2d::Node::getRealSize() const
2017-10-17 21:22:25 +08:00
{
2018-02-27 21:07:43 +08:00
return Size(m_fWidth, m_fHeight);
2017-10-17 21:22:25 +08:00
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getPivotX() const
{
return m_fPivotX;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getPivotY() const
{
return m_fPivotY;
}
e2d::Size e2d::Node::getSize() const
2017-10-17 21:22:25 +08:00
{
return Size(getWidth(), getHeight());
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getScaleX() const
{
2017-10-15 02:46:24 +08:00
return m_fScaleX;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getScaleY() const
{
2017-10-15 02:46:24 +08:00
return m_fScaleY;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getSkewX() const
{
2017-10-15 02:46:24 +08:00
return m_fSkewAngleX;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getSkewY() const
{
2017-10-15 02:46:24 +08:00
return m_fSkewAngleY;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getRotation() const
{
2017-10-15 02:46:24 +08:00
return m_fRotation;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getOpacity() const
{
return m_fRealOpacity;
2017-10-15 02:46:24 +08:00
}
e2d::Shape * e2d::Node::getShape() const
2018-02-03 22:04:43 +08:00
{
return m_pShape;
}
int e2d::Node::getOrder() const
2017-10-15 02:46:24 +08:00
{
return m_nOrder;
}
void e2d::Node::setOrder(int order)
2017-10-15 02:46:24 +08:00
{
m_nOrder = order;
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPosX(double x)
2017-10-17 21:22:25 +08:00
{
2018-02-27 21:07:43 +08:00
this->setPos(x, m_fPosY);
2017-10-17 21:22:25 +08:00
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPosY(double y)
{
2018-02-27 21:07:43 +08:00
this->setPos(m_fPosX, y);
}
void e2d::Node::setPos(const Point & p)
{
2017-10-17 21:22:25 +08:00
this->setPos(p.x, p.y);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPos(double x, double y)
{
2018-02-27 21:07:43 +08:00
if (m_fPosX == x && m_fPosY == y)
2017-10-14 18:43:32 +08:00
return;
2018-02-27 21:07:43 +08:00
m_fPosX = static_cast<float>(x);
m_fPosY = static_cast<float>(y);
m_bTransformNeeded = true;
}
2018-02-27 21:07:43 +08:00
void e2d::Node::movePosX(double x)
2017-11-03 12:51:01 +08:00
{
this->movePos(x, 0);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::movePosY(double y)
2017-11-03 12:51:01 +08:00
{
this->movePos(0, y);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::movePos(double x, double y)
{
2018-02-27 21:07:43 +08:00
this->setPos(m_fPosX + x, m_fPosY + y);
2017-10-17 21:22:25 +08:00
}
void e2d::Node::movePos(const Vector & v)
2017-10-17 21:22:25 +08:00
{
2017-11-03 12:51:01 +08:00
this->movePos(v.x, v.y);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setScaleX(double scaleX)
{
2017-10-15 02:46:24 +08:00
this->setScale(scaleX, m_fScaleY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setScaleY(double scaleY)
{
2017-10-15 02:46:24 +08:00
this->setScale(m_fScaleX, scaleY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setScale(double scale)
{
2017-10-15 02:46:24 +08:00
this->setScale(scale, scale);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setScale(double scaleX, double scaleY)
{
2017-10-15 02:46:24 +08:00
if (m_fScaleX == scaleX && m_fScaleY == scaleY)
return;
2018-02-27 21:07:43 +08:00
m_fScaleX = static_cast<float>(scaleX);
m_fScaleY = static_cast<float>(scaleY);
m_bTransformNeeded = true;
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setSkewX(double angleX)
{
2017-10-15 02:46:24 +08:00
this->setSkew(angleX, m_fSkewAngleY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setSkewY(double angleY)
{
2017-10-15 02:46:24 +08:00
this->setSkew(m_fSkewAngleX, angleY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setSkew(double angleX, double angleY)
{
2017-10-15 02:46:24 +08:00
if (m_fSkewAngleX == angleX && m_fSkewAngleY == angleY)
return;
2018-02-27 21:07:43 +08:00
m_fSkewAngleX = static_cast<float>(angleX);
m_fSkewAngleY = static_cast<float>(angleY);
m_bTransformNeeded = true;
2017-10-15 02:46:24 +08:00
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setRotation(double angle)
2017-10-15 02:46:24 +08:00
{
if (m_fRotation == angle)
return;
2018-02-27 21:07:43 +08:00
m_fRotation = static_cast<float>(angle);
m_bTransformNeeded = true;
2017-10-15 02:46:24 +08:00
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setOpacity(double opacity)
2017-10-15 02:46:24 +08:00
{
if (m_fRealOpacity == opacity)
return;
2018-02-27 21:07:43 +08:00
m_fDisplayOpacity = m_fRealOpacity = min(max(static_cast<float>(opacity), 0), 1);
2017-10-21 19:09:31 +08:00
// <20><><EFBFBD>½ڵ<C2BD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
_updateOpacity(this);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPivotX(double pivotX)
{
this->setPivot(pivotX, m_fPivotY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPivotY(double pivotY)
{
this->setPivot(m_fPivotX, pivotY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPivot(double pivotX, double pivotY)
{
if (m_fPivotX == pivotX && m_fPivotY == pivotY)
return;
2018-02-27 21:07:43 +08:00
m_fPivotX = min(max(static_cast<float>(pivotX), 0), 1);
m_fPivotY = min(max(static_cast<float>(pivotY), 0), 1);
m_bTransformNeeded = true;
}
2018-03-01 19:28:22 +08:00
void e2d::Node::setWidth(double width)
{
this->setSize(width, m_fHeight);
}
void e2d::Node::setHeight(double height)
{
this->setSize(m_fWidth, height);
}
void e2d::Node::setSize(double width, double height)
{
if (m_fWidth == width && m_fHeight == height)
return;
m_fWidth = static_cast<float>(width);
m_fHeight = static_cast<float>(height);
m_bTransformNeeded = true;
}
void e2d::Node::setSize(Size size)
{
this->setSize(size.width, size.height);
}
void e2d::Node::setShape(Shape * pShape)
{
2017-10-28 18:48:21 +08:00
// ɾ<><C9BE><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD>״
ShapeManager::__delShape(m_pShape);
2017-10-29 23:48:32 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>״
ShapeManager::__addShape(pShape);
2017-10-29 23:48:32 +08:00
2018-02-03 22:04:43 +08:00
if (pShape)
2017-10-28 18:48:21 +08:00
{
2017-10-29 23:48:32 +08:00
// ˫<><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-03 22:04:43 +08:00
this->m_pShape = pShape;
pShape->m_pParentNode = this;
2017-10-28 18:48:21 +08:00
}
2017-10-29 23:48:32 +08:00
else
2017-10-28 18:48:21 +08:00
{
2018-02-03 22:04:43 +08:00
this->m_pShape = nullptr;
2017-10-28 18:48:21 +08:00
}
}
void e2d::Node::addChild(Node * child, int order /* = 0 */)
2017-10-14 11:40:47 +08:00
{
WARN_IF(child == nullptr, "Node::addChild NULL pointer exception.");
2017-10-14 11:40:47 +08:00
if (child)
{
ASSERT(child->m_pParent == nullptr, "Node already added. It can't be added again!");
2018-02-03 22:04:43 +08:00
for (Node * parent = this; parent != nullptr; parent = parent->getParent())
2017-10-14 11:40:47 +08:00
{
ASSERT(child != parent, "A Node cannot be the child of his own children!");
2017-10-14 11:40:47 +08:00
}
2017-10-14 18:43:32 +08:00
m_vChildren.push_back(child);
child->setOrder(order);
child->retain();
child->m_pParent = this;
2017-10-17 21:22:25 +08:00
if (this->m_pParentScene)
{
child->_setParentScene(this->m_pParentScene);
}
if (this->m_bDisplayedInScene)
{
child->_onEnter();
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
_updateOpacity(child);
// <20><><EFBFBD>½ڵ<C2BD>ת<EFBFBD><D7AA>
child->m_bTransformNeeded = true;
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
m_bSortChildrenNeeded = true;
2017-10-14 11:40:47 +08:00
}
}
e2d::Node * e2d::Node::getParent() const
{
return m_pParent;
}
e2d::Scene * e2d::Node::getParentScene() const
{
return m_pParentScene;
}
std::vector<e2d::Node*>& e2d::Node::getChildren()
2017-10-14 11:40:47 +08:00
{
return m_vChildren;
}
int e2d::Node::getChildrenCount() const
2017-10-14 11:40:47 +08:00
{
2018-02-03 22:04:43 +08:00
return static_cast<int>(m_vChildren.size());
2017-10-14 11:40:47 +08:00
}
e2d::Node * e2d::Node::getChild(const String & name)
2017-10-14 11:40:47 +08:00
{
WARN_IF(name.isEmpty(), "Invalid Node name.");
2017-10-14 11:40:47 +08:00
unsigned int hash = name.getHashCode();
2017-10-14 11:40:47 +08:00
2018-02-03 22:04:43 +08:00
for (auto child : m_vChildren)
2017-10-14 11:40:47 +08:00
{
// <20><>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD>ƿ<EFBFBD><C6BF>ܻ<EFBFBD><DCBB><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC> Hash ֵ<><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱȽ<C8B1> Hash <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>
2018-02-03 22:04:43 +08:00
if (child->m_nHashName == hash && child->m_sName == name)
return child;
2017-10-14 11:40:47 +08:00
}
return nullptr;
}
std::vector<e2d::Node*> e2d::Node::getChildren(const String & name)
2018-02-03 22:04:43 +08:00
{
std::vector<Node*> vChildren;
unsigned int hash = name.getHashCode();
2018-02-03 22:04:43 +08:00
for (auto child : m_vChildren)
if (child->m_nHashName == hash && child->m_sName == name)
vChildren.push_back(child);
return std::move(vChildren);
}
void e2d::Node::removeFromParent()
2017-10-14 11:40:47 +08:00
{
2017-10-14 18:43:32 +08:00
if (m_pParent)
{
2017-10-31 17:19:13 +08:00
m_pParent->removeChild(this);
2017-10-14 18:43:32 +08:00
}
2017-10-14 11:40:47 +08:00
}
bool e2d::Node::removeChild(Node * child)
2017-10-14 11:40:47 +08:00
{
WARN_IF(child == nullptr, "Node::removeChildren NULL pointer exception.");
2017-10-14 18:43:32 +08:00
if (m_vChildren.empty())
{
2017-10-17 21:22:25 +08:00
return false;
2017-10-14 18:43:32 +08:00
}
if (child)
{
2017-12-15 23:15:06 +08:00
size_t size = m_vChildren.size();
for (size_t i = 0; i < size; i++)
2017-10-14 18:43:32 +08:00
{
if (m_vChildren[i] == child)
{
m_vChildren.erase(m_vChildren.begin() + i);
child->m_pParent = nullptr;
2018-02-03 22:04:43 +08:00
2017-10-17 21:22:25 +08:00
if (child->m_pParentScene)
{
child->_setParentScene(nullptr);
}
2018-02-03 22:04:43 +08:00
if (child->m_bDisplayedInScene)
{
child->_onExit();
}
2017-10-14 18:43:32 +08:00
child->release();
2017-10-17 21:22:25 +08:00
return true;
2017-10-14 18:43:32 +08:00
}
}
}
2017-10-17 21:22:25 +08:00
return false;
2017-10-14 11:40:47 +08:00
}
void e2d::Node::removeChildren(const String & childName)
2017-10-14 11:40:47 +08:00
{
WARN_IF(childName.isEmpty(), "Invalid Node name.");
2017-10-14 18:43:32 +08:00
if (m_vChildren.empty())
{
return;
}
2017-10-17 21:22:25 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Hash ֵ
unsigned int hash = childName.getHashCode();
2017-10-17 21:22:25 +08:00
2017-12-15 23:15:06 +08:00
size_t size = m_vChildren.size();
for (size_t i = 0; i < size; i++)
2017-10-14 18:43:32 +08:00
{
auto child = m_vChildren[i];
if (child->m_nHashName == hash && child->m_sName == childName)
{
m_vChildren.erase(m_vChildren.begin() + i);
child->m_pParent = nullptr;
2017-10-17 21:22:25 +08:00
if (child->m_pParentScene)
{
child->_setParentScene(nullptr);
}
2018-02-03 22:04:43 +08:00
if (child->m_bDisplayedInScene)
{
child->_onExit();
}
2017-10-14 18:43:32 +08:00
child->release();
}
}
2017-10-14 11:40:47 +08:00
}
void e2d::Node::clearAllChildren()
2017-10-17 21:22:25 +08:00
{
// <20><><EFBFBD>нڵ<D0BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD>һ
2018-02-03 22:04:43 +08:00
for (auto child : m_vChildren)
2017-10-17 21:22:25 +08:00
{
2018-02-03 22:04:43 +08:00
if (child->m_bDisplayedInScene)
{
child->_onExit();
}
child->release();
2017-10-17 21:22:25 +08:00
}
// <20><><EFBFBD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
m_vChildren.clear();
}
void e2d::Node::runAction(Action * action)
2017-10-19 00:50:04 +08:00
{
2018-02-06 15:34:47 +08:00
if (this != action->getTarget())
{
WARN_IF(
nullptr != action->getTarget(),
"The action has already got a target, The clone of the action will be created automatically!"
);
if (nullptr != action->getTarget())
{
action = action->clone();
}
ActionManager::_add(action, this);
2018-02-06 15:34:47 +08:00
}
else
2018-02-03 22:04:43 +08:00
{
2018-02-06 15:34:47 +08:00
action->reset();
2018-02-03 22:04:43 +08:00
}
2017-10-19 00:50:04 +08:00
}
void e2d::Node::resumeAction(Action * action)
{
if (action->getTarget() == this)
{
action->resume();
}
}
void e2d::Node::pauseAction(Action * action)
{
if (action->getTarget() == this)
{
action->pause();
}
}
bool e2d::Node::isPointIn(Point point)
2017-10-21 19:09:31 +08:00
{
if (m_bTransformNeeded)
2017-10-21 19:09:31 +08:00
{
_updateTransform(this);
}
// Ϊ<>ڵ㴴<DAB5><E3B4B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>״
ID2D1RectangleGeometry * rect;
Renderer::getID2D1Factory()->CreateRectangleGeometry(
2018-02-27 21:07:43 +08:00
D2D1::RectF(0, 0, m_fWidth * m_fScaleX, m_fHeight * m_fScaleY),
&rect
2017-10-21 19:09:31 +08:00
);
// <20>жϵ<D0B6><CFB5>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>״<EFBFBD><D7B4>
BOOL ret;
rect->FillContainsPoint(
2018-02-27 21:07:43 +08:00
D2D1::Point2F(
static_cast<float>(point.x),
static_cast<float>(point.y)),
&m_MatriFinal,
&ret
2017-10-21 19:09:31 +08:00
);
if (ret)
2017-10-21 19:09:31 +08:00
{
return true;
}
else
{
2018-02-03 22:04:43 +08:00
for (auto child : m_vChildren)
if (child->isPointIn(point))
2017-10-21 19:09:31 +08:00
return true;
}
return false;
}
void e2d::Node::setAutoUpdate(bool bAutoUpdate)
2018-02-03 22:04:43 +08:00
{
m_bAutoUpdate = bAutoUpdate;
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setDefaultPiovt(double defaultPiovtX, double defaultPiovtY)
{
2018-02-27 21:07:43 +08:00
s_fDefaultPiovtX = min(max(static_cast<float>(defaultPiovtX), 0), 1);
s_fDefaultPiovtY = min(max(static_cast<float>(defaultPiovtY), 0), 1);
}
void e2d::Node::stopAction(Action * action)
2017-10-19 00:50:04 +08:00
{
if (action->getTarget() == this)
{
action->stop();
}
2017-10-19 00:50:04 +08:00
}
void e2d::Node::resumeAllActions()
2017-10-21 19:09:31 +08:00
{
ActionManager::resumeAllActionsBindedWith(this);
2017-10-21 19:09:31 +08:00
}
void e2d::Node::pauseAllActions()
2017-10-21 19:09:31 +08:00
{
ActionManager::pauseAllActionsBindedWith(this);
2017-10-21 19:09:31 +08:00
}
void e2d::Node::stopAllActions()
2017-10-21 19:09:31 +08:00
{
ActionManager::stopAllActionsBindedWith(this);
2017-10-21 19:09:31 +08:00
}
void e2d::Node::setVisiable(bool value)
{
m_bVisiable = value;
}
void e2d::Node::setName(const String & name)
2017-10-14 11:40:47 +08:00
{
WARN_IF(name.isEmpty(), "Invalid Node name.");
2017-10-14 11:40:47 +08:00
2018-02-03 22:04:43 +08:00
if (!name.isEmpty() && m_sName != name)
2017-10-14 11:40:47 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>
m_sName = name;
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> Hash <20><>
m_nHashName = name.getHashCode();
2017-10-14 11:40:47 +08:00
}
}
2017-10-17 21:22:25 +08:00
void e2d::Node::_setParentScene(Scene * scene)
2017-10-17 21:22:25 +08:00
{
m_pParentScene = scene;
2018-02-03 22:04:43 +08:00
for (auto child : m_vChildren)
2017-10-17 21:22:25 +08:00
{
2018-02-03 22:04:43 +08:00
child->_setParentScene(scene);
2017-10-17 21:22:25 +08:00
}
}