Magic_Game/core/Node/Node.cpp

975 lines
18 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dnode.h"
2018-08-14 00:41:59 +08:00
#include "..\e2devent.h"
2018-04-21 21:24:46 +08:00
#include "..\e2dmanager.h"
#include "..\e2daction.h"
const e2d::Node::Property e2d::Node::Property::Origin;
e2d::Node::Property e2d::Node::Property::operator+(Property const & prop) const
{
Property result;
result.position = this->position + prop.position;
result.size = this->size + prop.size;
result.anchorX = this->anchorX + prop.anchorX;
result.anchorY = this->anchorY + prop.anchorY;
result.scaleX = this->scaleX + prop.scaleX;
result.scaleY = this->scaleY + prop.scaleY;
result.rotation = this->rotation + prop.rotation;
result.skewAngleX = this->skewAngleX + prop.skewAngleX;
result.skewAngleY = this->skewAngleY + prop.skewAngleY;
return std::move(result);
}
e2d::Node::Property e2d::Node::Property::operator-(Property const & prop) const
{
Property result;
result.position = this->position - prop.position;
result.size = this->size - prop.size;
result.anchorX = this->anchorX - prop.anchorX;
result.anchorY = this->anchorY - prop.anchorY;
result.scaleX = this->scaleX - prop.scaleX;
result.scaleY = this->scaleY - prop.scaleY;
result.rotation = this->rotation - prop.rotation;
result.skewAngleX = this->skewAngleX - prop.skewAngleX;
result.skewAngleY = this->skewAngleY - prop.skewAngleY;
return std::move(result);
}
e2d::Node::Node()
: _order(0)
, _position()
, _size()
, _scaleX(1.0f)
, _scaleY(1.0f)
, _rotation(0)
, _skewAngleX(0)
, _skewAngleY(0)
2018-07-04 17:00:21 +08:00
, _displayOpacity(1.f)
, _realOpacity(1.f)
, _anchorX(0.f)
, _anchorY(0.f)
, _initialMatri(D2D1::Matrix3x2F::Identity())
, _finalMatri(D2D1::Matrix3x2F::Identity())
2018-07-07 01:48:39 +08:00
, _visible(true)
, _parent(nullptr)
, _parentScene(nullptr)
, _hashName(0)
, _clipEnabled(false)
, _needSort(false)
, _needTransform(false)
, _positionFixed(false)
2018-07-07 18:04:18 +08:00
, _collider(this)
, _extrapolate(Property::Origin)
{
}
e2d::Node::~Node()
{
ActionManager::instance()->clearAllBindedWith(this);
2018-08-12 14:30:28 +08:00
for (const auto& child : _children)
2018-07-07 01:43:41 +08:00
{
GC::instance()->safeRelease(child);
2018-07-07 01:43:41 +08:00
}
}
2018-08-19 15:11:20 +08:00
void e2d::Node::visit(Game * game)
2017-10-13 11:42:36 +08:00
{
2018-07-07 01:48:39 +08:00
if (!_visible)
2018-01-30 16:45:38 +08:00
return;
if (!game->paused())
2018-08-19 17:46:37 +08:00
{
auto updatableNode = dynamic_cast<Updatable*>(this);
if (updatableNode)
{
updatableNode->update();
}
2018-08-19 17:46:37 +08:00
// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
_updateTransform();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
_extrapolate = this->properties();
2018-08-19 17:46:37 +08:00
}
2018-07-13 01:59:04 +08:00
auto renderer = game->renderer();
auto renderTarget = renderer->renderTarget();
if (_clipEnabled)
{
2018-08-19 15:11:20 +08:00
renderTarget->SetTransform(_finalMatri);
renderTarget->PushAxisAlignedClip(
D2D1::RectF(0, 0, _size.width, _size.height),
D2D1_ANTIALIAS_MODE_PER_PRIMITIVE
);
}
if (_children.empty())
{
2018-08-19 17:46:37 +08:00
auto drawableNode = dynamic_cast<Drawable*>(this);
if (drawableNode)
{
renderTarget->SetTransform(_finalMatri);
drawableNode->draw(renderer);
}
}
else
2018-01-30 16:45:38 +08:00
{
// <20>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
_sortChildren();
2018-01-30 16:45:38 +08:00
size_t i;
2018-07-13 00:45:39 +08:00
for (i = 0; i < _children.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->order() < 0)
2018-01-30 16:45:38 +08:00
{
2018-08-19 15:11:20 +08:00
child->visit(game);
2018-01-30 16:45:38 +08:00
}
else
{
break;
}
}
2018-08-19 17:46:37 +08:00
auto drawableNode = dynamic_cast<Drawable*>(this);
if (drawableNode)
{
renderTarget->SetTransform(_finalMatri);
drawableNode->draw(renderer);
}
2018-01-30 16:45:38 +08:00
// <20><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ڵ<EFBFBD>
2018-07-13 00:45:39 +08:00
for (; i < _children.size(); ++i)
2018-08-19 15:11:20 +08:00
_children[i]->visit(game);
2018-01-30 16:45:38 +08:00
}
if (_clipEnabled)
{
2018-08-19 15:11:20 +08:00
renderTarget->PopAxisAlignedClip();
}
}
2018-08-15 00:06:03 +08:00
void e2d::Node::drawOutline(Renderer * renderer)
2018-07-07 18:04:18 +08:00
{
if (_visible)
2018-07-07 18:04:18 +08:00
{
renderer->renderTarget()->SetTransform(_finalMatri);
renderer->renderTarget()->DrawRectangle(
D2D1::RectF(0, 0, _size.width, _size.height),
renderer->solidBrush(),
1.5f
);
2018-07-07 18:04:18 +08:00
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-08-12 14:30:28 +08:00
for (const auto& child : _children)
{
2018-08-15 00:06:03 +08:00
child->drawOutline(renderer);
}
2018-07-07 18:04:18 +08:00
}
}
2018-08-15 00:06:03 +08:00
void e2d::Node::drawCollider()
2017-10-29 23:48:32 +08:00
{
if (_visible)
{
_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>
2018-08-12 14:30:28 +08:00
for (const auto& child : _children)
{
2018-08-15 00:06:03 +08:00
child->drawCollider();
}
2017-10-29 23:48:32 +08:00
}
}
2018-08-19 17:46:37 +08:00
void e2d::Node::_updateTransform()
{
if (!_needTransform)
return;
2018-07-13 01:59:04 +08:00
_needTransform = false;
2018-07-13 00:45:39 +08:00
// <20><><EFBFBD><EFBFBD>ê<EFBFBD><C3AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
D2D1_POINT_2F anchor = { _size.width * _anchorX, _size.height * _anchorY };
2018-07-13 01:59:04 +08:00
// <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,
anchor
2018-07-13 01:59:04 +08:00
) * D2D1::Matrix3x2F::Skew(
_skewAngleX,
_skewAngleY,
anchor
2018-07-13 01:59:04 +08:00
) * D2D1::Matrix3x2F::Rotation(
_rotation,
anchor
2018-07-13 01:59:04 +08:00
) * D2D1::Matrix3x2F::Translation(
_position.x,
_position.y
2018-07-13 01:59:04 +08:00
);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ê<EFBFBD><C3AA><EFBFBD>任 Final <20><><EFBFBD><EFBFBD>
_finalMatri = _initialMatri * D2D1::Matrix3x2F::Translation(-anchor.x, -anchor.y);
2018-07-13 01:59:04 +08:00
// <20>͸<EFBFBD><CDB8>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (!_positionFixed && _parent)
2018-07-13 00:45:39 +08:00
{
2018-07-13 01:59:04 +08:00
_initialMatri = _initialMatri * _parent->_initialMatri;
_finalMatri = _finalMatri * _parent->_initialMatri;
2018-07-13 00:45:39 +08:00
}
// ֪ͨ<CDA8>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
2018-08-12 14:30:28 +08:00
for (const auto& child : _children)
2018-07-13 00:45:39 +08:00
{
child->_needTransform = true;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
_collider.recreate();
if (_collider.enabled() &&
_collider.notify() &&
_collider.shape() != Collider::Shape::None)
{
CollisionManager::instance()->__updateCollider(&_collider);
}
2018-07-13 00:45:39 +08:00
}
bool e2d::Node::dispatch(const MouseEvent & e, bool handled)
{
2018-08-12 14:30:28 +08:00
if (_visible)
{
for (auto riter = _children.crbegin(); riter != _children.crend(); ++riter)
handled = (*riter)->dispatch(e, handled);
2018-08-19 17:46:37 +08:00
auto handler = dynamic_cast<MouseEventHandler*>(this);
if (handler)
handler->handle(e);
2018-08-12 14:30:28 +08:00
}
return handled;
}
bool e2d::Node::dispatch(const KeyEvent & e, bool handled)
{
2018-08-12 14:30:28 +08:00
if (_visible)
{
for (auto riter = _children.crbegin(); riter != _children.crend(); ++riter)
handled = (*riter)->dispatch(e, handled);
2018-08-19 17:46:37 +08:00
auto handler = dynamic_cast<KeyEventHandler*>(this);
if (handler)
handler->handle(e);
2018-08-12 14:30:28 +08:00
}
return handled;
}
void e2d::Node::_sortChildren()
{
if (_needSort)
{
std::sort(
std::begin(_children),
std::end(_children),
[](Node * n1, Node * n2) { return n1->order() < n2->order(); }
);
_needSort = false;
}
}
2017-10-14 18:43:32 +08:00
void e2d::Node::_updateOpacity()
{
if (_parent)
{
_displayOpacity = _realOpacity * _parent->_displayOpacity;
}
2018-08-12 14:30:28 +08:00
for (const auto& child : _children)
{
child->_updateOpacity();
}
2017-10-14 18:43:32 +08:00
}
bool e2d::Node::visible() const
2017-10-14 18:43:32 +08:00
{
2018-07-07 01:48:39 +08:00
return _visible;
2017-10-14 18:43:32 +08:00
}
const e2d::String& e2d::Node::name() const
2017-10-17 21:22:25 +08:00
{
return _name;
2017-10-17 21:22:25 +08:00
}
size_t e2d::Node::hashName() const
{
return _hashName;
}
float e2d::Node::positionX() const
2017-10-17 21:22:25 +08:00
{
return _position.x;
2017-10-17 21:22:25 +08:00
}
float e2d::Node::positionY() const
2017-10-15 02:46:24 +08:00
{
return _position.y;
2017-10-15 02:46:24 +08:00
}
const e2d::Point& e2d::Node::position() const
2017-10-15 02:46:24 +08:00
{
return _position;
2017-10-15 02:46:24 +08:00
}
float e2d::Node::width() const
2017-10-14 18:43:32 +08:00
{
return _size.width * _scaleX;
2017-10-14 18:43:32 +08:00
}
float e2d::Node::height() const
2017-10-14 18:43:32 +08:00
{
return _size.height * _scaleY;
2017-10-17 21:22:25 +08:00
}
e2d::Size e2d::Node::size() const
2017-10-17 21:22:25 +08:00
{
return Size(width(), height());
2017-10-17 21:22:25 +08:00
}
float e2d::Node::realWidth() const
2017-10-17 21:22:25 +08:00
{
return _size.width;
2017-10-17 21:22:25 +08:00
}
float e2d::Node::realHeight() const
2017-10-17 21:22:25 +08:00
{
return _size.height;
2017-10-17 21:22:25 +08:00
}
const e2d::Size& e2d::Node::realSize() const
{
return _size;
}
float e2d::Node::anchorX() const
{
return _anchorX;
}
float e2d::Node::anchorY() const
2017-10-17 21:22:25 +08:00
{
return _anchorY;
}
float e2d::Node::scaleX() const
{
return _scaleX;
}
float e2d::Node::scaleY() const
{
return _scaleY;
}
float e2d::Node::skewX() const
{
return _skewAngleX;
}
float e2d::Node::skewY() const
{
return _skewAngleY;
}
float e2d::Node::rotation() const
{
return _rotation;
}
float e2d::Node::opacity() const
{
return _realOpacity;
2017-10-15 02:46:24 +08:00
}
e2d::Node::Property e2d::Node::properties() const
{
Property prop;
prop.position = _position;
prop.size = _size;
prop.anchorX = _anchorX;
prop.anchorY = _anchorY;
prop.scaleX = _scaleX;
prop.scaleY = _scaleY;
prop.rotation = _rotation;
prop.skewAngleX = _skewAngleX;
prop.skewAngleY = _skewAngleY;
return std::move(prop);
}
e2d::Node::Property e2d::Node::extrapolate() const
2018-07-13 00:45:39 +08:00
{
return this->properties() - _extrapolate;
2018-07-13 00:45:39 +08:00
}
e2d::Collider* e2d::Node::collider()
2018-02-03 22:04:43 +08:00
{
return &_collider;
2018-02-03 22:04:43 +08:00
}
int e2d::Node::order() const
2017-10-15 02:46:24 +08:00
{
return _order;
2017-10-15 02:46:24 +08:00
}
e2d::Node& e2d::Node::order(int order)
2017-10-15 02:46:24 +08:00
{
if (_order == order)
return *this;
_order = order;
if (_parent)
{
_parent->_needSort = true;
}
return *this;
}
e2d::Node& e2d::Node::positionX(float x)
2017-10-17 21:22:25 +08:00
{
return position(x, _position.y);
2017-10-17 21:22:25 +08:00
}
e2d::Node& e2d::Node::positionY(float y)
{
return position(_position.x, y);
}
e2d::Node& e2d::Node::position(const Point & p)
{
return position(p.x, p.y);
}
e2d::Node& e2d::Node::position(float x, float y)
{
if (_position.x == x && _position.y == y)
return *this;
2017-10-14 18:43:32 +08:00
_position.x = x;
_position.y = y;
_needTransform = true;
return *this;
}
e2d::Node& e2d::Node::positionFixed(bool fixed)
{
if (_positionFixed == fixed)
return *this;
_positionFixed = fixed;
_needTransform = true;
return *this;
}
e2d::Node& e2d::Node::move(float x, float y)
{
return position(_position.x + x, _position.y + y);
2017-10-17 21:22:25 +08:00
}
e2d::Node& e2d::Node::move(const Vector2 & v)
2017-10-17 21:22:25 +08:00
{
return move(v.x, v.y);
}
e2d::Node& e2d::Node::scaleX(float scaleX)
{
return scale(scaleX, _scaleY);
}
e2d::Node& e2d::Node::scaleY(float scaleY)
{
return scale(_scaleX, scaleY);
}
e2d::Node& e2d::Node::scale(float scale0)
{
return scale(scale0, scale0);
}
e2d::Node& e2d::Node::scale(float scaleX, float scaleY)
{
if (_scaleX == scaleX && _scaleY == scaleY)
return *this;
2017-10-15 02:46:24 +08:00
2018-07-28 20:06:27 +08:00
_scaleX = scaleX;
_scaleY = scaleY;
_needTransform = true;
return *this;
}
e2d::Node& e2d::Node::skewX(float angleX)
{
return skew(angleX, _skewAngleY);
}
e2d::Node& e2d::Node::skewY(float angleY)
{
return skew(_skewAngleX, angleY);
}
e2d::Node& e2d::Node::skew(float angleX, float angleY)
{
if (_skewAngleX == angleX && _skewAngleY == angleY)
return *this;
2017-10-15 02:46:24 +08:00
2018-07-28 20:06:27 +08:00
_skewAngleX = angleX;
_skewAngleY = angleY;
_needTransform = true;
return *this;
2017-10-15 02:46:24 +08:00
}
e2d::Node& e2d::Node::rotation(float angle)
2017-10-15 02:46:24 +08:00
{
if (_rotation == angle)
return *this;
2017-10-15 02:46:24 +08:00
2018-07-28 20:06:27 +08:00
_rotation = angle;
_needTransform = true;
return *this;
2017-10-15 02:46:24 +08:00
}
e2d::Node& e2d::Node::opacity(float opacity)
2017-10-15 02:46:24 +08:00
{
if (_realOpacity == opacity)
return *this;
2018-07-28 20:06:27 +08:00
_displayOpacity = _realOpacity = std::min(std::max(opacity, 0.f), 1.f);
2017-10-21 19:09:31 +08:00
// <20><><EFBFBD>½ڵ<C2BD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
_updateOpacity();
return *this;
}
e2d::Node& e2d::Node::anchorX(float anchorX)
{
return anchor(anchorX, _anchorY);
}
e2d::Node& e2d::Node::anchorY(float anchorY)
{
return anchor(_anchorX, anchorY);
}
e2d::Node& e2d::Node::anchor(float anchorX, float anchorY)
{
if (_anchorX == anchorX && _anchorY == anchorY)
return *this;
_anchorX = std::min(std::max(anchorX, 0.f), 1.f);
_anchorY = std::min(std::max(anchorY, 0.f), 1.f);
_needTransform = true;
return *this;
}
e2d::Node& e2d::Node::width(float width)
2018-03-01 19:28:22 +08:00
{
return size(width, _size.height);
2018-03-01 19:28:22 +08:00
}
e2d::Node& e2d::Node::height(float height)
2018-03-01 19:28:22 +08:00
{
return size(_size.width, height);
2018-03-01 19:28:22 +08:00
}
e2d::Node& e2d::Node::size(float width, float height)
2018-03-01 19:28:22 +08:00
{
if (_size.width == width && _size.height == height)
return *this;
2018-03-01 19:28:22 +08:00
_size.width = width;
_size.height = height;
_needTransform = true;
return *this;
2018-03-01 19:28:22 +08:00
}
e2d::Node& e2d::Node::size(Size size0)
2018-03-01 19:28:22 +08:00
{
return size(size0.width, size0.height);
2018-03-01 19:28:22 +08:00
}
e2d::Node& e2d::Node::properties(Property prop)
{
this->position(prop.position.x, prop.position.y);
this->size(prop.size.width, prop.size.height);
this->anchor(prop.anchorX, prop.anchorY);
this->scale(prop.scaleX, prop.scaleY);
this->rotation(prop.rotation);
this->skew(prop.skewAngleX, prop.skewAngleY);
return *this;
}
e2d::Node& e2d::Node::clipEnabled(bool enabled)
{
_clipEnabled = enabled;
return *this;
}
e2d::Node& 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-24 00:58:16 +08:00
if (child->_parent != nullptr)
{
2018-08-15 00:06:03 +08:00
throw Exception("<EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD><EFBFBD>ڵ<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>");
2018-05-24 00:58:16 +08:00
}
2018-02-03 22:04:43 +08:00
for (Node * parent = this; parent != nullptr; parent = parent->parent())
2017-10-14 11:40:47 +08:00
{
2018-05-24 00:58:16 +08:00
if (child == parent)
{
2018-08-15 00:06:03 +08:00
throw Exception("һ<EFBFBD><EFBFBD><EFBFBD>ڵ㲻<EFBFBD><EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD>ĸ<EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<EFBFBD>");
2018-05-24 00:58:16 +08:00
}
2017-10-14 11:40:47 +08:00
}
2017-10-14 18:43:32 +08:00
2018-07-22 21:22:27 +08:00
child->retain();
_children.push_back(child);
child->order(order);
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
}
return *this;
2017-10-14 11:40:47 +08:00
}
e2d::Node& e2d::Node::addChild(const std::vector<Node*>& nodes, int order)
{
2018-08-12 14:30:28 +08:00
for (const auto& node : nodes)
{
this->addChild(node, order);
}
return *this;
}
e2d::Node * e2d::Node::parent() const
{
return _parent;
}
e2d::Scene * e2d::Node::parentScene() const
{
return _parentScene;
}
std::vector<e2d::Node*> e2d::Node::children(const String& name) const
2017-10-14 11:40:47 +08:00
{
std::vector<Node*> vChildren;
2018-08-19 15:11:20 +08:00
size_t hash = name.hash();
2017-10-14 11:40:47 +08:00
2018-08-12 14:30:28 +08:00
for (const 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::child(const String& name) const
2018-04-24 13:28:21 +08:00
{
2018-08-19 15:11:20 +08:00
size_t hash = name.hash();
2018-04-24 13:28:21 +08:00
2018-08-12 14:30:28 +08:00
for (const 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;
}
const std::vector<e2d::Node*>& e2d::Node::children() const
2018-02-03 22:04:43 +08:00
{
return _children;
}
2018-02-03 22:04:43 +08:00
int e2d::Node::childrenCount() const
{
return static_cast<int>(_children.size());
2018-02-03 22:04:43 +08:00
}
e2d::Node& 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
}
return *this;
2017-10-14 11:40:47 +08:00
}
e2d::Node& 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
{
return *this;
2017-10-14 18:43:32 +08:00
}
if (child)
{
2018-05-21 23:04:58 +08:00
auto iter = std::find(_children.begin(), _children.end(), child);
if (iter != _children.end())
2017-10-14 18:43:32 +08:00
{
2018-05-21 23:04:58 +08:00
_children.erase(iter);
child->_parent = nullptr;
2018-02-03 22:04:43 +08:00
2018-05-21 23:04:58 +08:00
if (child->_parentScene)
{
child->_setParentScene(nullptr);
2017-10-14 18:43:32 +08:00
}
2018-05-21 23:04:58 +08:00
2018-07-22 21:22:27 +08:00
child->release();
return *this;
2017-10-14 18:43:32 +08:00
}
}
return *this;
2017-10-14 11:40:47 +08:00
}
e2d::Node& e2d::Node::removeChildren(const String& childName)
2017-10-14 11:40:47 +08:00
{
WARN_IF(childName.empty(), "Invalid Node name.");
2017-10-14 18:43:32 +08:00
if (_children.empty())
2017-10-14 18:43:32 +08:00
{
return *this;
2017-10-14 18:43:32 +08:00
}
2017-10-17 21:22:25 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Hash ֵ
2018-08-19 15:11:20 +08:00
size_t hash = childName.hash();
2017-10-17 21:22:25 +08:00
2018-08-12 15:38:02 +08:00
auto iter = std::find_if(
_children.begin(),
_children.end(),
[childName, hash](Node* child) ->bool { return child->_hashName == hash && child->_name == childName; }
);
if (iter != _children.end())
2017-10-14 18:43:32 +08:00
{
2018-08-12 15:38:02 +08:00
(*iter)->_parent = nullptr;
if ((*iter)->_parentScene)
2017-10-14 18:43:32 +08:00
{
2018-08-12 15:38:02 +08:00
(*iter)->_setParentScene(nullptr);
2017-10-14 18:43:32 +08:00
}
2018-08-12 15:38:02 +08:00
(*iter)->release();
_children.erase(iter);
2017-10-14 18:43:32 +08:00
}
return *this;
2017-10-14 11:40:47 +08:00
}
e2d::Node& e2d::Node::removeAllChildren()
2017-10-17 21:22:25 +08:00
{
// <20><><EFBFBD>нڵ<D0BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD>һ
2018-08-12 14:30:28 +08:00
for (const auto& child : _children)
2017-10-17 21:22:25 +08:00
{
2018-07-22 21:22:27 +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();
return *this;
2017-10-17 21:22:25 +08:00
}
e2d::Node& e2d::Node::runAction(Action * action)
2017-10-19 00:50:04 +08:00
{
ActionManager::instance()->start(action, this, false);
return *this;
2017-10-19 00:50:04 +08:00
}
e2d::Node& e2d::Node::resumeAction(const String& name)
{
auto& actions = ActionManager::instance()->actions(name);
2018-08-12 14:30:28 +08:00
for (const auto& action : actions)
{
if (action->target() == this)
2018-03-06 09:56:17 +08:00
{
action->resume();
}
}
return *this;
2018-03-06 09:56:17 +08:00
}
e2d::Node& e2d::Node::pauseAction(const String& name)
2018-03-06 09:56:17 +08:00
{
auto& actions = ActionManager::instance()->actions(name);
2018-08-12 14:30:28 +08:00
for (const auto& action : actions)
2018-03-06 09:56:17 +08:00
{
if (action->target() == this)
2018-03-06 09:56:17 +08:00
{
action->pause();
}
}
return *this;
2018-03-06 09:56:17 +08:00
}
e2d::Node& e2d::Node::stopAction(const String& name)
2018-03-06 09:56:17 +08:00
{
auto& actions = ActionManager::instance()->actions(name);
2018-08-12 14:30:28 +08:00
for (const auto& action : actions)
2018-03-06 09:56:17 +08:00
{
if (action->target() == this)
2018-03-06 09:56:17 +08:00
{
action->stop();
}
}
return *this;
2018-03-06 09:56:17 +08:00
}
2018-07-07 18:04:18 +08:00
bool e2d::Node::containsPoint(const Point& point)
2017-10-21 19:09:31 +08:00
{
// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-08-19 17:46:37 +08:00
_updateTransform();
// Ϊ<>ڵ㴴<DAB5><E3B4B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
BOOL ret = 0;
ID2D1RectangleGeometry * rectGeo = nullptr;
auto factory = Game::instance()->renderer()->factory();
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
factory->CreateRectangleGeometry(
D2D1::RectF(0, 0, _size.width, _size.height),
2018-08-15 00:06:03 +08:00
&rectGeo
)
);
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
rectGeo->FillContainsPoint(
2018-07-28 20:06:27 +08:00
D2D1::Point2F(point.x, point.y),
_finalMatri,
&ret
2018-08-15 00:06:03 +08:00
)
);
SafeRelease(rectGeo);
return ret != 0;
2017-10-21 19:09:31 +08:00
}
2018-07-07 18:04:18 +08:00
bool e2d::Node::intersects(Node * node)
2018-03-06 09:56:17 +08:00
{
// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-08-19 17:46:37 +08:00
_updateTransform();
node->_updateTransform();
// Ϊ<>ڵ㴴<DAB5><E3B4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
D2D1_GEOMETRY_RELATION relation = D2D1_GEOMETRY_RELATION_UNKNOWN;
ID2D1RectangleGeometry *rectGeo = nullptr, *rectGeo2 = nullptr;
ID2D1TransformedGeometry *transGeo = nullptr, *transGeo2 = nullptr;
auto factory = Game::instance()->renderer()->factory();
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
factory->CreateRectangleGeometry(
D2D1::RectF(0, 0, _size.width, _size.height),
2018-08-15 00:06:03 +08:00
&rectGeo
)
);
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
factory->CreateRectangleGeometry(
D2D1::RectF(0, 0, node->_size.width, node->_size.height),
&rectGeo2
2018-08-15 00:06:03 +08:00
)
);
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
factory->CreateTransformedGeometry(
rectGeo,
_finalMatri,
&transGeo
2018-08-15 00:06:03 +08:00
)
);
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
factory->CreateTransformedGeometry(
rectGeo2,
node->_finalMatri,
&transGeo2
2018-08-15 00:06:03 +08:00
)
);
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
// <20><>ȡ<EFBFBD>ཻ״̬
transGeo->CompareWithGeometry(
transGeo2,
2018-07-07 18:04:18 +08:00
D2D1::Matrix3x2F::Identity(),
&relation
2018-08-15 00:06:03 +08:00
)
);
SafeRelease(rectGeo);
SafeRelease(rectGeo2);
SafeRelease(transGeo);
SafeRelease(transGeo2);
return relation != D2D1_GEOMETRY_RELATION_UNKNOWN &&
relation != D2D1_GEOMETRY_RELATION_DISJOINT;
2018-03-06 09:56:17 +08:00
}
e2d::Node& e2d::Node::resumeAllActions()
2017-10-21 19:09:31 +08:00
{
ActionManager::instance()->resumeAllBindedWith(this);
return *this;
2017-10-21 19:09:31 +08:00
}
e2d::Node& e2d::Node::pauseAllActions()
2017-10-21 19:09:31 +08:00
{
ActionManager::instance()->pauseAllBindedWith(this);
return *this;
2017-10-21 19:09:31 +08:00
}
e2d::Node& e2d::Node::stopAllActions()
2017-10-21 19:09:31 +08:00
{
ActionManager::instance()->stopAllBindedWith(this);
return *this;
2017-10-21 19:09:31 +08:00
}
e2d::Node& e2d::Node::visible(bool value)
{
2018-07-07 01:48:39 +08:00
_visible = value;
return *this;
}
e2d::Node& e2d::Node::name(const String& name)
2017-10-14 11:40:47 +08:00
{
WARN_IF(name.empty(), "Invalid Node name.");
2017-10-14 11:40:47 +08:00
if (!name.empty() && _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><>
2018-08-19 15:11:20 +08:00
_hashName = name.hash();
2017-10-14 11:40:47 +08:00
}
return *this;
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;
2018-08-12 14:30:28 +08:00
for (const 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
}
}