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

Send down mouse buttons to mouse motion events #612

Closed
bitwes opened this issue May 29, 2024 · 1 comment
Closed

Send down mouse buttons to mouse motion events #612

bitwes opened this issue May 29, 2024 · 1 comment
Labels
enhancement next release This has been implemented and will be in the next release.

Comments

@bitwes
Copy link
Owner

bitwes commented May 29, 2024

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)

@bitwes bitwes added the next release This has been implemented and will be in the next release. label May 29, 2024
@bitwes
Copy link
Owner Author

bitwes commented May 31, 2024

Implemented in main.

@bitwes bitwes closed this as completed May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement next release This has been implemented and will be in the next release.
Projects
None yet
Development

No branches or pull requests

1 participant