ULRE/inc/hgl/graph/SceneNode.h

141 lines
6.1 KiB
C
Raw Normal View History

#pragma once
2019-05-21 21:28:33 +08:00
2023-07-28 20:17:46 +08:00
#include<hgl/type/ObjectList.h>
2024-09-06 01:04:28 +08:00
#include<hgl/type/IDName.h>
2019-05-21 21:28:33 +08:00
#include<hgl/graph/SceneOrient.h>
2021-09-24 01:53:44 +08:00
#include<hgl/graph/AABB.h>
#include<hgl/component/Component.h>
namespace hgl::graph
2019-05-21 21:28:33 +08:00
{
2025-06-14 02:32:15 +08:00
using SceneNodeID =int64;
2025-06-07 05:43:31 +08:00
HGL_DEFINE_U16_IDNAME(SceneNodeName)
/**
* <br>
* (SceneOrient)
* (/)
*/
class SceneNode:public SceneOrient ///场景节点类
2019-05-21 21:28:33 +08:00
{
SceneNode *parent_node=nullptr; ///<上级节点
SceneNodeID node_id=-1; ///<节点ID
SceneNodeName node_name; ///<节点名称
2024-09-06 01:04:28 +08:00
protected:
2019-05-21 21:28:33 +08:00
AABB bounding_box; ///<绑定盒
AABB local_bounding_box; ///<本地坐标绑定盒
//AABB WorldBoundingBox; ///<世界坐标绑定盒
2019-05-21 21:28:33 +08:00
protected:
2019-05-21 21:28:33 +08:00
ObjectList<SceneNode> child_nodes; ///<子节点
2025-06-14 02:32:15 +08:00
/**
* SceneNode下可能会包含多个组件SceneNode使用
* ComponentManager管理
*/
ComponentSet component_set; ///<组件合集
public:
2019-05-21 21:28:33 +08:00
const SceneNodeID & GetNodeID ()const { return node_id; } ///<取得节点ID
const SceneNodeName & GetNodeName ()const { return node_name; } ///<取得节点名称
2024-09-06 01:04:28 +08:00
const ObjectList<SceneNode> &GetChildNode()const { return child_nodes; } ///<取得子节点列表
2024-10-06 02:30:59 +08:00
public:
2024-09-06 01:04:28 +08:00
SceneNode()=default;
SceneNode(const SceneNode &)=delete;
SceneNode(const SceneNode *)=delete;
SceneNode(const SceneOrient &so ):SceneOrient(so) {}
SceneNode(const Matrix4f &mat ):SceneOrient(mat) {}
2019-05-21 21:28:33 +08:00
public:
2024-09-06 01:04:28 +08:00
virtual ~SceneNode();
void Clear() override
{
SceneOrient::Clear();
parent_node=nullptr;
bounding_box.SetZero();
local_bounding_box.SetZero();
child_nodes.Clear();
component_set.Clear();
}
2024-07-28 23:34:04 +08:00
const bool ChildNodeIsEmpty()const
{
if(child_nodes.GetCount())return(false);
2024-07-28 23:34:04 +08:00
return(true);
}
void SetParent(SceneNode *sn) {parent_node=sn;}
SceneNode * GetParent() noexcept{return parent_node;}
const SceneNode * GetParent()const noexcept{return parent_node;}
SceneNode *Add(SceneNode *sn)
{
if(!sn)
return(nullptr);
child_nodes.Add(sn);
sn->SetParent(this);
return sn;
}
public: //坐标相关方法
2019-05-21 21:28:33 +08:00
virtual void SetBoundingBox (const AABB &bb){bounding_box=bb;} ///<设置绑定盒
2019-05-21 21:28:33 +08:00
virtual void RefreshMatrix () override; ///<刷新世界变换
virtual void RefreshBoundingBox (); ///<刷新绑定盒
2019-05-21 21:28:33 +08:00
virtual const AABB & GetBoundingBox ()const{return bounding_box;} ///<取得绑定盒
virtual const AABB & GetLocalBoundingBox ()const{return local_bounding_box;} ///<取得本地坐标绑定盒
// virtual const AABB & GetWorldBoundingBox ()const{return WorldBoundingBox;} ///<取得世界坐标绑定盒
public: //组件相关方法
bool ComponentIsEmpty ()const{return component_set.IsEmpty();} ///<是否没有组件
virtual const int64 GetComponentCount ()const{return component_set.GetCount();} ///<取得组件数量
2025-06-14 21:05:36 +08:00
virtual bool AttachComponent (Component *comp) ///<添加一个组件
{
if(!comp)return(false);
if(component_set.Add(comp)<0)
2025-06-14 21:05:36 +08:00
return(false);
comp->OnAttach(this); //调用组件的OnAttach方法
return(true);
}
virtual void DetachComponent (Component *comp) ///<删除一个组件
{
if (!comp)return;
component_set.Delete(comp);
2025-06-14 21:05:36 +08:00
comp->OnDetach(this); //调用组件的OnDetach方法
}
bool Contains (Component *comp){return component_set.Contains(comp);} ///<是否包含指定组件
bool HasComponent (const ComponentManager *); ///<是否有指定组件管理器的组件
virtual int GetComponents (ComponentList &comp_list,const ComponentManager *); ///<取得所有组件
const ComponentSet &GetComponents ()const{return component_set;}
};//class SceneNode
2024-09-06 01:04:28 +08:00
SceneNode *Duplication(SceneNode *); ///<复制一个场景节点
}//namespace hgl::graph