mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-25 06:02:13 +00:00
Run rustfmt
This commit is contained in:
parent
29fabdddb1
commit
d04930c2a2
@ -1,18 +1,18 @@
|
||||
#[macro_use] mod utils;
|
||||
#[macro_use]
|
||||
mod utils;
|
||||
|
||||
use futures::{future, Future};
|
||||
use js_sys::Promise;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use utils::set_panic_hook;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::JsCast;
|
||||
use wasm_bindgen_futures::future_to_promise;
|
||||
use wasm_bindgen_futures::JsFuture;
|
||||
use web_sys::*;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use utils::set_panic_hook;
|
||||
|
||||
|
||||
// A macro to provide `println!(..)`-style syntax for `console.log` logging.
|
||||
macro_rules! log {
|
||||
@ -21,50 +21,46 @@ macro_rules! log {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn request_animation_frame(session: &XrSession, f: &Closure<dyn FnMut(f64, XrFrame)>) -> i32 {
|
||||
// This turns the Closure into a js_sys::Function
|
||||
// See https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen/closure/struct.Closure.html#casting-a-closure-to-a-js_sysfunction
|
||||
session
|
||||
.request_animation_frame(f.as_ref().unchecked_ref())
|
||||
session.request_animation_frame(f.as_ref().unchecked_ref())
|
||||
}
|
||||
|
||||
|
||||
pub fn create_webgl_context(xr_mode: bool) -> Result<WebGl2RenderingContext, JsValue> {
|
||||
let canvas = web_sys::window()
|
||||
.unwrap()
|
||||
.document()
|
||||
.unwrap()
|
||||
.get_element_by_id("canvas")
|
||||
.unwrap()
|
||||
.dyn_into::<HtmlCanvasElement>()
|
||||
.unwrap();
|
||||
.unwrap()
|
||||
.document()
|
||||
.unwrap()
|
||||
.get_element_by_id("canvas")
|
||||
.unwrap()
|
||||
.dyn_into::<HtmlCanvasElement>()
|
||||
.unwrap();
|
||||
|
||||
let gl: WebGl2RenderingContext = if xr_mode {
|
||||
let mut gl_attribs = HashMap::new();
|
||||
gl_attribs.insert(String::from("xrCompatible"), true);
|
||||
let js_gl_attribs = JsValue::from_serde(&gl_attribs).unwrap();
|
||||
|
||||
canvas.get_context_with_context_options("webgl2", &js_gl_attribs)?.unwrap().dyn_into()?
|
||||
}
|
||||
else {
|
||||
canvas
|
||||
.get_context_with_context_options("webgl2", &js_gl_attribs)?
|
||||
.unwrap()
|
||||
.dyn_into()?
|
||||
} else {
|
||||
canvas.get_context("webgl2")?.unwrap().dyn_into()?
|
||||
};
|
||||
|
||||
Ok(gl)
|
||||
}
|
||||
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct XrApp {
|
||||
session: Rc<RefCell<Option<XrSession>>>,
|
||||
gl: Rc<WebGl2RenderingContext>
|
||||
gl: Rc<WebGl2RenderingContext>,
|
||||
}
|
||||
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl XrApp {
|
||||
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new() -> XrApp {
|
||||
set_panic_hook();
|
||||
@ -93,7 +89,8 @@ impl XrApp {
|
||||
let gl = self.gl.clone();
|
||||
|
||||
let future_ = async move {
|
||||
let supports_session = wasm_bindgen_futures::JsFuture::from(session_supported_promise).await;
|
||||
let supports_session =
|
||||
wasm_bindgen_futures::JsFuture::from(session_supported_promise).await;
|
||||
let supports_session = supports_session.unwrap();
|
||||
if supports_session == false {
|
||||
log!("XR session not supported");
|
||||
@ -140,11 +137,14 @@ impl XrApp {
|
||||
// Schedule ourself for another requestAnimationFrame callback.
|
||||
// TODO: WebXR Samples call this at top of request_animation_frame - should this be moved?
|
||||
request_animation_frame(&sess, f.borrow().as_ref().unwrap());
|
||||
|
||||
}) as Box<dyn FnMut(f64, XrFrame)>));
|
||||
|
||||
let session: &Option<XrSession> = &self.session.borrow();
|
||||
let sess: &XrSession = if let Some(sess) = session { sess } else { return () };
|
||||
let sess: &XrSession = if let Some(sess) = session {
|
||||
sess
|
||||
} else {
|
||||
return ();
|
||||
};
|
||||
|
||||
request_animation_frame(sess, g.borrow().as_ref().unwrap());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user