diff --git a/src/entities/AngleReflexComponent.h b/src/entities/AngleReflexComponent.h index b0ae6f6f..3e1eb756 100644 --- a/src/entities/AngleReflexComponent.h +++ b/src/entities/AngleReflexComponent.h @@ -13,7 +13,7 @@ public: ~AngleReflexComponent(); static std::string GetTypeName(); - std::string GetSelfTypeName() override { + std::string GetSelfTypeName() const override { return AngleReflexComponent::GetTypeName(); } diff --git a/src/entities/ChaffBombsComponent.h b/src/entities/ChaffBombsComponent.h index 944504ee..3150c886 100644 --- a/src/entities/ChaffBombsComponent.h +++ b/src/entities/ChaffBombsComponent.h @@ -13,7 +13,7 @@ public: ~ChaffBombsComponent(); static std::string GetTypeName(); - std::string GetSelfTypeName() override { + std::string GetSelfTypeName() const override { return ChaffBombsComponent::GetTypeName(); } diff --git a/src/entities/Component.cpp b/src/entities/Component.cpp index 95c4d313..84aa1c46 100644 --- a/src/entities/Component.cpp +++ b/src/entities/Component.cpp @@ -86,6 +86,10 @@ std::vector Component::GetChildren() const { return std::vector(); } +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(); } diff --git a/src/entities/Component.h b/src/entities/Component.h index 86b79329..c38e77fa 100644 --- a/src/entities/Component.h +++ b/src/entities/Component.h @@ -26,15 +26,17 @@ 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_; } const QString& GetUUID() const { return uuid_; } + protected: void SetUUId(const QString& uuid) { diff --git a/src/entities/ConeWaveComponent.h b/src/entities/ConeWaveComponent.h index ac7f5c35..15f3c938 100644 --- a/src/entities/ConeWaveComponent.h +++ b/src/entities/ConeWaveComponent.h @@ -15,7 +15,7 @@ public: ~ConeWaveComponent(); static std::string GetTypeName(); - std::string GetSelfTypeName() override { + std::string GetSelfTypeName() const override { return ConeWaveComponent::GetTypeName(); } diff --git a/src/entities/DashedLineComponent.h b/src/entities/DashedLineComponent.h index 30b35cec..70bb0e34 100644 --- a/src/entities/DashedLineComponent.h +++ b/src/entities/DashedLineComponent.h @@ -13,7 +13,7 @@ public: ~DashedLineComponent(); static std::string GetTypeName(); - std::string GetSelfTypeName() override { + std::string GetSelfTypeName() const override { return DashedLineComponent::GetTypeName(); } diff --git a/src/entities/Entity.cpp b/src/entities/Entity.cpp index 8cd217a1..4f12b6cb 100644 --- a/src/entities/Entity.cpp +++ b/src/entities/Entity.cpp @@ -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_) { diff --git a/src/entities/Entity.h b/src/entities/Entity.h index 37bee874..13e7752d 100644 --- a/src/entities/Entity.h +++ b/src/entities/Entity.h @@ -56,6 +56,8 @@ public: return rootComponet_->GetComponent(); } + bool HasComponent(const std::string& name) const; + void SetRootComponent(SceneComponent* root) { rootComponet_ = root; } diff --git a/src/entities/LabelComponent.cpp b/src/entities/LabelComponent.cpp index 08fa05d4..43ca3967 100644 --- a/src/entities/LabelComponent.cpp +++ b/src/entities/LabelComponent.cpp @@ -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(); } diff --git a/src/entities/LabelComponent.h b/src/entities/LabelComponent.h index cc56b3bf..7b82b93c 100644 --- a/src/entities/LabelComponent.h +++ b/src/entities/LabelComponent.h @@ -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; diff --git a/src/entities/MeshComponent.cpp b/src/entities/MeshComponent.cpp index a6b12f45..f35be102 100644 --- a/src/entities/MeshComponent.cpp +++ b/src/entities/MeshComponent.cpp @@ -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(); } diff --git a/src/entities/MeshComponent.h b/src/entities/MeshComponent.h index cf3b7083..cfc1aefe 100644 --- a/src/entities/MeshComponent.h +++ b/src/entities/MeshComponent.h @@ -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 { diff --git a/src/entities/PathComponent.h b/src/entities/PathComponent.h index 923bd484..fabefb53 100644 --- a/src/entities/PathComponent.h +++ b/src/entities/PathComponent.h @@ -12,7 +12,7 @@ public: ~PathComponent(); static std::string GetTypeName(); - std::string GetSelfTypeName() override { + std::string GetSelfTypeName() const override { return PathComponent::GetTypeName(); } diff --git a/src/entities/SceneComponent.cpp b/src/entities/SceneComponent.cpp index cf2a05e9..7dcbd70f 100644 --- a/src/entities/SceneComponent.cpp +++ b/src/entities/SceneComponent.cpp @@ -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_) { diff --git a/src/entities/SceneComponent.h b/src/entities/SceneComponent.h index 95ef0cb9..7a26a899 100644 --- a/src/entities/SceneComponent.h +++ b/src/entities/SceneComponent.h @@ -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; diff --git a/src/translations/Dyt_zh_CN.ts b/src/translations/Dyt_zh_CN.ts index a4141f94..75e3555c 100644 --- a/src/translations/Dyt_zh_CN.ts +++ b/src/translations/Dyt_zh_CN.ts @@ -668,22 +668,22 @@ - + Add Label Component - + Add Mesh Component - + Add Path Component - + Delete diff --git a/src/ui/ModelBrowser/ModelTreeWidget.cpp b/src/ui/ModelBrowser/ModelTreeWidget.cpp index 6a96c766..46ad3ba8 100644 --- a/src/ui/ModelBrowser/ModelTreeWidget.cpp +++ b/src/ui/ModelBrowser/ModelTreeWidget.cpp @@ -258,33 +258,47 @@ void ModelTreeWidget::PopupEntityMenu(QContextMenuEvent* event, Entity* entity) ); menu.addAction(releaseTrack); } - } else { - QAction* addEntiy = new QAction(tr("Track"), this); + else { + 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); connect(addEntiy, &QAction::triggered, [this, entity]() { - OnTrackEntity(entity); + OnAddComponent("LabelComponent", entity); } ); } - - 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); - menu.addAction(addEntiy); - - addEntiy = new QAction(tr("Add Path Component"), this); - menu.addAction(addEntiy); - connect(addEntiy, &QAction::triggered, [this, entity]() { - OnAddComponent("PathComponent", entity); - } - ); + 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("Delete"), 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); + } + ); + } + + QAction* addEntiy = new QAction(tr("Delete"), this); menu.addAction(addEntiy); connect(addEntiy, &QAction::triggered, [this, entity]() { OnDeleteEntity(entity);