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::{
|
2023-06-14 17:43:53 +10:00
|
|
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
2022-04-10 11:32:02 +10:00
|
|
|
event_loop::EventLoop,
|
2023-06-14 17:43:53 +10:00
|
|
|
keyboard::KeyCode,
|
|
|
|
window::{Fullscreen, WindowBuilder},
|
2019-12-04 04:16:06 +11:00
|
|
|
};
|
|
|
|
|
2023-04-11 21:50:52 +10:00
|
|
|
pub fn main() -> Result<(), impl std::error::Error> {
|
2019-12-04 04:16:06 +11:00
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
|
2023-07-13 01:11:52 +10:00
|
|
|
let builder = WindowBuilder::new().with_title("A fantastic window!");
|
|
|
|
#[cfg(wasm_platform)]
|
|
|
|
let builder = {
|
|
|
|
use winit::platform::web::WindowBuilderExtWebSys;
|
|
|
|
builder.with_append(true)
|
|
|
|
};
|
|
|
|
let window = builder.build(&event_loop).unwrap();
|
2019-12-04 04:16:06 +11:00
|
|
|
|
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(),
|
2023-07-29 02:37:56 +10:00
|
|
|
Event::AboutToWait => {
|
2019-12-04 04:16:06 +11:00
|
|
|
window.request_redraw();
|
|
|
|
}
|
2023-06-14 17:43:53 +10:00
|
|
|
Event::WindowEvent {
|
|
|
|
window_id,
|
|
|
|
event:
|
|
|
|
WindowEvent::KeyboardInput {
|
|
|
|
event:
|
|
|
|
KeyEvent {
|
|
|
|
physical_key: KeyCode::KeyF,
|
|
|
|
state: ElementState::Released,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
},
|
|
|
|
} if window_id == window.id() => {
|
|
|
|
if window.fullscreen().is_some() {
|
|
|
|
window.set_fullscreen(None);
|
|
|
|
} else {
|
|
|
|
window.set_fullscreen(Some(Fullscreen::Borderless(None)));
|
|
|
|
}
|
|
|
|
}
|
2020-01-05 18:12:03 +11:00
|
|
|
_ => (),
|
2019-12-04 04:16:06 +11:00
|
|
|
}
|
2023-04-11 21:50:52 +10: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 {
|
2023-07-10 10:02:38 +10:00
|
|
|
use std::num::NonZeroU32;
|
|
|
|
|
|
|
|
use softbuffer::{Surface, SurfaceExtWeb};
|
2019-12-04 04:16:06 +11:00
|
|
|
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)]
|
2023-04-11 21:50:52 +10:00
|
|
|
let _ = super::main();
|
2019-12-04 04:16:06 +11:00
|
|
|
}
|
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();
|
2023-07-10 10:02:38 +10:00
|
|
|
let mut surface = Surface::from_canvas(canvas.clone()).unwrap();
|
|
|
|
surface
|
|
|
|
.resize(
|
|
|
|
NonZeroU32::new(canvas.width()).unwrap(),
|
|
|
|
NonZeroU32::new(canvas.height()).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let mut buffer = surface.buffer_mut().unwrap();
|
|
|
|
buffer.fill(0xFFF0000);
|
|
|
|
buffer.present().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();
|
|
|
|
|
2023-07-10 10:02:38 +10:00
|
|
|
let style = &canvas.style();
|
|
|
|
style.set_property("margin", "50px").unwrap();
|
|
|
|
// Use to test interactions with border and padding.
|
|
|
|
//style.set_property("border", "50px solid black").unwrap();
|
|
|
|
//style.set_property("padding", "50px").unwrap();
|
2022-02-25 22:57:46 +11:00
|
|
|
|
|
|
|
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.
|
2023-07-10 10:02:38 +10:00
|
|
|
let event = match event {
|
|
|
|
Event::WindowEvent { event, .. } => Some(format!("{event:?}")),
|
|
|
|
Event::Resumed | Event::Suspended => Some(format!("{event:?}")),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(event) = event {
|
2022-02-25 22:57:46 +11:00
|
|
|
let window = web_sys::window().unwrap();
|
|
|
|
let document = window.document().unwrap();
|
|
|
|
let log = document.create_element("li").unwrap();
|
2023-07-10 10:02:38 +10:00
|
|
|
|
|
|
|
let date = js_sys::Date::new_0();
|
|
|
|
log.set_text_content(Some(&format!(
|
|
|
|
"{:02}:{:02}:{:02}.{:03}: {event}",
|
|
|
|
date.get_hours(),
|
|
|
|
date.get_minutes(),
|
|
|
|
date.get_seconds(),
|
|
|
|
date.get_milliseconds(),
|
|
|
|
)));
|
|
|
|
|
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
|
|
|
}
|