JSands
JSands

Reputation: 153

Change mouse cursor when entering/exiting Area2d

I have tried every google result that seems relevant and cannot find a solution to something I'm sure is simpler than I'm making it. There's a possibility all of the results are outdated, as the official documents don't seem to offer what I'm seeing in search result suggestions.

When the mouse enters an Area2D, I want the mouse cursor to change to the hand pointing cursor. When I exit the Area2D, I want it to change back to the pointer. I am not looking to use custom cursors, just the basic ones already implemented in GODOT.

I know how to use mouse_entered/mouse_exited signals. I just can't determine the correct code and location to make the change happen.

Upvotes: 3

Views: 4770

Answers (2)

BluJayyy
BluJayyy

Reputation: 1

One thing I recommend you can do would be to make an Area2D as a child of the root node (most likely a Node2D) rename the new Area2D to whatever you want (as long as it isn't named Area2D. For the sake of teaching you, I'm gonna rename it to CursorBox) and attach a script to that new Area2D. Add a CollisionShape2D node as a child of the Area2D, set the shape as a circle, and set the radius to 0.5. Next, go to the top right and press the node button. Connect "Area Entered' and "Area Exited" to the CursorBox. Inside of your script, make sure your functions look like what I have written below.

extends Area2D
var mouseCheck = false
func _on_CursorBox_area_entered(area):
    if area.name == ("Area2D"): #This `Area2D` its looking for would be the buttons Area2D
        mouseCheck = true
func _on_CursorBox_area_exited(area):
    if area.name == ("Area2D"):
        mouseCheck = false

The mouse check is just there to see if it works, obviously put your code in there, whatever you need to put in. I would imagine you have some animation you want to play or something. If you have any more questions, just reply and we'll see what else we can do to fix it.

Upvotes: 0

Theraot
Theraot

Reputation: 40220

When the mouse enters an Area2D, I want the mouse cursor to change to the hand pointing cursor. When I exit the Area2D, I want it to change back to the pointer. I am not looking to use custom cursors, just the basic ones already implemented in GODOT.

For that, the functions you want are Input.set_default_cursor_shape and Input.get_current_cursor_shape.

I know how to use mouse_entered/mouse_exited signals. I just can't determine the correct code and location to make the change happen.

You place the code in whatever signal handlers you connected. I remind you that you can connect the signals from the IDE in the "Node" panel, "Signals" tab. And in the code, you should see a green icon next to the func is connected to a signal. See also Signals.

For example, I connected them to a script in the same Area2D, and added this code:

func _on_Area2D_mouse_entered() -> void:
    Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND)

func _on_Area2D_mouse_exited() -> void:
    Input.set_default_cursor_shape(Input.CURSOR_ARROW)

Or if you want to store the previous CursorShape, you can do this:

var old_cursor_shape:int = Input.CURSOR_ARROW

func _on_Area2D_mouse_entered() -> void:
    old_cursor_shape = Input.get_current_cursor_shape()
    Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND)

func _on_Area2D_mouse_exited() -> void:
    Input.set_default_cursor_shape(old_cursor_shape)

See CursorShape for the values. Note: There seems to be a bug in the current beta that cause these to not autocomplete.


Reason why mouse_entered/mouse_exited might not work.

Assuming they are correctly connected.

If there is any Control (for example a Container or a background) overlapping a Node2D - such as an Area2D - regardless if it is in front or behind visually. It will prevent it from taking the input. Thus, you must set its mouse_filter of the Control to Ignore.

And make sure the collision_layer and collision_mask of the Area2D are not empty (0), input_pickable is true, and of course the Area2D has a valid CollisionShape2D or CollisionPolygon2D. Otherwise mouse_entered/mouse_exited won't work.

Also, I remind you to pay attention to any errors reported by the IDE.

Upvotes: 1

Related Questions