mouse motion
This commit is contained in:
parent
ec65cfef4a
commit
6d53100ff3
@ -1,4 +1,4 @@
|
|||||||
use bevy::{input::mouse::MouseInput, prelude::*};
|
use bevy::{input::mouse::{MouseButtonInput, MouseMotion}, prelude::*};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::build()
|
App::build()
|
||||||
@ -8,12 +8,18 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn mouse_input_system(resources: &mut Resources) -> Box<dyn Schedulable> {
|
pub fn mouse_input_system(resources: &mut Resources) -> Box<dyn Schedulable> {
|
||||||
let mut mouse_input_event_reader = resources.get_event_reader::<MouseInput>();
|
let mut mouse_button_input_event_reader = resources.get_event_reader::<MouseButtonInput>();
|
||||||
|
let mut mouse_motion_event_reader = resources.get_event_reader::<MouseMotion>();
|
||||||
SystemBuilder::new("mouse_input")
|
SystemBuilder::new("mouse_input")
|
||||||
.read_resource::<Events<MouseInput>>()
|
.read_resource::<Events<MouseButtonInput>>()
|
||||||
|
.read_resource::<Events<MouseMotion>>()
|
||||||
.build(
|
.build(
|
||||||
move |_command_buffer, _world, mouse_input_events, _queries| {
|
move |_command_buffer, _world, (mouse_button_input_events, mouse_motion_events), _queries| {
|
||||||
for event in mouse_input_events.iter(&mut mouse_input_event_reader) {
|
for event in mouse_button_input_events.iter(&mut mouse_button_input_event_reader) {
|
||||||
|
println!("{:?}", event);
|
||||||
|
}
|
||||||
|
|
||||||
|
for event in mouse_motion_events.iter(&mut mouse_motion_event_reader) {
|
||||||
println!("{:?}", event);
|
println!("{:?}", event);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -3,7 +3,7 @@ pub mod mouse;
|
|||||||
|
|
||||||
use crate::{app::AppBuilder, prelude::AppPlugin};
|
use crate::{app::AppBuilder, prelude::AppPlugin};
|
||||||
use keyboard::KeyboardInput;
|
use keyboard::KeyboardInput;
|
||||||
use mouse::MouseInput;
|
use mouse::{MouseButtonInput, MouseMotion};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct InputPlugin;
|
pub struct InputPlugin;
|
||||||
@ -11,7 +11,8 @@ pub struct InputPlugin;
|
|||||||
impl AppPlugin for InputPlugin {
|
impl AppPlugin for InputPlugin {
|
||||||
fn build(&self, app: AppBuilder) -> AppBuilder {
|
fn build(&self, app: AppBuilder) -> AppBuilder {
|
||||||
app.add_event::<KeyboardInput>()
|
app.add_event::<KeyboardInput>()
|
||||||
.add_event::<MouseInput>()
|
.add_event::<MouseButtonInput>()
|
||||||
|
.add_event::<MouseMotion>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use super::keyboard::ElementState;
|
use super::keyboard::ElementState;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct MouseInput {
|
pub struct MouseButtonInput {
|
||||||
pub button: MouseButton,
|
pub button: MouseButton,
|
||||||
pub state: ElementState,
|
pub state: ElementState,
|
||||||
}
|
}
|
||||||
@ -12,4 +12,9 @@ pub enum MouseButton {
|
|||||||
Right,
|
Right,
|
||||||
Middle,
|
Middle,
|
||||||
Other(u8),
|
Other(u8),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct MouseMotion {
|
||||||
|
pub delta: (f64, f64),
|
||||||
}
|
}
|
@ -3,14 +3,17 @@ mod winit_windows;
|
|||||||
pub use winit_windows::*;
|
pub use winit_windows::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
input::{keyboard::KeyboardInput, mouse::MouseInput},
|
input::{
|
||||||
|
keyboard::KeyboardInput,
|
||||||
|
mouse::{MouseMotion, MouseButtonInput},
|
||||||
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{CreateWindow, Window, WindowCreated, WindowResized, Windows};
|
use super::{CreateWindow, Window, WindowCreated, WindowResized, Windows};
|
||||||
use winit::{
|
use winit::{
|
||||||
event,
|
event,
|
||||||
event::WindowEvent,
|
event::{DeviceEvent, WindowEvent},
|
||||||
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
|
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -85,15 +88,23 @@ pub fn winit_runner(mut app: App) {
|
|||||||
keyboard_input_events.send(input.into());
|
keyboard_input_events.send(input.into());
|
||||||
}
|
}
|
||||||
WindowEvent::MouseInput { state, button, .. } => {
|
WindowEvent::MouseInput { state, button, .. } => {
|
||||||
let mut mouse_input_events =
|
let mut mouse_button_input_events =
|
||||||
app.resources.get_mut::<Events<MouseInput>>().unwrap();
|
app.resources.get_mut::<Events<MouseButtonInput>>().unwrap();
|
||||||
mouse_input_events.send(MouseInput {
|
mouse_button_input_events.send(MouseButtonInput {
|
||||||
button: button.into(),
|
button: button.into(),
|
||||||
state: state.into(),
|
state: state.into(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
|
event::Event::DeviceEvent { ref event, .. } => match event {
|
||||||
|
DeviceEvent::MouseMotion { delta } => {
|
||||||
|
let mut mouse_motion_events =
|
||||||
|
app.resources.get_mut::<Events<MouseMotion>>().unwrap();
|
||||||
|
mouse_motion_events.send(MouseMotion { delta: *delta });
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
event::Event::MainEventsCleared => {
|
event::Event::MainEventsCleared => {
|
||||||
handle_create_window_events(
|
handle_create_window_events(
|
||||||
&mut app.resources,
|
&mut app.resources,
|
||||||
|
Loading…
Reference in New Issue
Block a user