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,171 +210,157 @@ 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 =
|
||||||
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
if let Some(window_id) = winit_windows.get_window_id(winit_window_id) {
|
||||||
let window = windows.get_mut(window_id).unwrap();
|
window_id
|
||||||
window.update_actual_size_from_backend(size.width, size.height);
|
} else {
|
||||||
let mut resize_events =
|
warn!(
|
||||||
app.resources.get_mut::<Events<WindowResized>>().unwrap();
|
"Skipped event for unknown winit Window Id {:?}",
|
||||||
resize_events.send(WindowResized {
|
winit_window_id
|
||||||
id: window_id,
|
);
|
||||||
width: window.width(),
|
return;
|
||||||
height: window.height(),
|
};
|
||||||
});
|
|
||||||
}
|
|
||||||
WindowEvent::CloseRequested => {
|
|
||||||
let mut window_close_requested_events = app
|
|
||||||
.resources
|
|
||||||
.get_mut::<Events<WindowCloseRequested>>()
|
|
||||||
.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 });
|
|
||||||
}
|
|
||||||
WindowEvent::KeyboardInput { ref input, .. } => {
|
|
||||||
let mut keyboard_input_events =
|
|
||||||
app.resources.get_mut::<Events<KeyboardInput>>().unwrap();
|
|
||||||
keyboard_input_events.send(converters::convert_keyboard_input(input));
|
|
||||||
}
|
|
||||||
WindowEvent::CursorMoved { position, .. } => {
|
|
||||||
let mut cursor_moved_events =
|
|
||||||
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 window = windows.get_mut(window_id).unwrap();
|
|
||||||
let position = position.to_logical(winit_window.scale_factor());
|
|
||||||
let inner_size = winit_window
|
|
||||||
.inner_size()
|
|
||||||
.to_logical::<f32>(winit_window.scale_factor());
|
|
||||||
|
|
||||||
// move origin to bottom left
|
let window = if let Some(window) = windows.get_mut(window_id) {
|
||||||
let y_position = inner_size.height - position.y;
|
window
|
||||||
|
} else {
|
||||||
|
warn!("Skipped event for unknown Window Id {:?}", winit_window_id);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let position = Vec2::new(position.x, y_position);
|
match event {
|
||||||
window.update_cursor_position_from_backend(Some(position));
|
WindowEvent::Resized(size) => {
|
||||||
|
window.update_actual_size_from_backend(size.width, size.height);
|
||||||
cursor_moved_events.send(CursorMoved {
|
let mut resize_events =
|
||||||
id: window_id,
|
app.resources.get_mut::<Events<WindowResized>>().unwrap();
|
||||||
position,
|
resize_events.send(WindowResized {
|
||||||
});
|
id: window_id,
|
||||||
}
|
width: window.width(),
|
||||||
WindowEvent::CursorEntered { .. } => {
|
height: window.height(),
|
||||||
let mut cursor_entered_events =
|
|
||||||
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 });
|
|
||||||
}
|
|
||||||
WindowEvent::CursorLeft { .. } => {
|
|
||||||
let mut cursor_left_events =
|
|
||||||
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);
|
|
||||||
cursor_left_events.send(CursorLeft { id: window_id });
|
|
||||||
}
|
|
||||||
WindowEvent::MouseInput { state, button, .. } => {
|
|
||||||
let mut mouse_button_input_events =
|
|
||||||
app.resources.get_mut::<Events<MouseButtonInput>>().unwrap();
|
|
||||||
mouse_button_input_events.send(MouseButtonInput {
|
|
||||||
button: converters::convert_mouse_button(button),
|
|
||||||
state: converters::convert_element_state(state),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
WindowEvent::MouseWheel { delta, .. } => match delta {
|
|
||||||
event::MouseScrollDelta::LineDelta(x, y) => {
|
|
||||||
let mut mouse_wheel_input_events =
|
|
||||||
app.resources.get_mut::<Events<MouseWheel>>().unwrap();
|
|
||||||
mouse_wheel_input_events.send(MouseWheel {
|
|
||||||
unit: MouseScrollUnit::Line,
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
event::MouseScrollDelta::PixelDelta(p) => {
|
WindowEvent::CloseRequested => {
|
||||||
let mut mouse_wheel_input_events =
|
let mut window_close_requested_events = app
|
||||||
app.resources.get_mut::<Events<MouseWheel>>().unwrap();
|
.resources
|
||||||
mouse_wheel_input_events.send(MouseWheel {
|
.get_mut::<Events<WindowCloseRequested>>()
|
||||||
unit: MouseScrollUnit::Pixel,
|
.unwrap();
|
||||||
x: p.x as f32,
|
window_close_requested_events.send(WindowCloseRequested { id: window_id });
|
||||||
y: p.y as f32,
|
}
|
||||||
|
WindowEvent::KeyboardInput { ref input, .. } => {
|
||||||
|
let mut keyboard_input_events =
|
||||||
|
app.resources.get_mut::<Events<KeyboardInput>>().unwrap();
|
||||||
|
keyboard_input_events.send(converters::convert_keyboard_input(input));
|
||||||
|
}
|
||||||
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
|
let mut cursor_moved_events =
|
||||||
|
app.resources.get_mut::<Events<CursorMoved>>().unwrap();
|
||||||
|
let winit_window = winit_windows.get_window(window_id).unwrap();
|
||||||
|
let position = position.to_logical(winit_window.scale_factor());
|
||||||
|
let inner_size = winit_window
|
||||||
|
.inner_size()
|
||||||
|
.to_logical::<f32>(winit_window.scale_factor());
|
||||||
|
|
||||||
|
// move origin to bottom left
|
||||||
|
let y_position = inner_size.height - position.y;
|
||||||
|
|
||||||
|
let position = Vec2::new(position.x, y_position);
|
||||||
|
window.update_cursor_position_from_backend(Some(position));
|
||||||
|
|
||||||
|
cursor_moved_events.send(CursorMoved {
|
||||||
|
id: window_id,
|
||||||
|
position,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
WindowEvent::CursorEntered { .. } => {
|
||||||
WindowEvent::Touch(touch) => {
|
let mut cursor_entered_events =
|
||||||
let mut touch_input_events =
|
app.resources.get_mut::<Events<CursorEntered>>().unwrap();
|
||||||
app.resources.get_mut::<Events<TouchInput>>().unwrap();
|
cursor_entered_events.send(CursorEntered { id: window_id });
|
||||||
|
|
||||||
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 mut location = touch.location.to_logical(winit_window.scale_factor());
|
|
||||||
|
|
||||||
// FIXME?: On Android window start is top while on PC/Linux/OSX on bottom
|
|
||||||
if cfg!(target_os = "android") {
|
|
||||||
let window_height = windows.get_primary().unwrap().height();
|
|
||||||
location.y = window_height - location.y;
|
|
||||||
}
|
}
|
||||||
touch_input_events.send(converters::convert_touch_input(touch, location));
|
WindowEvent::CursorLeft { .. } => {
|
||||||
}
|
let mut cursor_left_events =
|
||||||
WindowEvent::ReceivedCharacter(c) => {
|
app.resources.get_mut::<Events<CursorLeft>>().unwrap();
|
||||||
let mut char_input_events = app
|
window.update_cursor_position_from_backend(None);
|
||||||
.resources
|
cursor_left_events.send(CursorLeft { id: window_id });
|
||||||
.get_mut::<Events<ReceivedCharacter>>()
|
}
|
||||||
.unwrap();
|
WindowEvent::MouseInput { state, button, .. } => {
|
||||||
|
let mut mouse_button_input_events =
|
||||||
|
app.resources.get_mut::<Events<MouseButtonInput>>().unwrap();
|
||||||
|
mouse_button_input_events.send(MouseButtonInput {
|
||||||
|
button: converters::convert_mouse_button(button),
|
||||||
|
state: converters::convert_element_state(state),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
WindowEvent::MouseWheel { delta, .. } => match delta {
|
||||||
|
event::MouseScrollDelta::LineDelta(x, y) => {
|
||||||
|
let mut mouse_wheel_input_events =
|
||||||
|
app.resources.get_mut::<Events<MouseWheel>>().unwrap();
|
||||||
|
mouse_wheel_input_events.send(MouseWheel {
|
||||||
|
unit: MouseScrollUnit::Line,
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
event::MouseScrollDelta::PixelDelta(p) => {
|
||||||
|
let mut mouse_wheel_input_events =
|
||||||
|
app.resources.get_mut::<Events<MouseWheel>>().unwrap();
|
||||||
|
mouse_wheel_input_events.send(MouseWheel {
|
||||||
|
unit: MouseScrollUnit::Pixel,
|
||||||
|
x: p.x as f32,
|
||||||
|
y: p.y as f32,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
WindowEvent::Touch(touch) => {
|
||||||
|
let mut touch_input_events =
|
||||||
|
app.resources.get_mut::<Events<TouchInput>>().unwrap();
|
||||||
|
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
let winit_window = winit_windows.get_window(window_id).unwrap();
|
||||||
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
let mut location = touch.location.to_logical(winit_window.scale_factor());
|
||||||
|
|
||||||
char_input_events.send(ReceivedCharacter {
|
// FIXME?: On Android window start is top while on PC/Linux/OSX on bottom
|
||||||
id: window_id,
|
if cfg!(target_os = "android") {
|
||||||
char: c,
|
let window_height = windows.get_primary().unwrap().height();
|
||||||
})
|
location.y = window_height - location.y;
|
||||||
}
|
}
|
||||||
WindowEvent::ScaleFactorChanged {
|
touch_input_events.send(converters::convert_touch_input(touch, location));
|
||||||
scale_factor,
|
}
|
||||||
new_inner_size,
|
WindowEvent::ReceivedCharacter(c) => {
|
||||||
} => {
|
let mut char_input_events = app
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
.resources
|
||||||
let mut windows = app.resources.get_mut::<Windows>().unwrap();
|
.get_mut::<Events<ReceivedCharacter>>()
|
||||||
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
.unwrap();
|
||||||
let window = windows.get_mut(window_id).unwrap();
|
|
||||||
window.update_actual_size_from_backend(
|
char_input_events.send(ReceivedCharacter {
|
||||||
new_inner_size.width,
|
id: window_id,
|
||||||
new_inner_size.height,
|
char: c,
|
||||||
);
|
})
|
||||||
window.update_scale_factor_from_backend(scale_factor);
|
}
|
||||||
// should we send a resize event to indicate the change in
|
WindowEvent::ScaleFactorChanged {
|
||||||
// logical size?
|
scale_factor,
|
||||||
}
|
new_inner_size,
|
||||||
WindowEvent::Focused(focused) => {
|
} => {
|
||||||
let mut focused_events =
|
window.update_actual_size_from_backend(
|
||||||
app.resources.get_mut::<Events<WindowFocused>>().unwrap();
|
new_inner_size.width,
|
||||||
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
new_inner_size.height,
|
||||||
match (winit_windows.get_window_id(winit_window_id), focused) {
|
);
|
||||||
(Some(window_id), _) => focused_events.send(WindowFocused {
|
window.update_scale_factor_from_backend(scale_factor);
|
||||||
|
// should we send a resize event to indicate the change in
|
||||||
|
// logical size?
|
||||||
|
}
|
||||||
|
WindowEvent::Focused(focused) => {
|
||||||
|
let mut focused_events =
|
||||||
|
app.resources.get_mut::<Events<WindowFocused>>().unwrap();
|
||||||
|
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