@@ -12,7 +12,7 @@ pub use winit_windows::*;
1212use bevy_app:: { prelude:: * , AppExit } ;
1313use bevy_ecs:: { IntoSystem , Resources , World } ;
1414use bevy_math:: Vec2 ;
15- use bevy_utils:: tracing:: { error, trace} ;
15+ use bevy_utils:: tracing:: { error, trace, warn } ;
1616use bevy_window:: {
1717 CreateWindow , CursorEntered , CursorLeft , CursorMoved , ReceivedCharacter , WindowCloseRequested ,
1818 WindowCreated , WindowFocused , WindowResized , Windows ,
@@ -210,171 +210,157 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
210210 event,
211211 window_id : winit_window_id,
212212 ..
213- } => match event {
214- WindowEvent :: Resized ( size) => {
215- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
216- let mut windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
217- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
218- let window = windows. get_mut ( window_id) . unwrap ( ) ;
219- window. update_actual_size_from_backend ( size. width , size. height ) ;
220- let mut resize_events =
221- app. resources . get_mut :: < Events < WindowResized > > ( ) . unwrap ( ) ;
222- resize_events. send ( WindowResized {
223- id : window_id,
224- width : window. width ( ) ,
225- height : window. height ( ) ,
226- } ) ;
227- }
228- WindowEvent :: CloseRequested => {
229- let mut window_close_requested_events = app
230- . resources
231- . get_mut :: < Events < WindowCloseRequested > > ( )
232- . unwrap ( ) ;
233- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
234- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
235- window_close_requested_events. send ( WindowCloseRequested { id : window_id } ) ;
236- }
237- WindowEvent :: KeyboardInput { ref input, .. } => {
238- let mut keyboard_input_events =
239- app. resources . get_mut :: < Events < KeyboardInput > > ( ) . unwrap ( ) ;
240- keyboard_input_events. send ( converters:: convert_keyboard_input ( input) ) ;
241- }
242- WindowEvent :: CursorMoved { position, .. } => {
243- let mut cursor_moved_events =
244- app. resources . get_mut :: < Events < CursorMoved > > ( ) . unwrap ( ) ;
245- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
246- let mut windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
247- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
248- let winit_window = winit_windows. get_window ( window_id) . unwrap ( ) ;
249- let window = windows. get_mut ( window_id) . unwrap ( ) ;
250- let position = position. to_logical ( winit_window. scale_factor ( ) ) ;
251- let inner_size = winit_window
252- . inner_size ( )
253- . to_logical :: < f32 > ( winit_window. scale_factor ( ) ) ;
213+ } => {
214+ let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
215+ let mut windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
216+ let window_id =
217+ if let Some ( window_id) = winit_windows. get_window_id ( winit_window_id) {
218+ window_id
219+ } else {
220+ warn ! (
221+ "Skipped event for unknown winit Window Id {:?}" ,
222+ winit_window_id
223+ ) ;
224+ return ;
225+ } ;
226+
227+ let window = if let Some ( window) = windows. get_mut ( window_id) {
228+ window
229+ } else {
230+ warn ! ( "Skipped event for unknown Window Id {:?}" , winit_window_id) ;
231+ return ;
232+ } ;
233+
234+ match event {
235+ WindowEvent :: Resized ( size) => {
236+ window. update_actual_size_from_backend ( size. width , size. height ) ;
237+ let mut resize_events =
238+ app. resources . get_mut :: < Events < WindowResized > > ( ) . unwrap ( ) ;
239+ resize_events. send ( WindowResized {
240+ id : window_id,
241+ width : window. width ( ) ,
242+ height : window. height ( ) ,
243+ } ) ;
244+ }
245+ WindowEvent :: CloseRequested => {
246+ let mut window_close_requested_events = app
247+ . resources
248+ . get_mut :: < Events < WindowCloseRequested > > ( )
249+ . unwrap ( ) ;
250+ window_close_requested_events. send ( WindowCloseRequested { id : window_id } ) ;
251+ }
252+ WindowEvent :: KeyboardInput { ref input, .. } => {
253+ let mut keyboard_input_events =
254+ app. resources . get_mut :: < Events < KeyboardInput > > ( ) . unwrap ( ) ;
255+ keyboard_input_events. send ( converters:: convert_keyboard_input ( input) ) ;
256+ }
257+ WindowEvent :: CursorMoved { position, .. } => {
258+ let mut cursor_moved_events =
259+ app. resources . get_mut :: < Events < CursorMoved > > ( ) . unwrap ( ) ;
260+ let winit_window = winit_windows. get_window ( window_id) . unwrap ( ) ;
261+ let position = position. to_logical ( winit_window. scale_factor ( ) ) ;
262+ let inner_size = winit_window
263+ . inner_size ( )
264+ . to_logical :: < f32 > ( winit_window. scale_factor ( ) ) ;
254265
255- // move origin to bottom left
256- let y_position = inner_size. height - position. y ;
266+ // move origin to bottom left
267+ let y_position = inner_size. height - position. y ;
257268
258- let position = Vec2 :: new ( position. x , y_position) ;
259- window. update_cursor_position_from_backend ( Some ( position) ) ;
269+ let position = Vec2 :: new ( position. x , y_position) ;
270+ window. update_cursor_position_from_backend ( Some ( position) ) ;
260271
261- cursor_moved_events. send ( CursorMoved {
262- id : window_id,
263- position,
264- } ) ;
265- }
266- WindowEvent :: CursorEntered { .. } => {
267- let mut cursor_entered_events =
268- app. resources . get_mut :: < Events < CursorEntered > > ( ) . unwrap ( ) ;
269- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
270- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
271- cursor_entered_events. send ( CursorEntered { id : window_id } ) ;
272- }
273- WindowEvent :: CursorLeft { .. } => {
274- let mut cursor_left_events =
275- app. resources . get_mut :: < Events < CursorLeft > > ( ) . unwrap ( ) ;
276- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
277- let mut windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
278- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
279- let window = windows. get_mut ( window_id) . unwrap ( ) ;
280- window. update_cursor_position_from_backend ( None ) ;
281- cursor_left_events. send ( CursorLeft { id : window_id } ) ;
282- }
283- WindowEvent :: MouseInput { state, button, .. } => {
284- let mut mouse_button_input_events =
285- app. resources . get_mut :: < Events < MouseButtonInput > > ( ) . unwrap ( ) ;
286- mouse_button_input_events. send ( MouseButtonInput {
287- button : converters:: convert_mouse_button ( button) ,
288- state : converters:: convert_element_state ( state) ,
289- } ) ;
290- }
291- WindowEvent :: MouseWheel { delta, .. } => match delta {
292- event:: MouseScrollDelta :: LineDelta ( x, y) => {
293- let mut mouse_wheel_input_events =
294- app. resources . get_mut :: < Events < MouseWheel > > ( ) . unwrap ( ) ;
295- mouse_wheel_input_events. send ( MouseWheel {
296- unit : MouseScrollUnit :: Line ,
297- x,
298- y,
272+ cursor_moved_events. send ( CursorMoved {
273+ id : window_id,
274+ position,
299275 } ) ;
300276 }
301- event:: MouseScrollDelta :: PixelDelta ( p) => {
302- let mut mouse_wheel_input_events =
303- app. resources . get_mut :: < Events < MouseWheel > > ( ) . unwrap ( ) ;
304- mouse_wheel_input_events. send ( MouseWheel {
305- unit : MouseScrollUnit :: Pixel ,
306- x : p. x as f32 ,
307- y : p. y as f32 ,
277+ WindowEvent :: CursorEntered { .. } => {
278+ let mut cursor_entered_events =
279+ app. resources . get_mut :: < Events < CursorEntered > > ( ) . unwrap ( ) ;
280+ cursor_entered_events. send ( CursorEntered { id : window_id } ) ;
281+ }
282+ WindowEvent :: CursorLeft { .. } => {
283+ let mut cursor_left_events =
284+ app. resources . get_mut :: < Events < CursorLeft > > ( ) . unwrap ( ) ;
285+ window. update_cursor_position_from_backend ( None ) ;
286+ cursor_left_events. send ( CursorLeft { id : window_id } ) ;
287+ }
288+ WindowEvent :: MouseInput { state, button, .. } => {
289+ let mut mouse_button_input_events =
290+ app. resources . get_mut :: < Events < MouseButtonInput > > ( ) . unwrap ( ) ;
291+ mouse_button_input_events. send ( MouseButtonInput {
292+ button : converters:: convert_mouse_button ( button) ,
293+ state : converters:: convert_element_state ( state) ,
308294 } ) ;
309295 }
310- } ,
311- WindowEvent :: Touch ( touch) => {
312- let mut touch_input_events =
313- app. resources . get_mut :: < Events < TouchInput > > ( ) . unwrap ( ) ;
296+ WindowEvent :: MouseWheel { delta, .. } => match delta {
297+ event:: MouseScrollDelta :: LineDelta ( x, y) => {
298+ let mut mouse_wheel_input_events =
299+ app. resources . get_mut :: < Events < MouseWheel > > ( ) . unwrap ( ) ;
300+ mouse_wheel_input_events. send ( MouseWheel {
301+ unit : MouseScrollUnit :: Line ,
302+ x,
303+ y,
304+ } ) ;
305+ }
306+ event:: MouseScrollDelta :: PixelDelta ( p) => {
307+ let mut mouse_wheel_input_events =
308+ app. resources . get_mut :: < Events < MouseWheel > > ( ) . unwrap ( ) ;
309+ mouse_wheel_input_events. send ( MouseWheel {
310+ unit : MouseScrollUnit :: Pixel ,
311+ x : p. x as f32 ,
312+ y : p. y as f32 ,
313+ } ) ;
314+ }
315+ } ,
316+ WindowEvent :: Touch ( touch) => {
317+ let mut touch_input_events =
318+ app. resources . get_mut :: < Events < TouchInput > > ( ) . unwrap ( ) ;
314319
315- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
316- let windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
317- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
318- let winit_window = winit_windows. get_window ( window_id) . unwrap ( ) ;
319- let mut location = touch. location . to_logical ( winit_window. scale_factor ( ) ) ;
320+ let winit_window = winit_windows. get_window ( window_id) . unwrap ( ) ;
321+ let mut location = touch. location . to_logical ( winit_window. scale_factor ( ) ) ;
320322
321- // FIXME?: On Android window start is top while on PC/Linux/OSX on bottom
322- if cfg ! ( target_os = "android" ) {
323- let window_height = windows. get_primary ( ) . unwrap ( ) . height ( ) ;
324- location. y = window_height - location. y ;
323+ // FIXME?: On Android window start is top while on PC/Linux/OSX on bottom
324+ if cfg ! ( target_os = "android" ) {
325+ let window_height = windows. get_primary ( ) . unwrap ( ) . height ( ) ;
326+ location. y = window_height - location. y ;
327+ }
328+ touch_input_events. send ( converters:: convert_touch_input ( touch, location) ) ;
325329 }
326- touch_input_events. send ( converters:: convert_touch_input ( touch, location) ) ;
327- }
328- WindowEvent :: ReceivedCharacter ( c) => {
329- let mut char_input_events = app
330- . resources
331- . get_mut :: < Events < ReceivedCharacter > > ( )
332- . unwrap ( ) ;
333-
334- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
335- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
330+ WindowEvent :: ReceivedCharacter ( c) => {
331+ let mut char_input_events = app
332+ . resources
333+ . get_mut :: < Events < ReceivedCharacter > > ( )
334+ . unwrap ( ) ;
336335
337- char_input_events. send ( ReceivedCharacter {
338- id : window_id,
339- char : c,
340- } )
341- }
342- WindowEvent :: ScaleFactorChanged {
343- scale_factor,
344- new_inner_size,
345- } => {
346- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
347- let mut windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
348- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
349- let window = windows. get_mut ( window_id) . unwrap ( ) ;
350- window. update_actual_size_from_backend (
351- new_inner_size. width ,
352- new_inner_size. height ,
353- ) ;
354- window. update_scale_factor_from_backend ( scale_factor) ;
355- // should we send a resize event to indicate the change in
356- // logical size?
357- }
358- WindowEvent :: Focused ( focused) => {
359- let mut focused_events =
360- app. resources . get_mut :: < Events < WindowFocused > > ( ) . unwrap ( ) ;
361- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
362- match ( winit_windows. get_window_id ( winit_window_id) , focused) {
363- ( Some ( window_id) , _) => focused_events. send ( WindowFocused {
336+ char_input_events. send ( ReceivedCharacter {
337+ id : window_id,
338+ char : c,
339+ } )
340+ }
341+ WindowEvent :: ScaleFactorChanged {
342+ scale_factor,
343+ new_inner_size,
344+ } => {
345+ window. update_actual_size_from_backend (
346+ new_inner_size. width ,
347+ new_inner_size. height ,
348+ ) ;
349+ window. update_scale_factor_from_backend ( scale_factor) ;
350+ // should we send a resize event to indicate the change in
351+ // logical size?
352+ }
353+ WindowEvent :: Focused ( focused) => {
354+ let mut focused_events =
355+ app. resources . get_mut :: < Events < WindowFocused > > ( ) . unwrap ( ) ;
356+ focused_events. send ( WindowFocused {
364357 id : window_id,
365358 focused,
366- } ) ,
367- // unfocus event for an unknown window, ignore it
368- ( None , false ) => ( ) ,
369- // focus event on an unknown window, this is an error
370- _ => panic ! (
371- "Focused(true) event on unknown window {:?}" ,
372- winit_window_id
373- ) ,
359+ } ) ;
374360 }
361+ _ => { }
375362 }
376- _ => { }
377- } ,
363+ }
378364 event:: Event :: DeviceEvent {
379365 event : DeviceEvent :: MouseMotion { delta } ,
380366 ..
0 commit comments