Skip to content

Commit

Permalink
Add window id to mouse and keyboard events
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Jun 15, 2023
1 parent 5291110 commit c93ddf6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions crates/bevy_input/src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{ButtonState, Input};
use bevy_ecs::entity::Entity;
use bevy_ecs::{
change_detection::DetectChangesMut,
event::{Event, EventReader},
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_input/src/mouse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{ButtonState, Input};
use bevy_ecs::entity::Entity;
use bevy_ecs::{
change_detection::DetectChangesMut,
event::{Event, EventReader},
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_winit/src/converters.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy_ecs::entity::Entity;
use bevy_input::{
keyboard::{KeyCode, KeyboardInput},
mouse::MouseButton,
Expand All @@ -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,
}
}

Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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, .. } => {
Expand All @@ -500,13 +501,15 @@ pub fn winit_runner(mut app: App) {
unit: MouseScrollUnit::Line,
x,
y,
window: window_entity,
});
}
event::MouseScrollDelta::PixelDelta(p) => {
input_events.mouse_wheel_input.send(MouseWheel {
unit: MouseScrollUnit::Pixel,
x: p.x as f32,
y: p.y as f32,
window: window_entity,
});
}
},
Expand Down

0 comments on commit c93ddf6

Please sign in to comment.