diff options
Diffstat (limited to 'editor/editor.cpp')
| -rw-r--r-- | editor/editor.cpp | 85 |
1 files changed, 34 insertions, 51 deletions
diff --git a/editor/editor.cpp b/editor/editor.cpp index ab8598c..2a4860a 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -12,6 +12,7 @@ #include "game-settings.hpp" #include "game.hpp" #include "imgui.h" +#include "level-types.hpp" #include "level.hpp" #include "math-helpers.hpp" #include "msdf-atlas-gen/msdf-atlas-gen.h" @@ -35,6 +36,7 @@ #include <cstdio> #include <filesystem> #include <future> +#include <fstream> #include <regex> #ifdef WIN32 @@ -84,7 +86,7 @@ CameraHandle selectedCamera = CameraHandle_MAX; static pke_ui_box *selected_ui_box = NULL; const char* const newSceneName = "newScene.pstf"; -bool shouldOpenLoadSceneDialog = false; +const char* const editorLevelName = "editor_level"; bool shouldOpenSaveSceneDialog = false; bool shouldOpenNewScene = false; bool shouldSaveProjectFile = false; @@ -108,6 +110,7 @@ glm::vec3 unproject(glm::vec3 windowCoords) { } void PkeEditor_Tick(double delta) { + const char *scene_to_load_or_create_path = nullptr; if (shouldRunCurrentScene) { shouldRunCurrentScene = false; auto *task = pk_new<std::packaged_task<void()>>(); @@ -142,48 +145,18 @@ void PkeEditor_Tick(double delta) { } if (shouldDisableEditor) { PkeEditor_Teardown(); + return; } if (shouldOpenNewScene) { shouldOpenNewScene = false; - if (editor_mstr.active_scene != nullptr) { - // TODO move most of this to the level - pke_ui_box_count_T box_count; - pke_ui_box **root_boxes = pke_ui_get_root_boxes(&box_count); - for (pke_ui_box_count_T i = 0; i < box_count; ++i) { - ECS_MarkForRemoval(root_boxes[i]); - } - pke_scene_remove(editor_mstr.active_scene->scene_handle); - } - ActiveCamera = &NullCamera; - editor_mstr.active_scene = pke_scene_create(newSceneName); - return; + editor_mstr.shouldLoadScene = false; + editor_mstr.shouldSaveScene = false; + scene_to_load_or_create_path = newSceneName; } if (shouldSaveProjectFile) { shouldSaveProjectFile = false; PkeProject_Save(); } - if (shouldOpenLoadSceneDialog) { - shouldOpenLoadSceneDialog = false; - auto *task = pk_new<std::packaged_task<void()>>(); - new (task) std::packaged_task<void()>( [] { - // TODO move most of this to the level - pke_ui_box_count_T box_count; - pke_ui_box **root_boxes = pke_ui_get_root_boxes(&box_count); - for (pke_ui_box_count_T i = 0; i < box_count; ++i) { - ECS_MarkForRemoval(root_boxes[i]); - } - const char * patterns[1] = {"*.pstf"}; - char *selectedScene = tinyfd_openFileDialog(nullptr, "cafebabe.pstf", 1, patterns, "Pke Scene Text File", 0); - if (editor_mstr.active_scene != nullptr) { - pke_scene_remove(editor_mstr.active_scene->scene_handle); - } - ActiveCamera = &NullCamera; - editor_mstr.active_scene = pke_scene_create(selectedScene); - return; - }); - PkeThreads_Enqueue(threadPoolHandle, task); - } - if (shouldOpenSaveSceneDialog) { assert(editor_mstr.active_scene != nullptr); shouldOpenSaveSceneDialog = false; @@ -225,27 +198,36 @@ void PkeEditor_Tick(double delta) { shouldRebuildProjectDir = true; } if (editor_mstr.target_scene_path.val != nullptr) { - // TODO move most of this to the level - pke_ui_box_count_T box_count; - pke_ui_box **root_boxes = pke_ui_get_root_boxes(&box_count); - for (pke_ui_box_count_T i = 0; i < box_count; ++i) { - ECS_MarkForRemoval(root_boxes[i]); + scene_to_load_or_create_path = editor_mstr.target_scene_path.val; + } + if (scene_to_load_or_create_path != nullptr) { + ActiveCamera = &NullCamera; + + pkeSettings.rt.nextLevel = pke_level_create(editorLevelName, pk_uuid_zed, pk_uuid_zed); + editor_mstr.active_scene = pke_scene_create(scene_to_load_or_create_path); + scene_instance si{}; + si.scene_handle = editor_mstr.active_scene->scene_handle; + pk_arr_append_t(&pkeSettings.rt.nextLevel->scene_instances, si); + + if (scene_to_load_or_create_path == newSceneName) { + return; } - if (editor_mstr.active_scene != nullptr) { - pke_scene_remove(editor_mstr.active_scene->scene_handle); + + std::filesystem::path p(scene_to_load_or_create_path); + std::ifstream f(p); + if (f.good()) { + pke_editor_scene_load(pkeSettings.rt.nextLevel, p.c_str()); } - ActiveCamera = &NullCamera; - pke_editor_scene_load(editor_mstr.target_scene_path.val); - std::filesystem::path p(editor_mstr.target_scene_path.val); - editor_mstr.active_scene = pke_scene_get_by_name(p.stem().c_str()); - if (editor_mstr.active_scene) { + + if (editor_mstr.target_scene_path.reserved > 0) { editor_mstr.active_scene->file_path = editor_mstr.target_scene_path; + editor_mstr.target_scene_path = {}; } - editor_mstr.target_scene_path = {}; + return; } - if (selectedEntity && pk_arr_find_first_index(&EntitiesToBeRemoved, ECS_GetEntity(selectedEntity->entHandle), ecs_pk_arr_find_first_matching_pointer) != uint32_t(-1)) { + if (selectedEntity && ECS_GetEntity(selectedEntity->entHandle)->isMarkedForRemoval == true) { selectedEntity = nullptr; } @@ -681,7 +663,7 @@ void RecordImGuiEditorWrapper() { * - the goal is not to prevent a specific scene name, * I just want to know if they clicked the "New Scene" button */ - ImGui::BeginDisabled(editor_mstr.active_scene && editor_mstr.active_scene->name == newSceneName); + ImGui::BeginDisabled(editor_mstr.active_scene && (editor_mstr.active_scene->name == newSceneName || editor_mstr.active_scene->file_path.val == nullptr)); if (ImGui::MenuItem("Save")) { shouldSaveProjectFile = true; editor_mstr.shouldSaveScene = true; @@ -1153,7 +1135,7 @@ void RecordImGuiCameras() { cam.type = ActiveCamera->type; cam.view = ActiveCamera->view; cam.isPrimary = false; - pke_scene_register_camera(editor_mstr.active_scene->scene_handle, cam.camHandle); + pke_level_register_camera(pkeSettings.rt.activeLevel, &cam); } static ImGuiTableFlags tableFlags{ @@ -1317,6 +1299,7 @@ void RecordImGuiUITree() { box = pke_ui_box_new_child(selected_ui_box, type); } else { box = pke_ui_box_new_root(type); + pke_level_register_root_ui_box(pkeSettings.rt.activeLevel, box); } box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_DYNAMIC; box->flags |= PKE_UI_BOX_FLAG_CENTER_BOTH; |
