diff --git a/include/Engine/Editor/EditorGUI.h b/include/Engine/Editor/EditorGUI.h index 7e36f211..f28b95cf 100644 --- a/include/Engine/Editor/EditorGUI.h +++ b/include/Engine/Editor/EditorGUI.h @@ -50,6 +50,9 @@ public: // Called when the user means to change the parent of an entity. typedef std::function OnEntityChangeParent_t; void SetEntityChangeParentCallback(OnEntityChangeParent_t f) { m_OnEntityChangeParent = f; } + // Called when the user means to rename an entity. + typedef std::function OnEntityChangeName_t; + void SetEntityChangeNameCallback(OnEntityChangeName_t f) { m_OnEntityChangeName = f; } // Called when the user means to attach a new component to an entity. typedef std::function OnComponentAttach_t; void SetComponentAttachCallback(OnComponentAttach_t f) { m_OnComponentAttach = f; } @@ -77,6 +80,7 @@ private: OnEntityCreate_t m_OnEntityCreate = nullptr; OnEntityDelete_t m_OnEntityDelete = nullptr; OnEntityChangeParent_t m_OnEntityChangeParent = nullptr; + OnEntityChangeName_t m_OnEntityChangeName = nullptr; OnComponentAttach_t m_OnComponentAttach = nullptr; OnComponentDelete_t m_OnComponentDelete = nullptr; diff --git a/include/Engine/Editor/EditorSystem.h b/include/Engine/Editor/EditorSystem.h index 8ae3ac2e..c0db917d 100644 --- a/include/Engine/Editor/EditorSystem.h +++ b/include/Engine/Editor/EditorSystem.h @@ -41,6 +41,7 @@ private: EntityWrapper OnEntityCreate(EntityWrapper parent); void OnEntityDelete(EntityWrapper entity); void OnEntityChangeParent(EntityWrapper entity, EntityWrapper parent); + void OnEntityChangeName(EntityWrapper entity, const std::string& name); void OnComponentAttach(EntityWrapper entity, const std::string& componentType); void OnComponentDelete(EntityWrapper entity, const std::string& componentType); }; \ No newline at end of file diff --git a/src/Engine/Editor/EditorGUI.cpp b/src/Engine/Editor/EditorGUI.cpp index d0a6fd05..68a136c3 100644 --- a/src/Engine/Editor/EditorGUI.cpp +++ b/src/Engine/Editor/EditorGUI.cpp @@ -101,6 +101,31 @@ void EditorGUI::drawEntities(World* world) ImGui::SameLine(0.f, 5.f); ImGui::Button("Reference", ImVec2(buttonWidth, 0)); + // Naming + char buffer[256]; + buffer[0] = '\0'; + buffer[255] = '\0'; + std::size_t nameLength = 0; + ImGuiInputTextFlags flags = ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_AutoSelectAll; + if (m_CurrentSelection.Valid()) { + std::string name = world->GetName(m_CurrentSelection.ID); + nameLength = name.length(); + if (!name.empty()) { + memcpy(buffer, name.c_str(), std::min(sizeof(buffer) - 1, name.length() + 1)); + } + } else { + flags |= ImGuiInputTextFlags_ReadOnly; + } + ImGui::PushItemWidth(ImGui::GetContentRegionAvailWidth() - 7.f); + if (ImGui::InputText("", &buffer[0], sizeof(buffer), flags)) { + if (m_CurrentSelection.Valid()) { + if (m_OnEntityChangeName != nullptr) { + m_OnEntityChangeName(m_CurrentSelection, std::string(buffer)); + } + } + } + ImGui::PopItemWidth(); + ImGui::ItemSize(ImVec2(0, 3)); drawEntitiesRecursive(world, EntityID_Invalid); @@ -487,7 +512,7 @@ const std::string EditorGUI::formatEntityName(EntityWrapper entity) std::stringstream name; - const std::string& entityName = entity.World->GetName(entity); + std::string entityName = entity.World->GetName(entity.ID); if (!entityName.empty()) { name << entityName; } else { diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index 05e9974b..36f2baa4 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -26,6 +26,7 @@ EditorSystem::EditorSystem(World* world, EventBroker* eventBroker, IRenderer* re m_EditorGUI->SetEntityCreateCallback(std::bind(&EditorSystem::OnEntityCreate, this, std::placeholders::_1)); m_EditorGUI->SetEntityDeleteCallback(std::bind(&EditorSystem::OnEntityDelete, this, std::placeholders::_1)); m_EditorGUI->SetEntityChangeParentCallback(std::bind(&EditorSystem::OnEntityChangeParent, this, std::placeholders::_1, std::placeholders::_2)); + m_EditorGUI->SetEntityChangeNameCallback(std::bind(&EditorSystem::OnEntityChangeName, this, std::placeholders::_1, std::placeholders::_2)); m_EditorGUI->SetComponentAttachCallback(std::bind(&EditorSystem::OnComponentAttach, this, std::placeholders::_1, std::placeholders::_2)); m_EditorGUI->SetComponentDeleteCallback(std::bind(&EditorSystem::OnComponentDelete, this, std::placeholders::_1, std::placeholders::_2)); @@ -85,6 +86,11 @@ void EditorSystem::OnEntityChangeParent(EntityWrapper entity, EntityWrapper pare entity.World->SetParent(entity.ID, parent.ID); } +void EditorSystem::OnEntityChangeName(EntityWrapper entity, const std::string& name) +{ + entity.World->SetName(entity.ID, name); +} + void EditorSystem::OnComponentAttach(EntityWrapper entity, const std::string& componentType) { entity.World->AttachComponent(entity.ID, componentType);