跳至主要內容

ED11.EdGraph|Comment注释节点

Mr.Si大约 2 分钟u++

UEdGraphNode_Comment

头像
现在要加入一个注释节点
头像

很简单,使用 UE 内置 Comment Node

二、实现步骤

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
};

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());
    }
}

头像

什么!你不知道这个GraphEvents在什么地方注册?罚你回去看一眼直通车

头像

其实就是在InitEditor中就行

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);
	}
}