diff options
author | Matthew Michelotti <michelotti.matthew@gmail.com> | 2018-07-21 18:34:31 -0500 |
---|---|---|
committer | Matthew Michelotti <michelotti.matthew@gmail.com> | 2018-07-21 18:34:31 -0500 |
commit | 13b96831ac7c326e7f623d801b04662f122d5922 (patch) | |
tree | de5cbac3b451831229c2a5c5a340a0cfb4c5c89d | |
parent | 70fd79a5a036002247f170315be4820510763f42 (diff) |
added html fullscreen requests
-rw-r--r-- | gate/src/app_context.rs | 29 | ||||
-rw-r--r-- | gate/src/core/wasm/mod.rs | 24 | ||||
-rw-r--r-- | gate/src/core/wasm/wasm_imports.rs | 4 | ||||
-rw-r--r-- | gate_build/src/html/gate.js | 41 |
4 files changed, 94 insertions, 4 deletions
diff --git a/gate/src/app_context.rs b/gate/src/app_context.rs index ac21747..879daa7 100644 --- a/gate/src/app_context.rs +++ b/gate/src/app_context.rs @@ -25,6 +25,8 @@ pub struct AppContext<A: AppAssetId> { cursor: (f64, f64), close_requested: bool, native_px: f64, + is_fullscreen: bool, + desires_fullscreen: bool, } impl<A: AppAssetId> AppContext<A> { @@ -35,6 +37,8 @@ impl<A: AppAssetId> AppContext<A> { cursor: (0., 0.), close_requested: false, native_px, + is_fullscreen: false, + desires_fullscreen: false, } } @@ -82,6 +86,31 @@ impl<A: AppAssetId> AppContext<A> { ) } + /// Requests the app to enter fullscreen mode. + /// + /// Depending on the target and how this method is invoked, the app may or may not + /// actually enter fullscreen mode. When compiling to `wasm32-unknown-unknown` and + /// running in a web browser, fullscreen requests can only be made successfully during + /// certain user input events, so invoking fullscreen during `App.start` or `App.advance` + /// will likely fail. + pub fn request_fullscreen(&mut self) { self.desires_fullscreen = true; } + + /// Requests the app to cancel fullscreen mode. + pub fn cancel_fullscreen(&mut self) { self.desires_fullscreen = false; } + + /// Checks whether or not the app is currently in fullscreen mode. + /// + /// This value will not change immediately after a call to `request_fullscreen` or + /// `cancel_fullscreen`. + pub fn is_fullscreen(&self) -> bool { self.is_fullscreen } + + pub(crate) fn desires_fullscreen(&self) -> bool { self.desires_fullscreen } + + pub(crate) fn set_is_fullscreen(&mut self, is_fullscreen: bool) { + self.is_fullscreen = is_fullscreen; + self.desires_fullscreen = is_fullscreen; + } + /// Closes the app entirely. /// /// When compiling to `wasm32-unknown-unknown`, the app may be resumed after it is closed diff --git a/gate/src/core/wasm/mod.rs b/gate/src/core/wasm/mod.rs index c71c97d..47584b7 100644 --- a/gate/src/core/wasm/mod.rs +++ b/gate/src/core/wasm/mod.rs @@ -93,6 +93,20 @@ struct AppRunner<AS: AppAssetId, AP: App<AS>> { held_keys: HashSet<KeyCode>, } +impl<AS: AppAssetId, AP: App<AS>> AppRunner<AS, AP> { + fn update_is_fullscreen(&mut self) { + self.ctx.set_is_fullscreen(unsafe { gateWasmIsFullscreen() != 0 }); + } + + fn resolve_fullscreen_requests(&self) { + match (self.ctx.is_fullscreen(), self.ctx.desires_fullscreen()) { + (false, true) => unsafe { gateWasmRequestFullscreen() }, + (true, false) => unsafe { gateWasmCancelFullscreen() }, + (false, false) | (true, true) => {}, + } + } +} + impl<AS: AppAssetId, AP: App<AS>> TraitAppRunner for AppRunner<AS, AP> { fn init(&mut self) { assert!(self.renderer.is_none()); @@ -121,6 +135,7 @@ impl<AS: AppAssetId, AP: App<AS>> TraitAppRunner for AppRunner<AS, AP> { } fn update_and_draw(&mut self, time_sec: f64) -> bool { + self.update_is_fullscreen(); let elapsed = self.last_time_sec.map(|x| time_sec - x).unwrap_or(0.0).max(0.0).min(0.1); if elapsed > 0.0 { self.app.advance(elapsed.min(::MAX_TIMESTEP), &mut self.ctx); @@ -140,6 +155,7 @@ impl<AS: AppAssetId, AP: App<AS>> TraitAppRunner for AppRunner<AS, AP> { } fn input(&mut self, key: KeyCode, down: bool) -> bool { + self.update_is_fullscreen(); if down { if self.held_keys.insert(key) { self.app.key_down(key, &mut self.ctx); @@ -149,13 +165,19 @@ impl<AS: AppAssetId, AP: App<AS>> TraitAppRunner for AppRunner<AS, AP> { self.app.key_up(key, &mut self.ctx); } } - !self.ctx.take_close_request() + if self.ctx.take_close_request() { + false + } else { + self.resolve_fullscreen_requests(); + true + } } fn music_count(&self) -> u16 { AS::Music::count() } fn sound_count(&self) -> u16 { AS::Sound::count() } fn on_restart(&mut self) { + self.update_is_fullscreen(); for key in self.held_keys.drain() { self.app.key_up(key, &mut self.ctx); } diff --git a/gate/src/core/wasm/wasm_imports.rs b/gate/src/core/wasm/wasm_imports.rs index c99757b..0cd1a93 100644 --- a/gate/src/core/wasm/wasm_imports.rs +++ b/gate/src/core/wasm/wasm_imports.rs @@ -31,4 +31,8 @@ extern { pub fn gateWasmSpriteAtlasBinFill(buffer: *mut c_void); pub fn gateWasmTiledAtlasBinSize() -> usize; pub fn gateWasmTiledAtlasBinFill(buffer: *mut c_void); + + pub fn gateWasmRequestFullscreen(); + pub fn gateWasmCancelFullscreen(); + pub fn gateWasmIsFullscreen() -> c_int; } diff --git a/gate_build/src/html/gate.js b/gate_build/src/html/gate.js index 71ea778..cc84d7b 100644 --- a/gate_build/src/html/gate.js +++ b/gate_build/src/html/gate.js @@ -27,7 +27,7 @@ function gate(args) { canvas = args.canvas; wasmFilePath = args.wasmFilePath; onload = args.onload; - onquit = args.onQuit; + onquit = args.onquit; const floatSize = 4; @@ -129,6 +129,41 @@ function gate(args) { gateWasmSpriteAtlasBinFill: function(bufferPtr) { new Uint8Array(Module.memory.buffer).set(Module.spriteAtlas, bufferPtr); }, + gateWasmRequestFullscreen: function () { + if (wrapperDiv.requestFullscreen) { + wrapperDiv.requestFullscreen(); + } else if (wrapperDiv.mozRequestFullScreen) { + wrapperDiv.mozRequestFullScreen(); + } else if (wrapperDiv.webkitRequestFullScreen) { + wrapperDiv.webkitRequestFullScreen(); + } else if (wrapperDiv.msRequestFullscreen) { + wrapperDiv.msRequestFullscreen(); + } + }, + gateWasmCancelFullscreen: function () { + if (document.exitFullscreen) { + document.exitFullscreen(); + } else if (document.mozCancelFullScreen) { + document.mozCancelFullScreen(); + } else if (document.webkitCancelFullScreen) { + document.webkitCancelFullScreen(); + } else if (document.msExitFullscreen) { + document.msExitFullscreen(); + } + }, + gateWasmIsFullscreen: function () { + if (document.fullscreen !== undefined) { + return document.fullscreen; + } else if (document.mozFullScreen !== undefined) { + return document.mozFullScreen; + } else if (document.webkitIsFullScreen !== undefined) { + return document.webkitIsFullScreen; + } else if (document.msFullscreenElement !== undefined) { + return document.msFullscreenElement; + } else { + return false; + } + }, Math_atan2: Math.atan2, cos: Math.cos, sin: Math.sin, @@ -359,8 +394,8 @@ function gate(args) { if (Module.currentMusic != null) { Module.currentMusic.pause(); } - if (onQuit) { - onQuit(); + if (onquit) { + onquit(); } } |