#How do I simulate a mouse hover and click on a control node without the mouse?
55 messages · Page 1 of 1 (latest)
@raw gulch continuing the discussion from the ui channel... I tried to do this in godot 4.0 and it's not simulating pressed and focus on the button node...```
extends AnimatedSprite2D
@export_range(0,3) var id := 0
@export var input_map : Array[MyInputMap]
func _process(delta):
if id >= 0 && id < input_map.size():
var input_vector = input_map[id].action_vector .get_strength()
var input_a = input_map[id].action_button_1.is_just_pressed()
var input_b = input_map[id].action_button_2.is_just_pressed()
global_position += input_vector
if input_a: left_click()
if input_b: right_click()
func left_click():
var e = InputEventMouseButton.new()
e.button_index = MOUSE_BUTTON_LEFT
e.position = global_position
e.pressed = true
Input.parse_input_event(e)
e.pressed = false
Input.parse_input_event(e)
func middle_click():
var e = InputEventMouseButton.new()
e.button_index = MOUSE_BUTTON_MIDDLE
e.position = global_position
e.pressed = true
Input.parse_input_event(e)
e.pressed = false
Input.parse_input_event(e)
func right_click():
var e = InputEventMouseButton.new()
e.button_index = MOUSE_BUTTON_RIGHT
e.position = global_position
e.pressed = true
Input.parse_input_event(e)
e.pressed = false
Input.parse_input_event(e)
this is my testing scene
also changing it from a animated sprite to a texture rect doesn't change anything
Do you really need to simulate events? Seems like it might be easier to just handle input normally
Just keep track of what button the mouse is over, and call the relevant function
yes I do. also this doesn't work either... func left_click(): var prev = get_global_mouse_position() Input.warp_mouse (global_position) Input.action_press("ui_mouse_left") Input.warp_mouse (prev) await get_tree().process_frame prev = get_global_mouse_position() Input.warp_mouse (global_position) Input.action_release("ui_mouse_left") Input.warp_mouse (prev) func right_click(): var prev = get_global_mouse_position() Input.warp_mouse (global_position) Input.action_press("ui_mouse_right") Input.warp_mouse (prev) await get_tree().process_frame prev = get_global_mouse_position() Input.warp_mouse (global_position) Input.action_release("ui_mouse_right") Input.warp_mouse (prev)
nothing works it seems to button node is hardcoded to only accept mouse events from the os
this is a whole can of worm I don't want to open, and frankly seems like a hack
I think what you're trying to do seems more like a hack
alot of game engines support simulating mouse presses or at least they expose the ability to simulate ui states
Sure, and Godot does as well. I just don't think this is a use case for that, especially if you're trying to handle multiple players
There's only one mouse cursor
what your suggesting means I can't use control nodes and would have to reprogram the whole ui system from scratch
You can still use control nodes
yes right now it doesn't even let me use the real mouse for simulation
thats what the 2nd block of code I posted tries to do
I think you need to use screen positions, which may not be the same as global positions
how do I do that?
You'll need to find the screen position
is there a function for that?
Not really
how would I do it?
Math
It really depends, I don't know enough about how you have this set up
Plus I still think it's not the right path forward
How are you going to differentiate the different cursors?
I have an id
@export_range(0,3) var id := 0
@export var input_map : Array[MyInputMap]
But how will the button click handler know which one it was?
Which is why this is the wrong solution
how would you do it again?
Well, one reason
However these cursors are moved, have them keep track of what they are focused on, so you can just activate that option when needed
I can't just use 1
how do I get the control under it?
don't I need to know the control under it to keep track?
this would be good for a video or godot engine tutorial
I searched and noone has tried to do this yet
which is weird since smash bros is so popular
Well for something like a character select, you don't necessarily need a freely moving cursor, you can just move around a grid
@undone rose so I think I know what the problem is
var local_position := get_local_mouse_position()
Input.warp_mouse(local_position)```
this is the process event doesn't match my ingame cursor location
you can't see the mouse cursor but it's right there in the red circle
is there a way to get the location of the mouse in the scene?