Add mouse input

This commit is contained in:
Carter Anderson 2020-04-04 23:42:39 -07:00
parent 5b83698738
commit ec65cfef4a
7 changed files with 70 additions and 9 deletions

21
examples/input_mouse.rs Normal file
View file

@ -0,0 +1,21 @@
use bevy::{input::mouse::MouseInput, prelude::*};
fn main() {
App::build()
.add_default_plugins()
.build_system(mouse_input_system)
.run();
}
pub fn mouse_input_system(resources: &mut Resources) -> Box<dyn Schedulable> {
let mut mouse_input_event_reader = resources.get_event_reader::<MouseInput>();
SystemBuilder::new("mouse_input")
.read_resource::<Events<MouseInput>>()
.build(
move |_command_buffer, _world, mouse_input_events, _queries| {
for event in mouse_input_events.iter(&mut mouse_input_event_reader) {
println!("{:?}", event);
}
},
)
}

View file

@ -2,19 +2,19 @@
pub struct KeyboardInput {
pub scan_code: u32,
pub virtual_key_code: Option<VirtualKeyCode>,
pub state: KeyState,
pub state: ElementState,
}
#[derive(Debug, Clone)]
pub enum KeyState {
pub enum ElementState {
Pressed,
Released,
}
impl KeyState {
impl ElementState {
pub fn is_pressed(&self) -> bool {
match self {
KeyState::Pressed => true,
ElementState::Pressed => true,
_ => false,
}
}

View file

@ -1,7 +1,9 @@
pub mod keyboard;
pub mod mouse;
use crate::{app::AppBuilder, prelude::AppPlugin};
use keyboard::KeyboardInput;
use mouse::MouseInput;
#[derive(Default)]
pub struct InputPlugin;
@ -9,6 +11,7 @@ pub struct InputPlugin;
impl AppPlugin for InputPlugin {
fn build(&self, app: AppBuilder) -> AppBuilder {
app.add_event::<KeyboardInput>()
.add_event::<MouseInput>()
}
fn name(&self) -> &str {

15
src/input/mouse.rs Normal file
View file

@ -0,0 +1,15 @@
use super::keyboard::ElementState;
#[derive(Debug, Clone)]
pub struct MouseInput {
pub button: MouseButton,
pub state: ElementState,
}
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub enum MouseButton {
Left,
Right,
Middle,
Other(u8),
}

View file

@ -1,4 +1,4 @@
use crate::input::keyboard::{KeyState, KeyboardInput, VirtualKeyCode};
use crate::input::{mouse::MouseButton, keyboard::{ElementState, KeyboardInput, VirtualKeyCode}};
impl From<&winit::event::KeyboardInput> for KeyboardInput {
fn from(keyboard_input: &winit::event::KeyboardInput) -> Self {
@ -10,11 +10,22 @@ impl From<&winit::event::KeyboardInput> for KeyboardInput {
}
}
impl From<winit::event::ElementState> for KeyState {
impl From<winit::event::ElementState> for ElementState {
fn from(element_state: winit::event::ElementState) -> Self {
match element_state {
winit::event::ElementState::Pressed => KeyState::Pressed,
winit::event::ElementState::Released => KeyState::Released,
winit::event::ElementState::Pressed => ElementState::Pressed,
winit::event::ElementState::Released => ElementState::Released,
}
}
}
impl From<winit::event::MouseButton> for MouseButton {
fn from(mouse_button: winit::event::MouseButton) -> Self {
match mouse_button {
winit::event::MouseButton::Left => MouseButton::Left,
winit::event::MouseButton::Right => MouseButton::Right,
winit::event::MouseButton::Middle => MouseButton::Middle,
winit::event::MouseButton::Other(val) => MouseButton::Other(val),
}
}
}

View file

@ -2,7 +2,10 @@ mod converters;
mod winit_windows;
pub use winit_windows::*;
use crate::{input::keyboard::KeyboardInput, prelude::*};
use crate::{
input::{keyboard::KeyboardInput, mouse::MouseInput},
prelude::*,
};
use super::{CreateWindow, Window, WindowCreated, WindowResized, Windows};
use winit::{
@ -81,6 +84,14 @@ pub fn winit_runner(mut app: App) {
app.resources.get_mut::<Events<KeyboardInput>>().unwrap();
keyboard_input_events.send(input.into());
}
WindowEvent::MouseInput { state, button, .. } => {
let mut mouse_input_events =
app.resources.get_mut::<Events<MouseInput>>().unwrap();
mouse_input_events.send(MouseInput {
button: button.into(),
state: state.into(),
});
}
_ => {}
},
event::Event::MainEventsCleared => {