Magic_Game/core/Node/Node.cpp

1025 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"
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)
, _posX(0)
, _posY(0)
, _width(0)
, _height(0)
, _scaleX(1.0f)
, _scaleY(1.0f)
, _rotation(0)
, _skewAngleX(0)
, _skewAngleY(0)
, _displayOpacity(1.0f)
, _realOpacity(1.0f)
, _pivotX(s_fDefaultPiovtX)
, _pivotY(s_fDefaultPiovtY)
, _initialMatri(D2D1::Matrix3x2F::Identity())
, _finalMatri(D2D1::Matrix3x2F::Identity())
, _visiable(true)
, _collider(nullptr)
, _parent(nullptr)
, _parentScene(nullptr)
, _hashName(0)
, _needSort(false)
, _needTransform(false)
, _autoUpdate(true)
, _positionFixed(false)
{
if (s_fDefaultColliderEnabled)
2018-03-07 20:14:58 +08:00
{
2018-05-19 01:10:37 +08:00
this->setCollider(GC::create<ColliderRect>(this));
2018-03-07 20:14:58 +08:00
}
}
e2d::Node::~Node()
{
}
void e2d::Node::_update()
2017-10-14 18:43:32 +08:00
{
if (_needTransform)
{
_updateTransform();
}
2017-10-14 18:43:32 +08:00
if (!_children.empty())
2017-10-14 18:43:32 +08:00
{
if (_needSort)
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(
std::begin(_children),
std::end(_children),
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
_needSort = false;
2018-02-03 22:04:43 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>
size_t size = _children.size();
2017-12-15 23:15:06 +08:00
size_t i;
2018-05-14 22:51:40 +08:00
for (i = 0; i < size; ++i)
2017-10-14 18:43:32 +08:00
{
auto child = _children[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-19 00:00:12 +08:00
if (_autoUpdate && !Game::isPaused())
2018-02-04 21:24:27 +08:00
{
2018-05-19 00:00:12 +08:00
this->onUpdate();
2018-02-03 22:04:43 +08:00
}
2018-05-19 00:00:12 +08:00
this->_fixedUpdate();
2017-10-14 18:43:32 +08:00
// <20><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ڵ<EFBFBD>
2018-05-14 22:51:40 +08:00
for (; i < size; ++i)
_children[i]->_update();
2017-10-14 18:43:32 +08:00
}
else
{
2018-05-19 00:00:12 +08:00
if (_autoUpdate && !Game::isPaused())
2018-02-04 21:24:27 +08:00
{
2018-05-19 00:00:12 +08:00
this->onUpdate();
2018-02-04 21:24:27 +08:00
}
2018-05-19 00:00:12 +08:00
this->_fixedUpdate();
2017-10-14 18:43:32 +08:00
}
}
void e2d::Node::_render()
2017-10-13 11:42:36 +08:00
{
if (!_visiable)
2018-01-30 16:45:38 +08:00
{
return;
}
if (!_children.empty())
2018-01-30 16:45:38 +08:00
{
size_t size = _children.size();
2018-01-30 16:45:38 +08:00
size_t i;
2018-05-14 22:51:40 +08:00
for (i = 0; i < size; ++i)
2018-01-30 16:45:38 +08:00
{
auto child = _children[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>
Renderer::getRenderTarget()->SetTransform(_finalMatri);
2018-01-30 16:45:38 +08:00
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
this->onRender();
// <20><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ڵ<EFBFBD>
2018-05-14 22:51:40 +08:00
for (; i < size; ++i)
_children[i]->_render();
2018-01-30 16:45:38 +08:00
}
else
{
// ת<><D7AA><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD>Ķ<EFBFBD>ά<EFBFBD><CEAC><EFBFBD><EFBFBD>
Renderer::getRenderTarget()->SetTransform(_finalMatri);
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>
if (_collider && _collider->_visiable)
2017-10-29 23:48:32 +08:00
{
_collider->_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>
for (auto child : _children)
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>
D2D1_POINT_2F pivot = { _width * _pivotX, _height * _pivotY };
// <20>任 Initial <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ㽫<DAB5><E3BDAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б
_initialMatri = D2D1::Matrix3x2F::Scale(
_scaleX,
_scaleY,
pivot
) * D2D1::Matrix3x2F::Skew(
_skewAngleX,
_skewAngleY,
pivot
2017-10-17 21:22:25 +08:00
) * D2D1::Matrix3x2F::Rotation(
_rotation,
pivot
) * D2D1::Matrix3x2F::Translation(
_posX,
_posY
);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5>任 Final <20><><EFBFBD><EFBFBD>
_finalMatri = _initialMatri * D2D1::Matrix3x2F::Translation(-pivot.x, -pivot.y);
// <20>͸<EFBFBD><CDB8>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (!_positionFixed && _parent)
{
_initialMatri = _initialMatri * _parent->_initialMatri;
_finalMatri = _finalMatri * _parent->_initialMatri;
}
}
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>
if (_collider)
2017-10-29 23:48:32 +08:00
{
_collider->_transform();
2017-10-29 23:48:32 +08:00
}
// <20><>־<EFBFBD><D6BE>ִ<EFBFBD>й<EFBFBD><D0B9>
_needTransform = false;
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>нڵ<D0BD>
for (auto child : this->_children)
{
child->_updateTransform();
}
}
2017-10-14 18:43:32 +08:00
void e2d::Node::_updateOpacity()
{
if (_parent)
{
_displayOpacity = _realOpacity * _parent->_displayOpacity;
}
for (auto child : _children)
{
child->_updateOpacity();
}
2017-10-14 18:43:32 +08:00
}
2018-05-19 00:00:12 +08:00
void e2d::Node::_fixedUpdate()
{
}
bool e2d::Node::isVisiable() const
2017-10-14 18:43:32 +08:00
{
return _visiable;
2017-10-14 18:43:32 +08:00
}
e2d::String e2d::Node::getName() const
2017-10-17 21:22:25 +08:00
{
return _name;
2017-10-17 21:22:25 +08:00
}
unsigned int e2d::Node::getHashName() const
{
return _hashName;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getPosX() const
2017-10-17 21:22:25 +08:00
{
return _posX;
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
{
return _posY;
2017-10-15 02:46:24 +08:00
}
e2d::Point e2d::Node::getPos() const
2017-10-15 02:46:24 +08:00
{
return Point(_posX, _posY);
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
{
return _width * _scaleX;
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
{
return _height * _scaleY;
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
{
return _width;
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
{
return _height;
2017-10-17 21:22:25 +08:00
}
e2d::Size e2d::Node::getRealSize() const
2017-10-17 21:22:25 +08:00
{
return Size(_width, _height);
2017-10-17 21:22:25 +08:00
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getPivotX() const
{
return _pivotX;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getPivotY() const
{
return _pivotY;
}
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
{
return _scaleX;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getScaleY() const
{
return _scaleY;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getSkewX() const
{
return _skewAngleX;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getSkewY() const
{
return _skewAngleY;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getRotation() const
{
return _rotation;
}
2018-02-27 21:07:43 +08:00
double e2d::Node::getOpacity() const
{
return _realOpacity;
2017-10-15 02:46:24 +08:00
}
e2d::Node::Property e2d::Node::getProperty() const
{
Property prop;
prop.visable = _visiable;
prop.posX = _posX;
prop.posY = _posY;
prop.width = _width;
prop.height = _height;
prop.opacity = _realOpacity;
prop.pivotX = _pivotX;
prop.pivotY = _pivotY;
prop.scaleX = _scaleX;
prop.scaleY = _scaleY;
prop.rotation = _rotation;
prop.skewAngleX = _skewAngleX;
prop.skewAngleY = _skewAngleY;
return prop;
}
e2d::Collider * e2d::Node::getCollider() const
2018-02-03 22:04:43 +08:00
{
return _collider;
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
{
this->setPos(x, _posY);
2017-10-17 21:22:25 +08:00
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPosY(double y)
{
this->setPos(_posX, 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)
{
if (_posX == x && _posY == y)
2017-10-14 18:43:32 +08:00
return;
_posX = float(x);
_posY = float(y);
_needTransform = true;
}
void e2d::Node::setPosFixed(bool fixed)
{
if (_positionFixed == fixed)
return;
_positionFixed = fixed;
_needTransform = 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)
{
this->setPos(_posX + x, _posY + 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)
{
this->setScale(scaleX, _scaleY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setScaleY(double scaleY)
{
this->setScale(_scaleX, 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)
{
if (_scaleX == scaleX && _scaleY == scaleY)
2017-10-15 02:46:24 +08:00
return;
_scaleX = float(scaleX);
_scaleY = float(scaleY);
_needTransform = true;
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setSkewX(double angleX)
{
this->setSkew(angleX, _skewAngleY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setSkewY(double angleY)
{
this->setSkew(_skewAngleX, angleY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setSkew(double angleX, double angleY)
{
if (_skewAngleX == angleX && _skewAngleY == angleY)
2017-10-15 02:46:24 +08:00
return;
_skewAngleX = float(angleX);
_skewAngleY = float(angleY);
_needTransform = 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 (_rotation == angle)
2017-10-15 02:46:24 +08:00
return;
_rotation = float(angle);
_needTransform = 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 (_realOpacity == opacity)
return;
_displayOpacity = _realOpacity = min(max(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)
{
this->setPivot(pivotX, _pivotY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPivotY(double pivotY)
{
this->setPivot(_pivotX, pivotY);
}
2018-02-27 21:07:43 +08:00
void e2d::Node::setPivot(double pivotX, double pivotY)
{
if (_pivotX == pivotX && _pivotY == pivotY)
return;
_pivotX = min(max(float(pivotX), 0), 1);
_pivotY = min(max(float(pivotY), 0), 1);
_needTransform = true;
}
2018-03-01 19:28:22 +08:00
void e2d::Node::setWidth(double width)
{
this->setSize(width, _height);
2018-03-01 19:28:22 +08:00
}
void e2d::Node::setHeight(double height)
{
this->setSize(_width, height);
2018-03-01 19:28:22 +08:00
}
void e2d::Node::setSize(double width, double height)
{
if (_width == width && _height == height)
2018-03-01 19:28:22 +08:00
return;
_width = float(width);
_height = float(height);
_needTransform = 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(Property 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(Collider::Type type)
{
switch (type)
{
case Collider::Type::RECT:
{
2018-05-19 01:10:37 +08:00
this->setCollider(GC::create<ColliderRect>(this));
break;
}
case Collider::Type::CIRCLE:
{
2018-05-19 01:10:37 +08:00
this->setCollider(GC::create<ColliderCircle>(this));
break;
}
case Collider::Type::ELLIPSE:
{
2018-05-19 01:10:37 +08:00
this->setCollider(GC::create<ColliderEllipse>(this));
break;
}
default:
break;
}
}
void e2d::Node::setCollider(Collider * pCollider)
{
// ɾ<><C9BE><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD>ײ<EFBFBD><D7B2>
ColliderManager::__removeCollider(_collider);
// <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>
this->_collider = pCollider;
pCollider->_parentNode = 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
{
this->_collider = nullptr;
2017-10-28 18:48:21 +08:00
}
}
void e2d::Node::addColliableName(const String& collliderName)
{
unsigned int hash = collliderName.getHashCode();
_colliders.insert(hash);
}
void e2d::Node::addColliableName(const std::vector<String>& colliderNames)
{
2018-05-10 00:58:43 +08:00
for (const auto &name : colliderNames)
{
this->addColliableName(name);
}
}
void e2d::Node::removeColliableName(const String& collliderName)
{
unsigned int hash = collliderName.getHashCode();
_colliders.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)
{
ASSERT(child->_parent == 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
_children.push_back(child);
2017-10-14 18:43:32 +08:00
child->setOrder(order);
child->retain();
child->_parent = this;
if (this->_parentScene)
2017-10-17 21:22:25 +08:00
{
child->_setParentScene(this->_parentScene);
2017-10-17 21:22:25 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
child->_updateOpacity();
// <20><><EFBFBD>½ڵ<C2BD>ת<EFBFBD><D7AA>
child->_needTransform = true;
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
_needSort = true;
2017-10-14 11:40:47 +08:00
}
}
void e2d::Node::addChild(const std::vector<Node*>& nodes, int order)
{
for (auto node : nodes)
{
this->addChild(node, order);
}
}
e2d::Node * e2d::Node::getParent() const
{
return _parent;
}
e2d::Scene * e2d::Node::getParentScene() const
{
return _parentScene;
}
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
for (auto child : _children)
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>
if (child->_hashName == hash && child->_name == 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();
for (auto child : _children)
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>
if (child->_hashName == hash && child->_name == name)
2018-04-24 13:28:21 +08:00
{
return child;
}
}
return nullptr;
}
2018-05-10 14:16:36 +08:00
const std::vector<e2d::Node*>& e2d::Node::getAllChildren() const
2018-02-03 22:04:43 +08:00
{
return _children;
}
2018-02-03 22:04:43 +08:00
int e2d::Node::getChildrenCount() const
{
return static_cast<int>(_children.size());
2018-02-03 22:04:43 +08:00
}
void e2d::Node::removeFromParent()
2017-10-14 11:40:47 +08:00
{
if (_parent)
2017-10-14 18:43:32 +08:00
{
_parent->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 (_children.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)
{
size_t size = _children.size();
2018-05-14 22:51:40 +08:00
for (size_t i = 0; i < size; ++i)
2017-10-14 18:43:32 +08:00
{
if (_children[i] == child)
2017-10-14 18:43:32 +08:00
{
_children.erase(_children.begin() + i);
child->_parent = nullptr;
2018-02-03 22:04:43 +08:00
if (child->_parentScene)
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
if (_children.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
size_t size = _children.size();
2018-05-14 22:51:40 +08:00
for (size_t i = 0; i < size; ++i)
2017-10-14 18:43:32 +08:00
{
auto child = _children[i];
if (child->_hashName == hash && child->_name == childName)
2017-10-14 18:43:32 +08:00
{
_children.erase(_children.begin() + i);
child->_parent = nullptr;
if (child->_parentScene)
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>һ
for (auto child : _children)
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>
_children.clear();
2017-10-17 21:22:25 +08:00
}
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())
{
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::Action * 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::Action*> e2d::Node::getActions(const String& strActionName)
{
std::vector<Action*>::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
{
2018-05-14 22:51:40 +08:00
++iter;
2018-03-06 09:56:17 +08:00
}
}
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>
if (_collider)
{
_collider->getD2dGeometry()->FillContainsPoint(
D2D1::Point2F(
float(point.x),
float(point.y)),
_finalMatri,
&ret
);
}
else
{
// Ϊ<>ڵ㴴<DAB5><E3B4B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ײ<EFBFBD><D7B2>
ID2D1RectangleGeometry * rect;
Renderer::getID2D1Factory()->CreateRectangleGeometry(
D2D1::RectF(0, 0, _width, _height),
&rect
);
// <20>жϵ<D0B6><CFB5>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2><EFBFBD><EFBFBD>
rect->FillContainsPoint(
D2D1::Point2F(
float(point.x),
float(point.y)),
_finalMatri,
&ret
);
// ɾ<><C9BE><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
2018-05-10 20:23:32 +08:00
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;
}
bool e2d::Node::isOverlappedWith(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>
if (this->_collider && node->_collider)
{
Collider::Relation relation = this->_collider->getRelationWith(node->_collider);
if ((relation != Collider::Relation::UNKNOWN) &&
(relation != Collider::Relation::DISJOIN))
{
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(
D2D1::RectF(0, 0, _width, _height),
&pRect1
);
// <20><><EFBFBD>ݶ<EFBFBD>ά<EFBFBD><CEAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
Renderer::getID2D1Factory()->CreateTransformedGeometry(
pRect1,
_finalMatri,
&pCollider
);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȽϽڵ<CFBD><DAB5>Ĵ<EFBFBD>Сλ<D0A1>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Renderer::getID2D1Factory()->CreateRectangleGeometry(
D2D1::RectF(0, 0, node->_width, node->_height),
&pRect2
);
// <20><>ȡ<EFBFBD>ཻ״̬
pCollider->CompareWithGeometry(
pRect2,
node->_finalMatri,
&relation
);
// ɾ<><C9BE><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
2018-05-10 20:23:32 +08:00
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
{
_autoUpdate = 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)
{
s_fDefaultPiovtX = min(max(float(defaultPiovtX), 0), 1);
s_fDefaultPiovtY = min(max(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::onDestroy()
{
ActionManager::__clearAllBindedWith(this);
ColliderManager::__removeCollider(_collider);
for (auto child : _children)
{
2018-05-19 01:10:37 +08:00
GC::release(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)
{
_visiable = 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
if (!name.isEmpty() && _name != name)
2017-10-14 11:40:47 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>
_name = name;
2017-10-14 11:40:47 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> Hash <20><>
_hashName = 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
{
_parentScene = scene;
for (auto child : _children)
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
}
}