xyz This is clever! And I've finally figured it out: Adding a component to each right clickable node. It's a bit roundabout but it works. Thanks everyone for all the ideas.
context_menu_manager.gd (Autoload)
var right_click_handler: PackedScene = preload("[...]/right_click_handler.tscn")
func _ready():
get_tree().node_added.connect(_add_right_click_handler)
func _add_right_click_handler(node: Node):
if node.is_in_group("right_click_enabled"):
node.add_child(right_click_handler.instantiate())
func handle_right_click(node: Control):
pass # Handle stuff here and show context menu
right_click_handler.tscn is just a Node that has right_click_handler.gd, so I won't have to replace any scripts:
extends Node
func _ready():
get_parent().gui_input.connect(_parent_gui_input)
func _parent_gui_input(event: InputEvent):
if event is InputEventMouseButton and event.button_index == 2 and event.is_pressed():
ContextMenu.handle_right_click(get_parent())
So basically the context menu manager adds a little component which calls it whenever its parent gets right clicked, sending the control node with it. I wonder what the performance hit for doing this is, but it's probably not big enough to care.