-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
DeviceSourceManager #8031
DeviceSourceManager #8031
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that this API is entirely polling based, specifically to get current input state. It seems like for many situations this may be hard to use. For example:
- When a key is pressed, I want to add an object to a scene - do I need to manually track key state and look for a change across two frames and then react to that?
- As a pointer is moved across the screen, I want to rotate an object - do I need to track pointer position deltas across frames and apply that delta to a rotation?
I would have though we'd want to include a more event driven model, or at the very least for polling have a way of querying deltas across frames without the consumer having to manage this themselves. Am I missing anything, or are these scenarios not well covered with the current proposal?
Edit: It's possible I'm not understanding how this is supposed to work. In your example above, you check whether the Xbox input A's state is 1
and your comment says "Do something when A is hit". Would that example be doing something only when A transitions from a non-pressed to pressed state, or would it do something every frame as long as A is in a pressed state? I guess if it is the latter, then how would you expect to do the former (perform an action when A transitions from non-pressed to pressed)?
This is a polling based system. For #1, I'm not 100% sure that I understand the scenario. With the code as is, you should be able to just listen for input using onBeforeRenderObservable and add your object there. This may not be what you're asking so I'd like to confirm things with you offline. For #2, the current system just keeps track of absolute position and click status. For this scenario, you'd need to calculate and track deltas manually. If you want, I could add additional support for things like movement deltas without major issue. For the XboxInputs.A example, the comment may be a bit misleading. It should perform whatever action is in the if statement block, each frame, as long as the A button remains pressed. For the alternative, the user would have to add additional code to their query to handle single button presses (eg. add a bool to check that a button was pressed and the execute an action if the button set the bool to true and is currently 0). Edit: I also changed the comment to properly state what is happening. |
For my first example above, I just meant that when the
cc @bghgary |
Here's a sample script that should demonstrate some of the new features:
|
…ing deviceSlot to handle pointer id.
Would it be possible to add an observable when input actually changed and have the user listen to this, instead of running it on the beforeRender loop? And just for me to understand - I noticed you removed observables and switched to callback-based API. Is there a specific reason for that? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The biggest piece of feedback I have for this (mentioned in one of the comments) is that I would make DeviceSource
be a nested class inside DeviceSourceManager
whose purpose is to provide a type safe interface to dealing with input for a specific DeviceType
(and slot), and have all the state managed inside DeviceSourceManager
.
To answer your questions, I've added a callback and observables to allow the user to receive and handle events that will fire off when input sources are updated. The only caveat is that these will not be able to be used with Gamepads because they are not event based. I may revisit this later on. For the change back to callback from Observables, this was done to make the implementation of the Native version of the DeviceInputSystem easier. |
Some fixes to make things type safe as intended
this.onDeviceConnectedObservable.clear(); | ||
this.onDeviceDisconnectedObservable.clear(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this? If intended, perhaps update the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I converted those observables back to callbacks. I'll update the comment.
This PR has the initial code for the DeviceSourceManager, a manager that keeps track of specific devices and provides the user with easier to understand context when getting input.
deviceEnum.ts:
deviceInputSystem.ts:
deviceSourceManager.ts:
To use this code, the minimum code that you need is to create an instance of the DeviceSourceManager and have a getInput call somewhere in your game/render loop.
Example: