Files
Cog/Plugins/CogAI/Source/CogAI/Private/CogAIWindow_Blackboard.cpp
T
Arnaud Jamin 29e8fa4522 CogAbility: Improve Effects Ability and Attribute window
CogAbility:
- Add sorting on Effects and Ability
- Add search on Effects and Ability
- Colors are now per user config
- Add blocking info in ability window and change its color when blocked
- Add separate windows for Effects
CogEnine: Change the command binding key widget
2023-11-03 02:40:38 -04:00

179 lines
5.5 KiB
C++

#include "CogAIWindow_Blackboard.h"
#include "AIController.h"
#include "CogWindowWidgets.h"
#include "BrainComponent.h"
#include "GameFramework/Pawn.h"
#include "BehaviorTree/BlackboardComponent.h"
//--------------------------------------------------------------------------------------------------------------------------
void FCogAIWindow_Blackboard::Initialize()
{
Super::Initialize();
bHasMenu = true;
Config = GetConfig<UCogAIConfig_Blackboard>();
}
//--------------------------------------------------------------------------------------------------------------------------
void FCogAIWindow_Blackboard::RenderHelp()
{
ImGui::Text(
"This window displays the blackboard of the selected actor. "
);
}
//--------------------------------------------------------------------------------------------------------------------------
void FCogAIWindow_Blackboard::ResetConfig()
{
Super::ResetConfig();
Config->Reset();
}
//--------------------------------------------------------------------------------------------------------------------------
void FCogAIWindow_Blackboard::RenderContent()
{
Super::RenderContent();
if (ImGui::BeginMenuBar())
{
if (ImGui::BeginMenu("Options"))
{
ImGui::Checkbox("Sort by Name", &Config->bSortByName);
ImGui::EndMenu();
}
FCogWindowWidgets::MenuSearchBar(Filter);
ImGui::EndMenuBar();
}
AActor* Selection = GetSelection();
if (Selection == nullptr)
{
ImGui::TextDisabled("No Selection");
return;
}
APawn* Pawn = Cast<APawn>(Selection);
if (Pawn == nullptr)
{
ImGui::TextDisabled("Not a pawn");
return;
}
AAIController* AIController = Cast<AAIController>(Pawn->Controller);
if (AIController == nullptr)
{
ImGui::TextDisabled("No AIController");
return;
}
UBrainComponent* BehaviorTree = AIController->GetBrainComponent();
if (BehaviorTree == nullptr)
{
ImGui::TextDisabled("No BrainComponent");
return;
}
UBlackboardComponent* Blackboard = BehaviorTree->GetBlackboardComponent();
if (Blackboard == nullptr)
{
ImGui::TextDisabled("No BlackboardComponent");
return;
}
UBlackboardData* BlackboardAsset = Blackboard->GetBlackboardAsset();
if (BlackboardAsset == nullptr)
{
ImGui::TextDisabled("No BlackboardAsset");
return;
}
TArray<const FBlackboardEntry*> Keys;
uint8 Offset = 0;
for (UBlackboardData* It = BlackboardAsset; It; It = It->Parent)
{
for (int32 KeyID = 0; KeyID < It->Keys.Num(); KeyID++)
{
if (const FBlackboardEntry* Key = BlackboardAsset->GetKey(KeyID))
{
Keys.Add(Key);
}
}
Offset += It->Keys.Num();
}
if (Config->bSortByName)
{
Keys.Sort([](const FBlackboardEntry& Key1, const FBlackboardEntry& Key2)
{
return Key1.EntryName.Compare(Key2.EntryName) < 0;
});
}
if (ImGui::BeginTable("Blackboard", 3, ImGuiTableFlags_SizingFixedFit
| ImGuiTableFlags_Resizable
| ImGuiTableFlags_NoBordersInBodyUntilResize
| ImGuiTableFlags_ScrollY
| ImGuiTableFlags_RowBg
| ImGuiTableFlags_BordersOuter
| ImGuiTableFlags_BordersV
| ImGuiTableFlags_Reorderable
| ImGuiTableFlags_Hideable))
{
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_DefaultHide);
ImGui::TableSetupColumn("Key");
ImGui::TableSetupColumn("Value");
ImGui::TableHeadersRow();
const FString CommonTypePrefix = UBlackboardKeyType::StaticClass()->GetName().AppendChar(TEXT('_'));
for (int32 KeyID = 0; KeyID < Keys.Num(); ++KeyID)
{
const FBlackboardEntry* Key = Keys[KeyID];
if (Key == nullptr)
{
continue;
}
const char* KeyName = TCHAR_TO_ANSI(*Key->EntryName.ToString());
if (Filter.PassFilter(KeyName) == false)
{
continue;
}
ImGui::TableNextRow();
//------------------------
// Type
//------------------------
ImGui::TableNextColumn();
const FString FullKeyType = Key->KeyType ? GetNameSafe(Key->KeyType->GetClass()) : FString();
const FString DescKeyType = FullKeyType.StartsWith(CommonTypePrefix) ? FullKeyType.RightChop(CommonTypePrefix.Len()) : FullKeyType;
ImGui::Text("%s", TCHAR_TO_ANSI(*DescKeyType));
//------------------------
// Name
//------------------------
ImGui::TableNextColumn();
ImGui::Text("%s", KeyName);
//------------------------
// Value
//------------------------
ImGui::TableNextColumn();
const uint8* ValueData = Blackboard->GetKeyRawData(KeyID);
FString ValueDesc = Key->KeyType && ValueData ? *(Key->KeyType->WrappedDescribeValue(*Blackboard, ValueData)) : TEXT("Empty");
ImGui::Text("%s", TCHAR_TO_ANSI(*ValueDesc));
}
ImGui::EndTable();
}
}