oxedyne/fe2o3/fe2o3_text/tests/annealer_corpus/bevy_input.rs
5.7 KiB, 1 run
created by r1870400018:11700, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 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")] |
| 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 | |
| 25 | #[cfg(feature = "gamepad")] |
| 26 | pub mod gamepad; |
| 27 | |
| 28 | #[cfg(feature = "gestures")] |
| 29 | pub mod gestures; |
| 30 | |
| 31 | #[cfg(feature = "keyboard")] |
| 32 | pub mod keyboard; |
| 33 | |
| 34 | #[cfg(feature = "mouse")] |
| 35 | pub mod mouse; |
| 36 | |
| 37 | // Also enabled with `mouse` because `MouseWheel` reuses `TouchPhase` for trackpad scroll phases. |
| 38 | #[cfg(any(feature = "touch", feature = "mouse"))] |
| 39 | pub mod touch; |
| 40 | |
| 41 | pub use axis::*; |
| 42 | pub use button_input::*; |
| 43 | |
| 44 | /// The input prelude. |
| 45 | /// |
| 46 | /// This includes the most common types in this crate, re-exported for your convenience. |
| 47 | pub mod prelude { |
| 48 | #[doc(hidden)] |
| 49 | pub use crate::{Axis, ButtonInput}; |
| 50 | |
| 51 | #[doc(hidden)] |
| 52 | #[cfg(feature = "gamepad")] |
| 53 | pub use crate::gamepad::{Gamepad, GamepadAxis, GamepadButton, GamepadSettings}; |
| 54 | |
| 55 | #[doc(hidden)] |
| 56 | #[cfg(feature = "keyboard")] |
| 57 | pub use crate::keyboard::KeyCode; |
| 58 | |
| 59 | #[doc(hidden)] |
| 60 | #[cfg(feature = "mouse")] |
| 61 | pub use crate::mouse::MouseButton; |
| 62 | |
| 63 | #[doc(hidden)] |
| 64 | #[cfg(feature = "touch")] |
| 65 | pub use crate::touch::{TouchInput, Touches}; |
| 66 | } |
| 67 | |
| 68 | use bevy_app::prelude::*; |
| 69 | use bevy_ecs::prelude::*; |
| 70 | #[cfg(feature = "bevy_reflect")] |
| 71 | use bevy_reflect::Reflect; |
| 72 | |
| 73 | #[cfg(feature = "gestures")] |
| 74 | use gestures::*; |
| 75 | |
| 76 | #[cfg(feature = "keyboard")] |
| 77 | use keyboard::{keyboard_input_system, Key, KeyCode, KeyboardFocusLost, KeyboardInput}; |
| 78 | |
| 79 | #[cfg(feature = "mouse")] |
| 80 | use mouse::{ |
| 81 | accumulate_mouse_motion_system, accumulate_mouse_scroll_system, mouse_button_input_system, |
| 82 | AccumulatedMouseMotion, AccumulatedMouseScroll, MouseButton, MouseButtonInput, MouseMotion, |
| 83 | MouseWheel, |
| 84 | }; |
| 85 | |
| 86 | #[cfg(feature = "touch")] |
| 87 | use touch::{touch_screen_input_system, TouchInput, Touches}; |
| 88 | |
| 89 | #[cfg(feature = "gamepad")] |
| 90 | use gamepad::{ |
| 91 | gamepad_connection_system, gamepad_event_processing_system, GamepadAxisChangedEvent, |
| 92 | GamepadButtonChangedEvent, GamepadButtonStateChangedEvent, GamepadConnectionEvent, |
| 93 | GamepadEvent, GamepadRumbleRequest, RawGamepadAxisChangedEvent, RawGamepadButtonChangedEvent, |
| 94 | RawGamepadEvent, |
| 95 | }; |
| 96 | |
| 97 | #[cfg(all(feature = "serialize", feature = "bevy_reflect"))] |
| 98 | use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; |
| 99 | |
| 100 | /// Adds input from various sources to an App |
| 101 | #[derive(Default)] |
| 102 | pub struct InputPlugin; |
| 103 | |
| 104 | /// Label for systems that update the input data. |
| 105 | #[derive(Debug, PartialEq, Eq, Clone, Hash, SystemSet)] |
| 106 | pub struct InputSystems; |
| 107 | |
| 108 | impl Plugin for InputPlugin { |
| 109 | #[expect(clippy::allow_attributes, reason = "this is only sometimes unused")] |
| 110 | #[allow(unused, reason = "all features could be disabled")] |
| 111 | fn build(&self, app: &mut App) { |
| 112 | #[cfg(feature = "keyboard")] |
| 113 | app.add_message::<KeyboardInput>() |
| 114 | .add_message::<KeyboardFocusLost>() |
| 115 | .init_resource::<ButtonInput<KeyCode>>() |
| 116 | .init_resource::<ButtonInput<Key>>() |
| 117 | .add_systems(PreUpdate, keyboard_input_system.in_set(InputSystems)); |
| 118 | |
| 119 | #[cfg(feature = "mouse")] |
| 120 | app.add_message::<MouseButtonInput>() |
| 121 | .add_message::<MouseMotion>() |
| 122 | .add_message::<MouseWheel>() |
| 123 | .init_resource::<AccumulatedMouseMotion>() |
| 124 | .init_resource::<AccumulatedMouseScroll>() |
| 125 | .init_resource::<ButtonInput<MouseButton>>() |
| 126 | .add_systems( |
| 127 | PreUpdate, |
| 128 | ( |
| 129 | mouse_button_input_system, |
| 130 | accumulate_mouse_motion_system, |
| 131 | accumulate_mouse_scroll_system, |
| 132 | ) |
| 133 | .in_set(InputSystems), |
| 134 | ); |
| 135 | |
| 136 | #[cfg(feature = "gestures")] |
| 137 | app.add_message::<PinchGesture>() |
| 138 | .add_message::<RotationGesture>() |
| 139 | .add_message::<DoubleTapGesture>() |
| 140 | .add_message::<PanGesture>(); |
| 141 | |
| 142 | #[cfg(feature = "gamepad")] |
| 143 | app.add_message::<GamepadEvent>() |
| 144 | .add_message::<GamepadConnectionEvent>() |
| 145 | .add_message::<GamepadButtonChangedEvent>() |
| 146 | .add_message::<GamepadButtonStateChangedEvent>() |
| 147 | .add_message::<GamepadAxisChangedEvent>() |
| 148 | .add_message::<RawGamepadEvent>() |
| 149 | .add_message::<RawGamepadAxisChangedEvent>() |
| 150 | .add_message::<RawGamepadButtonChangedEvent>() |
| 151 | .add_message::<GamepadRumbleRequest>() |
| 152 | .add_systems( |
| 153 | PreUpdate, |
| 154 | ( |
| 155 | gamepad_connection_system, |
| 156 | gamepad_event_processing_system.after(gamepad_connection_system), |
| 157 | ) |
| 158 | .in_set(InputSystems), |
| 159 | ); |
| 160 | |
| 161 | #[cfg(feature = "touch")] |
| 162 | app.add_message::<TouchInput>() |
| 163 | .init_resource::<Touches>() |
| 164 | .add_systems(PreUpdate, touch_screen_input_system.in_set(InputSystems)); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// The current "press" state of an element |
| 169 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
| 170 | #[cfg_attr( |
| 171 | feature = "bevy_reflect", |
| 172 | derive(Reflect), |
| 173 | reflect(Debug, Hash, PartialEq, Clone) |
| 174 | )] |
| 175 | #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] |
| 176 | #[cfg_attr( |
| 177 | all(feature = "serialize", feature = "bevy_reflect"), |
| 178 | reflect(Serialize, Deserialize) |
| 179 | )] |
| 180 | pub enum ButtonState { |
| 181 | /// The button is pressed. |
| 182 | Pressed, |
| 183 | /// The button is not pressed. |
| 184 | Released, |
| 185 | } |
| 186 | |
| 187 | impl ButtonState { |
| 188 | /// Is this button pressed? |
| 189 | pub fn is_pressed(&self) -> bool { |
| 190 | matches!(self, ButtonState::Pressed) |
| 191 | } |
| 192 | } |