Added saving and loading

This commit is contained in:
Erris
2026-02-19 23:45:10 +01:00
parent e0396fedd1
commit a897d5c798
99 changed files with 889 additions and 9784 deletions

View File

@@ -1,3 +1,5 @@
#include "open_engine/scene/components.hpp"
#include "open_engine/scene/entity.hpp"
#include <panels/scene_hierarchy.hpp>
#include <cstring>
@@ -13,55 +15,98 @@ namespace OpenEngine {
void SceneHierarchy::SetContext(const Ref<Scene>& context)
{
scene = context;
selected_context = {};
}
void ComponentPopup(Entity& selected_context)
{
ImGui::SeparatorText("Add");
if (!selected_context.HasComponent<TransformComponent>())
if (ImGui::MenuItem("Transform")) {
selected_context.AddComponents<TransformComponent>();
ImGui::CloseCurrentPopup();
}
if (!selected_context.HasComponent<SpriteRendererComponent>())
if (ImGui::MenuItem("Sprite")) {
selected_context.AddComponents<SpriteRendererComponent>();
ImGui::CloseCurrentPopup();
}
if (!selected_context.HasComponent<CameraComponent>())
if (ImGui::MenuItem("Camera")) {
selected_context.AddComponents<CameraComponent>();
ImGui::CloseCurrentPopup();
}
}
void EntityPopup(Ref<Scene>& scene)
{
ImGui::SeparatorText("Add");
if (ImGui::MenuItem("Empty entity")) {
scene->CreateEntity("Empty entity");
ImGui::CloseCurrentPopup();
}
}
void SceneHierarchy::OnImGuiRender()
{
ImGui::Begin("Scene");
// Scene hierarchy
if (ImGui::Begin("Scene")) {
for (auto entity_entt : scene->GetRegistry().view<TagComponent>()) {
Entity entity{ entity_entt, scene.get() };
// Draw all enitity tree names
for (auto entity_entt : scene->GetRegistry().view<TagComponent>()) {
Entity entity{ entity_entt, scene.get() };
DrawEntityNode(entity);
}
if ((ImGui::IsMouseDown(0) && ImGui::IsWindowHovered())
|| (ImGui::IsKeyDown(ImGuiKey_Escape) && ImGui::IsWindowHovered()))
{
selected_context = {};
renamed_entity = {};
}
if (ImGui::BeginPopupContextWindow(0, ImGuiPopupFlags_MouseButtonRight | ImGuiPopupFlags_NoOpenOverItems))
{
if (ImGui::MenuItem("Create Empty Entity"))
{
scene->CreateEntity("Empty entity");
DrawEntityNode(entity);
}
ImGui::EndPopup();
// On click not on item, deselect
if ((ImGui::IsMouseDown(0) && ImGui::IsWindowHovered())
|| (ImGui::IsKeyDown(ImGuiKey_Escape) && ImGui::IsWindowHovered())) {
selected_context = {};
renamed_entity = {};
}
// On Right click open entity creation window
if (ImGui::BeginPopupContextWindow(0, ImGuiPopupFlags_MouseButtonRight | ImGuiPopupFlags_NoOpenOverItems)) {
EntityPopup(scene);
ImGui::EndPopup();
}
// Centred fullwidth bottom button to add entity
float button_height = ImGui::GetFrameHeight();
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetContentRegionAvail().y - button_height);
if (ImGui::Button("Add Entity", ImVec2(ImGui::GetContentRegionAvail().x, 0.0f)))
ImGui::OpenPopup("add_entity");
if (ImGui::BeginPopup("add_entity")) {
EntityPopup(scene);
ImGui::EndPopup();
}
ImGui::End();
}
ImGui::End();
// Property window
ImGui::Begin("Properties");
if (selected_context) {
DrawComponents(selected_context);
if (ImGui::BeginPopupContextWindow(0, ImGuiPopupFlags_MouseButtonRight | ImGuiPopupFlags_NoOpenOverItems))
{
if (ImGui::MenuItem("Add transform"))
{
selected_context.AddComponents<TransformComponent>();
}
if (ImGui::MenuItem("Add Sprite"))
{
selected_context.AddComponents<SpriteRendererComponent>();
}
if (ImGui::MenuItem("Add camera"))
{
selected_context.AddComponents<CameraComponent>();
}
if (ImGui::BeginPopupContextWindow(0, ImGuiPopupFlags_MouseButtonRight | ImGuiPopupFlags_NoOpenOverItems)) {
ComponentPopup(selected_context);
ImGui::EndPopup();
}
// Centred fullwidth bottom button to add component
float button_height = ImGui::GetFrameHeight();
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetContentRegionAvail().y - button_height);
if (ImGui::Button("Add Component", ImVec2(ImGui::GetContentRegionAvail().x, 0.0f)))
ImGui::OpenPopup("add_component");
if (ImGui::BeginPopup("add_component")) {
ComponentPopup(selected_context);
ImGui::EndPopup();
}
}
@@ -73,6 +118,7 @@ namespace OpenEngine {
{
auto& tag = entity.GetComponents<TagComponent>().tag;
bool entity_marked_deletion = false;
if (renamed_entity == entity && selected_context == entity) {
char buffer[255];
std::memset(buffer, 0, sizeof(buffer));
@@ -93,13 +139,18 @@ namespace OpenEngine {
renamed_entity = {};
ImGui::PopStyleVar();
} else {
ImGuiTreeNodeFlags flags = ((selected_context == entity) ? ImGuiTreeNodeFlags_Selected : 0) | ImGuiTreeNodeFlags_OpenOnArrow;
flags |= ImGuiTreeNodeFlags_SpanAvailWidth;
flags |= ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_SpanAvailWidth;
bool opened = ImGui::TreeNodeEx((void*)(uint64_t)(uint32_t)entity, flags, "%s", tag.c_str());
if (ImGui::BeginPopupContextItem()) {
if (ImGui::MenuItem("Delete entity"))
entity_marked_deletion = true;
ImGui::EndPopup();
}
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
// FocusViewportOnEntity(entity); // future implementation
renamed_entity = {};
@@ -120,10 +171,15 @@ namespace OpenEngine {
last_selected_time = current_time;
}
}
if (opened)
ImGui::TreePop();
}
if (entity_marked_deletion) {
scene->DeleteEntity(entity);
if (selected_context == entity)
selected_context = {};
}
}
void SceneHierarchy::DrawComponents(Entity& entity)