Skip to content

Instantly share code, notes, and snippets.

@rms80
Created July 29, 2024 04:14
Show Gist options
  • Save rms80/622fd2fadd7fbecd2ab411fc7561d727 to your computer and use it in GitHub Desktop.
Save rms80/622fd2fadd7fbecd2ab411fc7561d727 to your computer and use it in GitHub Desktop.
bit of code to add Mode Buttons to the Modes Toolbar in UE5 (to the right of Modes dropdown)
// grab some global stuff from the level editor
FLevelEditorModule& LevelEditorModule = FModuleManager::GetModuleChecked<FLevelEditorModule>("LevelEditor");
TSharedRef<FUICommandList> CommandBindings = LevelEditorModule.GetGlobalLevelEditorActions();
const FLevelEditorModesCommands& ModesCommands = LevelEditorModule.GetLevelEditorModesCommands();
FToolMenuOwnerScoped OwnerScoped(this); // not sure what this is for exactly, but other UToolMenu extenders do it
// hardcoded name for the modes toolbar, from FLevelEditorToolBar::RegisterLevelEditorToolBar()
UToolMenu* ModesToolbar = UToolMenus::Get()->ExtendMenu("LevelEditor.LevelEditorToolBar.ModesToolBar");
// add a new section
FToolMenuSection& NewSection = ModesToolbar->AddSection("MyModeButtons", LOCTEXT("MyModeButtons", "MyModeButtons"));
TArray<FName> IncludeModeIDButtons;
IncludeModeIDButtons.Add(FBuiltinEditorModes::EM_Default);
IncludeModeIDButtons.Add(FBuiltinEditorModes::EM_Landscape);
IncludeModeIDButtons.Add(FBuiltinEditorModes::EM_Foliage);
IncludeModeIDButtons.Add(TEXT("MeshPaintMode"));
IncludeModeIDButtons.Add(TEXT("EM_ModelingToolsEditorMode"));
for (FName ModeID : IncludeModeIDButtons)
{
// this is how the command names are constructed in FLevelEditorModesCommands::RegisterCommands()
FName EditorModeCommandName = FName(*(FString("EditorMode.") + ModeID.ToString()));
TSharedPtr<FUICommandInfo> EditorModeCommand =
FInputBindingManager::Get().FindCommandInContext(ModesCommands.GetContextName(), EditorModeCommandName);
if (EditorModeCommand.IsValid() == false)
continue;
// cannot directly add the FUICommandInfo here because it is not a Button type and InitToolbarButton will ensure :(
NewSection.AddEntry(FToolMenuEntry::InitToolBarButton(
EditorModeCommandName,
FToolUIActionChoice(EditorModeCommand, *CommandBindings),
EditorModeCommand->GetLabel(), EditorModeCommand->GetDescription(), EditorModeCommand->GetIcon()));
}
NewSection.AddSeparator(NAME_None);
@rms80
Copy link
Author

rms80 commented Jul 29, 2024

you will need at least these headers:

#include "LevelEditor.h"
#include "LevelEditorModesActions.h"
#include "ILevelEditor.h"
#include "SLevelViewport.h"
#include "ToolMenus.h"
#include "EditorModes.h"

and the "LevelEditor" and "ToolMenus" modules in your build.cs (and probably others)

Running this when the FEditorDelegates::OnEditorInitialized() delegate fires might work (or possibly in a module init)

@rms80
Copy link
Author

rms80 commented Jul 29, 2024

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment