修复组件能多次加载得问题

This commit is contained in:
brige 2025-07-20 15:19:43 +08:00
parent cdb7894e64
commit 37b0259579
17 changed files with 79 additions and 35 deletions

View File

@ -13,7 +13,7 @@ public:
~AngleReflexComponent(); ~AngleReflexComponent();
static std::string GetTypeName(); static std::string GetTypeName();
std::string GetSelfTypeName() override { std::string GetSelfTypeName() const override {
return AngleReflexComponent::GetTypeName(); return AngleReflexComponent::GetTypeName();
} }

View File

@ -13,7 +13,7 @@ public:
~ChaffBombsComponent(); ~ChaffBombsComponent();
static std::string GetTypeName(); static std::string GetTypeName();
std::string GetSelfTypeName() override { std::string GetSelfTypeName() const override {
return ChaffBombsComponent::GetTypeName(); return ChaffBombsComponent::GetTypeName();
} }

View File

@ -86,6 +86,10 @@ std::vector<SceneComponent*> Component::GetChildren() const {
return std::vector<SceneComponent*>(); return std::vector<SceneComponent*>();
} }
bool Component::HasComponent(const std::string& name) const {
return false;
}
bool Component::OnDestroy() { bool Component::OnDestroy() {
return true; return true;
} }
@ -94,7 +98,7 @@ std::string Component::GetTypeName() {
return "Component"; return "Component";
} }
std::string Component::GetSelfTypeName() { std::string Component::GetSelfTypeName() const {
return Component::GetTypeName(); return Component::GetTypeName();
} }

View File

