Editor plugins in UE4 #5 – Combo button

In this tutorial, we are going to look at how to add ComboButton to the toolbar. This is the final result.

If you’ve read the previous tutorials on how to add toolbar button and extending menu, creating combo button should not be a problem for you.

Combo button is an extension of a toolbar. You can use the one that you used for adding a toolbar button. In there just call AddComboButton function. The parameters are quite well commented.

void FTutorialPluginEditorModule::AddToolbarButton(FToolBarBuilder& Builder)
{
	Builder.AddToolBarButton(FTutorialPluginEditorCommands::Get().TestCommand);

	FUIAction TempCompileOptionsCommand;

    /**
	 * Adds a combo button
	 *
	 * @param	InAction					UI action that sets the enabled state for this combo button
	 * @param	InMenuContentGenerator		Delegate that generates a widget for this combo button's menu content.  Called when the menu is summoned.
	 * @param	InLabelOverride				Optional label override.  If omitted, then the action's label will be used instead.
	 * @param	InToolTipOverride			Optional tool tip override.	 If omitted, then the action's label will be used instead.
	 * @param	InIconOverride				Optional icon to use for the tool bar image.  If omitted, then the action's icon will be used instead.
	 * @param	bInSimpleComboBox			If true, the icon and label won't be displayed
	 * @param	InTutorialHighlightName		Name to identify this widget and highlight during tutorials
	 */
	Builder.AddComboButton(
		TempCompileOptionsCommand,
		FOnGetContent::CreateRaw(this, &FTutorialPluginEditorModule::FillComboButton, PluginCommands),
		LOCTEXT("ExampleComboButton", "Example combo button"),
		LOCTEXT("ExampleComboButtonTootlip", "Example combo button tootltip"),
		TAttribute<FSlateIcon>(),
        true
		);

}

 

You will also need to make another function, to fill the combo button. The function returns TSharedRef<SWidget>. You can also see it has one parameter, but it’s not mandatory. In this example, the parameter is used to pass the commands which are used when creating MenuBuilder. But if you don’t want to use MenuBuilder to fill the ComboButton, you don’t need to pass the commands. The function is almost identical to the function that was used to fill sub menu in the previous tutorial. The difference is, that you need to create Menu builder and then make a widget from it.

TSharedRef<SWidget> FTutorialPluginEditorModule::FillComboButton(TSharedPtr<class FUICommandList> Commands)
{
	FMenuBuilder MenuBuilder(true, Commands);

	MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand01);
	MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand02);
	MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand03);

	return MenuBuilder.MakeWidget();
}

 

And this is how you can add ComboButton to the toolbar. Just for fun I also created an example which uses a lambda expression to fill the ComboButton.

Builder.AddComboButton(
	TempCompileOptionsCommand,
	FOnGetContent::CreateLambda([](TSharedPtr<class FUICommandList> Commands)
	{
		FMenuBuilder MenuBuilder(true, Commands);

		MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand01);
		MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand02);
		MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand03);

		return MenuBuilder.MakeWidget();
	}, PluginCommands),
	LOCTEXT("ExampleComboButton", "Example combo button"),
	LOCTEXT("ExampleComboButtonTootlip", "Example combo button tootltip"),
	TAttribute<FSlateIcon>(),
	true
	);

Source code

#pragma once
#include "ModuleManager.h"
#include "UIAction.h"

class FToolBarBuilder;
class FMenuBuilder;
class FReply;
class SWidget;

class FTutorialPluginEditorModule : public IModuleInterface
{
public:

	/** IModuleInterface implementation */
	virtual void StartupModule() override;
	virtual void ShutdownModule() override;

private:

	TSharedPtr<class FUICommandList> PluginCommands;

	void AddToolbarButton(FToolBarBuilder& Builder);
	void AddMenuEntry(FMenuBuilder& MenuBuilder);
	void FillSubmenu(FMenuBuilder& MenuBuilder);

	TSharedRef<SWidget> FillComboButton(TSharedPtr<class FUICommandList> Commands);

	void TestAction();
	
};
#include "TutorialPluginEditor.h"
#include "TutorialPluginEditorCommands.h"
#include "LevelEditor.h"

#define LOCTEXT_NAMESPACE "FTutorialPluginEditorModule"

