diff --git a/Plugins/Cog/Source/CogEngine/Private/CogEngineWindow_Levels.cpp b/Plugins/Cog/Source/CogEngine/Private/CogEngineWindow_Levels.cpp new file mode 100644 index 0000000..9218214 --- /dev/null +++ b/Plugins/Cog/Source/CogEngine/Private/CogEngineWindow_Levels.cpp @@ -0,0 +1,182 @@ +#include "CogEngineWindow_Levels.h" + +#include "CogImguiHelper.h" +#include "CogWindowWidgets.h" +#include "AssetRegistry/AssetRegistryModule.h" + +//-------------------------------------------------------------------------------------------------------------------------- +void FCogEngineWindow_Levels::RenderHelp() +{ + ImGui::Text("This window can be used to load levels."); +} + +//-------------------------------------------------------------------------------------------------------------------------- +void FCogEngineWindow_Levels::Initialize() +{ + Super::Initialize(); + + bHasMenu = true; + Config = GetConfig(); + GetAllLevels(UnsortedLevels); + + RefreshSorting(); +} + +//-------------------------------------------------------------------------------------------------------------------------- + void FCogEngineWindow_Levels::GetAllLevels(TArray& OutLevels) +{ + const FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked("AssetRegistry"); + IAssetRegistry& AssetRegistry = AssetRegistryModule.Get(); + + AssetRegistry.SearchAllAssets(true); + + FARFilter AssetFilter; + AssetFilter.ClassPaths.Add(UWorld::StaticClass()->GetClassPathName()); + AssetFilter.bRecursiveClasses = true; + + AssetRegistry.GetAssets(AssetFilter, OutLevels); + + RefreshSorting(); +} + +//-------------------------------------------------------------------------------------------------------------------------- +void FCogEngineWindow_Levels::RefreshSorting() +{ + Levels = UnsortedLevels; + + if (Config->SortByName) + { + if (Config->ShowPath) + { + Levels.Sort([](const FAssetData& InA, const FAssetData& InB) { return InA.PackagePath.Compare(InB.PackagePath) < 0; }); + } + else + { + Levels.Sort([](const FAssetData& InA, const FAssetData& InB) { return InA.AssetName.Compare(InB.AssetName) < 0; }); + } + } +} + +//-------------------------------------------------------------------------------------------------------------------------- +void FCogEngineWindow_Levels::RenderMenu() +{ + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("Options")) + { + if (ImGui::Checkbox("Sort by Name", &Config->SortByName)) + { + RefreshSorting(); + } + + if (ImGui::Checkbox("Show Path", &Config->ShowPath)) + { + RefreshSorting(); + } + + ImGui::EndMenu(); + } + + FCogWindowWidgets::SearchBar("##Filter", Filter); + + ImGui::EndMenuBar(); + } +} + +//-------------------------------------------------------------------------------------------------------------------------- +void FCogEngineWindow_Levels::RenderContent() +{ + Super::RenderContent(); + + RenderMenu(); + + const float FooterHeight = ImGui::GetFrameHeightWithSpacing(); + const ImVec2 Size = IsWindowRenderedInMainMenu() + ? ImVec2(0.f, ImGui::GetFontSize() * 10 - FooterHeight) + : ImVec2(0.f, ImGui::GetContentRegionAvail().y - FooterHeight); + + const bool Visible = ImGui::BeginChild("Levels", Size, 0, ImGuiWindowFlags_HorizontalScrollbar); + + if (Visible) + { + for (int32 i = 0; i < Levels.Num(); i++) + { + ImGui::PushID(i); + + const FAssetData& Asset = Levels[i]; + RenderLevel(i, Asset); + + ImGui::PopID(); + } + } + ImGui::EndChild(); + + const bool CanLoad = Levels.IsValidIndex(SelectedIndex); + if (CanLoad == false) + { + ImGui::BeginDisabled(); + } + if (ImGui::Button("Load", ImVec2(-1, 0))) + { + if (Levels.IsValidIndex(SelectedIndex)) + { + LoadLevel(Levels[SelectedIndex]); + } + } + if (CanLoad == false) + { + ImGui::EndDisabled(); + } +} + + +//-------------------------------------------------------------------------------------------------------------------------- +void FCogEngineWindow_Levels::RenderLevel(int32 InIndex, const FAssetData& InAsset) +{ + FString Label; + if (Config->ShowPath) + { + Label = InAsset.PackageName.ToString(); + } + else + { + Label = InAsset.AssetName.ToString(); + } + + const auto LabelStr = StringCast(*Label); + if (Filter.PassFilter(LabelStr.Get()) == false) + { return; } + + if (ImGui::Selectable(LabelStr.Get(), SelectedIndex == InIndex, ImGuiSelectableFlags_AllowDoubleClick)) + { + SelectedIndex = InIndex; + if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) + { + LoadLevel(InAsset); + } + } + + ImGui::SetItemTooltip(StringCast(*InAsset.PackagePath.ToString()).Get()); + + RenderLevelContextMenu(InIndex, InAsset); +} + +//-------------------------------------------------------------------------------------------------------------------------- +void FCogEngineWindow_Levels::LoadLevel(const FAssetData& InAsset) +{ + APlayerController* LocalPlayerController = GetLocalPlayerController(); + if (LocalPlayerController == nullptr) + { return; } + + LocalPlayerController->ConsoleCommand(FString::Printf(TEXT("Travel %s"), *InAsset.PackageName.ToString())); +} + +//-------------------------------------------------------------------------------------------------------------------------- +void FCogEngineWindow_Levels::RenderLevelContextMenu(int Index, const FAssetData& Asset) +{ + if (ImGui::BeginPopupContextItem()) + { + FCogWindowWidgets::BrowseToAssetButton(Asset); + ImGui::EndPopup(); + } +} diff --git a/Plugins/Cog/Source/CogEngine/Private/CogEngineWindow_Spawns.cpp b/Plugins/Cog/Source/CogEngine/Private/CogEngineWindow_Spawns.cpp index 4ea09a5..17c4562 100644 --- a/Plugins/Cog/Source/CogEngine/Private/CogEngineWindow_Spawns.cpp +++ b/Plugins/Cog/Source/CogEngine/Private/CogEngineWindow_Spawns.cpp @@ -5,14 +5,6 @@ #include "CogImguiHelper.h" #include "CogWindowWidgets.h" -//-------------------------------------------------------------------------------------------------------------------------- -void FCogEngineWindow_Spawns::Initialize() -{ - Super::Initialize(); - - Asset = GetAsset(); -} - //-------------------------------------------------------------------------------------------------------------------------- void FCogEngineWindow_Spawns::RenderHelp() { @@ -23,6 +15,14 @@ void FCogEngineWindow_Spawns::RenderHelp() ); } +//-------------------------------------------------------------------------------------------------------------------------- +void FCogEngineWindow_Spawns::Initialize() +{ + Super::Initialize(); + + Asset = GetAsset(); +} + //-------------------------------------------------------------------------------------------------------------------------- void FCogEngineWindow_Spawns::RenderContent() { diff --git a/Plugins/Cog/Source/CogEngine/Public/CogEngineWindow_Levels.h b/Plugins/Cog/Source/CogEngine/Public/CogEngineWindow_Levels.h new file mode 100644 index 0000000..7201231 --- /dev/null +++ b/Plugins/Cog/Source/CogEngine/Public/CogEngineWindow_Levels.h @@ -0,0 +1,69 @@ +#pragma once + +#include "CoreMinimal.h" +#include "CogWindow.h" +#include "CogEngineWindow_Levels.generated.h" + +class UCogEngineWindowConfig_LevelLoader; + +class COGENGINE_API FCogEngineWindow_Levels : public FCogWindow +{ + typedef FCogWindow Super; + +public: + + virtual void Initialize() override; + +protected: + + virtual void RenderHelp() override; + + virtual void RenderContent() override; + + virtual void RenderMenu(); + + virtual void RenderLevel(int32 InIndex, const FAssetData& InAsset); + + virtual void RenderLevelContextMenu(int Index, const FAssetData& Asset); + + virtual void GetAllLevels(TArray& OutLevels); + void RefreshSorting(); + + virtual void LoadLevel(const FAssetData& InAsset); + + +private: + + TArray Levels; + + TArray UnsortedLevels; + + int32 SelectedIndex = -1; + + TObjectPtr Config = nullptr; + + ImGuiTextFilter Filter; +}; + +//-------------------------------------------------------------------------------------------------------------------------- +UCLASS(Config = Cog) +class UCogEngineWindowConfig_LevelLoader : public UCogCommonConfig +{ + GENERATED_BODY() + +public: + + virtual void Reset() override + { + UCogCommonConfig::Reset(); + + SortByName = true; + ShowPath = false; + } + + UPROPERTY(Config) + bool SortByName = true; + + UPROPERTY(Config) + bool ShowPath = false; +}; diff --git a/Plugins/Cog/Source/CogWindow/Private/CogWindowWidgets.cpp b/Plugins/Cog/Source/CogWindow/Private/CogWindowWidgets.cpp index 38e8533..6c6bb76 100644 --- a/Plugins/Cog/Source/CogWindow/Private/CogWindowWidgets.cpp +++ b/Plugins/Cog/Source/CogWindow/Private/CogWindowWidgets.cpp @@ -1236,6 +1236,24 @@ bool FCogWindowWidgets::BrowseToAssetButton(const UObject* InAsset, const ImVec2 #endif } +//-------------------------------------------------------------------------------------------------------------------------- +bool FCogWindowWidgets::BrowseToAssetButton(const FAssetData& InAssetData, const ImVec2& InSize) +{ +#if WITH_EDITOR + + const bool result = ImGui::Button("Browse To Asset", InSize); + if (result) + { + IAssetTools::Get().SyncBrowserToAssets({ InAssetData }); + } + + return result; + +#else + return false; +#endif +} + //-------------------------------------------------------------------------------------------------------------------------- bool FCogWindowWidgets::BrowseToObjectAssetButton(const UObject* InObject, const ImVec2& InSize) { diff --git a/Plugins/Cog/Source/CogWindow/Public/CogWindowWidgets.h b/Plugins/Cog/Source/CogWindow/Public/CogWindowWidgets.h index 18e2d0d..f2c07eb 100644 --- a/Plugins/Cog/Source/CogWindow/Public/CogWindowWidgets.h +++ b/Plugins/Cog/Source/CogWindow/Public/CogWindowWidgets.h @@ -133,6 +133,8 @@ public: static void MenuItemShortcut(const char* Id, const FString& Text); static bool BrowseToAssetButton(const UObject* InAsset, const ImVec2& InSize = ImVec2(0, 0)); + + static bool BrowseToAssetButton(const FAssetData& InAssetData, const ImVec2& InSize = ImVec2(0, 0)); static bool BrowseToObjectAssetButton(const UObject* InObject, const ImVec2& InSize = ImVec2(0, 0)); diff --git a/Plugins/CogAll/Source/CogAll/Private/CogAll.cpp b/Plugins/CogAll/Source/CogAll/Private/CogAll.cpp index 8cb9d2d..a569e3f 100644 --- a/Plugins/CogAll/Source/CogAll/Private/CogAll.cpp +++ b/Plugins/CogAll/Source/CogAll/Private/CogAll.cpp @@ -18,6 +18,7 @@ #include "CogEngineWindow_DebugSettings.h" #include "CogEngineWindow_ImGui.h" #include "CogEngineWindow_Inspector.h" +#include "CogEngineWindow_Levels.h" #include "CogEngineWindow_LogCategories.h" #include "CogEngineWindow_Metrics.h" #include "CogEngineWindow_NetImgui.h" @@ -69,6 +70,8 @@ void Cog::AddAllWindows(UCogWindowManager& CogWindowManager) } }); + CogWindowManager.AddWindow("Engine.Levels"); + CogWindowManager.AddWindow("Engine.Log Categories"); CogWindowManager.AddWindow("Engine.Metrics"); diff --git a/Source/CogSample/CogSample.Build.cs b/Source/CogSample/CogSample.Build.cs index f2ecb5d..9118ff5 100644 --- a/Source/CogSample/CogSample.Build.cs +++ b/Source/CogSample/CogSample.Build.cs @@ -22,7 +22,7 @@ public class CogSample : ModuleRules "Niagara", }); - if (Target.Configuration != UnrealTargetConfiguration.Shipping) + if (Target.Configuration != UnrealTargetConfiguration.Shipping && Target.Type != TargetRules.TargetType.Server) { PublicDependencyModuleNames.AddRange(new string[] {