From eb32e873870d2153562a4addf0854bdeebe72aad Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 14 Nov 2022 15:10:23 +0800 Subject: [PATCH 01/14] Camera Orthographic projection --- SHADE_Engine/src/Camera/SHCameraSystem.cpp | 37 ++++++++++++++-------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.cpp b/SHADE_Engine/src/Camera/SHCameraSystem.cpp index ff942666..5ae6d35e 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.cpp +++ b/SHADE_Engine/src/Camera/SHCameraSystem.cpp @@ -107,7 +107,11 @@ namespace SHADE editorCamera.SetPitch(0.0f); editorCamera.SetYaw(0.0f); editorCamera.SetRoll(0.0f); + editorCamera.SetWidth(1080.0f); + editorCamera.SetHeight(720.0f); + editorCamera.SetFar(10000000.0f); editorCamera.movementSpeed = 2.0f; + editorCamera.perspProj = true; SHComponentManager::CreateComponentSparseSet(); SHComponentManager::CreateComponentSparseSet(); @@ -228,18 +232,24 @@ namespace SHADE } else { - //const float R = camera.width * 0.5f; - //const float L = -R; - //const float T = camera.height * 0.5f; - //const float B = -T; + const float right = camera.GetWidth() * 0.5f; + const float left = -right; + const float top = camera.GetHeight() * 0.5f; + const float btm = -top; + const float n = camera.GetNear(); + const float f = camera.GetFar(); - //camera.projMatrix = SHMatrix::Identity; - //camera.projMatrix(0, 0) = 2.0f / (R - L); - //camera.projMatrix(1, 1) = 2.0f / (B - T); - //camera.projMatrix(2, 2) = 1.0f / (camera.zFar - camera.zNear); - //camera.projMatrix(3, 0) = -(R + L) / (R - L); - //camera.projMatrix(3, 1) = -(B + T) / (B - T); - //camera.projMatrix(3, 2) = -camera.zNear / (camera.zFar - camera.zNear); + camera.projMatrix = SHMatrix::Identity; + camera.projMatrix(0, 0) = 2.0f / (right - left); + camera.projMatrix(1, 1) = 2.0f / (btm - top); + camera.projMatrix(2, 2) = 1.0f / (f-n); + camera.projMatrix(0, 3) = -(right + left) / (right - left); + camera.projMatrix(1, 3) = -(btm + top) / (btm - top); + camera.projMatrix(2, 3) = -n / (f-n); + camera.projMatrix(3, 3) = 1.0f; + + camera.projMatrix = SHMatrix::OrthographicRH(camera.GetWidth(), camera.GetHeight(), camera.GetNear(), camera.GetFar()); + //camera.projMatrix.Transpose(); camera.dirtyProj = false; } @@ -252,8 +262,6 @@ namespace SHADE SHVec3 up = { 0.0f,1.0f,0.0f }; - - target = SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch)); target = SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw)); target += camera.position; @@ -287,6 +295,9 @@ namespace SHADE if (SHSceneManager::CheckNodeAndComponentsActive(cam.GetEID())) system->UpdateCameraComponent(cam); } + + + for (auto& handle : system->directorHandleList) { handle->UpdateMatrix(); From 1bd71832310686f7a7ccd72ad220a7717e2cf4f2 Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Wed, 16 Nov 2022 17:30:19 +0800 Subject: [PATCH 02/14] Added validation for Renderable drag drop fields --- SHADE_Engine/src/Assets/SHAssetManager.cpp | 12 +++++ SHADE_Engine/src/Assets/SHAssetManager.h | 2 + .../src/Editor/DragDrop/SHDragDrop.cpp | 7 ++- .../src/Editor/DragDrop/SHDragDrop.hpp | 13 ++++-- .../HierarchyPanel/SHHierarchyPanel.cpp | 46 +++++++++---------- .../HierarchyPanel/SHHierarchyPanel.h | 3 +- .../Inspector/SHEditorComponentView.hpp | 10 ++++ 7 files changed, 64 insertions(+), 29 deletions(-) diff --git a/SHADE_Engine/src/Assets/SHAssetManager.cpp b/SHADE_Engine/src/Assets/SHAssetManager.cpp index b4ea7d35..8d2c8dc3 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.cpp +++ b/SHADE_Engine/src/Assets/SHAssetManager.cpp @@ -143,6 +143,18 @@ namespace SHADE return result; } + AssetType SHAssetManager::GetType(AssetID id) noexcept + { + if (assetCollection.contains(id)) + { + return assetCollection[id].type; + } + + SHLOG_WARNING("AssetID {}, does not belong to an asset", id) + + return AssetType::INVALID; + } + /**************************************************************************** * \brief Create record for new asset. CAN ONLY CREATE FOR CUSTOM * ASSETS CREATED BY THE ENGINE. diff --git a/SHADE_Engine/src/Assets/SHAssetManager.h b/SHADE_Engine/src/Assets/SHAssetManager.h index f6ecb3a3..0962a41f 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.h +++ b/SHADE_Engine/src/Assets/SHAssetManager.h @@ -51,6 +51,8 @@ namespace SHADE ****************************************************************************/ static std::vector GetAllAssets() noexcept; + static AssetType GetType(AssetID id) noexcept; + /**************************************************************************** * \brief Create record for new resource. CAN ONLY CREATE FOR CUSTOM * RESOURCES CREATED BY THE ENGINE. diff --git a/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.cpp b/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.cpp index 97fd8a22..4c56d032 100644 --- a/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.cpp +++ b/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.cpp @@ -5,6 +5,7 @@ namespace SHADE { bool SHDragDrop::hasDragDrop = false; + SHDragDrop::DragDropTag SHDragDrop::currentDragDropTag{}; bool SHDragDrop::BeginSource(ImGuiDragDropFlags const flags) { return ImGui::BeginDragDropSource(flags); } @@ -16,6 +17,10 @@ namespace SHADE { return ImGui::BeginDragDropTarget(); } void SHDragDrop::EndTarget() - { ImGui::EndDragDropTarget(); hasDragDrop = false;} + { + ImGui::EndDragDropTarget(); + hasDragDrop = false; + currentDragDropTag = {}; + } } diff --git a/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.hpp b/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.hpp index ce0615e1..f111eaeb 100644 --- a/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.hpp +++ b/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.hpp @@ -19,9 +19,13 @@ namespace SHADE static void EndSource(); template - static bool SetPayload(std::string_view const type, T* object, ImGuiCond const cond = 0) + static bool SetPayload(DragDropTag const& type, T* object, ImGuiCond const cond = 0) { - hasDragDrop = ImGui::SetDragDropPayload(type.data(), static_cast(object), sizeof(T), cond); + ImGui::SetDragDropPayload(type.data(), static_cast(object), sizeof(T), cond); + + hasDragDrop = true; + currentDragDropTag = type; + return hasDragDrop; } @@ -32,13 +36,16 @@ namespace SHADE static void EndTarget(); template - static T* AcceptPayload(std::string_view const type, ImGuiDragDropFlags const flags = 0) + static T* AcceptPayload(DragDropTag const& type, ImGuiDragDropFlags const flags = 0) { if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(type.data(), flags)) + { return static_cast(payload->Data); + } return nullptr; } static bool hasDragDrop; + static DragDropTag currentDragDropTag; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp index cf545223..2235f831 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp @@ -117,9 +117,12 @@ namespace SHADE { if(ImGui::IsDragDropActive()) { - ParentSelectedEntities(MAX_EID, draggingEntities); - draggingEntities.clear(); - ImGui::ClearDragDrop(); + if (SHDragDrop::currentDragDropTag == SHDragDrop::DRAG_EID) + { + ParentSelectedEntities(MAX_EID, draggingEntities); + draggingEntities.clear(); + ImGui::ClearDragDrop(); + } } } ImGui::End(); @@ -233,8 +236,9 @@ namespace SHADE { ParentSelectedEntities(eid, draggingEntities); draggingEntities.clear(); - SHDragDrop::EndTarget(); + //ImGui::ClearDragDrop(); } + SHDragDrop::EndTarget(); } //Context menu @@ -342,17 +346,12 @@ namespace SHADE SHEntityManager::CreateEntity(MAX_EID, "DefaultChild", parentEID); } - void SHHierarchyPanel::ParentSelectedEntities(EntityID parentEID, std::vector const& entities) const noexcept + void SHHierarchyPanel::ParentSelectedEntities(EntityID parentEID, std::vector const& entities) noexcept { auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); - std::vector entitiesToParent{}; - std::ranges::copy_if(entities, std::back_inserter(entitiesToParent), [&sceneGraph](EntityID const& eid) - { - if (sceneGraph.GetParent(eid)->GetEntityID() == MAX_EID) - return true; - return false; - }); + std::vector entitiesToParent = CleanUpEIDList(entities); + //auto const editor = SHSystemManager::GetSystem(); SHEntityParentCommand::EntityParentData entityParentData; std::vector parentedEIDS; @@ -419,14 +418,7 @@ namespace SHADE void SHHierarchyPanel::CopySelectedEntities() { const auto editor = SHSystemManager::GetSystem(); - auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); - std::vector entitiesToCopy{}; - std::ranges::copy_if(editor->selectedEntities, std::back_inserter(entitiesToCopy), [&sceneGraph](EntityID const& eid) - { - if(sceneGraph.GetParent(eid)->GetEntityID() == MAX_EID) - return true; - return false; - }); + std::vector entitiesToCopy = CleanUpEIDList(editor->selectedEntities); SHClipboardUtilities::WriteToClipboard(SHSerialization::SerializeEntitiesToString(entitiesToCopy)); } @@ -439,19 +431,25 @@ namespace SHADE void SHHierarchyPanel::DeleteSelectedEntities() { const auto editor = SHSystemManager::GetSystem(); + std::vector entitiesToDelete = CleanUpEIDList(editor->selectedEntities); + SHCommandManager::PerformCommand(std::make_shared(entitiesToDelete)); + } + + std::vector SHHierarchyPanel::CleanUpEIDList(std::vector const& entities) + { + std::vector result; auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); - std::vector entitiesToDelete{}; - std::ranges::copy_if(editor->selectedEntities, std::back_inserter(entitiesToDelete), [&sceneGraph, &selectedEntities = editor->selectedEntities](EntityID const& eid) + std::ranges::copy_if(entities, std::back_inserter(result), [&sceneGraph, &entities](EntityID const& eid) { EntityID parentEID = sceneGraph.GetParent(eid)->GetEntityID(); if (parentEID == MAX_EID) return true; - else if(std::ranges::find(selectedEntities, parentEID) == selectedEntities.end()) + if (std::ranges::find(entities, parentEID) == entities.end()) return true; return false; }); - SHCommandManager::PerformCommand(std::make_shared(entitiesToDelete)); + return result; } }//namespace SHADE diff --git a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.h b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.h index 66b9ca2f..9278a0a3 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.h +++ b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.h @@ -27,12 +27,13 @@ namespace SHADE void DrawMenuBar() const noexcept; ImRect RecursivelyDrawEntityNode(SHSceneNode* const); void CreateChildEntity(EntityID parentEID) const noexcept; - void ParentSelectedEntities(EntityID parentEID, std::vector const& entities) const noexcept; + void ParentSelectedEntities(EntityID parentEID, std::vector const& entities) noexcept; void SelectRangeOfEntities(EntityID beginEID, EntityID EndEID); void SelectAllEntities(); void CopySelectedEntities(); void PasteEntities(EntityID parentEID = MAX_EID); void DeleteSelectedEntities(); + std::vector CleanUpEIDList(std::vector const& entities); bool skipFrame = false; std::string filter; bool isAnyNodeSelected = false; diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp index 79891b82..8619e204 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp @@ -454,6 +454,11 @@ namespace SHADE }, [component](AssetID const& id) { + if(SHAssetManager::GetType(id) != AssetType::MESH) + { + SHLOG_WARNING("Attempted to assign non mesh asset to Renderable Mesh property!") + return; + } component->SetMesh(SHResourceManager::LoadOrGet(id)); SHResourceManager::FinaliseChanges(); }, SHDragDrop::DRAG_RESOURCE); @@ -467,6 +472,11 @@ namespace SHADE }, [component](AssetID const& id) { + if (SHAssetManager::GetType(id) != AssetType::MATERIAL) + { + SHLOG_WARNING("Attempted to assign non material asset to Renderable Mesh property!") + return; + } auto gfxSystem = SHSystemManager::GetSystem(); component->SetMaterial(gfxSystem->AddOrGetBaseMaterialInstance(SHResourceManager::LoadOrGet(id))); }, SHDragDrop::DRAG_RESOURCE); From 37e845336b6867007db261448061bf9ce7900ee6 Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Wed, 16 Nov 2022 17:46:48 +0800 Subject: [PATCH 03/14] Fix line draws for asset browser --- .../Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp b/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp index 889c24cc..66b3c962 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp @@ -109,7 +109,7 @@ namespace SHADE ImVec2 vertLineEnd = vertLineStart; for (auto const& subFolder : subFolders) { - const float horizontalLineSize = 8.0f; + const float horizontalLineSize = (subFolder->subFolders.empty() && subFolder->files.empty()) ? 25.0f : 8.0f; const ImRect childRect = RecursivelyDrawTree(subFolder); const float midPoint = (childRect.Min.y + childRect.Max.y) * 0.5f; drawList->AddLine(ImVec2(vertLineStart.x, midPoint), ImVec2(vertLineStart.x + horizontalLineSize, midPoint), treeLineColor, 1); @@ -117,7 +117,7 @@ namespace SHADE } for (auto& file : files) { - const float horizontalLineSize = 25.0f; + const float horizontalLineSize = (file.assetMeta && !file.assetMeta->subAssets.empty()) ? 8.0f : 25.0f; const ImRect childRect = DrawFile(file); const float midPoint = (childRect.Min.y + childRect.Max.y) * 0.5f; drawList->AddLine(ImVec2(vertLineStart.x, midPoint), ImVec2(vertLineStart.x + horizontalLineSize, midPoint), treeLineColor, 1); @@ -182,7 +182,10 @@ namespace SHADE return nodeRect; } if(file.assetMeta) - DrawAsset(file.assetMeta, file.ext); + { + const ImRect childRect = DrawAsset(file.assetMeta, file.ext); + return childRect; + } } ImRect SHAssetBrowser::DrawAsset(SHAsset const* const asset, FileExt const& ext /*= ""*/) noexcept From c80a819b6e1dbe387c16c6273cff9113b4bf5060 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Thu, 17 Nov 2022 04:55:46 +0800 Subject: [PATCH 04/14] Guard Find entity by name function --- SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.cpp b/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.cpp index 6ce4f277..19eb5168 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.cpp +++ b/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.cpp @@ -218,7 +218,7 @@ namespace SHADE EntityID result = MAX_EID; for (auto& entity : entityVec) { - if (entity->name == name) + if (entity && entity->name == name) result = entity->GetEID(); } return result; From 7d71390d435949e70ceea307b459e93257d4ce22 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Thu, 17 Nov 2022 05:29:11 +0800 Subject: [PATCH 05/14] separated ortho and proj matrix in camera comp --- SHADE_Engine/src/Camera/SHCameraComponent.cpp | 17 +++++- SHADE_Engine/src/Camera/SHCameraComponent.h | 6 +- SHADE_Engine/src/Camera/SHCameraDirector.cpp | 60 +++++++++++++------ SHADE_Engine/src/Camera/SHCameraDirector.h | 16 ++--- SHADE_Engine/src/Camera/SHCameraSystem.cpp | 48 +++++++-------- 5 files changed, 93 insertions(+), 54 deletions(-) diff --git a/SHADE_Engine/src/Camera/SHCameraComponent.cpp b/SHADE_Engine/src/Camera/SHCameraComponent.cpp index ac451df5..7fc71db1 100644 --- a/SHADE_Engine/src/Camera/SHCameraComponent.cpp +++ b/SHADE_Engine/src/Camera/SHCameraComponent.cpp @@ -12,7 +12,7 @@ namespace SHADE :yaw(0.0f), pitch(0.0f), roll(0.0f) , width(1920.0f), height(1080.0f), zNear(0.01f), zFar(10000.0f), fov(90.0f), movementSpeed(1.0f), turnSpeed(0.5f) , perspProj(true), dirtyView(true), dirtyProj(true) - , viewMatrix(), projMatrix() + , viewMatrix(), perspProjMatrix(), orthoProjMatrix() , position(), offset() { ComponentFamily::GetID(); @@ -213,9 +213,22 @@ namespace SHADE const SHMatrix& SHCameraComponent::GetProjMatrix() const noexcept { - return projMatrix; + if (perspProj) + return perspProjMatrix; + else + return orthoProjMatrix; } + const SHMatrix& SHCameraComponent::GetOrthoMatrix() const noexcept + { + return orthoProjMatrix; + } + + const SHMatrix& SHCameraComponent::GetPerspectiveMatrix() const noexcept + { + return orthoProjMatrix; + } + //void SHCameraComponent::SetMainCamera(size_t directorCameraIndex) noexcept //{ // auto system = SHSystemManager::GetSystem(); diff --git a/SHADE_Engine/src/Camera/SHCameraComponent.h b/SHADE_Engine/src/Camera/SHCameraComponent.h index b778b8fa..b0999fe9 100644 --- a/SHADE_Engine/src/Camera/SHCameraComponent.h +++ b/SHADE_Engine/src/Camera/SHCameraComponent.h @@ -29,12 +29,14 @@ namespace SHADE SHMatrix viewMatrix; - SHMatrix projMatrix; + SHMatrix perspProjMatrix; + SHMatrix orthoProjMatrix; SHVec3 position; bool perspProj; SHVec3 offset; + public: @@ -78,6 +80,8 @@ namespace SHADE const SHMatrix& GetViewMatrix() const noexcept; const SHMatrix& GetProjMatrix() const noexcept; + const SHMatrix& GetOrthoMatrix() const noexcept; + const SHMatrix& GetPerspectiveMatrix() const noexcept; //void SetMainCamera(size_t cameraDirectorIndex = 0) noexcept; diff --git a/SHADE_Engine/src/Camera/SHCameraDirector.cpp b/SHADE_Engine/src/Camera/SHCameraDirector.cpp index 9e9ddb5c..ec539fa1 100644 --- a/SHADE_Engine/src/Camera/SHCameraDirector.cpp +++ b/SHADE_Engine/src/Camera/SHCameraDirector.cpp @@ -15,36 +15,42 @@ namespace SHADE } - SHMatrix SHCameraDirector::GetViewMatrix() const noexcept + SHMatrix const& SHCameraDirector::GetViewMatrix() const noexcept { return viewMatrix; } - SHMatrix SHCameraDirector::GetProjMatrix() const noexcept + SHMatrix const& SHCameraDirector::GetProjMatrix() const noexcept { return projMatrix; } - SHMatrix SHCameraDirector::GetVPMatrix() const noexcept + SHMatrix const& SHCameraDirector::GetVPMatrix() const noexcept { return projMatrix * viewMatrix; } + SHCameraComponent* SHCameraDirector::GetMainCameraComponent() noexcept + { + if (mainCameraEID == MAX_EID) + { + auto& dense = SHComponentManager::GetDense(); + if (dense.size() == 0) + { + return nullptr; + } + mainCameraEID = dense[0].GetEID(); + } + SHCameraComponent* camComponent = SHComponentManager::GetComponent_s(mainCameraEID); + if (!camComponent) + { + SHLOG_WARNING("Camera Director warning: Entity does not have a camera"); + } + } + + void SHCameraDirector::UpdateMatrix() noexcept { - if (mainCameraEID == MAX_EID) - { - auto& dense = SHComponentManager::GetDense(); - if (dense.size() == 0) - { - return; - } - mainCameraEID = dense[0].GetEID(); - } - SHCameraComponent* camComponent = SHComponentManager::GetComponent_s(mainCameraEID); - if (!camComponent) - { - SHLOG_WARNING("Camera Director warning: Entity does not have a camera"); - } - else + SHCameraComponent* camComponent = GetMainCameraComponent(); + if(camComponent) { viewMatrix = camComponent->GetViewMatrix(); projMatrix = camComponent->GetProjMatrix(); @@ -62,6 +68,24 @@ namespace SHADE mainCameraEID = camera.GetEID(); } + SHMatrix const& SHCameraDirector::GetOrthoMatrix() noexcept + { + + SHCameraComponent* camComponent = GetMainCameraComponent(); + if (camComponent) + return camComponent->GetOrthoMatrix(); + else + return SHMatrix::Identity; + } + + SHMatrix const& SHCameraDirector::GetPerspectiveMatrix() noexcept + { + SHCameraComponent* camComponent = GetMainCameraComponent(); + if (camComponent) + return camComponent->GetPerspectiveMatrix(); + else + return SHMatrix::Identity; + } } diff --git a/SHADE_Engine/src/Camera/SHCameraDirector.h b/SHADE_Engine/src/Camera/SHCameraDirector.h index 6d5404c5..8538a824 100644 --- a/SHADE_Engine/src/Camera/SHCameraDirector.h +++ b/SHADE_Engine/src/Camera/SHCameraDirector.h @@ -23,20 +23,20 @@ namespace SHADE EntityID transitionCameraEID; - SHMatrix GetViewMatrix() const noexcept; - SHMatrix GetProjMatrix() const noexcept; - SHMatrix GetVPMatrix() const noexcept; + SHMatrix const& GetViewMatrix() const noexcept; + SHMatrix const& GetProjMatrix() const noexcept; + SHMatrix const& GetVPMatrix() const noexcept; void UpdateMatrix() noexcept; void SetMainCamera(SHCameraComponent& cam) noexcept; - + SHMatrix const& GetOrthoMatrix() noexcept; + SHMatrix const& GetPerspectiveMatrix() noexcept; private: + SHMatrix viewMatrix; + SHMatrix projMatrix; + SHCameraComponent* GetMainCameraComponent() noexcept; - protected: - SHMatrix viewMatrix; - SHMatrix projMatrix; - }; typedef Handle DirectorHandle; diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.cpp b/SHADE_Engine/src/Camera/SHCameraSystem.cpp index 5ae6d35e..78dabc37 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.cpp +++ b/SHADE_Engine/src/Camera/SHCameraSystem.cpp @@ -214,24 +214,22 @@ namespace SHADE } if (camera.dirtyProj == true) { - if (camera.perspProj == true) - { + //Perspective projection matrix. const float ASPECT_RATIO = (camera.GetAspectRatio()); const float TAN_HALF_FOV = tan(SHMath::DegreesToRadians(camera.fov) * 0.5f); - camera.projMatrix = SHMatrix::Identity; - camera.projMatrix(0, 0) = 1.0f / (ASPECT_RATIO * TAN_HALF_FOV); - camera.projMatrix(1, 1) = 1.0f / TAN_HALF_FOV; - camera.projMatrix(2, 2) = camera.zFar / (camera.zFar - camera.zNear); - camera.projMatrix(3, 3) = 0.0f; + camera.perspProjMatrix = SHMatrix::Identity; + camera.perspProjMatrix(0, 0) = 1.0f / (ASPECT_RATIO * TAN_HALF_FOV); + camera.perspProjMatrix(1, 1) = 1.0f / TAN_HALF_FOV; + camera.perspProjMatrix(2, 2) = camera.zFar / (camera.zFar - camera.zNear); + camera.perspProjMatrix(3, 3) = 0.0f; + + camera.perspProjMatrix(3, 2) = 1.0f; + camera.perspProjMatrix(2, 3) = -(camera.zFar * camera.zNear) / (camera.zFar - camera.zNear); + + + + //Orthographic projection matrix - camera.projMatrix(3, 2) = 1.0f; - camera.projMatrix(2, 3) = -(camera.zFar * camera.zNear) / (camera.zFar - camera.zNear); - - - camera.dirtyProj = false; - } - else - { const float right = camera.GetWidth() * 0.5f; const float left = -right; const float top = camera.GetHeight() * 0.5f; @@ -239,20 +237,20 @@ namespace SHADE const float n = camera.GetNear(); const float f = camera.GetFar(); - camera.projMatrix = SHMatrix::Identity; - camera.projMatrix(0, 0) = 2.0f / (right - left); - camera.projMatrix(1, 1) = 2.0f / (btm - top); - camera.projMatrix(2, 2) = 1.0f / (f-n); - camera.projMatrix(0, 3) = -(right + left) / (right - left); - camera.projMatrix(1, 3) = -(btm + top) / (btm - top); - camera.projMatrix(2, 3) = -n / (f-n); - camera.projMatrix(3, 3) = 1.0f; + camera.orthoProjMatrix = SHMatrix::Identity; + camera.orthoProjMatrix(0, 0) = 2.0f / (right - left); + camera.orthoProjMatrix(1, 1) = 2.0f / (btm - top); + camera.orthoProjMatrix(2, 2) = 1.0f / (f-n); + camera.orthoProjMatrix(0, 3) = -(right + left) / (right - left); + camera.orthoProjMatrix(1, 3) = -(btm + top) / (btm - top); + camera.orthoProjMatrix(2, 3) = -n / (f-n); + camera.orthoProjMatrix(3, 3) = 1.0f; - camera.projMatrix = SHMatrix::OrthographicRH(camera.GetWidth(), camera.GetHeight(), camera.GetNear(), camera.GetFar()); + camera.orthoProjMatrix = SHMatrix::OrthographicRH(camera.GetWidth(), camera.GetHeight(), camera.GetNear(), camera.GetFar()); //camera.projMatrix.Transpose(); camera.dirtyProj = false; - } + } } From 0bb97413ef4f6e3648ce7c0d726bebc9d7d56973 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Thu, 17 Nov 2022 05:39:55 +0800 Subject: [PATCH 06/14] Added Canvas and UI Components --- SHADE_Engine/src/Camera/SHCameraSystem.h | 2 - SHADE_Engine/src/UI/SHCanvasComponent.cpp | 55 +++++++++++++++++++++++ SHADE_Engine/src/UI/SHCanvasComponent.h | 12 ++++- SHADE_Engine/src/UI/SHUIComponent.cpp | 20 +++++++++ SHADE_Engine/src/UI/SHUIComponent.h | 32 +++++++++++++ 5 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 SHADE_Engine/src/UI/SHCanvasComponent.cpp create mode 100644 SHADE_Engine/src/UI/SHUIComponent.cpp create mode 100644 SHADE_Engine/src/UI/SHUIComponent.h diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.h b/SHADE_Engine/src/Camera/SHCameraSystem.h index fc6e9166..9c8157f4 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.h +++ b/SHADE_Engine/src/Camera/SHCameraSystem.h @@ -37,8 +37,6 @@ namespace SHADE void Exit (void); - friend class EditorCameraUpdate; - class SH_API CameraSystemUpdate final: public SHSystemRoutine { public: diff --git a/SHADE_Engine/src/UI/SHCanvasComponent.cpp b/SHADE_Engine/src/UI/SHCanvasComponent.cpp new file mode 100644 index 00000000..4c9ea360 --- /dev/null +++ b/SHADE_Engine/src/UI/SHCanvasComponent.cpp @@ -0,0 +1,55 @@ +#include "SHpch.h" + +#include "SHCanvasComponent.h" + +namespace SHADE +{ + + SHCanvasComponent::SHCanvasComponent() + :width(1),height(1), dirtyMatrix(false) + { + + } + + void SHCanvasComponent::SetCanvasSize(CanvasSizeType width, CanvasSizeType height) noexcept + { + this->width = width; + this->height = height; + } + + void SHCanvasComponent::SetCanvasWidth(CanvasSizeType val) noexcept + { + width = val; + } + + void SHCanvasComponent::SetCanvasHeight(CanvasSizeType val) noexcept + { + height = val; + } + + + SHCanvasComponent::CanvasSizeType const SHCanvasComponent::GetCanvasWidth() const noexcept + { + return width; + } + + SHCanvasComponent::CanvasSizeType const SHCanvasComponent::GetCanvasHeight() const noexcept + { + return height; + } + +} + + +RTTR_REGISTRATION +{ + using namespace SHADE; + using namespace rttr; + + registration::class_("Canvas Component") + .property("Canvas Width", &SHCanvasComponent::GetCanvasWidth, &SHCanvasComponent::SetCanvasWidth) + .property("Canvas Height", &SHCanvasComponent::GetCanvasHeight, &SHCanvasComponent::SetCanvasHeight) + ; + + +} diff --git a/SHADE_Engine/src/UI/SHCanvasComponent.h b/SHADE_Engine/src/UI/SHCanvasComponent.h index 2e9a54f1..63d8c2c4 100644 --- a/SHADE_Engine/src/UI/SHCanvasComponent.h +++ b/SHADE_Engine/src/UI/SHCanvasComponent.h @@ -1,7 +1,10 @@ #pragma once +#include + #include "SH_API.h" #include "ECS_Base/Components/SHComponent.h" +#include "Math/SHMatrix.h" namespace SHADE @@ -13,6 +16,9 @@ namespace SHADE public: + friend class SHUISystem; + + SHCanvasComponent(); ~SHCanvasComponent() = default; @@ -26,9 +32,11 @@ namespace SHADE private: CanvasSizeType width; CanvasSizeType height; - - + bool dirtyMatrix; + SHMatrix modelToCanvasMatrix; + + RTTR_ENABLE() }; diff --git a/SHADE_Engine/src/UI/SHUIComponent.cpp b/SHADE_Engine/src/UI/SHUIComponent.cpp new file mode 100644 index 00000000..66522947 --- /dev/null +++ b/SHADE_Engine/src/UI/SHUIComponent.cpp @@ -0,0 +1,20 @@ +#include "SHpch.h" +#include "SHUIComponent.h" + + + +namespace SHADE +{ + + SHUIComponent::SHUIComponent() + { + + } + + + SHMatrix const& SHUIComponent::GetMatrix()const noexcept + { + return LocalToCanvasMatrix; + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHUIComponent.h b/SHADE_Engine/src/UI/SHUIComponent.h new file mode 100644 index 00000000..70a667e8 --- /dev/null +++ b/SHADE_Engine/src/UI/SHUIComponent.h @@ -0,0 +1,32 @@ +#pragma once + +#include + +#include "SH_API.h" +#include "ECS_Base/Components/SHComponent.h" +#include "Math/SHMatrix.h" + + +namespace SHADE +{ + class SH_API SHUIComponent final: public SHComponent + { + public: + friend class SHUISystem; + + SHUIComponent(); + ~SHUIComponent() = default; + + SHMatrix const& GetMatrix() const noexcept; + + + private: + SHMatrix LocalToCanvasMatrix; + + + + RTTR_ENABLE() + } + + +} \ No newline at end of file From ad30f150d13e7f54dd1677c73d5ab0024ef29bed Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Thu, 17 Nov 2022 07:12:38 +0800 Subject: [PATCH 07/14] UI component and UI System prep --- .../src/Application/SBApplication.cpp | 4 + SHADE_Engine/src/UI/SHCanvasComponent.cpp | 11 +- SHADE_Engine/src/UI/SHCanvasComponent.h | 7 +- SHADE_Engine/src/UI/SHUIComponent.cpp | 14 ++- SHADE_Engine/src/UI/SHUIComponent.h | 6 +- SHADE_Engine/src/UI/SHUISystem.cpp | 109 ++++++++++++++++++ SHADE_Engine/src/UI/SHUISystem.h | 55 +++++++++ 7 files changed, 196 insertions(+), 10 deletions(-) create mode 100644 SHADE_Engine/src/UI/SHUISystem.cpp create mode 100644 SHADE_Engine/src/UI/SHUISystem.h diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index bf5b8d49..57b4c189 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -33,6 +33,7 @@ #include "Physics/System/SHPhysicsSystem.h" #include "Physics/System/SHPhysicsDebugDrawSystem.h" #include "Scripting/SHScriptEngine.h" +#include "UI/SHUISystem.h" // Components #include "Graphics/MiddleEnd/Interface/SHRenderable.h" @@ -75,6 +76,7 @@ namespace Sandbox SHSystemManager::CreateSystem(); SHSystemManager::CreateSystem(); + SHSystemManager::CreateSystem(); SHSystemManager::CreateSystem(); @@ -117,6 +119,8 @@ namespace Sandbox SHSystemManager::RegisterRoutine(); //SHSystemManager::RegisterRoutine(); + SHSystemManager::RegisterRoutine(); + SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); #ifdef SHEDITOR diff --git a/SHADE_Engine/src/UI/SHCanvasComponent.cpp b/SHADE_Engine/src/UI/SHCanvasComponent.cpp index 4c9ea360..1ffc7a19 100644 --- a/SHADE_Engine/src/UI/SHCanvasComponent.cpp +++ b/SHADE_Engine/src/UI/SHCanvasComponent.cpp @@ -6,7 +6,7 @@ namespace SHADE { SHCanvasComponent::SHCanvasComponent() - :width(1),height(1), dirtyMatrix(false) + :width(1),height(1), dirtyMatrix(false), canvasMatrix() { } @@ -28,16 +28,21 @@ namespace SHADE } - SHCanvasComponent::CanvasSizeType const SHCanvasComponent::GetCanvasWidth() const noexcept + SHCanvasComponent::CanvasSizeType SHCanvasComponent::GetCanvasWidth() const noexcept { return width; } - SHCanvasComponent::CanvasSizeType const SHCanvasComponent::GetCanvasHeight() const noexcept + SHCanvasComponent::CanvasSizeType SHCanvasComponent::GetCanvasHeight() const noexcept { return height; } + SHMatrix const& SHCanvasComponent::GetMatrix() const noexcept + { + return canvasMatrix; + } + } diff --git a/SHADE_Engine/src/UI/SHCanvasComponent.h b/SHADE_Engine/src/UI/SHCanvasComponent.h index 63d8c2c4..145b3cb3 100644 --- a/SHADE_Engine/src/UI/SHCanvasComponent.h +++ b/SHADE_Engine/src/UI/SHCanvasComponent.h @@ -26,14 +26,15 @@ namespace SHADE void SetCanvasWidth(CanvasSizeType width) noexcept; void SetCanvasHeight(CanvasSizeType height) noexcept; - CanvasSizeType const GetCanvasWidth() const noexcept; - CanvasSizeType const GetCanvasHeight() const noexcept; + CanvasSizeType GetCanvasWidth() const noexcept; + CanvasSizeType GetCanvasHeight() const noexcept; + SHMatrix const& GetMatrix() const noexcept; private: CanvasSizeType width; CanvasSizeType height; bool dirtyMatrix; - SHMatrix modelToCanvasMatrix; + SHMatrix canvasMatrix; RTTR_ENABLE() diff --git a/SHADE_Engine/src/UI/SHUIComponent.cpp b/SHADE_Engine/src/UI/SHUIComponent.cpp index 66522947..23c86ec3 100644 --- a/SHADE_Engine/src/UI/SHUIComponent.cpp +++ b/SHADE_Engine/src/UI/SHUIComponent.cpp @@ -14,7 +14,19 @@ namespace SHADE SHMatrix const& SHUIComponent::GetMatrix()const noexcept { - return LocalToCanvasMatrix; + return localToCanvasMatrix; } +} + + +RTTR_REGISTRATION +{ + using namespace SHADE; + using namespace rttr; + + registration::class_("Canvas Component") + ; + + } \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHUIComponent.h b/SHADE_Engine/src/UI/SHUIComponent.h index 70a667e8..190a40e1 100644 --- a/SHADE_Engine/src/UI/SHUIComponent.h +++ b/SHADE_Engine/src/UI/SHUIComponent.h @@ -21,12 +21,12 @@ namespace SHADE private: - SHMatrix LocalToCanvasMatrix; - + SHMatrix localToCanvasMatrix; + EntityID canvasID; RTTR_ENABLE() - } + }; } \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp new file mode 100644 index 00000000..3f7559b5 --- /dev/null +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -0,0 +1,109 @@ +#include "SHpch.h" +#include "SHUISystem.h" +#include "ECS_Base/Managers/SHComponentManager.h" +#include "ECS_Base/Managers/SHSystemManager.h" +#include "Math/Transform/SHTransformComponent.h" + +namespace SHADE +{ + + void SHUISystem::Init() + { + SystemFamily::GetID(); + SHComponentManager::CreateComponentSparseSet(); + SHComponentManager::CreateComponentSparseSet(); + } + + void SHUISystem::Exit() + { + + } + + + void SHUISystem::AddUIComponentRoutine::Execute(double dt) noexcept + { + auto& dense = SHComponentManager::GetDense(); + auto& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); + + //Go through all the canvases and make sure all the children has a UIComponent. + for (auto& canvas : dense) + { + auto& children = sceneGraph.GetChildren(canvas.GetEID()); + for (auto& child : children) + { + RecurssiveUIComponentCheck(child); + } + } + + //Go through all the UI Component and make sure the parent has a UI or Canvas Component + std::vector entitiesToRemove; + auto& UIDense = SHComponentManager::GetDense(); + for (auto& ui : UIDense) + { + SHSceneNode* parentNode = sceneGraph.GetParent(ui.GetEID()); + if (parentNode == nullptr || !(SHComponentManager::HasComponent(parentNode->GetEntityID()) || SHComponentManager::HasComponent(parentNode->GetEntityID()))) + entitiesToRemove.push_back(ui.GetEID()); + } + + for (auto& id : entitiesToRemove) + { + SHComponentManager::RemoveComponent(id); + } + + + } + + void SHUISystem::AddUIComponentRoutine::RecurssiveUIComponentCheck(SHSceneNode* node) noexcept + { + if (node == nullptr) + return; + + EntityID eid = node->GetEntityID(); + + if(SHComponentManager::HasComponent(eid) == false) + SHComponentManager::AddComponent(eid); + + auto& children = SHSceneManager::GetCurrentSceneGraph().GetChildren(eid); + for (auto& child : children) + { + RecurssiveUIComponentCheck(child); + } + + } + + + void SHUISystem::UpdateUIMatrixRoutine::Execute(double dt) noexcept + { + SHUISystem* system = (SHUISystem* )GetSystem(); + auto& dense = SHComponentManager::GetDense(); + for (auto& comp : dense) + { + system->UpdateUIComponent(comp); + } + + + } + + void SHUISystem::UpdateUIComponent(SHUIComponent& comp) noexcept + { + auto canvasComp = SHComponentManager::GetComponent_s(comp.canvasID); + if (SHComponentManager::HasComponent(comp.GetEID())) + { + auto transform = SHComponentManager::GetComponent(comp.GetEID()); + if (canvasComp != nullptr) + comp.localToCanvasMatrix = canvasComp->GetMatrix() * transform->GetTRS(); + else + comp.localToCanvasMatrix = transform->GetTRS(); + } + else //If for some reason UI Components entities does not have a transform. + { + if (canvasComp != nullptr) + comp.localToCanvasMatrix = canvasComp->GetMatrix(); + else + comp.localToCanvasMatrix = SHMatrix::Identity; + } + } + + + +}//end namespace diff --git a/SHADE_Engine/src/UI/SHUISystem.h b/SHADE_Engine/src/UI/SHUISystem.h new file mode 100644 index 00000000..99891348 --- /dev/null +++ b/SHADE_Engine/src/UI/SHUISystem.h @@ -0,0 +1,55 @@ +#pragma once + +#include "SH_API.h" +#include "ECS_Base/System/SHSystem.h" +#include "ECS_Base/System/SHSystemRoutine.h" +#include "SHUIComponent.h" +#include "SHCanvasComponent.h" +#include "Scene/SHSceneGraph.h" +#include "Scene/SHSceneManager.h" + +namespace SHADE +{ + class SH_API SHUISystem final: public SHSystem + { + public: + + SHUISystem() = default; + ~SHUISystem() = default; + + class SH_API AddUIComponentRoutine final: public SHSystemRoutine + { + public: + AddUIComponentRoutine() :SHSystemRoutine("Add UI Component Routine", true) {}; + virtual void Execute(double dt) noexcept override final; + + private: + + void RecurssiveUIComponentCheck(SHSceneNode* node) noexcept; + }; + friend class AddUIComponentRoutine; + + class SH_API UpdateUIMatrixRoutine final: public SHSystemRoutine + { + public: + UpdateUIMatrixRoutine() : SHSystemRoutine("Update UI Matrix Routine", true) {}; + virtual void Execute(double dt) noexcept override final; + + }; + friend class UpdateUIMatrixRoutine; + + + void Init(); + void Exit(); + + + private: + void UpdateUIComponent(SHUIComponent& comp) noexcept; + + + + + }; + + +} From e170a5fca8b5aab74944e102a11ea01b2f6581b3 Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Thu, 17 Nov 2022 13:33:22 +0800 Subject: [PATCH 08/14] Added controls to editor viewport menu bar to adjust movement speed and turn speed for editor camera --- .../ViewportWindow/SHEditorViewport.cpp | 8 ++++++++ SHADE_Engine/src/Editor/SHEditorWidgets.hpp | 15 ++++++++------- .../src/Serialization/SHSerialization.cpp | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/SHADE_Engine/src/Editor/EditorWindow/ViewportWindow/SHEditorViewport.cpp b/SHADE_Engine/src/Editor/EditorWindow/ViewportWindow/SHEditorViewport.cpp index 564731d6..93f4a615 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/ViewportWindow/SHEditorViewport.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/ViewportWindow/SHEditorViewport.cpp @@ -14,6 +14,7 @@ #include "Camera/SHCameraSystem.h" #include "FRC/SHFramerateController.h" +#include "../../SHEditorWidgets.hpp" constexpr std::string_view windowName = "\xef\x80\x95 Viewport"; @@ -193,6 +194,13 @@ namespace SHADE if (isScale) ImGui::PopStyleColor(); ImGui::EndDisabled(); + + auto camSystem = SHSystemManager::GetSystem(); + auto editorCamera = camSystem->GetEditorCamera(); + //ImGui::SetNextItemWidth(10.0f); + SHEditorWidgets::SliderFloat("CamSpeed", 0.0f, 5.0f, [editorCamera] {return editorCamera->movementSpeed; }, [editorCamera](float const& value) {editorCamera->movementSpeed = value; }); + SHEditorWidgets::DragVec3("TurnSpeed", { "X", "Y", "Z" }, [editorCamera] {return editorCamera->turnSpeed; }, [editorCamera](SHVec3 const& value) {editorCamera->turnSpeed = value; }); + ImGui::EndMenuBar(); } } diff --git a/SHADE_Engine/src/Editor/SHEditorWidgets.hpp b/SHADE_Engine/src/Editor/SHEditorWidgets.hpp index bfde7525..2681e916 100644 --- a/SHADE_Engine/src/Editor/SHEditorWidgets.hpp +++ b/SHADE_Engine/src/Editor/SHEditorWidgets.hpp @@ -422,7 +422,7 @@ namespace SHADE ImGui::BeginGroup(); ImGui::PushID(label.data()); TextLabel(label); - bool changed = ImGui::InputText("##", &text, ImGuiInputTextFlags_ReadOnly, nullptr, nullptr); + bool changed = ImGui::InputText("##inputText", &text, ImGuiInputTextFlags_ReadOnly, nullptr, nullptr); if(SHDragDrop::BeginTarget()) { if(T* payload = SHDragDrop::AcceptPayload(dragDropTag)) @@ -454,7 +454,7 @@ namespace SHADE ImGui::BeginGroup(); ImGui::PushID(label.data()); TextLabel(label); - const bool hasChange = ImGui::DragScalar("##", data_type, &value, speed, &p_min, &p_max, displayFormat, flags); + const bool hasChange = ImGui::DragScalar("##dragScalar", data_type, &value, speed, &p_min, &p_max, displayFormat, flags); static bool startRecording = false; if (hasChange) { @@ -487,7 +487,7 @@ namespace SHADE ImGui::BeginGroup(); ImGui::PushID(label.data()); TextLabel(label); - const bool hasChange = ImGui::DragFloat("##", &value, speed, p_min, p_max, displayFormat, flags); + const bool hasChange = ImGui::DragFloat("##dragFloat", &value, speed, p_min, p_max, displayFormat, flags); static bool startRecording = false; if (hasChange) { @@ -520,7 +520,7 @@ namespace SHADE ImGui::BeginGroup(); ImGui::PushID(label.data()); TextLabel(label); - const bool hasChange = ImGui::DragInt("##", &value, speed, p_min, p_max, displayFormat, flags); + const bool hasChange = ImGui::DragInt("##dragInt", &value, speed, p_min, p_max, displayFormat, flags); static bool startRecording = false; if (hasChange) { @@ -553,7 +553,7 @@ namespace SHADE ImGui::BeginGroup(); ImGui::PushID(label.data()); TextLabel(label); - bool const hasChange = ImGui::SliderScalar("##", data_type, &value, &min, &max, displayFormat, flags); + bool const hasChange = ImGui::SliderScalar("##sliderScalar", data_type, &value, &min, &max, displayFormat, flags); static bool startRecording = false; if (hasChange) { @@ -587,7 +587,8 @@ namespace SHADE ImGui::BeginGroup(); ImGui::PushID(label.data()); TextLabel(label); - bool const hasChange = ImGui::SliderFloat("##", &value, min, max, displayFormat, flags); + ImGui::SetNextItemWidth(ImGui::CalcTextSize(displayFormat).x + ImGui::GetStyle().ItemInnerSpacing.x * 2.0f); + bool const hasChange = ImGui::SliderFloat("##sliderFloat", &value, min, max, displayFormat, flags); static bool startRecording = false; if (hasChange) { @@ -621,7 +622,7 @@ namespace SHADE ImGui::BeginGroup(); ImGui::PushID(label.data()); TextLabel(label); - bool const hasChange = ImGui::SliderInt("##", &value, min, max, displayFormat, flags); + bool const hasChange = ImGui::SliderInt("##sliderInt", &value, min, max, displayFormat, flags); static bool startRecording = false; if (hasChange) { diff --git a/SHADE_Engine/src/Serialization/SHSerialization.cpp b/SHADE_Engine/src/Serialization/SHSerialization.cpp index 6933fbb5..3ed96d7d 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.cpp +++ b/SHADE_Engine/src/Serialization/SHSerialization.cpp @@ -12,8 +12,8 @@ #include "Assets/Asset Types/SHSceneAsset.h" #include "Camera/SHCameraComponent.h" -#include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Math/Transform/SHTransformComponent.h" +#include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Physics/Interface/SHRigidBodyComponent.h" #include "ECS_Base/Managers/SHSystemManager.h" #include "Graphics/MiddleEnd/Lights/SHLightComponent.h" From 17662abef1fcbffc852ca5d538c9307a5ef15341 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 17 Nov 2022 15:42:40 +0800 Subject: [PATCH 09/14] Fixed bug where certain null objects in deserialization was not handled --- .../Serialisation/SerialisationUtilities.h++ | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ index dde6705a..2c943452 100644 --- a/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ +++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ @@ -142,6 +142,31 @@ namespace SHADE bool SerialisationUtilities::fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node) { System::Object^ valueObj = fieldInfo->GetValue(object); + if (valueObj == nullptr) + { + if constexpr (std::is_same_v) + { + if (fieldInfo->FieldType->IsSubclassOf(System::Enum::typeid)) + { + valueObj = 0; + } + } + else + { + if (fieldInfo->FieldType == FieldType::typeid) + { + if constexpr (std::is_same_v) + { + valueObj = ""; + } + else + { + valueObj = FieldType(); + } + } + } + } + if (varAssignYamlInternal(valueObj, node)) { fieldInfo->SetValue(object, valueObj); From 6efbea54e169dac2753255e6ac5ee1959b76b237 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 17 Nov 2022 15:43:35 +0800 Subject: [PATCH 10/14] Fixed issue where scripts of Entities destroyed during scene unloading are not properly disposed --- SHADE_Engine/src/Scripting/SHScriptEngine.cpp | 2 +- SHADE_Managed/src/Scripts/ScriptStore.cxx | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index 0db11c7a..e0b87bea 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -307,7 +307,7 @@ namespace SHADE SHEventHandle SHScriptEngine::onEntityDestroyed(SHEventPtr eventPtr) { auto eventData = reinterpret_cast*>(eventPtr.get()); - csScriptsRemoveAll(eventData->data->eid); + csScriptsRemoveAllImmediately(eventData->data->eid, true); return eventData->handle; } diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index d57122ee..29ba6e52 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -402,8 +402,8 @@ namespace SHADE System::Collections::Generic::List^ scriptList = scripts[entity]; for each (Script ^ script in scriptList) { - // Call OnDestroy only if indicated and also in play mode - if (callOnDestroy) + // Call OnDestroy only if indicated and also if the game has run + if (callOnDestroy && Application::IsPlaying || Application::IsPaused) { script->OnDestroy(); } @@ -469,7 +469,7 @@ namespace SHADE script->OnDestroy(); } auto entity = script->Owner.GetEntity(); - auto scriptList = scripts[script->Owner.GetEntity()]; + auto scriptList = scripts[script->Owner.GetEntity()]; // Unable to find here scriptList->Remove(script); if (scriptList->Count <= 0) { From 042bff5c1ad1639e552c82775d5f8d0174096ce9 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 17 Nov 2022 15:49:06 +0800 Subject: [PATCH 11/14] Added additional exception handling for serialization and deserialization failures for scripts to prevent data loss --- .../Serialisation/SerialisationUtilities.cxx | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx b/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx index 147591a5..cfa94540 100644 --- a/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx +++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx @@ -45,12 +45,22 @@ namespace SHADE System::Collections::Generic::IEnumerable^ fields = ReflectionUtilities::GetInstanceFields(object); for each (FieldInfo^ field in fields) { - // Ignore private and non-SerialiseField - if (!ReflectionUtilities::FieldIsSerialisable(field)) - continue; + try + { + // Ignore private and non-SerialiseField + if (!ReflectionUtilities::FieldIsSerialisable(field)) + continue; - // Serialise - writeFieldIntoYaml(field, object, scriptNode); + // Serialise + writeFieldIntoYaml(field, object, scriptNode); + } + catch (System::Exception^ e) + { + Debug::LogError + ( + System::String::Format("[SerialisationUtilities] Failed to serialise field ({0}): {1}", field->Name, e->ToString()) + ); + } } scriptListNode.push_back(scriptNode); @@ -74,15 +84,25 @@ namespace SHADE System::Collections::Generic::IEnumerable^ fields = ReflectionUtilities::GetInstanceFields(object); for each (FieldInfo^ field in fields) { - // Ignore private and non-SerialiseField - if (!ReflectionUtilities::FieldIsSerialisable(field)) - continue; - - // Deserialise - const std::string FIELD_NAME = Convert::ToNative(field->Name); - if (yamlNode[FIELD_NAME]) + try { - writeYamlIntoField(field, object, yamlNode[FIELD_NAME]); + // Ignore private and non-SerialiseField + if (!ReflectionUtilities::FieldIsSerialisable(field)) + continue; + + // Deserialise + const std::string FIELD_NAME = Convert::ToNative(field->Name); + if (yamlNode[FIELD_NAME]) + { + writeYamlIntoField(field, object, yamlNode[FIELD_NAME]); + } + } + catch (System::Exception^ e) + { + Debug::LogError + ( + System::String::Format("[SerialisationUtilities] Failed to deserialise field ({0}): {1}", field->Name, e->ToString()) + ); } } } From e8bd33ec9e037dc744026dbc79e3d52cf2f102ee Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 17 Nov 2022 17:36:35 +0800 Subject: [PATCH 12/14] Scripts are now always built on engine launch --- SHADE_Engine/src/Scripting/SHScriptEngine.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index e0b87bea..84b50373 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -58,10 +58,9 @@ namespace SHADE loadFunctions(); // Generate script assembly if it hasn't been before - if (!fileExists(std::string(MANAGED_SCRIPT_LIB_NAME) + ".dll")) - { - BuildScriptAssembly(); - } +#ifndef _PUBLISH + BuildScriptAssembly(); +#endif // Initialise the CSharp Engine csEngineInit(); From d997cd4da661771a56f57f27a54f4ce96d8c9ffd Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Fri, 18 Nov 2022 09:38:31 +0800 Subject: [PATCH 13/14] Added canvas ID to UI Component --- SHADE_Engine/src/UI/SHUISystem.cpp | 10 +++++++--- SHADE_Engine/src/UI/SHUISystem.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 3f7559b5..d7dfe3c5 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -31,7 +31,7 @@ namespace SHADE auto& children = sceneGraph.GetChildren(canvas.GetEID()); for (auto& child : children) { - RecurssiveUIComponentCheck(child); + RecurssiveUIComponentCheck(child, canvas); } } @@ -53,7 +53,7 @@ namespace SHADE } - void SHUISystem::AddUIComponentRoutine::RecurssiveUIComponentCheck(SHSceneNode* node) noexcept + void SHUISystem::AddUIComponentRoutine::RecurssiveUIComponentCheck(SHSceneNode* node, SHCanvasComponent& canvas) noexcept { if (node == nullptr) return; @@ -62,11 +62,15 @@ namespace SHADE if(SHComponentManager::HasComponent(eid) == false) SHComponentManager::AddComponent(eid); + else + { + SHComponentManager::GetComponent(eid)->canvasID = canvas.GetEID(); + } auto& children = SHSceneManager::GetCurrentSceneGraph().GetChildren(eid); for (auto& child : children) { - RecurssiveUIComponentCheck(child); + RecurssiveUIComponentCheck(child, canvas); } } diff --git a/SHADE_Engine/src/UI/SHUISystem.h b/SHADE_Engine/src/UI/SHUISystem.h index 99891348..3da7efb3 100644 --- a/SHADE_Engine/src/UI/SHUISystem.h +++ b/SHADE_Engine/src/UI/SHUISystem.h @@ -25,7 +25,7 @@ namespace SHADE private: - void RecurssiveUIComponentCheck(SHSceneNode* node) noexcept; + void RecurssiveUIComponentCheck(SHSceneNode* node, SHCanvasComponent& canvas) noexcept; }; friend class AddUIComponentRoutine; From a181d7b8960cbb637af5f3336dd59bbfa7634c48 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Fri, 18 Nov 2022 16:37:33 +0800 Subject: [PATCH 14/14] Added Canvas and UI to editor --- .../EditorWindow/Inspector/SHEditorInspector.cpp | 11 +++++++++++ SHADE_Engine/src/UI/SHUIComponent.cpp | 13 ++++++++++++- SHADE_Engine/src/UI/SHUIComponent.h | 3 ++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp index f240e321..3a5a15b5 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp @@ -18,6 +18,8 @@ #include "Physics/Interface/SHColliderComponent.h" #include "Camera/SHCameraComponent.h" #include "Camera/SHCameraArmComponent.h" +#include "UI/SHUIComponent.h" +#include "UI/SHCanvasComponent.h" #include "SHEditorComponentView.h" namespace SHADE @@ -132,6 +134,14 @@ namespace SHADE { DrawComponent(cameraArmComponent); } + if (auto canvasComponent= SHComponentManager::GetComponent_s(eid)) + { + DrawComponent(canvasComponent); + } + if (auto uiComponent = SHComponentManager::GetComponent_s(eid)) + { + DrawComponent(uiComponent); + } ImGui::Separator(); // Render Scripts SHScriptEngine* scriptEngine = static_cast(SHSystemManager::GetSystem()); @@ -143,6 +153,7 @@ namespace SHADE DrawAddComponentButton(eid); DrawAddComponentButton(eid); DrawAddComponentButton(eid); + DrawAddComponentButton(eid); // Components that require Transforms diff --git a/SHADE_Engine/src/UI/SHUIComponent.cpp b/SHADE_Engine/src/UI/SHUIComponent.cpp index 23c86ec3..8131d081 100644 --- a/SHADE_Engine/src/UI/SHUIComponent.cpp +++ b/SHADE_Engine/src/UI/SHUIComponent.cpp @@ -17,6 +17,16 @@ namespace SHADE return localToCanvasMatrix; } + EntityID SHUIComponent::GetCanvasID() const noexcept + { + return canvasID; + } + + void SHUIComponent::SetCanvasID(EntityID id) noexcept + { + (void)id; + } + } @@ -25,7 +35,8 @@ RTTR_REGISTRATION using namespace SHADE; using namespace rttr; - registration::class_("Canvas Component") + registration::class_("UI Component") + .property("Canvas ID", &SHUIComponent::GetCanvasID, &SHUIComponent::SetCanvasID) ; diff --git a/SHADE_Engine/src/UI/SHUIComponent.h b/SHADE_Engine/src/UI/SHUIComponent.h index 190a40e1..5a9290cc 100644 --- a/SHADE_Engine/src/UI/SHUIComponent.h +++ b/SHADE_Engine/src/UI/SHUIComponent.h @@ -18,7 +18,8 @@ namespace SHADE ~SHUIComponent() = default; SHMatrix const& GetMatrix() const noexcept; - + EntityID GetCanvasID() const noexcept; + void SetCanvasID(EntityID id) noexcept; private: SHMatrix localToCanvasMatrix;