Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_input/src/lib.rs
6595 views
1
#![cfg_attr(docsrs, feature(doc_auto_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")]
16
extern crate std;
17
18
extern crate alloc;
19
20
mod axis;
21
mod button_input;
22
/// Common run conditions
23
pub mod common_conditions;
24
pub mod gamepad;
25
pub mod gestures;
26
pub mod keyboard;
27
pub mod mouse;
28
pub mod touch;
29
30
pub use axis::*;
31
pub use button_input::*;
32
33
/// The input prelude.
34
///
35
/// This includes the most common types in this crate, re-exported for your convenience.
36
pub 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
47
use bevy_app::prelude::*;
48
use bevy_ecs::prelude::*;
49
#[cfg(feature = "bevy_reflect")]
50
use bevy_reflect::Reflect;
51
use gestures::*;
52
use keyboard::{keyboard_input_system, Key, KeyCode, KeyboardFocusLost, KeyboardInput};
53
use mouse::{
54
accumulate_mouse_motion_system, accumulate_mouse_scroll_system, mouse_button_input_system,
55
AccumulatedMouseMotion, AccumulatedMouseScroll, MouseButton, MouseButtonInput, MouseMotion,
56
MouseWheel,
57
};
58
use touch::{touch_screen_input_system, TouchInput, Touches};
59
60
use 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"))]
68
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
69
70
/// Adds keyboard and mouse input to an App
71
#[derive(Default)]
72
pub struct InputPlugin;
73
74
/// Label for systems that update the input data.
75
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemSet)]
76
pub struct InputSystems;
77
78
/// Deprecated alias for [`InputSystems`].
79
#[deprecated(since = "0.17.0", note = "Renamed to `InputSystems`.")]
80
pub type InputSystem = InputSystems;
81
82
impl Plugin for InputPlugin {
83
fn build(&self, app: &mut App) {
84
app
85
// keyboard
86
.add_event::<KeyboardInput>()
87
.add_event::<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_event::<MouseButtonInput>()
93
.add_event::<MouseMotion>()
94
.add_event::<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_event::<PinchGesture>()
106
.add_event::<RotationGesture>()
107
.add_event::<DoubleTapGesture>()
108
.add_event::<PanGesture>()
109
// gamepad
110
.add_event::<GamepadEvent>()
111
.add_event::<GamepadConnectionEvent>()
112
.add_event::<GamepadButtonChangedEvent>()
113
.add_event::<GamepadButtonStateChangedEvent>()
114
.add_event::<GamepadAxisChangedEvent>()
115
.add_event::<RawGamepadEvent>()
116
.add_event::<RawGamepadAxisChangedEvent>()
117
.add_event::<RawGamepadButtonChangedEvent>()
118
.add_event::<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_event::<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
)]
148
pub enum ButtonState {
149
/// The button is pressed.
150
Pressed,
151
/// The button is not pressed.
152
Released,
153
}
154
155
impl ButtonState {
156
/// Is this button pressed?
157
pub fn is_pressed(&self) -> bool {
158
matches!(self, ButtonState::Pressed)
159
}
160
}
161
162