mirror of
https://github.com/rust-windowing/winit.git
synced 2026-06-26 22:53:15 -04:00
Add gamepad support for stdweb and web-sys, as well as web-specific gamepad examples. * [web] Fix compilation error from device api * [wasm] Apply device api changes * [wasm] Format and cleanup * [wasm32] Implement gamepad connections * [wasm] Harmonize * [Test] Made some tests with wasm-pack * Quick fix instant non supporting Hash trait * Fix on_received_character * [web_sys] Split add_event and add_window_event * [web] split device implementations * Update tests/web...still does not work * [tests/web] do not ignore index.html * [web/web_sys] split canvas and window * [tests/web] enable stack trace * [web] fix borrowmut * [web_sys] fix gamepad registration * [web] harmonize naming * [web_sys] create global emitter * [web] implement gamepad buttons * [web] implement gamepad axis * [web] cleanup * [web] update test * [web] move tests/web to examples/web * [web] axis does produce stick event * [web] Support Stick event * [web] implement gamepad to stdweb * [web] rename examples/web to examples/wasm * [web/web-sys] Move gamepad_manager from backend * [web/web_sys] implement EventLoop::gamepads * [web/web_sys] Drain gamepad events * [web/stdweb] apply web_sys changes * [web] update web/examples * [web] move gamepads code to gamepad_manager * [web] simplify and optimise * [web] replace EventCode to GamepadAxis and GamepadButton structs * [web] reuse gamepad events due to chrome issue * [web] rumble does not work * [web/stdweb] try debugging * [web] fix Chrome gamepad not updated * [web/stdweb] created an example * [examples] fix paths * fix warnings * [web/examples] update comments * [web/stdweb] add experimental support to vibrate() * [web] add CR
86 lines
2.4 KiB
Rust
86 lines
2.4 KiB
Rust
mod canvas;
|
|
pub mod gamepad;
|
|
mod timeout;
|
|
mod utils;
|
|
pub mod window;
|
|
|
|
pub use canvas::Canvas;
|
|
pub use timeout::Timeout;
|
|
|
|
use crate::dpi::LogicalSize;
|
|
use crate::platform::web::WindowExtWebSys;
|
|
use crate::window::Window;
|
|
use wasm_bindgen::{closure::Closure, JsCast};
|
|
use web_sys::{window, BeforeUnloadEvent, Element, HtmlCanvasElement};
|
|
|
|
pub fn throw(msg: &str) {
|
|
wasm_bindgen::throw_str(msg);
|
|
}
|
|
|
|
pub fn exit_fullscreen() {
|
|
let window = web_sys::window().expect("Failed to obtain window");
|
|
let document = window.document().expect("Failed to obtain document");
|
|
|
|
document.exit_fullscreen();
|
|
}
|
|
|
|
pub fn on_unload(mut handler: impl FnMut() + 'static) {
|
|
let window = web_sys::window().expect("Failed to obtain window");
|
|
|
|
let closure = Closure::wrap(
|
|
Box::new(move |_: BeforeUnloadEvent| handler()) as Box<dyn FnMut(BeforeUnloadEvent)>
|
|
);
|
|
|
|
window
|
|
.add_event_listener_with_callback("beforeunload", &closure.as_ref().unchecked_ref())
|
|
.expect("Failed to add close listener");
|
|
}
|
|
|
|
impl WindowExtWebSys for Window {
|
|
fn canvas(&self) -> HtmlCanvasElement {
|
|
self.window.canvas().raw().clone()
|
|
}
|
|
}
|
|
|
|
pub fn window_size() -> LogicalSize {
|
|
let window = web_sys::window().expect("Failed to obtain window");
|
|
let width = window
|
|
.inner_width()
|
|
.expect("Failed to get width")
|
|
.as_f64()
|
|
.expect("Failed to get width as f64");
|
|
let height = window
|
|
.inner_height()
|
|
.expect("Failed to get height")
|
|
.as_f64()
|
|
.expect("Failed to get height as f64");
|
|
|
|
LogicalSize { width, height }
|
|
}
|
|
|
|
pub fn is_fullscreen(canvas: &HtmlCanvasElement) -> bool {
|
|
let window = window().expect("Failed to obtain window");
|
|
let document = window.document().expect("Failed to obtain document");
|
|
|
|
match document.fullscreen_element() {
|
|
Some(elem) => {
|
|
let raw: Element = canvas.clone().into();
|
|
raw == elem
|
|
}
|
|
None => false,
|
|
}
|
|
}
|
|
|
|
pub fn get_gamepads() -> impl Iterator<Item = gamepad::Gamepad> {
|
|
let mut gamepads: Vec<gamepad::Gamepad> = Vec::new();
|
|
let web_gamepads = web_sys::window().unwrap().navigator().get_gamepads().ok().unwrap();
|
|
for index in 0..web_gamepads.length() {
|
|
let jsvalue = web_gamepads.get(index);
|
|
if !jsvalue.is_null() {
|
|
let gamepad: web_sys::Gamepad = jsvalue.into();
|
|
gamepads.push(gamepad::Gamepad::new(gamepad));
|
|
}
|
|
}
|
|
gamepads.into_iter()
|
|
}
|