ED11.EdGraph|Comment注释节点
大约 2 分钟
UEdGraphNode_Comment

二、实现步骤
1 在 新建FEdGraphSchemaAction
NodeActions.h
#pragma once
#include "CoreMinimal.h"
#include "EdGraph/EdGraphSchema.h"
#include "ComboNodeActions.generated.h"
/** Action to add a comment to the graph */
USTRUCT()
struct FComboSchemaAction_AddComment : public FEdGraphSchemaAction
{
GENERATED_BODY()
FComboSchemaAction_AddComment() : FEdGraphSchemaAction() {}
FComboSchemaAction_AddComment(FText InDescription, FText InToolTip)
: FEdGraphSchemaAction(FText(), MoveTemp(InDescription), MoveTemp(InToolTip), 0)
{
}
// FEdGraphSchemaAction interface
virtual UEdGraphNode* PerformAction(class UEdGraph* ParentGraph, UEdGraphPin* FromPin, const FVector2D Location, bool bSelectNewNode = true) override final;
// End of FEdGraphSchemaAction interface
};
NodeActions.cpp
UEdGraphNode* FComboSchemaAction_AddComment::PerformAction(class UEdGraph* ParentGraph, UEdGraphPin* FromPin,
const FVector2D Location, bool bSelectNewNode)
{
UEdGraphNode_Comment* const CommentTemplate = NewObject<UEdGraphNode_Comment>();
FVector2D SpawnLocation = Location;
FSlateRect Bounds;
TSharedPtr<SGraphEditor> GraphEditorPtr = SGraphEditor::FindGraphEditorForGraph(ParentGraph);
if (GraphEditorPtr.IsValid())
{
// If they have a selection, build a bounding box around the selection
if (GraphEditorPtr->GetBoundsForSelectedNodes(/*out*/ Bounds, 50.0f))
{
CommentTemplate->SetBounds(Bounds);
SpawnLocation.X = CommentTemplate->NodePosX;
SpawnLocation.Y = CommentTemplate->NodePosY;
}
else
{
// Otherwise initialize a default comment at the user's cursor location.
SpawnLocation = GraphEditorPtr->GetPasteLocation();
}
}
UEdGraphNode* const NewNode = FEdGraphSchemaAction_NewNode::SpawnNodeFromTemplate<UEdGraphNode_Comment>(ParentGraph, CommentTemplate, SpawnLocation, bSelectNewNode);
return NewNode;
}
2 允许创建 Comment 节点
你的UEdGraphSchema中重写虚函数
virtual TSharedPtr<FEdGraphSchemaAction> GetCreateCommentAction() const override
{
return TSharedPtr<FEdGraphSchemaAction>(static_cast<FEdGraphSchemaAction*>(new FComboSchemaAction_AddComment));
}
3. 在FAssetEditorToolkit的CommandList中注册命令
void FComboGraphEditorApp::CreateCommandList()
{
if (GraphEditorCommands.IsValid())
{
return;
}
GraphEditorCommands = MakeShareable(new FUICommandList);
GraphEditorCommands->MapAction(FGraphEditorCommands::Get().CreateComment,
FExecuteAction::CreateRaw(this, &FComboGraphEditorApp::OnCreateComment)
);
}
4. 最重要的是Graph Events委托注册OnNodeTitleCommitted支持ReName
SGraphEditor::FGraphEditorEvents GraphEvents;
//注释支持修改的关键
GraphEvents.OnTextCommitted = FOnNodeTextCommitted::CreateSP(this, &FComboGraphEditorApp::OnNodeTitleCommitted);
void FComboGraphEditorApp::OnNodeTitleCommitted(const FText& NewText, ETextCommit::Type CommitInfo, UEdGraphNode* NodeBeingChanged)
{
if (NodeBeingChanged)
{
const FScopedTransaction Transaction( FText::FromString("ReName") );
NodeBeingChanged->Modify();
NodeBeingChanged->OnRenameNode(NewText.ToString());
}
}
5. 在FAssetEditorToolkit 修改以显示注释的属性
SGraphEditor::FGraphEditorEvents InEvents;
//注释支持修改的关键
InEvents.OnTextCommitted = FOnNodeTextCommitted::CreateSP(this, &FComboGraphEditorApp::OnNodeTitleCommitted);
//修改选中后显示属性面板
InEvents.OnSelectionChanged = SGraphEditor::FOnSelectionChanged::CreateSP(this, &FComboGraphEditorApp::OnGraphSelectionChanged);
void FSuperComboGraphAssetsEditor::OnGraphSelectionChanged(const TSet<UObject*>& NewSelection)
{
if (!SuperComboGraphProperties.IsValid())
{
return;
}
for (UObject* Selection : NewSelection)
{
if (UEdGraphNode_Comment* CommentNode = Cast<UEdGraphNode_Comment>(Selection))
{
SuperComboGraphProperties->SetObject(CommentNode);
return;
}
if (USuperComboGraphEdNodeBase* Node = Cast<USuperComboGraphEdNodeBase>(Selection))
{
// Root 节点:显示 Graph 本身
if (Node && !Node->IsRootNode())
{
SuperComboGraphProperties->SetObject(Node->GetNodeData());
return;
}
}
}
SuperComboGraphProperties->SetObject(SuperComboGraphObj);
}
6. 派生的UEdGraphSchema类中注册Action,以便手动右键内容中可以创建注释节点
void UComboGraphSchema::GetGraphContextActions(FGraphContextMenuBuilder& ContextMenuBuilder) const
{
//TODO 其他节点菜单
// Add the ability to create a comment to the context menu too for discoverability
//右键菜单中显示添加注释节点
{
TSharedPtr<FComboSchemaAction_AddComment> Action = MakeShared<FComboSchemaAction_AddComment>(
LOCTEXT("AddComment", "Add Comment"), LOCTEXT("AddComment_Tooltip", "Adds a comment node to the graph."));
ContextMenuBuilder.AddAction(Action);
}
}


.png)