From 242317c1f7f2a6abdfbdbc99d5297539bbdc842f Mon Sep 17 00:00:00 2001 From: hnOsmium0001 Date: Sat, 30 Apr 2022 13:55:20 -0700 Subject: Add ImGuizmo for GameObjects, start to make things actually render --- source/EditorCore.cpp | 198 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 169 insertions(+), 29 deletions(-) (limited to 'source/EditorCore.cpp') diff --git a/source/EditorCore.cpp b/source/EditorCore.cpp index 8288df3..bd5d78d 100644 --- a/source/EditorCore.cpp +++ b/source/EditorCore.cpp @@ -20,7 +20,6 @@ #define GLFW_INCLUDE_NONE #include -#include #include #include #include @@ -204,6 +203,117 @@ void EditorContentBrowser::Show(bool* open) { ImGui::End(); } +void EditorTransformEdit::ShowWorld(float* cameraView, float* cameraProjection) { + glm::mat4 identityMatrix(1.00f); + + ImGuiIO& io = ImGui::GetIO(); + float viewManipulateRight = io.DisplaySize.x; + float viewManipulateTop = 0; + static ImGuiWindowFlags gizmoWindowFlags = 0; + + ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y); + + if (showGrid) { + ImGuizmo::DrawGrid(cameraView, cameraProjection, &identityMatrix[0][0], 100.f); + } + + ImGuizmo::ViewManipulate(cameraView, camDistance, ImVec2(viewManipulateRight - 128, viewManipulateTop), ImVec2(128, 128), 0x10101010); +} + +bool EditorTransformEdit::ShowObjectInspector(float* cameraView, float* cameraProjection, glm::vec3* pos, glm::quat* rotation, glm::vec3* scale) { + glm::mat4 identityMatrix(1.00f); + + if (ImGui::IsKeyPressed(90)) + mCurrentGizmoOperation = ImGuizmo::TRANSLATE; + if (ImGui::IsKeyPressed(69)) + mCurrentGizmoOperation = ImGuizmo::ROTATE; + if (ImGui::IsKeyPressed(82)) // r Key + mCurrentGizmoOperation = ImGuizmo::SCALE; + if (ImGui::RadioButton("Translate", mCurrentGizmoOperation == ImGuizmo::TRANSLATE)) + mCurrentGizmoOperation = ImGuizmo::TRANSLATE; + ImGui::SameLine(); + if (ImGui::RadioButton("Rotate", mCurrentGizmoOperation == ImGuizmo::ROTATE)) + mCurrentGizmoOperation = ImGuizmo::ROTATE; + ImGui::SameLine(); + if (ImGui::RadioButton("Scale", mCurrentGizmoOperation == ImGuizmo::SCALE)) + mCurrentGizmoOperation = ImGuizmo::SCALE; + if (ImGui::RadioButton("Universal", mCurrentGizmoOperation == ImGuizmo::UNIVERSAL)) + mCurrentGizmoOperation = ImGuizmo::UNIVERSAL; + + bool edited = false; + + float* matTranslation = &pos->x; + edited |= ImGui::InputFloat3("Tr", matTranslation); + + glm::vec3 eulerAngleRotation = glm::eulerAngles(*rotation); + float* matRotation = &eulerAngleRotation.x; + edited |= ImGui::InputFloat3("Rt", matRotation); + + float* matScale = &scale->x; + edited |= ImGui::InputFloat3("Sc", matScale); + + if (mCurrentGizmoOperation != ImGuizmo::SCALE) { + if (ImGui::RadioButton("Local", mCurrentGizmoMode == ImGuizmo::LOCAL)) + mCurrentGizmoMode = ImGuizmo::LOCAL; + ImGui::SameLine(); + if (ImGui::RadioButton("World", mCurrentGizmoMode == ImGuizmo::WORLD)) + mCurrentGizmoMode = ImGuizmo::WORLD; + } + if (ImGui::IsKeyPressed(83)) + useSnap = !useSnap; + ImGui::Checkbox("##UseSnap", &useSnap); + ImGui::SameLine(); + + switch (mCurrentGizmoOperation) { + case ImGuizmo::TRANSLATE: + ImGui::InputFloat3("Snap", &snap[0]); + break; + case ImGuizmo::ROTATE: + ImGui::InputFloat("Angle Snap", &snap[0]); + break; + case ImGuizmo::SCALE: + ImGui::InputFloat("Scale Snap", &snap[0]); + break; + default: break; + } + ImGui::Checkbox("Bound Sizing", &boundSizing); + if (boundSizing) { + ImGui::PushID(3); + ImGui::Checkbox("##BoundSizing", &boundSizingSnap); + ImGui::SameLine(); + ImGui::InputFloat3("Snap", boundsSnap); + ImGui::PopID(); + } + + return edited; +} + +bool EditorTransformEdit::ShowObject(float* cameraView, float* cameraProjection, glm::vec3* pos, glm::quat* rotation, glm::vec3* scale) { + glm::mat4 identityMatrix(1.00f); + glm::vec3 eulerAngleRotation = glm::eulerAngles(*rotation); + + float matrix[16]; + ImGuizmo::RecomposeMatrixFromComponents(&pos->x, &eulerAngleRotation.x, &scale->x, matrix); + + bool edited = ImGuizmo::Manipulate( + cameraView, + cameraProjection, + mCurrentGizmoOperation, + mCurrentGizmoMode, + matrix, + nullptr, + useSnap ? &snap[0] : nullptr, + boundSizing ? bounds : nullptr, + boundSizingSnap ? boundsSnap : nullptr); + + if (edited) { + ImGuizmo::DecomposeMatrixToComponents(matrix, &pos->x, &eulerAngleRotation.x, &scale->x); + *rotation = glm::quat(eulerAngleRotation); + } + + return edited; +} + namespace ProjectBrussel_UNITY_ID { void PushKeyCodeRecorder(App* app, int* writeKey, bool* writeKeyStatus) { app->PushKeyCaptureCallback([=](int key, int action) { @@ -300,22 +410,32 @@ struct CreatableGameObject { #undef GAMEOBJECT_CONSTRUCTOR } // namespace ProjectBrussel_UNITY_ID -EditorInstance::EditorInstance(App* app, GameWorld* world) +EditorInstance::EditorInstance(App* app) : mApp{ app } - , mWorld{ world } + , mFallbackFrameInfo{ + .camera = nullptr, + .matrixCombined = {}, + .time = 0.0f, + .deltaTime = 0.0f, + } + , mCurrentFrameInfo{ &mFallbackFrameInfo } , mEdContentBrowser(&mEdInspector) {} EditorInstance::~EditorInstance() { } +void EditorInstance::OnUpdate() { +} + +void EditorInstance::OnDraw(const RendererFrameInfo& frameInfo) { + mCurrentFrameInfo = &frameInfo; +} + void EditorInstance::Show() { using namespace ProjectBrussel_UNITY_ID; using namespace Tags; - if (!mWorld) { - return; - } - + auto world = mApp->GetWorld(); auto& io = ImGui::GetIO(); ImGui::BeginMainMenuBar(); @@ -348,11 +468,34 @@ void EditorInstance::Show() { mEdContentBrowser.Show(&mWindowVisible_ContentBrowser); } + auto cameraViewMat = mCurrentFrameInfo->matrixView; + auto cameraView = &cameraViewMat[0][0]; + auto cameraProjMat = mCurrentFrameInfo->matrixProj; + auto cameraProj = &cameraProjMat[0][0]; + // auto cameraView = &mCurrentFrameInfo->matrixView[0][0]; + // auto cameraProj = &mCurrentFrameInfo->matrixProj[0][0]; + // TODO moving camera + // mEdTransformEdit.ShowWorld(cameraView, cameraProj); + ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y); + ImGuizmo::SetDrawlist(ImGui::GetForegroundDrawList()); + if (mWindowVisible_Inspector) { ImGui::Begin("Inspector"); switch (mEdInspector.selectedItt) { case EditorInspector::ITT_GameObject: { - ShowInspector(static_cast(mEdInspector.selectedItPtr)); + auto object = static_cast(mEdInspector.selectedItPtr); + + auto objectPos = object->GetPos(); + auto objectRot = object->GetRotation(); + glm::vec3 scale(1.0f); + if (mEdTransformEdit.ShowObjectInspector(cameraView, cameraProj, &objectPos, &objectRot, &scale) || + mEdTransformEdit.ShowObject(cameraView, cameraProj, &objectPos, &objectRot, &scale)) + { + object->SetPos(objectPos); + object->SetRotation(objectRot); + } + + ShowInspector(object); } break; case EditorInspector::ITT_Ires: { @@ -377,7 +520,7 @@ void EditorInstance::Show() { GobjTreeNodeShowInfo showInfo{ .in_editor = this, }; - GobjTreeNode(showInfo, &mWorld->GetRoot()); + GobjTreeNode(showInfo, &world->GetRoot()); if (showInfo.out_openPopup) { mPopupCurrent_GameObject = showInfo.out_openPopup; @@ -395,7 +538,7 @@ void EditorInstance::Show() { for (size_t i = 0; i < std::size(creatableGameObjects); ++i) { auto& info = creatableGameObjects[i]; if (ImGui::MenuItem(info.name)) { - auto object = info.factory(mWorld); + auto object = info.factory(world); mPopupCurrent_GameObject->AddChild(object); } } @@ -444,22 +587,14 @@ void EditorInstance::ShowInspector(GameObject* object) { using namespace Tags; using namespace ProjectBrussel_UNITY_ID; - auto ShowFields = [&]() { - auto pos = object->GetPos(); - if (ImGui::InputFloat3("Position", &pos.x)) { - object->SetPos(pos); - } - - auto quat = object->GetRotation(); - if (ImGui::InputFloat4("Rotation", &quat.x)) { - object->SetRotation(quat); - } + auto ShowBasics = [&]() { + // TODO }; auto type = object->GetKind(); switch (type) { case GameObject::KD_Player: { - ShowFields(); + ShowBasics(); ImGui::Separator(); auto player = static_cast(object); @@ -475,7 +610,9 @@ void EditorInstance::ShowInspector(GameObject* object) { if (auto payload = ImGui::AcceptDragDropPayload(IresObject::ToString(IresObject::KD_Spritesheet).data())) { auto spritesheet = *static_cast(payload->Data); ea->confSprite.Attach(spritesheet); - player->sprite.SetDefinition(spritesheet->GetInstance()); + auto def = spritesheet->GetInstance(); + player->sprite.SetDefinition(def); + player->renderObject.autofill_TextureAtlas.Attach(def->GetAtlas()); } ImGui::EndDragDropTarget(); } @@ -532,15 +669,18 @@ void EditorInstance::ShowInspector(GameObject* object) { } break; case GameObject::KD_SimpleGeometry: { - ShowFields(); - ImGui::Separator(); - auto sg = static_cast(object); - // TODO + + ShowBasics(); + + auto size = sg->GetSize(); + if (ImGui::InputFloat2("Size", &size.x)) { + sg->SetSize(size); + } } break; case GameObject::KD_Building: { - ShowFields(); + ShowBasics(); ImGui::Separator(); auto b = static_cast(object); @@ -548,7 +688,7 @@ void EditorInstance::ShowInspector(GameObject* object) { } break; case GameObject::KD_LevelWrapper: { - ShowFields(); + ShowBasics(); ImGui::Separator(); auto lwo = static_cast(object); @@ -556,7 +696,7 @@ void EditorInstance::ShowInspector(GameObject* object) { } break; default: { - ShowFields(); + ShowBasics(); } break; } } -- cgit v1.2.3-70-g09d2