UI Editor: Enhance GameObject system and panel functionality
This commit is contained in:
@@ -14,45 +14,10 @@ namespace UI {
|
|||||||
using EntityID = uint64_t;
|
using EntityID = uint64_t;
|
||||||
constexpr EntityID INVALID_ENTITY_ID = 0;
|
constexpr EntityID INVALID_ENTITY_ID = 0;
|
||||||
|
|
||||||
class Component {
|
class Component;
|
||||||
public:
|
class TransformComponent;
|
||||||
virtual ~Component() = default;
|
|
||||||
virtual std::string GetName() const = 0;
|
|
||||||
|
|
||||||
virtual void Awake() {}
|
class GameObject {
|
||||||
virtual void Start() {}
|
|
||||||
virtual void Update(float deltaTime) {}
|
|
||||||
virtual void OnDestroy() {}
|
|
||||||
|
|
||||||
class Entity* GetEntity() const { return m_entity; }
|
|
||||||
bool IsEnabled() const { return m_enabled; }
|
|
||||||
void SetEnabled(bool enabled) { m_enabled = enabled; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
class Entity* m_entity = nullptr;
|
|
||||||
bool m_enabled = true;
|
|
||||||
|
|
||||||
friend class Entity;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TransformComponent : public Component {
|
|
||||||
public:
|
|
||||||
float position[3] = {0.0f, 0.0f, 0.0f};
|
|
||||||
float rotation[3] = {0.0f, 0.0f, 0.0f};
|
|
||||||
float scale[3] = {1.0f, 1.0f, 1.0f};
|
|
||||||
|
|
||||||
std::string GetName() const override { return "Transform"; }
|
|
||||||
};
|
|
||||||
|
|
||||||
class MeshRendererComponent : public Component {
|
|
||||||
public:
|
|
||||||
std::string materialName = "Default-Material";
|
|
||||||
std::string meshName = "";
|
|
||||||
|
|
||||||
std::string GetName() const override { return "Mesh Renderer"; }
|
|
||||||
};
|
|
||||||
|
|
||||||
class Entity {
|
|
||||||
public:
|
public:
|
||||||
EntityID id = INVALID_ENTITY_ID;
|
EntityID id = INVALID_ENTITY_ID;
|
||||||
std::string name;
|
std::string name;
|
||||||
@@ -64,7 +29,7 @@ public:
|
|||||||
template<typename T, typename... Args>
|
template<typename T, typename... Args>
|
||||||
T* AddComponent(Args&&... args) {
|
T* AddComponent(Args&&... args) {
|
||||||
auto comp = std::make_unique<T>(std::forward<Args>(args)...);
|
auto comp = std::make_unique<T>(std::forward<Args>(args)...);
|
||||||
comp->m_entity = this;
|
comp->m_gameObject = this;
|
||||||
T* ptr = comp.get();
|
T* ptr = comp.get();
|
||||||
components.push_back(std::move(comp));
|
components.push_back(std::move(comp));
|
||||||
return ptr;
|
return ptr;
|
||||||
@@ -90,6 +55,48 @@ public:
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TransformComponent* GetTransform() {
|
||||||
|
return GetComponent<TransformComponent>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Component {
|
||||||
|
public:
|
||||||
|
virtual ~Component() = default;
|
||||||
|
virtual std::string GetName() const = 0;
|
||||||
|
|
||||||
|
virtual void Awake() {}
|
||||||
|
virtual void Start() {}
|
||||||
|
virtual void Update(float deltaTime) {}
|
||||||
|
virtual void OnDestroy() {}
|
||||||
|
|
||||||
|
GameObject* GetGameObject() const { return m_gameObject; }
|
||||||
|
bool IsEnabled() const { return m_enabled; }
|
||||||
|
void SetEnabled(bool enabled) { m_enabled = enabled; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
GameObject* m_gameObject = nullptr;
|
||||||
|
bool m_enabled = true;
|
||||||
|
|
||||||
|
friend class GameObject;
|
||||||
|
};
|
||||||
|
|
||||||
|
class TransformComponent : public Component {
|
||||||
|
public:
|
||||||
|
float position[3] = {0.0f, 0.0f, 0.0f};
|
||||||
|
float rotation[3] = {0.0f, 0.0f, 0.0f};
|
||||||
|
float scale[3] = {1.0f, 1.0f, 1.0f};
|
||||||
|
|
||||||
|
std::string GetName() const override { return "Transform"; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class MeshRendererComponent : public Component {
|
||||||
|
public:
|
||||||
|
std::string materialName = "Default-Material";
|
||||||
|
std::string meshName = "";
|
||||||
|
|
||||||
|
std::string GetName() const override { return "Mesh Renderer"; }
|
||||||
};
|
};
|
||||||
|
|
||||||
using ComponentInspectorFn = std::function<void(Component*)>;
|
using ComponentInspectorFn = std::function<void(Component*)>;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace UI {
|
|||||||
|
|
||||||
EntityID SceneManager::CreateEntity(const std::string& name, EntityID parent) {
|
EntityID SceneManager::CreateEntity(const std::string& name, EntityID parent) {
|
||||||
EntityID id = m_nextEntityId++;
|
EntityID id = m_nextEntityId++;
|
||||||
Entity entity;
|
GameObject entity;
|
||||||
entity.id = id;
|
entity.id = id;
|
||||||
entity.name = name;
|
entity.name = name;
|
||||||
entity.parent = parent;
|
entity.parent = parent;
|
||||||
@@ -26,7 +26,7 @@ void SceneManager::DeleteEntity(EntityID id) {
|
|||||||
auto it = m_entities.find(id);
|
auto it = m_entities.find(id);
|
||||||
if (it == m_entities.end()) return;
|
if (it == m_entities.end()) return;
|
||||||
|
|
||||||
Entity& entity = it->second;
|
GameObject& entity = it->second;
|
||||||
|
|
||||||
std::vector<EntityID> childrenToDelete = entity.children;
|
std::vector<EntityID> childrenToDelete = entity.children;
|
||||||
for (EntityID childId : childrenToDelete) {
|
for (EntityID childId : childrenToDelete) {
|
||||||
@@ -51,7 +51,7 @@ void SceneManager::DeleteEntity(EntityID id) {
|
|||||||
OnEntityDeleted.Invoke(id);
|
OnEntityDeleted.Invoke(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
ClipboardData SceneManager::CopyEntityRecursive(const Entity* entity) {
|
ClipboardData SceneManager::CopyEntityRecursive(const GameObject* entity) {
|
||||||
ClipboardData data;
|
ClipboardData data;
|
||||||
data.name = entity->name;
|
data.name = entity->name;
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ ClipboardData SceneManager::CopyEntityRecursive(const Entity* entity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (EntityID childId : entity->children) {
|
for (EntityID childId : entity->children) {
|
||||||
const Entity* child = GetEntity(childId);
|
const GameObject* child = GetEntity(childId);
|
||||||
if (child) {
|
if (child) {
|
||||||
data.children.push_back(CopyEntityRecursive(child));
|
data.children.push_back(CopyEntityRecursive(child));
|
||||||
}
|
}
|
||||||
@@ -82,7 +82,7 @@ ClipboardData SceneManager::CopyEntityRecursive(const Entity* entity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SceneManager::CopyEntity(EntityID id) {
|
void SceneManager::CopyEntity(EntityID id) {
|
||||||
const Entity* entity = GetEntity(id);
|
const GameObject* entity = GetEntity(id);
|
||||||
if (!entity) return;
|
if (!entity) return;
|
||||||
|
|
||||||
m_clipboard = CopyEntityRecursive(entity);
|
m_clipboard = CopyEntityRecursive(entity);
|
||||||
@@ -90,7 +90,7 @@ void SceneManager::CopyEntity(EntityID id) {
|
|||||||
|
|
||||||
EntityID SceneManager::PasteEntityRecursive(const ClipboardData& data, EntityID parent) {
|
EntityID SceneManager::PasteEntityRecursive(const ClipboardData& data, EntityID parent) {
|
||||||
EntityID newId = CreateEntity(data.name, parent);
|
EntityID newId = CreateEntity(data.name, parent);
|
||||||
Entity* newEntity = GetEntity(newId);
|
GameObject* newEntity = GetEntity(newId);
|
||||||
|
|
||||||
if (newEntity) {
|
if (newEntity) {
|
||||||
newEntity->components.clear();
|
newEntity->components.clear();
|
||||||
@@ -125,17 +125,17 @@ EntityID SceneManager::PasteEntity(EntityID parent) {
|
|||||||
|
|
||||||
EntityID SceneManager::DuplicateEntity(EntityID id) {
|
EntityID SceneManager::DuplicateEntity(EntityID id) {
|
||||||
CopyEntity(id);
|
CopyEntity(id);
|
||||||
const Entity* entity = GetEntity(id);
|
const GameObject* entity = GetEntity(id);
|
||||||
if (!entity) return INVALID_ENTITY_ID;
|
if (!entity) return INVALID_ENTITY_ID;
|
||||||
return PasteEntity(entity->parent);
|
return PasteEntity(entity->parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SceneManager::MoveEntity(EntityID id, EntityID newParent) {
|
void SceneManager::MoveEntity(EntityID id, EntityID newParent) {
|
||||||
Entity* entity = GetEntity(id);
|
GameObject* entity = GetEntity(id);
|
||||||
if (!entity || id == newParent) return;
|
if (!entity || id == newParent) return;
|
||||||
|
|
||||||
if (entity->parent != INVALID_ENTITY_ID) {
|
if (entity->parent != INVALID_ENTITY_ID) {
|
||||||
Entity* oldParent = GetEntity(entity->parent);
|
GameObject* oldParent = GetEntity(entity->parent);
|
||||||
if (oldParent) {
|
if (oldParent) {
|
||||||
auto& siblings = oldParent->children;
|
auto& siblings = oldParent->children;
|
||||||
siblings.erase(std::remove(siblings.begin(), siblings.end(), id), siblings.end());
|
siblings.erase(std::remove(siblings.begin(), siblings.end(), id), siblings.end());
|
||||||
@@ -147,7 +147,7 @@ void SceneManager::MoveEntity(EntityID id, EntityID newParent) {
|
|||||||
entity->parent = newParent;
|
entity->parent = newParent;
|
||||||
|
|
||||||
if (newParent != INVALID_ENTITY_ID) {
|
if (newParent != INVALID_ENTITY_ID) {
|
||||||
Entity* newParentEntity = GetEntity(newParent);
|
GameObject* newParentEntity = GetEntity(newParent);
|
||||||
if (newParentEntity) {
|
if (newParentEntity) {
|
||||||
newParentEntity->children.push_back(id);
|
newParentEntity->children.push_back(id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public:
|
|||||||
|
|
||||||
EntityID CreateEntity(const std::string& name, EntityID parent = INVALID_ENTITY_ID);
|
EntityID CreateEntity(const std::string& name, EntityID parent = INVALID_ENTITY_ID);
|
||||||
|
|
||||||
Entity* GetEntity(EntityID id) {
|
GameObject* GetEntity(EntityID id) {
|
||||||
auto it = m_entities.find(id);
|
auto it = m_entities.find(id);
|
||||||
if (it != m_entities.end()) {
|
if (it != m_entities.end()) {
|
||||||
return &it->second;
|
return &it->second;
|
||||||
@@ -33,7 +33,7 @@ public:
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Entity* GetEntity(EntityID id) const {
|
const GameObject* GetEntity(EntityID id) const {
|
||||||
auto it = m_entities.find(id);
|
auto it = m_entities.find(id);
|
||||||
if (it != m_entities.end()) {
|
if (it != m_entities.end()) {
|
||||||
return &it->second;
|
return &it->second;
|
||||||
@@ -75,11 +75,11 @@ public:
|
|||||||
private:
|
private:
|
||||||
SceneManager() = default;
|
SceneManager() = default;
|
||||||
|
|
||||||
ClipboardData CopyEntityRecursive(const Entity* entity);
|
ClipboardData CopyEntityRecursive(const GameObject* entity);
|
||||||
EntityID PasteEntityRecursive(const ClipboardData& data, EntityID parent);
|
EntityID PasteEntityRecursive(const ClipboardData& data, EntityID parent);
|
||||||
|
|
||||||
EntityID m_nextEntityId = 1;
|
EntityID m_nextEntityId = 1;
|
||||||
std::unordered_map<EntityID, Entity> m_entities;
|
std::unordered_map<EntityID, GameObject> m_entities;
|
||||||
std::vector<EntityID> m_rootEntities;
|
std::vector<EntityID> m_rootEntities;
|
||||||
std::optional<ClipboardData> m_clipboard;
|
std::optional<ClipboardData> m_clipboard;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ void HierarchyPanel::Render() {
|
|||||||
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("ENTITY_ID")) {
|
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("ENTITY_ID")) {
|
||||||
EntityID sourceId = *(const EntityID*)payload->Data;
|
EntityID sourceId = *(const EntityID*)payload->Data;
|
||||||
if (sourceId != INVALID_ENTITY_ID) {
|
if (sourceId != INVALID_ENTITY_ID) {
|
||||||
const Entity* sourceEntity = SceneManager::Get().GetEntity(sourceId);
|
const GameObject* sourceEntity = SceneManager::Get().GetEntity(sourceId);
|
||||||
if (sourceEntity && sourceEntity->parent != INVALID_ENTITY_ID) {
|
if (sourceEntity && sourceEntity->parent != INVALID_ENTITY_ID) {
|
||||||
SceneManager::Get().MoveEntity(sourceId, INVALID_ENTITY_ID);
|
SceneManager::Get().MoveEntity(sourceId, INVALID_ENTITY_ID);
|
||||||
}
|
}
|
||||||
@@ -71,7 +71,7 @@ void HierarchyPanel::RenderSearchBar() {
|
|||||||
|
|
||||||
void HierarchyPanel::RenderEntity(EntityID id, const std::string& filter) {
|
void HierarchyPanel::RenderEntity(EntityID id, const std::string& filter) {
|
||||||
auto& sceneManager = SceneManager::Get();
|
auto& sceneManager = SceneManager::Get();
|
||||||
Entity* entity = sceneManager.GetEntity(id);
|
GameObject* entity = sceneManager.GetEntity(id);
|
||||||
if (!entity) return;
|
if (!entity) return;
|
||||||
|
|
||||||
if (!filter.empty() && !PassesFilter(id, filter)) {
|
if (!filter.empty() && !PassesFilter(id, filter)) {
|
||||||
@@ -156,7 +156,7 @@ void HierarchyPanel::RenderContextMenu(EntityID id) {
|
|||||||
ImGui::Separator();
|
ImGui::Separator();
|
||||||
|
|
||||||
if (ImGui::MenuItem("Rename", "F2")) {
|
if (ImGui::MenuItem("Rename", "F2")) {
|
||||||
const Entity* entity = sceneManager.GetEntity(id);
|
const GameObject* entity = sceneManager.GetEntity(id);
|
||||||
if (entity) {
|
if (entity) {
|
||||||
m_renaming = true;
|
m_renaming = true;
|
||||||
m_renamingEntity = id;
|
m_renamingEntity = id;
|
||||||
@@ -239,7 +239,7 @@ void HierarchyPanel::HandleDragDrop(EntityID id) {
|
|||||||
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) {
|
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) {
|
||||||
m_dragSource = id;
|
m_dragSource = id;
|
||||||
ImGui::SetDragDropPayload("ENTITY_ID", &id, sizeof(EntityID));
|
ImGui::SetDragDropPayload("ENTITY_ID", &id, sizeof(EntityID));
|
||||||
const Entity* entity = sceneManager.GetEntity(id);
|
const GameObject* entity = sceneManager.GetEntity(id);
|
||||||
if (entity) {
|
if (entity) {
|
||||||
ImGui::Text("%s", entity->name.c_str());
|
ImGui::Text("%s", entity->name.c_str());
|
||||||
}
|
}
|
||||||
@@ -250,8 +250,8 @@ void HierarchyPanel::HandleDragDrop(EntityID id) {
|
|||||||
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("ENTITY_ID")) {
|
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("ENTITY_ID")) {
|
||||||
EntityID sourceId = *(const EntityID*)payload->Data;
|
EntityID sourceId = *(const EntityID*)payload->Data;
|
||||||
if (sourceId != id && sourceId != INVALID_ENTITY_ID) {
|
if (sourceId != id && sourceId != INVALID_ENTITY_ID) {
|
||||||
const Entity* targetEntity = sceneManager.GetEntity(id);
|
const GameObject* targetEntity = sceneManager.GetEntity(id);
|
||||||
const Entity* sourceEntity = sceneManager.GetEntity(sourceId);
|
const GameObject* sourceEntity = sceneManager.GetEntity(sourceId);
|
||||||
|
|
||||||
bool isValidMove = true;
|
bool isValidMove = true;
|
||||||
EntityID checkParent = targetEntity ? targetEntity->parent : INVALID_ENTITY_ID;
|
EntityID checkParent = targetEntity ? targetEntity->parent : INVALID_ENTITY_ID;
|
||||||
@@ -260,7 +260,7 @@ void HierarchyPanel::HandleDragDrop(EntityID id) {
|
|||||||
isValidMove = false;
|
isValidMove = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const Entity* parentEntity = sceneManager.GetEntity(checkParent);
|
const GameObject* parentEntity = sceneManager.GetEntity(checkParent);
|
||||||
checkParent = parentEntity ? parentEntity->parent : INVALID_ENTITY_ID;
|
checkParent = parentEntity ? parentEntity->parent : INVALID_ENTITY_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,7 +288,7 @@ void HierarchyPanel::HandleKeyboardShortcuts() {
|
|||||||
|
|
||||||
if (ImGui::IsKeyPressed(ImGuiKey_F2)) {
|
if (ImGui::IsKeyPressed(ImGuiKey_F2)) {
|
||||||
if (selectedId != INVALID_ENTITY_ID) {
|
if (selectedId != INVALID_ENTITY_ID) {
|
||||||
const Entity* entity = sceneManager.GetEntity(selectedId);
|
const GameObject* entity = sceneManager.GetEntity(selectedId);
|
||||||
if (entity) {
|
if (entity) {
|
||||||
m_renaming = true;
|
m_renaming = true;
|
||||||
m_renamingEntity = selectedId;
|
m_renamingEntity = selectedId;
|
||||||
@@ -326,7 +326,7 @@ void HierarchyPanel::HandleKeyboardShortcuts() {
|
|||||||
|
|
||||||
bool HierarchyPanel::PassesFilter(EntityID id, const std::string& filter) {
|
bool HierarchyPanel::PassesFilter(EntityID id, const std::string& filter) {
|
||||||
auto& sceneManager = SceneManager::Get();
|
auto& sceneManager = SceneManager::Get();
|
||||||
const Entity* entity = sceneManager.GetEntity(id);
|
const GameObject* entity = sceneManager.GetEntity(id);
|
||||||
if (!entity) return false;
|
if (!entity) return false;
|
||||||
|
|
||||||
if (entity->name.find(filter) != std::string::npos) {
|
if (entity->name.find(filter) != std::string::npos) {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ void InspectorPanel::Render() {
|
|||||||
ImGui::Begin(m_name.c_str(), nullptr, ImGuiWindowFlags_None);
|
ImGui::Begin(m_name.c_str(), nullptr, ImGuiWindowFlags_None);
|
||||||
|
|
||||||
EntityID selectedId = SelectionManager::Get().GetSelectedEntity();
|
EntityID selectedId = SelectionManager::Get().GetSelectedEntity();
|
||||||
Entity* entity = SceneManager::Get().GetEntity(selectedId);
|
GameObject* entity = SceneManager::Get().GetEntity(selectedId);
|
||||||
|
|
||||||
if (entity) {
|
if (entity) {
|
||||||
RenderEntity(entity);
|
RenderEntity(entity);
|
||||||
@@ -31,7 +31,7 @@ void InspectorPanel::Render() {
|
|||||||
ImGui::End();
|
ImGui::End();
|
||||||
}
|
}
|
||||||
|
|
||||||
void InspectorPanel::RenderEntity(Entity* entity) {
|
void InspectorPanel::RenderEntity(GameObject* entity) {
|
||||||
ImGui::Text("%s", entity->name.c_str());
|
ImGui::Text("%s", entity->name.c_str());
|
||||||
ImGui::Separator();
|
ImGui::Separator();
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public:
|
|||||||
void Render() override;
|
void Render() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void RenderEntity(Entity* entity);
|
void RenderEntity(GameObject* entity);
|
||||||
void RenderComponent(Component* component);
|
void RenderComponent(Component* component);
|
||||||
|
|
||||||
uint64_t m_selectionHandlerId = 0;
|
uint64_t m_selectionHandlerId = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user