Editor entity naming
This commit is contained in:
@@ -50,6 +50,9 @@ public:
|
||||
// Called when the user means to change the parent of an entity.
|
||||
typedef std::function<void(EntityWrapper, EntityWrapper)> OnEntityChangeParent_t;
|
||||
void SetEntityChangeParentCallback(OnEntityChangeParent_t f) { m_OnEntityChangeParent = f; }
|
||||
// Called when the user means to rename an entity.
|
||||
typedef std::function<void(EntityWrapper, const std::string&)> 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<void(EntityWrapper, const std::string&)> 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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user