diff --git a/Assets/Scripts/RaccoonShowcase.cs b/Assets/Scripts/RaccoonShowcase.cs index 836b93d0..dc9d914d 100644 --- a/Assets/Scripts/RaccoonShowcase.cs +++ b/Assets/Scripts/RaccoonShowcase.cs @@ -1,5 +1,6 @@ using SHADE; using System; +using System.Collections.Generic; public class RaccoonShowcase : Script { @@ -17,6 +18,11 @@ public class RaccoonShowcase : Script private double rotation = 0.0; private Vector3 scale = Vector3.Zero; private double originalScale = 1.0f; + [Tooltip("Sample list of Vector3s.")] + public List vecList = new List(new Vector3[] { new Vector3(1, 2, 3), new Vector3(4, 5, 6) }); + [Range(-5, 5)] + public List intList = new List(new int[] { 2, 8, 2, 6, 8, 0, 1 }); + public List enumList = new List(new Light.Type[] { Light.Type.Point, Light.Type.Directional, Light.Type.Ambient }); public RaccoonShowcase(GameObject gameObj) : base(gameObj) {} protected override void awake() diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.cpp b/SHADE_Engine/src/Camera/SHCameraSystem.cpp index d5bd414d..8f886926 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.cpp +++ b/SHADE_Engine/src/Camera/SHCameraSystem.cpp @@ -7,13 +7,15 @@ #include "ECS_Base/Managers/SHComponentManager.h" #include "Math/Transform/SHTransformComponent.h" #include - +#include "Scene/SHSceneManager.h" namespace SHADE { void SHCameraSystem::UpdateEditorCamera(double dt) noexcept { + + auto& camera = editorCamera; SHVec3 view, right, UP; GetCameraAxis(camera, view, right, UP); @@ -62,62 +64,50 @@ namespace SHADE UpdateCameraComponent(editorCamera); + if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::LEFT_ALT)) + { + UpdateEditorArm(dt, true, SHVec3{ 0.0f }); + } + else + UpdateEditorArm(dt, false, SHVec3{ 0.0f }); + } - void SHCameraSystem::EditorCameraUpdate::Execute(double dt) noexcept - { - SHCameraSystem* system = static_cast(GetSystem()); - auto& camera = system->editorCamera; - SHVec3 view, right, UP; - system->GetCameraAxis(camera, view, right, UP); - if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::A)) + void SHCameraSystem::UpdateEditorArm(double dt,bool active ,SHVec3 const& targetPos) noexcept + { + if (active == false) { - //std::cout << "Camera movement: "< pitchClamp) + editorCameraArm.pitch = pitchClamp; + if (editorCameraArm.pitch < -pitchClamp) + editorCameraArm.pitch = -pitchClamp; + + editorCameraArm.armLength -= SHInputManager::GetMouseWheelVerticalDelta() * dt; + + if (editorCameraArm.armLength < 1.0f) + editorCameraArm.armLength = 1.0f; + + UpdatePivotArmComponent(editorCameraArm); + + editorCamera.offset = editorCameraArm.GetOffset(); + + CameraLookAt(editorCamera, targetPos); + + } - //std::cout << "Camera position: " << camera.position.x << " " << camera.position.y << std::endl; - system->UpdateCameraComponent(system->editorCamera); - - system->DecomposeViewMatrix(camera.viewMatrix, camera.pitch, camera.yaw, camera.roll, camera.position); - } void SHCameraSystem::Init(void) { @@ -164,6 +154,9 @@ namespace SHADE void SHCameraSystem::UpdateCameraComponent(SHCameraComponent& camera) noexcept { + if (camera.isActive == false) + return; + if (SHComponentManager::HasComponent(camera.GetEID()) == true && &camera != &editorCamera) { auto transform = SHComponentManager::GetComponent(camera.GetEID()); @@ -183,11 +176,17 @@ namespace SHADE if (SHComponentManager::HasComponent(camera.GetEID())) { auto arm = SHComponentManager::GetComponent(camera.GetEID()); - camera.offset = arm->GetOffset(); - if(arm->lookAtCameraOrigin) - CameraLookAt(camera, camera.position); + if (arm->isActive == true) + { + camera.offset = arm->GetOffset(); + if (arm->lookAtCameraOrigin) + CameraLookAt(camera, camera.position); + } + } + + SHVec3 view, right, UP; @@ -287,12 +286,14 @@ namespace SHADE for (auto& pivot : pivotDense) { - system->UpdatePivotArmComponent(pivot); + if(SHSceneManager::CheckNodeAndComponentsActive(pivot.GetEID())) + system->UpdatePivotArmComponent(pivot); } for (auto& cam : dense) { - system->UpdateCameraComponent(cam); + if (SHSceneManager::CheckNodeAndComponentsActive(cam.GetEID())) + system->UpdateCameraComponent(cam); } for (auto& handle : system->directorHandleList) { @@ -399,7 +400,7 @@ namespace SHADE SHVec3 up = { 0.0f,1.0f,0.0f }; - ////SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll)); + //SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll)); //target = SHVec3::Normalise(target); diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.h b/SHADE_Engine/src/Camera/SHCameraSystem.h index 98fd442f..fc6e9166 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.h +++ b/SHADE_Engine/src/Camera/SHCameraSystem.h @@ -5,6 +5,7 @@ #include "ECS_Base/System/SHSystemRoutine.h" #include "Resource/SHResourceLibrary.h" #include "SHCameraDirector.h" +#include "SHCameraArmComponent.h" #include "SH_API.h" namespace SHADE @@ -18,6 +19,7 @@ namespace SHADE //A camera component that represents editor camera. //This is not tied to any entity. Hence this EID should not be used. SHCameraComponent editorCamera; + SHCameraArmComponent editorCameraArm; SHResourceLibrary directorLibrary; std::vector directorHandleList; @@ -34,14 +36,7 @@ namespace SHADE void Init (void); void Exit (void); - class SH_API EditorCameraUpdate final : public SHSystemRoutine - { - public: - EditorCameraUpdate() : SHSystemRoutine("Editor Camera Update", true) { }; - virtual void Execute(double dt) noexcept override final; - - }; friend class EditorCameraUpdate; class SH_API CameraSystemUpdate final: public SHSystemRoutine @@ -63,6 +58,7 @@ namespace SHADE void DecomposeViewMatrix(SHMatrix const& matrix, float& pitch, float& yaw, float& roll, SHVec3& pos) noexcept; void SetCameraViewMatrix(SHCameraComponent& camera, SHMatrix const& viewMatrix) noexcept; void CameraLookAt(SHCameraComponent& camera, SHVec3 target) noexcept; + void UpdateEditorArm(double dt,bool active ,SHVec3 const& targetPos) noexcept; }; diff --git a/SHADE_Engine/src/Editor/Command/SHCommandManager.cpp b/SHADE_Engine/src/Editor/Command/SHCommandManager.cpp index 3c0ee5dd..b86f9247 100644 --- a/SHADE_Engine/src/Editor/Command/SHCommandManager.cpp +++ b/SHADE_Engine/src/Editor/Command/SHCommandManager.cpp @@ -10,63 +10,102 @@ namespace SHADE { - SHCommandManager::CommandStack SHCommandManager::undoStack{}; - SHCommandManager::CommandStack SHCommandManager::redoStack{}; + + SHCommandManager::CommandStack SHCommandManager::undoStack(defaultStackSize); + SHCommandManager::CommandStack SHCommandManager::redoStack(defaultStackSize); + SHCommandManager::CommandStack SHCommandManager::secondaryUndoStack(defaultStackSize); + SHCommandManager::CommandStack SHCommandManager::secondaryRedoStack(defaultStackSize); + + SHCommandManager::CommandStackPtr SHCommandManager::pCurrUndoStack(&undoStack); + SHCommandManager::CommandStackPtr SHCommandManager::pCurrRedoStack(&redoStack); void SHCommandManager::PerformCommand(BaseCommandPtr commandPtr, bool const& overrideValue) { - redoStack = CommandStack(); + *pCurrRedoStack = CommandStack(defaultStackSize); commandPtr->Execute(); - if (overrideValue && !undoStack.empty()) + if (overrideValue && !pCurrUndoStack->Empty()) { - undoStack.top()->Merge(commandPtr); + pCurrUndoStack->Top()->Merge(commandPtr); } else { - undoStack.push(commandPtr); + pCurrUndoStack->Push(commandPtr); } } void SHCommandManager::RegisterCommand(BaseCommandPtr commandPtr) { - undoStack.push(commandPtr); + pCurrUndoStack->Push(commandPtr); } void SHCommandManager::UndoCommand() { - if (undoStack.empty()) + if (pCurrUndoStack->Empty()) return; - undoStack.top()->Undo(); - redoStack.push(undoStack.top()); - undoStack.pop(); + pCurrUndoStack->Top()->Undo(); + pCurrRedoStack->Push(pCurrUndoStack->Top()); + pCurrUndoStack->Pop(); } void SHCommandManager::RedoCommand() { - if (redoStack.empty()) + if (pCurrRedoStack->Empty()) return; - redoStack.top()->Execute(); - undoStack.push(redoStack.top()); - redoStack.pop(); + pCurrRedoStack->Top()->Execute(); + pCurrUndoStack->Push(pCurrRedoStack->Top()); + pCurrRedoStack->Pop(); } std::size_t SHCommandManager::GetUndoStackSize() { - return undoStack.size(); + return pCurrUndoStack->Size(); } std::size_t SHCommandManager::GetRedoStackSize() { - return redoStack.size(); + return pCurrRedoStack->Size(); } void SHCommandManager::PopLatestCommandFromRedoStack() { - redoStack.pop(); + pCurrRedoStack->Pop(); } void SHCommandManager::PopLatestCommandFromUndoStack() { - undoStack.pop(); + pCurrUndoStack->Pop(); } + + void SHCommandManager::SwapStacks() + { + if (pCurrUndoStack == &undoStack) + { + pCurrUndoStack = &secondaryUndoStack; + } + else + { + secondaryUndoStack.Clear(); + pCurrUndoStack = &undoStack; + } + + if (pCurrRedoStack == &redoStack) + { + pCurrRedoStack = &secondaryRedoStack; + } + else + { + secondaryRedoStack.Clear(); + pCurrRedoStack = &redoStack; + } + } + + void SHCommandManager::ClearAll() + { + undoStack.Clear(); + redoStack.Clear(); + + secondaryUndoStack.Clear(); + secondaryRedoStack.Clear(); + } + }//namespace SHADE diff --git a/SHADE_Engine/src/Editor/Command/SHCommandManager.h b/SHADE_Engine/src/Editor/Command/SHCommandManager.h index a514c464..178347b5 100644 --- a/SHADE_Engine/src/Editor/Command/SHCommandManager.h +++ b/SHADE_Engine/src/Editor/Command/SHCommandManager.h @@ -10,6 +10,7 @@ //#==============================================================# #include "SHCommand.hpp" #include "SH_API.h" +#include "Tools/SHDeque.h" namespace SHADE { @@ -22,7 +23,8 @@ namespace SHADE using BaseCommandPtr = std::shared_ptr; template using SHCommandPtr = std::shared_ptr>; - using CommandStack = std::stack; + using CommandStack = SHDeque; + using CommandStackPtr = CommandStack*; static void PerformCommand(BaseCommandPtr commandPtr, bool const& overrideValue = false); static void RegisterCommand(BaseCommandPtr commandPtr); @@ -34,8 +36,17 @@ namespace SHADE static void PopLatestCommandFromRedoStack(); static void PopLatestCommandFromUndoStack(); + static void SwapStacks(); + static void ClearAll(); + + static constexpr CommandStack::SizeType defaultStackSize = 100; private: + static CommandStackPtr pCurrUndoStack; + static CommandStackPtr pCurrRedoStack; + static CommandStack undoStack; + static CommandStack secondaryUndoStack; static CommandStack redoStack; + static CommandStack secondaryRedoStack; }; }//namespace SHADE diff --git a/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp b/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp index 37b8ecd4..37521581 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp @@ -17,7 +17,7 @@ namespace SHADE { SHAssetBrowser::SHAssetBrowser() - :SHEditorWindow("\xee\x8b\x87 Asset Browser", ImGuiWindowFlags_MenuBar), rootFolder(SHAssetManager::GetRootFolder()), prevFolder(rootFolder), currentFolder(rootFolder), assetBeingCreated(std::nullopt) + :SHEditorWindow("\xee\x8b\x87 Asset Browser", ImGuiWindowFlags_MenuBar), rootFolder(SHAssetManager::GetRootFolder()), prevFolder(rootFolder), currentFolder(rootFolder) { } @@ -34,23 +34,48 @@ namespace SHADE RecursivelyDrawTree(rootFolder); DrawMenuBar(); DrawCurrentFolder(); + DrawAssetBeingCreated(); + } ImGui::End(); + if(refreshQueued) + Refresh(); + } + + void SHAssetBrowser::QueueRefresh() noexcept + { + refreshQueued = true; + } + + void SHAssetBrowser::Refresh() noexcept + { + SHAssetManager::RefreshDirectory(); + rootFolder = SHAssetManager::GetRootFolder(); + refreshQueued = false; } void SHAssetBrowser::DrawMenuBar() { if (ImGui::BeginMenuBar()) { - + if(ImGui::SmallButton(ICON_MD_SYNC)) + { + QueueRefresh(); + } + if(ImGui::SmallButton(ICON_FA_CIRCLE_PLUS)) + { + isAssetBeingCreated = true; + } ImGui::EndMenuBar(); } } + //if !compiled, set genMeta to true + ImRect SHAssetBrowser::RecursivelyDrawTree(FolderPointer folder) { auto const& subFolders = folder->subFolders; - auto const& files = folder->files; + auto files = folder->files; const bool isSelected = std::ranges::find(selectedFolders, folder) != selectedFolders.end(); ImGuiTreeNodeFlags flags = (subFolders.empty() && files.empty()) ? ImGuiTreeNodeFlags_Leaf : ImGuiTreeNodeFlags_OpenOnArrow; if (isSelected) @@ -62,21 +87,10 @@ namespace SHADE ImGuiID folderID = ImGui::GetItemID(); const ImRect nodeRect = ImRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax()); - if (ImGui::BeginPopupContextItem()) - { - if (ImGui::BeginMenu("Create Asset")) - { - //TODO: Change to rttr type enum align - if (ImGui::Selectable("Material")) - { - assetBeingCreated = { folder, AssetType::MATERIAL, "New Material" }; - ImGui::TreeNodeSetOpen(folderID, true); - isOpen = true; - } - ImGui::EndMenu(); - } - ImGui::EndPopup(); - } + //if (ImGui::BeginPopupContextItem()) + //{ + // ImGui::EndPopup(); + //} if (ImGui::IsItemClicked()) { @@ -100,19 +114,15 @@ namespace SHADE drawList->AddLine(ImVec2(vertLineStart.x, midPoint), ImVec2(vertLineStart.x + horizontalLineSize, midPoint), treeLineColor, 1); vertLineEnd.y = midPoint; } - for (auto const& file : files) + for (auto& file : files) { - if(file.assetMeta == nullptr) - continue; const float horizontalLineSize = 25.0f; - const ImRect childRect = DrawFile(file.assetMeta); + 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); vertLineEnd.y = midPoint; } drawList->AddLine(vertLineStart, vertLineEnd, treeLineColor, 1); - if(assetBeingCreated.has_value() && std::get<0>(assetBeingCreated.value()) == folder) - DrawAssetBeingCreated(); ImGui::TreePop(); } @@ -148,7 +158,33 @@ namespace SHADE //} } - ImRect SHAssetBrowser::DrawFile(SHAsset const* const asset) noexcept + ImRect SHAssetBrowser::DrawFile(SHFile& file) noexcept + { + if(file.compilable) + { + ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_Leaf; + static constexpr std::string_view icon = ICON_MD_FILE_PRESENT; + ImGui::PushID(file.name.data()); + bool const isOpen = ImGui::TreeNodeEx(file.name.data(), flags, "%s %s%s", icon.data(), file.name.data(), file.ext.data()); + const ImRect nodeRect = ImRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax()); + if(ImGui::BeginPopupContextItem()) + { + if(ImGui::Selectable("Compile")) + { + SHAssetManager::CompileAsset(file.path, !file.compiled); + QueueRefresh(); + } + ImGui::EndPopup(); + } + ImGui::TreePop(); + ImGui::PopID(); + return nodeRect; + } + if(file.assetMeta) + DrawAsset(file.assetMeta, file.ext); + } + + ImRect SHAssetBrowser::DrawAsset(SHAsset const* const asset, FileExt const& ext /*= ""*/) noexcept { if (asset == nullptr) return ImRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax()); @@ -173,7 +209,7 @@ namespace SHADE default:; } - bool const isOpen = ImGui::TreeNodeEx(asset, flags, "%s %s", icon.data(), asset->name.data()); + bool const isOpen = ImGui::TreeNodeEx(asset, flags, "%s %s%s", icon.data(), asset->name.data(), ext.data()); const ImRect nodeRect = ImRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax()); if (SHDragDrop::BeginSource()) { @@ -212,7 +248,6 @@ namespace SHADE case AssetType::MAX_COUNT: break; default:; } - } //TODO: Combine Draw asset and Draw Folder recursive drawing @@ -227,7 +262,7 @@ namespace SHADE for(auto const& subAsset : asset->subAssets) { const float horizontalLineSize = 25.0f; - const ImRect childRect = DrawFile(subAsset); + const ImRect childRect = DrawAsset(subAsset); const float midPoint = (childRect.Min.y + childRect.Max.y) * 0.5f; drawList->AddLine(ImVec2(vertLineStart.x, midPoint), ImVec2(vertLineStart.x + horizontalLineSize, midPoint), treeLineColor, 1); vertLineEnd.y = midPoint; @@ -240,19 +275,52 @@ namespace SHADE void SHAssetBrowser::DrawAssetBeingCreated() noexcept { - if (!assetBeingCreated.has_value()) - return; - auto& path = std::get<0>(assetBeingCreated.value()); - auto& type = std::get<1>(assetBeingCreated.value()); - auto& assetName = std::get<2>(assetBeingCreated.value()); - if (ImGui::InputText("##newAssetName", &assetName, ImGuiInputTextFlags_EnterReturnsTrue)) + if(isAssetBeingCreated) + ImGui::OpenPopup(newAssetPopup.data()); + + if(ImGui::BeginPopupModal(newAssetPopup.data(), &isAssetBeingCreated)) { - AssetID assetId = SHAssetManager::CreateNewAsset(type, assetName); - if (auto matInspector = SHEditorWindowManager::GetEditorWindow()) + ImGui::RadioButton("Material", true); + ImGui::SameLine(); + if (ImGui::InputText("##newAssetName", &nameOfAssetBeingCreated, ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_AutoSelectAll)) { - matInspector->OpenMaterial(assetId, true); + AssetID assetId = SHAssetManager::CreateNewAsset(AssetType::MATERIAL, nameOfAssetBeingCreated); + if (auto matInspector = SHEditorWindowManager::GetEditorWindow()) + { + matInspector->OpenMaterial(assetId, true); + } + nameOfAssetBeingCreated.clear(); + QueueRefresh(); + isAssetBeingCreated = false; + ImGui::CloseCurrentPopup(); } - assetBeingCreated.reset(); + ImGui::EndPopup(); } + //if (ImGui::BeginMenu("Create Asset")) + //{ + // //TODO: Change to rttr type enum align + // if (ImGui::Selectable("Material")) + // { + // assetBeingCreated = { folder, AssetType::MATERIAL, "NewMaterial" }; + // ImGui::TreeNodeSetOpen(folderID, true); + // isOpen = true; + // } + // ImGui::EndMenu(); + //} + //if (!assetBeingCreated.has_value()) + // return; + //auto& path = std::get<0>(assetBeingCreated.value()); + //auto& type = std::get<1>(assetBeingCreated.value()); + //auto& assetName = std::get<2>(assetBeingCreated.value()); + //if (ImGui::InputText("##newAssetName", &assetName, ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_AutoSelectAll)) + //{ + // AssetID assetId = SHAssetManager::CreateNewAsset(type, assetName); + // if (auto matInspector = SHEditorWindowManager::GetEditorWindow()) + // { + // matInspector->OpenMaterial(assetId, true); + // } + // assetBeingCreated.reset(); + // QueueRefresh(); + //} } } diff --git a/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.h b/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.h index 00023ebe..6d3c5eb4 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.h +++ b/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.h @@ -10,24 +10,29 @@ namespace SHADE class SHAssetBrowser final : public SHEditorWindow { public: - using AssetEntry = std::tuple; SHAssetBrowser(); void Init(); void Update(); - void Refresh(); + void QueueRefresh() noexcept; private: void DrawMenuBar(); ImRect RecursivelyDrawTree(FolderPointer folder); void DrawCurrentFolder(); - ImRect DrawFile(SHAsset const* const asset) noexcept; + ImRect DrawFile(SHFile& file) noexcept; + ImRect DrawAsset(SHAsset const* const asset, FileExt const& ext = "") noexcept; void DrawAssetBeingCreated() noexcept; + void Refresh() noexcept; + FolderPointer rootFolder, prevFolder, currentFolder; - std::optional assetBeingCreated; std::vector selectedFolders; std::vector selectedAssets; static constexpr float tileWidth = 50.0f; + bool refreshQueued = false; + bool isAssetBeingCreated = false; + static constexpr std::string_view newAssetPopup = "Create New Asset"; + std::string nameOfAssetBeingCreated; }; } diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp index c4dd1fdb..f240e321 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp @@ -93,13 +93,14 @@ namespace SHADE { EntityID const& eid = editor->selectedEntities[0]; SHEntity* entity = SHEntityManager::GetEntityByID(eid); - if(!entity) + SHSceneNode* entityNode = SHSceneManager::GetCurrentSceneGraph().GetNode(eid); + if(!entity || !entityNode) { ImGui::End(); return; } ImGui::TextColored(ImGuiColors::green, "EID: %zu", eid); - SHEditorWidgets::CheckBox("##IsActive", [entity]()->bool {return entity->GetActive(); }, [entity](bool const& active) {entity->SetActive(active); }); + SHEditorWidgets::CheckBox("##IsActive", [entityNode]()->bool {return entityNode->IsActive(); }, [entityNode](bool const& active) {entityNode->SetActive(active); }); ImGui::SameLine(); ImGui::InputText("##EntityName", &entity->name); diff --git a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp index cfb36cd0..ce3ca8b5 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp @@ -223,39 +223,20 @@ namespace SHADE { if(editor->SaveScene()) { - const SHEditorStateChangeEvent STATE_CHANGE_EVENT - { - .previousState = editor->editorState - }; - editor->editorState = SHEditor::State::PLAY; - - SHEventManager::BroadcastEvent(STATE_CHANGE_EVENT, SH_EDITOR_ON_PLAY_EVENT); + editor->Play(); } } ImGui::EndDisabled(); ImGui::BeginDisabled(editor->editorState == SHEditor::State::PAUSE); if(ImGui::SmallButton(ICON_MD_PAUSE)) { - const SHEditorStateChangeEvent STATE_CHANGE_EVENT - { - .previousState = editor->editorState - }; - editor->editorState = SHEditor::State::PAUSE; - - SHEventManager::BroadcastEvent(STATE_CHANGE_EVENT, SH_EDITOR_ON_PAUSE_EVENT); + editor->Pause(); } ImGui::EndDisabled(); ImGui::BeginDisabled(editor->editorState == SHEditor::State::STOP); if(ImGui::SmallButton(ICON_MD_STOP)) { - const SHEditorStateChangeEvent STATE_CHANGE_EVENT - { - .previousState = editor->editorState - }; - editor->editorState = SHEditor::State::STOP; - - SHEventManager::BroadcastEvent(STATE_CHANGE_EVENT, SH_EDITOR_ON_STOP_EVENT); - editor->LoadScene(SHSceneManager::GetCurrentSceneAssetID()); + editor->Stop(); } ImGui::EndDisabled(); ImGui::EndMenuBar(); diff --git a/SHADE_Engine/src/Editor/EditorWindow/ViewportWindow/SHEditorViewport.cpp b/SHADE_Engine/src/Editor/EditorWindow/ViewportWindow/SHEditorViewport.cpp index d6ef8d19..7b3b5411 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/ViewportWindow/SHEditorViewport.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/ViewportWindow/SHEditorViewport.cpp @@ -40,6 +40,7 @@ namespace SHADE shouldUpdateCamera = false; } ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); + SHEditor* editor = SHSystemManager::GetSystem(); if (Begin()) { @@ -51,7 +52,6 @@ namespace SHADE beginCursorPos = ImGui::GetCursorScreenPos(); viewportMousePos = { mousePos.x - beginCursorPos.x, mousePos.y - beginCursorPos.y }; gfxSystem->GetMousePickSystem()->SetViewportMousePos(viewportMousePos); - ImGui::Image((ImTextureID)descriptorSet, { beginContentRegionAvailable.x, beginContentRegionAvailable.y }); if (ImGui::IsWindowHovered() && ImGui::IsMouseDown(ImGuiMouseButton_Right)) @@ -64,24 +64,25 @@ namespace SHADE shouldUpdateCamera = true; } - if (ImGui::IsWindowFocused() && !ImGui::IsMouseDown(ImGuiMouseButton_Right)) + if (editor->editorState != SHEditor::State::PLAY && ImGui::IsWindowFocused() && !ImGui::IsMouseDown(ImGuiMouseButton_Right)) { - if (ImGui::IsKeyReleased(ImGuiKey_Q)) + if (ImGui::IsKeyReleased(ImGuiKey_W)) { transformGizmo.operation = SHTransformGizmo::Operation::TRANSLATE; } - if (ImGui::IsKeyReleased(ImGuiKey_W)) + if (ImGui::IsKeyReleased(ImGuiKey_E)) { transformGizmo.operation = SHTransformGizmo::Operation::ROTATE; } - if (ImGui::IsKeyReleased(ImGuiKey_E)) + if (ImGui::IsKeyReleased(ImGuiKey_R)) { transformGizmo.operation = SHTransformGizmo::Operation::SCALE; } } } ImGuizmo::SetRect(beginCursorPos.x, beginCursorPos.y, beginContentRegionAvailable.x, beginContentRegionAvailable.y); - transformGizmo.Draw(); + if(editor->editorState != SHEditor::State::PLAY) + transformGizmo.Draw(); ImGui::End(); ImGui::PopStyleVar(); } diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp index c4ad3459..90655a62 100644 --- a/SHADE_Engine/src/Editor/SHEditor.cpp +++ b/SHADE_Engine/src/Editor/SHEditor.cpp @@ -168,7 +168,19 @@ namespace SHADE { SHCommandManager::UndoCommand(); } - + if(ImGui::IsKeyReleased(ImGuiKey_F5)) + { + Play(); + } + else if (ImGui::IsKeyReleased(ImGuiKey_F6)) + { + Pause(); + } + else if (ImGui::IsKeyReleased(ImGuiKey_F7)) + { + Stop(); + } + Render(); } @@ -597,6 +609,48 @@ namespace SHADE } } + void SHEditor::Play() + { + if(editorState == State::PLAY) + return; + if (SaveScene()) + { + const SHEditorStateChangeEvent STATE_CHANGE_EVENT + { + .previousState = editorState + }; + editorState = State::PLAY; + SHCommandManager::SwapStacks(); + SHEventManager::BroadcastEvent(STATE_CHANGE_EVENT, SH_EDITOR_ON_PLAY_EVENT); + } + } + + void SHEditor::Pause() + { + if (editorState == State::PAUSE) + return; + const SHEditorStateChangeEvent STATE_CHANGE_EVENT + { + .previousState = editorState + }; + editorState = State::PAUSE; + SHEventManager::BroadcastEvent(STATE_CHANGE_EVENT, SH_EDITOR_ON_PAUSE_EVENT); + } + + void SHEditor::Stop() + { + if (editorState == State::STOP) + return; + const SHEditorStateChangeEvent STATE_CHANGE_EVENT + { + .previousState = editorState + }; + editorState = SHEditor::State::STOP; + SHCommandManager::SwapStacks(); + SHEventManager::BroadcastEvent(STATE_CHANGE_EVENT, SH_EDITOR_ON_STOP_EVENT); + LoadScene(SHSceneManager::GetCurrentSceneAssetID()); + } + void SHEditor::NewFrame() { SDL_Event event; diff --git a/SHADE_Engine/src/Editor/SHEditor.h b/SHADE_Engine/src/Editor/SHEditor.h index 0f5a3aaa..0de7796a 100644 --- a/SHADE_Engine/src/Editor/SHEditor.h +++ b/SHADE_Engine/src/Editor/SHEditor.h @@ -184,6 +184,10 @@ namespace SHADE void LoadScene(AssetID const& assetID) noexcept; + void Play(); + void Pause(); + void Stop(); + // List of selected entities std::vector selectedEntities; diff --git a/SHADE_Engine/src/Editor/SHEditorUI.cpp b/SHADE_Engine/src/Editor/SHEditorUI.cpp index 49cfbfd6..40e08042 100644 --- a/SHADE_Engine/src/Editor/SHEditorUI.cpp +++ b/SHADE_Engine/src/Editor/SHEditorUI.cpp @@ -53,9 +53,12 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ /* ImGui Wrapper Functions - Organizers */ /*-----------------------------------------------------------------------------------*/ - bool SHEditorUI::CollapsingHeader(const std::string& title) + bool SHEditorUI::CollapsingHeader(const std::string& title, bool* isHovered) { - return ImGui::CollapsingHeader(title.c_str(), ImGuiTreeNodeFlags_DefaultOpen); + const bool OPENED = ImGui::CollapsingHeader(title.c_str(), ImGuiTreeNodeFlags_DefaultOpen); + if (isHovered) + *isHovered = ImGui::IsItemHovered(); + return OPENED; } void SHEditorUI::SameLine() @@ -75,7 +78,7 @@ namespace SHADE bool SHEditorUI::BeginMenu(const std::string& label) { - return ImGui::BeginMenu(label.data()); + return ImGui::BeginMenu(label.data()); } bool SHEditorUI::BeginMenu(const std::string& label, const char* icon) @@ -143,7 +146,7 @@ namespace SHADE bool SHEditorUI::Selectable(const std::string& label) { - return ImGui::Selectable(label.data()); + return ImGui::Selectable(label.data()); } bool SHEditorUI::Selectable(const std::string& label, const char* icon) @@ -165,8 +168,10 @@ namespace SHADE if (isHovered) *isHovered = ImGui::IsItemHovered(); ImGui::SameLine(); - return ImGui::InputInt("##", &value, - 1, 10, + return ImGui::DragInt("##", &value, 0.001f, + std::numeric_limits::min(), + std::numeric_limits::max(), + "%d", ImGuiInputTextFlags_EnterReturnsTrue); } bool SHEditorUI::InputUnsignedInt(const std::string& label, unsigned int& value, bool* isHovered) @@ -190,31 +195,22 @@ namespace SHADE if (isHovered) *isHovered = ImGui::IsItemHovered(); ImGui::SameLine(); - return ImGui::InputFloat("##", &value, - 0.1f, 1.0f, "%.3f", + return ImGui::DragFloat("##", &value, 0.001f, + std::numeric_limits::lowest(), + std::numeric_limits::max(), + "%.3f", ImGuiInputTextFlags_EnterReturnsTrue); } bool SHEditorUI::InputDouble(const std::string& label, double& value, bool* isHovered) { - ImGui::Text(label.c_str()); - if (isHovered) - *isHovered = ImGui::IsItemHovered(); - ImGui::SameLine(); - return ImGui::InputDouble("##", &value, - 0.1, 1.0, "%.3f", - ImGuiInputTextFlags_EnterReturnsTrue); + float val = value; + const bool CHANGED = InputFloat(label, val, isHovered); + if (CHANGED) + { + value = static_cast(val); + } + return CHANGED; } - bool SHEditorUI::InputAngle(const std::string& label, double& value, bool* isHovered) - { - ImGui::Text(label.c_str()); - if (isHovered) - *isHovered = ImGui::IsItemHovered(); - ImGui::SameLine(); - return ImGui::InputDouble("##", &value, - 1.0, 45.0, "%.3f", - ImGuiInputTextFlags_EnterReturnsTrue); - } - bool SHEditorUI::InputSlider(const std::string& label, int min, int max, int& value, bool* isHovered /*= nullptr*/) { ImGui::Text(label.c_str()); @@ -266,10 +262,10 @@ namespace SHADE static const std::vector COMPONENT_LABELS = { "X", "Y" }; return SHEditorWidgets::DragN(label, COMPONENT_LABELS, { &value.x, &value.y }, 0.1f, "%.3f", float{}, float{}, 0, isHovered); } - bool SHEditorUI::InputVec3(const std::string& label, SHVec3& value, bool* isHovered, float speed) + bool SHEditorUI::InputVec3(const std::string& label, SHVec3& value, bool* isHovered) { static const std::vector COMPONENT_LABELS = { "X", "Y", "Z"}; - return SHEditorWidgets::DragN(label, COMPONENT_LABELS, { &value.x, &value.y, &value.z }, speed, "%.3f", float{}, float{}, 0, isHovered); + return SHEditorWidgets::DragN(label, COMPONENT_LABELS, { &value.x, &value.y, &value.z }, 0.1f, "%.3f", float{}, float{}, 0, isHovered); } bool SHEditorUI::InputTextField(const std::string& label, std::string& value, bool* isHovered) diff --git a/SHADE_Engine/src/Editor/SHEditorUI.h b/SHADE_Engine/src/Editor/SHEditorUI.h index 4e8f4400..f450ac0d 100644 --- a/SHADE_Engine/src/Editor/SHEditorUI.h +++ b/SHADE_Engine/src/Editor/SHEditorUI.h @@ -85,8 +85,9 @@ namespace SHADE /// Wraps up ImGui::CollapsingHeader(). /// /// Label for the header. + /// Label used to identify this widget. - /// Reference to the variable to store the result. - /// Reference to the variable to store the result. /// + static std::enable_if_t<(... && std::is_base_of_v), bool> CheckNodeAndComponentsActive(EntityID eid) + { + return CheckNodeActive(eid) && (... && SHComponentManager::GetComponent_s(eid)->isActive); + } + + /******************************************************************** + * \brief + * Check if the Entity's scene node is active and all the + * components specified are active. + * This also checks to verify that the entity has such components. + * \param eid + * EntityID of the entity to check for. + * \return + * true if scene node is active and all the components specified + * are also active. + ********************************************************************/ + template + static std::enable_if_t<(... && std::is_base_of_v), bool> CheckNodeAndHasComponentsActive(EntityID eid) + { + return CheckNodeActive(eid) + && (... && SHComponentManager::HasComponent(eid)) + && (... && SHComponentManager::GetComponent_s(eid)->isActive); + } + + /******************************************************************** + * \brief + * Check if Scene node is active. + * \param eid + * EntityID of the entity to check for. + * \return + * true if scene node is active + ********************************************************************/ + static bool CheckNodeActive(EntityID eid) + { + return GetCurrentSceneGraph().IsActiveInHierarchy(eid); + } + /*!************************************************************************* * \brief diff --git a/SHADE_Engine/src/Scene/SHSceneNode.cpp b/SHADE_Engine/src/Scene/SHSceneNode.cpp index b619d464..8dac20bd 100644 --- a/SHADE_Engine/src/Scene/SHSceneNode.cpp +++ b/SHADE_Engine/src/Scene/SHSceneNode.cpp @@ -136,7 +136,7 @@ namespace SHADE for (auto* child : children) { - SetActive(newActiveState); + child->SetActive(newActiveState); } } diff --git a/SHADE_Engine/src/Tools/SHDeque.h b/SHADE_Engine/src/Tools/SHDeque.h new file mode 100644 index 00000000..99df910a --- /dev/null +++ b/SHADE_Engine/src/Tools/SHDeque.h @@ -0,0 +1,69 @@ +#pragma once +#pragma once + +#include "SH_API.h" +#include + +namespace SHADE +{ + template + class SH_API SHDeque + { + public: + using ValueType = T; + using Pointer = T*; + using ValueRef = T&; + using ValueConstRef = T const&; + using SizeType = uint32_t; + using ContainerType = std::deque; + using ContainerTypeConstRef = std::deque; + + SHDeque(SizeType n) : max_size(n) {} + + ContainerTypeConstRef const& GetDeque() const + { + return deque; + } + + void Push(ValueConstRef obj) + { + if (deque.size() < max_size) + deque.push_front(std::move(obj)); + else + { + deque.pop_back(); + deque.push_front(std::move(obj)); + } + } + + bool Empty() + { + return deque.empty(); + } + + void Pop() + { + deque.pop_front(); + } + + ValueConstRef Top() + { + return deque.front(); + } + + SizeType Size() const noexcept + { + return deque.size(); + } + + void Clear() + { + deque.clear(); + } + + private: + int max_size; + ContainerType deque{}; + + }; +} \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHCanvasComponent.h b/SHADE_Engine/src/UI/SHCanvasComponent.h new file mode 100644 index 00000000..2e9a54f1 --- /dev/null +++ b/SHADE_Engine/src/UI/SHCanvasComponent.h @@ -0,0 +1,35 @@ +#pragma once + +#include "SH_API.h" +#include "ECS_Base/Components/SHComponent.h" + + +namespace SHADE +{ + + class SH_API SHCanvasComponent final: public SHComponent + { + using CanvasSizeType = uint32_t; + + + public: + SHCanvasComponent(); + ~SHCanvasComponent() = default; + + void SetCanvasSize(CanvasSizeType width, CanvasSizeType height) noexcept; + void SetCanvasWidth(CanvasSizeType width) noexcept; + void SetCanvasHeight(CanvasSizeType height) noexcept; + + CanvasSizeType const GetCanvasWidth() const noexcept; + CanvasSizeType const GetCanvasHeight() const noexcept; + + private: + CanvasSizeType width; + CanvasSizeType height; + + + + }; + + +} \ No newline at end of file diff --git a/SHADE_Managed/src/Editor/Editor.cxx b/SHADE_Managed/src/Editor/Editor.cxx index 54200c1e..68dddf34 100644 --- a/SHADE_Managed/src/Editor/Editor.cxx +++ b/SHADE_Managed/src/Editor/Editor.cxx @@ -18,12 +18,9 @@ of DigiPen Institute of Technology is prohibited. #include "Editor/Editor.hxx" // STL Includes #include -// External Dependencies -#include "Editor/SHEditorUI.h" // Project Headers #include "Components/Component.hxx" #include "Scripts/ScriptStore.hxx" -#include "Utility/Convert.hxx" #include "Utility/Debug.hxx" #include "Serialisation/ReflectionUtilities.hxx" #include "Editor/IconsMaterialDesign.h" @@ -31,98 +28,14 @@ of DigiPen Institute of Technology is prohibited. #include "Editor/Command/SHCommand.hpp" #include "TooltipAttribute.hxx" #include "RangeAttribute.hxx" +#include "Math/Vector2.hxx" +#include "Math/Vector3.hxx" +#include // Using Directives using namespace System; using namespace System::Collections::Generic; -/*-------------------------------------------------------------------------------------*/ -/* Macro Functions */ -/*-------------------------------------------------------------------------------------*/ -/// -/// Macro expansion that is used in renderFieldInInspector() to check the type of a field -/// named "field" against the specified type and if it matches, retrieves the value of -/// that field from an object named "object" and pass it into the specified SHEditorUI:: -/// function named "FUNC" by casting it into the NATIVE_TYPE specified. -///
-/// This only works for primitive types that have the same types for managed and native. -///
-/// The managed type of the object to edit. -/// The native type of the object to edit. -/// The SHEditorUI:: function to use for editing. -#define RENDER_FIELD(MANAGED_TYPE, NATIVE_TYPE, FUNC) \ -(field->FieldType == MANAGED_TYPE::typeid) \ -{ \ - NATIVE_TYPE val = safe_cast(field->GetValue(object)); \ - NATIVE_TYPE oldVal = val; \ - if (SHEditorUI::FUNC(Convert::ToNative(field->Name), val, &isHovered))\ - { \ - field->SetValue(object, val); \ - registerUndoAction(object, field, val, oldVal); \ - } \ -} \ -/// -/// Alternative to RENDER_FIELD that checks for RangeAttribute and switches to a slider -/// instead. -/// -/// The managed type of the object to edit. -/// The native type of the object to edit. -/// The SHEditorUI:: function to use for editing. -#define RENDER_FIELD_RANGE(MANAGED_TYPE, NATIVE_TYPE, FUNC) \ -(field->FieldType == MANAGED_TYPE::typeid) \ -{ \ - NATIVE_TYPE val = safe_cast(field->GetValue(object)); \ - NATIVE_TYPE oldVal = val; \ - \ - RangeAttribute^ rangeAttrib = hasAttribute(field);\ - const std::string FIELD_NAME = Convert::ToNative(field->Name); \ - bool changed = false; \ - if (rangeAttrib) \ - { \ - changed = SHEditorUI::InputSlider \ - ( \ - FIELD_NAME, \ - static_cast(rangeAttrib->Min), \ - static_cast(rangeAttrib->Max), \ - val, &isHovered \ - ); \ - } \ - else \ - { \ - changed = SHEditorUI::FUNC(FIELD_NAME, val, &isHovered); \ - } \ - \ - if (changed) \ - { \ - field->SetValue(object, val); \ - registerUndoAction(object, field, val, oldVal); \ - } \ -} \ -/// -/// Macro expansion that is used in renderFieldInInspector() to check the type of a field -/// named "field" against the specified type and if it matches, retrieves the value of -/// that field from an object named "object" and pass it into the specified SHEditorUI:: -/// function named "FUNC" by casting it into the NATIVE_TYPE specified. -///
-/// This only works for types that have an implementation of Convert::ToNative and -/// Convert::ToCLI. -///
-/// The managed type of the object to edit. -/// The native type of the object to edit. -/// The SHEditorUI:: function to use for editing. -#define RENDER_FIELD_CASTED(MANAGED_TYPE, NATIVE_TYPE, FUNC) \ -(field->FieldType == MANAGED_TYPE::typeid) \ -{ \ - NATIVE_TYPE val = Convert::ToNative(safe_cast(field->GetValue(object))); \ - NATIVE_TYPE oldVal = val; \ - \ - if (SHEditorUI::FUNC(Convert::ToNative(field->Name), val, &isHovered)) \ - { \ - field->SetValue(object, Convert::ToCLI(val)); \ - registerUndoAction(object, field, Convert::ToCLI(val), Convert::ToCLI(oldVal)); \ - } \ -} \ - /*-------------------------------------------------------------------------------------*/ /* Function Definitions */ /*-------------------------------------------------------------------------------------*/ @@ -234,142 +147,137 @@ namespace SHADE } SHEditorUI::PopID(); } - void Editor::renderFieldInInspector(Reflection::FieldInfo^ field, Object^ object) + + void Editor::renderFieldInInspector(Reflection::FieldInfo^ field, System::Object^ object) { bool isHovered = false; - if RENDER_FIELD_RANGE (Int16, int, InputInt) - else if RENDER_FIELD_RANGE (Int32, int, InputInt) - else if RENDER_FIELD_RANGE (Int64, int, InputInt) - else if RENDER_FIELD_RANGE (UInt16, unsigned int, InputUnsignedInt) - else if RENDER_FIELD_RANGE (UInt32, unsigned int, InputUnsignedInt) - else if RENDER_FIELD_RANGE (UInt64, unsigned int, InputUnsignedInt) - else if RENDER_FIELD_RANGE (Byte, int, InputInt) - else if RENDER_FIELD (bool, bool, InputCheckbox) - else if RENDER_FIELD_RANGE (float, float, InputFloat) - else if RENDER_FIELD_RANGE (double, double, InputDouble) - else if (field->FieldType->IsSubclassOf(Enum::typeid)) - { - // Get all the names of the enums - const array^ ENUM_NAMES = field->FieldType->GetEnumNames(); - std::vector nativeEnumNames; - for each (String^ str in ENUM_NAMES) - { - nativeEnumNames.emplace_back(Convert::ToNative(str)); - } + const bool MODIFIED_PRIMITIVE = + renderSpecificField(field, object, SHEditorUI::InputInt , &isHovered) || + renderSpecificField(field, object, SHEditorUI::InputInt , &isHovered) || + renderSpecificField(field, object, SHEditorUI::InputInt , &isHovered) || + renderSpecificField(field, object, SHEditorUI::InputInt , &isHovered) || + renderSpecificField(field, object, SHEditorUI::InputInt , &isHovered) || + renderSpecificField(field, object, SHEditorUI::InputInt , &isHovered) || + renderSpecificField(field, object, SHEditorUI::InputInt , &isHovered) || + renderSpecificField(field, object, SHEditorUI::InputCheckbox, &isHovered) || + renderSpecificField(field, object, SHEditorUI::InputFloat , &isHovered) || + renderSpecificField(field, object, SHEditorUI::InputDouble , &isHovered) || + renderSpecificField(field, object, SHEditorUI::InputVec2 , &isHovered) || + renderSpecificField(field, object, SHEditorUI::InputVec3 , &isHovered) || + renderSpecificField(field, object, nullptr , &isHovered) || + renderSpecificField(field, object, nullptr , &isHovered) || + renderSpecificField(field, object, nullptr , &isHovered); - int val = safe_cast(field->GetValue(object)); - int oldVal = val; - if (SHEditorUI::InputEnumCombo(Convert::ToNative(field->Name), val, nativeEnumNames, &isHovered)) - { - field->SetValue(object, val); - registerUndoAction(object, field, val, oldVal); - } - } - else if RENDER_FIELD_CASTED(Vector2, SHVec2, InputVec2) - else if RENDER_FIELD_CASTED(Vector3, SHVec3, InputVec3) - else if (field->FieldType == String::typeid) + if (!MODIFIED_PRIMITIVE) { - // Prevent issues where String^ is null due to being empty - String^ stringVal = safe_cast(field->GetValue(object)); - if (stringVal == nullptr) + // Any List + if (ReflectionUtilities::FieldIsList(field)) { - stringVal = ""; - } + System::Type^ listType = field->FieldType->GenericTypeArguments[0]; + RangeAttribute^ rangeAttrib = hasAttribute(field); + System::Collections::IList^ iList = safe_cast(field->GetValue(object)); - // Actual Field - std::string val = Convert::ToNative(stringVal); - std::string oldVal = val; - if (SHEditorUI::InputTextField(Convert::ToNative(field->Name), val, &isHovered)) - { - field->SetValue(object, Convert::ToCLI(val)); - registerUndoAction(object, field, Convert::ToCLI(val), Convert::ToCLI(oldVal)); - } - } - else if (field->FieldType == GameObject::typeid) - { - GameObject gameObj = safe_cast(field->GetValue(object)); - uint32_t entityId = gameObj.GetEntity(); - if (SHEditorUI::InputGameObjectField(Convert::ToNative(field->Name), entityId, &isHovered, !gameObj)) - { - GameObject newVal = GameObject(entityId); - if (entityId != MAX_EID) + if (SHEditorUI::CollapsingHeader(Convert::ToNative(field->Name), &isHovered)) { - // Null GameObject set - newVal = GameObject(entityId); - } - field->SetValue(object, newVal); - registerUndoAction(object, field, newVal, gameObj); - } - } - else - { - array^ interfaces = field->FieldType->GetInterfaces(); - if (interfaces->Length > 0 && interfaces[0] == ICallbackEvent::typeid) - { - array^ typeArgs = field->FieldType->GenericTypeArguments; - System::String^ title = field->Name + " : CallbackEvent<"; - for (int i = 0; i < typeArgs->Length; ++i) - { - title += typeArgs[i]->Name; - if (i < typeArgs->Length - 1) - title += ", "; - } - title += ">"; - if (SHEditorUI::CollapsingHeader(Convert::ToNative(title))) - { - // Constants - const std::string LABEL = Convert::ToNative(field->Name); - SHEditorUI::PushID(LABEL); - - ICallbackEvent^ callbackEvent = safe_cast(field->GetValue(object)); - if (callbackEvent == nullptr) + if (SHEditorUI::Button("Add Item")) { - // Construct one since it was not constructed before - callbackEvent = safe_cast(System::Activator::CreateInstance(field->FieldType)); + System::Object^ obj = System::Activator::CreateInstance(listType); + iList->Add(obj); + registerUndoListAddAction(listType, iList, iList->Count - 1, obj); } - for each (ICallbackAction ^ action in callbackEvent->Actions) + for (int i = 0; i < iList->Count; ++i) { - if (action->IsRuntimeAction) - continue; - - // Attempt to get the object if any - int entityId = static_cast(-1); - if (action->TargetObject) + SHEditorUI::PushID(i); + System::Object^ obj = iList[i]; + System::Object^ oldObj = iList[i]; + if (renderFieldEditor(std::to_string(i), obj, rangeAttrib)) { - Script^ script = safe_cast(action->TargetObject); - if (script) - { - entityId = static_cast(script->Owner.GetEntity()); - } + iList[i] = obj; + registerUndoListChangeAction(listType, iList, i, obj, oldObj); } - SHEditorUI::InputInt("", entityId); - SHEditorUI::SameLine(); - System::String^ methodName = ""; - if (action->TargetMethodName != nullptr) - { - methodName = action->TargetMethodName; - } - std::string methodNameNative = Convert::ToNative(methodName); - SHEditorUI::InputTextField("", methodNameNative); SHEditorUI::SameLine(); if (SHEditorUI::Button("-")) { - callbackEvent->DeregisterAction(action); + System::Object^ obj = iList[i]; + iList->RemoveAt(i); + registerUndoListRemoveAction(listType, iList, i, obj); + SHEditorUI::PopID(); break; } + SHEditorUI::PopID(); } - if (SHEditorUI::Button("Add Action")) - { - callbackEvent->RegisterAction(); - } - - SHEditorUI::PopID(); } } else { - return; + array^ interfaces = field->FieldType->GetInterfaces(); + if (interfaces->Length > 0 && interfaces[0] == ICallbackEvent::typeid) + { + array^ typeArgs = field->FieldType->GenericTypeArguments; + System::String^ title = field->Name + " : CallbackEvent<"; + for (int i = 0; i < typeArgs->Length; ++i) + { + title += typeArgs[i]->Name; + if (i < typeArgs->Length - 1) + title += ", "; + } + title += ">"; + if (SHEditorUI::CollapsingHeader(Convert::ToNative(title))) + { + // Constants + const std::string LABEL = Convert::ToNative(field->Name); + SHEditorUI::PushID(LABEL); + + ICallbackEvent^ callbackEvent = safe_cast(field->GetValue(object)); + if (callbackEvent == nullptr) + { + // Construct one since it was not constructed before + callbackEvent = safe_cast(System::Activator::CreateInstance(field->FieldType)); + } + for each (ICallbackAction ^ action in callbackEvent->Actions) + { + if (action->IsRuntimeAction) + continue; + + // Attempt to get the object if any + int entityId = static_cast(-1); + if (action->TargetObject) + { + Script^ script = safe_cast(action->TargetObject); + if (script) + { + entityId = static_cast(script->Owner.GetEntity()); + } + } + SHEditorUI::InputInt("", entityId); + SHEditorUI::SameLine(); + System::String^ methodName = ""; + if (action->TargetMethodName != nullptr) + { + methodName = action->TargetMethodName; + } + std::string methodNameNative = Convert::ToNative(methodName); + SHEditorUI::InputTextField("", methodNameNative); + SHEditorUI::SameLine(); + if (SHEditorUI::Button("-")) + { + callbackEvent->DeregisterAction(action); + break; + } + } + if (SHEditorUI::Button("Add Action")) + { + callbackEvent->RegisterAction(); + } + + SHEditorUI::PopID(); + } + } + else + { + return; + } } } @@ -383,6 +291,51 @@ namespace SHADE } } + bool Editor::renderFieldEditor(const std::string& fieldName, System::Object^% object, RangeAttribute^ rangeAttrib) + { + bool modified; + + const bool RENDERED = + renderFieldEditor(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, SHEditorUI::InputInt , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, SHEditorUI::InputCheckbox, nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, SHEditorUI::InputFloat , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, SHEditorUI::InputDouble , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, SHEditorUI::InputVec2 , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, SHEditorUI::InputVec3 , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, nullptr , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, nullptr , nullptr, rangeAttrib, modified) || + renderFieldEditor(fieldName, object, nullptr , nullptr, rangeAttrib, modified); + + return modified; + } + + bool Editor::renderEnumEditor(const std::string& fieldName, System::Object^% object, bool* isHovered) + { + // Get all the names of the enums + const array^ ENUM_NAMES = object->GetType()->GetEnumNames(); + std::vector nativeEnumNames; + for each (String ^ str in ENUM_NAMES) + { + nativeEnumNames.emplace_back(Convert::ToNative(str)); + } + + int val = safe_cast(object); + int oldVal = val; + if (SHEditorUI::InputEnumCombo(fieldName, val, nativeEnumNames, isHovered)) + { + object = val; + return true; + } + + return false; + } + void Editor::renderScriptContextMenu(Entity entity, Script^ script) { // Right Click Menu @@ -400,12 +353,40 @@ namespace SHADE void Editor::registerUndoAction(System::Object^ object, System::Reflection::FieldInfo^ field, System::Object^ newData, System::Object^ oldData) { // Create command and add it into the undo stack - UndoRedoStack::Command cmd; - cmd.Field = field; - cmd.Object = object; - cmd.NewData = newData; - cmd.OldData = oldData; - actionStack.Add(cmd); + actionStack.Add(gcnew FieldChangeCommand(object, field, newData, oldData)); + + // Inform the C++ Undo-Redo stack + SHCommandManager::RegisterCommand(std::reinterpret_pointer_cast(std::make_shared())); + } + + void Editor::registerUndoListChangeAction(System::Type^ type, System::Collections::IList^ list, int index, System::Object^ newData, System::Object^ oldData) + { + if (list == nullptr) + return; + + actionStack.Add(gcnew ListElementChangeCommand(list, index, newData, oldData)); + + // Inform the C++ Undo-Redo stack + SHCommandManager::RegisterCommand(std::reinterpret_pointer_cast(std::make_shared())); + } + + void Editor::registerUndoListAddAction(System::Type^ type, System::Collections::IList^ list, int index, System::Object^ data) + { + if (list == nullptr) + return; + + actionStack.Add(gcnew ListElementAddCommand(list, index, data)); + + // Inform the C++ Undo-Redo stack + SHCommandManager::RegisterCommand(std::reinterpret_pointer_cast(std::make_shared())); + } + + void Editor::registerUndoListRemoveAction(System::Type^ type, System::Collections::IList^ list, int index, System::Object^ data) + { + if (list == nullptr) + return; + + actionStack.Add(gcnew ListElementRemoveCommand(list, index, data)); // Inform the C++ Undo-Redo stack SHCommandManager::RegisterCommand(std::reinterpret_pointer_cast(std::make_shared())); diff --git a/SHADE_Managed/src/Editor/Editor.h++ b/SHADE_Managed/src/Editor/Editor.h++ new file mode 100644 index 00000000..a186d7ea --- /dev/null +++ b/SHADE_Managed/src/Editor/Editor.h++ @@ -0,0 +1,203 @@ +/************************************************************************************//*! +\file Editor.h++ +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 10, 2022 +\brief Contains the definition of templated functions for the managed Editor + static class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +// Primary Include +#include "Editor.hxx" +// External Dependencies +#include "Editor/SHEditorUI.h" +// Project Includes +#include "Utility/Convert.hxx" + +namespace SHADE +{ + template + bool Editor::renderSpecificField(System::Reflection::FieldInfo^ fieldInfo, System::Object^ object, EditorFieldFunc fieldEditor, bool* isHovered) + { + if constexpr (std::is_same_v) + { + if (fieldInfo->FieldType->IsSubclassOf(Enum::typeid)) + { + System::Object^ enumObj = fieldInfo->GetValue(object); + int oldVal = safe_cast(enumObj); + int val = oldVal; + if (renderEnumEditor + ( + Convert::ToNative(fieldInfo->Name), + enumObj, + isHovered + )) + { + fieldInfo->SetValue(object, safe_cast(enumObj)); + registerUndoAction(object, fieldInfo, fieldInfo->GetValue(object), oldVal); + } + + return true; + } + } + else + { + if (fieldInfo->FieldType == ManagedType::typeid) + { + RangeAttribute^ rangeAttrib; + if constexpr (std::is_arithmetic_v && !std::is_same_v) + { + rangeAttrib = hasAttribute(fieldInfo); + } + + ManagedType oldVal = safe_cast(fieldInfo->GetValue(object)); + ManagedType val = oldVal; + if (renderFieldEditorInternal + ( + Convert::ToNative(fieldInfo->Name), + &val, + fieldEditor, + isHovered, + rangeAttrib + )) + { + fieldInfo->SetValue(object, val); + registerUndoAction(object, fieldInfo, fieldInfo->GetValue(object), oldVal); + } + + return true; + } + } + + return false; + } + + template + bool Editor::renderFieldEditor(const std::string& fieldName, System::Object^% object, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib, bool& modified) + { + modified = false; + + if constexpr (std::is_same_v) + { + if (object->GetType()->IsSubclassOf(Enum::typeid)) + { + modified = renderEnumEditor(fieldName, object, isHovered); + return true; + } + } + else + { + if (object->GetType() == ManagedType::typeid) + { + ManagedType managedVal = safe_cast(object); + cli::interior_ptr managedValPtr = &managedVal; + if (renderFieldEditorInternal(fieldName, managedValPtr, fieldEditor, isHovered, rangeAttrib)) + { + object = managedVal; + modified = true; + return true; + } + return false; + } + } + + return false; + } + + + template + bool Editor::renderFieldEditorInternal(const std::string& fieldName, interior_ptr managedValPtr, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib) + { + // Retrieve the native version of the object + NativeType val; + if constexpr (IsPrimitiveTypeMatches_V) + { + val = safe_cast(*managedValPtr); + } + else + { + val = Convert::ToNative(*managedValPtr); + } + + // Throw into the SHEditorUI function + NativeType oldVal = val; + bool changed = false; + if (rangeAttrib) + { + // Do not allow bools for Sliders just in case + if constexpr (std::is_arithmetic_v && !std::is_same_v) + { + changed = SHEditorUI::InputSlider + ( + fieldName, + static_cast(rangeAttrib->Min), + static_cast(rangeAttrib->Max), + val, isHovered + ); + } + } + else + { + changed = fieldEditor(fieldName, val, isHovered); + } + + if (changed) + { + if constexpr (IsPrimitiveTypeMatches_V) + { + *managedValPtr = val; + } + else + { + + *managedValPtr = Convert::ToCLI(val); + } + + return true; + } + + return false; + } + template<> + bool Editor::renderFieldEditorInternal(const std::string& fieldName, interior_ptr managedValPtr, EditorFieldFunc, bool* isHovered, RangeAttribute^) + { + // Prevent issues where String^ is null due to being empty + if (*managedValPtr == nullptr) + *managedValPtr = ""; + + // Actual Field + std::string val = Convert::ToNative(*managedValPtr); + if (SHEditorUI::InputTextField(fieldName, val, isHovered)) + { + *managedValPtr = Convert::ToCLI(val); + return true; + } + + return false; + } + template<> + bool Editor::renderFieldEditorInternal(const std::string& fieldName, interior_ptr managedValPtr, EditorFieldFunc, bool* isHovered, RangeAttribute^) + { + uint32_t entityId = managedValPtr->GetEntity(); + if (SHEditorUI::InputGameObjectField(fieldName, entityId, isHovered, !(*managedValPtr))) + { + GameObject newVal = GameObject(entityId); + if (entityId != MAX_EID) + { + // Null GameObject set + newVal = GameObject(entityId); + } + *managedValPtr = newVal; + return true; + } + + return false; + } +} diff --git a/SHADE_Managed/src/Editor/Editor.hxx b/SHADE_Managed/src/Editor/Editor.hxx index 109842b5..64c445e5 100644 --- a/SHADE_Managed/src/Editor/Editor.hxx +++ b/SHADE_Managed/src/Editor/Editor.hxx @@ -17,9 +17,14 @@ of DigiPen Institute of Technology is prohibited. #include "Engine/Entity.hxx" #include "Scripts/Script.hxx" #include "UndoRedoStack.hxx" +#include "RangeAttribute.hxx" namespace SHADE { + + template + using EditorFieldFunc = bool(*)(const std::string& label, NativeType& val, bool* isHovered); + /// /// Static class for Editor-related functions /// @@ -84,15 +89,114 @@ namespace SHADE /// /// The object that contains the data of the field to render. /// - static void renderFieldInInspector(System::Reflection::FieldInfo^ field, Object^ object); + static void renderFieldInInspector(System::Reflection::FieldInfo^ field, System::Object^ object); + /// + /// Renders a raw editor for a single value. + /// + /// The name of the field to render. + /// Tracking reference to the object to modify. + /// + /// If specified, will be used to constrain values. + /// + /// True if the value was modified. + static bool renderFieldEditor(const std::string& fieldName, System::Object^% object, RangeAttribute^ rangeAttrib); + /// + /// Renders a ImGui field editor based on the type of parameters specified if the + /// type matches. + /// + /// Native type of the field. + /// Managed type of the field. + /// Label to use for the field editor. + /// + /// Tracking reference for the managed variable to modify. + /// + /// ImGui field editor function to use. + /// + /// Pointer to a bool that stores if the field editor was hovered over. + /// + /// + /// If provided and the type supports it, the field will be rendered with a + /// slider instead. + /// + /// + /// True if the field was rendered.. + template + static bool renderFieldEditor(const std::string& fieldName, System::Object^% object, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib, bool& modified); + /// + /// Renders a raw editor for a single enum value. + /// + /// The name of the field to render. + /// + /// Tracking reference to the object to modify. Must be an enum. + /// + /// + /// Pointer to a bool that stores if the field editor was hovered over. + /// + /// True if the value was modified. + static bool renderEnumEditor(const std::string& fieldName, System::Object^% object, bool* isHovered); + /// + /// Checks if the specified field is of the specified native and managed type + /// equivalent and renders a ImGui field editor based on the specified field + /// editor function. Also handles fields that contain a RangeAttribute. + /// + /// Native type of the field. + /// Managed type of the field. + /// Describes the field to modify. + /// Object to modify that has the specified field. + /// ImGui field editor function to use. + /// + /// Pointer to a bool that stores if the field editor was hovered over. + /// + /// True if the field is modified. + template + static bool renderSpecificField(System::Reflection::FieldInfo^ fieldInfo, System::Object^ object, EditorFieldFunc fieldEditor, bool* isHovered); + /// + /// Renders a ImGui field editor based on the type of parameters specified. + /// + /// Native type of the field. + /// Managed type of the field. + /// Label to use for the field editor. + /// + /// Tracking reference for the managed variable to modify. + /// + /// ImGui field editor function to use. + /// + /// Pointer to a bool that stores if the field editor was hovered over. + /// + /// + /// If provided and the type supports it, the field will be rendered with a + /// slider instead. + /// + /// True if the field is modified. + template + static bool renderFieldEditorInternal(const std::string& fieldName, interior_ptr managedValPtr, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib); + /// /// Renders a context menu when right clicked for the scripts /// /// The Entity to render the Scripts of. /// The Script to render the inspector for. static void renderScriptContextMenu(Entity entity, Script^ script); + /// + /// Adds changes to a variable as an undo-able/redo-able action on the Undo-Redo + /// stack. + /// + /// The object that changes are applied to. + /// The field that was changed. + /// New data to set. + /// Data that was overriden. static void registerUndoAction(System::Object^ object, System::Reflection::FieldInfo^ field, System::Object^ newData, System::Object^ oldData); + static void registerUndoListChangeAction(System::Type^ type, System::Collections::IList^ list, int index, System::Object^ newData, System::Object^ oldData); + static void registerUndoListAddAction(System::Type^ type, System::Collections::IList^ list, int index, System::Object^ data); + static void registerUndoListRemoveAction(System::Type^ type, System::Collections::IList^ list, int index, System::Object^ data); + /// + /// Checks if a specific field has the specified attribute + /// + /// Type of Attribute to check for. + /// The field to check. + /// The attribute to check for if it exists. Null otherwise. generic where Attribute : System::Attribute static Attribute hasAttribute(System::Reflection::FieldInfo^ field); }; } +#include "Editor.h++" diff --git a/SHADE_Managed/src/Editor/UndoRedoStack.cxx b/SHADE_Managed/src/Editor/UndoRedoStack.cxx index 08e289cc..789d285d 100644 --- a/SHADE_Managed/src/Editor/UndoRedoStack.cxx +++ b/SHADE_Managed/src/Editor/UndoRedoStack.cxx @@ -19,9 +19,14 @@ of DigiPen Institute of Technology is prohibited. // External Dependencies #include "Editor/SHEditorUI.h" // Project Headers +#include "Utility/Debug.hxx" +#include "Utility/Convert.hxx" namespace SHADE { + /*---------------------------------------------------------------------------------*/ + /* UndoRedoStack - Properties */ + /*---------------------------------------------------------------------------------*/ bool UndoRedoStack::UndoActionPresent::get() { return commandStack->Count > 0 && latestActionIndex >= 0; @@ -31,8 +36,11 @@ namespace SHADE { return latestActionIndex >= 0 && latestActionIndex < commandStack->Count - 1; } - - void UndoRedoStack::Add(Command command) + + /*---------------------------------------------------------------------------------*/ + /* UndoRedoStack - Usage Functions */ + /*---------------------------------------------------------------------------------*/ + void UndoRedoStack::Add(ICommand^ command) { // Erase any other actions ahead of the current action if (latestActionIndex >= 0 && latestActionIndex < commandStack->Count - 1) @@ -52,8 +60,8 @@ namespace SHADE if (!UndoActionPresent) return; - Command cmd = commandStack[latestActionIndex]; - cmd.Field->SetValue(cmd.Object, cmd.OldData); + ICommand^ cmd = commandStack[latestActionIndex]; + cmd->Unexceute(); --latestActionIndex; } @@ -62,8 +70,192 @@ namespace SHADE if (!RedoActionPresent) return; - Command cmd = commandStack[latestActionIndex]; - cmd.Field->SetValue(cmd.Object, cmd.NewData); + ICommand^ cmd = commandStack[latestActionIndex]; + cmd->Execute(); ++latestActionIndex; } + + /*---------------------------------------------------------------------------------*/ + /* FieldChangeCommand - Constructor */ + /*---------------------------------------------------------------------------------*/ + FieldChangeCommand::FieldChangeCommand(System::Object^ obj, System::Reflection::FieldInfo^ field, System::Object^ newData, System::Object^ oldData) + : objectToChange { obj } + , field { field } + , newData { newData } + , oldData { oldData } + {} + + /*---------------------------------------------------------------------------------*/ + /* FieldChangeCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + bool FieldChangeCommand::Execute() + { + if (field && objectToChange) + { + field->SetValue(objectToChange, newData); + return true; + } + + return false; + } + + bool FieldChangeCommand::Unexceute() + { + if (field && objectToChange) + { + field->SetValue(objectToChange, oldData); + return true; + } + + return false; + } + + bool FieldChangeCommand::Merge(ICommand^ command) + { + FieldChangeCommand^ otherCommand = safe_cast(command); + if (otherCommand == nullptr) + { + Debug::LogWarning("[Field Change Command] Attempted to merge two incompatible commands!"); + return false; + } + + // Only merge if they are workng on the same object and field + if (field == otherCommand->field && objectToChange == otherCommand->objectToChange) + { + newData = otherCommand->newData; + return true; + } + + return false; + } + + /*---------------------------------------------------------------------------------*/ + /* ListElementChangeCommand - Constructor */ + /*---------------------------------------------------------------------------------*/ + ListElementChangeCommand::ListElementChangeCommand(System::Collections::IList^ list, int index, System::Object^ newData, System::Object^ oldData) + : list { list } + , index { index } + , newData { newData } + , oldData { oldData } + {} + + /*---------------------------------------------------------------------------------*/ + /* ListElementChangeCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + bool ListElementChangeCommand::Execute() + { + if (list && index < list->Count) + { + list[index] = newData; + return true; + } + + return false; + } + + bool ListElementChangeCommand::Unexceute() + { + if (list && index < list->Count) + { + list[index] = oldData; + return true; + } + + return false; + } + bool ListElementChangeCommand::Merge(ICommand^ command) + { + ListElementChangeCommand^ otherCommand = safe_cast(command); + if (otherCommand == nullptr) + { + Debug::LogWarning("[Field Change Command] Attempted to merge two incompatible commands!"); + return false; + } + + if (command && list == otherCommand->list && index == otherCommand->index) + { + newData = otherCommand->newData; + return true; + } + } + + /*---------------------------------------------------------------------------------*/ + /* ListElementAddCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + ListElementAddCommand::ListElementAddCommand(System::Collections::IList^ list, int addIndex, System::Object^ data) + : list { list } + , addIndex { addIndex } + , data { data } + {} + + /*---------------------------------------------------------------------------------*/ + /* ListElementAddCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + bool ListElementAddCommand::Execute() + { + if (list) + { + list->Insert(addIndex, data); + return true; + } + + return false; + } + + bool ListElementAddCommand::Unexceute() + { + if (list && addIndex < list->Count) + { + list->RemoveAt(addIndex); + return true; + } + + return false; + } + + bool ListElementAddCommand::Merge(ICommand^) + { + // Not allowed + return false; + } + + /*---------------------------------------------------------------------------------*/ + /* ListElementRemoveCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + ListElementRemoveCommand::ListElementRemoveCommand(System::Collections::IList^ list, int removeIndex, System::Object^ data) + : list { list } + , removeIndex { removeIndex } + , data { data } + {} + + /*---------------------------------------------------------------------------------*/ + /* ListElementRemoveCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + bool ListElementRemoveCommand::Execute() + { + if (list && removeIndex < list->Count) + { + list->RemoveAt(removeIndex); + return true; + } + + return false; + } + + bool ListElementRemoveCommand::Unexceute() + { + if (list) + { + list->Insert(removeIndex, data); + return true; + } + + return false; + } + + bool ListElementRemoveCommand::Merge(ICommand^) + { + // Not allowed + return false; + } } diff --git a/SHADE_Managed/src/Editor/UndoRedoStack.hxx b/SHADE_Managed/src/Editor/UndoRedoStack.hxx index 4c525228..dea458bc 100644 --- a/SHADE_Managed/src/Editor/UndoRedoStack.hxx +++ b/SHADE_Managed/src/Editor/UndoRedoStack.hxx @@ -15,27 +15,99 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { + /// + /// Interface for command that fits into the UndoRedoStack which can perform + /// undo-able and redo-able operations. + /// + private interface class ICommand + { + /// + /// Executes an action. This is called when a "Redo" is performed. + /// + /// Whether the action was successful or not. + bool Execute(); + /// + /// Undoes an action. This is called when an "Undo" is performed. + /// + /// Whether the action was successful or not. + bool Unexceute(); + /// + /// Merges this command with another command. + /// + /// + /// Whether the merge was successful or not. + bool Merge(ICommand^ command); + }; + + private ref class FieldChangeCommand sealed : public ICommand + { + public: + FieldChangeCommand(System::Object^ obj, System::Reflection::FieldInfo^ field, System::Object^ newData, System::Object^ oldData); + + bool Execute() override; + bool Unexceute() override; + bool Merge(ICommand^ command) override; + + private: + System::Object^ objectToChange; + System::Reflection::FieldInfo^ field; + System::Object^ newData; + System::Object^ oldData; + }; + + + private ref class ListElementChangeCommand sealed : public ICommand + { + public: + ListElementChangeCommand(System::Collections::IList^ list, int index, System::Object^ newData, System::Object^ oldData); + + bool Execute() override; + bool Unexceute() override; + bool Merge(ICommand^ command) override; + + private: + System::Collections::IList^ list; + int index; + System::Object^ newData; + System::Object^ oldData; + }; + + private ref class ListElementAddCommand sealed : public ICommand + { + public: + ListElementAddCommand(System::Collections::IList^ list, int addIndex, System::Object^ data); + + bool Execute() override; + bool Unexceute() override; + bool Merge(ICommand^ command) override; + + private: + System::Collections::IList^ list; + int addIndex; // New index of the added element + System::Object^ data; + }; + + private ref class ListElementRemoveCommand sealed : public ICommand + { + public: + ListElementRemoveCommand(System::Collections::IList^ list, int removeIndex, System::Object^ data); + + bool Execute() override; + bool Unexceute() override; + bool Merge(ICommand^ command) override; + + private: + System::Collections::IList^ list; + int removeIndex; // Index of the element to remove at + System::Object^ data; + }; + /// /// Class that is able to store a stack of actions that can be done and redone. /// private ref class UndoRedoStack sealed { public: - /*-----------------------------------------------------------------------------*/ - /* Type Definitions */ - /*-----------------------------------------------------------------------------*/ - /// - /// Command for the stack that represents a data modification. - /// - value struct Command - { - public: - System::Object^ Object; - System::Reflection::FieldInfo^ Field; - System::Object^ NewData; - System::Object^ OldData; - }; - /*-----------------------------------------------------------------------------*/ /* Properties */ /*-----------------------------------------------------------------------------*/ @@ -55,7 +127,7 @@ namespace SHADE /// Adds a command onto the stack. /// /// - void Add(Command command); + void Add(ICommand^ command); /// /// Undos the last added command if it exists. /// @@ -70,6 +142,6 @@ namespace SHADE /* Data Members */ /*-----------------------------------------------------------------------------*/ int latestActionIndex = -1; - System::Collections::Generic::List^ commandStack = gcnew System::Collections::Generic::List(); + System::Collections::Generic::List^ commandStack = gcnew System::Collections::Generic::List(); }; } diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index b3e02a9e..ba176644 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -26,7 +26,7 @@ of DigiPen Institute of Technology is prohibited. #include "Utility/Convert.hxx" #include "Script.hxx" #include "Engine/Entity.hxx" -#include "Serialisation/ReflectionUtilities.hxx" +#include "Serialisation/SerialisationUtilities.hxx" #include "Engine/Application.hxx" #include "Physics/System/SHPhysicsSystemInterface.h" #include "Physics/SHPhysicsEvents.h" @@ -614,7 +614,7 @@ namespace SHADE System::Collections::Generic::List^ scriptList = scripts[entity]; for each (Script^ script in scriptList) { - ReflectionUtilities::Serialise(script, *yamlNode); + SerialisationUtilities::Serialise(script, *yamlNode); } return true; @@ -659,7 +659,7 @@ namespace SHADE if (AddScriptViaNameWithRef(entity, typeName, script)) { // Copy the data in - ReflectionUtilities::Deserialise(script, node); + SerialisationUtilities::Deserialise(script, node); } else { diff --git a/SHADE_Managed/src/Serialisation/ReflectionUtilities.cxx b/SHADE_Managed/src/Serialisation/ReflectionUtilities.cxx index 651afb73..f371686c 100644 --- a/SHADE_Managed/src/Serialisation/ReflectionUtilities.cxx +++ b/SHADE_Managed/src/Serialisation/ReflectionUtilities.cxx @@ -18,31 +18,6 @@ of DigiPen Institute of Technology is prohibited. #include "Serialisation/ReflectionUtilities.hxx" // Project Includes #include "SerializeFieldAttribute.hxx" -#include "Utility/Convert.hxx" -#include "Math/Vector2.hxx" -#include "Math/Vector3.hxx" -#include "Utility/Debug.hxx" -#include "Engine/GameObject.hxx" - -/*-------------------------------------------------------------------------------------*/ -/* Macro Functions */ -/*-------------------------------------------------------------------------------------*/ -/// -/// Macro expansion that is used in RapidJsonValueToField() to retrieve the specified -/// member of a Vector type that is stored into a Vector named "vec". -/// -/// The name of the member to retrieve. -#define PRIMITIVE_VECTOR_FIELD_ASSIGN(MEMBER) \ -iter = jsonValue.FindMember(#MEMBER); \ -if (iter != jsonValue.MemberEnd()) \ -{ \ - vec.MEMBER = iter->value.GetDouble(); \ -} \ - -/*-------------------------------------------------------------------------------------*/ -/* File-Level Constants */ -/*-------------------------------------------------------------------------------------*/ -static const std::string_view SCRIPT_TYPE_YAMLTAG = "Type"; /*-------------------------------------------------------------------------------------*/ /* Function Definitions */ @@ -64,202 +39,14 @@ namespace SHADE return fieldInfo->IsPublic || fieldInfo->GetCustomAttributes(SerializeField::typeid, true)->Length > 0; } - /*---------------------------------------------------------------------------------*/ - /* Serialisation Functions */ - /*---------------------------------------------------------------------------------*/ - void ReflectionUtilities::Serialise(System::Object^ object, YAML::Node& scriptListNode) + bool ReflectionUtilities::FieldIsList(System::Reflection::FieldInfo^ fieldInfo) { - using namespace System::Reflection; - - // Create YAML object - YAML::Node scriptNode; - scriptNode.SetStyle(YAML::EmitterStyle::Block); - scriptNode[SCRIPT_TYPE_YAMLTAG.data()] = Convert::ToNative(object->GetType()->FullName); - - // Get all fields - System::Collections::Generic::IEnumerable^ fields = GetInstanceFields(object); - for each (FieldInfo^ field in fields) - { - // Ignore private and non-SerialiseField - if (!FieldIsSerialisable(field)) - continue; - - // Serialise - writeFieldIntoYaml(field, object, scriptNode); - } - - scriptListNode.push_back(scriptNode); - } - void ReflectionUtilities::Deserialise(Object^ object, YAML::Node& yamlNode) - { - using namespace System::Reflection; - - // Load the YAML - if (!yamlNode.IsMap()) - { - // Invalid - Debug::LogError - ( - System::String::Format("[ReflectionUtilities] Invalid YAML Node provided for deserialization of \"{0}\" script.", - object->GetType()->FullName) - ); - return; - } - // Get all fields - System::Collections::Generic::IEnumerable^ fields = GetInstanceFields(object); - for each (FieldInfo^ field in fields) - { - // Ignore private and non-SerialiseField - if (!FieldIsSerialisable(field)) - continue; - - // Deserialise - const std::string FIELD_NAME = Convert::ToNative(field->Name); - if (yamlNode[FIELD_NAME]) - { - writeYamlIntoField(field, object, yamlNode[FIELD_NAME]); - } - } - } - /*---------------------------------------------------------------------------------*/ - /* Serialization Helper Functions */ - /*---------------------------------------------------------------------------------*/ - void ReflectionUtilities::writeFieldIntoYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& yamlNode) - { - // Field YAML Node - YAML::Node fieldNode; - - // Retrieve string for the YAML - const bool PRIMITIVE_SERIALIZED = fieldInsertYaml(fieldInfo, object, fieldNode) || - fieldInsertYaml(fieldInfo, object, fieldNode) || - fieldInsertYaml(fieldInfo, object, fieldNode) || - fieldInsertYaml(fieldInfo, object, fieldNode) || - fieldInsertYaml(fieldInfo, object, fieldNode) || - fieldInsertYaml(fieldInfo, object, fieldNode) || - fieldInsertYaml(fieldInfo, object, fieldNode) || - fieldInsertYaml(fieldInfo, object, fieldNode) || - fieldInsertYaml(fieldInfo, object, fieldNode) || - fieldInsertYaml(fieldInfo, object, fieldNode); - - // Serialization of more complex types - if (!PRIMITIVE_SERIALIZED) - { - if (fieldInfo->FieldType->IsSubclassOf(System::Enum::typeid)) - { - fieldNode = std::to_string(safe_cast(fieldInfo->GetValue(object))); - } - else if (fieldInfo->FieldType == System::String::typeid) - { - System::String^ str = safe_cast(fieldInfo->GetValue(object)); - fieldNode = Convert::ToNative(str); - } - else if (fieldInfo->FieldType == Vector2::typeid) - { - Vector2 vec = safe_cast(fieldInfo->GetValue(object)); - fieldNode.SetStyle(YAML::EmitterStyle::Flow); - fieldNode.push_back(vec.x); - fieldNode.push_back(vec.y); - } - else if (fieldInfo->FieldType == Vector3::typeid) - { - Vector3 vec = safe_cast(fieldInfo->GetValue(object)); - fieldNode.SetStyle(YAML::EmitterStyle::Flow); - fieldNode.push_back(vec.x); - fieldNode.push_back(vec.y); - fieldNode.push_back(vec.z); - } - else if (fieldInfo->FieldType == GameObject::typeid) - { - GameObject gameObj = safe_cast(fieldInfo->GetValue(object)); - fieldNode = gameObj ? gameObj.GetEntity() : MAX_EID; - } - else // Not any of the supported types - { - Debug::LogWarning(Convert::ToNative(System::String::Format - ( - "[ReflectionUtilities] Failed to parse \"{0}\" of \"{1}\" type for serialization.", - fieldInfo->Name, fieldInfo->FieldType) - )); - return; - } - } - - // Store the field into YAML - yamlNode[Convert::ToNative(fieldInfo->Name)] = fieldNode; + return IsList(fieldInfo->FieldType); } - void ReflectionUtilities::writeYamlIntoField(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node) + bool ReflectionUtilities::IsList(System::Type^ type) { - if (fieldAssignYaml (fieldInfo, object, node) || - fieldAssignYaml (fieldInfo, object, node) || - fieldAssignYaml (fieldInfo, object, node) || - fieldAssignYaml(fieldInfo, object, node) || - fieldAssignYaml(fieldInfo, object, node) || - fieldAssignYaml(fieldInfo, object, node) || - fieldAssignYaml (fieldInfo, object, node) || - fieldAssignYaml (fieldInfo, object, node) || - fieldAssignYaml (fieldInfo, object, node) || - fieldAssignYaml (fieldInfo, object, node)) - { - return; - } - else if (fieldInfo->FieldType->IsSubclassOf(System::Enum::typeid)) - { - fieldInfo->SetValue(object, node.as()); - } - else if (fieldInfo->FieldType == System::String::typeid) - { - fieldInfo->SetValue(object, Convert::ToCLI(node.as())); - } - else if (fieldInfo->FieldType == Vector2::typeid) - { - if (node.IsSequence() && node.size() == 2) - { - Vector2 vec; - vec.x = node[0].as(); - vec.y = node[1].as(); - fieldInfo->SetValue(object, vec); - } - else - { - Debug::LogWarning - ( - System::String::Format("[ReflectionUtilities] Invalid YAML Node provided for deserialization of a Vector2 \"{0}\" field in \"{1}\" script.", - fieldInfo->Name, object->GetType()->FullName) - ); - } - } - else if (fieldInfo->FieldType == Vector3::typeid) - { - if (node.IsSequence() && node.size() == 3) - { - Vector3 vec; - vec.x = node[0].as(); - vec.y = node[1].as(); - vec.z = node[2].as(); - fieldInfo->SetValue(object, vec); - } - else - { - Debug::LogWarning - ( - System::String::Format("[ReflectionUtilities] Invalid YAML Node provided for deserialization of a Vector3 \"{0}\" field in \"{1}\" script.", - fieldInfo->Name, object->GetType()->FullName) - ); - } - } - else if (fieldInfo->FieldType == GameObject::typeid) - { - const uint32_t EID = node.as(); - fieldInfo->SetValue(object, EID == MAX_EID ? GameObject() : GameObject(EID)); - } - else // Not any of the supported types - { - Debug::LogWarning(Convert::ToNative(System::String::Format - ( - "[ReflectionUtilities] Failed to parse \"{0}\" of \"{1}\" type for deserialisation.", - fieldInfo->Name, fieldInfo->FieldType) - )); - } + return type->IsGenericType + && type->GetGenericTypeDefinition() == System::Collections::Generic::List::typeid->GetGenericTypeDefinition(); } } diff --git a/SHADE_Managed/src/Serialisation/ReflectionUtilities.h++ b/SHADE_Managed/src/Serialisation/ReflectionUtilities.h++ deleted file mode 100644 index 7c39232a..00000000 --- a/SHADE_Managed/src/Serialisation/ReflectionUtilities.h++ +++ /dev/null @@ -1,55 +0,0 @@ -/************************************************************************************//*! -\file ReflectionUtilities.h++ -\author Tng Kah Wei, kahwei.tng, 390009620 -\par email: kahwei.tng\@digipen.edu -\date Sep 16, 2022 -\brief Contains the definition of the template functions of the managed - ReflectionUtilities static class. - - Note: This file is written in C++17/CLI. - -Copyright (C) 2022 DigiPen Institute of Technology. -Reproduction or disclosure of this file or its contents without the prior written consent -of DigiPen Institute of Technology is prohibited. -*//*************************************************************************************/ -#pragma once - -// Primary Header -#include "ReflectionUtilities.hxx" - -namespace SHADE -{ - /*---------------------------------------------------------------------------------*/ - /* Serialization Helper Functions */ - /*---------------------------------------------------------------------------------*/ - template - bool ReflectionUtilities::fieldInsertYaml(System::Reflection::FieldInfo^ fieldInfo, System::Object^ object, YAML::Node& fieldNode) - { - if (fieldInfo->FieldType == FieldType::typeid) - { - const FieldType VALUE = safe_cast(fieldInfo->GetValue(object)); - fieldNode = static_cast(VALUE); - return true; - } - - return false; - } - - template - bool ReflectionUtilities::fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node) - { - return fieldAssignYaml>(fieldInfo, object, node); - } - - template - bool ReflectionUtilities::fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node) - { - if (fieldInfo->FieldType == FieldType::typeid) - { - fieldInfo->SetValue(object, node.as()); - return true; - } - - return false; - } -} diff --git a/SHADE_Managed/src/Serialisation/ReflectionUtilities.hxx b/SHADE_Managed/src/Serialisation/ReflectionUtilities.hxx index 403c913c..ae66cc34 100644 --- a/SHADE_Managed/src/Serialisation/ReflectionUtilities.hxx +++ b/SHADE_Managed/src/Serialisation/ReflectionUtilities.hxx @@ -13,9 +13,6 @@ of DigiPen Institute of Technology is prohibited. *//*************************************************************************************/ #pragma once -// External Dependencies -#include - namespace SHADE { /// @@ -42,40 +39,17 @@ namespace SHADE /// True if the specified field is a candidate for serialisation. /// static bool FieldIsSerialisable(System::Reflection::FieldInfo^ fieldInfo); - - /*-----------------------------------------------------------------------------*/ - /* Serialisation Functions */ - /*-----------------------------------------------------------------------------*/ /// - /// Creates a JSON node that represents the specified object and its associated - /// serialisable fields. Public fields and fields marked with the SerialiseField - /// attribute will be serialised. + /// Checks if the specified field is a generic List. /// - /// The object to serialise. - static void Serialise(System::Object^ object, YAML::Node& yamlNode); + /// The field to check. + /// True if fieldInfo is describing a generic List. + static bool FieldIsList(System::Reflection::FieldInfo^ fieldInfo); /// - /// Deserialises a YAML node that contains a map of Scripts and copies the - /// deserialised data into the specified object if there are matching fields. + /// Checks if the specified type is a generic List type. /// - /// - /// The JSON string that contains the data to copy into this Script object. - /// - /// The object to copy deserialised data into. - static void Deserialise(System::Object^ object, YAML::Node& yamlNode); - - private: - /*-----------------------------------------------------------------------------*/ - /* Serialization Helper Functions */ - /*-----------------------------------------------------------------------------*/ - static void writeFieldIntoYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& yamlNode); - template - static bool fieldInsertYaml(System::Reflection::FieldInfo^ fieldInfo, System::Object^ object, YAML::Node& fieldNode); - static void writeYamlIntoField(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node); - template - static bool fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node); - template - static bool fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node); + /// The type to check. + /// True if type is a generic List. + static bool IsList(System::Type^ type); }; -} - -#include "ReflectionUtilities.h++" \ No newline at end of file +} \ No newline at end of file diff --git a/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx b/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx new file mode 100644 index 00000000..147591a5 --- /dev/null +++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx @@ -0,0 +1,263 @@ +/************************************************************************************//*! +\file SerialisationUtilities.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 6, 2021 +\brief Contains the definition of the functions for the SerialisationUtilities + managed static class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2021 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +// Precompiled Headers +#include "SHpch.h" +// Primary Header +#include "Serialisation/SerialisationUtilities.hxx" +// Project Includes +#include "ReflectionUtilities.hxx" + +/*-------------------------------------------------------------------------------------*/ +/* File-Level Constants */ +/*-------------------------------------------------------------------------------------*/ +static const std::string_view SCRIPT_TYPE_YAMLTAG = "Type"; + +/*-------------------------------------------------------------------------------------*/ +/* Function Definitions */ +/*-------------------------------------------------------------------------------------*/ +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Serialisation Functions */ + /*---------------------------------------------------------------------------------*/ + void SerialisationUtilities::Serialise(System::Object^ object, YAML::Node& scriptListNode) + { + using namespace System::Reflection; + + // Create YAML object + YAML::Node scriptNode; + scriptNode.SetStyle(YAML::EmitterStyle::Block); + scriptNode[SCRIPT_TYPE_YAMLTAG.data()] = Convert::ToNative(object->GetType()->FullName); + + // Get all fields + System::Collections::Generic::IEnumerable^ fields = ReflectionUtilities::GetInstanceFields(object); + for each (FieldInfo^ field in fields) + { + // Ignore private and non-SerialiseField + if (!ReflectionUtilities::FieldIsSerialisable(field)) + continue; + + // Serialise + writeFieldIntoYaml(field, object, scriptNode); + } + + scriptListNode.push_back(scriptNode); + } + void SerialisationUtilities::Deserialise(Object^ object, YAML::Node& yamlNode) + { + using namespace System::Reflection; + + // Load the YAML + if (!yamlNode.IsMap()) + { + // Invalid + Debug::LogError + ( + System::String::Format("[SerialisationUtilities] Invalid YAML Node provided for deserialization of \"{0}\" script.", + object->GetType()->FullName) + ); + return; + } + // Get all fields + 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]) + { + writeYamlIntoField(field, object, yamlNode[FIELD_NAME]); + } + } + } + /*---------------------------------------------------------------------------------*/ + /* Serialization Helper Functions */ + /*---------------------------------------------------------------------------------*/ + void SerialisationUtilities::writeFieldIntoYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& yamlNode) + { + // Field YAML Node + YAML::Node fieldNode; + + // Retrieve string for the YAML + const bool PRIMITIVE_SERIALIZED = fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode) || + fieldInsertYaml(fieldInfo, object, fieldNode); + + // Serialization of more complex types + if (!PRIMITIVE_SERIALIZED) + { + if (ReflectionUtilities::FieldIsList(fieldInfo)) + { + System::Type^ listType = fieldInfo->FieldType->GenericTypeArguments[0]; + System::Collections::IList^ iList = safe_cast(fieldInfo->GetValue(object)); + + + fieldNode.SetStyle(YAML::EmitterStyle::Block); + for (int i = 0; i < iList->Count; ++i) + { + YAML::Node elemNode; + if (varInsertYaml(iList[i], elemNode)) + { + fieldNode.push_back(elemNode); + } + else + { + Debug::LogWarning(Convert::ToNative(System::String::Format + ( + "[SerialisationUtilities] Failed to parse element # {2} of \"{0}\" of \"{1}\" type for serialization.", + fieldInfo->Name, fieldInfo->FieldType, i) + )); + } + } + } + else // Not any of the supported types + { + Debug::LogWarning(Convert::ToNative(System::String::Format + ( + "[SerialisationUtilities] Failed to parse \"{0}\" of \"{1}\" type for serialization.", + fieldInfo->Name, fieldInfo->FieldType) + )); + return; + } + } + + // Store the field into YAML + yamlNode[Convert::ToNative(fieldInfo->Name)] = fieldNode; + } + + bool SerialisationUtilities::varInsertYaml(System::Object^ object, YAML::Node& fieldNode) + { + const bool INSERTED = + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode) || + varInsertYamlInternal(object, fieldNode); + return INSERTED; + } + + /*---------------------------------------------------------------------------------*/ + /* Deserialization Helper Functions */ + /*---------------------------------------------------------------------------------*/ + bool SerialisationUtilities::writeYamlIntoField(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node) + { + const bool ASSIGNED = + fieldAssignYaml (fieldInfo, object, node) || + fieldAssignYaml (fieldInfo, object, node) || + fieldAssignYaml (fieldInfo, object, node) || + fieldAssignYaml(fieldInfo, object, node) || + fieldAssignYaml(fieldInfo, object, node) || + fieldAssignYaml(fieldInfo, object, node) || + fieldAssignYaml (fieldInfo, object, node) || + fieldAssignYaml (fieldInfo, object, node) || + fieldAssignYaml (fieldInfo, object, node) || + fieldAssignYaml (fieldInfo, object, node) || + fieldAssignYaml (fieldInfo, object, node) || + fieldAssignYaml(fieldInfo, object, node) || + fieldAssignYaml (fieldInfo, object, node) || + fieldAssignYaml (fieldInfo, object, node) || + fieldAssignYaml (fieldInfo, object, node); + if (!ASSIGNED) + { + if (ReflectionUtilities::FieldIsList(fieldInfo)) + { + System::Type^ elemType = fieldInfo->FieldType->GenericTypeArguments[0]; + System::Collections::IList^ iList = safe_cast(fieldInfo->GetValue(object)); + if (node.IsSequence()) + { + // Get list size + const int LIST_SIZE = static_cast(node.size()); + if (LIST_SIZE > 0) + { + // Get list type + array^ typeList = gcnew array{ elemType }; + System::Type^ listType = System::Collections::Generic::List::typeid->GetGenericTypeDefinition()->MakeGenericType(typeList); + // Create a list of the specified type + array^ params = gcnew array{ node.size() }; + System::Collections::IList^ list = safe_cast + ( + System::Activator::CreateInstance(listType, params) + ); + + // Populate the list + for (int i = 0; i < LIST_SIZE; ++i) + { + // Create the object + System::Object^ obj = System::Activator::CreateInstance(elemType); + + // Set it's value + if (varAssignYaml(obj, node[i])) + { + list->Add(obj); + } + } + fieldInfo->SetValue(object, list); + } + } + + return true; + } + } + + return ASSIGNED; + } + + bool SerialisationUtilities::varAssignYaml(System::Object^% object, YAML::Node& node) + { + const bool DESERIALISED = + varAssignYamlInternal (object, node) || + varAssignYamlInternal (object, node) || + varAssignYamlInternal (object, node) || + varAssignYamlInternal(object, node) || + varAssignYamlInternal(object, node) || + varAssignYamlInternal(object, node) || + varAssignYamlInternal (object, node) || + varAssignYamlInternal (object, node) || + varAssignYamlInternal (object, node) || + varAssignYamlInternal (object, node) || + varAssignYamlInternal (object, node) || + varAssignYamlInternal(object, node) || + varAssignYamlInternal (object, node) || + varAssignYamlInternal (object, node) || + varAssignYamlInternal (object, node); + return DESERIALISED; + } +} diff --git a/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ new file mode 100644 index 00000000..c1728fe6 --- /dev/null +++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ @@ -0,0 +1,188 @@ +/************************************************************************************//*! +\file SerialisationUtilities.h++ +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Sep 16, 2022 +\brief Contains the definition of the template functions of the managed + ReflectionUtilities static class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +// Primary Header +#include "SerialisationUtilities.hxx" +// Project Includes +#include "Utility/Convert.hxx" +#include "Utility/Debug.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Serialization Helper Functions */ + /*---------------------------------------------------------------------------------*/ + template + bool SerialisationUtilities::fieldInsertYaml(System::Reflection::FieldInfo^ fieldInfo, System::Object^ object, YAML::Node& fieldNode) + { + Debug::Log(FieldType::typeid->Name); + return varInsertYamlInternal(fieldInfo->GetValue(object), fieldNode); + } + template + bool SerialisationUtilities::varInsertYamlInternal(System::Object^ object, YAML::Node& fieldNode) + { + if constexpr (std::is_same_v) + { + if (object->GetType()->IsSubclassOf(System::Enum::typeid)) + { + fieldNode = std::to_string(safe_cast(object)); + return true; + } + } + else if constexpr (std::is_same_v) + { + if (object->GetType() == System::String::typeid) + { + System::String^ str = safe_cast(object); + fieldNode = Convert::ToNative(str); + return true; + } + } + else if constexpr (std::is_same_v) + { + if (object->GetType() == Vector2::typeid) + { + Vector2 vec = safe_cast(object); + fieldNode.SetStyle(YAML::EmitterStyle::Flow); + fieldNode.push_back(vec.x); + fieldNode.push_back(vec.y); + return true; + } + } + else if constexpr (std::is_same_v) + { + if (object->GetType() == Vector3::typeid) + { + Vector3 vec = safe_cast(object); + fieldNode.SetStyle(YAML::EmitterStyle::Flow); + fieldNode.push_back(vec.x); + fieldNode.push_back(vec.y); + fieldNode.push_back(vec.z); + return true; + } + } + else if constexpr (std::is_same_v) + { + if (object->GetType() == GameObject::typeid) + { + GameObject gameObj = safe_cast(object); + fieldNode = gameObj ? gameObj.GetEntity() : MAX_EID; + return true; + } + } + else + { + if (object->GetType() == FieldType::typeid) + { + FieldType value = safe_cast(object); + fieldNode = static_cast(value); + return true; + } + } + + return false; + } + + /*---------------------------------------------------------------------------------*/ + /* Deserialization Helper Functions */ + /*---------------------------------------------------------------------------------*/ + template + bool SerialisationUtilities::fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node) + { + System::Object^ valueObj = fieldInfo->GetValue(object); + if (varAssignYamlInternal(valueObj, node)) + { + fieldInfo->SetValue(object, valueObj); + return true; + } + + return false; + } + + template + bool SerialisationUtilities::varAssignYamlInternal(System::Object^% object, YAML::Node& node) + { + if constexpr (std::is_same_v) + { + if (object->GetType()->IsSubclassOf(System::Enum::typeid)) + { + object = node.as(); + return true; + } + } + else if constexpr (std::is_same_v) + { + if (ReflectionUtilities::FieldIsList(fieldInfo)) + { + System::Collections::IList^ iList = safe_cast(object); + object = gcnew + if (node.IsSequence() ) + + } + } + else + { + if (object->GetType() == FieldType::typeid) + { + if constexpr (std::is_same_v) + { + object = Convert::ToCLI(node.as()); + } + else if constexpr (std::is_same_v) + { + if (node.IsSequence() && node.size() == 2) + { + Vector2 vec; + vec.x = node[0].as(); + vec.y = node[1].as(); + object = vec; + } + else + { + return false; + } + } + else if constexpr (std::is_same_v) + { + if (node.IsSequence() && node.size() == 3) + { + Vector3 vec; + vec.x = node[0].as(); + vec.y = node[1].as(); + vec.z = node[2].as(); + object = vec; + } + else + { + return false; + } + } + else if constexpr (std::is_same_v) + { + const uint32_t EID = node.as(); + object = (EID == MAX_EID ? GameObject() : GameObject(EID)); + } + else + { + object = node.as(); + } + return true; + } + } + + return false; + } +} diff --git a/SHADE_Managed/src/Serialisation/SerialisationUtilities.hxx b/SHADE_Managed/src/Serialisation/SerialisationUtilities.hxx new file mode 100644 index 00000000..5b6fc69e --- /dev/null +++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.hxx @@ -0,0 +1,75 @@ +/************************************************************************************//*! +\file SerialisationUtilities.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 6, 2021 +\brief Contains the definition of the managed SerialisationUtilities static + class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2021 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +// External Dependencies +#include +// Project Includes +#include "Math/Vector2.hxx" +#include "Math/Vector3.hxx" +#include "Engine/GameObject.hxx" + +namespace SHADE +{ + /// + /// Contains useful static functions for working with Serialisation of Managed data. + /// + private ref class SerialisationUtilities abstract sealed + { + public: + /*-----------------------------------------------------------------------------*/ + /* Serialisation Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Creates a JSON node that represents the specified object and its associated + /// serialisable fields. Public fields and fields marked with the SerialiseField + /// attribute will be serialised. + /// + /// The object to serialise. + static void Serialise(System::Object^ object, YAML::Node& yamlNode); + /// + /// Deserialises a YAML node that contains a map of Scripts and copies the + /// deserialised data into the specified object if there are matching fields. + /// + /// + /// The JSON string that contains the data to copy into this Script object. + /// + /// The object to copy deserialised data into. + static void Deserialise(System::Object^ object, YAML::Node& yamlNode); + + private: + /*-----------------------------------------------------------------------------*/ + /* Serialization Helper Functions */ + /*-----------------------------------------------------------------------------*/ + static void writeFieldIntoYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& yamlNode); + template + static bool fieldInsertYaml(System::Reflection::FieldInfo^ fieldInfo, System::Object^ object, YAML::Node& fieldNode); + static bool varInsertYaml(System::Object^ object, YAML::Node& fieldNode); + template + static bool varInsertYamlInternal(System::Object^ object, YAML::Node& fieldNode); + + /*-----------------------------------------------------------------------------*/ + /* Deserialization Helper Functions */ + /*-----------------------------------------------------------------------------*/ + static bool writeYamlIntoField(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node); + template + static bool fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node); + static bool varAssignYaml(System::Object^% object, YAML::Node& node); + template> + static bool varAssignYamlInternal(System::Object^% object, YAML::Node& node); + }; +} + +#include "SerialisationUtilities.h++" \ No newline at end of file diff --git a/SHADE_Managed/src/Utility/Convert.hxx b/SHADE_Managed/src/Utility/Convert.hxx index 666b5062..4d0c5b59 100644 --- a/SHADE_Managed/src/Utility/Convert.hxx +++ b/SHADE_Managed/src/Utility/Convert.hxx @@ -152,6 +152,40 @@ namespace SHADE }; + /// + /// Checks if the specified type is matching between native C++ and the managed type. + /// + /// Type to check. + template + struct IsPrimitiveTypeMatches : public std::integral_constant + < + bool, + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> || + std::is_same_v> + > + {}; + /// + /// Short hand for IsPrimitiveTypeMatches::value + /// + /// Type to check. + template + inline constexpr bool IsPrimitiveTypeMatches_V = IsPrimitiveTypeMatches::value; + /// /// Type Transformer for managed types to native types. /// @@ -163,6 +197,7 @@ namespace SHADE { public: using Value = void; + static bool IsDefined() { return is_same_v; } }; template<> struct ToNativeType { using Value = int16_t; }; template<> struct ToNativeType { using Value = int32_t; }; @@ -193,19 +228,20 @@ namespace SHADE template struct ToManagedType { - public: + public: using Value = void; + static bool IsDefined() { return is_same_v; } }; - template<> struct ToManagedType { using Value = System::Byte; }; - template<> struct ToManagedType { using Value = System::Int16; }; - template<> struct ToManagedType { using Value = System::Int32; }; - template<> struct ToManagedType { using Value = System::Int64; }; + template<> struct ToManagedType { using Value = System::Byte; }; + template<> struct ToManagedType { using Value = System::Int16; }; + template<> struct ToManagedType { using Value = System::Int32; }; + template<> struct ToManagedType { using Value = System::Int64; }; template<> struct ToManagedType { using Value = System::UInt16; }; template<> struct ToManagedType { using Value = System::UInt32; }; template<> struct ToManagedType { using Value = System::UInt64; }; - template<> struct ToManagedType { using Value = bool; }; - template<> struct ToManagedType { using Value = double; }; - template<> struct ToManagedType { using Value = float; }; + template<> struct ToManagedType { using Value = bool; }; + template<> struct ToManagedType { using Value = double; }; + template<> struct ToManagedType { using Value = float; }; /// /// Alias for ToManagedType::Value