1002 lines
		
	
	
		
			17 KiB
		
	
	
	
		
			C++
		
	
	
	
			
		
		
	
	
			1002 lines
		
	
	
		
			17 KiB
		
	
	
	
		
			C++
		
	
	
	
| #include "..\e2dnode.h"
 | ||
| #include "..\e2dmanager.h"
 | ||
| #include "..\e2daction.h"
 | ||
| 
 | ||
| const e2d::Node::Property e2d::Node::Property::Origin = { 0 };
 | ||
| 
 | ||
| e2d::Node::Property e2d::Node::Property::operator+(Property const & prop) const
 | ||
| {
 | ||
| 	Property result;
 | ||
| 	result.posX = this->posX + prop.posX;
 | ||
| 	result.posY = this->posY + prop.posY;
 | ||
| 	result.width = this->width + prop.width;
 | ||
| 	result.height = this->height + prop.height;
 | ||
| 	result.pivotX = this->pivotX + prop.pivotX;
 | ||
| 	result.pivotY = this->pivotY + prop.pivotY;
 | ||
| 	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.posX = this->posX - prop.posX;
 | ||
| 	result.posY = this->posY - prop.posY;
 | ||
| 	result.width = this->width - prop.width;
 | ||
| 	result.height = this->height - prop.height;
 | ||
| 	result.pivotX = this->pivotX - prop.pivotX;
 | ||
| 	result.pivotY = this->pivotY - prop.pivotY;
 | ||
| 	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)
 | ||
| 	, _posX(0)
 | ||
| 	, _posY(0)
 | ||
| 	, _width(0)
 | ||
| 	, _height(0)
 | ||
| 	, _scaleX(1.0f)
 | ||
| 	, _scaleY(1.0f)
 | ||
| 	, _rotation(0)
 | ||
| 	, _skewAngleX(0)
 | ||
| 	, _skewAngleY(0)
 | ||
| 	, _displayOpacity(1.f)
 | ||
| 	, _realOpacity(1.f)
 | ||
| 	, _pivotX(0.f)
 | ||
| 	, _pivotY(0.f)
 | ||
| 	, _initialMatri(D2D1::Matrix3x2F::Identity())
 | ||
| 	, _finalMatri(D2D1::Matrix3x2F::Identity())
 | ||
| 	, _visible(true)
 | ||
| 	, _parent(nullptr)
 | ||
| 	, _parentScene(nullptr)
 | ||
| 	, _hashName(0)
 | ||
| 	, _clipEnabled(false)
 | ||
| 	, _needSort(false)
 | ||
| 	, _needTransform(false)
 | ||
| 	, _autoUpdate(true)
 | ||
| 	, _positionFixed(false)
 | ||
| 	, _collider(this)
 | ||
| 	, _extrapolate(Property::Origin)
 | ||
| {
 | ||
| }
 | ||
| 
 | ||
| e2d::Node::~Node()
 | ||
