2023-06-05 10:44:54 +10:00
|
|
|
#![allow(clippy::disallowed_methods, clippy::single_match)]
|
2022-06-10 20:43:33 +10:00
|
|
|
|
2019-12-04 04:16:06 +11:00
|
|
|
use winit::{
|
|
|
|
event::{Event, WindowEvent},
|
2022-04-10 11:32:02 +10:00
|
|
|
event_loop::EventLoop,
|
2019-12-04 04:16:06 +11:00
|
|
|
window::WindowBuilder,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
|
|
|
|
let window = WindowBuilder::new()
|
|
|
|
.with_title("A fantastic window!")
|
|
|
|
.build(&event_loop)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-12-25 18:57:27 +11:00
|
|
|
#[cfg(wasm_platform)]
|
2022-09-04 02:26:24 +10:00
|
|
|
let log_list = wasm::insert_canvas_and_create_log_list(&window);
|
2019-12-04 04:16:06 +11:00
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2022-04-10 11:32:02 +10:00
|
|
|
control_flow.set_wait();
|
2020-01-05 18:12:03 +11:00
|
|
|
|
2022-12-25 18:57:27 +11:00
|
|
|
#[cfg(wasm_platform)]
|
2022-02-25 22:57:46 +11:00
|
|
|
wasm::log_event(&log_list, &event);
|
2019-12-04 04:16:06 +11:00
|
|
|
|
|
|
|
match event {
|
|
|
|
Event::WindowEvent {
|
|
|
|
event: WindowEvent::CloseRequested,
|
|
|
|
window_id,
|
2022-04-10 11:32:02 +10:00
|
|
|
} if window_id == window.id() => control_flow.set_exit(),
|
2019-12-04 04:16:06 +11:00
|
|
|
Event::MainEventsCleared => {
|
|
|
|
window.request_redraw();
|
|
|
|
}
|
2020-01-05 18:12:03 +11:00
|
|
|
_ => (),
|
2019-12-04 04:16:06 +11:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-25 18:57:27 +11:00
|
|
|
#[cfg(wasm_platform)]
|
2019-12-04 04:16:06 +11:00
|
|
|
mod wasm {
|
|
|
|
use wasm_bindgen::prelude::*;
|
2022-02-25 22:57:46 +11:00
|
|
|
use winit::{event::Event, window::Window};
|
2019-12-04 04:16:06 +11:00
|
|
|
|
|
|
|
#[wasm_bindgen(start)]
|
|
|
|
pub fn run() {
|
2021-12-11 13:02:48 +11:00
|
|
|
console_log::init_with_level(log::Level::Debug).expect("error initializing logger");
|
2019-12-04 04:16:06 +11:00
|
|
|
|
2022-06-10 20:43:33 +10:00
|
|
|
#[allow(clippy::main_recursion)]
|
2019-12-04 04:16:06 +11:00
|
|
|
super::main();
|
|
|
|
}
|
2022-02-25 22:57:46 +11:00
|
|
|
|
2022-09-04 02:26:24 +10:00
|
|
|
pub fn insert_canvas_and_create_log_list(window: &Window) -> web_sys::Element {
|
2022-02-25 22:57:46 +11:00
|
|
|
use winit::platform::web::WindowExtWebSys;
|
|
|
|
|
2023-06-05 10:44:54 +10:00
|
|
|
let canvas = window.canvas().unwrap();
|
2022-02-25 22:57:46 +11:00
|
|
|
|
|
|
|
let window = web_sys::window().unwrap();
|
|
|
|
let document = window.document().unwrap();
|
|
|
|
let body = document.body().unwrap();
|
|
|
|
|
2022-09-04 02:26:24 +10:00
|
|
|
// Set a background color for the canvas to make it easier to tell where the canvas is for debugging purposes.
|
2023-06-04 22:29:59 +10:00
|
|
|
canvas
|
|
|
|
.style()
|
|
|
|
.set_property("background-color", "crimson")
|
|
|
|
.unwrap();
|
2022-02-25 22:57:46 +11:00
|
|
|
body.append_child(&canvas).unwrap();
|
|
|
|
|
|
|
|
let log_header = document.create_element("h2").unwrap();
|
|
|
|
log_header.set_text_content(Some("Event Log"));
|
|
|
|
body.append_child(&log_header).unwrap();
|
|
|
|
|
|
|
|
let log_list = document.create_element("ul").unwrap();
|
|
|
|
body.append_child(&log_list).unwrap();
|
|
|
|
log_list
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log_event(log_list: &web_sys::Element, event: &Event<()>) {
|
|
|
|
log::debug!("{:?}", event);
|
|
|
|
|
|
|
|
// Getting access to browser logs requires a lot of setup on mobile devices.
|
|
|
|
// So we implement this basic logging system into the page to give developers an easy alternative.
|
|
|
|
// As a bonus its also kind of handy on desktop.
|
|
|
|
if let Event::WindowEvent { event, .. } = &event {
|
|
|
|
let window = web_sys::window().unwrap();
|
|
|
|
let document = window.document().unwrap();
|
|
|
|
let log = document.create_element("li").unwrap();
|
2023-01-27 15:18:58 +11:00
|
|
|
log.set_text_content(Some(&format!("{event:?}")));
|
2022-02-25 22:57:46 +11:00
|
|
|
log_list
|
|
|
|
.insert_before(&log, log_list.first_child().as_ref())
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 04:16:06 +11:00
|
|
|
}
|