bevy_input/
lib.rs

1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2#![forbid(unsafe_code)]
3#![doc(
4    html_logo_url = "https://bevyengine.org/assets/icon.png",
5    html_favicon_url = "https://bevyengine.org/assets/icon.png"
6)]
7
8//! Input functionality for the [Bevy game engine](https://bevyengine.org/).
9//!
10//! # Supported input devices
11//!
12//! `bevy` currently supports keyboard, mouse, gamepad, and touch inputs.
13
14mod axis;
15mod button_input;
16/// Common run conditions
17pub mod common_conditions;
18pub mod gamepad;
19pub mod gestures;
20pub mod keyboard;
21pub mod mouse;
22pub mod touch;
23
24pub use axis::*;
25pub use button_input::*;
26
27/// The input prelude.
28///
29/// This includes the most common types in this crate, re-exported for your convenience.
30pub mod prelude {
31    #[doc(hidden)]
32    pub use crate::{
33        gamepad::{Gamepad, GamepadAxis, GamepadButton, GamepadSettings},
34        keyboard::KeyCode,
35        mouse::MouseButton,
36        touch::{TouchInput, Touches},
37        Axis, ButtonInput,
38    };
39}
40
41use bevy_app::prelude::*;
42use bevy_ecs::prelude::*;
43#[cfg(feature = "bevy_reflect")]
44use bevy_reflect::Reflect;
45use gestures::*;
46use keyboard::{keyboard_input_system, KeyCode, KeyboardFocusLost, KeyboardInput};
47use mouse::{
48    accumulate_mouse_motion_system, accumulate_mouse_scroll_system, mouse_button_input_system,
49    AccumulatedMouseMotion, AccumulatedMouseScroll, MouseButton, MouseButtonInput, MouseMotion,
50    MouseWheel,
51};
52use touch::{touch_screen_input_system, TouchInput, Touches};
53
54#[cfg(feature = "bevy_reflect")]
55use gamepad::Gamepad;
56use gamepad::{
57    gamepad_connection_system, gamepad_event_processing_system, GamepadAxis,
58    GamepadAxisChangedEvent, GamepadButton, GamepadButtonChangedEvent,
59    GamepadButtonStateChangedEvent, GamepadConnection, GamepadConnectionEvent, GamepadEvent,
60    GamepadInput, GamepadRumbleRequest, GamepadSettings, RawGamepadAxisChangedEvent,
61    RawGamepadButtonChangedEvent, RawGamepadEvent,
62};
63
64#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
65use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
66
67/// Adds keyboard and mouse input to an App
68#[derive(Default)]
69pub struct InputPlugin;
70
71/// Label for systems that update the input data.
72#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemSet)]
73pub struct InputSystem;
74
75impl Plugin for InputPlugin {
76    fn build(&self, app: &mut App) {
77        app
78            // keyboard
79            .add_event::<KeyboardInput>()
80            .add_event::<KeyboardFocusLost>()
81            .init_resource::<ButtonInput<KeyCode>>()
82            .add_systems(PreUpdate, keyboard_input_system.in_set(InputSystem))
83            // mouse
84            .add_event::<MouseButtonInput>()
85            .add_event::<MouseMotion>()
86            .add_event::<MouseWheel>()
87            .init_resource::<ButtonInput<MouseButton>>()
88            .add_systems(
89                PreUpdate,
90                (
91                    mouse_button_input_system,
92                    accumulate_mouse_motion_system,
93                    accumulate_mouse_scroll_system,
94                )
95                    .in_set(InputSystem),
96            )
97            .add_event::<PinchGesture>()
98            .add_event::<RotationGesture>()
99            .add_event::<DoubleTapGesture>()
100            .add_event::<PanGesture>()
101            // gamepad
102            .add_event::<GamepadEvent>()
103            .add_event::<GamepadConnectionEvent>()
104            .add_event::<GamepadButtonChangedEvent>()
105            .add_event::<GamepadButtonStateChangedEvent>()
106            .add_event::<GamepadAxisChangedEvent>()
107            .add_event::<RawGamepadEvent>()
108            .add_event::<RawGamepadAxisChangedEvent>()
109            .add_event::<RawGamepadButtonChangedEvent>()
110            .add_event::<GamepadRumbleRequest>()
111            .init_resource::<AccumulatedMouseMotion>()
112            .init_resource::<AccumulatedMouseScroll>()
113            .add_systems(
114                PreUpdate,
115                (
116                    gamepad_connection_system,
117                    gamepad_event_processing_system.after(gamepad_connection_system),
118                )
119                    .in_set(InputSystem),
120            )
121            // touch
122            .add_event::<TouchInput>()
123            .init_resource::<Touches>()
124            .add_systems(PreUpdate, touch_screen_input_system.in_set(InputSystem));
125
126        #[cfg(feature = "bevy_reflect")]
127        {
128            // Register common types
129            app.register_type::<ButtonState>()
130                .register_type::<KeyboardInput>()
131                .register_type::<MouseButtonInput>()
132                .register_type::<PinchGesture>()
133                .register_type::<RotationGesture>()
134                .register_type::<DoubleTapGesture>()
135                .register_type::<PanGesture>()
136                .register_type::<TouchInput>()
137                .register_type::<RawGamepadEvent>()
138                .register_type::<RawGamepadAxisChangedEvent>()
139                .register_type::<RawGamepadButtonChangedEvent>()
140                .register_type::<Gamepad>()
141                .register_type::<GamepadConnectionEvent>()
142                .register_type::<GamepadButtonChangedEvent>()
143                .register_type::<GamepadAxisChangedEvent>()
144                .register_type::<GamepadButtonStateChangedEvent>()
145                .register_type::<GamepadConnection>()
146                .register_type::<GamepadSettings>()
147                .register_type::<GamepadAxis>()
148                .register_type::<GamepadButton>()
149                .register_type::<GamepadInput>()
150                .register_type::<AccumulatedMouseMotion>()
151                .register_type::<AccumulatedMouseScroll>();
152        }
153    }
154}
155
156/// The current "press" state of an element
157#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
158#[cfg_attr(
159    feature = "bevy_reflect",
160    derive(Reflect),
161    reflect(Debug, Hash, PartialEq)
162)]
163#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
164#[cfg_attr(
165    all(feature = "serialize", feature = "bevy_reflect"),
166    reflect(Serialize, Deserialize)
167)]
168pub enum ButtonState {
169    /// The button is pressed.
170    Pressed,
171    /// The button is not pressed.
172    Released,
173}
174
175impl ButtonState {
176    /// Is this button pressed?
177    pub fn is_pressed(&self) -> bool {
178        matches!(self, ButtonState::Pressed)
179    }
180}