Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Support adding custom items to editor right-click context menus #94525

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from

Conversation

citizenll
Copy link
Contributor

@citizenll citizenll commented Jul 19, 2024

Add right-click menu plugin functionality to implement this proposal

plugin.gd

tool
extends EditorPlugin
var FileSystemPlugin = preload("res://addons/csv-update-plugin/file_context_menu.gd")
func _enter_tree():
	filesystem_plugin = FileSystemPlugin.new()
	add_context_menu_plugin(EditorPlugin.CONTEXT_SLOT_FILESYSTEM, filesystem_plugin)

func _exit_tree():
	remove_context_menu_plugin(EditorPlugin.CONTEXT_SLOT_FILESYSTEM, filesystem_plugin)

file_context_menu.gd

extends EditorContextMenuPlugin
var icon = preload("res://addons/csv-update-plugin/context-icon.svg")
func _init():
	add_context_menu_item(icon, "Update csv", self, "handle_csv_update")


func can_handle(paths:Array):
	var has_cvs = false
	for path in paths:
		if path.get_extension()=="csv":
			has_cvs = true
	return has_cvs


func handle_csv_update(files):
	## files arg is Array<String>
	print("update csv files:",files)

In popMenu event handling, the parameter types for 3 different areas are different.

CONTEXT_SLOT_SCENE_TREE -> Array<Node>
CONTEXT_SLOT_FILESYSTEM -> Array<String>
CONTEXT_SLOT_SCRIPT_EDITOR -> Resource<GDScript>

Finally, I will explain the motivation behind this pull request. It mainly stems from my desire to create a plugin.
The general functionality is as follows: we use GoogleDocs as a place for game configuration (collaboration among multiple remote users).
In order to use this configuration in the game, I have developed a plugin that converts GoogleDocs to CSV (I also used another plugin of mine for configuration).
Each update from GoogleDocs involves updating the entire file. When the number of configuration files and data increases, our workflow slows down.
Therefore, I wondered if it would be possible to implement the operation of updating a single configuration file in the file browser area's right-click menu.
After searching around, I came across this proposal which had the same requirement, so I decided to attempt to implement it.
This is my first time contributing a pull request to the Godot Engine source code, and I'm not particularly skilled in C++. There may be areas in the code of this pull request that are not perfect. Thank you for pointing them out. 🤚~

editor/editor_context_menu_plugin.h Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@AThousandShips
Copy link
Member

AThousandShips commented Jul 19, 2024

This should be implemented for 4.x first and then considered for inclusion in 3.x, we do not normally accept features for 3.x exclusively unless they are not relevant for 4.x

@AThousandShips AThousandShips modified the milestones: 4.x, 3.x Jul 19, 2024
@citizenll
Copy link
Contributor Author

This should be implemented for 4.x first and then considered for inclusion in 3.x, we do not normally accept features for 3.x exclusively unless they are not relevant for 4.x

Currently I only work in 3.x, I will check this weekend if porting to 4.x is easy

@AThousandShips
Copy link
Member

It might be some difficult changes involved, but it might not be too complicated either, but this might not get approved if it's just for 3.x depending

@citizenll citizenll force-pushed the feat_context_menu_plugin branch from 859a34a to a649608 Compare July 19, 2024 10:52
@KoBeWi
Copy link
Member

KoBeWi commented Jul 19, 2024

This addresses #6128 (not sure if someone opened a new proposal).

In Godot 4 there is a Create New menu in FileSystem:
image
It would be nice to handle it as an option too. It would close godotengine/godot-proposals#8254

@AThousandShips AThousandShips changed the title Support adding custom items to editor right-click context menus [3.x] Support adding custom items to editor right-click context menus Jul 19, 2024
@citizenll citizenll force-pushed the feat_context_menu_plugin branch from a649608 to 3cbc4c9 Compare July 21, 2024 08:49
@citizenll
Copy link
Contributor Author

This addresses #6128 (not sure if someone opened a new proposal).

In Godot 4 there is a Create New menu in FileSystem: image It would be nice to handle it as an option too. It would close godotengine/godot-proposals#8254

image
The submenu option has been implemented in version 4.x, below is an example code.

extends EditorContextMenuPlugin
const PLUG = preload("res://addons/context-menu-plugin/plug.svg")

func _can_handle(paths: PackedStringArray) -> bool:
	return true

func _init() -> void:
	add_context_menu_item(PLUG, "Svg...", _handle_create_svg, SUBMENU_SLOT_FILESYSTEM_CREATE)

func _handle_create_svg(args):
	print(args)

@citizenll citizenll force-pushed the feat_context_menu_plugin branch from 3cbc4c9 to 60d8f77 Compare July 21, 2024 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants