Skip to content

Custom context menu options

Wolfyxon edited this page Jun 25, 2023 · 9 revisions

Currently, you can add custom options to FileSystemDock and SceneTreeDock context menu.
Warning: This system is not 100% stable and often causes to also trigger the built-in Godot options when calling a custom one.

FileSystemDock

You will need these methods:

  • register_fs_dir_context_option()
  • register_fs_file_context_option()

And the filesystem_context_menu_item_clicked signal.

Do not use add_fs_context_menu_item() and add_fs_context_menu_icon_item() since they are not permanent since Godot clears and re-adds the options each time you open it

Also see https://github.com/Wolfyxon/godot-BetterEditorPlugin/blob/main/examples/OpenInTerminal/OpenInTerminal.gd

Registering

File options

To register a context option that will apply only to files use:
register_fs_file_context_option(<label:String>, [ID:String], [icon:Texture2D], [allowed_types:PackedStringArray]).
If allowed_types is empty or not specified, all file extensions will be accepted.
Example:

register_fs_file_context_option("Show image", "show_image", load("res://icon.svg"), ["png","jpg","jpeg","bmp","svg"])

Directory options

To register a context option that will apply only to directories use:
register_fs_dir_context_option(<label:String>, [ID:String], [icon:Texture2D]).
Example:

register_fs_dir_context_option("Is this a directory?", "is_dir")

Detecting

This works the same for directory and file options. Connect the filesystem_context_menu_item_clicked signal to a function that takes 1 argument, let's name it item as an example. Then, use item.get_string_id() to get the specified ID.

Full example

func _enter_tree():
	register_fs_file_context_option("Show image", "show_image", load("res://icon.svg"), ["png","jpg","jpeg","bmp","svg"])
	register_fs_dir_context_option("Is this a directory?", "is_dir")

	filesystem_context_menu_item_clicked.connect(_fs_context_option_clicked)

func _fs_context_option_clicked(item):
	match item.get_string_id():
		"show_image":
			OS.shell_open(get_fs_selected_path())
			print("Showing image")

		"is_dir":
			OS.alert("yes it is")

SceneTreeDock

You will need the register_node_context_option() method and the node_context_menu_item_clicked signal.

Registering

To register a node context menu option use: register_node_context_option(<label:String>, [id:String], [icon:Texture2D], [allowed_classes]).
If allowed_classes is empty or unspecified, all node classes will be accepted.

register_node_context_option("Drink", "drink_sprite", load("res://sprite_icon.png"), ["Sprite2D","Sprite3D"])

Detecting

Connect the filesystem_context_menu_item_clicked signal to a function that takes 1 argument, let's name it item as an example. Then, use item.get_string_id() to get the specified ID.

Full example

func _enter_tree():
    register_node_context_option("Drink", "drink_sprite", load("res://sprite_icon.png"), ["Sprite2D","Sprite3D"])
    node_context_menu_item_clicked.connect(_node_option_clicked)  

func _node_option_clicked(item):
    if item.get_string_id() === "drink_sprite":
        print("Yummy")

Clone this wiki locally