mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-17 11:35:08 -04:00
Use a less awful approach to fullscreening webclient that works in IE11 and lets me sleep at night
This commit is contained in:
parent
cae2f15873
commit
5aeca085dc
83
src/Window.c
83
src/Window.c
@ -3047,8 +3047,7 @@ void Window_FreeFramebuffer(struct Bitmap* bmp) {
|
|||||||
#include <emscripten/emscripten.h>
|
#include <emscripten/emscripten.h>
|
||||||
#include <emscripten/html5.h>
|
#include <emscripten/html5.h>
|
||||||
#include <emscripten/key_codes.h>
|
#include <emscripten/key_codes.h>
|
||||||
static cc_bool keyboardOpen, needResize, goingFullscreen;
|
static cc_bool keyboardOpen, needResize;
|
||||||
static int restoreWidth, restoreHeight;
|
|
||||||
|
|
||||||
static int RawDpiScale(int x) { return (int)(x * emscripten_get_device_pixel_ratio()); }
|
static int RawDpiScale(int x) { return (int)(x * emscripten_get_device_pixel_ratio()); }
|
||||||
static int GetCanvasWidth(void) { return EM_ASM_INT_V({ return Module['canvas'].width }); }
|
static int GetCanvasWidth(void) { return EM_ASM_INT_V({ return Module['canvas'].width }); }
|
||||||
@ -3175,10 +3174,18 @@ static EM_BOOL OnFocus(int type, const EmscriptenFocusEvent* ev, void* data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static EM_BOOL OnResize(int type, const EmscriptenUiEvent* ev, void *data) {
|
static EM_BOOL OnResize(int type, const EmscriptenUiEvent* ev, void *data) {
|
||||||
UpdateWindowBounds();
|
UpdateWindowBounds(); needResize = true;
|
||||||
needResize = true;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
/* This is only raised when going into fullscreen */
|
||||||
|
static EM_BOOL OnCanvasResize(int type, const void* reserved, void *data) {
|
||||||
|
UpdateWindowBounds(); needResize = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
static EM_BOOL OnFullscreenChange(int type, const EmscriptenFullscreenChangeEvent* ev, void *data) {
|
||||||
|
UpdateWindowBounds(); needResize = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static const char* OnBeforeUnload(int type, const void* ev, void *data) {
|
static const char* OnBeforeUnload(int type, const void* ev, void *data) {
|
||||||
Window_Close();
|
Window_Close();
|
||||||
@ -3305,7 +3312,6 @@ static EM_BOOL OnKeyPress(int type, const EmscriptenKeyboardEvent* ev, void* dat
|
|||||||
#define EMSCRIPTEN_EVENT_TARGET_WINDOW "#window"
|
#define EMSCRIPTEN_EVENT_TARGET_WINDOW "#window"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static EM_BOOL OnFullscreenChange(int type, const EmscriptenFullscreenChangeEvent* ev, void *data);
|
|
||||||
static void HookEvents(void) {
|
static void HookEvents(void) {
|
||||||
emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 0, OnMouseWheel);
|
emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 0, OnMouseWheel);
|
||||||
emscripten_set_mousedown_callback("#canvas", NULL, 0, OnMouseButton);
|
emscripten_set_mousedown_callback("#canvas", NULL, 0, OnMouseButton);
|
||||||
@ -3466,55 +3472,30 @@ int Window_GetWindowState(void) {
|
|||||||
return status.isFullscreen ? WINDOW_STATE_FULLSCREEN : WINDOW_STATE_NORMAL;
|
return status.isFullscreen ? WINDOW_STATE_FULLSCREEN : WINDOW_STATE_NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void RestoreWindowBounds(void) {
|
|
||||||
emscripten_set_canvas_element_size("#canvas", restoreWidth, restoreHeight);
|
|
||||||
restoreWidth = 0;
|
|
||||||
restoreHeight = 0;
|
|
||||||
EM_ASM({ var cvs = Module['canvas']; cvs.style.width = ""; cvs.style.height = ""; });
|
|
||||||
}
|
|
||||||
|
|
||||||
static EM_BOOL OnFullscreenChange(int type, const EmscriptenFullscreenChangeEvent* ev, void *data) {
|
|
||||||
if (!restoreWidth || !restoreHeight || goingFullscreen || ev->isFullscreen) return false;
|
|
||||||
RestoreWindowBounds();
|
|
||||||
UpdateWindowBounds();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
cc_result Window_EnterFullscreen(void) {
|
cc_result Window_EnterFullscreen(void) {
|
||||||
|
EmscriptenFullscreenStrategy strategy;
|
||||||
|
const char* target;
|
||||||
int res;
|
int res;
|
||||||
goingFullscreen = true;
|
strategy.scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH;
|
||||||
restoreWidth = GetCanvasWidth();
|
strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_HIDEF;
|
||||||
restoreHeight = GetCanvasHeight();
|
strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
|
||||||
SetFullscreenBounds();
|
|
||||||
|
|
||||||
/* For chrome on android, need to fullscreen div surrounding canvas */
|
strategy.canvasResizedCallback = OnCanvasResize;
|
||||||
/* to get input to work in fullscreen (instead of the actual canvas) */
|
strategy.canvasResizedCallbackUserData = NULL;
|
||||||
res = EM_ASM_INT_V({
|
|
||||||
var target = Module['canvas'];
|
/* For chrome on android, need to make container div fullscreen instead */
|
||||||
target.style.width = '100%';
|
res = EM_ASM_INT_V({ return document.getElementById('canvas_wrapper') ? 1 : 0; });
|
||||||
target.style.height = '100%';
|
target = res ? "canvas_wrapper" : "#canvas";
|
||||||
target = document.getElementById('canvas_wrapper') || target;
|
if ((res = emscripten_request_fullscreen_strategy(target, 1, &strategy))) return res;
|
||||||
|
|
||||||
if (target.requestFullscreen) {
|
/* emscripten sets css size to screen's base width/height, */
|
||||||
target.requestFullscreen();
|
/* except that becomes wrong when device rotates. */
|
||||||
} else if (target.msRequestFullscreen) {
|
/* Better to just set CSS width/height to always be 100% */
|
||||||
target.msRequestFullscreen();
|
EM_ASM({
|
||||||
} else if (target.mozRequestFullScreen) {
|
var canvas = Module['canvas'];
|
||||||
target.mozRequestFullScreen();
|
canvas.style.width = '100%';
|
||||||
} else if (target.mozRequestFullscreen) {
|
canvas.style.height = '100%';
|
||||||
target.mozRequestFullscreen();
|
|
||||||
} else if (target.webkitRequestFullscreen) {
|
|
||||||
target.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res) RestoreWindowBounds();
|
|
||||||
UpdateWindowBounds();
|
|
||||||
goingFullscreen = false;
|
|
||||||
return res;
|
|
||||||
/* TODO: navigator.keyboard.lock(["Escape"] */
|
/* TODO: navigator.keyboard.lock(["Escape"] */
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3543,7 +3524,7 @@ void Window_ProcessEvents(void) {
|
|||||||
if (!needResize) return;
|
if (!needResize) return;
|
||||||
needResize = false;
|
needResize = false;
|
||||||
|
|
||||||
if (goingFullscreen || Window_GetWindowState() == WINDOW_STATE_FULLSCREEN) {
|
if (Window_GetWindowState() == WINDOW_STATE_FULLSCREEN) {
|
||||||
SetFullscreenBounds();
|
SetFullscreenBounds();
|
||||||
} else {
|
} else {
|
||||||
EM_ASM( if (typeof(resizeGameCanvas) === 'function') resizeGameCanvas(); );
|
EM_ASM( if (typeof(resizeGameCanvas) === 'function') resizeGameCanvas(); );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user