This commit is contained in:
2026-02-08 16:20:50 +08:00
parent 0ae47e5d6a
commit 8b88904ef7
72 changed files with 5963 additions and 2038 deletions

View File

@@ -1,45 +1,41 @@
#include "Actor.h"
#include "EngineFrame/Scene/Scene.h"
#include "EngineCore/Game.h"
#include "EngineFrame/Component/RenderBase.h"
#include "EngineFrame/Render/RenderManager.h"
#include <algorithm>
Actor::Actor()
{
Init();
}
void Actor::Init()
{
addTag(Tag::ACTOR);
addTag(Tag::RENDER);
addTag(Tag::TRANSFORM);
addTag(Tag::UPDATE);
}
void Actor::Update(float deltaTime)
void Actor::OnAdded(Actor *node)
{
if (!Visible)
return;
BaseNode::Update(deltaTime);
}
void Actor::SetBlendMode(LE_BlEND_MODE mode)
{
this->_BlendMode = mode;
}
LE_BlEND_MODE Actor::GetBlendMode()
{
return this->_BlendMode;
}
void Actor::Render()
{
if (!Visible)
return;
// 如果有裁切视口
if (this->_CropViewportFlag)
{
RenderManager *renderer = Game::GetInstance().GetRenderer();
renderer->SetClipRect(&this->_CropViewport);
BaseNode::Render();
Node::Render();
renderer->CloseClipRect();
}
else
{
BaseNode::Render();
Node::Render();
}
}

View File

@@ -1,5 +1,5 @@
#pragma once
#include "EngineFrame/Base/BaseNode.h"
#include "EngineFrame/Base/Node.h"
#include "Tool/IntrusiveList.hpp"
class Scene;
/**
@@ -7,24 +7,31 @@ class Scene;
*
* Actor类是一个基础的游戏对象类可以添加到场景中
*/
class Actor : public BaseNode
class Actor : public Node
{
private:
//裁切视口Flag
protected:
/**裁切视口Flag */
bool _CropViewportFlag = false;
//裁切视口
/**裁切视口 */
SDL_Rect _CropViewport = {0, 0, 0, 0};
/**混合模式 */
LE_BlEND_MODE _BlendMode = NONE;
public:
Actor();
// 初始化
virtual void Init();
// 被添加时
virtual void OnAdded(Actor *node);
// 设置混合模式
void SetBlendMode(LE_BlEND_MODE mode);
// 获取混合模式
LE_BlEND_MODE GetBlendMode();
public:
void Init() override;
void Update(float deltaTime) override;
/**重载渲染函数 */
void Render() override;
// 设置裁切视口(放在Actor里 他与他的子对象都会被裁切)
/**设置裁切视口(放在Actor里 他与他的子对象都会被裁切) */
void SetCropViewport(SDL_Rect CropViewport);
// 获取裁切视口
/**获取裁切视口 */
SDL_Rect GetCropViewport();
};

View File

