From 5c19a41cfe834a4a01cebcc4eab9083b24aa2254 Mon Sep 17 00:00:00 2001 From: Jakob Bouchard Date: Thu, 6 Feb 2025 14:30:47 -0500 Subject: [PATCH] Add render function for TMap and TSet --- .../Private/CogEngineWindow_Inspector.cpp | 113 +++++++++++++++++- .../Public/CogEngineWindow_Inspector.h | 6 +- 2 files changed, 116 insertions(+), 3 deletions(-) diff --git a/Plugins/Cog/Source/CogEngine/Private/CogEngineWindow_Inspector.cpp b/Plugins/Cog/Source/CogEngine/Private/CogEngineWindow_Inspector.cpp index cbbef3c..fe298dc 100644 --- a/Plugins/Cog/Source/CogEngine/Private/CogEngineWindow_Inspector.cpp +++ b/Plugins/Cog/Source/CogEngine/Private/CogEngineWindow_Inspector.cpp @@ -456,7 +456,7 @@ bool FCogEngineWindow_Inspector::RenderPropertyList(TArray& Pr } //-------------------------------------------------------------------------------------------------------------------------- -bool FCogEngineWindow_Inspector::RenderProperty(const FProperty* Property, uint8* PointerToValue, int IndexInArray) +bool FCogEngineWindow_Inspector::RenderProperty(const FProperty* Property, uint8* PointerToValue, int IndexInArray, const char* NameSuffix) { bool HasChanged = false; @@ -473,6 +473,11 @@ bool FCogEngineWindow_Inspector::RenderProperty(const FProperty* Property, uint8 if (IndexInArray != -1) { PropertyName = FString::Printf(TEXT("[%d]"), IndexInArray); + if (NameSuffix != nullptr) + { + PropertyName.Append(" "); + PropertyName.Append(NameSuffix); + } } else { @@ -635,6 +640,14 @@ bool FCogEngineWindow_Inspector::RenderProperty(const FProperty* Property, uint8 { HasChanged = RenderArray(ArrayProperty, PointerToValue, ShowChildren); } + else if (const FSetProperty* SetProperty = CastField(Property)) + { + HasChanged = RenderSet(SetProperty, PointerToValue, ShowChildren); + } + else if (const FMapProperty* MapProperty = CastField(Property)) + { + HasChanged = RenderMap(MapProperty, PointerToValue, ShowChildren); + } else if (const FDelegateProperty* DelegateProperty = CastField(Property)) { } @@ -936,7 +949,12 @@ bool FCogEngineWindow_Inspector::RenderArray(const FArrayProperty* ArrayProperty const int32 Num = Helper.Num(); ImGui::BeginDisabled(); - ImGui::Text("%s [%d]", TCHAR_TO_ANSI(*ArrayProperty->Inner->GetClass()->GetName()), Num); + FString ElementPropertyName = ArrayProperty->Inner->GetClass()->GetName(); + if (const FStructProperty* StructProperty = CastField(ArrayProperty->Inner)) + { + ElementPropertyName = StructProperty->Struct->GetStructCPPName(); + } + ImGui::Text("%s [%d]", StringCast(*ElementPropertyName).Get(), Num); ImGui::EndDisabled(); bool HasChanged = false; @@ -955,6 +973,75 @@ bool FCogEngineWindow_Inspector::RenderArray(const FArrayProperty* ArrayProperty return HasChanged; } +//-------------------------------------------------------------------------------------------------------------------------- +bool FCogEngineWindow_Inspector::RenderSet(const FSetProperty* SetProperty, uint8* PointerToValue, bool ShowChildren) +{ + FScriptSetHelper Helper(SetProperty, PointerToValue); + const int32 Num = Helper.Num(); + + ImGui::BeginDisabled(); + FString ElementPropertyName = SetProperty->GetElementProperty()->GetClass()->GetName(); + if (const FStructProperty* StructProperty = CastField(SetProperty->GetElementProperty())) + { + ElementPropertyName = StructProperty->Struct->GetStructCPPName(); + } + ImGui::Text("%s {%d}", StringCast(*ElementPropertyName).Get(), Num); + ImGui::EndDisabled(); + + bool HasChanged = false; + + if (ShowChildren) + { + for (int32 i = 0; i < Num; ++i) + { + ImGui::PushID(i); + HasChanged |= RenderProperty(SetProperty->GetElementProperty(), Helper.GetElementPtr(i), i); + ImGui::PopID(); + } + ImGui::TreePop(); + } + + return HasChanged; +} + +//-------------------------------------------------------------------------------------------------------------------------- +bool FCogEngineWindow_Inspector::RenderMap(const FMapProperty* MapProperty, uint8* PointerToValue, bool ShowChildren) +{ + FScriptMapHelper Helper(MapProperty, PointerToValue); + const int32 Num = Helper.Num(); + + ImGui::BeginDisabled(); + FString KeyPropertyName = MapProperty->GetKeyProperty()->GetClass()->GetName(); + if (const FStructProperty* StructProperty = CastField(MapProperty->GetKeyProperty())) + { + KeyPropertyName = StructProperty->Struct->GetStructCPPName(); + } + FString ValuePropertyName = MapProperty->GetValueProperty()->GetClass()->GetName(); + if (const FStructProperty* StructProperty = CastField(MapProperty->GetValueProperty())) + { + ValuePropertyName = StructProperty->Struct->GetStructCPPName(); + } + ImGui::Text("%s -> %s [%d]", StringCast(*KeyPropertyName).Get(), StringCast(*ValuePropertyName).Get(), Num); + ImGui::EndDisabled(); + + bool HasChanged = false; + + if (ShowChildren) + { + for (int32 i = 0; i < Num; ++i) + { + ImGui::PushID(i); + // @todo: refactor this so it's better? + HasChanged |= RenderProperty(MapProperty->GetKeyProperty(), Helper.GetKeyPtr(i), i, "Key"); + HasChanged |= RenderProperty(MapProperty->GetValueProperty(), Helper.GetValuePtr(i), i, "Value"); + ImGui::PopID(); + } + ImGui::TreePop(); + } + + return HasChanged; +} + //-------------------------------------------------------------------------------------------------------------------------- bool FCogEngineWindow_Inspector::HasPropertyAnyChildren(const FProperty* Property, uint8* PointerToValue) { @@ -974,6 +1061,28 @@ bool FCogEngineWindow_Inspector::HasPropertyAnyChildren(const FProperty* Propert return true; } + else if (const FSetProperty* SetProperty = CastField(Property)) + { + const FScriptSetHelper Helper(SetProperty, PointerToValue); + const int32 Num = Helper.Num(); + if (Num == 0) + { + return false; + } + + return true; + } + else if (const FMapProperty* MapProperty = CastField(Property)) + { + const FScriptMapHelper Helper(MapProperty, PointerToValue); + const int32 Num = Helper.Num(); + if (Num == 0) + { + return false; + } + + return true; + } else if (const FClassProperty* ClassProperty = CastField(Property)) { return false; diff --git a/Plugins/Cog/Source/CogEngine/Public/CogEngineWindow_Inspector.h b/Plugins/Cog/Source/CogEngine/Public/CogEngineWindow_Inspector.h index 8218f42..7876f0c 100644 --- a/Plugins/Cog/Source/CogEngine/Public/CogEngineWindow_Inspector.h +++ b/Plugins/Cog/Source/CogEngine/Public/CogEngineWindow_Inspector.h @@ -44,7 +44,7 @@ protected: virtual bool RenderPropertyList(TArray& Properties, uint8* PointerToValue); - virtual bool RenderProperty(const FProperty* Property, uint8* PointerToValue, int IndexInArray); + virtual bool RenderProperty(const FProperty* Property, uint8* PointerToValue, int IndexInArray, const char* NameSuffix = nullptr); virtual bool RenderBool(const FBoolProperty* BoolProperty, uint8* PointerToValue); @@ -79,6 +79,10 @@ protected: virtual bool RenderObject(UObject* Object, bool ShowChildren); virtual bool RenderArray(const FArrayProperty* ArrayProperty, uint8* PointerToValue, bool ShowChildren); + + virtual bool RenderSet(const FSetProperty* SetProperty, uint8* PointerToValue, bool ShowChildren); + + virtual bool RenderMap(const FMapProperty* MapProperty, uint8* PointerToValue, bool ShowChildren); virtual FString GetPropertyName(const FProperty& Property);