Skip to content

Send down mouse buttons to mouse motion events #612

@bitwes

Description

@bitwes

I'm relatively new to GUT, but I made a minimal project explaining what I tried to do, why it didn't work, and what I had to do to work around it: GUTInputHandling.zip

This project has a TextureRect with basic code to set the drag data and preview:

extends TextureRect

func _get_drag_data(_at_position):
	var preview = self.duplicate(0)
	set_drag_preview(preview)
	return self

func _gui_input(event):
	print(event)

and two unit tests, test_drag_using_input_sender and test_drag_using_input_factory:

extends GutTest

# _drag_node spawns in the top-left corner with the size 128x128
var _drag_node: Control
var _sender

func before_all():
	_drag_node = preload("res://DragTest.tscn").instantiate()
	add_child(_drag_node)
	_sender = InputSender.new(Input)
	print(_drag_node)
func after_each():
	_sender.clear()
func after_all():
	_drag_node.queue_free()

# This test tries to use exclusively InputSender to press, drag,
# and release the control.
func test_drag_using_input_sender():
	await (_sender.mouse_set_position(Vector2(64,64)) # Set position to middle of control
		.mouse_left_button_down().wait_secs(1) # Begin click
		.mouse_relative_motion(Vector2(128,0)) # Move to the right to begin drag
	).wait_secs(1).idle
	
	assert_not_null(get_viewport().gui_get_drag_data(), "Control is being dragged")
	
	await _sender.mouse_left_button_up().wait_frames(1).idle # Release the mouse
	
	assert_null(get_viewport().gui_get_drag_data(), "Control is no longer being dragged")

# This test uses InputSender to press and release the mouse button,
# but generates a drag event with InputFactory
func test_drag_using_input_factory():
	await _sender.mouse_left_button_down(Vector2(64,64)).wait_secs(1) # Mouse down on control
	
	var event = InputFactory.mouse_motion(Vector2(196,64)) # Create a mouse motion event at the destination
	event.relative = Vector2i(128,0) # Set the relative motion
	event.button_mask = MOUSE_BUTTON_MASK_LEFT # Indicate we are holding left click
	
	await _sender.send_event(event).wait_secs(1).idle # Drag event
	
	assert_not_null(get_viewport().gui_get_drag_data(), "Control is being dragged")
	
	await _sender.mouse_left_button_up(Vector2(196,64)).wait_frames(1).idle # Release the mouse
	
	assert_null(get_viewport().gui_get_drag_data(), "Control is no longer being dragged")

In test_drag_using_input_sender, the test tries to perform a drag exclusively through InputSender's relative events. It does not end up working, and the console shows that the InputEventMouseMotion event has the button_index set to 0, which means the left mouse button isn't held down.

input_sender.mp4
InputEventMouseButton: button_index=1, mods=none, pressed=true, canceled=false, position=((64, 64)), button_mask=0, double_click=false
InputEventMouseMotion: button_mask=0, position=((192, 64)), relative=((128, 0)), velocity=((0, 0)), pressure=0.00, tilt=((0, 0)), pen_inverted=(false)
InputEventMouseButton: button_index=1, mods=none, pressed=false, canceled=false, position=((192, 64)), button_mask=0, double_click=false

In test_drag_using_input_factory, I create a InputEventMouseMotion event manually, set the relative to simulate the mouse dragging to the right, and set button_index to indicate that the left mouse button is down. This results in the drag event occurring, as seen by the preview node appearing and the test passing.

input_factory.mp4

It could also be possible that I'm using InputSender incorrectly. Like I said, I'm basically brand new to GUT.

Originally posted by @TGRCdev in #608 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementnext releaseThis has been implemented and will be in the next release.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions