Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signals for InputEventAction to the Input singleton #75971

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

qaptoR
Copy link

@qaptoR qaptoR commented Apr 12, 2023

each InputEventAction of the InputMap will add an *_pressed and *_released signal that can be connected to.

additionally an action_toggled signal was added that will fire for all actions that are triggered by an InputEvent

Implements such usability:

func _ready():
    Input.connect('ui_accept_pressed', func(): print('accept action pressed'))
    Input.action_toggled.connect(func(action, pressed): print('%s.pressed is %s' %[action, pressed]))

This PR is made to solve this proposal:
Closes godotengine/godot-proposals#1853

This PR essentially updates the work from this closed PR to bring it into alignment with master:
#43646

That is to say that superficial changes have been made to get it working but the underlying implementation remains the same, and it is in my opinion that the solution constitutes essentially atomic work that could not be significantly changed without causing needless obfuscation.
I am of course, just one person with a biased sense of coding principles and a limited knowledge of all the methods available in the Godot source project, therefore I open this PR in the hopes that it will be merged for the greater benefit of the community, but also to further the discussion about what may need to be changed in order to satisfy the stigma that surrounds the original work.

Some conversation was already had on the dev chat:
https://chat.godotengine.org/channel/devel?msg=n2RaX48Nre7x3CpZC

The general consensus was that the original PR constituted a public distribution in good faith at the time it was authored under the MIT license, and it is thus fair to use. However the question regarding the ship of Theseus does remain unanswered.

@qaptoR qaptoR requested a review from a team as a code owner April 12, 2023 04:52
@AThousandShips
Copy link
Member

AThousandShips commented Apr 12, 2023

Your code has invalid formatting, it needs to be indented with tabs and match indentation, it's good to always use clang-format on any source file you've changed

You also need to run --doctool on the compiled code to update documentation and preferably document the new signal action_toggled

This feature should probably also be documented in Input and/or InputMap and also clarify that this does not work for actions added during runtime, only actions in the project settings or the default actions

@dalexeev
Copy link
Member

dalexeev commented Apr 12, 2023

  1. This PR doesn't take into account that actions can be added/removed at runtime using the InputMap.
  2. I'm not sure if adding signals dynamically is a good thing, since such generated signals cannot be documented, they are harder to discover.
    • Also, the GDScript analyzer will not be able to statically infer the type of these signals. Completion will not work with them, lines will be marked as unsafe and UNSAFE_* warnings will be raised if they are enabled.
  3. Unlike the _input methods, Input signals are global, you can't mark an action as handled. I would prefer a new callback like _input_action.

@AThousandShips
Copy link
Member

That approach would also negate any issue with consent of the copied code etc.

@YuriSizov YuriSizov changed the title Implementation of signals for InputEventAction on Input object Add signals for InputEventAction to the Input singleton Apr 12, 2023
@YuriSizov YuriSizov added this to the 4.x milestone Apr 12, 2023
@qaptoR
Copy link
Author

qaptoR commented Apr 12, 2023

My apologies for forgetting to run clang format. I have it installed but it's been over a year since I've created a PR so my wheels are a bit rusty.

I will run those commands before my next push.

This is my first stab at the Input.xml doc for the custom signals:
image

I thought documenting them here since it is where they are connectable made sense.

EDIT: When I ran the --doctool it only erased the work I had done in the image above... not sure what to make of that, as it didn't generate any documentation for the action_toggled signal

@AThousandShips
Copy link
Member

AThousandShips commented Apr 12, 2023

Adding signals that aren't actually in the class won't work with documentation and will cause a ci error

@qaptoR
Copy link
Author

qaptoR commented Apr 12, 2023

Adding signals that aren't actually in the class won't work with documentation and will cause a ci error

I'm assuming you mean that they should be removed, so I will do that.
However I'm not sure how else to document them. Unless what you meant earlier was that it should be documented officially by the documentation team?

@AThousandShips
Copy link
Member

It should be documented in the description of Input is what I meant

@qaptoR
Copy link
Author

qaptoR commented Apr 12, 2023

It should be documented in the description of Input is what I meant

You're going to think I'm quite dense for not figuring that out, lol, but it's very practically a core component of why I enjoy programming. I often think in very literal terms, and could not see past 'signals go in the signal section'.
Well I guess there's another force push this morning!

@AThousandShips
Copy link
Member

I fully understand, I tend to be very literal at times, and I'm sorry for not communicating that clearly, it's easy to become used to what you yourself know and it can be hard to realize the things that are not obvious if someone isn't experienced with a thing

@qaptoR qaptoR force-pushed the master_input_signals branch 2 times, most recently from 039af26 to 58b9749 Compare April 12, 2023 13:28
@qaptoR
Copy link
Author

qaptoR commented Apr 12, 2023

I should probably get some kind of moniker that says 'frequent force pusher', because yes, all of my PR's are like this, it's a curse.

void Input::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_POSTINITIALIZE: {
for (const KeyValue<StringName, InputMap::Action> &E : InputMap::get_singleton()->get_action_map()) {
Copy link
Member

@AThousandShips AThousandShips Apr 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check that the InputMap is created, it isn't always, it isn't when running tests for example, so you need to check that InputMap::get_singleton() returns non-null

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank-you for providing that fix information. I for sure would not have figured that out on my own

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! I had to dig a little before I realised why this could happen myself

each InputEventAction of the InputMap will add an *_pressed and *_released signal
that can be connected to.

additionally an action_toggled signal was added that will fire for all actions that
are triggered by an InputEvent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add _on_Input_action(action) signal
5 participants