Simulate mouse "pressed" on TextureButton

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mefihl
:warning: Old Version Published before Godot 3 was released.

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 ?

:bust_in_silhouette: Reply From: ericdl

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)

And how reset button state?

Mefihl | 2016-08-21 09:17

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)

ericdl | 2016-08-21 12:26

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

Mefihl | 2016-08-21 13:05

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

ericdl | 2016-08-21 14:50

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

ericdl | 2016-08-22 02:24

Wow, works :wink: Thanks

Mefihl | 2016-08-22 09:13

:bust_in_silhouette: Reply From: sleepprogger

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)