ComponentData改用SharedPtr保存,这样在复制Component时,不会对数据产生真正的复制
This commit is contained in:
parent
dd083bb82b
commit
7d41722baa
@ -50,9 +50,9 @@ public:
|
||||
|
||||
ComponentData()=default;
|
||||
virtual ~ComponentData()=default;
|
||||
};//struct ComponentData
|
||||
|
||||
virtual ComponentData *Duplication()=0;
|
||||
};
|
||||
using ComponentDataPtr=SharedPtr<ComponentData>;
|
||||
|
||||
/**
|
||||
* 基础组件<br>
|
||||
@ -66,7 +66,7 @@ class Component
|
||||
|
||||
SceneNode * OwnerNode;
|
||||
ComponentManager * Manager;
|
||||
ComponentData * Data;
|
||||
ComponentDataPtr Data;
|
||||
|
||||
protected:
|
||||
|
||||
@ -81,7 +81,7 @@ protected:
|
||||
public:
|
||||
|
||||
Component()=delete;
|
||||
Component(ComponentData *,ComponentManager *);
|
||||
Component(ComponentDataPtr,ComponentManager *);
|
||||
virtual ~Component();
|
||||
|
||||
virtual const size_t GetHashCode()const=0;
|
||||
@ -92,7 +92,7 @@ public:
|
||||
|
||||
SceneNode * GetOwnerNode()const{return OwnerNode;}
|
||||
ComponentManager * GetManager ()const{return Manager;}
|
||||
ComponentData * GetData ()const{return Data;}
|
||||
ComponentDataPtr GetData ()const{return Data;}
|
||||
|
||||
public:
|
||||
|
||||
@ -137,7 +137,7 @@ public:
|
||||
|
||||
public:
|
||||
|
||||
virtual Component * CreateComponent(ComponentData *)=0;
|
||||
virtual Component * CreateComponent(ComponentDataPtr)=0;
|
||||
|
||||
const size_t GetComponentCount()const{return component_set.GetCount();}
|
||||
|
||||
@ -153,6 +153,12 @@ public: //事件
|
||||
virtual void OnFocusGained(){} ///<焦点获得事件
|
||||
};//class ComponentManager
|
||||
|
||||
#define COMPONENT_MANAGER_CLASS_BODY(name) static name##ComponentManager * GetDefaultManager () {return GetComponentManager<name##ComponentManager>(true);} \
|
||||
static constexpr const size_t StaticHashCode () {return hgl::GetTypeHash<name##ComponentManager>();} \
|
||||
static constexpr const size_t StaticComponentHashCode () {return hgl::GetTypeHash<name##Component>();} \
|
||||
const size_t GetComponentHashCode ()const override{return name##ComponentManager::StaticComponentHashCode();} \
|
||||
const size_t GetHashCode ()const override{return name##ComponentManager::StaticHashCode();} \
|
||||
|
||||
bool RegistryComponentManager(ComponentManager *);
|
||||
ComponentManager *GetComponentManager(const size_t hash_code);
|
||||
|
||||
|
@ -3,10 +3,16 @@
|
||||
#include<hgl/component/RenderComponent.h>
|
||||
#include<hgl/graph/Mesh.h>
|
||||
|
||||
//#include<hgl/log/LogInfo.h>
|
||||
|
||||
COMPONENT_NAMESPACE_BEGIN
|
||||
|
||||
struct MeshComponentData:public ComponentData
|
||||
{
|
||||
//static uint unique_id_count;
|
||||
|
||||
//uint unique_id;
|
||||
|
||||
Mesh *mesh;
|
||||
|
||||
public:
|
||||
@ -14,19 +20,20 @@ public:
|
||||
MeshComponentData()
|
||||
{
|
||||
mesh=nullptr;
|
||||
|
||||
// unique_id=++unique_id_count;
|
||||
// LOG_INFO(AnsiString("MeshComponentData():")+AnsiString::numberOf(unique_id));
|
||||
}
|
||||
|
||||
MeshComponentData(Mesh *m)
|
||||
{
|
||||
mesh=m;
|
||||
|
||||
// unique_id=++unique_id_count;
|
||||
// LOG_INFO(AnsiString("MeshComponentData(Mesh *):")+AnsiString::numberOf(unique_id));
|
||||
}
|
||||
|
||||
virtual ~MeshComponentData();
|
||||
|
||||
ComponentData *Duplication() override
|
||||
{
|
||||
return(new MeshComponentData(mesh));
|
||||
}
|
||||
};//struct MeshComponentData
|
||||
|
||||
class MeshComponent;
|
||||
@ -35,36 +42,20 @@ class MeshComponentManager:public ComponentManager
|
||||
{
|
||||
public:
|
||||
|
||||
static MeshComponentManager *GetDefaultManager()
|
||||
{
|
||||
return GetComponentManager<MeshComponentManager>(true);
|
||||
}
|
||||
|
||||
static constexpr const size_t StaticHashCode (){return hgl::GetTypeHash<MeshComponentManager>();}
|
||||
static constexpr const size_t StaticComponentHashCode (){return hgl::GetTypeHash<MeshComponent>();}
|
||||
|
||||
const size_t GetComponentHashCode ()const override{return MeshComponentManager::StaticComponentHashCode();}
|
||||
const size_t GetHashCode ()const override{return MeshComponentManager::StaticHashCode();}
|
||||
COMPONENT_MANAGER_CLASS_BODY(Mesh)
|
||||
|
||||
public:
|
||||
|
||||
MeshComponentManager()=default;
|
||||
|
||||
MeshComponent *CreateComponent(MeshComponentData *data);
|
||||
Component *CreateComponent(ComponentDataPtr cdp) override;
|
||||
|
||||
MeshComponent *CreateComponent(Mesh *m)
|
||||
{
|
||||
auto sm_cd=new MeshComponentData(m);
|
||||
|
||||
return CreateComponent(sm_cd);
|
||||
}
|
||||
|
||||
virtual Component *CreateComponent(ComponentData *data) override;
|
||||
MeshComponent *CreateComponent(Mesh *);
|
||||
};//class MeshComponentManager
|
||||
|
||||
class MeshComponent:public RenderComponent
|
||||
{
|
||||
MeshComponentData *sm_data;
|
||||
WeakPtr<ComponentData> sm_data;
|
||||
|
||||
public:
|
||||
|
||||
@ -72,14 +63,28 @@ public:
|
||||
|
||||
public:
|
||||
|
||||
MeshComponent(MeshComponentData *cd,MeshComponentManager *cm):RenderComponent(cd,cm){sm_data=cd;}
|
||||
MeshComponent(ComponentDataPtr cdp,MeshComponentManager *cm):RenderComponent(cdp,cm)
|
||||
{
|
||||
sm_data=cdp;
|
||||
}
|
||||
|
||||
virtual ~MeshComponent()=default;
|
||||
|
||||
MeshComponentData &GetData() {return *sm_data;}
|
||||
const MeshComponentData &GetData()const {return *sm_data;}
|
||||
MeshComponentData *GetData() {return dynamic_cast< MeshComponentData *>(sm_data.get());}
|
||||
const MeshComponentData *GetData()const {return dynamic_cast<const MeshComponentData *>(sm_data.const_get());}
|
||||
|
||||
Mesh *GetMesh() const{return sm_data->mesh;}
|
||||
Mesh *GetMesh()const
|
||||
{
|
||||
if(!sm_data.valid())
|
||||
return(nullptr);
|
||||
|
||||
const MeshComponentData *mcd=dynamic_cast<const MeshComponentData *>(sm_data.const_get());
|
||||
|
||||
if(!mcd)
|
||||
return(nullptr);
|
||||
|
||||
return mcd->mesh;
|
||||
}
|
||||
};//class MeshComponent
|
||||
|
||||
COMPONENT_NAMESPACE_END
|
||||
|
@ -6,12 +6,12 @@ namespace hgl::graph
|
||||
{
|
||||
uint Component::unique_id_count=0;
|
||||
|
||||
Component::Component(ComponentData *cd,ComponentManager *cm)
|
||||
Component::Component(ComponentDataPtr cdp,ComponentManager *cm)
|
||||
{
|
||||
unique_id=++unique_id_count;
|
||||
|
||||
OwnerNode=nullptr;
|
||||
Data=cd;
|
||||
Data=cdp;
|
||||
Manager=cm;
|
||||
|
||||
Manager->AttachComponent(this);
|
||||
@ -31,12 +31,10 @@ namespace hgl::graph
|
||||
|
||||
if(Manager)
|
||||
Manager->DetachComponent(this);
|
||||
|
||||
SAFE_CLEAR(Data);
|
||||
}
|
||||
|
||||
Component *Component::Duplication()
|
||||
{
|
||||
return GetManager()->CreateComponent(Data?Data->Duplication():nullptr);
|
||||
return GetManager()->CreateComponent(Data);
|
||||
}
|
||||
}//namespace hgl::graph
|
||||
|
@ -4,8 +4,12 @@
|
||||
|
||||
COMPONENT_NAMESPACE_BEGIN
|
||||
|
||||
//uint MeshComponentData::unique_id_count=0;
|
||||
|
||||
MeshComponentData::~MeshComponentData()
|
||||
{
|
||||
// LOG_INFO(AnsiString("~MeshComponentData():")+AnsiString::numberOf(unique_id));
|
||||
|
||||
if(mesh)
|
||||
{
|
||||
//mesh->Release(); //外面有RenderResource管理,不需要在这里释放.但未来要考虑是增加Release函数通知这里释放了一次使用权
|
||||
@ -13,18 +17,24 @@ MeshComponentData::~MeshComponentData()
|
||||
}
|
||||
}
|
||||
|
||||
Component *MeshComponentManager::CreateComponent(ComponentData *data)
|
||||
Component *MeshComponentManager::CreateComponent(ComponentDataPtr cdp)
|
||||
{
|
||||
if(!data)return(nullptr);
|
||||
if(!cdp)return(nullptr);
|
||||
|
||||
return CreateComponent(reinterpret_cast<MeshComponentData *>(data));
|
||||
if(!dynamic_cast<MeshComponentData *>(cdp.get()))
|
||||
{
|
||||
//LOG_ERROR(OS_TEXT("MeshComponentManager::CreateMeshComponent: invalid component data type."));
|
||||
return(nullptr);
|
||||
}
|
||||
|
||||
return(new MeshComponent(cdp,this));
|
||||
}
|
||||
|
||||
MeshComponent *MeshComponentManager::CreateComponent(MeshComponentData *data)
|
||||
MeshComponent *MeshComponentManager::CreateComponent(Mesh *m)
|
||||
{
|
||||
if(!data)return(nullptr);
|
||||
ComponentDataPtr cdp=new MeshComponentData(m);
|
||||
|
||||
return(new MeshComponent(data,this));
|
||||
return dynamic_cast<MeshComponent *>(CreateComponent(cdp));
|
||||
}
|
||||
|
||||
COMPONENT_NAMESPACE_END
|
||||
|
@ -28,7 +28,10 @@ namespace hgl
|
||||
if(component->GetHashCode()!=MeshComponent::StaticHashCode()) //暂时只支持MeshComponent
|
||||
continue;
|
||||
|
||||
MeshComponent *smc=reinterpret_cast<MeshComponent *>(component);
|
||||
MeshComponent *smc=dynamic_cast<MeshComponent *>(component);
|
||||
|
||||
if(!smc)
|
||||
continue;
|
||||
|
||||
Mesh *mesh=smc->GetMesh();
|
||||
|
||||
|
@ -61,7 +61,7 @@ namespace hgl::graph
|
||||
|
||||
for(Component *com:component_set)
|
||||
{
|
||||
SceneComponent *sc=reinterpret_cast<SceneComponent *>(com);
|
||||
SceneComponent *sc=dynamic_cast<SceneComponent *>(com);
|
||||
|
||||
if(!sc)
|
||||
continue;
|
||||
|
Loading…
x
Reference in New Issue
Block a user