From e7ef3f208c109357538b1f68af10bcd78db95c95 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Fri, 8 Apr 2022 22:30:12 -0700 Subject: Changeset: 3 More work --- source/EditorCore.cpp | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 source/EditorCore.cpp (limited to 'source/EditorCore.cpp') diff --git a/source/EditorCore.cpp b/source/EditorCore.cpp new file mode 100644 index 0000000..3bf9ecb --- /dev/null +++ b/source/EditorCore.cpp @@ -0,0 +1,200 @@ +#include "EditorCore.hpp" +#include "EditorCoreAPI.hpp" + +#include "App.hpp" +#include "AppConfig.hpp" +#include "CpuMesh.hpp" +#include "EditorAccessories.hpp" +#include "EditorCore_Egoa.hpp" +#include "EditorNotification.hpp" +#include "GameObjectTypeTag.hpp" +#include "Level.hpp" +#include "Mesh.hpp" +#include "Player.hpp" + +#define GLFW_INCLUDE_NONE +#include + +#include +#include +#include +#include +#include +#include + +namespace ProjectBrussel_UNITY_ID { +void PushKeyCodeRecorder(App* app, int* writeKey, bool* writeKeyStatus) { + app->PushKeyCaptureCallback([=](int key, int action) { + // Allow the user to cancel by pressing Esc + if (key == GLFW_KEY_ESCAPE) { + return true; + } + + if (action == GLFW_PRESS) { + *writeKey = key; + *writeKeyStatus = writeKeyStatus; + return true; + } + return false; + }); +} +} // namespace ProjectBrussel_UNITY_ID + +const char* ImGui::GetKeyNameGlfw(int key) { + return GetKeyName(ImGui_ImplGlfw_KeyToImGuiKey(key)); +} + +std::unique_ptr EditorGameObjectAttachment::Create(GameObject* object) { + EditorGameObjectAttachment* result; + + using namespace Tags; + switch (object->GetTypeTag()) { + case GOT_Player: result = new EgoaPlayer(); break; + case GOT_LevelWrapper: result = new EgoaLevelWrapper(); break; + + default: result = new EditorGameObjectAttachment(); break; + } + + result->name = NameOf(object->GetTypeTag()); + return std::unique_ptr(result); +} + +void EditorInstance::Show() { + if (!mWorld) return; + + ImGui::Begin("World properties"); + ShowWorldProperties(); + ImGui::End(); + + ImGui::Begin("World structure"); + ShowGameObjectInTree(&mWorld->GetRoot()); + ImGui::End(); + + ImGui::Begin("Inspector"); + ShowInspector(); + ImGui::End(); +} + +void EditorInstance::ShowWorldProperties() { +} + +void EditorInstance::ShowInspector() { + using namespace Tags; + using namespace ProjectBrussel_UNITY_ID; + + if (!mSelectedGameObject) return; + + auto type = mSelectedGameObject->GetTypeTag(); + switch (type) { + case Tags::GOT_Player: { + ShowGameObjecetFields(mSelectedGameObject); + ImGui::Separator(); + + auto player = static_cast(mSelectedGameObject); + auto& kb = player->keybinds; + + ImGui::Text("Player #%d", player->GetId()); + + if (ImGui::Button("Load config")) { + bool success = player->LoadFromFile(); + if (success) { + ImGui::AddNotification(ImGuiToast(ImGuiToastType_Success, "Successfully loaded player config")); + } + } + ImGui::SameLine(); + if (ImGui::Button("Save config")) { + bool success = player->SaveToFile(); + if (success) { + ImGui::AddNotification(ImGuiToast(ImGuiToastType_Success, "Successfully saved player config")); + } + } + + ImGui::Text("Move left (%s)", ImGui::GetKeyNameGlfw(kb.keyLeft)); + ImGui::SameLine(); + if (ImGui::Button("Change##Move left")) { + PushKeyCodeRecorder(mApp, &kb.keyLeft, &kb.pressedLeft); + } + + ImGui::Text("Move right (%s)", ImGui::GetKeyNameGlfw(kb.keyRight)); + ImGui::SameLine(); + if (ImGui::Button("Change##Move right")) { + PushKeyCodeRecorder(mApp, &kb.keyRight, &kb.pressedRight); + } + + ImGui::Text("Jump (%s)", ImGui::GetKeyNameGlfw(kb.keyJump)); + ImGui::SameLine(); + if (ImGui::Button("Change##Jump")) { + PushKeyCodeRecorder(mApp, &kb.keyJump, &kb.pressedJump); + } + + ImGui::Text("Attack (%s)", ImGui::GetKeyNameGlfw(kb.keyAttack)); + ImGui::SameLine(); + if (ImGui::Button("Change##Attack")) { + PushKeyCodeRecorder(mApp, &kb.keyAttack, &kb.pressedAttack); + } + } break; + + case Tags::GOT_LevelWrapper: { + ShowGameObjecetFields(mSelectedGameObject); + ImGui::Separator(); + + auto lwo = static_cast(mSelectedGameObject); + // TODO + } break; + + default: + ShowGameObjecetFields(mSelectedGameObject); + break; + } +} + +void EditorInstance::ShowGameObjecetFields(GameObject* object) { +} + +void EditorInstance::ShowGameObjectInTree(GameObject* object) { + auto attachment = object->GetEditorAttachment(); + if (!attachment) { + attachment = EditorGameObjectAttachment::Create(object).release(); + object->SetEditorAttachment(attachment); // NOTE: takes ownership + } + + ImGuiTreeNodeFlags flags = 0; + flags |= ImGuiTreeNodeFlags_DefaultOpen; + flags |= ImGuiTreeNodeFlags_OpenOnDoubleClick; + flags |= ImGuiTreeNodeFlags_OpenOnArrow; + flags |= ImGuiTreeNodeFlags_SpanAvailWidth; + if (mSelectedGameObject == object) { + flags |= ImGuiTreeNodeFlags_Selected; + } + + if (ImGui::TreeNodeEx(attachment->name.c_str(), flags)) { + if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen()) { + mSelectedGameObject = object; + } + + for (auto& child : object->GetChildren()) { + ShowGameObjectInTree(child); + } + ImGui::TreePop(); + } +} + +// ======================== // +// EditorCoreAPI.hpp things // +// ======================== // + +void EditorGameObjectAttachmentDeleter::operator()(EditorGameObjectAttachment* obj) { + delete obj; +} + +EditorInstance* EditorInstance_Alloc(App* app, GameWorld* world) { + return new EditorInstance(app, world); +} + +void EditorInstance_Free(EditorInstance* editor) { + delete editor; +} + +void EditorInstance_Show(EditorInstance* editor) { + editor->Show(); +} -- cgit v1.2.3-70-g09d2