void FTutorialPluginEditorModule::StartupModule()
{
	FTutorialPluginEditorCommands::Register();
	
	PluginCommands = MakeShareable(new FUICommandList);
	PluginCommands->MapAction(
		FTutorialPluginEditorCommands::Get().TestCommand,
		FExecuteAction::CreateRaw(this, &FTutorialPluginEditorModule::TestAction)
	);

	PluginCommands->MapAction(
		FTutorialPluginEditorCommands::Get().MenuTestCommand,
		FExecuteAction::CreateRaw(this, &FTutorialPluginEditorModule::TestAction)
	);
	PluginCommands->MapAction(
		FTutorialPluginEditorCommands::Get().SubmenuTestCommand01,
		FExecuteAction::CreateLambda([](){UE_LOG(LogTemp, Warning, TEXT("SUBMENU 01 works!!!"));})
	);
	PluginCommands->MapAction(
		FTutorialPluginEditorCommands::Get().SubmenuTestCommand02,
		FExecuteAction::CreateLambda([](){UE_LOG(LogTemp, Warning, TEXT("SUBMENU 02 works!!!"));})
	);
	PluginCommands->MapAction(
		FTutorialPluginEditorCommands::Get().SubmenuTestCommand03,
		FExecuteAction::CreateLambda([](){UE_LOG(LogTemp, Warning, TEXT("SUBMENU 03 works!!!"));})
	);

	FLevelEditorModule& LevelEditorModule = FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");

	{
		TSharedPtr<FExtender> NewToolbarExtender = MakeShareable(new FExtender);
		NewToolbarExtender->AddToolBarExtension("Content", 
												EExtensionHook::Before, 
												PluginCommands, 
												FToolBarExtensionDelegate::CreateRaw(this, &FTutorialPluginEditorModule::AddToolbarButton));

		LevelEditorModule.GetToolBarExtensibilityManager()->AddExtender(NewToolbarExtender);
	}

	{
		TSharedPtr<FExtender> NewMenuExtender = MakeShareable(new FExtender);
		NewMenuExtender->AddMenuExtension("LevelEditor", 
											EExtensionHook::After, 
											PluginCommands, 
											FMenuExtensionDelegate::CreateRaw(this, &FTutorialPluginEditorModule::AddMenuEntry));

		LevelEditorModule.GetMenuExtensibilityManager()->AddExtender(NewMenuExtender);
	}

}

void FTutorialPluginEditorModule::ShutdownModule()
{
	FTutorialPluginEditorCommands::Unregister();
}

void FTutorialPluginEditorModule::AddToolbarButton(FToolBarBuilder& Builder)
{
	Builder.AddToolBarButton(FTutorialPluginEditorCommands::Get().TestCommand);

	FUIAction TempCompileOptionsCommand;

	Builder.AddComboButton(
		TempCompileOptionsCommand,
		FOnGetContent::CreateRaw(this, &FTutorialPluginEditorModule::FillComboButton, PluginCommands),
		LOCTEXT("ExampleComboButton", "Example combo button"),
		LOCTEXT("ExampleComboButtonTootlip", "Example combo button tootltip"),
		TAttribute<FSlateIcon>(),
		true
		);

}

void FTutorialPluginEditorModule::AddMenuEntry(FMenuBuilder& MenuBuilder)
{
	MenuBuilder.BeginSection("CustomMenu", TAttribute<FText>(FText::FromString("TestMenu")));

	MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().MenuTestCommand);
	MenuBuilder.AddSubMenu(FText::FromString("My Submenu"), 
							FText::FromString("My submenu toolkit "), 
							FNewMenuDelegate::CreateRaw(this, &FTutorialPluginEditorModule::FillSubmenu));

	MenuBuilder.EndSection();
}

void FTutorialPluginEditorModule::FillSubmenu(FMenuBuilder& MenuBuilder)
{
	MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand01);
	MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand02);
	MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand03);
}

TSharedRef<SWidget> FTutorialPluginEditorModule::FillComboButton(TSharedPtr<class FUICommandList> Commands)
{
	FMenuBuilder MenuBuilder(true, Commands);

	MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand01);
	MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand02);
	MenuBuilder.AddMenuEntry(FTutorialPluginEditorCommands::Get().SubmenuTestCommand03);

	return MenuBuilder.MakeWidget();
}

void FTutorialPluginEditorModule::TestAction()
{
	UE_LOG(LogTemp, Warning, TEXT("It Works!!!"));
}

#undef LOCTEXT_NAMESPACE
	
IMPLEMENT_MODULE(FTutorialPluginEditorModule, TutorialPluginEditor)

 

Leave a Reply

Your email address will not be published. Required fields are marked *