summaryrefslogtreecommitdiff
path: root/app.html
blob: 078d27dd03d5d1cb79d292169ee4bda35f7a48af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0">
    <title>$GODOT_PROJECT_NAME</title>
    <style>
    html, body, #canvas {
      margin: 0;
      padding: 0;
      border: 0;
    }

    body {
      color: white;
      background-color: black;
      overflow: hidden;
      touch-action: none;
    }

    #canvas {
      display: block;
    }

    #canvas:focus {
      outline: none;
    }

    #status, #status-splash, #status-progress {
      position: absolute;
      left: 0;
      right: 0;
    }

    #status, #status-splash {
      top: 0;
      bottom: 0;
    }

    #status {
      background-color: #242424;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      visibility: hidden;
    }

    #status-splash {
      max-height: 100%;
      max-width: 100%;
      margin: auto;
    }

    #status-progress, #status-notice {
      display: none;
    }

    #status-progress {
      bottom: 10%;
      width: 50%;
      margin: 0 auto;
    }

    #status-notice {
      background-color: #5b3943;
      border-radius: 0.5rem;
      border: 1px solid #9b3943;
      color: #e0e0e0;
      font-family: 'Noto Sans', 'Droid Sans', Arial, sans-serif;
      line-height: 1.3;
      margin: 0 2rem;
      overflow: hidden;
      padding: 1rem;
      text-align: center;
      z-index: 1;
    }
    </style>
    $GODOT_HEAD_INCLUDE
    <script>
    var godotCallbacks = {
      dataLoaded: null
    }

    function openLoadDialog() {
      var input = document.createElement('input');
      input.setAttribute('type', 'file');
      input.setAttribute('accept', '.json');
      input.click();

      input.addEventListener('change', (event) => {
        var file = event.target.files[0];
        var reader = new FileReader();

        reader.readAsText(file);

        reader.onloadend = () => {
          if (godotCallbacks.dataLoaded) {
            godotCallbacks.dataLoaded(reader.result);
          }
        }
      });
    }
    </script>
  </head>
  <body>
    <canvas id="canvas">
      Your browser does not support the canvas tag.
    </canvas>

    <noscript>
      Your browser does not support JavaScript.
    </noscript>

    <div id="status">
      <img id="status-splash" src="$GODOT_SPLASH" alt="">
      <progress id="status-progress"></progress>
      <div id="status-notice"></div>
    </div>

    <script src="$GODOT_URL"></script>
    <script>
    const GODOT_CONFIG = $GODOT_CONFIG;
    // const GODOT_THREADS_ENABLED = $GODOT_THREADS_ENABLED;
    const GODOT_THREADS_ENABLED = 'yes';
    const engine = new Engine(GODOT_CONFIG);

    (function () {
      const statusOverlay = document.getElementById('status');
      const statusProgress = document.getElementById('status-progress');
      const statusNotice = document.getElementById('status-notice');

      let initializing = true;
      let statusMode = '';

      function setStatusMode(mode) {
        if (statusMode === mode || !initializing) {
          return;
        }
        if (mode === 'hidden') {
          statusOverlay.remove();
          initializing = false;
          return;
        }
        statusOverlay.style.visibility = 'visible';
        statusProgress.style.display = mode === 'progress' ? 'block' : 'none';
        statusNotice.style.display = mode === 'notice' ? 'block' : 'none';
        statusMode = mode;
      }

      function setStatusNotice(text) {
        while (statusNotice.lastChild) {
          statusNotice.removeChild(statusNotice.lastChild);
        }
        const lines = text.split('\n');
        lines.forEach((line) => {
          statusNotice.appendChild(document.createTextNode(line));
          statusNotice.appendChild(document.createElement('br'));
        });
      }

      function displayFailureNotice(err) {
        const msg = err.message || err;
        console.error(msg);
        setStatusNotice(msg);
        setStatusMode('notice');
        initializing = false;
      }

      const missing = Engine.getMissingFeatures({
        threads: GODOT_THREADS_ENABLED,
      });

      if (missing.length !== 0) {
        if (GODOT_CONFIG['serviceWorker'] && GODOT_CONFIG['ensureCrossOriginIsolationHeaders'] && 'serviceWorker' in navigator) {
          // There's a chance that installing the service worker would fix the issue
          Promise.race([
            navigator.serviceWorker.getRegistration().then((registration) => {
              if (registration != null) {
                return Promise.reject(new Error('Service worker already exists.'));
              }
              return registration;
            }).then(() => engine.installServiceWorker()),
            // For some reason, `getRegistration()` can stall
            new Promise((resolve) => {
              setTimeout(() => resolve(), 2000);
            }),
          ]).catch((err) => {
              console.error('Error while registering service worker:', err);
            }).then(() => {
              window.location.reload();
            });
        } else {
          // Display the message as usual
          const missingMsg = 'Error\nThe following features required to run Godot projects on the Web are missing:\n';
          displayFailureNotice(missingMsg + missing.join('\n'));
        }
      } else {
        setStatusMode('progress');
        engine.startGame({
          'onProgress': function (current, total) {
            if (current > 0 && total > 0) {
              statusProgress.value = current;
              statusProgress.max = total;
            } else {
              statusProgress.removeAttribute('value');
              statusProgress.removeAttribute('max');
            }
          },
        }).then(() => {
            setStatusMode('hidden');
          }, displayFailureNotice);
      }
    }());
    </script>
  </body>
</html>