A situation I’ve run into a couple of times without finding a “good” solution to is handling mouse drag interactions that start in one Control “A” and end in another Control “B”:
With regular mouse events (like click, moves etc) each Control gets the corresponding InputEventMouse via gui_input based on the position, mouse filters etc. So if you click a control or move the mouse cursor over it, this control get this event. However, with drag operations there’s a exception to this rule. So if you click down in a Control and start dragging, this control will keep getting the mouse move events sent to it even if you drag out of the control and into another. This is often desirable, but in this case it means if you start dragging in control A, control B will not get any mouse input events even if you drag into it.
I’ve solved this in the past with various workarounds, but they all feel like hacks (for example forwarding the drag event from one control to another via calls, or having a higher level parent handle dragging for both).
So I’m wondering if you know of a good proper solution to this situation?
Edit: For example if there is a way to disable this behavior to lock drag mouse events to the mouse down control until mouse up.
Have you looked at Control._get_drag_data(), Control._can_drop_data(), Control._drop_data() and Control.force_drag()? That’s the usual way to do drag and drop in Godot.
_get_drag_data() is called when you start dragging Control A. You can also set a preview inside it with set_drag_preview()
When you’re dragging Control A and place your mouse over Control B, Godot calls _can_drop_data(position, data) on Control B every mouse move. “data” contains Control A’s data from _get_drag_data(), when you started dragging.
If _can_drop_data() returns true, and you release the mouse over Control B, _drop_data(position, data) is called.
There’s also NOTIFICATION_DRAG_BEGIN and NOTIFICATION_DRAG_END sent to all nodes
If that doesn’t solve your use case; since Godot only ever sends the event through _gui_input() to a single Control (and its parents), you can instead get the event from _input() inside ControlB and filter for mouse input:
extends Control
var _dragging: bool = false
func _notification(what: int) -> void:
match what:
NOTIFICATION_DRAG_BEGIN:
var data = get_viewport().gui_get_drag_data()
# Expects _get_drag_data() from Control A to return { "source": self }.
# Ignore drags that started from this Control, or it will react to itself.
_dragging = data is Dictionary and data.get("source") != self
NOTIFICATION_DRAG_END:
_dragging = false
func _input(event: InputEvent) -> void:
if event is InputEventMouseMotion and _dragging: # Are you dragging something?
if get_global_rect().has_point(event.global_position): # Is the mouse over this Control?
var data = get_viewport().gui_get_drag_data() # Control A's data
_on_drag_over(event.position, data)
func _on_drag_over(position, data) -> void:
## Your custom drag code
Thank you for all these suggestions, I didn’t know about _get_drag_data() _can_drop_data() etc functions, and those could be very useful for GUI-control style drag and drop operations, and I’ll have use for that in the future. In the case I’m working on now it’s more a case of tracking a drag operation moving in-game object across several viewports so I’ll go with the grab_click_focus() solution below.
Thanks for the suggestion, I didn’t know about grab_click_focus(), but indeed that seems to do the trick. From the documentation I would not have guessed it worked for this since it says it will try to click the control and have it grab focus. But this does indeed seem to transfer mouse event “ownership” to this control, even if the focus part is ignored. It has the drawback of also generating a click event that I needed to filter ignore at this shift, but might still be the best option.