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