From 0d009c6c3d95a08d5b69815043b995b41fa46169 Mon Sep 17 00:00:00 2001 From: Bob Voorneveld Date: Wed, 19 Jan 2022 15:11:27 +0100 Subject: [PATCH] Make sure game will work on iOS --- js/koi/gui/loader/loader.js | 19 ++++++++++++++----- js/koi/gui/loader/loaderDiscord.js | 9 ++++++--- js/koi/gui/menu/menu.js | 10 +++++++--- js/language.js | 4 ++-- js/main.js | 14 +++++++++++--- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/js/koi/gui/loader/loader.js b/js/koi/gui/loader/loader.js index 2b5d3f5d..4cbb8244 100644 --- a/js/koi/gui/loader/loader.js +++ b/js/koi/gui/loader/loader.js @@ -5,6 +5,8 @@ * @param {HTMLElement} elementSlots The save slots element * @param {HTMLElement} elementButtonSettings The element to build the settings button in * @param {HTMLElement} wrapper The wrapper to toggle fullscreen on + * @param {Boolean} loadDiscord If set to true, the Discord button will be shown + * @param {Boolean} loadFullscreen If set to true, the Fullscreen button will be shown * @constructor */ const Loader = function( @@ -12,7 +14,8 @@ const Loader = function( elementGraphics, elementSlots, elementButtonSettings, - wrapper) { + wrapper, + loadFullscreen) { this.element = element; this.icon = new LoaderIcon(); this.elementSlots = elementSlots; @@ -27,13 +30,18 @@ const Loader = function( this.onContinue = null; this.menu = null; this.fullscreen = new LoaderFullscreen(wrapper); + this.loadFullscreen = loadFullscreen; this.overlayCanvas = document.createElement("canvas"); this.element.appendChild(this.overlayCanvas); this.slots = null; - element.appendChild(this.fullscreen.element); element.appendChild(this.elementDiscord.element); + + if(loadFullscreen) { + element.appendChild(this.fullscreen.element); + } + elementGraphics.appendChild(this.icon.element); const loop = () => { @@ -133,8 +141,9 @@ Loader.prototype.createButtonSettings = function() { * Finish loading */ Loader.prototype.complete = function() { - this.fullscreen.setLoaded(); - + if(!this.loadFullscreen) { + this.fullscreen.setLoaded(); + } const onNewGame = index => { this.onNewGame(index); this.onFinish(); @@ -234,4 +243,4 @@ Loader.prototype.finish = function(weight) { */ Loader.prototype.createRequirement = function(weight) { return new Loader.Requirement(this, weight); -}; \ No newline at end of file +}; diff --git a/js/koi/gui/loader/loaderDiscord.js b/js/koi/gui/loader/loaderDiscord.js index 6d8bd0e2..8b4e3d86 100644 --- a/js/koi/gui/loader/loaderDiscord.js +++ b/js/koi/gui/loader/loaderDiscord.js @@ -28,10 +28,13 @@ LoaderDiscord.prototype.loadSVG = function() { }, 1000 * this.FADE_IN_DELAY); this.element.onclick = () => { - if (window["require"]) + if (window["require"]) { window["require"]("electron")["shell"]["openExternal"](this.URL); - else + } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openDiscordHandler) { + window.webkit.messageHandlers.openDiscordHandler.postMessage({url: this.URL}); + } else { window.open(this.URL, "_blank"); + } }; }; @@ -50,4 +53,4 @@ LoaderDiscord.prototype.createElement = function() { element.className = this.CLASS_INVISIBLE; return element; -}; \ No newline at end of file +}; diff --git a/js/koi/gui/menu/menu.js b/js/koi/gui/menu/menu.js index d579abbe..9929275a 100644 --- a/js/koi/gui/menu/menu.js +++ b/js/koi/gui/menu/menu.js @@ -63,13 +63,15 @@ Menu.prototype.LANGUAGES = [ * @param {AudioEngine} audioEngine The audio engine * @param {Object} externalSettings The external settings object * @param {AudioBank} audio Game audio + * @param {createFullscreenButton} If set to true, a fullscreen button will be created * @returns {HTMLDivElement} The menu box */ Menu.prototype.createBox = function( fullscreen, audioEngine, externalSettings, - audio) { + audio, + createFullscreenButton) { const element = document.createElement("div"); const table = document.createElement("table"); @@ -84,7 +86,9 @@ Menu.prototype.createBox = function( table.appendChild(this.languageChooser); element.appendChild(table); - element.appendChild(this.createButtonFullscreen(fullscreen, audio)); + if(createFullscreenButton) { + element.appendChild(this.createButtonFullscreen(fullscreen, audio)); + } element.appendChild(this.buttonBack); return element; @@ -371,4 +375,4 @@ Menu.prototype.toggle = function() { this.hide(); else this.show(); -}; \ No newline at end of file +}; diff --git a/js/language.js b/js/language.js index ab99d46a..9923c8ea 100644 --- a/js/language.js +++ b/js/language.js @@ -40,7 +40,7 @@ Language.prototype.load = function(onFinish, onFailure, file = this.file) { request.open("GET", file, true); request.onreadystatechange = () => { if (request.readyState === 4) { - if (request.status === 200) { + if (request.responseText.length > 0) { const data = JSON.parse(request.responseText); this.add(data); @@ -65,4 +65,4 @@ Language.prototype.load = function(onFinish, onFailure, file = this.file) { }; request.send(null); -}; \ No newline at end of file +}; diff --git a/js/main.js b/js/main.js index f0612ba7..5731a49b 100644 --- a/js/main.js +++ b/js/main.js @@ -11,6 +11,12 @@ const gl = canvas.getContext("experimental-webgl", glParameters); let chosenLocale = null; +/** + * Check if game is running within WKWebView on iOS. + */ + +const RUNNING_ON_WEBVIEW_IOS = (window.webkit && window.webkit.messageHandlers) ? true : false; + /** * Called when loading resources failed */ @@ -100,7 +106,8 @@ const loader = new Loader( document.getElementById("loader-graphics"), document.getElementById("loader-slots"), document.getElementById("loader-button-settings"), - document.getElementById("wrapper")); + document.getElementById("wrapper"), + !RUNNING_ON_WEBVIEW_IOS); let imperial = false; if (gl && @@ -132,7 +139,8 @@ if (gl && chosenLocale, audioEngine, settings, - audio); + audio, + !RUNNING_ON_WEBVIEW_IOS); let lastTime = null; let koi = null; let loaded = true; @@ -330,4 +338,4 @@ if (gl && document.getElementById("code-icon-defs")); } else - onFailure(); \ No newline at end of file + onFailure();