bevy/crates/bevy_input/src/lib.rs

103 lines
3 KiB
Rust
Raw Normal View History

mod axis;
pub mod gamepad;
mod input;
2020-04-04 21:59:49 +00:00
pub mod keyboard;
2020-04-05 06:42:39 +00:00
pub mod mouse;
2020-04-19 19:13:04 +00:00
pub mod system;
pub mod touch;
2020-04-04 21:59:49 +00:00
pub use axis::*;
use bevy_ecs::schedule::{ParallelSystemDescriptorCoercion, SystemLabel};
pub use input::*;
2020-07-17 02:27:19 +00:00
pub mod prelude {
#[doc(hidden)]
pub use crate::{
gamepad::{
Gamepad, GamepadAxis, GamepadAxisType, GamepadButton, GamepadButtonType, GamepadEvent,
GamepadEventType, Gamepads,
},
keyboard::KeyCode,
mouse::MouseButton,
touch::{TouchInput, Touches},
Axis, Input,
};
2020-07-17 02:27:19 +00:00
}
2020-07-17 01:47:51 +00:00
use bevy_app::prelude::*;
use keyboard::{keyboard_input_system, KeyCode, KeyboardInput};
use mouse::{mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseWheel};
use prelude::Gamepads;
use touch::{touch_screen_input_system, TouchInput, Touches};
2020-04-04 21:59:49 +00:00
use gamepad::{
gamepad_connection_system, gamepad_event_system, GamepadAxis, GamepadButton, GamepadEvent,
GamepadEventRaw, GamepadSettings,
};
2020-07-10 04:18:35 +00:00
/// Adds keyboard and mouse input to an App
2020-04-04 21:59:49 +00:00
#[derive(Default)]
pub struct InputPlugin;
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemLabel)]
pub struct InputSystem;
2020-08-08 03:22:17 +00:00
impl Plugin for InputPlugin {
fn build(&self, app: &mut App) {
app
// keyboard
.add_event::<KeyboardInput>()
.init_resource::<Input<KeyCode>>()
.add_system_to_stage(
CoreStage::PreUpdate,
keyboard_input_system.label(InputSystem),
)
// mouse
2020-04-05 07:32:53 +00:00
.add_event::<MouseButtonInput>()
.add_event::<MouseMotion>()
.add_event::<MouseWheel>()
.init_resource::<Input<MouseButton>>()
.add_system_to_stage(
CoreStage::PreUpdate,
mouse_button_input_system.label(InputSystem),
)
// gamepad
.add_event::<GamepadEvent>()
.add_event::<GamepadEventRaw>()
.init_resource::<GamepadSettings>()
.init_resource::<Gamepads>()
.init_resource::<Input<GamepadButton>>()
.init_resource::<Axis<GamepadAxis>>()
.init_resource::<Axis<GamepadButton>>()
.add_system_to_stage(
CoreStage::PreUpdate,
gamepad_event_system.label(InputSystem),
)
.add_system_to_stage(
CoreStage::PreUpdate,
gamepad_connection_system.label(InputSystem),
)
// touch
.add_event::<TouchInput>()
.init_resource::<Touches>()
.add_system_to_stage(
CoreStage::PreUpdate,
touch_screen_input_system.label(InputSystem),
);
2020-04-04 21:59:49 +00:00
}
}
/// The current "press" state of an element
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum ElementState {
Pressed,
Released,
}
impl ElementState {
pub fn is_pressed(&self) -> bool {
matches!(self, ElementState::Pressed)
}
}