godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.14k stars 93 forks source link

Adding mouse enter and mouse exit events as InputEvents #6680

Open GianptDev opened 1 year ago

GianptDev commented 1 year ago

Describe the project you are working on

Using Godot 3.5.2.stable.mono

I'm making a chess game.

Describe the problem or limitation you are having in your project

I'm using StaticBody2d for each tile of the chessboard and I want to show an animation when hovering the tiles,

Since I want to handle dragging I'm overriding the _input_event function, but the events of the function does not include mouse enter and exit. I have to use the signals instead an define 2 mode functions for the events.

It would be handy, even of everyone else that has my same idea, to add those events in the input events list and send them to _input_event.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

The signals could be kept to not broke compatibility with projects, but adding those 2 classes should not broke anything... I'm I right?

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

It could be done like this:


# remember this is a virtual method, not a signal!
func _input_event (viewport: Object, event: InputEvent, indx: int) -> void:

    if event is InputEventMouseEnter:
        do_enter_animation();
    elif event is InputEventMouseExit:
        do_exit_animation();
    # ...other events

If this enhancement will not be used often, can it be worked around with a few lines of script?

Using the signals is the current way to do it, like this:


# these could also be connected directly from the inspector, but the issue is the same.
func _ready() -> void:
    connect("mouse_entered", self, "do_enter_animation");
    connect("mouse_exited", self, "do_exit_animation");

func _input_event (viewport: Object, event: InputEvent, indx: int) -> void:
    # ... other events

# yes... do_enter_animation and do_exit_animation could be connected directly to the signals,
# but remember that people usually do functions wrappers for signals.

func _on_mouse_entered() -> void:
    do_enter_animation();

func _on_mouse_exited() -> void:
    do_exit_animation();

Is there a reason why this should be core and not an add-on in the asset library?

yes

AThousandShips commented 1 year ago

The separation of the two makes a lot of sense imo as it's connected with other features such as notifications, also there's several different mouse_entered signals in different places so can't realistically be merged into one behaviour, it'd be confusing if only some (like entering the window) are sent as events

Input events currently are global, valid everywhere, making it local is breaking that pattern

GianptDev commented 1 year ago

Damn you are right! I forgot that input events travel across the node tree. However if those specific events are triggered only to the target node without traveling across the nodes it should work... 🤔 but it would probably be confusing and I see why they are separated.