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 bevy_ecs::entity::Entity;
|
||||
use bevy_ecs::{
|
||||
change_detection::DetectChangesMut,
|
||||
event::{Event, EventReader},
|
||||
@ -32,6 +33,8 @@ pub struct KeyboardInput {
|
||||
pub key_code: Option<KeyCode>,
|
||||
/// The press state of the key.
|
||||
pub state: ButtonState,
|
||||
/// Window that received the input.
|
||||
pub window: Entity,
|
||||
}
|
||||
|
||||
/// Updates the [`Input<KeyCode>`] resource with the latest [`KeyboardInput`] events.
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::{ButtonState, Input};
|
||||
use bevy_ecs::entity::Entity;
|
||||
use bevy_ecs::{
|
||||
change_detection::DetectChangesMut,
|
||||
event::{Event, EventReader},
|
||||
@ -30,6 +31,8 @@ pub struct MouseButtonInput {
|
||||
pub button: MouseButton,
|
||||
/// The pressed state of the button.
|
||||
pub state: ButtonState,
|
||||
/// Window that received the input.
|
||||
pub window: Entity,
|
||||
}
|
||||
|
||||
/// A button on a mouse device.
|
||||
@ -124,6 +127,8 @@ pub struct MouseWheel {
|
||||
pub x: f32,
|
||||
/// The vertical scroll value.
|
||||
pub y: f32,
|
||||
/// Window that received the input.
|
||||
pub window: Entity,
|
||||
}
|
||||
|
||||
/// Updates the [`Input<MouseButton>`] resource with the latest [`MouseButtonInput`] events.
|
||||
|
@ -1,3 +1,4 @@
|
||||
use bevy_ecs::entity::Entity;
|
||||
use bevy_input::{
|
||||
keyboard::{KeyCode, KeyboardInput},
|
||||
mouse::MouseButton,
|
||||
@ -7,11 +8,15 @@ use bevy_input::{
|
||||
use bevy_math::Vec2;
|
||||
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 {
|
||||
scan_code: keyboard_input.scancode,
|
||||
state: convert_element_state(keyboard_input.state),
|
||||
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, .. } => {
|
||||
input_events
|
||||
.keyboard_input
|
||||
.send(converters::convert_keyboard_input(input));
|
||||
.send(converters::convert_keyboard_input(input, window_entity));
|
||||
}
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
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 {
|
||||
button: converters::convert_mouse_button(button),
|
||||
state: converters::convert_element_state(state),
|
||||
window: window_entity,
|
||||
});
|
||||
}
|
||||
WindowEvent::TouchpadMagnify { delta, .. } => {
|
||||
@ -500,6 +501,7 @@ pub fn winit_runner(mut app: App) {
|
||||
unit: MouseScrollUnit::Line,
|
||||
x,
|
||||
y,
|
||||
window: window_entity,
|
||||
});
|
||||
}
|
||||
event::MouseScrollDelta::PixelDelta(p) => {
|
||||
@ -507,6 +509,7 @@ pub fn winit_runner(mut app: App) {
|
||||
unit: MouseScrollUnit::Pixel,
|
||||
x: p.x as f32,
|
||||
y: p.y as f32,
|
||||
window: window_entity,
|
||||
});
|
||||
}
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user