Files
integration/Plugins/UEWingman/Source/UEWingman/Private/WingGraphActions.cpp

139 lines
4.0 KiB
C++

#include "WingGraphActions.h"
#include "EdGraph/EdGraphSchema.h"
#include "BlueprintActionDatabase.h"
#include "BlueprintNodeSpawner.h"
#include "EdGraphSchema_K2.h"
#include "Kismet2/BlueprintEditorUtils.h"
FString FWingGraphActions::GetFullName(const FEdGraphSchemaAction& Action)
{
FString Category = Action.GetCategory().ToString();
FString MenuName = Action.GetMenuDescription().ToString();
return Category + TEXT("|") + MenuName;
}
FString FWingGraphActions::GetFullName(const UBlueprintNodeSpawner* Spawner)
{
const FBlueprintActionUiSpec& UiSpec = Spawner->PrimeDefaultUiSpec();
FString Category = UiSpec.Category.ToString();
FString MenuName = UiSpec.MenuName.ToString();
return Category + TEXT("|") + MenuName;
}
bool FWingGraphActions::IsMatch(const FEdGraphSchemaAction& Action, const FString &QueryLower, bool Exact)
{
FString FullName = GetFullName(Action).ToLower();
if (FullName.Len() < 3) return false;
if (FullName == QueryLower) return true;
if (Exact) return false;
if (FullName.Contains(QueryLower)) return true;
FString Keywords = Action.GetKeywords().ToString();
if (Keywords.ToLower().Contains(QueryLower)) return true;
return false;
}
bool FWingGraphActions::IsMatch(const UBlueprintNodeSpawner* Spawner, const FString &QueryLower, bool Exact)
{
FString FullName = GetFullName(Spawner).ToLower();
if (FullName.Len() < 3) return false;
if (FullName == QueryLower) return true;
if (Exact) return false;
if (FullName.Contains(QueryLower)) return true;
FString Keywords = Spawner->PrimeDefaultUiSpec().Keywords.ToString();
if (Keywords.ToLower().Contains(QueryLower)) return true;
return false;
}
UEdGraphNode* FWingGraphActions::Execute(FEdGraphSchemaAction& Action, int32 X, int32 Y)
{
FVector2D Location(X, Y);
UEdGraphNode* NewNode = Action.PerformAction(Graph, nullptr, Location, /*bSelectNewNode=*/false);
return NewNode;
}
UEdGraphNode* FWingGraphActions::Execute(UBlueprintNodeSpawner* Spawner, int32 X, int32 Y)
{
FVector2D Location(X, Y);
IBlueprintNodeBinder::FBindingSet Bindings;
return Spawner->Invoke(Graph, Bindings, Location);
}
void FWingGraphActions::CollectActions(UEdGraph* GraphP, const FString& Query, int32 MaxResults, bool ExactMatch)
{
Graph = GraphP;
FString QueryLower = Query.ToLower();
FGraphContextMenuBuilder ContextMenuBuilder(Graph);
Graph->GetSchema()->GetGraphContextActions(ContextMenuBuilder);
for (int32 i = 0; i < ContextMenuBuilder.GetNumActions(); i++)
{
if ((MaxResults > 0) && (Actions.Num() >= MaxResults)) break;
TSharedPtr<FEdGraphSchemaAction> Action = ContextMenuBuilder.GetSchemaAction(i);
if (IsMatch(*Action, QueryLower, ExactMatch)) Actions.Add(Action);
}
}
void FWingGraphActions::CollectSpawners(UEdGraph* GraphP, const FString& Query, int32 MaxResults, bool ExactMatch)
{
Graph = GraphP;
FString QueryLower = Query.ToLower();
for (const auto& Pair : FBlueprintActionDatabase::Get().GetAllActions())
{
for (UBlueprintNodeSpawner* Spawner : Pair.Value)
{
if ((MaxResults > 0) && (Spawners.Num() >= MaxResults)) break;
if (!Spawner) continue;
// Filter by graph compatibility if a graph was provided
if (Spawner->NodeClass)
{
UEdGraphNode* NodeCDO = CastChecked<UEdGraphNode>(Spawner->NodeClass->ClassDefaultObject);
if (!NodeCDO->IsCompatibleWithGraph(Graph))
{
continue;
}
}
if (IsMatch(Spawner, QueryLower, ExactMatch)) Spawners.Add(Spawner);
}
}
}
FString FWingGraphActions::GetFullName(int N)
{
if (N < Actions.Num())
{
return GetFullName(*Actions[N]);
}
else
{
return GetFullName(Spawners[N - Actions.Num()]);
}
}
UEdGraphNode* FWingGraphActions::Execute(int32 N, int32 PosX, int32 PosY)
{
if (N < Actions.Num())
{
return Execute(*Actions[N], PosX, PosY);
}
else
{
return Execute(Spawners[N - Actions.Num()], PosX, PosY);
}
}
FWingGraphActions::FWingGraphActions(UEdGraph *Graph, const FString& Query, int32 MaxResults, bool ExactMatch)
{
if (Cast<UEdGraphSchema_K2>(Graph->GetSchema()))
{
CollectSpawners(Graph, Query, MaxResults, ExactMatch);
}
else
{
CollectActions(Graph, Query, MaxResults, ExactMatch);
}
}