Add window entity to mouse and keyboard events (#8852)
# Objective - Resolves #4649 ## Solution - Added the window entity to `KeyboardInput`, `MouseButtonInput`, and `MouseWheel` events.
This commit is contained in:
parent
8ec81496ff
commit
84de9e7f28
@ -1,4 +1,5 @@
|
|||||||
use crate::{ButtonState, Input};
|
use crate::{ButtonState, Input};
|
||||||
|
use bevy_ecs::entity::Entity;
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
change_detection::DetectChangesMut,
|
change_detection::DetectChangesMut,
|
||||||
event::{Event, EventReader},
|
event::{Event, EventReader},
|
||||||
@ -32,6 +33,8 @@ pub struct KeyboardInput {
|
|||||||
pub key_code: Option<KeyCode>,
|
pub key_code: Option<KeyCode>,
|
||||||
/// The press state of the key.
|
/// The press state of the key.
|
||||||
pub state: ButtonState,
|
pub state: ButtonState,
|
||||||
|
/// Window that received the input.
|
||||||
|
pub window: Entity,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates the [`Input<KeyCode>`] resource with the latest [`KeyboardInput`] events.
|
/// Updates the [`Input<KeyCode>`] resource with the latest [`KeyboardInput`] events.
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use crate::{ButtonState, Input};
|
use crate::{ButtonState, Input};
|
||||||
|
use bevy_ecs::entity::Entity;
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
change_detection::DetectChangesMut,
|
change_detection::DetectChangesMut,
|
||||||
event::{Event, EventReader},
|
event::{Event, EventReader},
|
||||||
@ -30,6 +31,8 @@ pub struct MouseButtonInput {
|
|||||||
pub button: MouseButton,
|
pub button: MouseButton,
|
||||||
/// The pressed state of the button.
|
/// The pressed state of the button.
|
||||||
pub state: ButtonState,
|
pub state: ButtonState,
|
||||||
|
/// Window that received the input.
|
||||||
|
pub window: Entity,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A button on a mouse device.
|
/// A button on a mouse device.
|
||||||
@ -124,6 +127,8 @@ pub struct MouseWheel {
|
|||||||
pub x: f32,
|
pub x: f32,
|
||||||
/// The vertical scroll value.
|
/// The vertical scroll value.
|
||||||
pub y: f32,
|
pub y: f32,
|
||||||
|
/// Window that received the input.
|
||||||
|
pub window: Entity,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates the [`Input<MouseButton>`] resource with the latest [`MouseButtonInput`] events.
|
/// Updates the [`Input<MouseButton>`] resource with the latest [`MouseButtonInput`] events.
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use bevy_ecs::entity::Entity;
|
||||||
use bevy_input::{
|
use bevy_input::{
|
||||||
keyboard::{KeyCode, KeyboardInput},
|
keyboard::{KeyCode, KeyboardInput},
|
||||||
mouse::MouseButton,
|
mouse::MouseButton,
|
||||||
@ -7,11 +8,15 @@ use bevy_input::{
|
|||||||
use bevy_math::Vec2;
|
use bevy_math::Vec2;
|
||||||
use bevy_window::{CursorIcon, WindowLevel, WindowTheme};
|
use bevy_window::{CursorIcon, WindowLevel, WindowTheme};
|
||||||
|
|
||||||
pub fn convert_keyboard_input(keyboard_input: &winit::event::KeyboardInput) -> KeyboardInput {
|
pub fn convert_keyboard_input(
|
||||||
|
keyboard_input: &winit::event::KeyboardInput,
|
||||||
|
window: Entity,
|
||||||
|
) -> KeyboardInput {
|
||||||
KeyboardInput {
|
KeyboardInput {
|
||||||
scan_code: keyboard_input.scancode,
|
scan_code: keyboard_input.scancode,
|
||||||
state: convert_element_state(keyboard_input.state),
|
state: convert_element_state(keyboard_input.state),
|
||||||
key_code: keyboard_input.virtual_keycode.map(convert_virtual_key_code),
|
key_code: keyboard_input.virtual_keycode.map(convert_virtual_key_code),
|
||||||
|
window,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -453,7 +453,7 @@ pub fn winit_runner(mut app: App) {
|
|||||||
WindowEvent::KeyboardInput { ref input, .. } => {
|
WindowEvent::KeyboardInput { ref input, .. } => {
|
||||||
input_events
|
input_events
|
||||||
.keyboard_input
|
.keyboard_input
|
||||||
.send(converters::convert_keyboard_input(input));
|
.send(converters::convert_keyboard_input(input, window_entity));
|
||||||
}
|
}
|
||||||
WindowEvent::CursorMoved { position, .. } => {
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
let physical_position = DVec2::new(position.x, position.y);
|
let physical_position = DVec2::new(position.x, position.y);
|
||||||
@ -482,6 +482,7 @@ pub fn winit_runner(mut app: App) {
|
|||||||
input_events.mouse_button_input.send(MouseButtonInput {
|
input_events.mouse_button_input.send(MouseButtonInput {
|
||||||
button: converters::convert_mouse_button(button),
|
button: converters::convert_mouse_button(button),
|
||||||
state: converters::convert_element_state(state),
|
state: converters::convert_element_state(state),
|
||||||
|
window: window_entity,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
WindowEvent::TouchpadMagnify { delta, .. } => {
|
WindowEvent::TouchpadMagnify { delta, .. } => {
|
||||||
@ -500,6 +501,7 @@ pub fn winit_runner(mut app: App) {
|
|||||||
unit: MouseScrollUnit::Line,
|
unit: MouseScrollUnit::Line,
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
|
window: window_entity,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
event::MouseScrollDelta::PixelDelta(p) => {
|
event::MouseScrollDelta::PixelDelta(p) => {
|
||||||
@ -507,6 +509,7 @@ pub fn winit_runner(mut app: App) {
|
|||||||
unit: MouseScrollUnit::Pixel,
|
unit: MouseScrollUnit::Pixel,
|
||||||
x: p.x as f32,
|
x: p.x as f32,
|
||||||
y: p.y as f32,
|
y: p.y as f32,
|
||||||
|
window: window_entity,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user