建档
This commit is contained in:
303
source/EngineFrame/Base/BaseNode.cpp
Normal file
303
source/EngineFrame/Base/BaseNode.cpp
Normal file
@@ -0,0 +1,303 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
124
source/EngineFrame/Base/BaseNode.h
Normal file
124
source/EngineFrame/Base/BaseNode.h
Normal file
@@ -0,0 +1,124 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <SDL.h>
|
||||
#include <string>
|
||||
#include "Tool/RefObject.h"
|
||||
#include "Tool/RefPtr.h"
|
||||
#include "Tool/IntrusiveList.hpp"
|
||||
#include "Tool/TagGed.h"
|
||||
#include "Tool/TransformT.h"
|
||||
class BaseNode : public RefObject, public TagGed, protected IntrusiveListValue<RefPtr<BaseNode>>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef std::function<void(float deltaTime)> UpdateCallback;
|
||||
|
||||
using IntrusiveListValue<RefPtr<BaseNode>>::GetNext;
|
||||
using IntrusiveListValue<RefPtr<BaseNode>>::GetPrev;
|
||||
|
||||
public:
|
||||
// 更新时的回调函数
|
||||
UpdateCallback cb_update_;
|
||||
// 节点名称
|
||||
std::string m_Name;
|
||||
// 子节点列表
|
||||
IntrusiveList<RefPtr<BaseNode>> m_BaseNodes;
|
||||
// 指向父对象的指针
|
||||
BaseNode *m_Parent = nullptr;
|
||||
// 渲染层级
|
||||
int m_RenderZOrder = 0;
|
||||
// 二维仿射变换
|
||||
TransformT transform;
|
||||
// 迭代的二维仿射变换
|
||||
TransformT transformIter;
|
||||
// 锚点
|
||||
VecFPos Anchor = {0.f, 0.f};
|
||||
// 大小
|
||||
VecSize Size = {0, 0};
|
||||
// 是否显示
|
||||
bool Visible = true;
|
||||
// 计算渲染信息Flag (为了保证每帧只计算一次)
|
||||
bool CalcRenderInfoFlag = true;
|
||||
|
||||
public:
|
||||
BaseNode(/* args */) = default;
|
||||
~BaseNode() = default;
|
||||
|
||||
public:
|
||||
virtual void Init();
|
||||
virtual void HandleEvents(SDL_Event *e);
|
||||
virtual void Update(float deltaTime);
|
||||
virtual void Render();
|
||||
virtual void Clear();
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 设置更新时的回调函数
|
||||
void SetCallbackOnUpdate(const UpdateCallback &cb);
|
||||
|
||||
void SetChildIterationTransform();
|
||||
|
||||
// 计算渲染信息
|
||||
virtual void CalcRenderInfo();
|
||||
// 设置名字
|
||||
void SetName(std::string name);
|
||||
// 获取名字
|
||||
std::string GetName();
|
||||
// 设置渲染层级
|
||||
int GetRenderZOrder();
|
||||
// 获取渲染层级
|
||||
void SetRenderZOrder(int zOrder);
|
||||
// 重新排序
|
||||
void Reorder();
|
||||
// 添加子对象
|
||||
virtual void AddChild(RefPtr<BaseNode> child);
|
||||
// 移除子对象
|
||||
void RemoveChild(RefPtr<BaseNode> child);
|
||||
// 被添加时
|
||||
virtual void OnAdded(BaseNode *node);
|
||||
// 设置迭代的二维仿射变换
|
||||
void SetIterationTransform(TransformT transform);
|
||||
// 获取迭代的二维仿射变换
|
||||
TransformT GetIterationTransform();
|
||||
// 设置二维仿射变换
|
||||
void SetTransform(TransformT transform);
|
||||
// 获取二维仿射变换
|
||||
TransformT GetTransform();
|
||||
|
||||
// 设置迭代的坐标
|
||||
virtual void SetIterationPos(VecFPos pos);
|
||||
// 获取迭代的坐标
|
||||
VecFPos GetIterationPos();
|
||||
// 设置迭代的缩放
|
||||
virtual void SetIterationScale(VecFPos scale);
|
||||
// 获取迭代的缩放
|
||||
VecFPos GetIterationScale();
|
||||
// 设置迭代的旋转角度
|
||||
virtual void SetIterationRotation(float angle);
|
||||
// 获取迭代的旋转角度
|
||||
float GetIterationRotation();
|
||||
|
||||
// 设置坐标
|
||||
virtual void SetPos(VecFPos pos);
|
||||
// 获取坐标
|
||||
VecFPos GetPos();
|
||||
// 设置缩放
|
||||
virtual void SetScale(VecFPos scale);
|
||||
// 获取缩放
|
||||
VecFPos GetScale();
|
||||
// 设置旋转角度
|
||||
virtual void SetRotation(float angle);
|
||||
// 获取旋转角度
|
||||
float GetRotation();
|
||||
// 设置中心点
|
||||
virtual void SetAnchor(VecFPos anchor);
|
||||
// 获取中心点
|
||||
VecFPos GetAnchor();
|
||||
// 设置大小
|
||||
virtual void SetSize(VecSize size);
|
||||
// 获取大小
|
||||
VecSize GetSize();
|
||||
// 设置是否显示
|
||||
void SetVisible(bool visible);
|
||||
// 获取是否显示
|
||||
bool GetVisible();
|
||||
};
|
||||
Reference in New Issue
Block a user