From fd6772f4505ca16b7454c653a90a9d96ec07a776 Mon Sep 17 00:00:00 2001 From: yakov Date: Mon, 24 Mar 2025 19:37:39 +1100 Subject: [PATCH] Added copy to json menu option --- Source/Private/Core/N2CEditorIntegration.cpp | 118 +++++++++++++++++++ Source/Private/Core/N2CToolbarCommand.cpp | 11 ++ Source/Public/Core/N2CEditorIntegration.h | 3 + Source/Public/Core/N2CToolbarCommand.h | 4 + 4 files changed, 136 insertions(+) diff --git a/Source/Private/Core/N2CEditorIntegration.cpp b/Source/Private/Core/N2CEditorIntegration.cpp index 81fa2f1..f81ff1a 100644 --- a/Source/Private/Core/N2CEditorIntegration.cpp +++ b/Source/Private/Core/N2CEditorIntegration.cpp @@ -20,6 +20,101 @@ FN2CEditorIntegration& FN2CEditorIntegration::Get() return Instance; } +void FN2CEditorIntegration::ExecuteCopyJsonForEditor(TWeakPtr InEditor) +{ + FN2CLogger::Get().Log(TEXT("ExecuteCopyJsonForEditor called"), EN2CLogSeverity::Debug); + + // Get the editor pointer + TSharedPtr Editor = InEditor.Pin(); + if (!Editor.IsValid()) + { + FN2CLogger::Get().LogError(TEXT("Invalid Blueprint Editor pointer")); + return; + } + FN2CLogger::Get().Log(TEXT("Successfully obtained Blueprint Editor pointer"), EN2CLogSeverity::Info); + + // Get focused graph + UEdGraph* FocusedGraph = Editor->GetFocusedGraph(); + if (!FocusedGraph) + { + FN2CLogger::Get().LogError(TEXT("No focused graph in Blueprint Editor")); + return; + } + + FString GraphName = FocusedGraph->GetName(); + FString BlueprintName = TEXT("Unknown"); + if (UBlueprint* Blueprint = Cast(FocusedGraph->GetOuter())) + { + BlueprintName = Blueprint->GetName(); + } + FN2CLogger::Get().Log( + FString::Printf(TEXT("Found focused graph: %s in Blueprint: %s"), + *GraphName, *BlueprintName), + EN2CLogSeverity::Info + ); + + // Get collector instance + FN2CNodeCollector& Collector = FN2CNodeCollector::Get(); + + // Collect nodes using the specific editor + TArray CollectedNodes; + if (Collector.CollectNodesFromGraph(FocusedGraph, CollectedNodes)) + { + FString Context = FString::Printf(TEXT("Collected %d nodes"), CollectedNodes.Num()); + FN2CLogger::Get().Log(TEXT("Node collection successful"), EN2CLogSeverity::Info, Context); + + // Get translator instance + FN2CNodeTranslator& Translator = FN2CNodeTranslator::Get(); + + // Generate N2CStruct from collected nodes + if (Translator.GenerateN2CStruct(CollectedNodes)) + { + FN2CLogger::Get().Log(TEXT("Node translation successful"), EN2CLogSeverity::Info); + + // Get the Blueprint structure + const FN2CBlueprint& Blueprint = FN2CNodeTranslator::Get().GetN2CBlueprint(); + + // Validate the generated Blueprint + if (Blueprint.IsValid()) + { + FN2CLogger::Get().Log(TEXT("Node translation validation successful"), EN2CLogSeverity::Info); + + // Serialize to JSON with pretty printing enabled for clipboard + FN2CSerializer::SetPrettyPrint(true); + FString JsonOutput = FN2CSerializer::ToJson(Blueprint); + + // Copy JSON to clipboard if not empty + if (!JsonOutput.IsEmpty()) + { + FPlatformApplicationMisc::ClipboardCopy(*JsonOutput); + + // Show notification + FNotificationInfo Info(NSLOCTEXT("NodeToCode", "BlueprintJsonCopied", "Blueprint JSON copied to clipboard")); + Info.bFireAndForget = true; + Info.FadeInDuration = 0.2f; + Info.FadeOutDuration = 0.5f; + Info.ExpireDuration = 2.0f; + FSlateNotificationManager::Get().AddNotification(Info); + + FN2CLogger::Get().Log(TEXT("Blueprint JSON copied to clipboard successfully"), EN2CLogSeverity::Info); + } + else + { + FN2CLogger::Get().LogError(TEXT("JSON serialization failed")); + } + } + else + { + FN2CLogger::Get().LogError(TEXT("Node translation validation failed")); + } + } + else + { + FN2CLogger::Get().LogError(TEXT("Failed to translate nodes")); + } + } +} + void FN2CEditorIntegration::Initialize() { // Register commands @@ -195,6 +290,28 @@ void FN2CEditorIntegration::RegisterToolbarForEditor(TSharedPtrGetCurrentMode() == FBlueprintEditorApplicationModes::StandardBlueprintEditorMode; }) ); + + // Map the Copy JSON command + CommandList->MapAction( + FN2CToolbarCommand::Get().CopyJsonCommand, + FExecuteAction::CreateLambda([this, WeakEditor, BlueprintName]() + { + FN2CLogger::Get().Log( + FString::Printf(TEXT("Copy Blueprint JSON triggered for Blueprint: %s"), *BlueprintName), + EN2CLogSeverity::Info + ); + ExecuteCopyJsonForEditor(WeakEditor); + }), + FCanExecuteAction::CreateLambda([WeakEditor]() + { + TSharedPtr Editor = WeakEditor.Pin(); + if (!Editor.IsValid()) + { + return false; + } + return Editor->GetCurrentMode() == FBlueprintEditorApplicationModes::StandardBlueprintEditorMode; + }) + ); // Store in our map EditorCommandLists.Add(WeakEditor, CommandList); @@ -222,6 +339,7 @@ void FN2CEditorIntegration::RegisterToolbarForEditor(TSharedPtr( @@ -45,6 +48,14 @@ void FN2CToolbarCommand::RegisterCommands() FInputChord() ); + UI_COMMAND( + CopyJsonCommand, + "Copy Blueprint JSON", + "Copy the serialized Blueprint JSON to clipboard for external use", + EUserInterfaceActionType::Button, + FInputChord() + ); + FN2CLogger::Get().Log(TEXT("N2C toolbar commands registered"), EN2CLogSeverity::Debug); } diff --git a/Source/Public/Core/N2CEditorIntegration.h b/Source/Public/Core/N2CEditorIntegration.h index b536f2c..7d8f538 100644 --- a/Source/Public/Core/N2CEditorIntegration.h +++ b/Source/Public/Core/N2CEditorIntegration.h @@ -54,6 +54,9 @@ class FN2CEditorIntegration /** Execute collect nodes for a specific editor */ void ExecuteCollectNodesForEditor(TWeakPtr InEditor); + /** Execute copy blueprint JSON to clipboard for a specific editor */ + void ExecuteCopyJsonForEditor(TWeakPtr InEditor); + /** Handle asset editor opened callback */ void HandleAssetEditorOpened(UObject* Asset, IAssetEditorInstance* EditorInstance); diff --git a/Source/Public/Core/N2CToolbarCommand.h b/Source/Public/Core/N2CToolbarCommand.h index 177cd60..63de7dd 100644 --- a/Source/Public/Core/N2CToolbarCommand.h +++ b/Source/Public/Core/N2CToolbarCommand.h @@ -16,12 +16,16 @@ class FN2CToolbarCommand : public TCommands // Commands TSharedPtr OpenWindowCommand; TSharedPtr CollectNodesCommand; + TSharedPtr CopyJsonCommand; // Command names and labels static const FName CommandName_Open; static const FName CommandName_Collect; + static const FName CommandName_CopyJson; static const FText CommandLabel_Open; static const FText CommandLabel_Collect; + static const FText CommandLabel_CopyJson; static const FText CommandTooltip_Open; static const FText CommandTooltip_Collect; + static const FText CommandTooltip_CopyJson; };