-
Notifications
You must be signed in to change notification settings - Fork 96
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
Is there a way to simulate mouse dragging in GUT 4? #608
Comments
There's a test script that accidentally made its way into main that does a drag. Let me know if that helps. It was originally part of some enhancements for InputSender, but those changes haven't been fully implemented yet. https://github.com/bitwes/Gut/blob/main/test%2Fintegration%2Ftest_more_input_ideas.gd#L71 |
I was able to simulate it with Some quick pseudo-code of how to simulate click-and-drag: var control_pos = Vector2i(50,50) # Where your draggable control is
var dest_pos = Vector2i(150,50) # Where you want to drop it
var sender = InputSender.new(Input)
Input.use_accumulated_input = false # Either use this or 'await sender.wait_frames(1).idle` after every event
var drag_ev = InputFactory.mouse_motion(dest_pos)
drag_ev.relative = (dest_pos - control_pos)
drag_ev.button_mask = MOUSE_BUTTON_MASK_LEFT
sender.mouse_left_button_down(control_pos)
sender.send_event(drag_ev)
sender.mouse_left_button_up(dest_pos) Here's my unit test where I test that items in a player's inventory can be moved with click-and-drag: drag_event.mp4 |
Can you paste your input handling code? I'm not sure what you mean by this. |
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, 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 input_sender.mp4
In input_factory.mp4It could also be possible that I'm using InputSender incorrectly. Like I said, I'm basically brand new to GUT. |
Thank you, I did not know about the drag related methods on Controls. My whole approach has been to handle drags manually with input events. Here's a simple test drag button I use in tests.
I think the mouse motion methods in InputSender can be changed to send the button index. The button is already being tracked when you call things like |
@kaiharuto closing this out since examples have been given and it is gone off topic. If you have any more questions feel free to reopen or make another issue. |
Sorry for being inactive for so long. Thank you all for the detailed thread, can't wait to try out the solutions. |
I'm using Godot 4.2-stable and would like to test controls that use mouse dragging. Despite there being support for clicking and mouse movement, I wasn't able to reproduce this functionality using any of the provided methods for clicking, moving or waiting for a few frames to simulate a mouse button being held during movement. Is there a way to achieve this?
The text was updated successfully, but these errors were encountered: