1
1
use anyhow:: Error ;
2
2
use rrr_head:: { platform:: platform:: time:: Time , query} ;
3
+ use std:: rc:: Rc ;
3
4
use wasm_bindgen:: prelude:: * ;
5
+ use wasm_bindgen:: JsCast ;
6
+ use web_sys:: { Element , HtmlCanvasElement } ;
7
+ use winit:: platform:: web:: WindowBuilderExtWebSys ;
8
+ use winit:: window:: WindowBuilder ;
4
9
use winit:: { dpi:: LogicalSize , event_loop:: EventLoop , window:: Window } ;
5
10
11
+ pub fn build_window (
12
+ event_loop : & EventLoop < ( ) > ,
13
+ canvas : Option < HtmlCanvasElement > ,
14
+ screen_width : u32 ,
15
+ screen_height : u32 ,
16
+ ) -> Result < winit:: window:: Window , winit:: error:: OsError > {
17
+ {
18
+ let size = LogicalSize :: new ( screen_width, screen_height) ;
19
+ WindowBuilder :: new ( )
20
+ . with_title ( "Rust Rust Revolution" )
21
+ . with_canvas ( canvas)
22
+ . with_inner_size ( size)
23
+ . with_resizable ( false )
24
+ . build ( event_loop)
25
+ }
26
+ }
27
+
6
28
#[ wasm_bindgen( start) ]
7
29
pub fn initialize ( ) {
8
30
console_log:: init ( ) . unwrap ( ) ;
9
31
log:: info!( "RRR loaded." ) ;
10
32
}
11
33
12
34
#[ wasm_bindgen]
13
- pub fn play ( width : u32 , height : u32 ) {
35
+ pub fn play ( canvas : Option < HtmlCanvasElement > , width : u32 , height : u32 ) {
14
36
use wasm_bindgen:: UnwrapThrowExt ;
15
37
std:: panic:: set_hook ( Box :: new ( console_error_panic_hook:: hook) ) ;
16
38
wasm_bindgen_futures:: spawn_local ( async move {
17
39
let event_loop = EventLoop :: new ( ) ;
18
- if let Ok ( window) = initialize_window ( width, height, & event_loop) . await {
40
+ if let Ok ( window) = initialize_window ( canvas , width, height, & event_loop) . await {
19
41
let extracted_settings: Option < query:: SettingsMerge > =
20
42
{ Some ( query:: get_optional_settings ( ) ) } ;
21
43
@@ -28,16 +50,12 @@ pub fn play(width: u32, height: u32) {
28
50
}
29
51
30
52
async fn initialize_window (
53
+ canvas : Option < HtmlCanvasElement > ,
31
54
width : u32 ,
32
55
height : u32 ,
33
56
event_loop : & EventLoop < ( ) > ,
34
57
) -> Result < Window , Error > {
35
- let window = rrr_head:: build_window ( & event_loop, width, height) ?;
36
-
37
- use std:: rc:: Rc ;
38
- use wasm_bindgen:: JsCast ;
39
- use web_sys:: { Element , HtmlCanvasElement } ;
40
- use winit:: platform:: web:: WindowExtWebSys ;
58
+ let window = build_window ( & event_loop, canvas. clone ( ) , width, height) ?;
41
59
42
60
// Initialize winit window with current dimensions of browser client
43
61
window. set_inner_size ( LogicalSize :: new ( width, height) ) ;
@@ -74,24 +92,11 @@ async fn initialize_window(
74
92
}
75
93
} ) as Box < dyn FnMut ( web_sys:: FocusEvent ) > ) ;
76
94
77
- // Attach winit canvas to body element
78
- web_sys:: window ( )
79
- . and_then ( |win| win. document ( ) )
80
- . and_then ( |doc| doc. get_element_by_id ( "canvas" ) )
81
- . and_then ( |canvas_div : Element | {
82
- let canvas: HtmlCanvasElement = window. canvas ( ) ;
83
- canvas. set_class_name ( "game" ) ;
84
- canvas. set_id ( "rrr" ) ;
85
- canvas. set_width ( width) ;
86
- canvas. set_height ( height) ;
87
- let res = canvas_div
88
- . append_child ( & web_sys:: Element :: from ( window. canvas ( ) ) )
89
- . ok ( ) ;
90
- canvas. set_onblur ( Some ( onblur. as_ref ( ) . unchecked_ref ( ) ) ) ;
91
- canvas. set_tab_index ( 1 ) ;
92
- canvas. focus ( ) . ok ( ) ;
93
- res
94
- } ) ;
95
+ if let Some ( canvas) = canvas {
96
+ canvas. set_onblur ( Some ( onblur. as_ref ( ) . unchecked_ref ( ) ) ) ;
97
+ canvas. set_tab_index ( 1 ) ;
98
+ canvas. focus ( ) . ok ( ) ;
99
+ }
95
100
96
101
onblur. forget ( ) ;
97
102
}
0 commit comments