Godot Engine – Multi-platform 2D and 3D game engine
web: Refactor Web Editor using iframes
Fabio Alessandrellie2c745db10ea
diff --git a/misc/dist/html/editor.html b/misc/dist/html/editor.htmlindex 65a48fa47ea..a69d63c0f94 100644--- a/misc/dist/html/editor.html+++ b/misc/dist/html/editor.html@@ -26,7 +26,7 @@<link rel="preload" href="inter-regular.woff2" as="font" crossorigin="anonymous"><link rel="preload" href="inter-bold.woff2" as="font" crossorigin="anonymous"><title>Godot Engine Web Editor (___GODOT_VERSION___)</title>- <style>+ <style id="web-editor-style">/* Use Inter in the same configuration as used in the editor (e.g. alternate lowercase `l`). */@font-face {font-family: "Inter";@@ -73,6 +73,14 @@ body {overflow: hidden;}+iframe {+ margin: 0;+ border: none;+ padding: 0;+ background-color: #262626;+ overflow: hidden;+}+h2 {font-size: 21px;}@@ -134,21 +142,9 @@ a:active {min-height: 100vh;}-#canvas, #gameCanvas {- display: block;- margin: 0;- color: white;-}-/* Don't show distracting focus outlines for the main tabs' contents. */-#tab-editor canvas:focus,-#tab-game canvas:focus,-#canvas:focus,-#gameCanvas:focus {- outline: none;-}-.godot {+ outline: none;color: #d0d0d0;background-color: #1b1b1b;background-image: linear-gradient(to bottom, #403e48, #35333c);@@ -156,6 +152,26 @@ a:active {box-shadow: 0 0 1px 1px #141414;}+#tab-status {+ position: absolute;+ top: 0;+ left: 0;+ right: 0;+ bottom: 0;+ display: flex;+ flex-direction: column;+ justify-content: center;+ align-items: center;+}++#tab-editor iframe:focus-visible,+#tab-game iframe:focus-visible,+#godot-canvas:focus-visible {+ outline: none;+ border: none;+ box-shadow: none;+}+.btn {appearance: none;color: #d0d0d0;@@ -217,19 +233,6 @@ a:active {/* Status display */-#status {- position: absolute;- left: 0;- top: 0;- right: 0;- bottom: 0;- display: flex;- justify-content: center;- align-items: center;- /* don't consume click events - make children visible explicitly */- visibility: hidden;-}-#status-progress {width: 366px;height: 7px;@@ -252,7 +255,7 @@ a:active {width: 0;box-sizing: border-box;transition: width 0.5s linear;- background-color: #202020;+ background-color: #478cbf;border: 1px solid #222223;box-shadow: 0 0 1px 1px #27282E;border-radius: 3px;@@ -297,7 +300,7 @@ a:active {role="dialog"aria-labelledby="welcome-modal-title"aria-describedby="welcome-modal-description"- onclick="if (event.target === this) closeWelcomeModal(false)"+ onclick="if (event.target === this) closeWelcomeModal()"><div class="welcome-modal-content"><h2 id="welcome-modal-title" class="welcome-modal-title">Important - Please read before continuing</h2>@@ -338,19 +341,24 @@ a:active {>Exporting for the Web</a> for more information.</p></div>+ <div style="text-align: center;">+ <label>+ <input id="welcome-modal-dont-show-again-check" type="checkbox">+ Don't Show Again+ </label>+ </div><div style="text-align: center">- <button id="welcome-modal-dismiss" class="btn" type="button" onclick="closeWelcomeModal(true)" style="margin-top: 1rem">- OK - Don't Show Again+ <button id="welcome-modal-dismiss" class="btn" type="button" onclick="closeWelcomeModal()" style="margin-top: 1rem">+ OK</button></div></div></div><div id="tabs-buttons">- <button id="btn-tab-loader" class="btn tab-btn" onclick="showTab('loader')">Loader</button>- <button id="btn-tab-editor" class="btn tab-btn next-to-close-btn" disabled="disabled" onclick="showTab('editor')">Editor</button>- <button id="btn-close-editor" class="btn close-btn" disabled="disabled" onclick="closeEditor()">×</button>- <button id="btn-tab-game" class="btn tab-btn next-to-close-btn" disabled="disabled" onclick="showTab('game')">Game</button>- <button id="btn-close-game" class="btn close-btn" disabled="disabled" onclick="closeGame()">×</button>+ <button id="btn-tab-editor" class="btn tab-btn next-to-close-btn" disabled="disabled">Editor</button>+ <button id="btn-close-editor" class="btn close-btn" disabled="disabled">×</button>+ <button id="btn-tab-game" class="btn tab-btn next-to-close-btn" disabled="disabled">Game</button>+ <button id="btn-close-game" class="btn close-btn" disabled="disabled">×</button><button id="btn-tab-update" class="btn tab-btn" style="display: none;">Update</button></div><div id="tabs">@@ -362,12 +370,7 @@ a:active {___GODOT_VERSION___ - <a href="/releases/">Change version</a><br><br>- <label for="zip-file" style="margin-right: 1rem">Preload project ZIP:</label>- <input id="zip-file" type="file" name="files" style="margin-bottom: 0.5rem">- <br>- <a href="demo.zip" id="zip-demo">(Try this for example)</a>- <br>- <button id="startButton" class="btn" style="margin-top: 2rem; margin-bottom: 3rem; font-weight: 700">Start Godot Editor</button>+ <button id="btn-start-editor" class="btn" style="margin-top: 2rem; margin-bottom: 3rem; font-weight: 700">Start Godot Editor</button><br><button class="btn" onclick="clearPersistence()" style="margin-bottom: 1.5rem">Clear Persistent Data</button><br>@@ -375,16 +378,10 @@ a:active {</div></div><div id="tab-editor" style="display: none;">- <canvas id="editor-canvas" tabindex="1">- HTML5 canvas appears to be unsupported in the current browser.<br>- Please try updating or use a different browser.- </canvas>+ <iframe id="editor-iframe" tabindex="1"></iframe></div><div id="tab-game" style="display: none;">- <canvas id="game-canvas" tabindex="2">- HTML5 canvas appears to be unsupported in the current browser.<br>- Please try updating or use a different browser.- </canvas>+ <iframe id="game-iframe" tabindex="2"></iframe></div><div id="tab-status" style="display: none;"><div id="status-progress" style="display: none;" oncontextmenu="event.preventDefault();">@@ -447,7 +444,7 @@ window.addEventListener('load', () => {});if (missing.length) {// Display error dialog as threading support is required for the editor.- document.getElementById('startButton').disabled = 'disabled';+ document.getElementById('btn-start-editor').disabled = 'disabled';document.getElementById('welcome-modal-description').style.display = 'none';document.getElementById('welcome-modal-missing-description').style.display = 'block';document.getElementById('welcome-modal-dismiss').style.display = 'none';@@ -465,8 +462,9 @@ window.addEventListener('load', () => {}});-function closeWelcomeModal(dontShowAgain) { // eslint-disable-line no-unused-vars+function closeWelcomeModal() { // eslint-disable-line no-unused-varsdocument.getElementById('welcome-modal').style.display = 'none';+ const dontShowAgain = document.getElementById('welcome-modal-dont-show-again-check').checked;if (dontShowAgain) {localStorage.setItem('welcomeModalDismissed', 'true');}@@ -474,10 +472,7 @@ function closeWelcomeModal(dontShowAgain) { // eslint-disable-line no-unused-var</script><script src="godot.editor.js"></script><script>-let editor = null;-let game = null;-let setStatusMode;-let setStatusNotice;+const fileSizes = ___GODOT_EDITOR_FILESIZES___;function clearPersistence() { // eslint-disable-line no-unused-varsfunction deleteDB(path) {@@ -500,116 +495,24 @@ function clearPersistence() { // eslint-disable-line no-unused-varsPromise.all([deleteDB('/home/web_user'),]).then(function (results) {+ localStorage.setItem('welcomeModalDismissed', 'false');alert('Done.');}).catch(function (err) {alert('Error deleting local files. Please retry after reloading the page.');});}-const tabs = [- document.getElementById('tab-loader'),- document.getElementById('tab-editor'),- document.getElementById('tab-game'),-];-function showTab(name) {- tabs.forEach(function (elem) {- if (elem.id === `tab-${name}`) {- elem.style.display = 'block';- if (name === 'editor' || name === 'game') {- const canvas = document.getElementById(`${name}-canvas`);- canvas.focus();- }- } else {- elem.style.display = 'none';- }- });-}--function setButtonEnabled(id, enabled) {- if (enabled) {- document.getElementById(id).disabled = '';- } else {- document.getElementById(id).disabled = 'disabled';- }-}--function setLoaderEnabled(enabled) {- setButtonEnabled('btn-tab-loader', enabled);- setButtonEnabled('btn-tab-editor', !enabled);- setButtonEnabled('btn-close-editor', !enabled);-}--function setGameTabEnabled(enabled) {- setButtonEnabled('btn-tab-game', enabled);- setButtonEnabled('btn-close-game', enabled);-}--function closeGame() {- if (game) {- game.requestQuit();- }-}--function closeEditor() { // eslint-disable-line no-unused-vars- closeGame();- if (editor) {- editor.requestQuit();- }-}--function startEditor(zip) {+function LoadStatus(document) {const INDETERMINATE_STATUS_STEP_MS = 100;- const persistentPaths = ['/home/web_user'];-- let editorCanvas = document.getElementById('editor-canvas');- let gameCanvas = document.getElementById('game-canvas');+ const statusTab = document.getElementById('tab-status');const statusProgress = document.getElementById('status-progress');const statusProgressInner = document.getElementById('status-progress-inner');const statusIndeterminate = document.getElementById('status-indeterminate');const statusNotice = document.getElementById('status-notice');- const headerDiv = document.getElementById('tabs-buttons');- let initializing = true;let statusMode = 'hidden';- showTab('status');-let animationCallbacks = [];- function animate(time) {- animationCallbacks.forEach((callback) => callback(time));- requestAnimationFrame(animate);- }- requestAnimationFrame(animate);-- let lastScale = 0;- let lastWidth = 0;- let lastHeight = 0;- function adjustCanvasDimensions() {- const scale = window.devicePixelRatio || 1;- const headerHeight = headerDiv.offsetHeight + 1;- const width = window.innerWidth;- const height = window.innerHeight - headerHeight;- if (lastScale !== scale || lastWidth !== width || lastHeight !== height) {- editorCanvas.width = width * scale;- editorCanvas.height = height * scale;- editorCanvas.style.width = `${width}px`;- editorCanvas.style.height = `${height}px`;- lastScale = scale;- lastWidth = width;- lastHeight = height;- }- }- animationCallbacks.push(adjustCanvasDimensions);- adjustCanvasDimensions();-- function replaceCanvas(from) {- const out = document.createElement('canvas');- out.id = from.id;- out.tabIndex = from.tabIndex;- from.parentNode.replaceChild(out, from);- lastScale = 0;- return out;- }function animateStatusIndeterminate(ms) {const i = Math.floor((ms / INDETERMINATE_STATUS_STEP_MS) % 8);@@ -621,8 +524,8 @@ function startEditor(zip) {}}- setStatusMode = function (mode) {- if (statusMode === mode || !initializing) {+ function setStatusMode(mode) {+ if (statusMode === mode) {return;}[statusProgress, statusIndeterminate, statusNotice].forEach((elem) => {@@ -631,6 +534,7 @@ function startEditor(zip) {animationCallbacks = animationCallbacks.filter(function (value) {return (value !== animateStatusIndeterminate);});+ let display = '';switch (mode) {case 'progress':statusProgress.style.display = 'block';@@ -643,14 +547,16 @@ function startEditor(zip) {statusNotice.style.display = 'block';break;case 'hidden':+ display = 'none';break;default:throw new Error('Invalid status mode');}+ statusTab.style.display = display;statusMode = mode;};- setStatusNotice = function (text) {+ function setStatusNotice(text) {while (statusNotice.lastChild) {statusNotice.removeChild(statusNotice.lastChild);}@@ -661,104 +567,23 @@ function startEditor(zip) {});};- const gameConfig = {- 'persistentPaths': persistentPaths,- 'unloadAfterInit': false,- 'canvas': gameCanvas,- 'canvasResizePolicy': 1,- 'debugPort': null,- 'onExit': function () {- gameCanvas = replaceCanvas(gameCanvas);- setGameTabEnabled(false);- showTab('editor');- game = null;- },- };-- let OnEditorExit = function () {- showTab('loader');- setLoaderEnabled(true);- };- function Execute(args) {- const is_editor = args.filter(function (v) {- return v === '--editor' || v === '-e';- }).length !== 0;- const is_project_manager = args.filter(function (v) {- return v === '--project-manager';- }).length !== 0;- const is_game = !is_editor && !is_project_manager;-- if (is_game) {- if (game) {- console.error('A game is already running. Close it first');- return;- }- setGameTabEnabled(true);- const channel = new MessageChannel();- gameConfig['debugPort'] = channel.port1;- game = new Engine(gameConfig);- showTab('game');- game.init().then(function () {- requestAnimationFrame(function () {- game.start({ 'args': args, 'canvas': gameCanvas }).then(function () {- editor.addDebuggerSession(channel.port2);- gameCanvas.focus();- });- });- });- } else { // New editor instances will be run in the same canvas. We want to wait for it to exit.- OnEditorExit = function (code) {- setLoaderEnabled(true);- setTimeout(function () {- editor.init().then(function () {- setLoaderEnabled(false);- OnEditorExit = function () {- showTab('loader');- setLoaderEnabled(true);- };- editor.start({ 'args': args, 'persistentDrops': is_project_manager, 'canvas': editorCanvas });- });- }, 0);- OnEditorExit = null;- };+ function updateProgress(active, progress) {+ if (statusMode === 'notice') {+ return;+ }+ if (active) {+ setStatusMode('hidden');+ return;+ }+ const [current, total] = progress;+ if (total > 0 && current < total) {+ statusProgressInner.style.width = `${(current / total) * 100}%`;+ setStatusMode('progress');+ } else {+ setStatusMode('indeterminate');}}- const clamp = (value, min, max) => Math.min(Math.max(value, min), max);- // We need at least 6 free threads from the pool to start the editor.- // At least 4 more will be reserved for the godot thread pool (3 is the bare minimum with the multithreaded variant of the servers).- const concurrency = clamp(navigator.hardwareConcurrency ?? 1, 12, 24);- const editorConfig = {- 'unloadAfterInit': false,- 'onProgress': function progressFunction(current, total) {- if (total > 0) {- statusProgressInner.style.width = `${(current / total) * 100}%`;- setStatusMode('progress');- if (current === total) {- // wait for progress bar animation- setTimeout(() => {- setStatusMode('indeterminate');- }, 100);- }- } else {- setStatusMode('indeterminate');- }- },- 'canvas': editorCanvas,- 'canvasResizePolicy': 0,- 'onExit': function () {- editorCanvas = replaceCanvas(editorCanvas);- if (OnEditorExit) {- OnEditorExit();- }- },- 'onExecute': Execute,- 'persistentPaths': persistentPaths,- 'emscriptenPoolSize': concurrency,- 'godotPoolSize': Math.floor(concurrency / 3), // Ensures at least 4 threads for the pool (see above).- };- editor = new Engine(editorConfig);-function displayFailureNotice(err) {console.error(err);if (err instanceof Error) {@@ -769,69 +594,308 @@ function startEditor(zip) {setStatusNotice('An unknown error occurred.');}setStatusMode('notice');- initializing = false;}- if (!Engine.isWebGLAvailable()) {- displayFailureNotice('WebGL not available');- } else {- setStatusMode('indeterminate');- editor.init('godot.editor').then(function () {- if (zip) {- editor.copyToFS('/tmp/preload.zip', zip);- }- try {- // Avoid user creating project in the persistent root folder.- editor.copyToFS('/home/web_user/keep', new Uint8Array());- } catch (e) {- // File exists+ setStatusMode('indeterminate');+ let updating = false;+ return {+ update: (instance) => {+ if (updating) {+ return;}- showTab('editor');- setLoaderEnabled(false);- const args = ['--project-manager', '--single-window'];- editor.start({ 'args': args, 'persistentDrops': true }).then(function () {- setStatusMode('hidden');- initializing = false;+ updating = true;+ requestAnimationFrame(() => {+ updating = false;+ updateProgress(instance.active, instance.progress);});- }).catch(displayFailureNotice);- }-}+ },+ animate: (time) => {+ animationCallbacks.forEach((callback) => callback(time));+ },+ setNotice: setStatusNotice,+ setError: displayFailureNotice,+ };+};-function preloadZip(target) {- return new Promise(function (resolve, reject) {- if (target.files.length > 0) {- target.files[0].arrayBuffer().then(function (data) {- resolve(data);+(function () {+ // EngineIFrame initialization (+ custom state).+ const iframeStyle = document.getElementById('web-editor-style');+ const tabStatus = document.getElementById('tab-status');+ const WrappedIFrame = (name) => {+ let loadStatus = null;+ const tab = document.getElementById(`tab-${name}`);+ const iframeOnload = (iframe) => {+ const title = iframe.contentDocument.getElementsByTagName('title')[0];+ const observer = new MutationObserver(function () {+ if (tab.style.display === 'none') {+ return;+ }+ document.title = iframe.contentDocument.title;});+ observer.observe(title, { childList: true });+ iframe.contentDocument.body.appendChild(iframe.contentDocument.importNode(iframeStyle, true));+ iframe.contentDocument.body.appendChild(iframe.contentDocument.importNode(tabStatus, true));+ loadStatus = new LoadStatus(iframe.contentDocument);+ };+ const state = new EngineIFrame('godot.editor', document.getElementById(`${name}-iframe`), iframeOnload);+ state.progress = [0, 0];+ state.active = false;+ state.updateStatus = function () {+ loadStatus.update(state);+ };+ state.setError = function (error) {+ loadStatus.setError(error);+ };+ const reset = state.reset;+ state.reset = function () {+ state.progress = [0, 0];+ state.active = false;+ return reset.apply(state);+ };+ state.animate = function (time) {+ if (loadStatus) {+ loadStatus.animate(time);+ }+ };+ return state;+ };+ const editorState = WrappedIFrame('editor');+ const gameState = WrappedIFrame('game');++ // IFrame size adjuster.+ const headerDiv = document.getElementById('tabs-buttons');+ let lastWidth = 0;+ let lastHeight = 0;+ const instances = [editorState, gameState];+ function adjustIframeDimensions(time) {+ const headerHeight = headerDiv.offsetHeight + 1;+ const width = window.innerWidth;+ const height = window.innerHeight - headerHeight;+ if (lastWidth !== width || lastHeight !== height) {+ instances.forEach((e) => {+ e.iframe.style.width = `${width}px`;+ e.iframe.style.height = `${height}px`;+ });+ lastWidth = width;+ lastHeight = height;+ }+ editorState.animate(time);+ gameState.animate(time);+ requestAnimationFrame(adjustIframeDimensions);+ }+ requestAnimationFrame(adjustIframeDimensions);++ // Tabs+ const tabs = [+ document.getElementById('tab-loader'),+ document.getElementById('tab-editor'),+ document.getElementById('tab-game'),+ ];+ const loaderTitle = document.title;+ function showTab(name) {+ tabs.forEach(function (elem) {+ elem.style.display = elem.id === `tab-${name}` ? 'block' : 'none';+ });+ if (name === 'editor' || name === 'game') {+ const instance = name === 'game' ? gameState : editorState;+ const title = instance.iframe.contentDocument.title;+ if (title) {+ document.title = title;+ }+ instance.focus();} else {- resolve();+ document.title = loaderTitle;}- });-}--document.getElementById('startButton').onclick = function () {- preloadZip(document.getElementById('zip-file')).then(function (zip) {- startEditor(zip);- });-};+ }+ function setButtonEnabled(id, enabled) {+ document.getElementById(id).disabled = enabled ? '' : 'disabled';+ }+ function setEditorTabEnabled(enabled) {+ setButtonEnabled('btn-tab-editor', enabled);+ setButtonEnabled('btn-close-editor', enabled);+ }+ function setGameTabEnabled(enabled) {+ setButtonEnabled('btn-tab-game', enabled);+ setButtonEnabled('btn-close-game', enabled);+ }-const zipFile = document.getElementById('zip-file');-const zipDemo = document.getElementById('zip-demo');+ // EngineIFrame Controllers.+ const persistentPaths = ['/home/web_user'];-function updateZipDemoDisplay() {- console.log(zipFile.files.length);- // Hide "Try this for example" link when a file is selected.- zipDemo.style.display = zipFile.files.length === 0 ? 'inline' : 'none';-}+ function runGame(gameConfig) {+ let session = null;+ const onmessage = (event) => {+ const cmd = event.data.cmd || '';+ if (event.data.pid !== gameState.pid) {+ console.error('Invalid event', event);+ return;+ }+ if (cmd == 'started') {+ gameState.active = true;+ gameState.updateStatus();+ editorState.addSession(session);+ showTab('game');+ } else if (cmd == 'error') {+ gameState.setError(event.data.msg);+ } else if (cmd == 'progress') {+ const [current, total] = event.data.msg;+ gameState.progress = [current, total];+ gameState.updateStatus();+ } else if (cmd == 'stopped') {+ gameState.reset();+ setGameTabEnabled(false);+ showTab('editor');+ } else if (cmd == 'execute' || cmd == 'terminate') {+ // Not implemented.+ } else {+ console.error('Invalid event', event);+ }+ };+ gameState.reset().then(() => {+ session = gameState.start(gameConfig, onmessage, true);+ });+ setGameTabEnabled(true);+ showTab('game');+ }-zipFile.addEventListener('change', (_pEvent) => {- updateZipDemoDisplay();-});+ function runEditor(editorArgs) {+ let OnEditorExit = function () {+ gameState.reset();+ setGameTabEnabled(false);+ editorState.reset();+ setEditorTabEnabled(false);+ showTab('loader');+ };+ const onmessage = (event) => {+ const cmd = event.data.cmd || '';+ if (editorState.pid !== event.data.pid) {+ console.error('Invalid event', event);+ return;+ }+ if (cmd == 'started') {+ editorState.active = true;+ editorState.updateStatus();+ showTab('editor');+ } else if (cmd == 'error') {+ editorState.setError(event.data.msg);+ } else if (cmd == 'stopped') {+ OnEditorExit();+ } else if (cmd == 'progress') {+ const [current, total] = event.data.msg;+ editorState.progress = [current, total];+ editorState.updateStatus();+ } else if (cmd == 'execute') {+ const [newPid, path, args] = event.data.msg; // eslint-disable-line no-unused-vars+ if (!Array.isArray(args)) {+ console.error('Invalid event', event);+ return;+ }+ const is_editor = args.findIndex((v) => (v === '--editor' || v === '-e')) !== -1;+ const is_project_manager = args.findIndex((v) => v === '--project-manager') !== -1;+ const is_game = !is_editor && !is_project_manager;+ if (is_game) {+ const gameConfig = {+ 'pid': newPid,+ 'persistentPaths': persistentPaths,+ 'unloadAfterInit': true,+ 'canvasResizePolicy': 2,+ 'fileSizes': fileSizes,+ 'args': args,+ };+ runGame(gameConfig);+ } else {+ OnEditorExit = function () {+ runEditor(args);+ };+ }+ } else if (cmd == 'terminate') {+ if (gameState.pid !== event.data.msg) {+ return;+ }+ gameState.reset();+ setGameTabEnabled(false);+ showTab('editor');+ } else {+ console.error('Invalid event', event);+ }+ };+ const clamp = (value, min, max) => Math.min(Math.max(value, min), max);+ // We need at least 6 free threads from the pool to start the editor.+ // At least 4 more will be reserved for the godot thread pool (3 is the bare minimum with the multithreaded variant of the servers).+ const concurrency = clamp(navigator.hardwareConcurrency ?? 1, 12, 24);+ const pid = Math.round(Math.random() * (1 << 30));+ const editorConfig = {+ 'pid': pid,+ 'persistentPaths': persistentPaths,+ 'unloadAfterInit': true,+ 'canvasResizePolicy': 2,+ 'args': editorArgs,+ 'emscriptenPoolSize': concurrency,+ 'persistentDrops': editorArgs.findIndex((v) => v == '--project-manager') !== -1,+ 'godotPoolSize': Math.floor(concurrency / 3), // Ensures at least 4 threads for the pool (see above).+ 'fileSizes': fileSizes,+ };+ gameState.reset();+ setGameTabEnabled(false);+ editorState.reset().then(() => editorState.start(editorConfig, onmessage));+ setEditorTabEnabled(true);+ showTab('editor');+ }-document.addEventListener('DOMContentLoaded', () => {- // Handle situation when a file is already specified on load (e.g. after a page refresh).- updateZipDemoDisplay();-});+ // Button handlers+ const buttons = {+ start: document.getElementById('btn-start-editor'),+ editorShow: document.getElementById('btn-tab-editor'),+ editorClose: document.getElementById('btn-close-editor'),+ gameShow: document.getElementById('btn-tab-game'),+ gameClose: document.getElementById('btn-close-game'),+ };+ buttons.start.onclick = function () {+ if (!Engine.isWebGLAvailable()) {+ setEditorTabEnabled(true);+ showTab('editor');+ editorState.setError('WebGL not available');+ } else {+ runEditor(['--project-manager', '--single-window']);+ }+ };+ function closeGame(force) {+ if (force && !window.confirm('Are you sure you want to forcibly close the game?')) {+ return;+ }+ if (gameState.active && !force) {+ showTab('game');+ gameState.stop();+ } else {+ gameState.reset();+ setGameTabEnabled(false);+ if (editorState.active) {+ showTab('editor');+ }+ }+ }+ function closeEditor(force) {+ if (force && !window.confirm('Are you sure you want to forcibly close the editor?\nClicking "OK" may result in data loss!')) {+ return;+ }+ if (editorState.active && !force) {+ showTab('editor');+ editorState.stop();+ } else {+ gameState.reset();+ setGameTabEnabled(false);+ editorState.reset();+ setEditorTabEnabled(false);+ showTab('loader');+ }+ }+ buttons.editorShow.onclick = () => showTab('editor');+ buttons.editorClose.onclick = () => closeEditor(false);+ buttons.editorClose.ondblclick = () => closeEditor(true);+ buttons.gameShow.onclick = () => showTab('game');+ buttons.gameClose.onclick = () => closeGame(false);+ buttons.gameClose.ondblclick = () => closeGame(true);+})();</script></body></html>diff --git a/platform/web/SCsub b/platform/web/SCsubindex 82cf164280e..0798c4f26f4 100644--- a/platform/web/SCsub+++ b/platform/web/SCsub@@ -117,6 +117,9 @@ engine = ["js/engine/config.js","js/engine/engine.js",]+if env["target"] == "editor":+ engine += ["js/engine/iframe.js"]+externs = [env.File("#platform/web/js/engine/engine.externs.js")]js_engine = env.CreateEngineFile("#bin/godot${PROGSUFFIX}.engine.js", engine, externs, env["threads"])env.Depends(js_engine, externs)diff --git a/platform/web/emscripten_helpers.py b/platform/web/emscripten_helpers.pyindex 612e3e87bb4..ebd800c1182 100644--- a/platform/web/emscripten_helpers.py+++ b/platform/web/emscripten_helpers.py@@ -56,6 +56,7 @@ def create_template_zip(env, js, wasm, side):html = "#misc/dist/html/editor.html"cache = ["godot.editor.html",+ "godot.editor.iframe.html","offline.html","godot.editor.js","godot.editor.audio.worklet.js",@@ -66,6 +67,8 @@ def create_template_zip(env, js, wasm, side):"inter-bold.woff2",]opt_cache = ["godot.editor.wasm"]++ env["GODOT_WEB_FILESIZES"] = lambda: json.dumps({"godot.editor.wasm": wasm.get_size()})subst_dict = {"___GODOT_VERSION___": get_build_version(False),"___GODOT_NAME___": "GodotEngine",@@ -75,9 +78,18 @@ def create_template_zip(env, js, wasm, side):"___GODOT_THREADS_ENABLED___": "true" if env["threads"] else "false","___GODOT_ENSURE_CROSSORIGIN_ISOLATION_HEADERS___": "true",}- html = env.Substfile(target="#bin/godot${PROGSUFFIX}.html", source=html, SUBST_DICT=subst_dict)+ subst_dict_sizes = dict(subst_dict)+ subst_dict_sizes["___GODOT_EDITOR_FILESIZES___"] = "${GODOT_WEB_FILESIZES()}"+ html = env.Substfile(target="#bin/godot${PROGSUFFIX}.html", source=html, SUBST_DICT=subst_dict_sizes)+ env.Depends(html, wasm)in_files.append(html)out_files.append(zip_dir.File(binary_name + ".html"))+ iframe = env.Textfile(+ target="#bin/godot${PROGSUFFIX}.iframe.html",+ source='<!DOCTYPE html>\n<html lang="en"><head><title></title></head></html>',+ )+ in_files.append(iframe)+ out_files.append(zip_dir.File(binary_name + ".iframe.html"))# And logo/faviconin_files.append("#misc/dist/html/logo.svg")out_files.append(zip_dir.File("logo.svg"))diff --git a/platform/web/eslint.config.cjs b/platform/web/eslint.config.cjsindex a5cb8fc6698..3a97b3ac02e 100644--- a/platform/web/eslint.config.cjs+++ b/platform/web/eslint.config.cjs@@ -189,10 +189,12 @@ module.exports = [globals: {...globals.browser,'Engine': true,+ 'EngineIFrame': true,'$GODOT_CONFIG': true,'$GODOT_PROJECT_NAME': true,'$GODOT_THREADS_ENABLED': true,'___GODOT_THREADS_ENABLED___': true,+ '___GODOT_EDITOR_FILESIZES___': true,},},rules: {diff --git a/platform/web/js/engine/engine.js b/platform/web/js/engine/engine.jsindex 59be0d0c418..00dd5c9241e 100644--- a/platform/web/js/engine/engine.js+++ b/platform/web/js/engine/engine.js@@ -238,7 +238,7 @@ const Engine = (function () {/*** Forcibly exit the current instance.*- * This is akin terminating the process on a regular OS, and *may* be able to terminate+ * This is akin to terminating the process on a regular OS, and *may* be able to terminate* the engine and free resources even if the engine has crashed, or is stuck in a loop.**/diff --git a/platform/web/js/engine/iframe.js b/platform/web/js/engine/iframe.jsnew file mode 100644index 00000000000..a3b9cabef7a--- /dev/null+++ b/platform/web/js/engine/iframe.js@@ -0,0 +1,290 @@+const EngineIFrame = (function () {+ const iframeScript = function (name) {+ // IMPORTANT!!! This function will run in the iframe document, not in the current context.+ const canvas = document.getElementById('godot-canvas');+ let engine = null;+ let iframe_port = null;+ let pid = 0;+ let terminating = false;+ function handleMessage(event) {+ /**+ * @param {string} cmd+ * @param {*=} msg+ * @ignore+ */+ function notify(cmd, msg) {+ if (!iframe_port) {+ return;+ }+ iframe_port.postMessage({ 'cmd': cmd, 'msg': msg, 'pid': pid });+ }+ const cmd = event.data['cmd'] || '';+ const msg = event.data['msg'] || '';+ if (cmd == 'start') {+ if (engine) {+ throw new Error('An instance is already running.');+ }+ if (event.ports.length < 1) {+ throw new Error('Not enough message ports received, cannot start.');+ }+ iframe_port = event.ports[0];+ const config = {+ ...msg,+ 'debugPort': event.ports[1] ?? null,+ 'canvas': canvas,+ 'onProgress': (current, total) => {+ notify('progress', [current, total]);+ },+ 'onExecute': (path, args) => {+ const newPid = Math.round(Math.random() * (1 << 30));+ notify('execute', [newPid, path, args]);+ return newPid;+ },+ 'onTerminatePID': (p_pid) => {+ notify('terminate', p_pid);+ },+ 'onPrint': function () {+ if (terminating) {+ return; // Don't spam console on force quit.+ }+ console.log.apply(console, Array.from(arguments)); // eslint-disable-line no-console+ },+ 'onPrintError': function () {+ if (terminating) {+ return; // Don't spam console on force quit.+ }+ console.error.apply(console, Array.from(arguments)); // eslint-disable-line no-console+ },+ 'onExit': () => {+ engine = null;+ notify('stopped');+ pid = 0;+ },+ };+ pid = config['pid'] ?? 0;+ engine = new Engine(config); // eslint-disable-line no-undef+ engine.init(name).then(function () {+ try {+ // Prevents editor from creating projects in the home folder.+ engine.copyToFS('/home/web_user/keep', new Uint8Array(0));+ } catch (e) {+ // File exists+ }+ requestAnimationFrame(function () {+ engine.start().then(function () {+ notify('started');+ });+ });+ }).catch(function (e) {+ notify('error', `Init error. ${e}`);+ });+ } else if (cmd == 'session') {+ if (event.ports.length < 1) {+ throw new Error('Not enough message ports received, cannot start.');+ }+ engine.addDebuggerSession(event.ports[0]);+ } else if (cmd == 'stop') {+ if (!engine) {+ return;+ }+ engine.requestQuit();+ } else if (cmd == 'focus') {+ canvas.focus();+ } else {+ console.error('Invalid event', event); // eslint-disable-line no-console+ }+ }+ const loadPromise = new Promise((accept, reject) => {+ const s = document.createElement('script');+ s.src = `${name}.js`;+ s.onload = () => {+ accept();+ };+ document.body.appendChild(s);+ });+ // We shouldn't need this, but Firefox seems to struggle GCing if the iframe is removed and the engine is running.+ window.onpagehide = (event) => {+ if (engine) {+ terminating = true;+ try {+ engine.forceQuit();+ } catch (e) { /* */ }+ engine = null;+ }+ };+ window.onmessage = (event) => {+ loadPromise.then(() => {+ handleMessage(event);+ });+ };+ };++ function replaceIframe(name, from, onload) {+ const iframe = document.importNode(from);+ iframe.src = `${name}.iframe.html`;+ let resolve = null;+ iframe.onload = () => {+ const canvas = iframe.contentDocument.createElement('canvas');+ canvas.id = 'godot-canvas';+ iframe.contentDocument.body.appendChild(canvas);+ // Add load/start script+ const source = `const start = ${iframeScript.toString()}; start(${JSON.stringify(name)});`;+ const script = iframe.contentDocument.createElement('script');+ script.type = 'application/javascript';+ script.appendChild(iframe.contentDocument.createTextNode(source));+ iframe.contentDocument.body.appendChild(script);+ if (onload) {+ onload(iframe);+ }+ resolve();+ };+ return [+ iframe,+ new Promise((accept, reject) => {+ resolve = accept;+ from.parentNode.replaceChild(iframe, from);+ }),+ ];+ }++ /**+ * @classdesc The ``EngineIFrame`` class provides methods for embedding an ``Engine`` instance into an iframe.+ *+ * .. attention::+ *+ * This class is experimental and may change in future versions.+ *+ * @description Creates a new EngineIFrame instance with the given configuration.+ * @global+ * @constructor+ * @param {string} name The name to pass to Engine.init.+ * @param {HTMLIFrameElement} iframe The iframe element to replace.+ * @param {function(HTMLIFrameElement)=} onload A function to run when the iframe loads (e.g. to add CSS rules).+ * @return {EngineIFrame}+ */+ function EngineIFrame(name, iframe, onload) {} // eslint-disable-line no-unused-vars, no-shadow+ const proto = (replace) => (/** @lends EngineIFrame.prototype */{+ /**+ * The currently bound iframe.+ * @export+ * @type {HTMLIFrameElement}+ */+ iframe: null,+ /**+ * The Process ID of the current instance.+ * @export+ * @type {number}+ */+ pid: 0,+ /**+ * @ignore+ * @export+ * @type {?MessageChannel}+ */+ iframeChannel: null,+ /**+ * @ignore+ * @export+ * @type {?Array<MessagePort>}+ */+ debugPorts: null,+ /**+ * Starts a new instance with the given config.+ * @export+ * @param {EngineConfig} config The engine configuration to use when starting the engine. Function arguments are not permitted here.+ * @param {function(Event)} onmessage The onmessage callback where to listen for iframe messages.+ * @param {boolean=} debug If the instance should be started with a debug port.+ */+ start: function (config, onmessage, debug) {+ this.pid = config.pid ?? Math.round(Math.random() * (1 << 30));+ this.iframeChannel = new MessageChannel();+ this.iframeChannel.port1.onmessage = onmessage;+ const transfers = [this.iframeChannel.port2];+ let debugSession = null;+ if (debug) {+ // Proxy the debug port so we want can send a 'close'+ // message even if we forcibly terminate the iframe.+ const debugChannel = new MessageChannel();+ const debugProxy = new MessageChannel();+ debugChannel.port1.onmessage = (evt) => debugProxy.port1.postMessage(evt.data);+ debugProxy.port1.onmessage = (evt) => debugChannel.port1.postMessage(evt.data);+ debugSession = debugProxy.port2;+ this.debugPorts = [debugProxy.port1, debugChannel.port1];+ transfers.push(debugChannel.port2);+ }+ this.iframe.contentWindow.postMessage({ 'cmd': 'start', 'msg': config }, this.iframe.contentWindow.origin, transfers);+ return debugSession;+ },+ /**+ * Sends a stop request to the currently running instance.+ * @export+ */+ stop: function () {+ this.iframe.contentWindow.postMessage({ 'cmd': 'stop' });+ },+ /**+ * Focus the wrapped iframe and notify the instance.+ * @export+ */+ focus: function () {+ this.iframe.focus();+ this.iframe.contentWindow.postMessage({ 'cmd': 'focus' });+ },+ /**+ * Transfers a debug session to attach to the editor debugger (this must be an editor instance).+ * @export+ * @param {MessagePort} port The MessagePort to connect to the editor debugger.+ */+ addSession: function (port) {+ this.iframe.contentWindow.postMessage({ 'cmd': 'session', 'msg': 0 }, this.iframe.contentWindow.origin, [port]);+ },+ /**+ * Forcibly resets the state by replacing the iframe with a new one.+ * @export+ * @return {Promise} A Promise upon which fullfillment a new instance can be started.+ */+ reset: function () {+ if (this.iframeChannel) {+ this.iframeChannel.port1.close();+ this.iframeChannel.port1.onmessage = null;+ }+ if (this.debugPorts) {+ this.debugPorts.forEach((port) => {+ // Make sure we always send a 'stop' message even if we+ // forcibly terminate the game.+ port.postMessage('stop');+ port.close();+ port.onmessage = null;+ });+ };+ const [next, promise] = replace(this.iframe);+ this.iframe = next;+ this.pid = 0;+ this.iframeChannel = null;+ this.debugPorts = null;+ return promise;+ },+ });+ /**+ * @ignore+ * @constructor+ * @return {EngineIFrame}+ */+ function SafeIframe(name, reference, onload) {+ /**+ * @ignore+ * @constructor+ * @return {EngineIFrame}+ */+ function EngineIFrame(iframe) { // eslint-disable-line no-shadow+ this.iframe = iframe;+ this.reset();+ }+ EngineIFrame.prototype = proto((iframe) => replaceIframe(name, iframe, onload));+ return new EngineIFrame(reference);+ };+ return SafeIframe;+})();+if (typeof window !== 'undefined') {+ window['EngineIFrame'] = EngineIFrame;+}