{"id":3088,"date":"2021-01-02T13:54:36","date_gmt":"2021-01-02T05:54:36","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=3088"},"modified":"2021-01-02T19:44:04","modified_gmt":"2021-01-02T11:44:04","slug":"creating-new-menu-entries","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=3088","title":{"rendered":"Creating new menu entries"},"content":{"rendered":"\n<p> The workflow for creating new menu entries is almost identical to that for creating new toolbar buttons, so this recipe will build on the previous one and show you how to add the command created therein to a menu rather than a toolbar. <\/p>\n\n\n\n<h1 class=\"wp-block-heading\">How to do it&#8230;<\/h1>\n\n\n\n<ol><li>Create a new function inside of the&nbsp;FChapter_10EditorModule&nbsp;class, which is in&nbsp;Chapter10_Editor.h:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">void AddMenuExtension(FMenuBuilder &amp;builder) \n{ \n  FSlateIcon IconBrush = <br>   FSlateIcon(FEditorStyle::GetStyleSetName(), <br>   \"LevelEditor.ViewOptions\", <br>   \"LevelEditor.ViewOptions.Small\"); \n \n  builder.AddMenuEntry(FCookbookCommands::Get().MyButton); \n}; <\/pre>\n\n\n\n<ol><li>In the implementation file (Chapter_10Editor.cpp), find the following code within the&nbsp;StartupModule&nbsp;function:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">EExtension = ToolbarExtender-&gt;AddToolBarExtension(\"Compile\", EExtensionHook::Before, CommandList, FToolBarExtensionDelegate::CreateRaw(this, &amp;FChapter_10EditorModule::AddToolbarExtension)); <br>LevelEditorModule.GetToolBarExtensibilityManager()-&gt;AddExtender(ToolbarExtender);<\/pre>\n\n\n\n<ol><li>Replace the preceding code with the following:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">Extension = ToolbarExtender-&gt;AddMenuExtension(\"LevelEditor\", EExtensionHook::Before, CommandList, FMenuExtensionDelegate::CreateRaw(this,&amp;FChapter_10EditorModule::AddMenuExtension)); <br>LevelEditorModule.GetMenuExtensibilityManager()-&gt;AddExtender(ToolbarExtender);<\/pre>\n\n\n\n<ol><li>Compile your code and launch the editor.<\/li><li>Verify that you now have a menu entry under the&nbsp;Window&nbsp;menu that displays the&nbsp;Cookbook&nbsp;window when clicked. If you followed the preceding recipe, you&#8217;ll also see the green text listing the UI extension points, including the one we used in this recipe (LevelEditor):<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2021\/01\/23b9b8b0-5d3a-40ba-9ba8-cc176d0f1b58.png\" alt=\"\"\/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">How it works&#8230;<\/h1>\n\n\n\n<p>You&#8217;ll note that&nbsp;ToolbarExtender&nbsp;is of type&nbsp;FExtender&nbsp;rather than&nbsp;FToolbarExtender&nbsp;or&nbsp;FMenuExtender.<\/p>\n\n\n\n<p>By using a generic&nbsp;FExtender&nbsp;class rather than a specific subclass, the framework allows you to create a series of command-function mappings that can be used on either menus or toolbars. The delegate that actually adds the UI controls (in this instance,&nbsp;AddMenuExtension) can link those controls to a subset of commands from your&nbsp;FExtender.<\/p>\n\n\n\n<p>This way, you don&#8217;t need to have different&nbsp;TCommands&nbsp;classes for different types of extensions, and you can place the commands into a single central class regardless of where those commands are invoked from the UI.<\/p>\n\n\n\n<p>As a result, the only changes that are required are as follows:<\/p>\n\n\n\n<ul><li>Swapping calls to&nbsp;AddToolBarExtension&nbsp;with&nbsp;AddMenuExtension<\/li><li>Creating a function that can be bound to&nbsp;FMenuExtensionDelegate&nbsp;rather than&nbsp;FToolbarExtensionDelegate<\/li><li>Adding the extender to a Menu Extensibility Manager rather than a Toolbar Extensibility Manager<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The workflow for creating new menu entries is almost id [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,1],"tags":[26],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3088"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3088"}],"version-history":[{"count":2,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3088\/revisions"}],"predecessor-version":[{"id":3346,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3088\/revisions\/3346"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}