@@ -1,368 +0,0 @@
#include "BaseNode.h"
#include "EngineCore/Game.h"
BaseNode::BaseNode()
{
Game::GetInstance().m_nodeCount++;
}
BaseNode::~BaseNode()
{
Game::GetInstance().m_nodeCount--;
}
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)
{
// 如果有子节点并含有刷新标签,则更新子节点
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
while (child)
{
if (child->hasTag(Tag::UPDATE))
child->Update(deltaTime);
child = child->GetNext();
}
// 如果有回调函数,则调用回调函数
if (cb_update_.size() > 0)
{
for (auto &cb : cb_update_)
{
cb.second(deltaTime);
}
}
}
void BaseNode::PreRender()
{
// 如果有子节点并含有渲染标签,则渲染子节点
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
while (child)
{
if (child->hasTag(Tag::RENDER))
child->PreRender();
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(std::string Key, const UpdateCallback &cb)
{
cb_update_[Key] = cb;
}
void BaseNode::RemoveCallbackOnUpdate(std::string Key)
{
cb_update_.erase(Key);
}
void BaseNode::SetChildIterationTransform()
{
Transform 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))
{
Transform 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::RemoveAllChild()
{
m_BaseNodes.Clear();
}
void BaseNode::OnAdded(BaseNode *node)
{
m_Parent = node;
}
void BaseNode::SetIterationTransform(Transform n_transform)
{
if (n_transform == transformIter)
return;
transformIter = n_transform;
CalcRenderInfo();
SetChildIterationTransform();
}
Transform BaseNode::GetIterationTransform()
{
return transformIter;
}
void BaseNode::SetTransform(Transform n_transform)
{
if (n_transform == transform)
return;
transform = n_transform;
CalcRenderInfo();
SetChildIterationTransform();
}
Transform BaseNode::GetTransform()
{
return transform;
}
void BaseNode::SetPos(Vec2 pos)
{
if (pos == this->transform.position)
return;
this->transform.position = pos;
CalcRenderInfo();
SetChildIterationTransform();
}
Vec2 BaseNode::GetPos()
{
return this->transform.position;
}
Vec2 BaseNode::GetWorldPos()
{
return this->transform.position + this->transformIter.position;
}
void BaseNode::SetScale(Vec2 scale)
{
if (scale == this->transform.scale)
return;
this->transform.scale = scale;
CalcRenderInfo();
SetChildIterationTransform();
}
Vec2 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(Vec2 anchor)
{
if (anchor == this->Anchor)
return;
Anchor.x = anchor.x;
Anchor.y = anchor.y;
}
Vec2 BaseNode::GetAnchor()
{
Vec2 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::SetAlpha(float alpha)
{
this->Alpha = alpha;
}
BaseNode *BaseNode::GetParent()
{
return this->m_Parent;
}
float BaseNode::GetAlpha()
{
return this->Alpha;
}
void BaseNode::SetIterationPos(Vec2 pos)
{
if (pos == this->transformIter.position)
return;
this->transformIter.position = pos;
CalcRenderInfo();
SetChildIterationTransform();
}
Vec2 BaseNode::GetIterationPos()
{
return this->transformIter.position;
}
void BaseNode::SetIterationScale(Vec2 scale)
{
if (scale == this->transformIter.scale)
return;
this->transformIter.scale = scale;
CalcRenderInfo();
SetChildIterationTransform();
}
Vec2 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;
}
void BaseNode::SetBlendMode(LE_BlEND_MODE mode)
{
this->_BlendMode = mode;
}
LE_BlEND_MODE BaseNode::GetBlendMode()
{
return this->_BlendMode;
}

View File

@@ -1,147 +0,0 @@
#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 "math/Transform.hpp"
#include "math/Math.h"
using namespace ember;
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:
// 更新时的回调函数
std::map<std::string ,UpdateCallback> cb_update_;
// 节点名称
std::string m_Name;
// 子节点列表
IntrusiveList<RefPtr<BaseNode>> m_BaseNodes;
// 指向父对象的指针
BaseNode *m_Parent = nullptr;
// 渲染层级
int m_RenderZOrder = 0;
// 二维仿射变换
Transform transform;
// 迭代的二维仿射变换
Transform transformIter;
// 锚点
Vec2 Anchor = {0.f, 0.f};
// 大小
VecSize Size = {0, 0};
// 透明度
float Alpha = 1.f;
// 混合模式
LE_BlEND_MODE _BlendMode = NONE;
// 是否显示
bool Visible = true;
// 计算渲染信息Flag (为了保证每帧只计算一次)
bool CalcRenderInfoFlag = true;
public:
BaseNode(/* args */);
~BaseNode();
public:
virtual void Init();
virtual void HandleEvents(SDL_Event *e);
virtual void Update(float deltaTime);
virtual void PreRender();
virtual void Render();
virtual void Clear();
/// \~chinese
/// @brief 设置更新时的回调函数
void SetCallbackOnUpdate(std::string Key,const UpdateCallback &cb);
void RemoveCallbackOnUpdate(std::string Key);
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);
// 移除所有子对象
void RemoveAllChild();
// 被添加时
virtual void OnAdded(BaseNode *node);
// 设置迭代的二维仿射变换
void SetIterationTransform(Transform transform);
// 获取迭代的二维仿射变换
Transform GetIterationTransform();
// 设置二维仿射变换
void SetTransform(Transform transform);
// 获取二维仿射变换
Transform GetTransform();
// 设置迭代的坐标
virtual void SetIterationPos(Vec2 pos);
// 获取迭代的坐标
Vec2 GetIterationPos();
// 设置迭代的缩放
virtual void SetIterationScale(Vec2 scale);
// 获取迭代的缩放
Vec2 GetIterationScale();
// 设置迭代的旋转角度
virtual void SetIterationRotation(float angle);
// 获取迭代的旋转角度
float GetIterationRotation();
// 设置坐标
virtual void SetPos(Vec2 pos);
// 获取坐标
Vec2 GetPos();
// 获取世界坐标
Vec2 GetWorldPos();
// 设置缩放
virtual void SetScale(Vec2 scale);
// 获取缩放
Vec2 GetScale();
// 设置旋转角度
virtual void SetRotation(float angle);
// 获取旋转角度
float GetRotation();
// 设置中心点
virtual void SetAnchor(Vec2 anchor);
// 获取中心点
Vec2 GetAnchor();
// 设置大小
virtual void SetSize(VecSize size);
// 获取大小
VecSize GetSize();
// 设置是否显示
virtual void SetVisible(bool visible);
// 获取是否显示
bool GetVisible();
// 设置透明度
virtual void SetAlpha(float alpha);
// 获取透明度
float GetAlpha();
// 设置混合模式
void SetBlendMode(LE_BlEND_MODE mode);
// 获取混合模式
LE_BlEND_MODE GetBlendMode();
//获取父对象
BaseNode *GetParent();
};

