@@ -3,8 +3,8 @@ use crate::custom_event::RuffleEvent;
3
3
use crate :: gui:: { GuiController , MENU_HEIGHT } ;
4
4
use crate :: player:: { PlayerController , PlayerOptions } ;
5
5
use crate :: util:: {
6
- get_screen_size, parse_url, pick_file, plot_stats_in_tracy, winit_key_to_char ,
7
- winit_to_ruffle_key_code , winit_to_ruffle_text_control,
6
+ get_screen_size, parse_url, pick_file, plot_stats_in_tracy, winit_to_ruffle_key_code ,
7
+ winit_to_ruffle_text_control,
8
8
} ;
9
9
use anyhow:: { Context , Error } ;
10
10
use ruffle_core:: { PlayerEvent , StageDisplayState } ;
@@ -14,8 +14,9 @@ use std::rc::Rc;
14
14
use std:: time:: { Duration , Instant } ;
15
15
use url:: Url ;
16
16
use winit:: dpi:: { LogicalSize , PhysicalPosition , PhysicalSize , Size } ;
17
- use winit:: event:: { ElementState , KeyboardInput , ModifiersState , VirtualKeyCode , WindowEvent } ;
17
+ use winit:: event:: { ElementState , KeyEvent , Modifiers , WindowEvent } ;
18
18
use winit:: event_loop:: { ControlFlow , EventLoop , EventLoopBuilder } ;
19
+ use winit:: keyboard:: { Key , NamedKey } ;
19
20
use winit:: window:: { Fullscreen , Icon , Window , WindowBuilder } ;
20
21
21
22
pub struct App {
@@ -35,7 +36,7 @@ impl App {
35
36
let icon =
36
37
Icon :: from_rgba ( icon_bytes. to_vec ( ) , 32 , 32 ) . context ( "Couldn't load app icon" ) ?;
37
38
38
- let event_loop = EventLoopBuilder :: with_user_event ( ) . build ( ) ;
39
+ let event_loop = EventLoopBuilder :: with_user_event ( ) . build ( ) ? ;
39
40
40
41
let min_window_size = ( 16 , if opt. no_gui { 16 } else { MENU_HEIGHT + 16 } ) . into ( ) ;
41
42
let max_window_size = get_screen_size ( & event_loop) ;
@@ -78,7 +79,7 @@ impl App {
78
79
} )
79
80
}
80
81
81
- pub fn run ( mut self ) -> ! {
82
+ pub fn run ( mut self ) -> Result < ( ) , Error > {
82
83
enum LoadingState {
83
84
Loading ,
84
85
WaitingForResize ,
@@ -89,7 +90,7 @@ impl App {
89
90
let mut time = Instant :: now ( ) ;
90
91
let mut next_frame_time = None ;
91
92
let mut minimized = false ;
92
- let mut modifiers = ModifiersState :: empty ( ) ;
93
+ let mut modifiers = Modifiers :: default ( ) ;
93
94
let mut fullscreen_down = false ;
94
95
95
96
if self . opt . movie_url . is_none ( ) {
@@ -100,10 +101,10 @@ impl App {
100
101
101
102
// Poll UI events.
102
103
let event_loop = self . event_loop . take ( ) . expect ( "App already running" ) ;
103
- event_loop. run ( move |event, _window_target , control_flow | {
104
+ event_loop. run ( move |event, elwt | {
104
105
let mut check_redraw = false ;
105
106
match event {
106
- winit:: event:: Event :: LoopDestroyed => {
107
+ winit:: event:: Event :: LoopExiting => {
107
108
if let Some ( mut player) = self . player . get ( ) {
108
109
player. flush_shared_objects ( ) ;
109
110
}
@@ -112,9 +113,9 @@ impl App {
112
113
}
113
114
114
115
// Core loop
115
- winit :: event :: Event :: MainEventsCleared
116
- if matches ! ( loaded , LoadingState :: Loaded ) =>
117
- {
116
+ // [NA] This used to be called ` MainEventsCleared`, but I think the behaviour is different now.
117
+ // We should look at changing our tick to happen somewhere else if we see any behavioural problems.
118
+ winit :: event :: Event :: AboutToWait if matches ! ( loaded , LoadingState :: Loaded ) => {
118
119
let new_time = Instant :: now ( ) ;
119
120
let dt = new_time. duration_since ( time) . as_micros ( ) ;
120
121
if dt > 0 {
@@ -130,7 +131,10 @@ impl App {
130
131
}
131
132
132
133
// Render
133
- winit:: event:: Event :: RedrawRequested ( _) => {
134
+ winit:: event:: Event :: WindowEvent {
135
+ event : WindowEvent :: RedrawRequested ,
136
+ ..
137
+ } => {
134
138
// Don't render when minimized to avoid potential swap chain errors in `wgpu`.
135
139
if !minimized {
136
140
if let Some ( mut player) = self . player . get ( ) {
@@ -156,7 +160,7 @@ impl App {
156
160
} ;
157
161
match event {
158
162
WindowEvent :: CloseRequested => {
159
- * control_flow = ControlFlow :: Exit ;
163
+ elwt . exit ( ) ;
160
164
return ;
161
165
}
162
166
WindowEvent :: Resized ( size) => {
@@ -211,7 +215,7 @@ impl App {
211
215
MouseButton :: Left => RuffleMouseButton :: Left ,
212
216
MouseButton :: Right => RuffleMouseButton :: Right ,
213
217
MouseButton :: Middle => RuffleMouseButton :: Middle ,
214
- MouseButton :: Other ( _ ) => RuffleMouseButton :: Unknown ,
218
+ _ => RuffleMouseButton :: Unknown ,
215
219
} ;
216
220
let event = match state {
217
221
ElementState :: Pressed => PlayerEvent :: MouseDown { x, y, button } ,
@@ -260,14 +264,14 @@ impl App {
260
264
WindowEvent :: ModifiersChanged ( new_modifiers) => {
261
265
modifiers = new_modifiers;
262
266
}
263
- WindowEvent :: KeyboardInput { input , .. } => {
267
+ WindowEvent :: KeyboardInput { event , .. } => {
264
268
// Handle fullscreen keyboard shortcuts: Alt+Return, Escape.
265
- match input {
266
- KeyboardInput {
269
+ match event {
270
+ KeyEvent {
267
271
state : ElementState :: Pressed ,
268
- virtual_keycode : Some ( VirtualKeyCode :: Return ) ,
272
+ logical_key : Key :: Named ( NamedKey :: Enter ) ,
269
273
..
270
- } if modifiers. alt ( ) => {
274
+ } if modifiers. state ( ) . alt_key ( ) => {
271
275
if !fullscreen_down {
272
276
if let Some ( mut player) = self . player . get ( ) {
273
277
player. update ( |uc| {
@@ -278,16 +282,16 @@ impl App {
278
282
fullscreen_down = true ;
279
283
return ;
280
284
}
281
- KeyboardInput {
285
+ KeyEvent {
282
286
state : ElementState :: Released ,
283
- virtual_keycode : Some ( VirtualKeyCode :: Return ) ,
287
+ logical_key : Key :: Named ( NamedKey :: Enter ) ,
284
288
..
285
289
} if fullscreen_down => {
286
290
fullscreen_down = false ;
287
291
}
288
- KeyboardInput {
292
+ KeyEvent {
289
293
state : ElementState :: Pressed ,
290
- virtual_keycode : Some ( VirtualKeyCode :: Escape ) ,
294
+ logical_key : Key :: Named ( NamedKey :: Escape ) ,
291
295
..
292
296
} => {
293
297
if let Some ( mut player) = self . player . get ( ) {
@@ -304,38 +308,42 @@ impl App {
304
308
_ => ( ) ,
305
309
}
306
310
307
- if let Some ( key) = input. virtual_keycode {
308
- let key_code = winit_to_ruffle_key_code ( key) ;
309
- let key_char = winit_key_to_char ( key, modifiers. shift ( ) ) ;
310
- match input. state {
311
- ElementState :: Pressed => {
312
- self . player . handle_event ( PlayerEvent :: KeyDown {
313
- key_code,
314
- key_char,
311
+ let key_code = winit_to_ruffle_key_code ( & event) ;
312
+ // [NA] TODO: This event used to give a single char. `last()` is functionally the same,
313
+ // but we may want to be better at this in the future.
314
+ let key_char = event. text . clone ( ) . and_then ( |text| text. chars ( ) . last ( ) ) ;
315
+ let mut allow_text = true ;
316
+
317
+ match & event. state {
318
+ ElementState :: Pressed => {
319
+ self . player
320
+ . handle_event ( PlayerEvent :: KeyDown { key_code, key_char } ) ;
321
+ if let Some ( control_code) =
322
+ winit_to_ruffle_text_control ( & event, & modifiers)
323
+ {
324
+ self . player . handle_event ( PlayerEvent :: TextControl {
325
+ code : control_code,
315
326
} ) ;
316
- if let Some ( control_code) =
317
- winit_to_ruffle_text_control ( key, modifiers)
318
- {
319
- self . player . handle_event ( PlayerEvent :: TextControl {
320
- code : control_code,
321
- } ) ;
322
- }
327
+ allow_text = false ;
323
328
}
324
- ElementState :: Released => {
325
- self . player . handle_event ( PlayerEvent :: KeyUp {
326
- key_code,
327
- key_char,
328
- } ) ;
329
+ }
330
+ ElementState :: Released => {
331
+ self . player
332
+ . handle_event ( PlayerEvent :: KeyUp { key_code, key_char } ) ;
333
+ }
334
+ } ;
335
+ check_redraw = true ;
336
+
337
+ if allow_text {
338
+ if let Some ( text) = event. text {
339
+ for codepoint in text. chars ( ) {
340
+ self . player
341
+ . handle_event ( PlayerEvent :: TextInput { codepoint } ) ;
329
342
}
330
- } ;
331
- check_redraw = true ;
343
+ check_redraw = true ;
344
+ }
332
345
}
333
346
}
334
- WindowEvent :: ReceivedCharacter ( codepoint) => {
335
- let event = PlayerEvent :: TextInput { codepoint } ;
336
- self . player . handle_event ( event) ;
337
- check_redraw = true ;
338
- }
339
347
_ => ( ) ,
340
348
}
341
349
}
@@ -385,7 +393,7 @@ impl App {
385
393
self . window . scale_factor ( ) ,
386
394
) ;
387
395
388
- self . window . set_inner_size ( window_size) ;
396
+ let _ = self . window . request_inner_size ( window_size) ;
389
397
self . window . set_fullscreen ( if self . opt . fullscreen {
390
398
Some ( Fullscreen :: Borderless ( None ) )
391
399
} else {
@@ -441,7 +449,7 @@ impl App {
441
449
}
442
450
443
451
winit:: event:: Event :: UserEvent ( RuffleEvent :: ExitRequested ) => {
444
- * control_flow = ControlFlow :: Exit ;
452
+ elwt . exit ( ) ;
445
453
return ;
446
454
}
447
455
@@ -458,7 +466,7 @@ impl App {
458
466
}
459
467
460
468
// After polling events, sleep the event loop until the next event or the next frame.
461
- * control_flow = if matches ! ( loaded, LoadingState :: Loaded ) {
469
+ elwt . set_control_flow ( if matches ! ( loaded, LoadingState :: Loaded ) {
462
470
if let Some ( next_frame_time) = next_frame_time {
463
471
ControlFlow :: WaitUntil ( next_frame_time)
464
472
} else {
@@ -468,7 +476,8 @@ impl App {
468
476
}
469
477
} else {
470
478
ControlFlow :: Wait
471
- } ;
472
- } ) ;
479
+ } ) ;
480
+ } ) ?;
481
+ Ok ( ( ) )
473
482
}
474
483
}
0 commit comments