Magic_Game/core/Node/Node.cpp

1036 lines
18 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dnode.h"
#include "..\e2dmanager.h"
#include "..\e2daction.h"
#include "..\e2dcollider.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;
static bool s_fDefaultColliderEnabled = true;
e2d::Node::Node()
2018-05-08 17:40:36 +08:00
: _nOrder(0)
, _fPosX(0)
, _fPosY(0)
, _fWidth(0)
, _fHeight(0)
, _fScaleX(1.0f)
, _fScaleY(1.0f)
, _fRotation(0)
, _fSkewAngleX(0)
, _fSkewAngleY(0)
, _fDisplayOpacity(1.0f)
, _fRealOpacity(1.0f)
, _fPivotX(s_fDefaultPiovtX)
, _fPivotY(s_fDefaultPiovtY)
, _MatriInitial(D2D1::Matrix3x2F::Identity())
, _MatriFinal(D2D1::Matrix3x2F::Identity())
, _bVisiable(true)
, _pCollider(nullptr)
, _pParent(nullptr)
, _pParentScene(nullptr)
, _nHashName(0)
, _bSortChildrenNeeded(false)
, _bTransformNeeded(false)
, _bAutoUpdate(true)
, _bPositionFixed(false)
{
if (s_fDefaultColliderEnabled)
2018-03-07 20:14:58 +08:00
{
auto rect = new ColliderRect(this);
this->setCollider(rect);
2018-03-07 20:14:58 +08:00
}
}
e2d::Node::~Node()
{
}
void e2d::Node::_update()
2017-10-14 18:43:32 +08:00
{
2018-05-08 17:40:36 +08:00
if (_bTransformNeeded)
{
_updateTransform();
}
2017-10-14 18:43:32 +08:00
2018-05-08 17:40:36 +08:00
if (!_vChildren.empty())
2017-10-14 18:43:32 +08:00
{
2018-05-08 17:40:36 +08:00
if (_bSortChildrenNeeded)
2018-02-03 22:04:43 +08:00
{
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(
2018-05-08 17:40:36 +08:00
std::begin(_vChildren),
std::end(_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-05-08 17:40:36 +08:00
_bSortChildrenNeeded = false;
2018-02-03 22:04:43 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>
2018-05-08 17:40:36 +08:00
size_t size = _vChildren.size();
2017-12-15 23:15:06 +08:00
size_t i;
2017-10-14 18:43:32 +08:00
for (i = 0; i < size; i++)
{
2018-05-08 17:40:36 +08:00
auto child = _vChildren[i];
2017-10-14 18:43:32 +08:00
// <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-05-08 17:40:36 +08:00
if (_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-05-08 17:40:36 +08:00
_vChildren[i]->_update();
2017-10-14 18:43:32 +08:00
}
else
{
2018-05-08 17:40:36 +08:00
if (_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-05-08 17:40:36 +08:00
if (!_bVisiable)
2018-01-30 16:45:38 +08:00
{
return;
}
2018-05-08 17:40:36 +08:00
if (!_vChildren.empty())
2018-01-30 16:45:38 +08:00
{
2018-05-08 17:40:36 +08:00
size_t size = _vChildren.size();
2018-01-30 16:45:38 +08:00
size_t i;
for (i = 0; i < size; i++)
{
2018-05-08 17:40:36 +08:00
auto child = _vChildren[i];
2018-01-30 16:45:38 +08:00
// <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>
2018-05-08 17:40:36 +08:00
Renderer::getRenderTarget()->SetTransform(_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++)
2018-05-08 17:40:36 +08:00
_vChildren[i]->_render();
2018-01-30 16:45:38 +08:00
}
else
{
// ת<><D7AA><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD>Ķ<EFBFBD>ά<EFBFBD><CEAC><EFBFBD><EFBFBD>
2018-05-08 17:40:36 +08:00
Renderer::getRenderTarget()->SetTransform(_MatriFinal);
2018-01-30 16:45:38 +08:00
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
this->onRender();
}
}
void e2d::Node::_drawCollider()
2017-10-29 23:48:32 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
2018-05-08 17:40:36 +08:00
if (_pCollider && _pCollider->_bIsVisiable)
2017-10-29 23:48:32 +08:00
{
2018-05-08 17:40:36 +08:00
_pCollider->_render();
2017-10-29 23:48:32 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
2018-05-08 17:40:36 +08:00
for (auto child : _vChildren)
2017-10-29 23:48:32 +08:00
{
child->_drawCollider();
2017-10-29 23:48:32 +08:00
}
}
void e2d::Node::_updateSelfTransform()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD>
2018-05-08 17:40:36 +08:00
D2D1_POINT_2F pivot = { _fWidth * _fPivotX, _fHeight * _fPivotY };
// <20>任 Initial <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ㽫<DAB5><E3BDAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б
2018-05-08 17:40:36 +08:00
_MatriInitial = D2D1::Matrix3x2F::Scale(
_fScaleX,
_fScaleY,
pivot
) * D2D1::Matrix3x2F::Skew(
2018-05-08 17:40:36 +08:00
_fSkewAngleX,
_fSkewAngleY,
pivot
2017-10-17 21:22:25 +08:00
) * D2D1::Matrix3x2F::Rotation(
2018-05-08 17:40:36 +08:00
_fRotation,
pivot
) * D2D1::Matrix3x2F::Translation(
2018-05-08 17:40:36 +08:00
_fPosX,
_fPosY
);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5>任 Final <20><><EFBFBD><EFBFBD>
2018-05-08 17:40:36 +08:00
_MatriFinal = _MatriInitial * D2D1::Matrix3x2F::Translation(-pivot.x, -pivot.y);
// <20>͸<EFBFBD><CDB8>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-05-08 17:40:36 +08:00
if (!_bPositionFixed && _pParent)
{
2018-05-08 17:40:36 +08:00
_MatriInitial = _MatriInitial * _pParent->_MatriInitial;
_MatriFinal = _MatriFinal * _pParent->_MatriInitial;
}
}
void e2d::Node::_updateTransform()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
_updateSelfTransform();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧת<D3A6><D7AA>
2018-05-08 17:40:36 +08:00
if (_pCollider)
2017-10-29 23:48:32 +08:00
{
2018-05-08 17:40:36 +08:00
_pCollider->_transform();
2017-10-29 23:48:32 +08:00
}
// <20><>־<EFBFBD><D6BE>ִ<EFBFBD>й<EFBFBD><D0B9>
2018-05-08 17:40:36 +08:00
_bTransformNeeded = false;
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>нڵ<D0BD>
2018-05-08 17:40:36 +08:00
for (auto child : this->_vChildren)
{
child->_updateTransform();
}
}
2017-10-14 18:43:32 +08:00
void e2d::Node::_updateOpacity()
{
2018-05-08 17:40:36 +08:00
if (_pParent)
{
2018-05-08 17:40:36 +08:00
_fDisplayOpacity = _fRealOpacity * _pParent->_fDisplayOpacity;
}
2018-05-08 17:40:36 +08:00
for (auto child : _vChildren)
{
child->_updateOpacity();
}
2017-10-14 18:43:32 +08:00
}
bool e2d::Node::isVisiable() const
2017-10-14 18:43:32 +08:00
{
2018-05-08 17:40:36 +08:00
return _bVisiable;
2017-10-14 18:43:32 +08:00
}
e2d::String e2d::Node::getName() const
2017-10-17 21:22:25 +08:00
{
2018-05-08 17:40:36 +08:00
return _sName;
2017-10-17 21:22:25 +08:00
}
unsigned int e2d::Node::getHashName() const
{
2018-05-08 17:40:36 +08:00
return _nHashName;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getPosX() const
2017-10-17 21:22:25 +08:00
{
2018-05-08 17:40:36 +08:00
return _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-05-08 17:40:36 +08:00
return _fPosY;
2017-10-15 02:46:24 +08:00
}
e2d::Point e2d::Node::getPos() const
2017-10-15 02:46:24 +08:00
{
2018-05-08 17:40:36 +08:00
return Point(_fPosX, _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-05-08 17:40:36 +08:00
return _fWidth * _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-05-08 17:40:36 +08:00
return _fHeight * _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-05-08 17:40:36 +08:00
return _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-05-08 17:40:36 +08:00
return _fHeight;
2017-10-17 21:22:25 +08:00
}
e2d::Size e2d::Node::getRealSize() const
2017-10-17 21:22:25 +08:00
{
2018-05-08 17:40:36 +08:00
return Size(_fWidth, _fHeight);
2017-10-17 21:22:25 +08:00
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getPivotX() const
{
2018-05-08 17:40:36 +08:00
return _fPivotX;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getPivotY() const
{
2018-05-08 17:40:36 +08:00
return _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
{
2018-05-08 17:40:36 +08:00
return _fScaleX;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getScaleY() const
{
2018-05-08 17:40:36 +08:00
return _fScaleY;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getSkewX() const
{
2018-05-08 17:40:36 +08:00
return _fSkewAngleX;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getSkewY() const
{
2018-05-08 17:40:36 +08:00
return _fSkewAngleY;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getRotation() const
{
2018-05-08 17:40:36 +08:00
return _fRotation;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getOpacity() const
{
2018-05-08 17:40:36 +08:00
return _fRealOpacity;
2017-10-15 02:46:24 +08:00
}
e2d::NodeProperty e2d::Node::getProperty() const
{
NodeProperty prop;
2018-05-08 17:40:36 +08:00
prop.visable = _bVisiable;
prop.posX = _fPosX;
prop.posY = _fPosY;
prop.width = _fWidth;
prop.height = _fHeight;
prop.opacity = _fRealOpacity;
prop.pivotX = _fPivotX;
prop.pivotY = _fPivotY;
prop.scaleX = _fScaleX;
prop.scaleY = _fScaleY;
prop.rotation = _fRotation;
prop.skewAngleX = _fSkewAngleX;
prop.skewAngleY = _fSkewAngleY;
return prop;
}
e2d::Collider * e2d::Node::getCollider() const
2018-02-03 22:04:43 +08:00
{
2018-05-08 17:40:36 +08:00
return _pCollider;
2018-02-03 22:04:43 +08:00
}
int e2d::Node::getOrder() const
2017-10-15 02:46:24 +08:00
{
2018-05-08 17:40:36 +08:00
return _nOrder;
2017-10-15 02:46:24 +08:00
}
void e2d::Node::setOrder(int order)
2017-10-15 02:46:24 +08:00
{
2018-05-08 17:40:36 +08:00
_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-05-08 17:40:36 +08:00
this->setPos(x, _fPosY);
2017-10-17 21:22:25 +08:00
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPosY(double y)
{
2018-05-08 17:40:36 +08:00
this->setPos(_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-05-08 17:40:36 +08:00
if (_fPosX == x && _fPosY == y)
2017-10-14 18:43:32 +08:00
return;
2018-05-08 17:40:36 +08:00
_fPosX = static_cast<float>(x);
_fPosY = static_cast<float>(y);
_bTransformNeeded = true;
}
void e2d::Node::setPosFixed(bool fixed)
{
2018-05-08 17:40:36 +08:00
if (_bPositionFixed == fixed)
return;
2018-05-08 17:40:36 +08:00
_bPositionFixed = fixed;
_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-05-08 17:40:36 +08:00
this->setPos(_fPosX + x, _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)
{
2018-05-08 17:40:36 +08:00
this->setScale(scaleX, _fScaleY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setScaleY(double scaleY)
{
2018-05-08 17:40:36 +08:00
this->setScale(_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)
{
2018-05-08 17:40:36 +08:00
if (_fScaleX == scaleX && _fScaleY == scaleY)
2017-10-15 02:46:24 +08:00
return;
2018-05-08 17:40:36 +08:00
_fScaleX = static_cast<float>(scaleX);
_fScaleY = static_cast<float>(scaleY);
_bTransformNeeded = true;
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setSkewX(double angleX)
{
2018-05-08 17:40:36 +08:00
this->setSkew(angleX, _fSkewAngleY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setSkewY(double angleY)
{
2018-05-08 17:40:36 +08:00
this->setSkew(_fSkewAngleX, angleY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setSkew(double angleX, double angleY)
{
2018-05-08 17:40:36 +08:00
if (_fSkewAngleX == angleX && _fSkewAngleY == angleY)
2017-10-15 02:46:24 +08:00
return;
2018-05-08 17:40:36 +08:00
_fSkewAngleX = static_cast<float>(angleX);
_fSkewAngleY = static_cast<float>(angleY);
_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
{
2018-05-08 17:40:36 +08:00
if (_fRotation == angle)
2017-10-15 02:46:24 +08:00
return;
2018-05-08 17:40:36 +08:00
_fRotation = static_cast<float>(angle);
_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
{
2018-05-08 17:40:36 +08:00
if (_fRealOpacity == opacity)
return;
2018-05-08 17:40:36 +08:00
_fDisplayOpacity = _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();
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPivotX(double pivotX)
{
2018-05-08 17:40:36 +08:00
this->setPivot(pivotX, _fPivotY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPivotY(double pivotY)
{
2018-05-08 17:40:36 +08:00
this->setPivot(_fPivotX, pivotY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPivot(double pivotX, double pivotY)
{
2018-05-08 17:40:36 +08:00
if (_fPivotX == pivotX && _fPivotY == pivotY)
return;
2018-05-08 17:40:36 +08:00
_fPivotX = min(max(static_cast<float>(pivotX), 0), 1);
_fPivotY = min(max(static_cast<float>(pivotY), 0), 1);
_bTransformNeeded = true;
}
2018-03-01 19:28:22 +08:00
void e2d::Node::setWidth(double width)
{
2018-05-08 17:40:36 +08:00
this->setSize(width, _fHeight);
2018-03-01 19:28:22 +08:00
}
void e2d::Node::setHeight(double height)
{
2018-05-08 17:40:36 +08:00
this->setSize(_fWidth, height);
2018-03-01 19:28:22 +08:00
}
void e2d::Node::setSize(double width, double height)
{
2018-05-08 17:40:36 +08:00
if (_fWidth == width && _fHeight == height)
2018-03-01 19:28:22 +08:00
return;
2018-05-08 17:40:36 +08:00
_fWidth = static_cast<float>(width);
_fHeight = static_cast<float>(height);
_bTransformNeeded = true;
2018-03-01 19:28:22 +08:00
}
void e2d::Node::setSize(Size size)
{
this->setSize(size.width, size.height);
}
void e2d::Node::setProperty(NodeProperty prop)
{
this->setVisiable(prop.visable);
this->setPos(prop.posX, prop.posY);
this->setSize(prop.width, prop.height);
this->setOpacity(prop.opacity);
this->setPivot(prop.pivotX, prop.pivotY);
this->setScale(prop.scaleX, prop.scaleY);
this->setRotation(prop.rotation);
this->setSkew(prop.skewAngleX, prop.skewAngleY);
}
void e2d::Node::setCollider(ColliderType nColliderType)
{
switch (nColliderType)
{
case ColliderType::RECT:
{
auto rect = new ColliderRect(this);
this->setCollider(rect);
break;
}
case ColliderType::CIRCLE:
{
auto rect = new ColliderCircle(this);
this->setCollider(rect);
break;
}
case ColliderType::ELLIPSE:
{
auto rect = new ColliderEllipse(this);
this->setCollider(rect);
break;
}
default:
break;
}
}
void e2d::Node::setCollider(Collider * pCollider)
{
// ɾ<><C9BE><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD>ײ<EFBFBD><D7B2>
2018-05-08 17:40:36 +08:00
ColliderManager::__removeCollider(_pCollider);
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>ײ<EFBFBD><D7B2>
ColliderManager::__addCollider(pCollider);
2017-10-29 23:48:32 +08:00
if (pCollider)
2017-10-28 18:48:21 +08:00
{
2017-10-29 23:48:32 +08:00
// ˫<><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-05-08 17:40:36 +08:00
this->_pCollider = pCollider;
pCollider->_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-05-08 17:40:36 +08:00
this->_pCollider = nullptr;
2017-10-28 18:48:21 +08:00
}
}
void e2d::Node::addColliableName(const String& collliderName)
{
unsigned int hash = collliderName.getHashCode();
2018-05-08 17:40:36 +08:00
_vColliders.insert(hash);
}
2018-04-24 20:22:41 +08:00
#ifdef HIGHER_THAN_VS2012
2018-05-08 11:37:11 +08:00
void e2d::Node::addColliableName(const std::initializer_list<String>& vCollliderName)
{
for (const auto &name : vCollliderName)
{
this->addColliableName(name);
}
}
2018-04-01 13:16:07 +08:00
#endif
void e2d::Node::removeColliableName(const String& collliderName)
{
unsigned int hash = collliderName.getHashCode();
2018-05-08 17:40:36 +08:00
_vColliders.erase(hash);
}
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)
{
2018-05-08 17:40:36 +08:00
ASSERT(child->_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
2018-05-08 17:40:36 +08:00
_vChildren.push_back(child);
2017-10-14 18:43:32 +08:00
child->setOrder(order);
child->retain();
2018-05-08 17:40:36 +08:00
child->_pParent = this;
2018-05-08 17:40:36 +08:00
if (this->_pParentScene)
2017-10-17 21:22:25 +08:00
{
2018-05-08 17:40:36 +08:00
child->_setParentScene(this->_pParentScene);
2017-10-17 21:22:25 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
child->_updateOpacity();
// <20><><EFBFBD>½ڵ<C2BD>ת<EFBFBD><D7AA>
2018-05-08 17:40:36 +08:00
child->_bTransformNeeded = true;
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
2018-05-08 17:40:36 +08:00
_bSortChildrenNeeded = true;
2017-10-14 11:40:47 +08:00
}
}
2018-04-24 20:22:41 +08:00
#ifdef HIGHER_THAN_VS2012
2018-05-08 11:37:11 +08:00
void e2d::Node::addChild(const std::initializer_list<Node*>& vNodes, int order)
{
for (const auto &node : vNodes)
{
this->addChild(node, order);
}
}
2018-04-01 13:16:07 +08:00
#endif
e2d::Node * e2d::Node::getParent() const
{
2018-05-08 17:40:36 +08:00
return _pParent;
}
e2d::Scene * e2d::Node::getParentScene() const
{
2018-05-08 17:40:36 +08:00
return _pParentScene;
}
std::vector<e2d::Node*> e2d::Node::getChildren(const String& name) const
2017-10-14 11:40:47 +08:00
{
std::vector<Node*> vChildren;
unsigned int hash = name.getHashCode();
2017-10-14 11:40:47 +08:00
2018-05-08 17:40:36 +08:00
for (auto child : _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-05-08 17:40:36 +08:00
if (child->_nHashName == hash && child->_sName == name)
{
vChildren.push_back(child);
}
2017-10-14 11:40:47 +08:00
}
return std::move(vChildren);
2017-10-14 11:40:47 +08:00
}
e2d::Node * e2d::Node::getChild(const String& name) const
2018-04-24 13:28:21 +08:00
{
unsigned int hash = name.getHashCode();
2018-05-08 17:40:36 +08:00
for (auto child : _vChildren)
2018-04-24 13:28:21 +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-05-08 17:40:36 +08:00
if (child->_nHashName == hash && child->_sName == name)
2018-04-24 13:28:21 +08:00
{
return child;
}
}
return nullptr;
}
std::vector<e2d::Node*> e2d::Node::getAllChildren() const
2018-02-03 22:04:43 +08:00
{
2018-05-08 17:40:36 +08:00
return _vChildren;
}
2018-02-03 22:04:43 +08:00
int e2d::Node::getChildrenCount() const
{
2018-05-08 17:40:36 +08:00
return static_cast<int>(_vChildren.size());
2018-02-03 22:04:43 +08:00
}
void e2d::Node::removeFromParent()
2017-10-14 11:40:47 +08:00
{
2018-05-08 17:40:36 +08:00
if (_pParent)
2017-10-14 18:43:32 +08:00
{
2018-05-08 17:40:36 +08:00
_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
2018-05-08 17:40:36 +08:00
if (_vChildren.empty())
2017-10-14 18:43:32 +08:00
{
2017-10-17 21:22:25 +08:00
return false;
2017-10-14 18:43:32 +08:00
}
if (child)
{
2018-05-08 17:40:36 +08:00
size_t size = _vChildren.size();
2017-12-15 23:15:06 +08:00
for (size_t i = 0; i < size; i++)
2017-10-14 18:43:32 +08:00
{
2018-05-08 17:40:36 +08:00
if (_vChildren[i] == child)
2017-10-14 18:43:32 +08:00
{
2018-05-08 17:40:36 +08:00
_vChildren.erase(_vChildren.begin() + i);
child->_pParent = nullptr;
2018-02-03 22:04:43 +08:00
2018-05-08 17:40:36 +08:00
if (child->_pParentScene)
2017-10-17 21:22:25 +08:00
{
child->_setParentScene(nullptr);
}
2018-02-03 22:04:43 +08:00
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
2018-05-08 17:40:36 +08:00
if (_vChildren.empty())
2017-10-14 18:43:32 +08:00
{
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
2018-05-08 17:40:36 +08:00
size_t size = _vChildren.size();
2017-12-15 23:15:06 +08:00
for (size_t i = 0; i < size; i++)
2017-10-14 18:43:32 +08:00
{
2018-05-08 17:40:36 +08:00
auto child = _vChildren[i];
if (child->_nHashName == hash && child->_sName == childName)
2017-10-14 18:43:32 +08:00
{
2018-05-08 17:40:36 +08:00
_vChildren.erase(_vChildren.begin() + i);
child->_pParent = nullptr;
if (child->_pParentScene)
2017-10-17 21:22:25 +08:00
{
child->_setParentScene(nullptr);
}
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-05-08 17:40:36 +08:00
for (auto child : _vChildren)
2017-10-17 21:22:25 +08:00
{
2018-02-03 22:04:43 +08:00
child->release();
2017-10-17 21:22:25 +08:00
}
// <20><><EFBFBD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-05-08 17:40:36 +08:00
_vChildren.clear();
2017-10-17 21:22:25 +08:00
}
void e2d::Node::runAction(ActionBase * action)
2017-10-19 00:50:04 +08:00
{
2018-02-06 15:34:47 +08:00
if (this != action->getTarget())
{
2018-05-08 17:40:36 +08:00
ASSERT(action->getTarget() == nullptr, "The action has already got a target!");
ActionManager::start(action, this, false);
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(const String& strActionName)
{
auto actions = ActionManager::get(strActionName);
2018-04-24 20:22:41 +08:00
for (auto action : actions)
{
2018-03-06 09:56:17 +08:00
if (action->getTarget() == this)
{
action->resume();
}
}
}
void e2d::Node::pauseAction(const String& strActionName)
2018-03-06 09:56:17 +08:00
{
auto actions = ActionManager::get(strActionName);
2018-04-24 20:22:41 +08:00
for (auto action : actions)
2018-03-06 09:56:17 +08:00
{
if (action->getTarget() == this)
{
action->pause();
}
}
}
void e2d::Node::stopAction(const String& strActionName)
2018-03-06 09:56:17 +08:00
{
auto actions = ActionManager::get(strActionName);
2018-04-24 20:22:41 +08:00
for (auto action : actions)
2018-03-06 09:56:17 +08:00
{
if (action->getTarget() == this)
{
action->stop();
}
}
}
e2d::ActionBase * e2d::Node::getAction(const String& strActionName)
2018-03-06 09:56:17 +08:00
{
auto actions = ActionManager::get(strActionName);
2018-04-24 20:22:41 +08:00
for (auto action : actions)
2018-03-06 09:56:17 +08:00
{
if (action->getTarget() == this)
{
return action;
}
}
2018-03-06 09:56:17 +08:00
return nullptr;
}
std::vector<e2d::ActionBase*> e2d::Node::getActions(const String& strActionName)
{
std::vector<ActionBase*>::iterator iter;
auto actions = ActionManager::get(strActionName);
2018-03-06 09:56:17 +08:00
for (iter = actions.begin(); iter != actions.end();)
{
2018-03-06 09:56:17 +08:00
if ((*iter)->getTarget() != this)
{
iter = actions.erase(iter);
}
else
{
iter++;
}
}
2018-03-06 09:56:17 +08:00
return std::move(actions);
}
2018-03-07 20:14:58 +08:00
bool e2d::Node::isPointIn(Point point) const
2017-10-21 19:09:31 +08:00
{
2018-03-07 20:14:58 +08:00
BOOL ret = 0;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><EFBFBD><E5A3AC><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2><EFBFBD>ж<EFBFBD>
2018-05-08 17:40:36 +08:00
if (_pCollider)
{
2018-05-08 17:40:36 +08:00
_pCollider->getD2dGeometry()->FillContainsPoint(
D2D1::Point2F(
static_cast<float>(point.x),
static_cast<float>(point.y)),
2018-05-08 17:40:36 +08:00
_MatriFinal,
&ret
);
}
else
{
// Ϊ<>ڵ㴴<DAB5><E3B4B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ײ<EFBFBD><D7B2>
ID2D1RectangleGeometry * rect;
Renderer::getID2D1Factory()->CreateRectangleGeometry(
2018-05-08 17:40:36 +08:00
D2D1::RectF(0, 0, _fWidth, _fHeight),
&rect
);
// <20>жϵ<D0B6><CFB5>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2><EFBFBD><EFBFBD>
rect->FillContainsPoint(
D2D1::Point2F(
static_cast<float>(point.x),
static_cast<float>(point.y)),
2018-05-08 17:40:36 +08:00
_MatriFinal,
&ret
);
// ɾ<><C9BE><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
SafeReleaseInterface(&rect);
}
2018-03-07 20:14:58 +08:00
if (ret)
2017-10-21 19:09:31 +08:00
{
return true;
}
2017-10-21 19:09:31 +08:00
return false;
}
2018-05-08 17:40:36 +08:00
bool e2d::Node::isIntersectWith(const Node * node) const
2018-03-06 09:56:17 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><EFBFBD><E5A3AC><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2><EFBFBD>ж<EFBFBD>
2018-05-08 17:40:36 +08:00
if (this->_pCollider && node->_pCollider)
{
2018-05-08 17:40:36 +08:00
Relation relation = this->_pCollider->getRelationWith(node->_pCollider);
if ((relation != Relation::UNKNOWN) &&
(relation != Relation::DISJOINT))
{
return true;
}
}
else
{
// Ϊ<>ڵ㴴<DAB5><E3B4B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ײ<EFBFBD><D7B2>
ID2D1RectangleGeometry * pRect1;
ID2D1RectangleGeometry * pRect2;
ID2D1TransformedGeometry * pCollider;
D2D1_GEOMETRY_RELATION relation;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сλ<D0A1>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Renderer::getID2D1Factory()->CreateRectangleGeometry(
2018-05-08 17:40:36 +08:00
D2D1::RectF(0, 0, _fWidth, _fHeight),
&pRect1
);
// <20><><EFBFBD>ݶ<EFBFBD>ά<EFBFBD><CEAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
Renderer::getID2D1Factory()->CreateTransformedGeometry(
pRect1,
2018-05-08 17:40:36 +08:00
_MatriFinal,
&pCollider
);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȽϽڵ<CFBD><DAB5>Ĵ<EFBFBD>Сλ<D0A1>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Renderer::getID2D1Factory()->CreateRectangleGeometry(
2018-05-08 17:40:36 +08:00
D2D1::RectF(0, 0, node->_fWidth, node->_fHeight),
&pRect2
);
// <20><>ȡ<EFBFBD>ཻ״̬
pCollider->CompareWithGeometry(
pRect2,
2018-05-08 17:40:36 +08:00
node->_MatriFinal,
&relation
);
// ɾ<><C9BE><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
SafeReleaseInterface(&pRect1);
SafeReleaseInterface(&pRect2);
SafeReleaseInterface(&pCollider);
2018-04-01 23:08:11 +08:00
if ((relation != D2D1_GEOMETRY_RELATION_UNKNOWN) &&
(relation != D2D1_GEOMETRY_RELATION_DISJOINT))
{
return true;
}
}
2018-03-06 09:56:17 +08:00
return false;
2018-03-06 09:56:17 +08:00
}
void e2d::Node::setAutoUpdate(bool bAutoUpdate)
2018-02-03 22:04:43 +08:00
{
2018-05-08 17:40:36 +08:00
_bAutoUpdate = bAutoUpdate;
2018-02-03 22:04:43 +08:00
}
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::setDefaultColliderEnable(bool enable)
2018-03-07 20:14:58 +08:00
{
s_fDefaultColliderEnabled = enable;
2018-03-07 20:14:58 +08:00
}
void e2d::Node::destroy()
{
ActionManager::__clearAllBindedWith(this);
2018-05-08 17:40:36 +08:00
ColliderManager::__removeCollider(_pCollider);
for (auto child : _vChildren)
{
SafeRelease(&child);
}
}
void e2d::Node::resumeAllActions()
2017-10-21 19:09:31 +08:00
{
ActionManager::__resumeAllBindedWith(this);
2017-10-21 19:09:31 +08:00
}
void e2d::Node::pauseAllActions()
2017-10-21 19:09:31 +08:00
{
ActionManager::__pauseAllBindedWith(this);
2017-10-21 19:09:31 +08:00
}
void e2d::Node::stopAllActions()
2017-10-21 19:09:31 +08:00
{
ActionManager::__stopAllBindedWith(this);
2017-10-21 19:09:31 +08:00
}
void e2d::Node::setVisiable(bool value)
{
2018-05-08 17:40:36 +08:00
_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-05-08 17:40:36 +08:00
if (!name.isEmpty() && _sName != name)
2017-10-14 11:40:47 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>
2018-05-08 17:40:36 +08:00
_sName = name;
2017-10-14 11:40:47 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> Hash <20><>
2018-05-08 17:40:36 +08:00
_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
{
2018-05-08 17:40:36 +08:00
_pParentScene = scene;
for (auto child : _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
}
}