简单添加entt实体组件系统库

This commit is contained in:
2025-05-29 21:21:15 +08:00
parent 1da0252d6a
commit 31c77dcb5e
43 changed files with 95682 additions and 250 deletions

View File

@ -0,0 +1,168 @@
//
// Created by sfd on 25-5-29.
//
#include "SceneHierachyPanel.h"
#include <imgui.h>
#include <glm/gtc/type_ptr.hpp>
#include <Hazel/Scene/Components.h>
namespace Hazel
{
SceneHierachyPanel::SceneHierachyPanel(const Ref<Scene>& context)
{
SetContext(context);
}
void SceneHierachyPanel::SetContext(const Ref<Scene>& context)
{
m_Context = context;
}
void SceneHierachyPanel::OnImGuiRender()
{
ImGui::Begin("Scene Hierachy");
for (const auto entityID : m_Context->m_Registry.view<entt::entity>())
{
DrawEntityNode({entityID, m_Context.get()});
}
if (ImGui::IsMouseDown(0) && ImGui::IsWindowHovered())
{
m_SelectionContext = { };
}
ImGui::End();
ImGui::Begin("Properties");
if (m_SelectionContext)
{
DrawComponents(m_SelectionContext);
}
ImGui::End();
}
void SceneHierachyPanel::DrawEntityNode(Entity entity)
{
auto& tag = entity.GetComponent<TagComponent>().Tag;
ImGuiTreeNodeFlags flags = ((m_SelectionContext == entity) ? ImGuiTreeNodeFlags_Selected : 0) | ImGuiTreeNodeFlags_OpenOnArrow;
const bool isopened = ImGui::TreeNodeEx((void*)(uint64_t)(uint32_t)entity, flags, tag.c_str());
if (ImGui::IsItemClicked())
{
m_SelectionContext = entity;
}
if (isopened)
{
ImGui::TreePop();
}
}
void SceneHierachyPanel::DrawComponents(Entity entity)
{
if (entity.HasComponent<TagComponent>())
{
auto& tag = entity.GetComponent<TagComponent>().Tag;
char buffer[256] = {};
strcpy_s(buffer,sizeof(buffer), tag.c_str());
if (ImGui::InputText("Tag", buffer, sizeof(buffer)))
{
tag = std::string(buffer);
}
}
if (entity.HasComponent<TransformComponent>())
{
if (ImGui::TreeNodeEx((void*)(uint64_t)typeid(TransformComponent).hash_code(), ImGuiTreeNodeFlags_DefaultOpen, "Transform"))
{
auto& transform = entity.GetComponent<TransformComponent>().Transform;
ImGui::DragFloat3("Position", glm::value_ptr(transform[3]), 0.01f);
ImGui::TreePop();
}
}
if (entity.HasComponent<CameraComponent>())
{
if (ImGui::TreeNodeEx((void*)(uint64_t)typeid(CameraComponent).hash_code(), ImGuiTreeNodeFlags_DefaultOpen, "Transform"))
{
auto& cameraComponent = entity.GetComponent<CameraComponent>();
auto& camera = cameraComponent.Camera;
ImGui::Checkbox("isPrimary", &cameraComponent.Primary);
static const char* projectionTypeStrings[] = { "Perspective", "Orthographic" };
const char* currentProjectionTypeString = projectionTypeStrings[(int)camera.GetProjectionType()];
if (ImGui::BeginCombo("Projection", currentProjectionTypeString))
{
for (int i = 0; i < 2; i++)
{
const bool isSelected = currentProjectionTypeString == projectionTypeStrings[i];
if (ImGui::Selectable(projectionTypeStrings[i], isSelected))
{
currentProjectionTypeString = projectionTypeStrings[i];
camera.SetProjectionType(static_cast<ScenceCamera::ProjectionType>(i));
}
if (isSelected)
{
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
if (camera.GetProjectionType() == ScenceCamera::ProjectionType::Perspective)
{
float fov = glm::degrees(camera.GetPerspectiveVerticalFOV());
if (ImGui::DragFloat("FOV", &fov, 0.1f))
camera.SetPerspectiveVerticalFOV(glm::radians(fov));
float near = camera.GetPerspectiveNearCLip();
if (ImGui::DragFloat("Near", &near))
camera.SetPerspectiveNearClip(near);
float far = camera.GetPerspectiveFarCLip();
if (ImGui::DragFloat("Far", &far))
camera.SetPerspectiveFarClip(far);
}
if (camera.GetProjectionType() == ScenceCamera::ProjectionType::Orthographic)
{
float size = camera.GetOrthographicSize();
if (ImGui::DragFloat("Size", &size))
camera.SetOrthographicSize(size);
float near = camera.GetOrthographicNearCLip();
if (ImGui::DragFloat("Near", &near))
camera.SetOrthographicNearClip(near);
float far = camera.GetOrthographicFarCLip();
if (ImGui::DragFloat("Far", &far))
camera.SetOrthographicFarClip(far);
ImGui::Checkbox("Fixed Aspect Ratio", &cameraComponent.FixedAspectRatio);
}
ImGui::TreePop();
}
}
if (entity.HasComponent<SpriteRendererComponent>())
{
if (ImGui::TreeNodeEx((void*)(uint64_t)typeid(SpriteRendererComponent).hash_code(), ImGuiTreeNodeFlags_DefaultOpen, "Sprite Renderer"))
{
auto& spriteRendererComponent = entity.GetComponent<SpriteRendererComponent>();
ImGui::ColorEdit4("Color", glm::value_ptr(spriteRendererComponent.Color));
ImGui::TreePop();
}
}
}
}