@ -26,9 +26,10 @@ public:
virtual bool OnDestroy(); virtual bool OnDestroy();
static std::string GetTypeName(); static std::string GetTypeName();
virtual std::string GetSelfTypeName(); virtual std::string GetSelfTypeName() const;
virtual void AttachEntity(class Entity* owner); virtual void AttachEntity(class Entity* owner);
virtual bool AttachTo(class SceneComponent* parent); virtual bool AttachTo(class SceneComponent* parent);
virtual bool HasComponent(const std::string& name) const;
class Entity* GetEntity() const { class Entity* GetEntity() const {
return owner_; return owner_;
} }
@ -36,6 +37,7 @@ public:
return uuid_; return uuid_;
} }
protected: protected:
void SetUUId(const QString& uuid) { void SetUUId(const QString& uuid) {
uuid_ = uuid; uuid_ = uuid;

View File

@ -15,7 +15,7 @@ public:
~ConeWaveComponent(); ~ConeWaveComponent();
static std::string GetTypeName(); static std::string GetTypeName();
std::string GetSelfTypeName() override { std::string GetSelfTypeName() const override {
return ConeWaveComponent::GetTypeName(); return ConeWaveComponent::GetTypeName();
} }

View File

@ -13,7 +13,7 @@ public:
~DashedLineComponent(); ~DashedLineComponent();
static std::string GetTypeName(); static std::string GetTypeName();
std::string GetSelfTypeName() override { std::string GetSelfTypeName() const override {
return DashedLineComponent::GetTypeName(); return DashedLineComponent::GetTypeName();
} }

View File

@ -183,6 +183,13 @@ void Entity::SetParent(Entity* parent) {
parent_->childer_.push_back(this); parent_->childer_.push_back(this);
} }
bool Entity::HasComponent(const std::string& name) const {
if (nullptr == rootComponet_) {
return false;
}
return rootComponet_->HasComponent(name);
}
void Entity::SetVisible(bool v) { void Entity::SetVisible(bool v) {
LOG_INFO("set visible: {}", v); LOG_INFO("set visible: {}", v);
if (nullptr == rootComponet_) { if (nullptr == rootComponet_) {

View File

@ -56,6 +56,8 @@ public:
return rootComponet_->GetComponent(); return rootComponet_->GetComponent();
} }
bool HasComponent(const std::string& name) const;
void SetRootComponent(SceneComponent* root) { void SetRootComponent(SceneComponent* root) {
rootComponet_ = root; rootComponet_ = root;
} }

View File

@ -78,7 +78,7 @@ bool LabelComponent::SaveAttribute(tinyxml2::XMLElement* element) {
return SceneComponent::SaveAttribute(element); return SceneComponent::SaveAttribute(element);
} }
std::string LabelComponent::GetSelfTypeName() { std::string LabelComponent::GetSelfTypeName() const {
return LabelComponent::GetTypeName(); return LabelComponent::GetTypeName();
} }

View File

@ -20,7 +20,7 @@ public:
void AttachEntity(class Entity* owner) override; void AttachEntity(class Entity* owner) override;
bool SetAttribute(const char* name, const char* value) override; bool SetAttribute(const char* name, const char* value) override;
bool SaveAttribute(tinyxml2::XMLElement* element) override; bool SaveAttribute(tinyxml2::XMLElement* element) override;
std::string GetSelfTypeName() override; std::string GetSelfTypeName() const override;
void Begin() override; void Begin() override;
void Update(double dt) override; void Update(double dt) override;

View File

@ -29,7 +29,7 @@ bool MeshComponent::SaveAttribute(tinyxml2::XMLElement* element) {
return SceneComponent::SaveAttribute(element); return SceneComponent::SaveAttribute(element);
} }
std::string MeshComponent::GetSelfTypeName() { std::string MeshComponent::GetSelfTypeName() const {
return MeshComponent::GetTypeName(); return MeshComponent::GetTypeName();
} }

View File

@ -16,7 +16,7 @@ public:
bool SetAttribute(const char* name, const char* value) override; bool SetAttribute(const char* name, const char* value) override;
bool SaveAttribute(tinyxml2::XMLElement* element) override; bool SaveAttribute(tinyxml2::XMLElement* element) override;
std::string GetSelfTypeName() override; std::string GetSelfTypeName() const override;
bool LoadNode(const char* mesh); bool LoadNode(const char* mesh);
const std::string& GetMesh() const { const std::string& GetMesh() const {

View File

@ -12,7 +12,7 @@ public:
~PathComponent(); ~PathComponent();
static std::string GetTypeName(); static std::string GetTypeName();
std::string GetSelfTypeName() override { std::string GetSelfTypeName() const override {
return PathComponent::GetTypeName(); return PathComponent::GetTypeName();
} }

View File

@ -262,6 +262,19 @@ void SceneComponent::AddToRender() {
} }
} }
bool SceneComponent::HasComponent(const std::string& name) const {
if (GetSelfTypeName() == name) {
return true;
}
for (auto& componet : children_) {
if (componet->GetSelfTypeName() == name) {
return true;
}
}
return false;
}
void SceneComponent::SetVisible(bool v) { void SceneComponent::SetVisible(bool v) {
visible_ = v; visible_ = v;
if (nullptr == mt_) { if (nullptr == mt_) {

View File

@ -10,6 +10,7 @@
#include "osg/Vec3" #include "osg/Vec3"
#include "osg/MatrixTransform" #include "osg/MatrixTransform"
#include "Entity.h"
class SceneComponent : public Component { class SceneComponent : public Component {
Q_OBJECT Q_OBJECT
@ -30,6 +31,7 @@ public:
bool AttachTo(SceneComponent* parent) override; bool AttachTo(SceneComponent* parent) override;
virtual void AddToRender(); virtual void AddToRender();
virtual bool HasComponent(const std::string& name) const override;
SceneComponent* GetRootComponent() const; SceneComponent* GetRootComponent() const;

View File

@ -668,22 +668,22 @@
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../ui/ModelBrowser/ModelTreeWidget.cpp" line="270"/> <location filename="../ui/ModelBrowser/ModelTreeWidget.cpp" line="273"/>
<source>Add Label Component</source> <source>Add Label Component</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../ui/ModelBrowser/ModelTreeWidget.cpp" line="277"/> <location filename="../ui/ModelBrowser/ModelTreeWidget.cpp" line="283"/>
<source>Add Mesh Component</source> <source>Add Mesh Component</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../ui/ModelBrowser/ModelTreeWidget.cpp" line="280"/> <location filename="../ui/ModelBrowser/ModelTreeWidget.cpp" line="293"/>
<source>Add Path Component</source> <source>Add Path Component</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../ui/ModelBrowser/ModelTreeWidget.cpp" line="287"/> <location filename="../ui/ModelBrowser/ModelTreeWidget.cpp" line="301"/>
<source>Delete</source> <source>Delete</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>

View File

@ -258,33 +258,47 @@ void ModelTreeWidget::PopupEntityMenu(QContextMenuEvent* event, Entity* entity)
); );
menu.addAction(releaseTrack); menu.addAction(releaseTrack);
} }
} else { else {
QAction* addEntiy = new QAction(tr("Track"), this); QAction* addEntiy = new QAction(tr("Track"), this);
menu.addAction(addEntiy);
connect(addEntiy, &QAction::triggered, [this, entity]() {
OnTrackEntity(entity);
}
);
}
}
bool hasLableComponent = entity->HasComponent("LabelComponent");
if (!hasLableComponent) {
QAction* addEntiy = new QAction(tr("Add Label Component"), this);
menu.addAction(addEntiy); menu.addAction(addEntiy);
connect(addEntiy, &QAction::triggered, [this, entity]() { connect(addEntiy, &QAction::triggered, [this, entity]() {
OnTrackEntity(entity); OnAddComponent("LabelComponent", entity);
} }
); );
} }
QAction* addEntiy = new QAction(tr("Add Label Component"), this); bool hasMeshComponent = entity->HasComponent("MeshComponent");
menu.addAction(addEntiy); if (!hasMeshComponent) {
connect(addEntiy, &QAction::triggered, [this, entity]() { QAction* addEntiy = new QAction(tr("Add Mesh Component"), this);
OnAddComponent("LabelComponent", entity); menu.addAction(addEntiy);
} connect(addEntiy, &QAction::triggered, [this, entity]() {
); OnAddComponent("MeshComponent", entity);
}
);
}
addEntiy = new QAction(tr("Add Mesh Component"), this); bool hasPathComponent = entity->HasComponent("PathComponent");
menu.addAction(addEntiy); if (!hasPathComponent) {
QAction* addEntiy = new QAction(tr("Add Path Component"), this);
menu.addAction(addEntiy);
connect(addEntiy, &QAction::triggered, [this, entity]() {
OnAddComponent("PathComponent", entity);
}
);
}
addEntiy = new QAction(tr("Add Path Component"), this); QAction* addEntiy = new QAction(tr("Delete"), this);
menu.addAction(addEntiy);
connect(addEntiy, &QAction::triggered, [this, entity]() {
OnAddComponent("PathComponent", entity);
}
);
addEntiy = new QAction(tr("Delete"), this);
menu.addAction(addEntiy); menu.addAction(addEntiy);
connect(addEntiy, &QAction::triggered, [this, entity]() { connect(addEntiy, &QAction::triggered, [this, entity]() {
OnDeleteEntity(entity); OnDeleteEntity(entity);