305 lines
6.2 KiB
C++
305 lines
6.2 KiB
C++
#include "BaseNode.h"
|
||
|
||
void BaseNode::Init()
|
||
{
|
||
}
|
||
void BaseNode::HandleEvents(SDL_Event *e)
|
||
{
|
||
// 如果有子节点并含有事件标签,则处理事件
|
||
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
|
||
while (child)
|
||
{
|
||
if (child->hasTag(Tag::ACTOR) || child->hasTag(Tag::HANDEL_EVENT))
|
||
child->HandleEvents(e);
|
||
child = child->GetNext();
|
||
}
|
||
}
|
||
|
||
void BaseNode::Update(float deltaTime)
|
||
{
|
||
// 如果有回调函数,则调用回调函数
|
||
if (cb_update_)
|
||
{
|
||
cb_update_(deltaTime);
|
||
}
|
||
// 如果有子节点并含有刷新标签,则更新子节点
|
||
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
|
||
while (child)
|
||
{
|
||
if (child->hasTag(Tag::UPDATE))
|
||
child->Update(deltaTime);
|
||
child = child->GetNext();
|
||
}
|
||
}
|
||
|
||
void BaseNode::Render()
|
||
{
|
||
// 如果有子节点并含有渲染标签,则渲染子节点
|
||
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
|
||
while (child)
|
||
{
|
||
if (child->hasTag(Tag::RENDER))
|
||
child->Render();
|
||
child = child->GetNext();
|
||
}
|
||
}
|
||
|
||
void BaseNode::Clear()
|
||
{
|
||
}
|
||
|
||
void BaseNode::SetCallbackOnUpdate(const UpdateCallback &cb)
|
||
{
|
||
cb_update_ = cb;
|
||
}
|
||
|
||
void BaseNode::SetChildIterationTransform()
|
||
{
|
||
TransformT n_transform;
|
||
n_transform.position = transform.position + transformIter.position;
|
||
n_transform.scale = transform.scale * transformIter.scale;
|
||
n_transform.rotation = transform.rotation + transformIter.rotation;
|
||
|
||
// 如果有子节点并含有transform标签,则设置其位置
|
||
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
|
||
while (child)
|
||
{
|
||
if (child->hasTag(Tag::TRANSFORM))
|
||
child->SetIterationTransform(n_transform);
|
||
child = child->GetNext();
|
||
}
|
||
}
|
||
|
||
void BaseNode::CalcRenderInfo()
|
||
{
|
||
}
|
||
|
||
void BaseNode::SetName(std::string name)
|
||
{
|
||
m_Name = name;
|
||
}
|
||
|
||
std::string BaseNode::GetName()
|
||
{
|
||
return m_Name;
|
||
}
|
||
|
||
int BaseNode::GetRenderZOrder()
|
||
{
|
||
return m_RenderZOrder;
|
||
}
|
||
|
||
void BaseNode::SetRenderZOrder(int zOrder)
|
||
{
|
||
m_RenderZOrder = zOrder;
|
||
Reorder();
|
||
}
|
||
|
||
void BaseNode::Reorder()
|
||
{
|
||
if (m_Parent)
|
||
{
|
||
RefPtr<BaseNode> me = this;
|
||
m_Parent->m_BaseNodes.Remove(me);
|
||
RefPtr<BaseNode> sibling = m_Parent->m_BaseNodes.GetLast();
|
||
if (sibling && sibling->GetRenderZOrder() > m_RenderZOrder)
|
||
{
|
||
sibling = sibling->GetPrev();
|
||
while (sibling)
|
||
{
|
||
if (sibling->GetRenderZOrder() <= m_RenderZOrder)
|
||
break;
|
||
sibling = sibling->GetPrev();
|
||
}
|
||
}
|
||
if (sibling)
|
||
{
|
||
m_Parent->m_BaseNodes.InsertAfter(me, sibling);
|
||
}
|
||
else
|
||
{
|
||
m_Parent->m_BaseNodes.PushFront(me);
|
||
}
|
||
}
|
||
}
|
||
|
||
void BaseNode::AddChild(RefPtr<BaseNode> child)
|
||
{
|
||
m_BaseNodes.PushBack(child);
|
||
child->OnAdded(this);
|
||
// 如果是需要渲染的子对象
|
||
if (child->hasTag(Tag::RENDER))
|
||
{
|
||
// 重新排序
|
||
child->Reorder();
|
||
// 如果组件有transform标签,则设置其位置
|
||
if (child->hasTag(Tag::TRANSFORM))
|
||
{
|
||
TransformT n_transform;
|
||
n_transform.position = transform.position + transformIter.position;
|
||
n_transform.scale = transform.scale * transformIter.scale;
|
||
n_transform.rotation = transform.rotation + transformIter.rotation;
|
||
child->SetIterationTransform(n_transform);
|
||
}
|
||
}
|
||
}
|
||
|
||
void BaseNode::RemoveChild(RefPtr<BaseNode> child)
|
||
{
|
||
child->m_Parent = nullptr;
|
||
m_BaseNodes.Remove(child);
|
||
}
|
||
|
||
void BaseNode::OnAdded(BaseNode *node)
|
||
{
|
||
m_Parent = node;
|
||
}
|
||
|
||
void BaseNode::SetIterationTransform(TransformT n_transform)
|
||
{
|
||
if (n_transform == transformIter)
|
||
return;
|
||
transformIter = n_transform;
|
||
CalcRenderInfo();
|
||
SetChildIterationTransform();
|
||
}
|
||
|
||
TransformT BaseNode::GetIterationTransform()
|
||
{
|
||
return transformIter;
|
||
}
|
||
|
||
void BaseNode::SetTransform(TransformT n_transform)
|
||
{
|
||
if (n_transform == transform)
|
||
return;
|
||
transform = n_transform;
|
||
CalcRenderInfo();
|
||
SetChildIterationTransform();
|
||
}
|
||
|
||
TransformT BaseNode::GetTransform()
|
||
{
|
||
return transform;
|
||
}
|
||
|
||
void BaseNode::SetPos(VecFPos pos)
|
||
{
|
||
if (pos == this->transform.position)
|
||
return;
|
||
this->transform.position = pos;
|
||
CalcRenderInfo();
|
||
SetChildIterationTransform();
|
||
}
|
||
|
||
VecFPos BaseNode::GetPos()
|
||
{
|
||
return this->transform.position;
|
||
}
|
||
|
||
void BaseNode::SetScale(VecFPos scale)
|
||
{
|
||
if (scale == this->transform.scale)
|
||
return;
|
||
this->transform.scale = scale;
|
||
CalcRenderInfo();
|
||
SetChildIterationTransform();
|
||
}
|
||
|
||
VecFPos BaseNode::GetScale()
|
||
{
|
||
return this->transform.scale;
|
||
}
|
||
|
||
void BaseNode::SetRotation(float angle)
|
||
{
|
||
if (angle == this->transform.rotation)
|
||
return;
|
||
this->transform.rotation = angle;
|
||
CalcRenderInfo();
|
||
SetChildIterationTransform();
|
||
}
|
||
|
||
float BaseNode::GetRotation()
|
||
{
|
||
return this->transform.rotation;
|
||
}
|
||
|
||
void BaseNode::SetAnchor(VecFPos anchor)
|
||
{
|
||
if (anchor == this->Anchor)
|
||
return;
|
||
Anchor.x = anchor.x;
|
||
Anchor.y = anchor.y;
|
||
}
|
||
|
||
VecFPos BaseNode::GetAnchor()
|
||
{
|
||
VecFPos P;
|
||
P.x = Anchor.x;
|
||
P.y = Anchor.y;
|
||
return P;
|
||
}
|
||
|
||
void BaseNode::SetSize(VecSize size)
|
||
{
|
||
this->Size = size;
|
||
}
|
||
|
||
VecSize BaseNode::GetSize()
|
||
{
|
||
return this->Size;
|
||
}
|
||
|
||
void BaseNode::SetVisible(bool visible)
|
||
{
|
||
this->Visible = visible;
|
||
}
|
||
|
||
bool BaseNode::GetVisible()
|
||
{
|
||
return this->Visible;
|
||
}
|
||
|
||
void BaseNode::SetIterationPos(VecFPos pos)
|
||
{
|
||
if (pos == this->transformIter.position)
|
||
return;
|
||
this->transformIter.position = pos;
|
||
CalcRenderInfo();
|
||
SetChildIterationTransform();
|
||
}
|
||
|
||
VecFPos BaseNode::GetIterationPos()
|
||
{
|
||
return this->transformIter.position;
|
||
}
|
||
|
||
void BaseNode::SetIterationScale(VecFPos scale)
|
||
{
|
||
if (scale == this->transformIter.scale)
|
||
return;
|
||
this->transformIter.scale = scale;
|
||
CalcRenderInfo();
|
||
SetChildIterationTransform();
|
||
}
|
||
|
||
VecFPos BaseNode::GetIterationScale()
|
||
{
|
||
return this->transformIter.scale;
|
||
}
|
||
|
||
void BaseNode::SetIterationRotation(float angle)
|
||
{
|
||
if (angle == this->transformIter.rotation)
|
||
return;
|
||
this->transformIter.rotation = angle;
|
||
CalcRenderInfo();
|
||
SetChildIterationTransform();
|
||
}
|
||
|
||
float BaseNode::GetIterationRotation()
|
||
{
|
||
return this->transformIter.rotation;
|
||
}
|