2025-03-18 01:26:35 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include<hgl/type/DataType.h>
|
2025-03-20 02:09:50 +08:00
|
|
|
|
#include<hgl/type/SortedSet.h>
|
2025-05-04 19:33:04 +08:00
|
|
|
|
#include<hgl/type/ArrayList.h>
|
2025-03-18 01:26:35 +08:00
|
|
|
|
|
2025-03-25 23:15:38 +08:00
|
|
|
|
/**
|
|
|
|
|
* Component/Data/Manager 体系设计简要说明
|
|
|
|
|
*
|
|
|
|
|
* 本体系参考AMD FidelityFX,但并不完全一致。
|
|
|
|
|
*
|
|
|
|
|
* AMD FidelityFX中,Component存放于Entity下,而我方中与其类似的定义为SceneNode。
|
|
|
|
|
* 不管是Entity还是SceneNode,它们都提供空间变换,以及子节点、Component的管理。
|
2025-06-04 00:10:27 +08:00
|
|
|
|
* 而AMD FidelityFX中的Scene,类似于我方的Scene,用于储存一个场景世界的根节点及其它世界唯一数据。
|
2025-03-25 23:15:38 +08:00
|
|
|
|
*
|
|
|
|
|
* ComponentData是每个Component的数据,用于向Component或是其它模块提供数据。
|
|
|
|
|
* ComponentManager是Component的管理器,用于管理Component的创建、销毁、更新等。
|
|
|
|
|
*
|
2025-06-04 00:10:27 +08:00
|
|
|
|
* 需要注意的是:同AMD FidelityFX一样,大部分ComponentManager与Scene基本无关。
|
2025-04-01 01:11:32 +08:00
|
|
|
|
* 因为同样的数据可能出现在多个World之中。
|
2025-06-04 00:10:27 +08:00
|
|
|
|
* 仅有那些与Scene密切相关的Component它对应的Manager才会出现在Scene中,比如CameraManager/LightManager。
|
2025-06-15 17:53:15 +08:00
|
|
|
|
* 而如MeshComponent之类的纯资源型就会是独立存在的。
|
2025-04-01 01:11:32 +08:00
|
|
|
|
*
|
2025-03-25 23:15:38 +08:00
|
|
|
|
* Component是组件的基类,所有组件都从这里派生。
|
|
|
|
|
*
|
2025-06-14 23:42:50 +08:00
|
|
|
|
* SceneComponent是场景组件基类,只要是放在场景中的都从它派生,
|
|
|
|
|
*
|
|
|
|
|
* PrimitiveComponent是图元组件的基类,所有图元组件都从这里派生。
|
2025-03-25 23:15:38 +08:00
|
|
|
|
* 它再度派生出的任何Component都必须是一个有3D空间的几何图元。
|
|
|
|
|
* 引擎中的空间、物理、等都由PrimitiveComponent提供数据进行计算。
|
2025-06-14 23:42:50 +08:00
|
|
|
|
|
|
|
|
|
* RenderComponent是可渲染组件的基类,所有可渲染组件都从这里派生。
|
2025-03-25 23:15:38 +08:00
|
|
|
|
*
|
2025-06-15 17:53:15 +08:00
|
|
|
|
* MeshComponent是静态网格组件,它是一个具体的RenderComponent实现。
|
2025-03-25 23:15:38 +08:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2025-03-25 02:19:32 +08:00
|
|
|
|
#define COMPONENT_NAMESPACE hgl::graph
|
|
|
|
|
#define COMPONENT_NAMESPACE_BEGIN namespace COMPONENT_NAMESPACE {
|
|
|
|
|
#define COMPONENT_NAMESPACE_END }
|
|
|
|
|
|
|
|
|
|
COMPONENT_NAMESPACE_BEGIN
|
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
class ComponentManager;
|
|
|
|
|
class SceneNode;
|
2025-03-20 02:09:50 +08:00
|
|
|
|
|
2025-06-14 04:13:49 +08:00
|
|
|
|
struct ComponentData
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
ComponentData()=default;
|
|
|
|
|
virtual ~ComponentData()=default;
|
|
|
|
|
};
|
2025-03-20 02:09:50 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
/**
|
|
|
|
|
* 基础组件<br>
|
|
|
|
|
* 是一切组件的基类
|
|
|
|
|
*/
|
|
|
|
|
class Component
|
|
|
|
|
{
|
2025-06-15 00:49:23 +08:00
|
|
|
|
static uint unique_id_count;
|
|
|
|
|
|
|
|
|
|
uint unique_id;
|
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
SceneNode * OwnerNode;
|
|
|
|
|
ComponentManager * Manager;
|
|
|
|
|
ComponentData * Data;
|
2025-03-20 02:09:50 +08:00
|
|
|
|
|
2025-06-15 00:49:23 +08:00
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
friend class ComponentManager;
|
|
|
|
|
|
|
|
|
|
virtual void OnDetachManager(ComponentManager *cm)
|
|
|
|
|
{
|
|
|
|
|
if(cm==Manager)
|
|
|
|
|
Manager=nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
public:
|
2025-03-25 01:04:46 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
Component()=delete;
|
2025-06-14 02:32:15 +08:00
|
|
|
|
Component(ComponentData *,ComponentManager *);
|
|
|
|
|
virtual ~Component();
|
2025-04-03 01:35:39 +08:00
|
|
|
|
|
|
|
|
|
virtual const size_t GetHashCode()const=0;
|
2025-03-25 01:17:18 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
public:
|
2025-03-25 01:17:18 +08:00
|
|
|
|
|
2025-06-15 00:49:23 +08:00
|
|
|
|
uint GetUniqueID ()const{return unique_id;}
|
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
SceneNode * GetOwnerNode()const{return OwnerNode;}
|
|
|
|
|
ComponentManager * GetManager ()const{return Manager;}
|
|
|
|
|
ComponentData * GetData ()const{return Data;}
|
2025-03-18 01:26:35 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
public:
|
2025-03-20 02:09:50 +08:00
|
|
|
|
|
2025-06-16 00:02:07 +08:00
|
|
|
|
virtual Component *Duplication()=0;
|
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
//virtual void Update(const double delta_time)=0;
|
2025-03-25 01:04:46 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
public: //事件
|
2025-03-20 02:09:50 +08:00
|
|
|
|
|
2025-06-14 02:32:15 +08:00
|
|
|
|
virtual void OnAttach(SceneNode *node){if(node)OwnerNode=node;} ///<附加到节点事件
|
2025-06-15 00:49:23 +08:00
|
|
|
|
virtual void OnDetach(SceneNode *){OwnerNode=nullptr;} ///<从节点分离事件
|
2025-06-14 02:32:15 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
virtual void OnFocusLost(){} ///<焦点丢失事件
|
|
|
|
|
virtual void OnFocusGained(){} ///<焦点获得事件
|
|
|
|
|
};//class Component
|
2025-03-25 01:17:18 +08:00
|
|
|
|
|
2025-06-16 00:02:07 +08:00
|
|
|
|
#define COMPONENT_CLASS_BODY(name) static name##ComponentManager *GetDefaultManager () {return name##ComponentManager::GetDefaultManager();} \
|
|
|
|
|
name##ComponentManager *GetManager ()const {return (name##ComponentManager *)Component::GetManager();} \
|
|
|
|
|
static constexpr const size_t StaticHashCode () {return hgl::GetTypeHash<name##Component>();} \
|
|
|
|
|
const size_t GetHashCode ()const override{return name##Component::StaticHashCode();}
|
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
using ComponentSet=SortedSet<Component *>;
|
2025-06-14 23:42:50 +08:00
|
|
|
|
using ComponentList=ArrayList<Component *>;
|
2025-03-20 02:09:50 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
class ComponentManager
|
|
|
|
|
{
|
|
|
|
|
ComponentSet component_set;
|
2025-03-20 02:09:50 +08:00
|
|
|
|
|
2025-06-14 02:32:15 +08:00
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
friend class Component; //Component可以直接访问ComponentManager的成员
|
|
|
|
|
|
|
|
|
|
virtual void AttachComponent(Component *c){if(!c)return;component_set.Add(c);}
|
|
|
|
|
virtual void DetachComponent(Component *c){if(!c)return;component_set.Delete(c);}
|
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
public:
|
2025-03-20 02:09:50 +08:00
|
|
|
|
|
2025-04-03 01:35:39 +08:00
|
|
|
|
virtual const size_t GetComponentHashCode()const=0;
|
|
|
|
|
virtual const size_t GetHashCode()const=0;
|
|
|
|
|
|
2025-06-15 00:49:23 +08:00
|
|
|
|
virtual ~ComponentManager();
|
2025-03-18 01:26:35 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
public:
|
2025-03-18 01:26:35 +08:00
|
|
|
|
|
2025-06-12 03:01:50 +08:00
|
|
|
|
virtual Component * CreateComponent(ComponentData *)=0;
|
2025-03-25 01:04:46 +08:00
|
|
|
|
|
2025-06-15 00:49:23 +08:00
|
|
|
|
const size_t GetComponentCount()const{return component_set.GetCount();}
|
2025-03-25 01:04:46 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
ComponentSet & GetComponents(){return component_set;}
|
2025-03-25 01:17:18 +08:00
|
|
|
|
|
2025-06-15 00:49:23 +08:00
|
|
|
|
int GetComponents(ComponentList &comp_list,SceneNode *);
|
2025-03-25 01:04:46 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
virtual void UpdateComponents(const double delta_time);
|
2025-03-25 01:04:46 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
public: //事件
|
2025-03-25 02:19:32 +08:00
|
|
|
|
|
2025-04-01 01:11:32 +08:00
|
|
|
|
virtual void OnFocusLost(){} ///<焦点丢失事件
|
|
|
|
|
virtual void OnFocusGained(){} ///<焦点获得事件
|
|
|
|
|
};//class ComponentManager
|
2025-04-03 01:35:39 +08:00
|
|
|
|
|
|
|
|
|
bool RegistryComponentManager(ComponentManager *);
|
|
|
|
|
ComponentManager *GetComponentManager(const size_t hash_code);
|
|
|
|
|
|
|
|
|
|
template<typename T> inline T *GetComponentManager(bool create_default=true)
|
|
|
|
|
{
|
|
|
|
|
T *cm=(T *)GetComponentManager(T::StaticHashCode());
|
|
|
|
|
|
|
|
|
|
if(!cm&&create_default)
|
|
|
|
|
{
|
|
|
|
|
cm=new T;
|
|
|
|
|
|
|
|
|
|
RegistryComponentManager(cm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cm;
|
|
|
|
|
}
|
2025-03-25 02:19:32 +08:00
|
|
|
|
COMPONENT_NAMESPACE_END
|