| {
 | ||
| 	ActionManager::getInstance()->clearAllBindedWith(this);
 | ||
| 	for (auto child : _children)
 | ||
| 	{
 | ||
| 		GC::getInstance()->safeRelease(child);
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::_update()
 | ||
| {
 | ||
| 	if (!_visible)
 | ||
| 		return;
 | ||
| 
 | ||
| 	if (_children.empty())
 | ||
| 	{
 | ||
| 		_updateSelf();
 | ||
| 	}
 | ||
| 	else
 | ||
| 	{
 | ||
| 		// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>
 | ||
| 		size_t i;
 | ||
| 		for (i = 0; i < _children.size(); ++i)
 | ||
| 		{
 | ||
| 			auto child = _children[i];
 | ||
| 			// <20><><EFBFBD><EFBFBD> Order С<><D0A1><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD>
 | ||
| 			if (child->getOrder() < 0)
 | ||
| 			{
 | ||
| 				child->_update();
 | ||
| 			}
 | ||
| 			else
 | ||
| 			{
 | ||
| 				break;
 | ||
| 			}
 | ||
| 		}
 | ||
| 
 | ||
| 		_updateSelf();
 | ||
| 
 | ||
| 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>
 | ||
| 		for (; i < _children.size(); ++i)
 | ||
| 			_children[i]->_update();
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::_updateSelf()
 | ||
| {
 | ||
| 	if (_needTransform)
 | ||
| 	{
 | ||
| 		updateTransform();
 | ||
| 		if (_collider.isEnabled() &&
 | ||
| 			_collider.isCollisionNotify() &&
 | ||
| 			_collider.getShape() != Collider::Shape::None)
 | ||
| 		{
 | ||
| 			CollisionManager::getInstance()->__updateCollider(&_collider);
 | ||
| 		}
 | ||
| 	}
 | ||
| 
 | ||
| 	if (_autoUpdate && !Game::getInstance()->isPaused())
 | ||
| 	{
 | ||
| 		this->onUpdate();
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::_render()
 | ||
| {
 | ||
| 	if (!_visible)
 | ||
| 		return;
 | ||
| 
 | ||
| 	// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | ||
| 	updateTransform();
 | ||
| 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | ||
| 	_extrapolate = this->getProperty();
 | ||
| 
 | ||
| 	auto pRT = Renderer::getInstance()->getRenderTarget();
 | ||
| 	if (_clipEnabled)
 | ||
| 	{
 | ||
| 		pRT->SetTransform(_finalMatri);
 | ||
| 		pRT->PushAxisAlignedClip(
 | ||
| 			D2D1::RectF(0, 0, _width, _height),
 | ||
| 			D2D1_ANTIALIAS_MODE_PER_PRIMITIVE
 | ||
| 		);
 | ||
| 	}
 | ||
| 
 | ||
| 	if (_children.empty())
 | ||
| 	{
 | ||
| 		// ת<><D7AA><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | ||
| 		pRT->SetTransform(_finalMatri);
 | ||
| 		// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
 | ||
| 		this->onRender();
 | ||
| 	}
 | ||
| 	else
 | ||
| 	{
 | ||
| 		// <20>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
 | ||
| 		_sortChildren();
 | ||
| 
 | ||
| 		size_t i;
 | ||
| 		for (i = 0; i < _children.size(); ++i)
 | ||
| 		{
 | ||
| 			auto child = _children[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><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | ||
| 		pRT->SetTransform(_finalMatri);
 | ||
| 		// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
 | ||
| 		this->onRender();
 | ||
| 
 | ||
| 		// <20><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ڵ<EFBFBD>
 | ||
| 		for (; i < _children.size(); ++i)
 | ||
| 			_children[i]->_render();
 | ||
| 	}
 | ||
| 
 | ||
| 	if (_clipEnabled)
 | ||
| 	{
 | ||
| 		pRT->PopAxisAlignedClip();
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::_renderOutline()
 | ||
| {
 | ||
| 	if (_visible)
 | ||
| 	{
 | ||
| 		auto renderer = Renderer::getInstance();
 | ||
| 		renderer->getRenderTarget()->SetTransform(_finalMatri);
 | ||
| 		renderer->getRenderTarget()->DrawRectangle(
 | ||
| 			D2D1::RectF(0, 0, _width, _height),
 | ||
| 			renderer->getSolidColorBrush(),
 | ||
| 			1.5f
 | ||
| 		);
 | ||
| 
 | ||
| 		// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | ||
| 		for (auto child : _children)
 | ||
| 		{
 | ||
| 			child->_renderOutline();
 | ||
| 		}
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::_renderCollider()
 | ||
| {
 | ||
| 	if (_visible)
 | ||
| 	{
 | ||
| 		_collider.render();
 | ||
| 
 | ||
| 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
 | ||
| 		for (auto child : _children)
 | ||
| 		{
 | ||
| 			child->_renderCollider();
 | ||
| 		}
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::updateTransform()
 | ||
| {
 | ||
| 	if (!_needTransform)
 | ||
| 		return;
 | ||
| 
 | ||
| 	_needTransform = false;
 | ||
| 
 | ||
| 	// <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
 | ||
| 	) * 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;
 | ||
| 	}
 | ||
| 
 | ||
| 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
 | ||
| 	_collider.recreate();
 | ||
| 
 | ||
| 	// ֪ͨ<CDA8>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
 | ||
| 	for (auto& child : _children)
 | ||
| 	{
 | ||
| 		child->_needTransform = true;
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::dispatch(const MouseEvent & e)
 | ||
| {
 | ||
| 	if (!onMouseEvent(e))
 | ||
| 		return;
 | ||
| 
 | ||
| 	if (!_children.empty())
 | ||
| 	{
 | ||
| 		for (auto child : _children)
 | ||
| 		{
 | ||
| 			child->dispatch(e);
 | ||
| 		}
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::dispatch(const KeyEvent & e)
 | ||
| {
 | ||
| 	if (!onKeyEvent(e))
 | ||
| 		return;
 | ||
| 
 | ||
| 	if (!_children.empty())
 | ||
| 	{
 | ||
| 		for (auto child : _children)
 | ||
| 		{
 | ||
| 			child->dispatch(e);
 | ||
| 		}
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::_sortChildren()
 | ||
| {
 | ||
| 	if (_needSort)
 | ||
| 	{
 | ||
| 		std::sort(
 | ||
| 			std::begin(_children),
 | ||
| 			std::end(_children),
 | ||
| 			[](Node * n1, Node * n2) { return n1->getOrder() < n2->getOrder(); }
 | ||
| 		);
 | ||
| 
 | ||
| 		_needSort = false;
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::_updateOpacity()
 | ||
| {
 | ||
| 	if (_parent)
 | ||
| 	{
 | ||
| 		_displayOpacity = _realOpacity * _parent->_displayOpacity;
 | ||
| 	}
 | ||
| 	for (auto child : _children)
 | ||
| 	{
 | ||
| 		child->_updateOpacity();
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| bool e2d::Node::isVisible() const
 | ||
| {
 | ||
| 	return _visible;
 | ||
| }
 | ||
| 
 | ||
| e2d::String e2d::Node::getName() const
 | ||
| {
 | ||
| 	return _name;
 | ||
| }
 | ||
| 
 | ||
| size_t e2d::Node::getHashName() const
 | ||
| {
 | ||
| 	return _hashName;
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getPosX() const
 | ||
| {
 | ||
| 	return _posX;
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getPosY() const
 | ||
| {
 | ||
| 	return _posY;
 | ||
| }
 | ||
| 
 | ||
| e2d::Point e2d::Node::getPos() const
 | ||
| {
 | ||
| 	return Point(_posX, _posY);
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getWidth() const
 | ||
| {
 | ||
| 	return _width * _scaleX;
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getHeight() const
 | ||
| {
 | ||
| 	return _height * _scaleY;
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getRealWidth() const
 | ||
| {
 | ||
| 	return _width;
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getRealHeight() const
 | ||
| {
 | ||
| 	return _height;
 | ||
| }
 | ||
| 
 | ||
| e2d::Size e2d::Node::getRealSize() const
 | ||
| {
 | ||
| 	return Size(_width, _height);
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getPivotX() const
 | ||
| {
 | ||
| 	return _pivotX;
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getPivotY() const
 | ||
| {
 | ||
| 	return _pivotY;
 | ||
| }
 | ||
| 
 | ||
| e2d::Size e2d::Node::getSize() const
 | ||
| {
 | ||
| 	return Size(getWidth(), getHeight());
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getScaleX() const
 | ||
| {
 | ||
| 	return _scaleX;
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getScaleY() const
 | ||
| {
 | ||
| 	return _scaleY;
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getSkewX() const
 | ||
| {
 | ||
| 	return _skewAngleX;
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getSkewY() const
 | ||
| {
 | ||
| 	return _skewAngleY;
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getRotation() const
 | ||
| {
 | ||
| 	return _rotation;
 | ||
| }
 | ||
| 
 | ||
| float e2d::Node::getOpacity() const
 | ||
| {
 | ||
| 	return _realOpacity;
 | ||
| }
 | ||
| 
 | ||
| e2d::Node::Property e2d::Node::getProperty() const
 | ||
| {
 | ||
| 	Property prop;
 | ||
| 	prop.posX = _posX;
 | ||
| 	prop.posY = _posY;
 | ||
| 	prop.width = _width;
 | ||
| 	prop.height = _height;
 | ||
| 	prop.pivotX = _pivotX;
 | ||
| 	prop.pivotY = _pivotY;
 | ||
| 	prop.scaleX = _scaleX;
 | ||
| 	prop.scaleY = _scaleY;
 | ||
| 	prop.rotation = _rotation;
 | ||
| 	prop.skewAngleX = _skewAngleX;
 | ||
| 	prop.skewAngleY = _skewAngleY;
 | ||
| 	return std::move(prop);
 | ||
| }
 | ||
| 
 | ||
| e2d::Node::Property e2d::Node::getExtrapolate() const
 | ||
| {
 | ||
| 	return this->getProperty() - _extrapolate;
 | ||
| }
 | ||
| 
 | ||
| e2d::Collider* e2d::Node::getCollider()
 | ||
| {
 | ||
| 	return &_collider;
 | ||
| }
 | ||
| 
 | ||
| int e2d::Node::getOrder() const
 | ||
| {
 | ||
| 	return _order;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setOrder(int order)
 | ||
| {
 | ||
| 	if (_order == order)
 | ||
| 		return;
 | ||
| 
 | ||
| 	_order = order;
 | ||
| 	if (_parent)
 | ||
| 	{
 | ||
| 		_parent->_needSort = true;
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setPosX(float x)
 | ||
| {
 | ||
| 	this->setPos(x, _posY);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setPosY(float y)
 | ||
| {
 | ||
| 	this->setPos(_posX, y);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setPos(const Point & p)
 | ||
| {
 | ||
| 	this->setPos(p.x, p.y);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setPos(float x, float y)
 | ||
| {
 | ||
| 	if (_posX == x && _posY == y)
 | ||
| 		return;
 | ||
| 
 | ||
| 	_posX = x;
 | ||
| 	_posY = y;
 | ||
| 	_needTransform = true;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setPosFixed(bool fixed)
 | ||
| {
 | ||
| 	if (_positionFixed == fixed)
 | ||
| 		return;
 | ||
| 
 | ||
| 	_positionFixed = fixed;
 | ||
| 	_needTransform = true;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::movePosX(float x)
 | ||
| {
 | ||
| 	this->movePos(x, 0);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::movePosY(float y)
 | ||
| {
 | ||
| 	this->movePos(0, y);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::movePos(float x, float y)
 | ||
| {
 | ||
| 	this->setPos(_posX + x, _posY + y);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::movePos(const Vector2 & v)
 | ||
| {
 | ||
| 	this->movePos(v.x, v.y);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setScaleX(float scaleX)
 | ||
| {
 | ||
| 	this->setScale(scaleX, _scaleY);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setScaleY(float scaleY)
 | ||
| {
 | ||
| 	this->setScale(_scaleX, scaleY);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setScale(float scale)
 | ||
| {
 | ||
| 	this->setScale(scale, scale);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setScale(float scaleX, float scaleY)
 | ||
| {
 | ||
| 	if (_scaleX == scaleX && _scaleY == scaleY)
 | ||
| 		return;
 | ||
| 
 | ||
| 	_scaleX = scaleX;
 | ||
| 	_scaleY = scaleY;
 | ||
| 	_needTransform = true;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setSkewX(float angleX)
 | ||
| {
 | ||
| 	this->setSkew(angleX, _skewAngleY);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setSkewY(float angleY)
 | ||
| {
 | ||
| 	this->setSkew(_skewAngleX, angleY);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setSkew(float angleX, float angleY)
 | ||
| {
 | ||
| 	if (_skewAngleX == angleX && _skewAngleY == angleY)
 | ||
| 		return;
 | ||
| 
 | ||
| 	_skewAngleX = angleX;
 | ||
| 	_skewAngleY = angleY;
 | ||
| 	_needTransform = true;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setRotation(float angle)
 | ||
| {
 | ||
| 	if (_rotation == angle)
 | ||
| 		return;
 | ||
| 
 | ||
| 	_rotation = angle;
 | ||
| 	_needTransform = true;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setOpacity(float opacity)
 | ||
| {
 | ||
| 	if (_realOpacity == opacity)
 | ||
| 		return;
 | ||
| 
 | ||
| 	_displayOpacity = _realOpacity = std::min(std::max(opacity, 0.f), 1.f);
 | ||
| 	// <20><><EFBFBD>½ڵ<C2BD><EFBFBD><CDB8><EFBFBD><EFBFBD>
 | ||
| 	_updateOpacity();
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setPivotX(float pivotX)
 | ||
| {
 | ||
| 	this->setPivot(pivotX, _pivotY);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setPivotY(float pivotY)
 | ||
| {
 | ||
| 	this->setPivot(_pivotX, pivotY);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setPivot(float pivotX, float pivotY)
 | ||
| {
 | ||
| 	if (_pivotX == pivotX && _pivotY == pivotY)
 | ||
| 		return;
 | ||
| 
 | ||
| 	_pivotX = std::min(std::max(pivotX, 0.f), 1.f);
 | ||
| 	_pivotY = std::min(std::max(pivotY, 0.f), 1.f);
 | ||
| 	_needTransform = true;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setWidth(float width)
 | ||
| {
 | ||
| 	this->setSize(width, _height);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setHeight(float height)
 | ||
| {
 | ||
| 	this->setSize(_width, height);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setSize(float width, float height)
 | ||
| {
 | ||
| 	if (_width == width && _height == height)
 | ||
| 		return;
 | ||
| 
 | ||
| 	_width = width;
 | ||
| 	_height = height;
 | ||
| 	_needTransform = true;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setSize(Size size)
 | ||
| {
 | ||
| 	this->setSize(size.width, size.height);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setProperty(Property prop)
 | ||
| {
 | ||
| 	this->setPos(prop.posX, prop.posY);
 | ||
| 	this->setSize(prop.width, prop.height);
 | ||
| 	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::setClipEnabled(bool enabled)
 | ||
| {
 | ||
| 	_clipEnabled = enabled;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::addChild(Node * child, int order  /* = 0 */)
 | ||
| {
 | ||
| 	WARN_IF(child == nullptr, "Node::addChild NULL pointer exception.");
 | ||
| 
 | ||
| 	if (child)
 | ||
| 	{
 | ||
| 		if (child->_parent != nullptr)
 | ||
| 		{
 | ||
| 			throw Exception(L"<EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD><EFBFBD>ڵ<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>");
 | ||
| 		}
 | ||
| 
 | ||
| 		for (Node * parent = this; parent != nullptr; parent = parent->getParent())
 | ||
| 		{
 | ||
| 			if (child == parent)
 | ||
| 			{
 | ||
| 				throw Exception(L"һ<EFBFBD><EFBFBD><EFBFBD>ڵ㲻<EFBFBD><EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD>ĸ<EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<EFBFBD>");
 | ||
| 			}
 | ||
| 		}
 | ||
| 
 | ||
| 		child->retain();
 | ||
| 		_children.push_back(child);
 | ||
| 		child->setOrder(order);
 | ||
| 		child->_parent = this;
 | ||
| 		if (this->_parentScene)
 | ||
| 		{
 | ||
| 			child->_setParentScene(this->_parentScene);
 | ||
| 		}
 | ||
| 
 | ||
| 		// <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;
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| 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
 | ||
| {
 | ||
| 	std::vector<Node*> vChildren;
 | ||
| 	size_t hash = name.getHashCode();
 | ||
| 
 | ||
| 	for (auto child : _children)
 | ||
| 	{
 | ||
| 		// <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);
 | ||
| 		}
 | ||
| 	}
 | ||
| 	return std::move(vChildren);
 | ||
| }
 | ||
| 
 | ||
| e2d::Node * e2d::Node::getChild(const String& name) const
 | ||
| {
 | ||
| 	size_t hash = name.getHashCode();
 | ||
| 
 | ||
| 	for (auto child : _children)
 | ||
| 	{
 | ||
| 		// <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)
 | ||
| 		{
 | ||
| 			return child;
 | ||
| 		}
 | ||
| 	}
 | ||
| 	return nullptr;
 | ||
| }
 | ||
| 
 | ||
| const std::vector<e2d::Node*>& e2d::Node::getAllChildren() const
 | ||
| {
 | ||
| 	return _children;
 | ||
| }
 | ||
| 
 | ||
| int e2d::Node::getChildrenCount() const
 | ||
| {
 | ||
| 	return static_cast<int>(_children.size());
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::removeFromParent()
 | ||
| {
 | ||
| 	if (_parent)
 | ||
| 	{
 | ||
| 		_parent->removeChild(this);
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| bool e2d::Node::removeChild(Node * child)
 | ||
| {
 | ||
| 	WARN_IF(child == nullptr, "Node::removeChildren NULL pointer exception.");
 | ||
| 
 | ||
| 	if (_children.empty())
 | ||
| 	{
 | ||
| 		return false;
 | ||
| 	}
 | ||
| 
 | ||
| 	if (child)
 | ||
| 	{
 | ||
| 		auto iter = std::find(_children.begin(), _children.end(), child);
 | ||
| 		if (iter != _children.end())
 | ||
| 		{
 | ||
| 			_children.erase(iter);
 | ||
| 			child->_parent = nullptr;
 | ||
| 
 | ||
| 			if (child->_parentScene)
 | ||
| 			{
 | ||
| 				child->_setParentScene(nullptr);
 | ||
| 			}
 | ||
| 
 | ||
| 			child->release();
 | ||
| 			return true;
 | ||
| 		}
 | ||
| 	}
 | ||
| 	return false;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::removeChildren(const String& childName)
 | ||
| {
 | ||
| 	WARN_IF(childName.isEmpty(), "Invalid Node name.");
 | ||
| 
 | ||
| 	if (_children.empty())
 | ||
| 	{
 | ||
| 		return;
 | ||
| 	}
 | ||
| 
 | ||
| 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Hash ֵ
 | ||
| 	size_t hash = childName.getHashCode();
 | ||
| 
 | ||
| 	size_t size = _children.size();
 | ||
| 	for (size_t i = 0; i < size; ++i)
 | ||
| 	{
 | ||
| 		auto child = _children[i];
 | ||
| 		if (child->_hashName == hash && child->_name == childName)
 | ||
| 		{
 | ||
| 			_children.erase(_children.begin() + i);
 | ||
| 			child->_parent = nullptr;
 | ||
| 			if (child->_parentScene)
 | ||
| 			{
 | ||
| 				child->_setParentScene(nullptr);
 | ||
| 			}
 | ||
| 			child->release();
 | ||
| 		}
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::removeAllChildren()
 | ||
| {
 | ||
| 	// <20><><EFBFBD>нڵ<D0BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD>һ
 | ||
| 	for (auto child : _children)
 | ||
| 	{
 | ||
| 		child->release();
 | ||
| 	}
 | ||
| 	// <20><><EFBFBD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | ||
| 	_children.clear();
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::runAction(Action * action)
 | ||
| {
 | ||
| 	ActionManager::getInstance()->start(action, this, false);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::resumeAction(const String& name)
 | ||
| {
 | ||
| 	auto& actions = ActionManager::getInstance()->get(name);
 | ||
| 	for (auto action : actions)
 | ||
| 	{
 | ||
| 		if (action->getTarget() == this)
 | ||
| 		{
 | ||
| 			action->resume();
 | ||
| 		}
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::pauseAction(const String& name)
 | ||
| {
 | ||
| 	auto& actions = ActionManager::getInstance()->get(name);
 | ||
| 	for (auto action : actions)
 | ||
| 	{
 | ||
| 		if (action->getTarget() == this)
 | ||
| 		{
 | ||
| 			action->pause();
 | ||
| 		}
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::stopAction(const String& name)
 | ||
| {
 | ||
| 	auto& actions = ActionManager::getInstance()->get(name);
 | ||
| 	for (auto action : actions)
 | ||
| 	{
 | ||
| 		if (action->getTarget() == this)
 | ||
| 		{
 | ||
| 			action->stop();
 | ||
| 		}
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| bool e2d::Node::containsPoint(const Point& point)
 | ||
| {
 | ||
| 	// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | ||
| 	updateTransform();
 | ||
| 
 | ||
| 	// Ϊ<>ڵ㴴<DAB5><E3B4B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | ||
| 	BOOL ret = 0;
 | ||
| 	ID2D1RectangleGeometry * rectGeo = nullptr;
 | ||
| 	auto factory = Renderer::getFactory();
 | ||
| 
 | ||
| 	HRESULT hr = factory->CreateRectangleGeometry(
 | ||
| 		D2D1::RectF(0, 0, _width, _height),
 | ||
| 		&rectGeo
 | ||
| 	);
 | ||
| 
 | ||
| 	if (SUCCEEDED(hr))
 | ||
| 	{
 | ||
| 		rectGeo->FillContainsPoint(
 | ||
| 			D2D1::Point2F(point.x, point.y),
 | ||
| 			_finalMatri,
 | ||
| 			&ret
 | ||
| 		);
 | ||
| 	}
 | ||
| 	SafeRelease(rectGeo);
 | ||
| 
 | ||
| 	return ret != 0;
 | ||
| }
 | ||
| 
 | ||
| bool e2d::Node::intersects(Node * node)
 | ||
| {
 | ||
| 	// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | ||
| 	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 = Renderer::getFactory();
 | ||
| 
 | ||
| 	HRESULT hr = factory->CreateRectangleGeometry(
 | ||
| 		D2D1::RectF(0, 0, _width, _height),
 | ||
| 		&rectGeo
 | ||
| 	);
 | ||
| 
 | ||
| 	if (SUCCEEDED(hr))
 | ||
| 	{
 | ||
| 		hr = factory->CreateRectangleGeometry(
 | ||
| 			D2D1::RectF(0, 0, node->_width, node->_height),
 | ||
| 			&rectGeo2
 | ||
| 		);
 | ||
| 	}
 | ||
| 
 | ||
| 	if (SUCCEEDED(hr))
 | ||
| 	{
 | ||
| 		hr = factory->CreateTransformedGeometry(
 | ||
| 			rectGeo,
 | ||
| 			_finalMatri,
 | ||
| 			&transGeo
 | ||
| 		);
 | ||
| 	}
 | ||
| 
 | ||
| 	if (SUCCEEDED(hr))
 | ||
| 	{
 | ||
| 		hr = factory->CreateTransformedGeometry(
 | ||
| 			rectGeo2,
 | ||
| 			node->_finalMatri,
 | ||
| 			&transGeo2
 | ||
| 		);
 | ||
| 	}
 | ||
| 
 | ||
| 	if (SUCCEEDED(hr))
 | ||
| 	{
 | ||
| 		// <20><>ȡ<EFBFBD>ཻ״̬
 | ||
| 		transGeo->CompareWithGeometry(
 | ||
| 			transGeo2,
 | ||
| 			D2D1::Matrix3x2F::Identity(),
 | ||
| 			&relation
 | ||
| 		);
 | ||
| 	}
 | ||
| 
 | ||
| 	SafeRelease(rectGeo);
 | ||
| 	SafeRelease(rectGeo2);
 | ||
| 	SafeRelease(transGeo);
 | ||
| 	SafeRelease(transGeo2);
 | ||
| 
 | ||
| 	return relation != D2D1_GEOMETRY_RELATION_UNKNOWN &&
 | ||
| 		relation != D2D1_GEOMETRY_RELATION_DISJOINT;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setAutoUpdate(bool bAutoUpdate)
 | ||
| {
 | ||
| 	_autoUpdate = bAutoUpdate;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::resumeAllActions()
 | ||
| {
 | ||
| 	ActionManager::getInstance()->resumeAllBindedWith(this);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::pauseAllActions()
 | ||
| {
 | ||
| 	ActionManager::getInstance()->pauseAllBindedWith(this);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::stopAllActions()
 | ||
| {
 | ||
| 	ActionManager::getInstance()->stopAllBindedWith(this);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setVisible(bool value)
 | ||
| {
 | ||
| 	_visible = value;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::setName(const String& name)
 | ||
| {
 | ||
| 	WARN_IF(name.isEmpty(), "Invalid Node name.");
 | ||
| 
 | ||
| 	if (!name.isEmpty() && _name != name)
 | ||
| 	{
 | ||
| 		// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>
 | ||
| 		_name = name;
 | ||
| 		// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> Hash <20><>
 | ||
| 		_hashName = name.getHashCode();
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Node::_setParentScene(Scene * scene)
 | ||
| {
 | ||
| 	_parentScene = scene;
 | ||
| 	for (auto child : _children)
 | ||
| 	{
 | ||
| 		child->_setParentScene(scene);
 | ||
| 	}
 | ||
| } |