Browse Source

[HTML5] Implement fullscreenchange in JS library.

Removes more emscripten HTML5 library dependencies.
Fabio Alessandrelli 3 years ago
parent
commit
ac78e7f940

+ 1 - 0
platform/javascript/godot_js.h

@@ -94,6 +94,7 @@ extern void godot_js_display_mouse_move_cb(void (*p_callback)(double p_x, double
 extern void godot_js_display_mouse_wheel_cb(int (*p_callback)(double p_delta_x, double p_delta_y));
 extern void godot_js_display_touch_cb(void (*p_callback)(int p_type, int p_count), uint32_t *r_identifiers, double *r_coords);
 extern void godot_js_display_key_cb(void (*p_callback)(int p_type, int p_repeat, int p_modifiers), char r_code[32], char r_key[32]);
+extern void godot_js_display_fullscreen_cb(void (*p_callback)(int p_fullscreen));
 extern void godot_js_display_notification_cb(void (*p_callback)(int p_notification), int p_enter, int p_exit, int p_in, int p_out);
 extern void godot_js_display_paste_cb(void (*p_callback)(const char *p_text));
 extern void godot_js_display_drop_files_cb(void (*p_callback)(char **p_filev, int p_filec));

+ 14 - 0
platform/javascript/js/libs/library_godot_display.js

@@ -853,6 +853,20 @@ const GodotDisplay = {
 	/*
 	 * Listeners
 	 */
+	godot_js_display_fullscreen_cb__sig: 'vi',
+	godot_js_display_fullscreen_cb: function (callback) {
+		const canvas = GodotConfig.canvas;
+		const func = GodotRuntime.get_func(callback);
+		function change_cb(evt) {
+			if (evt.target === canvas) {
+				func(GodotDisplayScreen.isFullscreen());
+			}
+		}
+		GodotDisplayListeners.add(document, 'fullscreenchange', change_cb, false);
+		GodotDisplayListeners.add(document, 'mozfullscreenchange', change_cb, false);
+		GodotDisplayListeners.add(document, 'webkitfullscreenchange', change_cb, false);
+	},
+
 	godot_js_display_notification_cb__sig: 'viiiii',
 	godot_js_display_notification_cb: function (callback, p_enter, p_exit, p_in, p_out) {
 		const canvas = GodotConfig.canvas;

+ 9 - 18
platform/javascript/os_javascript.cpp

@@ -96,23 +96,16 @@ bool OS_JavaScript::check_size_force_redraw() {
 	return godot_js_display_size_update() != 0;
 }
 
-EM_BOOL OS_JavaScript::fullscreen_change_callback(int p_event_type, const EmscriptenFullscreenChangeEvent *p_event, void *p_user_data) {
+void OS_JavaScript::fullscreen_change_callback(int p_fullscreen) {
 	OS_JavaScript *os = get_singleton();
-	// Empty ID is canvas.
-	String target_id = String::utf8(p_event->id);
-	if (target_id.empty() || target_id == String::utf8(&(os->canvas_id[1]))) {
-		// This event property is the only reliable data on
-		// browser fullscreen state.
-		os->video_mode.fullscreen = p_event->isFullscreen;
-		if (os->video_mode.fullscreen) {
-			os->entering_fullscreen = false;
-		} else {
-			// Restoring maximized window now will cause issues,
-			// so delay until main_loop_iterate.
-			os->just_exited_fullscreen = true;
-		}
+	os->video_mode.fullscreen = p_fullscreen;
+	if (os->video_mode.fullscreen) {
+		os->entering_fullscreen = false;
+	} else {
+		// Restoring maximized window now will cause issues,
+		// so delay until main_loop_iterate.
+		os->just_exited_fullscreen = true;
 	}
-	return false;
 }
 
 EM_BOOL OS_JavaScript::blur_callback(int p_event_type, const EmscriptenFocusEvent *p_event, void *p_user_data) {
@@ -823,7 +816,6 @@ Error OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver,
 	// These callbacks from Emscripten's html5.h suffice to access most
 	// JavaScript APIs.
 	SET_EM_WINDOW_CALLBACK(blur, blur_callback)
-	SET_EM_CALLBACK(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, fullscreenchange, fullscreen_change_callback)
 #undef SET_EM_CALLBACK
 #undef EM_CHECK
 
@@ -832,8 +824,7 @@ Error OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver,
 	godot_js_display_mouse_wheel_cb(&OS_JavaScript::mouse_wheel_callback);
 	godot_js_display_touch_cb(&OS_JavaScript::touch_callback, touch_event.identifier, touch_event.coords);
 	godot_js_display_key_cb(&OS_JavaScript::key_callback, key_event.code, key_event.key);
-	// For APIs that are not (sufficiently) exposed, a
-	// library is used below (implemented in library_godot_display.js).
+	godot_js_display_fullscreen_cb(&OS_JavaScript::fullscreen_change_callback);
 	godot_js_display_notification_cb(&OS_JavaScript::send_notification_callback,
 			MainLoop::NOTIFICATION_WM_MOUSE_ENTER,
 			MainLoop::NOTIFICATION_WM_MOUSE_EXIT,

+ 1 - 1
platform/javascript/os_javascript.h

@@ -82,9 +82,9 @@ private:
 	bool idb_needs_sync;
 	bool idb_is_syncing;
 
-	static EM_BOOL fullscreen_change_callback(int p_event_type, const EmscriptenFullscreenChangeEvent *p_event, void *p_user_data);
 	static EM_BOOL blur_callback(int p_event_type, const EmscriptenFocusEvent *p_event, void *p_user_data);
 
+	static void fullscreen_change_callback(int p_fullscreen);
 	static int mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers);
 	static void mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers);
 	static int mouse_wheel_callback(double p_delta_x, double p_delta_y);