前言
在UE4中存在大量的菜单栏和工具栏。如下面所示
本质上上面都是菜单栏和工具栏。
菜单(Menu)的扩展点(ExtendPoints)
在Edit->Editor Preferences->Miscellaneous->Display UI ExtendsionPoints勾选,开始显示引擎编辑器的UI扩展点功能
这些绿色的扩展点主要是用于菜单按钮和工具按钮的排序的,添加一个绿色的扩展点统管N个菜单按钮,如果你可以自定义自己的UI扩展点,并将自己的扩展点下的菜单按钮安插在某个扩展点之后。也就是UI扩展点是用于布局菜单栏和工具栏的顺序的。如果开发菜单和工具栏,最好在编辑器打开 “Display UI ExtendsionPoints” 这个辅助功能。
TCommands,FUICommandInfo,FUICommandList
本质上来说我们的菜单按钮和工具按钮其实也是一个按钮,点击菜单按钮执行的事件和“SButton的OnClick”可以看作是一样的。(PS: 看源码可以知道菜单按钮也是封装的SButton) 不过UE4为了别的目的,菜单按钮执行的事件绑定用了Commands命令绑定的方式。
FUICommandInfo
命令信息,用于绑定菜单执行的事件(Delegate)
TCommands
命令集合,用于声明各种FUICommandInfo和注册FUICommandInfo
class FTestMenuCommands : public TCommands<FTestMenuCommands>
{
public:FTestMenuCommands(): TCommands<FTestMenuCommands>(TEXT("TestMenu"), NSLOCTEXT("Contexts", "TestMenu", "TestMenu Plugin"), NAME_None, FTestMenuStyle::GetStyleSetName()){}// TCommands<> interfacevirtual void RegisterCommands() override;public:TSharedPtr< FUICommandInfo > PluginAction;TSharedPtr< FUICommandInfo > TestAction;
};
在RegisterCommands函数中利用 UI_COMMAND进行命令的注册
void FTestMenuCommands::RegisterCommands()
{UI_COMMAND(PluginAction, "TestMenu", "Execute TestMenu action", EUserInterfaceActionType::Button, FInputGesture());UI_COMMAND(TestAction, "aaa", "Execute aaa", EUserInterfaceActionType::Button, FInputGesture());
}
其中的FInputGesture可以于键盘Key绑定,比如直接按住“Delete”键触发菜单按钮事件。
FInputGesture(EKeys::Delete)
在每个模块开始得手动注册命令
FTestMenuCommands::Register();
FUICommandList
FUICommandList是命令队列,每个FUICommandInfo绑定委托都得通过FUICommandList的MapAction接口来
//初始化FUICommandListTSharedPtr<class FUICommandList> PluginCommands;PluginCommands = MakeShareable(new FUICommandList);//绑定动作PluginCommands->MapAction(FTestMenuCommands::Get().PluginAction,FExecuteAction::CreateRaw(this, &FTestMenuModule::PluginButtonClicked),FCanExecuteAction());void FTestMenuModule::TestClicked()
{FMessageDialog::Open(EAppMsgType::Ok, FText::FromString("1111111111111112222222"));
}
这里的FUICommandList的存在很多个重载的MapAction,不过都得由传入执行事件的委托变量FExecuteAction
/** Defines FExecuteAction delegate interface */
DECLARE_DELEGATE(FExecuteAction);
void FUICommandList::MapAction( const TSharedPtr< const FUICommandInfo > InUICommandInfo, FExecuteAction ExecuteAction, FCanExecuteAction CanExecuteAction, EUIActionRepeatMode RepeatMode )
像上面的话我们就为一个命令(FUICommandInfo)绑定了执行事件
FExtend扩展
扩展引擎编辑器的系统菜单栏,主要添加的控件由三种:FMenuBuilder, FMenuBarBuilder,FToolBarBuilder
并且都是FExtend来扩展,FExtend提供了AddMenuExtension来扩展Menu,AddMenuBarExtension扩展MenuBar, AddToolBarExtension扩展ToolBar. FExtend最后都在FLevelEditorModule中管理
FLevelEditorModule& LevelEditorModule = FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");
FMenuBuilder
FMenuBarBuilder主要用于菜单的扩展
TSharedPtr<FExtender> MenuExtender = MakeShareable(new FExtender());MenuExtender->AddMenuExtension("WindowLayout", EExtensionHook::After, PluginCommands, FMenuExtensionDelegate::CreateRaw(this, &FTestMenuModule::AddMenuExtension));LevelEditorModule.GetMenuExtensibilityManager()->AddExtender(MenuExtender);void FTestMenuModule::AddMenuExtension(FMenuBuilder& Builder)
{Builder.AddMenuEntry(FTestMenuCommands::Get().PluginAction);Builder.AddMenuEntry(FTestMenuCommands::Get().PluginAction);Builder.AddSubMenu(FText::FromString("MyTestLevel"), FText::FromString("MyTestLevel"), FNewMenuDelegate::CreateRaw(this, &FTestMenuModule::AddMenuSubMenu), false);
}void FTestMenuModule::AddMenuSubMenu(FMenuBuilder& Builder)
{Builder.AddMenuEntry(FTestMenuCommands::Get().TestAction);Builder.AddMenuSeparator();Builder.AddMenuEntry(FTestMenuCommands::Get().TestAction);Builder.AddSearchWidget();Builder.AddWrapperSubMenu(FText::FromString("Test"), FText::FromString("MyTest"), FOnGetContent::CreateRaw(this, &FTestMenuModule::GetWapperSubMenuContent), FSlateIcon());}
(1)
AddMenuExtension( FName ExtensionHook, EExtensionHook::Position HookPosition, const TSharedPtr< FUICommandList >& CommandList, const FMenuExtensionDelegate& MenuExtensionDelegate )
ExtensionHook就是上面图中的绿色显示的UI扩展点,用于定位我们添加的菜单控件的位置是相对于哪个扩展点,相对位置指定:
/** Where in relation to an extension hook should you apply your extension */
namespace EExtensionHook
{enum Position{/** Inserts the extension before the element or section. */Before,/** Inserts the extension after the element or section. */After,/** Sections only. Inserts the extension at the beginning of the section. */First,};
}
(2)这里BeginSection和EndSection是自定义UI扩展点的范围.
(3)在FMenuExtensionDelegate中可以继续扩展新的菜单,可以看作是嵌套。FMenuBuilder提供了AddMenuEntry(菜单项)和AddSubMenu(添加子菜单)可以继续嵌套式扩展菜单栏。
效果:
FMenuBarBuilder
FMenuBarBuilder主要用于主菜单的扩展
在这主菜单添加新的菜单,
TSharedPtr<FExtender> MenuBarExtend = MakeShareable(new FExtender());MenuBarExtend->AddMenuBarExtension("Help", EExtensionHook::After, PluginCommands, FMenuBarExtensionDelegate::CreateRaw(this, &FTestMenuModule::AdddMenuBarExtend));LevelEditorModule.GetMenuExtensibilityManager()->AddExtender(MenuBarExtend);void FTestMenuModule::AdddMenuBarExtend(FMenuBarBuilder& Builder)
{Builder.AddPullDownMenu(FText::FromString("Test"), FText::FromString("MyTest"), FNewMenuDelegate::CreateRaw(this, &FTestMenuModule::AddPullDownMenu));
}
AddMenuBarExtension( FName ExtensionHook, EExtensionHook::Position HookPosition, const TSharedPtr< FUICommandList >& CommandList, const FMenuBarExtensionDelegate& MenuBarExtensionDelegate
后面添加的菜单可以继续通过绑定的委托FMenuBarExtensionDelegate扩展, 如果你希望继续扩展这个菜单栏,就继续在委托里继续利用FMenuBarBuilder的AdddMenuBarExtend等接口往下嵌套
void FTestMenuModule::AdddMenuBarExtend(FMenuBarBuilder& Builder)
{Builder.AddPullDownMenu(FText::FromString("Test"), FText::FromString("MyTest"), FNewMenuDelegate::CreateRaw(this, &FTestMenuModule::AddPullDownMenu));
}void FTestMenuModule::AddPullDownMenu(FMenuBuilder& Builder)
{Builder.BeginSection(FName("TestEditor"));Builder.AddMenuEntry(FTestMenuCommands::Get().TestAction);Builder.EndSection();
}
效果:
FToolBarBuilder
FToolBarBuilder扩展工具栏
和前面差不多
TSharedPtr<FExtender> ToolbarExtender = MakeShareable(new FExtender);ToolbarExtender->AddToolBarExtension("Settings", EExtensionHook::After, PluginCommands, FToolBarExtensionDelegate::CreateRaw(this, &FTestMenuModule::AddToolbarExtension));LevelEditorModule.GetToolBarExtensibilityManager()->AddExtender(ToolbarExtender);void FTestMenuModule::AddToolbarExtension(FToolBarBuilder& Builder)
{Builder.AddToolBarButton(FTestMenuCommands::Get().PluginAction);Builder.AddToolBarButton(FTestMenuCommands::Get().PluginAction);Builder.AddComboButton(FUIAction(), FOnGetContent::CreateRaw(this, &FTestMenuModule::GetComboConent), FText::FromString("aaaadd"));
}
(1)AddToolBarButton添加工具按钮
(2)AddComboButton添加下拉连续按钮,可以通过FOnGetContent委托任意填充下拉按钮内容,只要是Sidget就行。
不过经常是填充FMenuBuilder建立的内容,像UE4的地形编辑器的各种ToolMode就是这样构造。
TSharedRef<SWidget> FTestMenuModule::GetComboConent()
{FMenuBuilder MenuBuilder(true, PluginCommands);MenuBuilder.AddMenuEntry(FTestMenuCommands::Get().TestAction);MenuBuilder.AddMenuEntry(FTestMenuCommands::Get().TestAction);MenuBuilder.AddMenuEntry(FTestMenuCommands::Get().TestAction);MenuBuilder.AddMenuEntry(FTestMenuCommands::Get().TestAction);MenuBuilder.AddMenuEntry(FTestMenuCommands::Get().TestAction);return MenuBuilder.MakeWidget();
}
DECLARE_DELEGATE_RetVal(/** return a widget */TSharedRef<SWidget>,FOnGetContent
)AddComboButton( const FUIAction& InAction, const FOnGetContent& InMenuContentGenerator, const TAttribute<FText>& InLabelOverride, const TAttribute<FText>& InToolTipOverride, const TAttribute<FSlateIcon>& InIconOverride, bool bInSimpleComboBox, FName InTutorialHighlightName )
效果:
FMenuBuilder, FMenuBarBuilder,FToolBarBuilder公共接口
其实FMenuBuilder, FMenuBarBuilder,FToolBarBuilder都是继承于FMultiBoxBuilder,部分公用接口挺有用的
MakeWidget转为SCompoundWidget
我们可以单独的建立菜单栏和工具栏,用MakeWidget转为Swidget对象,然后我们的菜单栏和工具栏就可以在Slate中的进行嵌套了,在UE4编辑器开发中经常会见到这样的开发技巧,如
FMenuBuilder MenuBuilder(true, PluginCommands);MenuBuilder.AddMenuEntry(FTestMenuCommands::Get().TestAction);MenuBuilder.AddMenuEntry(FTestMenuCommands::Get().TestAction);MenuBuilder.AddMenuEntry(FTestMenuCommands::Get().TestAction);MenuBuilder.AddMenuEntry(FTestMenuCommands::Get().TestAction);MenuBuilder.AddMenuEntry(FTestMenuCommands::Get().TestAction);SNew(SHorizontalBox)+ SHorizontalBox::Slot()[MenuBuilder.MakeWidget()]
Tab窗口
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
参考资料
[1]. UE4地形编辑器源码:SLandscapeEditor.h, LandscapeEditorDetails.cpp,SLandscapeEditor.cpp
[2].https://docs.unrealengine.com/zh-CN/Programming/Slate/Overview/index.html