Magic_Game/Easy2D/Node/ENode.cpp

408 lines
6.4 KiB
C++
Raw Normal View History

#include "..\enodes.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_bVisiable(true)
2017-10-14 11:40:47 +08:00
, m_pParent(nullptr)
, m_pParentScene(nullptr)
, m_nHashName(0)
2017-10-14 18:43:32 +08:00
, m_bSortNeeded(false)
, m_bTransformNeeded(false)
{
}
2017-10-14 11:40:47 +08:00
e2d::ENode::ENode(EString name)
: ENode()
{
2017-10-14 11:40:47 +08:00
setName(name);
}
e2d::ENode::~ENode()
{
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::callOn()
{
if (!m_bVisiable)
{
return;
}
this->_transfrom();
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();
}
else
{
break;
}
}
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
this->_onRender();
// <20><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ڵ<EFBFBD>
for (; i < size; i++)
m_vChildren[i]->callOn();
}
else
{
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
this->_onRender();
}
}
2017-10-13 11:42:36 +08:00
void e2d::ENode::_onRender()
{
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::_onTransfrom()
{
2017-10-14 18:43:32 +08:00
}
void e2d::ENode::_sortChildren()
{
if (m_bSortNeeded)
{
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_bSortNeeded = false;
}
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::_transfrom()
{
2017-10-14 18:43:32 +08:00
if (m_bTransformNeeded)
{
2017-10-14 18:43:32 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (m_pParent)
{
this->setPos(m_pParent->getX() + m_Pos.x, m_pParent->getY() + m_Pos.y);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>и<EFBFBD><D0B8><EFBFBD>
_onTransfrom();
// <20><>ʾ<EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (const auto &child : m_vChildren)
{
child->m_bTransformNeeded = true;
}
m_bTransformNeeded = false;
}
2017-10-14 18:43:32 +08:00
}
bool e2d::ENode::isVisiable() const
{
return m_bVisiable;
}
int e2d::ENode::getX() const
{
return m_Rect.TopLeft().x;
}
int e2d::ENode::getY() const
{
return m_Rect.TopLeft().y;
}
CPoint e2d::ENode::getPos() const
{
return m_Rect.TopLeft();
}
UINT32 e2d::ENode::getWidth() const
{
return UINT32(m_Rect.Size().cx);
}
UINT32 e2d::ENode::getHeight() const
{
return UINT32(m_Rect.Size().cy);
}
e2d::ESize e2d::ENode::getSize() const
{
return e2d::ESize(m_Rect.Size());
}
e2d::ERect e2d::ENode::getRect() const
{
return e2d::ERect(m_Rect);
}
void e2d::ENode::setX(int x)
{
2017-10-14 18:43:32 +08:00
this->setPos(x, m_Rect.TopLeft().y);
}
void e2d::ENode::setY(int y)
{
2017-10-14 18:43:32 +08:00
this->setPos(m_Rect.TopLeft().x, y);
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::setPos(EPoint p)
{
2017-10-14 18:43:32 +08:00
this->setPos(p.x, p.y);
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::setPos(int x, int y)
{
2017-10-14 18:43:32 +08:00
if (getX() == x && getY() == y)
return;
if (!m_bTransformNeeded)
{
m_Pos.x = x;
m_Pos.y = y;
m_bTransformNeeded = true;
}
m_Rect.MoveToXY(x, y);
}
void e2d::ENode::move(int x, int y)
{
m_Rect.OffsetRect(x, y);
}
void e2d::ENode::move(EVector v)
{
m_Rect.OffsetRect(v);
}
void e2d::ENode::setWidth(UINT32 width)
{
m_Rect.BottomRight().x = m_Rect.TopLeft().x + width;
}
void e2d::ENode::setHeight(UINT32 height)
{
m_Rect.BottomRight().y = m_Rect.TopLeft().y + height;
}
void e2d::ENode::setSize(UINT32 width, UINT32 height)
{
2017-10-14 18:43:32 +08:00
m_Rect.BottomRight().x = m_Rect.TopLeft().x + width;
m_Rect.BottomRight().y = m_Rect.TopLeft().y + height;
}
2017-10-13 17:14:00 +08:00
void e2d::ENode::setSize(ESize size)
{
2017-10-14 18:43:32 +08:00
this->setSize(size.cx, size.cy);
}
void e2d::ENode::setRect(int x1, int y1, int x2, int y2)
{
m_Rect.SetRect(x1, y1, x2, y2);
}
void e2d::ENode::setRect(EPoint leftTop, EPoint rightBottom)
{
m_Rect.TopLeft() = leftTop;
m_Rect.BottomRight() = rightBottom;
}
2017-10-13 17:14:00 +08:00
void e2d::ENode::setRect(ERect rect)
{
m_Rect = rect;
}
2017-10-14 18:43:32 +08:00
int e2d::ENode::getOrder() const
{
2017-10-14 18:43:32 +08:00
return m_nOrder;
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::setOrder(int order)
{
2017-10-14 18:43:32 +08:00
m_nOrder = order;
}
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.");
ASSERT(child->m_pParent == nullptr, "Child 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->m_pParent = this;
child->setOrder(order);
child->retain();
m_bSortNeeded = 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-14 11:40:47 +08:00
std::vector<e2d::ENode*>& e2d::ENode::getChildren()
{
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-14 18:43:32 +08:00
e2d::ENode * e2d::ENode::getChild(EString name) const
{
return ENode::getChild(name, this->m_vChildren);
}
e2d::ENode * e2d::ENode::getChild(EString name, const std::vector<ENode*> &children)
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-14 18:43:32 +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->m_nHashName == hash && child->m_sName == name)
return child;
}
return nullptr;
}
void e2d::ENode::setParentScene(EScene * scene)
{
2017-10-14 18:43:32 +08:00
if (m_pParentScene)
{
m_pParentScene = scene;
}
}
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-14 18:43:32 +08:00
void e2d::ENode::removeChild(ENode * child, bool release /* = false */)
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())
{
return;
}
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;
child->release();
if (release)
child->autoRelease();
return;
}
}
}
2017-10-14 11:40:47 +08:00
}
2017-10-14 18:43:32 +08:00
void e2d::ENode::removeChild(EString childName, bool release /* = false */)
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;
}
std::hash<EString> h;
size_t hash = h(childName);
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;
child->release();
if (release)
child->autoRelease();
return;
}
}
2017-10-14 11:40:47 +08:00
}
void e2d::ENode::setVisiable(bool value)
{
m_bVisiable = value;
}
2017-10-14 11:40:47 +08:00
void e2d::ENode::setName(EString name)
{
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);
}
}