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

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();
static std::string GetTypeName();
std::string GetSelfTypeName() override {
std::string GetSelfTypeName() const override {
return AngleReflexComponent::GetTypeName();
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -183,6 +183,13 @@ void Entity::SetParent(Entity* parent) {
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) {
LOG_INFO("set visible: {}", v);
if (nullptr == rootComponet_) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,7 +12,7 @@ public:
~PathComponent();
static std::string GetTypeName();
std::string GetSelfTypeName() override {
std::string GetSelfTypeName() const override {
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) {
visible_ = v;
if (nullptr == mt_) {

View File

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

View File

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

View File

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