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

Step-based design instead of Option<...> #64

Open
stefnotch opened this issue Sep 15, 2024 · 0 comments
Open

Step-based design instead of Option<...> #64

stefnotch opened this issue Sep 15, 2024 · 0 comments

Comments

@stefnotch
Copy link

This library currently uses a lot of Option<..>s to represent that the inputs are only valid at certain points in time.

This means that functions like key_pressed actually come with an invariant about when they can be called. (only when the window is focused, and if called multiple times between steps, it may return different results)

It also means that input actions can silently be dropped, for example when the following sequence of events happens

  1. step
  2. User presses W
  3. Window loses focus <== all unfinished events get dropped
  4. input.key_pressed(...)
  5. step

I think it would be more elegant to have the step method return a struct that contains all the user inputs that have happened in a given frame. Let's call it WindowInputs.

So, the new flow would be

  1. step
  2. User presses W. WinitInputHelper internally updates everything
  3. Window loses focus. WinitInputHelper simply sets a "lost focus" flag and ignores mouse motion events.
  4. if let Some(inputs) = input.update(event) { if input.key_pressed(...) { ... } }

Where input.update will call step when appropriate, and return its WindowInputs struct.

I did, in fact, write my own library that works like this. It is heavily based on winit_input_helper, but makes a few different design decisions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant