bevy_input/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![forbid(unsafe_code)]
3#![doc(
4    html_logo_url = "https://bevy.org/assets/icon.png",
5    html_favicon_url = "https://bevy.org/assets/icon.png"
6)]
7#![no_std]
8
9//! Input functionality for the [Bevy game engine](https://bevy.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, Key, 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
60use gamepad::{
61    gamepad_connection_system, gamepad_event_processing_system, GamepadAxisChangedEvent,
62    GamepadButtonChangedEvent, GamepadButtonStateChangedEvent, GamepadConnectionEvent,
63    GamepadEvent, GamepadRumbleRequest, RawGamepadAxisChangedEvent, RawGamepadButtonChangedEvent,
64    RawGamepadEvent,
65};
66
67#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
68use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
69
70/// Adds keyboard and mouse input to an App
71#[derive(Default)]
72pub struct InputPlugin;
73
74/// Label for systems that update the input data.
75#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemSet)]
76pub struct InputSystems;
77
78/// Deprecated alias for [`InputSystems`].
79#[deprecated(since = "0.17.0", note = "Renamed to `InputSystems`.")]
80pub type InputSystem = InputSystems;
81
82impl Plugin for InputPlugin {
83    fn build(&self, app: &mut App) {
84        app
85            // keyboard
86            .add_message::<KeyboardInput>()
87            .add_message::<KeyboardFocusLost>()
88            .init_resource::<ButtonInput<KeyCode>>()
89            .init_resource::<ButtonInput<Key>>()
90            .add_systems(PreUpdate, keyboard_input_system.in_set(InputSystems))
91            // mouse
92            .add_message::<MouseButtonInput>()
93            .add_message::<MouseMotion>()
94            .add_message::<MouseWheel>()
95            .init_resource::<ButtonInput<MouseButton>>()
96            .add_systems(
97                PreUpdate,
98                (
99                    mouse_button_input_system,
100                    accumulate_mouse_motion_system,
101                    accumulate_mouse_scroll_system,
102                )
103                    .in_set(InputSystems),
104            )
105            .add_message::<PinchGesture>()
106            .add_message::<RotationGesture>()
107            .add_message::<DoubleTapGesture>()
108            .add_message::<PanGesture>()
109            // gamepad
110            .add_message::<GamepadEvent>()
111            .add_message::<GamepadConnectionEvent>()
112            .add_message::<GamepadButtonChangedEvent>()
113            .add_message::<GamepadButtonStateChangedEvent>()
114            .add_message::<GamepadAxisChangedEvent>()
115            .add_message::<RawGamepadEvent>()
116            .add_message::<RawGamepadAxisChangedEvent>()
117            .add_message::<RawGamepadButtonChangedEvent>()
118            .add_message::<GamepadRumbleRequest>()
119            .init_resource::<AccumulatedMouseMotion>()
120            .init_resource::<AccumulatedMouseScroll>()
121            .add_systems(
122                PreUpdate,
123                (
124                    gamepad_connection_system,
125                    gamepad_event_processing_system.after(gamepad_connection_system),
126                )
127                    .in_set(InputSystems),
128            )
129            // touch
130            .add_message::<TouchInput>()
131            .init_resource::<Touches>()
132            .add_systems(PreUpdate, touch_screen_input_system.in_set(InputSystems));
133    }
134}
135
136/// The current "press" state of an element
137#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
138#[cfg_attr(
139    feature = "bevy_reflect",
140    derive(Reflect),
141    reflect(Debug, Hash, PartialEq, Clone)
142)]
143#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
144#[cfg_attr(
145    all(feature = "serialize", feature = "bevy_reflect"),
146    reflect(Serialize, Deserialize)
147)]
148pub enum ButtonState {
149    /// The button is pressed.
150    Pressed,
151    /// The button is not pressed.
152    Released,
153}
154
155impl ButtonState {
156    /// Is this button pressed?
157    pub fn is_pressed(&self) -> bool {
158        matches!(self, ButtonState::Pressed)
159    }
160}