UI Editor: Enhance GameObject system and panel functionality

This commit is contained in:
2026-03-20 19:43:17 +08:00
parent f8573d2715
commit 80e47a0ab9
6 changed files with 119 additions and 112 deletions

View File

@@ -14,45 +14,10 @@ namespace UI {
using EntityID = uint64_t;
constexpr EntityID INVALID_ENTITY_ID = 0;
class Component {
public:
virtual ~Component() = default;
virtual std::string GetName() const = 0;
class Component;
class TransformComponent;
virtual void Awake() {}
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 {
class GameObject {
public:
EntityID id = INVALID_ENTITY_ID;
std::string name;
@@ -64,7 +29,7 @@ public:
template<typename T, typename... Args>
T* AddComponent(Args&&... args) {
auto comp = std::make_unique<T>(std::forward<Args>(args)...);
comp->m_entity = this;
comp->m_gameObject = this;
T* ptr = comp.get();
components.push_back(std::move(comp));
return ptr;
@@ -90,6 +55,48 @@ public:
}
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*)>;

View File

@@ -6,7 +6,7 @@ namespace UI {
EntityID SceneManager::CreateEntity(const std::string& name, EntityID parent) {
EntityID id = m_nextEntityId++;
Entity entity;
GameObject entity;
entity.id = id;
entity.name = name;
entity.parent = parent;
@@ -26,7 +26,7 @@ void SceneManager::DeleteEntity(EntityID id) {
auto it = m_entities.find(id);
if (it == m_entities.end()) return;
Entity& entity = it->second;
GameObject& entity = it->second;
std::vector<EntityID> childrenToDelete = entity.children;
for (EntityID childId : childrenToDelete) {
@@ -51,7 +51,7 @@ void SceneManager::DeleteEntity(EntityID id) {
OnEntityDeleted.Invoke(id);
}
ClipboardData SceneManager::CopyEntityRecursive(const Entity* entity) {
ClipboardData SceneManager::CopyEntityRecursive(const GameObject* entity) {
ClipboardData data;
data.name = entity->name;
@@ -72,7 +72,7 @@ ClipboardData SceneManager::CopyEntityRecursive(const Entity* entity) {
}
for (EntityID childId : entity->children) {
const Entity* child = GetEntity(childId);
const GameObject* child = GetEntity(childId);
if (child) {
data.children.push_back(CopyEntityRecursive(child));
}
@@ -82,7 +82,7 @@ ClipboardData SceneManager::CopyEntityRecursive(const Entity* entity) {
}
void SceneManager::CopyEntity(EntityID id) {
const Entity* entity = GetEntity(id);
const GameObject* entity = GetEntity(id);
if (!entity) return;
m_clipboard = CopyEntityRecursive(entity);
@@ -90,7 +90,7 @@ void SceneManager::CopyEntity(EntityID id) {
EntityID SceneManager::PasteEntityRecursive(const ClipboardData& data, EntityID parent) {
EntityID newId = CreateEntity(data.name, parent);
Entity* newEntity = GetEntity(newId);
GameObject* newEntity = GetEntity(newId);
if (newEntity) {
newEntity->components.clear();
@@ -125,17 +125,17 @@ EntityID SceneManager::PasteEntity(EntityID parent) {
EntityID SceneManager::DuplicateEntity(EntityID id) {
CopyEntity(id);
const Entity* entity = GetEntity(id);
const GameObject* entity = GetEntity(id);
if (!entity) return INVALID_ENTITY_ID;
return PasteEntity(entity->parent);
}
void SceneManager::MoveEntity(EntityID id, EntityID newParent) {
Entity* entity = GetEntity(id);
GameObject* entity = GetEntity(id);
if (!entity || id == newParent) return;
if (entity->parent != INVALID_ENTITY_ID) {
Entity* oldParent = GetEntity(entity->parent);
GameObject* oldParent = GetEntity(entity->parent);
if (oldParent) {
auto& siblings = oldParent->children;
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;
if (newParent != INVALID_ENTITY_ID) {
Entity* newParentEntity = GetEntity(newParent);
GameObject* newParentEntity = GetEntity(newParent);
if (newParentEntity) {
newParentEntity->children.push_back(id);
}

View File

@@ -25,7 +25,7 @@ public:
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);
if (it != m_entities.end()) {
return &it->second;
@@ -33,7 +33,7 @@ public:
return nullptr;
}
const Entity* GetEntity(EntityID id) const {
const GameObject* GetEntity(EntityID id) const {
auto it = m_entities.find(id);
if (it != m_entities.end()) {
return &it->second;
@@ -75,11 +75,11 @@ public:
private:
SceneManager() = default;
ClipboardData CopyEntityRecursive(const Entity* entity);
ClipboardData CopyEntityRecursive(const GameObject* entity);
EntityID PasteEntityRecursive(const ClipboardData& data, EntityID parent);
EntityID m_nextEntityId = 1;
std::unordered_map<EntityID, Entity> m_entities;
std::unordered_map<EntityID, GameObject> m_entities;
std::vector<EntityID> m_rootEntities;
std::optional<ClipboardData> m_clipboard;
};

View File

@@ -50,7 +50,7 @@ void HierarchyPanel::Render() {
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("ENTITY_ID")) {
EntityID sourceId = *(const EntityID*)payload->Data;
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) {
SceneManager::Get().MoveEntity(sourceId, INVALID_ENTITY_ID);
}
@@ -71,7 +71,7 @@ void HierarchyPanel::RenderSearchBar() {
void HierarchyPanel::RenderEntity(EntityID id, const std::string& filter) {
auto& sceneManager = SceneManager::Get();
Entity* entity = sceneManager.GetEntity(id);
GameObject* entity = sceneManager.GetEntity(id);
if (!entity) return;
if (!filter.empty() && !PassesFilter(id, filter)) {
@@ -156,7 +156,7 @@ void HierarchyPanel::RenderContextMenu(EntityID id) {
ImGui::Separator();
if (ImGui::MenuItem("Rename", "F2")) {
const Entity* entity = sceneManager.GetEntity(id);
const GameObject* entity = sceneManager.GetEntity(id);
if (entity) {
m_renaming = true;
m_renamingEntity = id;
@@ -239,7 +239,7 @@ void HierarchyPanel::HandleDragDrop(EntityID id) {
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) {
m_dragSource = id;
ImGui::SetDragDropPayload("ENTITY_ID", &id, sizeof(EntityID));
const Entity* entity = sceneManager.GetEntity(id);
const GameObject* entity = sceneManager.GetEntity(id);
if (entity) {
ImGui::Text("%s", entity->name.c_str());
}
@@ -250,8 +250,8 @@ void HierarchyPanel::HandleDragDrop(EntityID id) {
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("ENTITY_ID")) {
EntityID sourceId = *(const EntityID*)payload->Data;
if (sourceId != id && sourceId != INVALID_ENTITY_ID) {
const Entity* targetEntity = sceneManager.GetEntity(id);
const Entity* sourceEntity = sceneManager.GetEntity(sourceId);
const GameObject* targetEntity = sceneManager.GetEntity(id);
const GameObject* sourceEntity = sceneManager.GetEntity(sourceId);
bool isValidMove = true;
EntityID checkParent = targetEntity ? targetEntity->parent : INVALID_ENTITY_ID;
@@ -260,7 +260,7 @@ void HierarchyPanel::HandleDragDrop(EntityID id) {
isValidMove = false;
break;
}
const Entity* parentEntity = sceneManager.GetEntity(checkParent);
const GameObject* parentEntity = sceneManager.GetEntity(checkParent);
checkParent = parentEntity ? parentEntity->parent : INVALID_ENTITY_ID;
}
@@ -288,7 +288,7 @@ void HierarchyPanel::HandleKeyboardShortcuts() {
if (ImGui::IsKeyPressed(ImGuiKey_F2)) {
if (selectedId != INVALID_ENTITY_ID) {
const Entity* entity = sceneManager.GetEntity(selectedId);
const GameObject* entity = sceneManager.GetEntity(selectedId);
if (entity) {
m_renaming = true;
m_renamingEntity = selectedId;
@@ -326,7 +326,7 @@ void HierarchyPanel::HandleKeyboardShortcuts() {
bool HierarchyPanel::PassesFilter(EntityID id, const std::string& filter) {
auto& sceneManager = SceneManager::Get();
const Entity* entity = sceneManager.GetEntity(id);
const GameObject* entity = sceneManager.GetEntity(id);
if (!entity) return false;
if (entity->name.find(filter) != std::string::npos) {

View File

@@ -19,7 +19,7 @@ void InspectorPanel::Render() {
ImGui::Begin(m_name.c_str(), nullptr, ImGuiWindowFlags_None);
EntityID selectedId = SelectionManager::Get().GetSelectedEntity();
Entity* entity = SceneManager::Get().GetEntity(selectedId);
GameObject* entity = SceneManager::Get().GetEntity(selectedId);
if (entity) {
RenderEntity(entity);
@@ -31,7 +31,7 @@ void InspectorPanel::Render() {
ImGui::End();
}
void InspectorPanel::RenderEntity(Entity* entity) {
void InspectorPanel::RenderEntity(GameObject* entity) {
ImGui::Text("%s", entity->name.c_str());
ImGui::Separator();

View File

@@ -13,7 +13,7 @@ public:
void Render() override;
private:
void RenderEntity(Entity* entity);
void RenderEntity(GameObject* entity);
void RenderComponent(Component* component);
uint64_t m_selectionHandlerId = 0;