geng-window 0.17.2

Game ENGine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use super::*;

use wasm_bindgen::prelude::*;

#[wasm_bindgen(module = "/src/backend/web.js")]
extern "C" {
    fn initialize_window(canvas: &web_sys::HtmlCanvasElement);
    fn is_fullscreen() -> bool;
    fn set_fullscreen(canvas: &web_sys::HtmlCanvasElement, fullscreen: bool);
    fn show();
    fn request_animation_frame_loop(closure: &Closure<dyn FnMut()>);
}

pub struct Context {
    canvas: web_sys::HtmlCanvasElement,
    ugli: Ugli,
    editing_text: Rc<Cell<bool>>,
    text_agent: web_sys::HtmlInputElement,
}

pub fn run<EH>(options: &Options, once_ready: impl 'static + FnOnce(Rc<Context>) -> EH)
where
    EH: 'static + FnMut(Event) -> std::ops::ControlFlow<()>,
{
    let context = Rc::new(Context::new(options));
    let event_handler = once_ready(context.clone());
    context.run(event_handler);
}

impl Context {
    pub fn new(options: &Options) -> Self {
        let canvas = web_sys::window()
            .unwrap()
            .document()
            .unwrap()
            .get_element_by_id("geng-canvas")
            .expect("#geng-canvas not found")
            .dyn_into::<web_sys::HtmlCanvasElement>()
            .expect("#geng-canvas is not a canvas");
        initialize_window(&canvas);
        let ugli = Ugli::create_webgl(
            &canvas,
            ugli::WebGLContextOptions {
                antialias: options.antialias,
                alpha: options.transparency,
                stencil: true,
                ..Default::default()
            },
        );
        Self {
            canvas,
            ugli,
            editing_text: Rc::new(Cell::new(false)),
            text_agent: Self::install_text_agent().unwrap(),
        }
    }

    pub fn real_size(&self) -> vec2<usize> {
        let width = self.canvas.width() as usize;
        let height = self.canvas.height() as usize;
        vec2(width, height)
    }

    pub fn ugli(&self) -> &Ugli {
        &self.ugli
    }

    pub fn set_fullscreen(&self, fullscreen: bool) {
        set_fullscreen(&self.canvas, fullscreen);
    }

    pub fn is_fullscreen(&self) -> bool {
        is_fullscreen()
    }

    pub fn set_cursor_type(&self, cursor_type: CursorType) {
        let cursor_type = match cursor_type {
            CursorType::Default => "initial",
            CursorType::Pointer => "pointer",
            CursorType::Drag => "all-scroll",
            CursorType::None => "none",
        };
        // TODO: only canvas
        web_sys::window()
            .unwrap()
            .document()
            .unwrap()
            .body()
            .unwrap()
            .style()
            .set_property("cursor", cursor_type)
            .unwrap();
    }

    pub fn cursor_locked(&self) -> bool {
        web_sys::window()
            .unwrap()
            .document()
            .unwrap()
            .pointer_lock_element()
            .is_some()
    }

    pub fn lock_cursor(&self) {
        self.canvas.request_pointer_lock();
    }

    pub fn unlock_cursor(&self) {
        web_sys::window()
            .unwrap()
            .document()
            .unwrap()
            .exit_pointer_lock();
    }

    pub fn start_text_edit(&self, text: &str) {
        self.editing_text.set(true);
        self.text_agent.set_value(text);
        self.update_text_agent();
    }

    pub fn stop_text_edit(&self) {
        self.editing_text.set(false);
        self.update_text_agent();
    }

    pub fn is_editing_text(&self) -> bool {
        self.editing_text.get()
    }

    pub fn run(
        self: Rc<Self>,
        event_handler: impl FnMut(Event) -> std::ops::ControlFlow<()> + 'static,
    ) {
        let event_handler = RefCell::new(event_handler);
        self.subscribe_events(move |event| {
            if let std::ops::ControlFlow::Break(()) = (event_handler.borrow_mut())(event) {
                panic!("Should not be exiting one the web!");
            }
        });
    }

    pub fn with_framebuffer<T>(&self, f: impl FnOnce(&mut ugli::Framebuffer) -> T) -> T {
        f(&mut ugli::Framebuffer::default(
            &self.ugli,
            self.real_size(),
        ))
    }

    pub fn show(&self) {
        show();
    }
}

trait Convert<T>: Sized {
    fn convert(value: T) -> Option<Self>;
}

trait ConvertEvent<T>: Sized {
    fn convert(value: T) -> Vec<Self>;
}

impl Convert<String> for Key {
    fn convert(key: String) -> Option<Key> {
        Some(match key.as_str() {
            "Backquote" => Key::Backquote,
            "Backslash" => Key::Backslash,
            "BracketLeft" => Key::BracketLeft,
            "BracketRight" => Key::BracketRight,
            "Comma" => Key::Comma,
            "Digit0" => Key::Digit0,
            "Digit1" => Key::Digit1,
            "Digit2" => Key::Digit2,
            "Digit3" => Key::Digit3,
            "Digit4" => Key::Digit4,
            "Digit5" => Key::Digit5,
            "Digit6" => Key::Digit6,
            "Digit7" => Key::Digit7,
            "Digit8" => Key::Digit8,
            "Digit9" => Key::Digit9,
            "Equal" => Key::Equal,
            "IntlBackslash" => Key::IntlBackslash,
            "IntlRo" => Key::IntlRo,
            "IntlYen" => Key::IntlYen,
            "KeyA" => Key::A,
            "KeyB" => Key::B,
            "KeyC" => Key::C,
            "KeyD" => Key::D,
            "KeyE" => Key::E,
            "KeyF" => Key::F,
            "KeyG" => Key::G,
            "KeyH" => Key::H,
            "KeyI" => Key::I,
            "KeyJ" => Key::J,
            "KeyK" => Key::K,
            "KeyL" => Key::L,
            "KeyM" => Key::M,
            "KeyN" => Key::N,
            "KeyO" => Key::O,
            "KeyP" => Key::P,
            "KeyQ" => Key::Q,
            "KeyR" => Key::R,
            "KeyS" => Key::S,
            "KeyT" => Key::T,
            "KeyU" => Key::U,
            "KeyV" => Key::V,
            "KeyW" => Key::W,
            "KeyX" => Key::X,
            "KeyY" => Key::Y,
            "KeyZ" => Key::Z,
            "Minus" => Key::Minus,
            "Period" => Key::Period,
            "Quote" => Key::Quote,
            "Semicolon" => Key::Semicolon,
            "Slash" => Key::Slash,
            "AltLeft" => Key::AltLeft,
            "AltRight" => Key::AltRight,
            "Backspace" => Key::Backspace,
            "CapsLock" => Key::CapsLock,
            "ContextMenu" => Key::ContextMenu,
            "ControlLeft" => Key::ControlLeft,
            "ControlRight" => Key::ControlRight,
            "Enter" => Key::Enter,
            "SuperLeft" => Key::SuperLeft,
            "SuperRight" => Key::SuperRight,
            "ShiftLeft" => Key::ShiftLeft,
            "ShiftRight" => Key::ShiftRight,
            "Space" => Key::Space,
            "Tab" => Key::Tab,
            "Delete" => Key::Delete,
            "End" => Key::End,
            "Help" => Key::Help,
            "Home" => Key::Home,
            "Insert" => Key::Insert,
            "PageDown" => Key::PageDown,
            "PageUp" => Key::PageUp,
            "ArrowDown" => Key::ArrowDown,
            "ArrowLeft" => Key::ArrowLeft,
            "ArrowRight" => Key::ArrowRight,
            "ArrowUp" => Key::ArrowUp,
            "NumLock" => Key::NumLock,
            "Numpad0" => Key::Numpad0,
            "Numpad1" => Key::Numpad1,
            "Numpad2" => Key::Numpad2,
            "Numpad3" => Key::Numpad3,
            "Numpad4" => Key::Numpad4,
            "Numpad5" => Key::Numpad5,
            "Numpad6" => Key::Numpad6,
            "Numpad7" => Key::Numpad7,
            "Numpad8" => Key::Numpad8,
            "Numpad9" => Key::Numpad9,
            "NumpadAdd" => Key::NumpadAdd,
            "NumpadBackspace" => Key::NumpadBackspace,
            "NumpadClear" => Key::NumpadClear,
            "NumpadClearEntry" => Key::NumpadClearEntry,
            "NumpadComma" => Key::NumpadComma,
            "NumpadDecimal" => Key::NumpadDecimal,
            "NumpadDivide" => Key::NumpadDivide,
            "NumpadEnter" => Key::NumpadEnter,
            "NumpadEqual" => Key::NumpadEqual,
            "NumpadHash" => Key::NumpadHash,
            "NumpadMemoryAdd" => Key::NumpadMemoryAdd,
            "NumpadMemoryClear" => Key::NumpadMemoryClear,
            "NumpadMemoryRecall" => Key::NumpadMemoryRecall,
            "NumpadMemoryStore" => Key::NumpadMemoryStore,
            "NumpadMemorySubtract" => Key::NumpadMemorySubtract,
            "NumpadMultiply" => Key::NumpadMultiply,
            "NumpadParenLeft" => Key::NumpadParenLeft,
            "NumpadParenRight" => Key::NumpadParenRight,
            "NumpadStar" => Key::NumpadStar,
            "NumpadSubtract" => Key::NumpadSubtract,
            "Escape" => Key::Escape,
            "BrowserBack" => Key::Back,
            "F1" => Key::F1,
            "F2" => Key::F2,
            "F3" => Key::F3,
            "F4" => Key::F4,
            "F5" => Key::F5,
            "F6" => Key::F6,
            "F7" => Key::F7,
            "F8" => Key::F8,
            "F9" => Key::F9,
            "F10" => Key::F10,
            "F11" => Key::F11,
            "F12" => Key::F12,

            _ => {
                log::trace!("Unrecognized key: {:?}", key);
                return None;
            }
        })
    }
}

impl Convert<i16> for MouseButton {
    fn convert(button: i16) -> Option<MouseButton> {
        Some(match button {
            0 => MouseButton::Left,
            1 => MouseButton::Middle,
            2 => MouseButton::Right,
            // 3 => MouseButton::Back,
            // 4 => MouseButton::Forward,
            _ => return None,
        })
    }
}

impl ConvertEvent<web_sys::KeyboardEvent> for Event {
    fn convert(event: web_sys::KeyboardEvent) -> Vec<Event> {
        if event.repeat() {
            return vec![];
        }
        let Some(key) = Convert::convert(event.code()) else {
            return vec![];
        };
        vec![match event.type_().as_str() {
            "keydown" => Event::KeyPress { key },
            "keyup" => Event::KeyRelease { key },
            _ => return vec![],
        }]
    }
}

impl ConvertEvent<web_sys::MouseEvent> for Event {
    fn convert(event: web_sys::MouseEvent) -> Vec<Event> {
        let event = || -> Option<Event> {
            let button = Convert::convert(event.button());
            let canvas: web_sys::HtmlCanvasElement = event.target().unwrap().dyn_into().unwrap();
            let position = vec2(
                event.offset_x(),
                canvas.height() as i32 - 1 - event.offset_y(),
            )
            .map(|x| x as f64);
            Some(match event.type_().as_str() {
                "mousedown" => Event::MousePress { button: button? },
                "mouseup" => Event::MouseRelease { button: button? },
                "mousemove" => {
                    let cursor_locked = web_sys::window()
                        .unwrap()
                        .document()
                        .unwrap()
                        .pointer_lock_element()
                        .is_some();
                    if cursor_locked {
                        let delta = vec2(event.movement_x(), -event.movement_y()).map(|x| x as f64);
                        // KEKW BROWSERS SUCK
                        const MAX: f64 = 50.0;
                        if delta.x.abs() > MAX || delta.y.abs() > MAX {
                            return None;
                        }

                        Event::RawMouseMove { delta }
                    } else {
                        Event::CursorMove { position }
                    }
                }
                _ => return None,
            })
        };
        event().into_iter().collect()
    }
}

impl ConvertEvent<web_sys::WheelEvent> for Event {
    fn convert(event: web_sys::WheelEvent) -> Vec<Event> {
        vec![Event::Wheel {
            delta: -event.delta_y()
                * match event.delta_mode() {
                    web_sys::WheelEvent::DOM_DELTA_PIXEL => 1.0,
                    web_sys::WheelEvent::DOM_DELTA_LINE => 51.0,
                    web_sys::WheelEvent::DOM_DELTA_PAGE => 800.0,
                    _ => {
                        log::error!("Unexpected delta mode: {}", event.delta_mode());
                        return vec![];
                    }
                },
        }]
    }
}

impl ConvertEvent<web_sys::TouchEvent> for Event {
    fn convert(event: web_sys::TouchEvent) -> Vec<Event> {
        let create_event: Box<dyn Fn(Touch) -> Event> = match event.type_().as_str() {
            "touchstart" => Box::new(Event::TouchStart),
            "touchmove" => Box::new(Event::TouchMove),
            "touchcancel" | "touchend" => Box::new(Event::TouchEnd),
            _ => return vec![],
        };
        let canvas: web_sys::HtmlCanvasElement = event.target().unwrap().dyn_into().unwrap();
        let rect = canvas.get_bounding_client_rect();
        let touches = event.changed_touches();
        (0..touches.length())
            .map(|index| {
                let touch = touches.item(index).unwrap();
                let offset_x = touch.page_x() as f64 - rect.left();
                let offset_y = touch.page_y() as f64 - rect.top();
                create_event(Touch {
                    id: touch.identifier() as u64,
                    position: vec2(offset_x, canvas.height() as f64 - 1.0 - offset_y),
                })
            })
            .collect()
    }
}

const TEXT_AGENT_PREFIX: &str = "💩";

impl Context {
    fn subscribe_to_raw<T>(
        &self,
        target: &web_sys::EventTarget,
        handler: impl Fn(T) + 'static,
        event_name: &str,
    ) where
        T: wasm_bindgen::convert::FromWasmAbi + 'static,
        T: AsRef<web_sys::Event>,
        T: Clone,
    {
        let handler = wasm_bindgen::closure::Closure::wrap(Box::new(handler) as Box<dyn Fn(T)>);
        target
            .add_event_listener_with_callback(event_name, handler.as_ref().unchecked_ref())
            .unwrap();
        handler.forget(); // TODO: not forget
    }
    fn subscribe_to<T>(
        &self,
        target: &web_sys::EventTarget,
        handler: &Rc<impl Fn(Event) + 'static>,
        event_name: &str,
    ) where
        T: wasm_bindgen::convert::FromWasmAbi + 'static,
        T: AsRef<web_sys::Event>,
        T: Clone,
        Event: ConvertEvent<T>,
    {
        let handler = handler.clone();
        let canvas = self.canvas.clone();
        let text_agent = self.text_agent.clone();
        let editing_text = self.editing_text.clone();
        let handler = move |event: T| {
            if editing_text.get() {
                text_agent.focus().unwrap();
            } else {
                canvas.focus().unwrap();
            }
            if event.as_ref().type_() == "contextmenu" {
                event.as_ref().prevent_default();
            }
            for e in ConvertEvent::convert(event.clone()) {
                handler(e);
                event.as_ref().prevent_default();
            }
        };
        self.subscribe_to_raw(target, handler, event_name);
    }
    fn subscribe_events<F: Fn(Event) + 'static>(&self, handler: F) {
        let handler = Rc::new(handler);
        let handler = &handler;
        self.subscribe_to::<web_sys::KeyboardEvent>(&self.canvas, handler, "keydown");
        self.subscribe_to::<web_sys::KeyboardEvent>(&self.canvas, handler, "keyup");
        self.subscribe_to_raw::<web_sys::InputEvent>(
            &self.text_agent,
            {
                let handler = handler.clone();
                move |event: web_sys::InputEvent| {
                    let input: web_sys::HtmlInputElement =
                        event.target().unwrap().dyn_into().unwrap();
                    handler(Event::EditText(input.value()));
                }
            },
            "input",
        );
        self.subscribe_to::<web_sys::MouseEvent>(&self.canvas, handler, "mousedown");
        self.subscribe_to::<web_sys::MouseEvent>(&self.canvas, handler, "mouseup");
        self.subscribe_to::<web_sys::MouseEvent>(&self.canvas, handler, "mousemove");
        self.subscribe_to::<web_sys::WheelEvent>(&self.canvas, handler, "wheel");
        self.subscribe_to::<web_sys::TouchEvent>(&self.canvas, handler, "touchstart");
        self.subscribe_to::<web_sys::TouchEvent>(&self.canvas, handler, "touchmove");
        self.subscribe_to::<web_sys::TouchEvent>(&self.canvas, handler, "touchend");
        self.subscribe_to::<web_sys::TouchEvent>(&self.canvas, handler, "touchcancel");
        self.subscribe_to::<web_sys::MouseEvent>(&self.canvas, handler, "contextmenu");
        {
            let handler = handler.clone();
            let closure =
                wasm_bindgen::closure::Closure::wrap(
                    Box::new(move || handler(Event::Draw)) as Box<dyn FnMut()>
                );
            request_animation_frame_loop(&closure);
            std::mem::forget(closure); // Don't drop so that JS can call this thing
        };
    }

    fn install_text_agent() -> Result<web_sys::HtmlInputElement, JsValue> {
        let window = web_sys::window().unwrap();
        let document = window.document().unwrap();
        let body = document.body().expect("document should have a body");
        let input = document
            .create_element("input")?
            .dyn_into::<web_sys::HtmlInputElement>()?;
        {
            let style = input.style();
            // Transparent
            style.set_property("opacity", "0").unwrap();
            // Hide under canvas
            style.set_property("z-index", "-1").unwrap();
            // z-index doesn't work otherwise
            style.set_property("position", "absolute").unwrap();
            style.set_property("top", "0").unwrap();
        }
        // Set size as small as possible, in case user may click on it.
        input.set_size(1);
        input.set_autofocus(true);
        input.set_hidden(true);
        input.set_value(TEXT_AGENT_PREFIX);

        body.append_child(&input)?;
        Ok(input)
    }

    /// Focus or blur text agent to toggle mobile keyboard.
    fn update_text_agent(&self) {
        if self.editing_text.get() {
            self.text_agent.set_hidden(false);
            self.text_agent.focus().unwrap();
        } else {
            self.text_agent.set_hidden(true);
            self.canvas.focus().unwrap();
        }
    }
}