+1 vote

I'm simulated mouse "pressed" on TextureButton by this code:

func _on_Button_pressed():
    get_node("TextureButton").emit_signal("pressed")

Works but this code not show texture "Pressed" when execute, becouse it is fake press (not by mouse). How simulate mouse press button with animation effect ?

in Engine by (207 points)

2 Answers

0 votes
Best answer

Send a mouse click to the texturebutton via gdscript:

var event = InputEvent()
event.type = InputEvent.MOUSE_BUTTON
event.button_index=BUTTON_LEFT
event.pos = get_node("TextureButton").get_global_pos()
event.pressed = true
get_tree().input_event(event)
by (1,316 points)
selected by

And how reset button state?

Set event.pressed to false:

var event = InputEvent()
event.type = InputEvent.MOUSE_BUTTON
event.button_index=BUTTON_LEFT
event.pos = get_node("TextureButton").get_global_pos()
event.pressed = false
get_tree().input_event(event)

Simulate pressed works (thanks) but not showing animation effect. Not show pressed image and back to normal image.

Note the button state will reset to 'normal' if your mouse passes over the game window.

Here is a example project to show how it works. Keep in mind, if the button state is 'pressed', then it will reset to 'normal' if mouse movement is detected passing over the game window.

Project download: https://drive.google.com/file/d/0BwnfZQAEnciARURTajZpRkpwNWM/view?usp=sharing

Wow, works ;) Thanks

0 votes

Updated answer from @ericdl

func simulate_click(tree, node, pressed=true):
    var event = InputEventMouseButton.new()
    event.button_index = BUTTON_LEFT
    event.position = node.get_global_position()
    event.pressed = pressed
    tree.input_event(event)
by (121 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.