|
73 | 73 | //---------------------------------------------------------------------------------- |
74 | 74 | typedef struct { |
75 | 75 | GLFWwindow *handle; // GLFW window handle (graphic device) |
| 76 | + bool ourFullscreen; // Internal var to filter our handling of fullscreen vs the user handling of fullscreen |
76 | 77 | } PlatformData; |
77 | 78 |
|
78 | 79 | //---------------------------------------------------------------------------------- |
@@ -140,6 +141,37 @@ bool WindowShouldClose(void) |
140 | 141 | // Toggle fullscreen mode |
141 | 142 | void ToggleFullscreen(void) |
142 | 143 | { |
| 144 | + platform.ourFullscreen = true; |
| 145 | + bool enterFullscreen = false; |
| 146 | + |
| 147 | + const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0); |
| 148 | + if (wasFullscreen) |
| 149 | + { |
| 150 | + EM_ASM(document.exitFullscreen();); |
| 151 | + |
| 152 | + if (CORE.Window.flags & FLAG_FULLSCREEN_MODE) enterFullscreen = false; |
| 153 | + else enterFullscreen = true; |
| 154 | + |
| 155 | + CORE.Window.fullscreen = false; |
| 156 | + CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE; |
| 157 | + CORE.Window.flags &= ~FLAG_BORDERLESS_WINDOWED_MODE; |
| 158 | + } |
| 159 | + else enterFullscreen = true; |
| 160 | + |
| 161 | + if (enterFullscreen) |
| 162 | + { |
| 163 | + // NOTE: The setTimeouts handle the browser mode change delay |
| 164 | + EM_ASM( |
| 165 | + setTimeout(function() |
| 166 | + { |
| 167 | + Module.requestFullscreen(false, false); |
| 168 | + }, 100); |
| 169 | + ); |
| 170 | + CORE.Window.fullscreen = true; |
| 171 | + CORE.Window.flags |= FLAG_FULLSCREEN_MODE; |
| 172 | + } |
| 173 | + |
| 174 | + // NOTE: Old notes below: |
143 | 175 | /* |
144 | 176 | EM_ASM |
145 | 177 | ( |
@@ -204,40 +236,44 @@ void ToggleFullscreen(void) |
204 | 236 | CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE; |
205 | 237 | } |
206 | 238 | */ |
207 | | - |
208 | | - CORE.Window.fullscreen = !CORE.Window.fullscreen; // Toggle fullscreen flag |
209 | 239 | } |
210 | 240 |
|
211 | 241 | // Toggle borderless windowed mode |
212 | 242 | void ToggleBorderlessWindowed(void) |
213 | 243 | { |
| 244 | + platform.ourFullscreen = true; |
| 245 | + bool enterBorderless = false; |
| 246 | + |
214 | 247 | const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0); |
215 | 248 | if (wasFullscreen) |
216 | 249 | { |
217 | 250 | EM_ASM(document.exitFullscreen();); |
218 | 251 |
|
| 252 | + if (CORE.Window.flags & FLAG_BORDERLESS_WINDOWED_MODE) enterBorderless = false; |
| 253 | + else enterBorderless = true; |
| 254 | + |
219 | 255 | CORE.Window.fullscreen = false; |
220 | 256 | CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE; |
| 257 | + CORE.Window.flags &= ~FLAG_BORDERLESS_WINDOWED_MODE; |
221 | 258 | } |
| 259 | + else enterBorderless = true; |
222 | 260 |
|
223 | | - if (!IsWindowState(FLAG_BORDERLESS_WINDOWED_MODE)) |
| 261 | + if (enterBorderless) |
224 | 262 | { |
225 | 263 | // NOTE: 1. The setTimeouts handle the browser mode change delay |
226 | | - // 2. The style unset handles the possibility of a width="100%" like on the default shell.html file |
| 264 | + // 2. The style unset handles the possibility of a width="value%" like on the default shell.html file |
227 | 265 | EM_ASM( |
228 | 266 | setTimeout(function() |
229 | 267 | { |
230 | | - Module.requestFullscreen(true, true); |
| 268 | + Module.requestFullscreen(false, true); |
231 | 269 | setTimeout(function() |
232 | 270 | { |
233 | 271 | canvas.style.width="unset"; |
234 | 272 | }, 100); |
235 | 273 | }, 100); |
236 | 274 | ); |
237 | | - |
238 | 275 | CORE.Window.flags |= FLAG_BORDERLESS_WINDOWED_MODE; |
239 | 276 | } |
240 | | - else CORE.Window.flags &= ~FLAG_BORDERLESS_WINDOWED_MODE; |
241 | 277 | } |
242 | 278 |
|
243 | 279 | // Set window state: maximized, if resizable |
@@ -277,9 +313,9 @@ void SetWindowState(unsigned int flags) |
277 | 313 | } |
278 | 314 |
|
279 | 315 | // State change: FLAG_FULLSCREEN_MODE |
280 | | - if ((flags & FLAG_FULLSCREEN_MODE) > 0) |
| 316 | + if ((CORE.Window.flags & FLAG_FULLSCREEN_MODE) != (flags & FLAG_FULLSCREEN_MODE)) |
281 | 317 | { |
282 | | - TRACELOG(LOG_WARNING, "SetWindowState(FLAG_FULLSCREEN_MODE) not available yet on target platform"); |
| 318 | + ToggleFullscreen(); // NOTE: Window state flag updated inside function |
283 | 319 | } |
284 | 320 |
|
285 | 321 | // State change: FLAG_WINDOW_RESIZABLE |
@@ -384,9 +420,9 @@ void ClearWindowState(unsigned int flags) |
384 | 420 | } |
385 | 421 |
|
386 | 422 | // State change: FLAG_FULLSCREEN_MODE |
387 | | - if ((flags & FLAG_FULLSCREEN_MODE) > 0) |
| 423 | + if (((CORE.Window.flags & FLAG_FULLSCREEN_MODE) > 0) && ((flags & FLAG_FULLSCREEN_MODE) > 0)) |
388 | 424 | { |
389 | | - TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_FULLSCREEN_MODE) not available yet on target platform"); |
| 425 | + ToggleFullscreen(); // NOTE: Window state flag updated inside function |
390 | 426 | } |
391 | 427 |
|
392 | 428 | // State change: FLAG_WINDOW_RESIZABLE |
@@ -1015,6 +1051,9 @@ int InitPlatform(void) |
1015 | 1051 | CORE.Window.display.width = CORE.Window.screen.width; |
1016 | 1052 | CORE.Window.display.height = CORE.Window.screen.height; |
1017 | 1053 |
|
| 1054 | + // Init fullscreen toggle required var: |
| 1055 | + platform.ourFullscreen = false; |
| 1056 | + |
1018 | 1057 | if (CORE.Window.fullscreen) |
1019 | 1058 | { |
1020 | 1059 | // remember center for switchinging from fullscreen to window |
@@ -1148,7 +1187,7 @@ int InitPlatform(void) |
1148 | 1187 | // Initialize input events callbacks |
1149 | 1188 | //---------------------------------------------------------------------------- |
1150 | 1189 | // Setup callback functions for the DOM events |
1151 | | - emscripten_set_fullscreenchange_callback("#canvas", NULL, 1, EmscriptenFullscreenChangeCallback); |
| 1190 | + emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenFullscreenChangeCallback); |
1152 | 1191 |
|
1153 | 1192 | // WARNING: Below resize code was breaking fullscreen mode for sample games and examples, it needs review |
1154 | 1193 | // Check fullscreen change events(note this is done on the window since most browsers don't support this on #canvas) |
@@ -1412,7 +1451,19 @@ static void CursorEnterCallback(GLFWwindow *window, int enter) |
1412 | 1451 | // Register fullscreen change events |
1413 | 1452 | static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *event, void *userData) |
1414 | 1453 | { |
1415 | | - // TODO: Implement EmscriptenFullscreenChangeCallback()? |
| 1454 | + // NOTE: 1. Reset the fullscreen flags if the user left fullscreen manually by pressing the Escape key |
| 1455 | + // 2. Which is a necessary safeguard because that case will bypass the toggles CORE.Window.flags resets |
| 1456 | + if (platform.ourFullscreen) platform.ourFullscreen = false; |
| 1457 | + else |
| 1458 | + { |
| 1459 | + const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0); |
| 1460 | + if (!wasFullscreen) |
| 1461 | + { |
| 1462 | + CORE.Window.fullscreen = false; |
| 1463 | + CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE; |
| 1464 | + CORE.Window.flags &= ~FLAG_BORDERLESS_WINDOWED_MODE; |
| 1465 | + } |
| 1466 | + } |
1416 | 1467 |
|
1417 | 1468 | return 1; // The event was consumed by the callback handler |
1418 | 1469 | } |
|
0 commit comments