Skip to content

Document how to mock user input #3494

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

Closed
alice-i-cecile opened this issue Dec 30, 2021 · 2 comments
Closed

Document how to mock user input #3494

alice-i-cecile opened this issue Dec 30, 2021 · 2 comments
Labels
A-Input Player input via keyboard, mouse, gamepad, and more C-Docs An addition or correction to our documentation D-Trivial Nice and easy! A great choice to get started with Bevy

Comments

@alice-i-cecile
Copy link
Member

How can Bevy's documentation be improved?

Simulating user input for use in tests is very important, but it's not always obvious how to do so.

We should document how to do this in two different places:

  1. The root tests directory, as part of an integration test.
  2. In a doc example in each of the input methods.

A user provided the following example for touch input, which should be helpful to get started.

pub fn mock_touch(
    mouse: Res<Input<MouseButton>>,
    windows: Res<Windows>,
    mut touch_events: EventWriter<TouchInput>,
) {
    let window = windows.get_primary().unwrap();
    let touch_phase = if mouse.just_pressed(MouseButton::Left) {
        Some(TouchPhase::Started)
    } else if mouse.just_released(MouseButton::Left) {
        Some(TouchPhase::Ended)
    } else if mouse.pressed(MouseButton::Left) {
        Some(TouchPhase::Moved)
    } else {
        None
    };
    if let (Some(phase), Some(cursor_pos)) = (touch_phase, window.cursor_position()) {
        touch_events.send(TouchInput {
            phase: phase,
            position: cursor_pos,
            force: None,
            id: 0,
        })
    }
    ```
@alice-i-cecile alice-i-cecile added C-Docs An addition or correction to our documentation D-Trivial Nice and easy! A great choice to get started with Bevy A-Input Player input via keyboard, mouse, gamepad, and more labels Dec 30, 2021
@mockersf
Copy link
Member

There is already an example in the tests for keyboard input:

let mut input = Input::<KeyCode>::default();
input.press(KeyCode::Space);
world.insert_resource(input);

@alice-i-cecile
Copy link
Member Author

Closing as redundant to #2896.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Input Player input via keyboard, mouse, gamepad, and more C-Docs An addition or correction to our documentation D-Trivial Nice and easy! A great choice to get started with Bevy
Projects
None yet
Development

No branches or pull requests

2 participants