EditorContextMenuPlugin

Inherits:RefCounted<Object

Plugin for adding custom context menus in the editor.

Description

EditorContextMenuPlugin allows for the addition of custom options in the editor's context menu.

Currently, context menus are supported for three commonly used areas: the file system, scene tree, and editor script list panel.

Methods

void

_popup_menu(paths:PackedStringArray)virtual

void

add_context_menu_item(name:String, callback:Callable, icon:Texture2D = null)

void

add_context_menu_item_from_shortcut(name:String, shortcut:Shortcut, icon:Texture2D = null)

void

add_context_submenu_item(name:String, menu:PopupMenu, icon:Texture2D = null)

void

add_menu_shortcut(shortcut:Shortcut, callback:Callable)


Enumerations

enumContextMenuSlot:🔗

ContextMenuSlotCONTEXT_SLOT_SCENE_TREE =0

Context menu of Scene dock._popup_menu() will be called with a list of paths to currently selected nodes, while option callback will receive the list of currently selected nodes.

ContextMenuSlotCONTEXT_SLOT_FILESYSTEM =1

Context menu of FileSystem dock._popup_menu() and option callback will be called with list of paths of the currently selected files.

ContextMenuSlotCONTEXT_SLOT_SCRIPT_EDITOR =2

Context menu of Script editor's script tabs._popup_menu() will be called with the path to the currently edited script, while option callback will receive reference to that script.

ContextMenuSlotCONTEXT_SLOT_FILESYSTEM_CREATE =3

The "Create..." submenu of FileSystem dock's context menu._popup_menu() and option callback will be called with list of paths of the currently selected files.

ContextMenuSlotCONTEXT_SLOT_SCRIPT_EDITOR_CODE =4

Context menu of Script editor's code editor._popup_menu() will be called with the path to theCodeEdit node. You can fetch it using this code:

func_popup_menu(paths):varcode_edit=Engine.get_main_loop().root.get_node(paths[0]);

The option callback will receive reference to that node. You can useCodeEdit methods to perform symbol lookups etc.

ContextMenuSlotCONTEXT_SLOT_SCENE_TABS =5

Context menu of scene tabs._popup_menu() will be called with the path of the clicked scene, or emptyPackedStringArray if the menu was opened on empty space. The option callback will receive the path of the clicked scene, or emptyString if none was clicked.

ContextMenuSlotCONTEXT_SLOT_2D_EDITOR =6

Context menu of 2D editor's basic right-click menu._popup_menu() will be called with paths to allCanvasItem nodes under the cursor. You can fetch them using this code:

func_popup_menu(paths):varcanvas_item=Engine.get_main_loop().root.get_node(paths[0]);# Replace 0 with the desired index.

The paths array is empty if there weren't any nodes under cursor. The option callback will receive a typed array ofCanvasItem nodes.


Method Descriptions

void_popup_menu(paths:PackedStringArray)virtual🔗

Called when creating a context menu, custom options can be added by using theadd_context_menu_item() oradd_context_menu_item_from_shortcut() functions.paths contains currently selected paths (depending on menu), which can be used to conditionally add options.


voidadd_context_menu_item(name:String, callback:Callable, icon:Texture2D = null)🔗

Add custom option to the context menu of the plugin's specified slot. When the option is activated,callback will be called. Callback should take singleArray argument; array contents depend on context menu slot.

func_popup_menu(paths):add_context_menu_item("File Custom options",handle,ICON)

If you want to assign shortcut to the menu item, useadd_context_menu_item_from_shortcut() instead.


voidadd_context_menu_item_from_shortcut(name:String, shortcut:Shortcut, icon:Texture2D = null)🔗

Add custom option to the context menu of the plugin's specified slot. The option will have theshortcut assigned and reuse its callback. The shortcut has to be registered beforehand withadd_menu_shortcut().

func_init():add_menu_shortcut(SHORTCUT,handle)func_popup_menu(paths):add_context_menu_item_from_shortcut("File Custom options",SHORTCUT,ICON)

voidadd_context_submenu_item(name:String, menu:PopupMenu, icon:Texture2D = null)🔗

Add a submenu to the context menu of the plugin's specified slot. The submenu is not automatically handled, you need to connect to its signals yourself. Also the submenu is freed on every popup, so provide a newPopupMenu every time.

func_popup_menu(paths):varpopup_menu=PopupMenu.new()popup_menu.add_item("Blue")popup_menu.add_item("White")popup_menu.id_pressed.connect(_on_color_submenu_option)add_context_submenu_item("Set Node Color",popup_menu)

voidadd_menu_shortcut(shortcut:Shortcut, callback:Callable)🔗

Registers a shortcut associated with the plugin's context menu. This method should be called once (e.g. in plugin'sObject._init()).callback will be called when user presses the specifiedshortcut while the menu's context is in effect (e.g. FileSystem dock is focused). Callback should take singleArray argument; array contents depend on context menu slot.

func_init():add_menu_shortcut(SHORTCUT,handle)

User-contributed notes

Please read theUser-contributed notes policy before submitting a comment.