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

41
editor/CMakeLists.txt Normal file
View File

@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.28)
set(CMAKE_CXX_STANDARD 20)
set(PROJECT_EXECUTABLE_NAME oe_editor)
set(CMAKE_BUILD_RPATH ".")
project(SandBox
VERSION 0.1.0
)
find_package(imgui REQUIRED CONFIG)
find_package(spdlog REQUIRED CONFIG)
find_package(EnTT REQUIRED)
file(GLOB_RECURSE SRC_FILES "src/*.cpp")
add_executable(${PROJECT_EXECUTABLE_NAME}
${SRC_FILES}
)
target_include_directories(${PROJECT_EXECUTABLE_NAME} PRIVATE
"${PROJECT_SOURCE_DIR}/include"
"${PROJECT_SOURCE_DIR}/editor/include"
"/usr/include/freetype2/"
)
target_link_libraries(${PROJECT_EXECUTABLE_NAME} PRIVATE
spdlog::spdlog
imgui::imgui
open_engine
dl
nethost
freetype
EnTT::EnTT
)
target_link_directories(${PROJECT_EXECUTABLE_NAME} PRIVATE
${PROJECT_SOURCE_DIR}/lib
/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.3/runtimes/arch-x64/native
)

View File

@@ -1,10 +1,11 @@
#ifndef EDITOR_HPP
#define EDITOR_HPP
#include "open_engine/ref_scope.hpp"
#include "panels/scene_hierarchy.hpp"
#include <open_engine.hpp>
#include "panels/scene_hierarchy.hpp"
//#include <imfilebrowser.h>
#include <glm/ext/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/matrix.hpp>
@@ -27,16 +28,18 @@ namespace OpenEngine {
{
auto delta = Time::DeltaTime();
auto& position = GetComponent<TransformComponent>().translation;
if (HasComponent<TransformComponent>()) {
auto& position = GetComponent<TransformComponent>().translation;
if (Input::IsKeyPressed(KeyCode::Up))
position.y += 5.0 * delta;
if (Input::IsKeyPressed(KeyCode::Down))
position.y -= 5.0 * delta;
if (Input::IsKeyPressed(KeyCode::Right))
position.x += 5.0 * delta;
if (Input::IsKeyPressed(KeyCode::Left))
position.x -= 5.0 * delta;
if (Input::IsKeyPressed(KeyCode::Up))
position.y += 5.0 * delta;
if (Input::IsKeyPressed(KeyCode::Down))
position.y -= 5.0 * delta;
if (Input::IsKeyPressed(KeyCode::Right))
position.x += 5.0 * delta;
if (Input::IsKeyPressed(KeyCode::Left))
position.x -= 5.0 * delta;
}
};
void OnDestroy()
@@ -48,16 +51,11 @@ namespace OpenEngine {
{
public:
EditorLayer()
: Layer("editor_layer")/*,
camera((float)Application::Get().GetWindow().GetWidth() /
Application::Get().GetWindow().GetHeight(), 1.0f)
*/
{
}
: Layer("editor_layer")
{
}
~EditorLayer() {};
entt::registry registry;
void OnAttach() override
{
OE_PROFILE_FUNCTION();
@@ -70,15 +68,15 @@ namespace OpenEngine {
scene = CreateRef<Scene>();
sq_entity = scene->CreateEntity("square");
//sq_entity = scene->CreateEntity("square");
sq_entity.AddComponents<TransformComponent>();
sq_entity.AddComponents<SpriteRendererComponent>(glm::vec4(1.0f, 0.0f, 0.0f, 1.0f));
//sq_entity.AddComponents<TransformComponent>();
//sq_entity.AddComponents<SpriteRendererComponent>(glm::vec4(1.0f, 0.0f, 0.0f, 1.0f));
camera_bis = scene->CreateEntity("Main camera");
camera_bis.AddComponents<TransformComponent>();
camera_bis.AddComponents<CameraComponent>();
camera_bis.AddComponents<NativeScriptComponent>().Bind<CameraController>();
//camera_bis = scene->CreateEntity("Main camera");
//camera_bis.AddComponents<TransformComponent>();
//camera_bis.AddComponents<CameraComponent>();
//camera_bis.AddComponents<NativeScriptComponent>().Bind<CameraController>();
scene_hierarchy.SetContext(scene);
}
@@ -258,10 +256,68 @@ namespace OpenEngine {
ImGui::End();
};
/*
void test2()
{
// (optional) set browser properties
fileDialog.SetTitle("title");
fileDialog.SetTypeFilters({ ".oes" });
fileDialog.Open();
};
*/
void OnImGuiRender() override
{
OE_PROFILE_FUNCTION();
{
// Testing file pickers
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Save As", "Ctrl+S")) {
std::string file = FileDialogs::SaveFile("useless");
OE_TRACE("saving to filename: {}", file);
if (!file.empty()) {
SceneSerializer serializer(scene);
serializer.Serialize(file);
}
}
if (ImGui::MenuItem("Load", "Ctrl+O")) {
std::string file = FileDialogs::OpenFile("useless");
OE_DEBUG("loading filename: {}", file);
if (!file.empty()) {
scene = CreateRef<Scene>();
scene->OnViewportResize((uint32_t)viewport_size.x, (uint32_t)viewport_size.y);
scene_hierarchy.SetContext(scene);
SceneSerializer serializer(scene);
serializer.Deserialize(file);
}
}
if (ImGui::MenuItem("New Scene", "Ctrl+N")) {
scene = CreateRef<Scene>();
scene->OnViewportResize((uint32_t)viewport_size.x, (uint32_t)viewport_size.y);
scene_hierarchy.SetContext(scene);
}
ImGui::Separator();
if (ImGui::MenuItem("Exit"))
Application::Get().Close();
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
/*
fileDialog.Display();
if(fileDialog.HasSelected())
{
OE_TRACE("Selected filename {}", fileDialog.GetSelected().string());
fileDialog.ClearSelected();
}
*/
}
ImGui::DockSpaceOverViewport();
DrawStats();
@@ -279,12 +335,13 @@ namespace OpenEngine {
glm::vec2 viewport_size = { 0.0f, 0.0f };
Ref<OpenEngine::FrameBuffer> framebuffer;
SceneHierarchy scene_hierarchy;
//OrthographicCameraController camera;
Entity camera_bis;
Ref<Scene> scene;
Entity sq_entity;
//ImGui::FileBrowser fileDialog;
};
}
@@ -296,3 +353,24 @@ class EditorApp : public OpenEngine::Application
};
#endif // EDITOR_HPP
/*
#include <cstdio>
#include <string>
std::string OpenFile() {
char buffer[1024];
// This command opens a native GTK file picker and returns the path
FILE* pipe = popen("zenity --file-selection", "r");
if (!pipe) return "";
if (fgets(buffer, sizeof(buffer), pipe) != NULL) {
std::string path = buffer;
path.erase(path.find_last_not_of("\n") + 1); // Clean newline
pclose(pipe);
return path;
}
pclose(pipe);
return "";
}
*/

View File

@@ -5,9 +5,9 @@
#include <open_engine.hpp>
#include <unistd.h>
#include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.0/runtimes/arch-x64/native/nethost.h"
#include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.0/runtimes/arch-x64/native/hostfxr.h"
#include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.0/runtimes/arch-x64/native/coreclr_delegates.h"
#include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.3/runtimes/arch-x64/native/nethost.h"
#include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.3/runtimes/arch-x64/native/hostfxr.h"
#include "/usr/share/dotnet/packs/Microsoft.NETCore.App.Host.arch-x64/10.0.3/runtimes/arch-x64/native/coreclr_delegates.h"
class Modding : public OpenEngine::Layer
{

View File

@@ -2,7 +2,6 @@
#define SCENE_HIERARCHY_HPP
#include "imgui.h"
#include <array>
#include <open_engine.hpp>
namespace OpenEngine {
@@ -23,12 +22,42 @@ namespace OpenEngine {
template <typename T>
void DrawComponentDrawer(Entity& entity, const char* header)
{
if (entity.HasComponent<T>())
if (ImGui::TreeNodeEx((void*)typeid(T).hash_code(), ImGuiTreeNodeFlags_DefaultOpen, "%s", header)) {
bool component_marked_deletion = false;
ImVec2 region_available = ImGui::GetContentRegionAvail();
ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_DefaultOpen
| ImGuiTreeNodeFlags_Framed
| ImGuiTreeNodeFlags_AllowOverlap;
if (entity.HasComponent<T>()) {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{ 4, 4 });
float line_height = ImGui::GetFontSize() + ImGui::GetStyle().FramePadding.y * 2.0f;
bool opened = ImGui::TreeNodeEx((void*)typeid(T).hash_code(), tree_node_flags, "%s", header);
ImGui::SameLine(region_available.x - line_height * 0.5f);
ImGui::PushStyleColor(ImGuiCol_Button, { 0.290f, 0.301f, 0.388f, 1.0f });
if (ImGui::Button("...", ImVec2{ line_height, line_height }))
ImGui::OpenPopup("component_settings");
ImGui::PopStyleColor();
ImGui::PopStyleVar();
if (ImGui::BeginPopup("component_settings")) {
if (ImGui::MenuItem("Remove component"))
component_marked_deletion = true;
ImGui::EndPopup();
}
if (opened) {
entity.GetComponents<T>().OnImGuiRender(entity);
ImGui::TreePop();
ImGui::Separator();
}
ImGui::Separator();
}
if (component_marked_deletion)
entity.RemoveComponents<T>();
}
private:

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)