0 votes

Hi,

Detecting if a mouse has clicked ON a control is easy. However, I'm having trouble detecting if a mouse entering a control is already clicked.

What I'm trying to do is a row of buttons, but you only need to click one of them, then drag the mouse and any "neighbor" buttons will toggle to match the first clicked button, a little bit like Photoshop's layer visibility buttons.

But the obvious "store the mouse click in variable, then check for mouse entering" is not working, i.e.:

func _ready():
    set_process_input(true)

func _input(event): 
    if event is InputEventMouseButton:
        if event.is_pressed():
            mouse_pressed = true
        else:
            mouse_pressed = false

func _on_mouse_entered():
    if mouse_pressed:
        print("button is drag-pressed!")

Any suggestions? Thanks!

in Engine by (12 points)

1 Answer

0 votes

Your code works fine for me. I created an HBoxContainer with a "Separation"-value of 50, then added three Button-nodes and attached your script liket his:

extends Button

var mouse_pressed = false

func _input(event):
    if event is InputEventMouseButton:
        if event.is_pressed():
            mouse_pressed = true
        else:
            mouse_pressed = false

func _on_mouse_entered():
    if mouse_pressed:
        print("button is drag-pressed!")

Lastly I connected each button's mouse_entered-signal to the _on_mouse_entered-callback and ran the project. Everything worked as you described it.

by (10,155 points)

Thanks! But it still doesn't work consistently here... it actually works if I move the mouse out of the window while dragging, then come back without letting go off the button...

Here's my test scene:
Test Scene Hierarchy

And here's the script on each button:

extends Button

var mouse_pressed = false

func _input(event):
    if event is InputEventMouseButton:
        if event.is_pressed():
            mouse_pressed = true
        else:
            mouse_pressed = false

func _on_mouse_entered():
    if mouse_pressed:
        print("button is drag-pressed!")


func _on_Button_button_down():
    print("button is pressed!")

I'm using Godot 3.2.1, on MacOS

The image (I assume) of your test scene hierarchy doesn't work. If you sure the callbacks in your script are connected correctly, you might consider posting a bug report on github. It definitely works for me (using Godot 3.2.1 under Arch Linux).

Yes, the image show just an HBoxContainer with 5 buttons under it. The signals are connected correctly, since when I comment out the "if mouse_pressed:" line I get the message printed every time the mouse enters.

The only problem is the _input function, it only seems to run when the mouse leaves the window and comes back...

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.

Categories