View File

@@ -5,11 +5,18 @@ void Node::Init()
}
void Node::HandleEvents(SDL_Event *e)
{
RefPtr<Node> child = children_.GetFirst();
while (child)
{
child->HandleEvents(e);
child = child->GetNext();
}
}
inline void Node::Update(float deltaTime)
{
if (children_.IsEmpty())
{
UpdateSelf(deltaTime);
return;
}
@@ -34,26 +41,118 @@ inline void Node::Update(float deltaTime)
}
inline void Node::PreRender()
{
if (!visible_)
return;
UpdateTransform();
UpdateOpacity();
if (children_.IsEmpty())
{
if (CheckVisibility())
{
OnPreRender();
}
}
else
{
RefPtr<Node> child = children_.GetFirst();
while (child)
{
if (child->GetZOrder() >= 0)
break;
child->PreRender();
child = child->GetNext();
}
if (CheckVisibility())
{
OnPreRender();
}
while (child)
{
child->PreRender();
child = child->GetNext();
}
}
}
inline void Node::Render()
{
if (!visible_)
return;
if (children_.IsEmpty())
{
if (visible_in_rt_)
{
PrepareToRender();
OnRender();
}
}
else
{
RefPtr<Node> child = children_.GetFirst();
while (child)
{
if (child->GetZOrder() >= 0)
break;
child->Render();
child = child->GetNext();
}
if (visible_in_rt_)
{
PrepareToRender();
OnRender();
}
while (child)
{
child->Render();
child = child->GetNext();
}
}
}
void Node::Clear()
{
}
void Node::OnPreRender()
{
}
void Node::OnRender()
{
}
void Node::OnUpdate(float deltaTime)
{
}
void Node::UpdateSelf(float deltaTime)
{
// 如果有回调函数,则调用回调函数
if (cb_update_.size() > 0)
if (!update_pausing_)
{
for (auto &cb : cb_update_)
OnUpdate(deltaTime);
// 如果有回调函数,则调用回调函数
if (cb_update_.size() > 0)
{
cb.second(deltaTime);
for (auto &cb : cb_update_)
{
cb.second(deltaTime);
}
}
}
}
void Node::PrepareToRender()
{
Game::GetInstance().GetRenderer()->SetMatrix(render_matrix_);
Game::GetInstance().GetRenderer()->SetOpacity(displayed_opacity_);
}
void Node::RemoveFromParent()
{
if (parent_)
@@ -109,7 +208,7 @@ void Node::ShowBorder(bool show)
show_border_ = show;
}
Node::Node() : visible_(true), update_pausing_(false), show_border_(false), parent_(nullptr), anchor_(0.0f, 0.0f), z_order_(0), opacity_(1.f), name_("Node")
Node::Node() : visible_(true), update_pausing_(false), show_border_(false), parent_(nullptr), anchor_(0.0f, 0.0f), z_order_(0), opacity_(1.f), name_("Node"), displayed_opacity_(1.f), cascade_opacity_(true)
{
Game::GetInstance().m_nodeCount++;
}
@@ -146,6 +245,8 @@ void Node::UpdateTransform() const
transform_matrix_ *= parent_->transform_matrix_;
}
GenerateRenderMatrix();
for (const auto &child : children_)
child->dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
@@ -165,6 +266,26 @@ void Node::UpdateTransformUpwards() const
UpdateTransform();
}
void Node::UpdateOpacity()
{
if (!dirty_flag_.Has(DirtyFlag::DirtyOpacity))
return;
dirty_flag_.Unset(DirtyFlag::DirtyOpacity);
if (parent_ && parent_->IsCascadeOpacityEnabled())
{
displayed_opacity_ = opacity_ * parent_->displayed_opacity_;
}
else
{
displayed_opacity_ = opacity_;
}
for (const auto &child : children_)
child->dirty_flag_.Set(DirtyFlag::DirtyOpacity);
}
const Matrix3x2 &Node::GetTransformMatrix() const
{
UpdateTransformUpwards();
@@ -188,6 +309,20 @@ const Matrix3x2 &Node::GetTransformMatrixToParent() const
return transform_matrix_to_parent_;
}
void Node::GenerateRenderMatrix() const
{
// 构造OpenGl渲染矩阵
render_matrix_ = glm::mat4(1.0f);
// 填充旋转+缩放分量2D 变换核心,对应 4x4 矩阵左上 2x2 区域)
render_matrix_[0][0] = transform_matrix_[0]; // _11 → x轴缩放+旋转
render_matrix_[0][1] = transform_matrix_[1]; // _12 → 影响y轴方向的旋转分量
render_matrix_[1][0] = transform_matrix_[2]; // _21 → 影响x轴方向的旋转分量
render_matrix_[1][1] = transform_matrix_[3]; // _22 → y轴缩放+旋转
// 填充平移分量(对应 4x4 矩阵最后一行前两列z轴平移为0
render_matrix_[3][0] = transform_matrix_[4]; // _31 → x方向平移世界坐标
render_matrix_[3][1] = transform_matrix_[5]; // _32 → y方向平移世界坐标
}
void Node::Reorder()
{
if (parent_)
@@ -220,287 +355,24 @@ void Node::Reorder()
}
}
inline void Node::RemoveAllChildren()
bool Node::CheckVisibility() const
{
RefPtr<Node> next;
for (RefPtr<Node> child = children_.GetFirst(); child; child = next)
if (dirty_flag_.Has(DirtyFlag::DirtyVisibility))
{
next = child->GetNext();
RemoveChild(child);
}
}
inline bool Node::IsVisible() const
{
return visible_;
}
inline int Node::GetZOrder() const
{
return z_order_;
}
inline glm::vec2 Node::GetPosition() const
{
return transform_.position;
}
inline float Node::GetPositionX() const
{
return GetPosition().x;
}
inline float Node::GetPositionY() const
{
return GetPosition().y;
}
inline VecSize Node::GetSize() const
{
return size_;
}
inline float Node::GetWidth() const
{
return GetSize().width;
}
inline float Node::GetHeight() const
{
return GetSize().height;
}
inline float Node::GetScaledWidth() const
{
return GetWidth() * GetScaleX();
}
inline float Node::GetScaledHeight() const
{
return GetHeight() * GetScaleY();
}
inline VecSize Node::GetScaledSize() const
{
return VecSize{GetScaledWidth(), GetScaledHeight()};
}
inline glm::vec2 Node::GetAnchor() const
{
return anchor_;
}
inline float Node::GetAnchorX() const
{
return GetAnchor().x;
}
inline float Node::GetAnchorY() const
{
return GetAnchor().y;
}
inline float Node::GetOpacity() const
{
return opacity_;
}
inline float Node::GetRotation() const
{
return transform_.rotation;
}
inline glm::vec2 Node::GetScale() const
{
return transform_.scale;
}
inline float Node::GetScaleX() const
{
return GetScale().x;
}
inline float Node::GetScaleY() const
{
return GetScale().y;
}
inline glm::vec2 Node::GetSkew() const
{
return transform_.skew;
}
inline float Node::GetSkewX() const
{
return GetSkew().x;
}
inline float Node::GetSkewY() const
{
return GetSkew().y;
}
inline Y_Transform Node::GetTransform() const
{
return transform_;
}
inline Node *Node::GetParent() const
{
return parent_;
}
inline void Node::SetVisible(bool val)
{
visible_ = val;
}
inline void Node::SetName(std::string name)
{
name_ = name;
}
inline void Node::SetPosition(const glm::vec2 &pos)
{
if (transform_.position == pos)
return;
transform_.position = pos;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetPosition(float x, float y)
{
this->SetPosition(glm::vec2(x, y));
}
inline void Node::SetPositionX(float x)
{
this->SetPosition(glm::vec2(x, GetPosition().y));
}
inline void Node::SetPositionY(float y)
{
this->SetPosition(glm::vec2(GetPosition().x, y));
}
inline void Node::MoveTo(const glm::vec2 &p)
{
this->SetPosition(p);
}
inline void Node::MoveTo(float x, float y)
{
this->SetPosition(glm::vec2(x, y));
}
inline void Node::MoveBy(const glm::vec2 &trans)
{
this->SetPosition(transform_.position.x + trans.x, transform_.position.y + trans.y);
}
inline void Node::MoveBy(float trans_x, float trans_y)
{
this->MoveBy(glm::vec2(trans_x, trans_y));
}
inline void Node::SetScale(const glm::vec2 &scale)
{
if (transform_.scale == scale)
return;
transform_.scale = scale;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetScale(float scalex, float scaley)
{
this->SetScale(glm::vec2(scalex, scaley));
}
inline void Node::SetSkew(const glm::vec2 &skew)
{
if (transform_.skew == skew)
return;
transform_.skew = skew;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetSkew(float skewx, float skewy)
{
this->SetSkew(glm::vec2(skewx, skewy));
}
inline void Node::SetRotation(float rotation)
{
if (transform_.rotation == rotation)
return;
transform_.rotation = rotation;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetAnchor(const glm::vec2 &anchor)
{
if (anchor_ == anchor)
return;
anchor_ = anchor;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetAnchor(float anchorx, float anchory)
{
this->SetAnchor(glm::vec2(anchorx, anchory));
}
inline void Node::SetSize(const VecSize &size)
{
if (size_ == size)
return;
size_ = size;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetSize(float width, float height)
{
this->SetSize(VecSize{width, height});
}
inline void Node::SetWidth(float width)
{
this->SetSize(width, GetHeight());
}
inline void Node::SetHeight(float height)
{
this->SetSize(GetWidth(), height);
}
inline void Node::SetOpacity(float opacity)
{
if (opacity_ == opacity)
return;
opacity_ = std::min(std::max(opacity, 0.f), 1.f);
dirty_flag_.Set(DirtyFlag::DirtyOpacity);
}
inline void Node::SetTransform(const Y_Transform &transform)
{
transform_ = transform;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetZOrder(int zorder)
{
if (z_order_ != zorder)
{
z_order_ = zorder;
Reorder();
dirty_flag_.Unset(DirtyFlag::DirtyVisibility);
if (size_.width == 0.f && size_.height == 0.f)
{
visible_in_rt_ = false;
}
else
{
// TODO
// visible_in_rt_ = ctx.CheckVisibility(GetBounds(), transform_matrix_ /* GetTransformMatrix() */);
visible_in_rt_ = true;
}
}
return visible_in_rt_;
}
void Node::AddChild(RefPtr<Node> child)
@@ -532,7 +404,7 @@ const Node::NodeList &Node::GetAllChildren() const
return children_;
}
inline void Node::RemoveChild(RefPtr<Node> child)
void Node::RemoveChild(RefPtr<Node> child)
{
if (children_.IsEmpty())
return;

View File

@@ -16,13 +16,19 @@ public:
using IntrusiveListValue<RefPtr<Node>>::GetNext;
using IntrusiveListValue<RefPtr<Node>>::GetPrev;
private:
protected:
/**渲染矩阵 */
mutable glm::mat4 render_matrix_;
/**变换属性 */
Y_Transform transform_;
/**变换矩阵 */
mutable Matrix3x2 transform_matrix_;
mutable Matrix3x2 transform_matrix_inverse_;
mutable Matrix3x2 transform_matrix_to_parent_;
private:
/**更新时的回调函数 */
std::map<std::string, UpdateCallback> cb_update_;
/**锚点 */
@@ -33,6 +39,8 @@ private:
VecSize size_;
/**透明度 */
float opacity_;
/**显示透明度 */
float displayed_opacity_;
/**是否可见 */
bool visible_;
@@ -40,6 +48,10 @@ private:
bool update_pausing_;
/**是否显示边界 */
bool show_border_;
/**是否在渲染区域中 */
mutable bool visible_in_rt_;
/**联级透明度 */
bool cascade_opacity_;
/**名称 */
std::string name_;
@@ -58,14 +70,22 @@ public:
virtual void Render();
virtual void Clear();
void UpdateSelf(float dt);
virtual void OnPreRender();
virtual void OnRender();
virtual void OnUpdate(float deltaTime);
void UpdateSelf(float dt);
void PrepareToRender();
public:
/// \~chinese
/// @brief 获取显示状态
bool IsVisible() const;
/// \~chinese
/// @brief 是否启用级联透明度
bool IsCascadeOpacityEnabled() const;
/// \~chinese
/// @brief 获取 Z 轴顺序
int GetZOrder() const;
@@ -160,12 +180,18 @@ public:
/// \~chinese
/// @brief 设置角色是否可见
void SetVisible(bool val);
virtual void SetVisible(bool val);
virtual void OnSetPosition();
/// \~chinese
/// @brief 设置名称
void SetName(std::string name);
/// \~chinese
/// @brief 获取名称
std::string GetName() const;
/// \~chinese
/// @brief 设置坐标
void SetPosition(const glm::vec2 &pos);
@@ -246,6 +272,10 @@ public:
/// @brief 设置透明度,默认为 1.0, 范围 [0, 1]
void SetOpacity(float opacity);
/// \~chinese
/// @brief 启用或禁用级联透明度
void SetCascadeOpacityEnabled(bool enabled);
/// \~chinese
/// @brief 设置二维仿射变换
void SetTransform(const Y_Transform &transform);
@@ -328,6 +358,10 @@ public:
/// @details 对于节点树 A->B(dirty)->C->D当对 D 执行 UpdateTransformUpwards 时会对 B、C、D 从上到下依次更新
void UpdateTransformUpwards() const;
/// \~chinese
/// @brief 更新自己和所有子角色的透明度
void UpdateOpacity();
/// \~chinese
/// @brief 获取二维变换矩阵
const Matrix3x2 &GetTransformMatrix() const;
@@ -340,10 +374,18 @@ public:
/// @brief 获取变换到父角色的二维变换矩阵
const Matrix3x2 &GetTransformMatrixToParent() const;
/// \~chinese
/// @brief 生成用于OpenGL渲染的矩阵
virtual void GenerateRenderMatrix() const;
/// \~chinese
/// @brief 将所有子角色按Z轴顺序排序
void Reorder();
/// \~chinese
/// @brief 检查是否在渲染上下文的视区内
virtual bool CheckVisibility() const;
enum DirtyFlag : uint8_t
{
Clean = 0,
@@ -353,3 +395,310 @@ public:
DirtyVisibility = 1 << 3
};
};
inline void Node::RemoveAllChildren()
{
RefPtr<Node> next;
for (RefPtr<Node> child = children_.GetFirst(); child; child = next)
{
next = child->GetNext();
RemoveChild(child);
}
}
inline bool Node::IsVisible() const
{
return visible_;
}
inline bool Node::IsCascadeOpacityEnabled() const
{
return cascade_opacity_;
}
inline int Node::GetZOrder() const
{
return z_order_;
}
inline glm::vec2 Node::GetPosition() const
{
return transform_.position;
}
inline float Node::GetPositionX() const
{
return GetPosition().x;
}
inline float Node::GetPositionY() const
{
return GetPosition().y;
}
inline VecSize Node::GetSize() const
{
return size_;
}
inline float Node::GetWidth() const
{
return GetSize().width;
}
inline float Node::GetHeight() const
{
return GetSize().height;
}
inline float Node::GetScaledWidth() const
{
return GetWidth() * GetScaleX();
}
inline float Node::GetScaledHeight() const
{
return GetHeight() * GetScaleY();
}
inline VecSize Node::GetScaledSize() const
{
return VecSize{GetScaledWidth(), GetScaledHeight()};
}
inline glm::vec2 Node::GetAnchor() const
{
return anchor_;
}
inline float Node::GetAnchorX() const
{
return GetAnchor().x;
}
inline float Node::GetAnchorY() const
{
return GetAnchor().y;
}
inline float Node::GetOpacity() const
{
return opacity_;
}
inline float Node::GetRotation() const
{
return transform_.rotation;
}
inline glm::vec2 Node::GetScale() const
{
return transform_.scale;
}
inline float Node::GetScaleX() const
{
return GetScale().x;
}
inline float Node::GetScaleY() const
{
return GetScale().y;
}
inline glm::vec2 Node::GetSkew() const
{
return transform_.skew;
}
inline float Node::GetSkewX() const
{
return GetSkew().x;
}
inline float Node::GetSkewY() const
{
return GetSkew().y;
}
inline Y_Transform Node::GetTransform() const
{
return transform_;
}
inline Node *Node::GetParent() const
{
return parent_;
}
inline void Node::SetVisible(bool val)
{
visible_ = val;
}
inline void Node::OnSetPosition()
{
}
inline void Node::SetName(std::string name)
{
name_ = name;
}
inline std::string Node::GetName() const
{
return name_;
}
inline void Node::SetPosition(const glm::vec2 &pos)
{
if (transform_.position == pos)
return;
transform_.position = pos;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
OnSetPosition();
}
inline void Node::SetPosition(float x, float y)
{
this->SetPosition(glm::vec2(x, y));
}
inline void Node::SetPositionX(float x)
{
this->SetPosition(glm::vec2(x, GetPosition().y));
}
inline void Node::SetPositionY(float y)
{
this->SetPosition(glm::vec2(GetPosition().x, y));
}
inline void Node::MoveTo(const glm::vec2 &p)
{
this->SetPosition(p);
}
inline void Node::MoveTo(float x, float y)
{
this->SetPosition(glm::vec2(x, y));
}
inline void Node::MoveBy(const glm::vec2 &trans)
{
this->SetPosition(transform_.position.x + trans.x, transform_.position.y + trans.y);
}
inline void Node::MoveBy(float trans_x, float trans_y)
{
this->MoveBy(glm::vec2(trans_x, trans_y));
}
inline void Node::SetScale(const glm::vec2 &scale)
{
if (transform_.scale == scale)
return;
transform_.scale = scale;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetScale(float scalex, float scaley)
{
this->SetScale(glm::vec2(scalex, scaley));
}
inline void Node::SetSkew(const glm::vec2 &skew)
{
if (transform_.skew == skew)
return;
transform_.skew = skew;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetSkew(float skewx, float skewy)
{
this->SetSkew(glm::vec2(skewx, skewy));
}
inline void Node::SetRotation(float rotation)
{
if (transform_.rotation == rotation)
return;
transform_.rotation = rotation;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetAnchor(const glm::vec2 &anchor)
{
if (anchor_ == anchor)
return;
anchor_ = anchor;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetAnchor(float anchorx, float anchory)
{
this->SetAnchor(glm::vec2(anchorx, anchory));
}
inline void Node::SetSize(const VecSize &size)
{
if (size_ == size)
return;
size_ = size;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetSize(float width, float height)
{
this->SetSize(VecSize{width, height});
}
inline void Node::SetWidth(float width)
{
this->SetSize(width, GetHeight());
}
inline void Node::SetHeight(float height)
{
this->SetSize(GetWidth(), height);
}
inline void Node::SetOpacity(float opacity)
{
if (opacity_ == opacity)
return;
opacity_ = std::min(std::max(opacity, 0.f), 1.f);
dirty_flag_.Set(DirtyFlag::DirtyOpacity);
}
inline void Node::SetCascadeOpacityEnabled(bool enabled)
{
if (cascade_opacity_ == enabled)
return;
cascade_opacity_ = enabled;
dirty_flag_.Set(DirtyFlag::DirtyOpacity);
}
inline void Node::SetTransform(const Y_Transform &transform)
{
transform_ = transform;
dirty_flag_.Set(DirtyFlag::DirtyTransform);
}
inline void Node::SetZOrder(int zorder)
{
if (z_order_ != zorder)
{
z_order_ = zorder;
Reorder();
}
}