Skip to content

Document how to mock user input #3494

Closed
@alice-i-cecile

Description

@alice-i-cecile

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,
        })
    }
    ```

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-InputPlayer input via keyboard, mouse, gamepad, and moreC-DocsAn addition or correction to our documentationD-TrivialNice and easy! A great choice to get started with Bevy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions