Magic_Game/Easy2D/Node/ENode.cpp

679 lines
12 KiB
C++
Raw Normal View History

#include "..\enodes.h"
2017-10-17 21:22:25 +08:00
#include "..\emsg.h"
#include "..\etools.h"
2017-10-19 00:50:04 +08:00
#include "..\eactions.h"
2017-10-13 11:42:36 +08:00
#include "..\Win\winbase.h"
2017-10-14 18:43:32 +08:00
#include <algorithm>
e2d::ENode::ENode()
2017-10-14 18:43:32 +08:00
: m_nOrder(0)
, m_fScaleX(1.0f)
, m_fScaleY(1.0f)
2017-10-15 02:46:24 +08:00
, m_fRotation(0)
, m_fSkewAngleX(0)
, m_fSkewAngleY(0)
, m_fDisplayOpacity(1.0f)
, m_fRealOpacity(1.0f)
, m_fAnchorX(0)
, m_fAnchorY(0)
2017-10-15 02:46:24 +08:00
, m_Matri(D2D1::Matrix3x2F::Identity())
, m_bVisiable(true)
2017-10-17 21:22:25 +08:00
, m_bDisplayedInScene(false)
2017-10-14 11:40:47 +08:00
, m_pParent(nullptr)
, m_pParentScene(nullptr)
, m_nHashName(0)
, m_bSortChildrenNeeded(false)
, m_bTransformChildrenNeeded(false)
{
}
2017-10-17 21:22:25 +08:00
e2d::ENode::ENode(const EString & name)
: ENode()
{
2017-10-14 11:40:47 +08:00
setName(name);
}
e2d::ENode::~ENode()
{
2017-10-19 00:50:04 +08:00
ETimerManager::clearAllTimersBindedWith(this);
EMsgManager::clearAllMouseListenersBindedWith(this);
EMsgManager::clearAllKeyboardListenersBindedWith(this);
EActionManager::clearAllActionsBindedWith(this);
}
2017-10-17 21:22:25 +08:00
void e2d::ENode::onEnter()
{
}
void e2d::ENode::onExit()
{
}
void e2d::ENode::_callOn()
2017-10-14 18:43:32 +08:00
{
if (!m_bVisiable)
{
return;
}
if (m_bTransformChildrenNeeded)
{
_updateTransform(this);
}
2017-10-14 18:43:32 +08:00
if (!m_vChildren.empty())
{
this->_sortChildren();
size_t size = m_vChildren.size();
size_t i;
for (i = 0; i < size; i++)
{
auto child = m_vChildren[i];
// <20><><EFBFBD><EFBFBD> Order С<><D0A1><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD>
if (child->getOrder() < 0)
{
child->_callOn();
2017-10-14 18:43:32 +08:00
}
else
{
break;
}
}
2017-10-15 02:46:24 +08:00
GetRenderTarget()->SetTransform(m_Matri);
2017-10-14 18:43:32 +08:00
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
this->_onRender();
// <20><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ڵ<EFBFBD>
for (; i < size; i++)
m_vChildren[i]->_callOn();
2017-10-14 18:43:32 +08:00
}
else
{
2017-10-15 02:46:24 +08:00
GetRenderTarget()->SetTransform(m_Matri);
2017-10-14 18:43:32 +08:00
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
this->_onRender();
}
}
2017-10-13 11:42:36 +08:00
void e2d::ENode::_onRender()
{
}
2017-10-17 21:22:25 +08:00
void e2d::ENode::_onEnter()
{
if (!this->m_bDisplayedInScene && this->isVisiable())
{
this->m_bDisplayedInScene = true;
ETimerManager::_notifyAllTimersBindedWith(this);
EMsgManager::_notifyAllMouseListenersBindedWith(this);
EMsgManager::_notifyAllKeyboardListenersBindedWith(this);
2017-10-19 00:50:04 +08:00
EActionManager::_notifyAllActionsBindedWith(this);
2017-10-17 21:22:25 +08:00
this->onEnter();
for (const auto &child : m_vChildren)
{
child->_onEnter();
}
}
}
void e2d::ENode::_onExit()
{
if (this->m_bDisplayedInScene)
{
this->m_bDisplayedInScene = false;
ETimerManager::_waitAllTimersBindedWith(this);
EMsgManager::_waitAllMouseListenersBindedWith(this);
EMsgManager::_waitAllKeyboardListenersBindedWith(this);
2017-10-19 00:50:04 +08:00
EActionManager::_waitAllActionsBindedWith(this);
2017-10-17 21:22:25 +08:00
this->onExit();
for (const auto &child : m_vChildren)
{
child->_onExit();
}
}
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::_sortChildren()
{
if (m_bSortChildrenNeeded)
{
2017-10-14 18:43:32 +08:00
// <20>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
std::sort(
std::begin(m_vChildren),
std::end(m_vChildren),
[](ENode * n1, ENode * n2) {
return n1->getOrder() < n2->getOrder();
}
);
m_bSortChildrenNeeded = false;
}
}
void e2d::ENode::_updateTransformToReal()
{
// <20><><EFBFBD><EFBFBD>ê<EFBFBD><C3AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
D2D1_POINT_2F anchorPos = D2D1::Point2F(
getRealWidth() * m_fAnchorX,
getRealHeight() * m_fAnchorY
);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD><CFBD><EFBFBD><EFBFBD><EFBFBD>
D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(
m_Pos.x - getRealWidth() * m_fAnchorX,
m_Pos.y - getRealHeight() * m_fAnchorY
);
// <20><>ά<EFBFBD><CEAC><EFBFBD>α
m_Matri = D2D1::Matrix3x2F::Scale(
m_fScaleX,
m_fScaleY,
anchorPos
) * D2D1::Matrix3x2F::Skew(
m_fSkewAngleX,
m_fSkewAngleY,
anchorPos
2017-10-17 21:22:25 +08:00
) * D2D1::Matrix3x2F::Rotation(
m_fRotation,
anchorPos
) * D2D1::Matrix3x2F::Translation(
upperLeftCorner.x,
upperLeftCorner.y
);
}
void e2d::ENode::_updateChildrenTransform()
{
for (const auto &child : m_vChildren)
{
_updateTransform(child);
}
}
void e2d::ENode::_updateTransform(ENode * node)
{
node->_updateTransformToReal();
if (node->m_pParent)
{
node->m_Matri = node->m_Matri * node->m_pParent->m_Matri;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>нڵ<D0BD>
node->_updateChildrenTransform();
node->m_bTransformChildrenNeeded = false;
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::_updateChildrenOpacity()
{
for (const auto &child : m_vChildren)
{
_updateOpacity(child);
}
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::_updateOpacity(ENode * node)
{
if (node->m_pParent)
{
node->m_fDisplayOpacity = node->m_fRealOpacity * node->m_pParent->m_fDisplayOpacity;
}
node->_updateChildrenOpacity();
2017-10-14 18:43:32 +08:00
}
bool e2d::ENode::isVisiable() const
{
return m_bVisiable;
}
2017-10-17 21:22:25 +08:00
e2d::EString e2d::ENode::getName() const
{
return m_sName;
}
float e2d::ENode::getPosX() const
{
return m_Pos.x;
}
float e2d::ENode::getPosY() const
2017-10-15 02:46:24 +08:00
{
2017-10-17 21:22:25 +08:00
return m_Pos.y;
2017-10-15 02:46:24 +08:00
}
2017-10-17 21:22:25 +08:00
e2d::EPoint e2d::ENode::getPos() const
2017-10-15 02:46:24 +08:00
{
2017-10-17 21:22:25 +08:00
return m_Pos;
2017-10-15 02:46:24 +08:00
}
float e2d::ENode::getWidth() const
2017-10-14 18:43:32 +08:00
{
2017-10-17 21:22:25 +08:00
return m_Size.width * m_fScaleX;
2017-10-14 18:43:32 +08:00
}
2017-10-15 02:46:24 +08:00
float e2d::ENode::getHeight() const
2017-10-14 18:43:32 +08:00
{
2017-10-17 21:22:25 +08:00
return m_Size.height * m_fScaleY;
}
float e2d::ENode::getRealWidth() const
{
return m_Size.width;
}
float e2d::ENode::getRealHeight() const
{
return m_Size.height;
}
e2d::ESize e2d::ENode::getRealSize() const
{
return m_Size;
}
e2d::ESize e2d::ENode::getSize() const
{
return ESize(getWidth(), getHeight());
}
2017-10-15 02:46:24 +08:00
float e2d::ENode::getScaleX() const
{
2017-10-15 02:46:24 +08:00
return m_fScaleX;
}
2017-10-15 02:46:24 +08:00
float e2d::ENode::getScaleY() const
{
2017-10-15 02:46:24 +08:00
return m_fScaleY;
}
2017-10-15 02:46:24 +08:00
float e2d::ENode::getSkewX() const
{
2017-10-15 02:46:24 +08:00
return m_fSkewAngleX;
}
2017-10-15 02:46:24 +08:00
float e2d::ENode::getSkewY() const
{
2017-10-15 02:46:24 +08:00
return m_fSkewAngleY;
}
2017-10-15 02:46:24 +08:00
float e2d::ENode::getRotation() const
{
2017-10-15 02:46:24 +08:00
return m_fRotation;
}
2017-10-15 02:46:24 +08:00
float e2d::ENode::getOpacity() const
{
return m_fRealOpacity;
2017-10-15 02:46:24 +08:00
}
int e2d::ENode::getOrder() const
{
return m_nOrder;
}
void e2d::ENode::setOrder(int order)
{
m_nOrder = order;
}
2017-10-17 21:22:25 +08:00
void e2d::ENode::setPosX(float x)
{
this->setPos(x, m_Pos.y);
}
void e2d::ENode::setPosY(float y)
{
2017-10-17 21:22:25 +08:00
this->setPos(m_Pos.x, y);
}
2017-10-17 21:22:25 +08:00
void e2d::ENode::setPos(const EPoint & p)
{
2017-10-17 21:22:25 +08:00
this->setPos(p.x, p.y);
}
2017-10-15 02:46:24 +08:00
void e2d::ENode::setPos(float x, float y)
{
2017-10-17 21:22:25 +08:00
if (m_Pos.x == x && m_Pos.y == y)
2017-10-14 18:43:32 +08:00
return;
2017-10-17 21:22:25 +08:00
m_Pos.x = x;
m_Pos.y = y;
m_bTransformChildrenNeeded = true;
}
2017-10-15 02:46:24 +08:00
void e2d::ENode::move(float x, float y)
{
2017-10-17 21:22:25 +08:00
this->setPos(m_Pos.x + x, m_Pos.y + y);
}
void e2d::ENode::move(const EVec & v)
{
this->move(v.x, v.y);
}
void e2d::ENode::_setWidth(float width)
{
this->_setSize(width, m_Size.height);
}
void e2d::ENode::_setHeight(float height)
{
this->_setSize(m_Size.width, height);
2017-10-17 21:22:25 +08:00
}
void e2d::ENode::_setSize(const ESize & size)
2017-10-17 21:22:25 +08:00
{
this->_setSize(size.width, size.height);
}
void e2d::ENode::_setSize(float width, float height)
{
2017-10-17 21:22:25 +08:00
if (m_Size.width == width && m_Size.height == height)
2017-10-15 02:46:24 +08:00
return;
2017-10-17 21:22:25 +08:00
m_Size.width = width;
m_Size.height = height;
m_bTransformChildrenNeeded = true;
}
2017-10-15 02:46:24 +08:00
void e2d::ENode::setScaleX(float scaleX)
{
2017-10-15 02:46:24 +08:00
this->setScale(scaleX, m_fScaleY);
}
2017-10-15 02:46:24 +08:00
void e2d::ENode::setScaleY(float scaleY)
{
2017-10-15 02:46:24 +08:00
this->setScale(m_fScaleX, scaleY);
}
2017-10-15 02:46:24 +08:00
void e2d::ENode::setScale(float scale)
{
2017-10-15 02:46:24 +08:00
this->setScale(scale, scale);
}
2017-10-15 02:46:24 +08:00
void e2d::ENode::setScale(float scaleX, float scaleY)
{
2017-10-15 02:46:24 +08:00
if (m_fScaleX == scaleX && m_fScaleY == scaleY)
return;
m_fScaleX = scaleX;
m_fScaleY = scaleY;
m_bTransformChildrenNeeded = true;
}
2017-10-15 02:46:24 +08:00
void e2d::ENode::setSkewX(float angleX)
{
2017-10-15 02:46:24 +08:00
this->setSkew(angleX, m_fSkewAngleY);
}
2017-10-15 02:46:24 +08:00
void e2d::ENode::setSkewY(float angleY)
{
2017-10-15 02:46:24 +08:00
this->setSkew(m_fSkewAngleX, angleY);
}
2017-10-15 02:46:24 +08:00
void e2d::ENode::setSkew(float angleX, float angleY)
{
2017-10-15 02:46:24 +08:00
if (m_fSkewAngleX == angleX && m_fSkewAngleY == angleY)
return;
m_fSkewAngleX = angleX;
m_fSkewAngleY = angleY;
m_bTransformChildrenNeeded = true;
2017-10-15 02:46:24 +08:00
}
void e2d::ENode::setRotation(float angle)
{
if (m_fRotation == angle)
return;
m_fRotation = angle;
m_bTransformChildrenNeeded = true;
2017-10-15 02:46:24 +08:00
}
void e2d::ENode::setOpacity(float opacity)
{
if (m_fRealOpacity == opacity)
return;
m_fDisplayOpacity = m_fRealOpacity = min(max(opacity, 0), 1);
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
_updateChildrenOpacity();
}
void e2d::ENode::setAnchorX(float anchorX)
{
this->setAnchor(anchorX, m_fAnchorY);
}
void e2d::ENode::setAnchorY(float anchorY)
{
this->setAnchor(m_fAnchorX, anchorY);
}
void e2d::ENode::setAnchor(float anchorX, float anchorY)
{
if (m_fAnchorX == anchorX && m_fAnchorY == anchorY)
return;
m_fAnchorX = min(max(anchorX, 0), 1);
m_fAnchorY = min(max(anchorY, 0), 1);
m_bTransformChildrenNeeded = true;
}
2017-10-13 17:14:00 +08:00
void e2d::ENode::setParent(ENode * parent)
{
2017-10-14 18:43:32 +08:00
if (m_pParent)
{
m_pParent->addChild(this);
}
else
{
removeFromParent();
}
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::addChild(ENode * child, int order /* = 0 */)
2017-10-14 11:40:47 +08:00
{
2017-10-14 18:43:32 +08:00
WARN_IF(child == nullptr, "ENode::addChild NULL pointer exception.");
2017-10-17 21:22:25 +08:00
ASSERT(child->m_pParent == nullptr, "ENode already added. It can't be added again!");
2017-10-14 11:40:47 +08:00
if (child)
{
for (ENode * parent = this; parent != nullptr; parent = parent->getParent())
{
2017-10-14 18:43:32 +08:00
ASSERT(child != parent, "A ENode cannot be the child of his own children!");
2017-10-14 11:40:47 +08:00
}
2017-10-14 18:43:32 +08:00
m_vChildren.push_back(child);
child->setOrder(order);
child->retain();
child->m_pParent = this;
_updateOpacity(child);
2017-10-17 21:22:25 +08:00
if (this->m_pParentScene)
{
child->_setParentScene(this->m_pParentScene);
}
if (this->m_bDisplayedInScene)
{
child->_onEnter();
}
m_bSortChildrenNeeded = true;
2017-10-14 11:40:47 +08:00
}
}
2017-10-14 18:43:32 +08:00
e2d::ENode * e2d::ENode::getParent() const
{
return m_pParent;
}
2017-10-14 18:43:32 +08:00
e2d::EScene * e2d::ENode::getParentScene() const
{
return m_pParentScene;
}
2017-10-17 21:22:25 +08:00
e2d::EVector<e2d::ENode*>& e2d::ENode::getChildren()
2017-10-14 11:40:47 +08:00
{
return m_vChildren;
}
2017-10-14 18:43:32 +08:00
size_t e2d::ENode::getChildrenCount() const
2017-10-14 11:40:47 +08:00
{
return m_vChildren.size();
}
2017-10-17 21:22:25 +08:00
e2d::ENode * e2d::ENode::getChild(const EString & name)
2017-10-14 11:40:47 +08:00
{
WARN_IF(name.empty(), "Invalid ENode name.");
std::hash<EString> h;
size_t hash = h(name);
2017-10-17 21:22:25 +08:00
for (const auto& child : m_vChildren)
2017-10-14 11:40:47 +08:00
{
// <20><>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD>ƿ<EFBFBD><C6BF>ܻ<EFBFBD><DCBB><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC> Hash ֵ<><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱȽ<C8B1> Hash <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>
if (child->m_nHashName == hash && child->m_sName == name)
return child;
}
return nullptr;
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::removeFromParent(bool release /* = false */)
2017-10-14 11:40:47 +08:00
{
2017-10-14 18:43:32 +08:00
if (m_pParent)
{
m_pParent->removeChild(this, release);
}
2017-10-14 11:40:47 +08:00
}
2017-10-19 00:50:04 +08:00
bool e2d::ENode::removeChild(ENode * child, bool release /* = true */)
2017-10-14 11:40:47 +08:00
{
2017-10-14 18:43:32 +08:00
WARN_IF(child == nullptr, "ENode::removeChild NULL pointer exception.");
if (m_vChildren.empty())
{
2017-10-17 21:22:25 +08:00
return false;
2017-10-14 18:43:32 +08:00
}
if (child)
{
size_t size = m_vChildren.size();
for (size_t i = 0; i < size; i++)
{
if (m_vChildren[i] == child)
{
m_vChildren.erase(m_vChildren.begin() + i);
child->m_pParent = nullptr;
2017-10-17 21:22:25 +08:00
if (child->m_pParentScene)
{
child->_setParentScene(nullptr);
}
child->_onExit();
2017-10-14 18:43:32 +08:00
child->release();
if (release)
child->autoRelease();
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
}
2017-10-19 00:50:04 +08:00
void e2d::ENode::removeChild(const EString & childName, bool release /* = true */)
2017-10-14 11:40:47 +08:00
{
2017-10-14 18:43:32 +08:00
WARN_IF(childName.empty(), "Invalid ENode name.");
if (m_vChildren.empty())
{
return;
}
2017-10-17 21:22:25 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Hash ֵ
2017-10-14 18:43:32 +08:00
std::hash<EString> h;
size_t hash = h(childName);
2017-10-17 21:22:25 +08:00
2017-10-14 18:43:32 +08:00
size_t size = m_vChildren.size();
for (size_t i = 0; i < size; i++)
{
auto child = m_vChildren[i];
if (child->m_nHashName == hash && child->m_sName == childName)
{
m_vChildren.erase(m_vChildren.begin() + i);
child->m_pParent = nullptr;
2017-10-17 21:22:25 +08:00
if (child->m_pParentScene)
{
child->_setParentScene(nullptr);
}
child->_onExit();
2017-10-14 18:43:32 +08:00
child->release();
if (release)
child->autoRelease();
return;
}
}
2017-10-14 11:40:47 +08:00
}
2017-10-19 00:50:04 +08:00
void e2d::ENode::clearAllChildren(bool release /* = true */)
2017-10-17 21:22:25 +08:00
{
// <20><><EFBFBD>нڵ<D0BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD>һ
for (auto child : m_vChildren)
{
if (release)
{
child->autoRelease();
}
2017-10-19 00:50:04 +08:00
child->_onExit();
2017-10-17 21:22:25 +08:00
child->release();
}
// <20><><EFBFBD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
m_vChildren.clear();
}
2017-10-19 00:50:04 +08:00
void e2d::ENode::runAction(EAction * action)
{
EActionManager::bindAction(action, this);
}
void e2d::ENode::stopAction(EAction * action)
{
}
void e2d::ENode::setVisiable(bool value)
{
m_bVisiable = value;
2017-10-17 21:22:25 +08:00
if (m_bDisplayedInScene == false)
{
this->_onEnter();
}
}
2017-10-17 21:22:25 +08:00
void e2d::ENode::setName(const EString & name)
2017-10-14 11:40:47 +08:00
{
WARN_IF(name.empty(), "Invalid ENode name.");
if (!name.empty())
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>
m_sName = name;
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> Hash <20><>
std::hash<EString> h;
m_nHashName = h(name);
}
}
2017-10-17 21:22:25 +08:00
void e2d::ENode::_setParentScene(EScene * scene)
{
m_pParentScene = scene;
for (const auto &child : m_vChildren)
{
child->_setParentScene(scene);
}
}