Add option to ignore events when receving unknown WindowId (#1072)
Ignore window events with unknown window id
This commit is contained in:
parent
f574c2c547
commit
909b396b9e
@ -12,7 +12,7 @@ pub use winit_windows::*;
|
|||||||
use bevy_app::{prelude::*, AppExit};
|
use bevy_app::{prelude::*, AppExit};
|
||||||
use bevy_ecs::{IntoSystem, Resources, World};
|
use bevy_ecs::{IntoSystem, Resources, World};
|
||||||
use bevy_math::Vec2;
|
use bevy_math::Vec2;
|
||||||
use bevy_utils::tracing::{error, trace};
|
use bevy_utils::tracing::{error, trace, warn};
|
||||||
use bevy_window::{
|
use bevy_window::{
|
||||||
CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, WindowCloseRequested,
|
CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, WindowCloseRequested,
|
||||||
WindowCreated, WindowFocused, WindowResized, Windows,
|
WindowCreated, WindowFocused, WindowResized, Windows,
|
||||||
@ -210,12 +210,29 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
|
|||||||
event,
|
event,
|
||||||
window_id: winit_window_id,
|
window_id: winit_window_id,
|
||||||
..
|
..
|
||||||
} => match event {
|
} => {
|
||||||
WindowEvent::Resized(size) => {
|
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
||||||
let mut windows = app.resources.get_mut::<Windows>().unwrap();
|
let mut windows = app.resources.get_mut::<Windows>().unwrap();
|
||||||
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
let window_id =
|
||||||
let window = windows.get_mut(window_id).unwrap();
|
if let Some(window_id) = winit_windows.get_window_id(winit_window_id) {
|
||||||
|
window_id
|
||||||
|
} else {
|
||||||
|
warn!(
|
||||||
|
"Skipped event for unknown winit Window Id {:?}",
|
||||||
|
winit_window_id
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let window = if let Some(window) = windows.get_mut(window_id) {
|
||||||
|
window
|
||||||
|
} else {
|
||||||
|
warn!("Skipped event for unknown Window Id {:?}", winit_window_id);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
match event {
|
||||||
|
WindowEvent::Resized(size) => {
|
||||||
window.update_actual_size_from_backend(size.width, size.height);
|
window.update_actual_size_from_backend(size.width, size.height);
|
||||||
let mut resize_events =
|
let mut resize_events =
|
||||||
app.resources.get_mut::<Events<WindowResized>>().unwrap();
|
app.resources.get_mut::<Events<WindowResized>>().unwrap();
|
||||||
@ -230,8 +247,6 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
|
|||||||
.resources
|
.resources
|
||||||
.get_mut::<Events<WindowCloseRequested>>()
|
.get_mut::<Events<WindowCloseRequested>>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
|
||||||
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
|
||||||
window_close_requested_events.send(WindowCloseRequested { id: window_id });
|
window_close_requested_events.send(WindowCloseRequested { id: window_id });
|
||||||
}
|
}
|
||||||
WindowEvent::KeyboardInput { ref input, .. } => {
|
WindowEvent::KeyboardInput { ref input, .. } => {
|
||||||
@ -242,11 +257,7 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
|
|||||||
WindowEvent::CursorMoved { position, .. } => {
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
let mut cursor_moved_events =
|
let mut cursor_moved_events =
|
||||||
app.resources.get_mut::<Events<CursorMoved>>().unwrap();
|
app.resources.get_mut::<Events<CursorMoved>>().unwrap();
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
|
||||||
let mut windows = app.resources.get_mut::<Windows>().unwrap();
|
|
||||||
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
|
||||||
let winit_window = winit_windows.get_window(window_id).unwrap();
|
let winit_window = winit_windows.get_window(window_id).unwrap();
|
||||||
let window = windows.get_mut(window_id).unwrap();
|
|
||||||
let position = position.to_logical(winit_window.scale_factor());
|
let position = position.to_logical(winit_window.scale_factor());
|
||||||
let inner_size = winit_window
|
let inner_size = winit_window
|
||||||
.inner_size()
|
.inner_size()
|
||||||
@ -266,17 +277,11 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
|
|||||||
WindowEvent::CursorEntered { .. } => {
|
WindowEvent::CursorEntered { .. } => {
|
||||||
let mut cursor_entered_events =
|
let mut cursor_entered_events =
|
||||||
app.resources.get_mut::<Events<CursorEntered>>().unwrap();
|
app.resources.get_mut::<Events<CursorEntered>>().unwrap();
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
|
||||||
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
|
||||||
cursor_entered_events.send(CursorEntered { id: window_id });
|
cursor_entered_events.send(CursorEntered { id: window_id });
|
||||||
}
|
}
|
||||||
WindowEvent::CursorLeft { .. } => {
|
WindowEvent::CursorLeft { .. } => {
|
||||||
let mut cursor_left_events =
|
let mut cursor_left_events =
|
||||||
app.resources.get_mut::<Events<CursorLeft>>().unwrap();
|
app.resources.get_mut::<Events<CursorLeft>>().unwrap();
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
|
||||||
let mut windows = app.resources.get_mut::<Windows>().unwrap();
|
|
||||||
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
|
||||||
let window = windows.get_mut(window_id).unwrap();
|
|
||||||
window.update_cursor_position_from_backend(None);
|
window.update_cursor_position_from_backend(None);
|
||||||
cursor_left_events.send(CursorLeft { id: window_id });
|
cursor_left_events.send(CursorLeft { id: window_id });
|
||||||
}
|
}
|
||||||
@ -312,9 +317,6 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
|
|||||||
let mut touch_input_events =
|
let mut touch_input_events =
|
||||||
app.resources.get_mut::<Events<TouchInput>>().unwrap();
|
app.resources.get_mut::<Events<TouchInput>>().unwrap();
|
||||||
|
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
|
||||||
let windows = app.resources.get_mut::<Windows>().unwrap();
|
|
||||||
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
|
||||||
let winit_window = winit_windows.get_window(window_id).unwrap();
|
let winit_window = winit_windows.get_window(window_id).unwrap();
|
||||||
let mut location = touch.location.to_logical(winit_window.scale_factor());
|
let mut location = touch.location.to_logical(winit_window.scale_factor());
|
||||||
|
|
||||||
@ -331,9 +333,6 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
|
|||||||
.get_mut::<Events<ReceivedCharacter>>()
|
.get_mut::<Events<ReceivedCharacter>>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
|
||||||
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
|
||||||
|
|
||||||
char_input_events.send(ReceivedCharacter {
|
char_input_events.send(ReceivedCharacter {
|
||||||
id: window_id,
|
id: window_id,
|
||||||
char: c,
|
char: c,
|
||||||
@ -343,10 +342,6 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
|
|||||||
scale_factor,
|
scale_factor,
|
||||||
new_inner_size,
|
new_inner_size,
|
||||||
} => {
|
} => {
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
|
||||||
let mut windows = app.resources.get_mut::<Windows>().unwrap();
|
|
||||||
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
|
||||||
let window = windows.get_mut(window_id).unwrap();
|
|
||||||
window.update_actual_size_from_backend(
|
window.update_actual_size_from_backend(
|
||||||
new_inner_size.width,
|
new_inner_size.width,
|
||||||
new_inner_size.height,
|
new_inner_size.height,
|
||||||
@ -358,23 +353,14 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
|
|||||||
WindowEvent::Focused(focused) => {
|
WindowEvent::Focused(focused) => {
|
||||||
let mut focused_events =
|
let mut focused_events =
|
||||||
app.resources.get_mut::<Events<WindowFocused>>().unwrap();
|
app.resources.get_mut::<Events<WindowFocused>>().unwrap();
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
focused_events.send(WindowFocused {
|
||||||
match (winit_windows.get_window_id(winit_window_id), focused) {
|
|
||||||
(Some(window_id), _) => focused_events.send(WindowFocused {
|
|
||||||
id: window_id,
|
id: window_id,
|
||||||
focused,
|
focused,
|
||||||
}),
|
});
|
||||||
// unfocus event for an unknown window, ignore it
|
|
||||||
(None, false) => (),
|
|
||||||
// focus event on an unknown window, this is an error
|
|
||||||
_ => panic!(
|
|
||||||
"Focused(true) event on unknown window {:?}",
|
|
||||||
winit_window_id
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
}
|
||||||
|
}
|
||||||
event::Event::DeviceEvent {
|
event::Event::DeviceEvent {
|
||||||
event: DeviceEvent::MouseMotion { delta },
|
event: DeviceEvent::MouseMotion { delta },
|
||||||
..
|
..
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user