From beeeae99ca500c98887210ff05033205c5a4cf36 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 3 Mar 2023 18:53:59 +0800 Subject: [PATCH] SHRawAnimInspector can now generate incomplete animation clips --- Assets/Animation Clips/Main | Bin 0 -> 289 bytes .../SHRawAnimInspector.cpp | 86 ++++++++++++++++-- .../SHRawAnimInspector.h | 15 ++- .../src/Resource/SHResourceManager.hpp | 6 +- 4 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 Assets/Animation Clips/Main diff --git a/Assets/Animation Clips/Main b/Assets/Animation Clips/Main new file mode 100644 index 0000000000000000000000000000000000000000..ac5a59fa92d3f11764856a8a751c2dab31a24899 GIT binary patch literal 289 vcmeZE%uTgY@J-CjWB5060WX)QiYxGN literal 0 HcmV?d00001 diff --git a/SHADE_Engine/src/Editor/EditorWindow/RawAnimationInspector/SHRawAnimInspector.cpp b/SHADE_Engine/src/Editor/EditorWindow/RawAnimationInspector/SHRawAnimInspector.cpp index 0734121e..0dc44bc4 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/RawAnimationInspector/SHRawAnimInspector.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/RawAnimationInspector/SHRawAnimInspector.cpp @@ -24,6 +24,7 @@ of DigiPen Institute of Technology is prohibited. #include "Resource/SHResourceManager.h" #include "Editor/EditorWindow/SHEditorWindowManager.h" #include "Editor/SHEditorUI.h" +#include "Assets/SHAssetManager.h" namespace SHADE { @@ -36,7 +37,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* SHAnimClipCreatePrompt - Lifecycle Functions */ /*---------------------------------------------------------------------------------*/ - void SHAnimClipCreatePrompt::Init(Handle rawAnim) + void SHAnimClipCreatePrompt::Init(Handle rawAnim, std::function onClose) { rawAnimation = rawAnim; @@ -47,6 +48,9 @@ namespace SHADE firstIndex = 0; lastIndex = rawAnimation->GetTotalFrames(); } + + // Assign callback + this->onClose = onClose; } void SHAnimClipCreatePrompt::Draw() @@ -71,10 +75,17 @@ namespace SHADE if (ImGui::Button("Save")) { // Generate new asset - + const AssetID NEW_ASSET_ID = SHAssetManager::CreateNewAsset(AssetType::ANIM_CLIP, newAssetName); + auto animClip = SHAssetManager::GetData(NEW_ASSET_ID); + animClip->animRawDataAssetId = SHResourceManager::GetAssetID(rawAnimation).value_or(INVALID_ASSET_ID); + animClip->firstIndex = firstIndex; + animClip->lastIndex = lastIndex; + SHAssetManager::SaveAsset(NEW_ASSET_ID); // Close isOpen = false; + if (onClose) + onClose(NEW_ASSET_ID); ImGui::CloseCurrentPopup(); } } @@ -84,6 +95,8 @@ namespace SHADE { // Close isOpen = false; + if (onClose) + onClose(INVALID_ASSET_ID); ImGui::CloseCurrentPopup(); } ImGui::EndPopup(); @@ -114,26 +127,62 @@ namespace SHADE // Draw if (Begin()) { - // Ignore if no asset if (currRawAnim) { drawMenuBar(); - - // Button to add a new clip if (ImGui::Button(std::format("{} Add", ICON_MD_ADD).data())) { auto prompt = SHEditorWindowManager::GetPopupWindow(); - prompt->Init(currRawAnim); + prompt->Init(currRawAnim, [this](AssetID createdAssetId) + { + if (createdAssetId != INVALID_ASSET_ID) + { + childAnimClips.emplace_back(SHResourceManager::LoadOrGet(createdAssetId)); + } + }); prompt->isOpen = true; } // Render all clips + for (auto animClip : childAnimClips) + { + bool changed = false; + std::optional animClipName = SHResourceManager::GetAssetName(animClip); + + int firstIndex = animClip->GetStartFrameIndex(); + int endIndex = animClip->GetEndFrameIndex(); + + ImGui::Text(animClipName.has_value() ? animClipName.value().c_str() : ""); + ImGui::SameLine(); + SHEditorUI::PushID(0); + changed |= SHEditorUI::InputInt("", firstIndex); + SHEditorUI::PopID(); + ImGui::SameLine(); + ImGui::Text(" - "); + ImGui::SameLine(); + SHEditorUI::PushID(1); + changed |= SHEditorUI::InputInt("", endIndex); + SHEditorUI::PopID(); + + // If there's a change we need to commit changes + if (changed) + { + auto assetId = SHResourceManager::GetAssetID(animClip); + if (assetId.has_value()) + { + auto animAsset = SHAssetManager::GetData(assetId.value()); + animAsset->firstIndex = firstIndex; + animAsset->lastIndex = endIndex; + SHAssetManager::SaveAsset(assetId.value()); + } + } + } } + ImGui::End(); } - ImGui::End(); } void SHRawAnimInspector::Exit() @@ -147,6 +196,12 @@ namespace SHADE void SHRawAnimInspector::Open(AssetID assetId) { currRawAnim = SHResourceManager::LoadOrGet(assetId); + + // Load anim clips + if (currRawAnim) + { + childAnimClips = getChildAnimClips(assetId); + } } /*-----------------------------------------------------------------------------------*/ @@ -167,4 +222,21 @@ namespace SHADE ImGui::EndMenuBar(); } } + + std::vector> SHRawAnimInspector::getChildAnimClips(AssetID rawAnimId) + { + std::vector> animClips; + + const auto ALL_ANIM_CLIPS = SHAssetManager::GetAllRecordOfType(AssetType::ANIM_CLIP); + for (auto asset : ALL_ANIM_CLIPS) + { + const SHAnimClipAsset* ANIM_CLIP = SHAssetManager::GetData(asset.id); + if (ANIM_CLIP->animRawDataAssetId == rawAnimId) + { + animClips.emplace_back(SHResourceManager::LoadOrGet(asset.id)); + } + } + + return animClips; + } } \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/EditorWindow/RawAnimationInspector/SHRawAnimInspector.h b/SHADE_Engine/src/Editor/EditorWindow/RawAnimationInspector/SHRawAnimInspector.h index 1b3c4855..b6857568 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/RawAnimationInspector/SHRawAnimInspector.h +++ b/SHADE_Engine/src/Editor/EditorWindow/RawAnimationInspector/SHRawAnimInspector.h @@ -20,6 +20,16 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { + /*-----------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*-----------------------------------------------------------------------------------*/ + struct SHAnimClipAsset; + class SHRawAnimation; + class SHAnimationClip; + + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ /// /// Prompt for creating an animation clip. Init() must be called to pass in the correct /// SHRawAnimation that the created clip will use. @@ -35,7 +45,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Lifecycle Functions */ /*---------------------------------------------------------------------------------*/ - void Init(Handle rawAnim); + void Init(Handle rawAnim, std::function onClose = nullptr); void Draw() override; private: @@ -46,6 +56,7 @@ namespace SHADE int firstIndex = 0; int lastIndex = 0; Handle rawAnimation; + std::function onClose; }; /// @@ -77,10 +88,12 @@ namespace SHADE /* Data Members */ /*---------------------------------------------------------------------------------*/ Handle currRawAnim; + std::vector> childAnimClips; /*---------------------------------------------------------------------------------*/ /* Helper Functions */ /*---------------------------------------------------------------------------------*/ void drawMenuBar(); + std::vector> getChildAnimClips(AssetID rawAnimId); }; } diff --git a/SHADE_Engine/src/Resource/SHResourceManager.hpp b/SHADE_Engine/src/Resource/SHResourceManager.hpp index be6cc5e6..f2469833 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.hpp +++ b/SHADE_Engine/src/Resource/SHResourceManager.hpp @@ -368,9 +368,9 @@ namespace SHADE loadedAssetData.emplace_back(assetId); return resourceHub.Create ( - LoadOrGet(assetData->animRawDataAssetId), - assetData->firstIndex, - assetData->lastIndex + LoadOrGet(assetData.animRawDataAssetId), + assetData.firstIndex, + assetData.lastIndex ); } else if constexpr (std::is_same_v)