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