Răsfoiți Sursa

[HTML5] Detect screen scale and DPI.

`OS.get_screen_scale` will now return the `window.devicePixelRatio`
value, `OS.get_screen_dpi` uses CSS media queries to find approximate
DPI value for the current display.
`OS.get_screen_size` also return the actual screen size (not the CSS
pixel size).
Fabio Alessandrelli 4 ani în urmă
părinte
comite
b3f78687de

+ 1 - 0
platform/javascript/godot_js.h

@@ -51,6 +51,7 @@ extern int godot_js_os_execute(const char *p_json);
 extern void godot_js_os_shell_open(const char *p_uri);
 extern void godot_js_os_shell_open(const char *p_uri);
 
 
 // Display
 // Display
+extern int godot_js_display_screen_dpi_get();
 extern double godot_js_display_pixel_ratio_get();
 extern double godot_js_display_pixel_ratio_get();
 extern void godot_js_display_alert(const char *p_text);
 extern void godot_js_display_alert(const char *p_text);
 extern int godot_js_display_touchscreen_is_available();
 extern int godot_js_display_touchscreen_is_available();

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

@@ -405,6 +405,27 @@ const GodotDisplay = {
 	$GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotDisplayListeners', '$GodotDisplayDragDrop', '$GodotDisplayGamepads'],
 	$GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotDisplayListeners', '$GodotDisplayDragDrop', '$GodotDisplayGamepads'],
 	$GodotDisplay: {
 	$GodotDisplay: {
 		window_icon: '',
 		window_icon: '',
+		findDPI: function () {
+			function testDPI(dpi) {
+				return window.matchMedia(`(max-resolution: ${dpi}dpi)`).matches;
+			}
+			function bisect(low, high, func) {
+				const mid = parseInt(((high - low) / 2) + low, 10);
+				if (high - low <= 1) {
+					return func(high) ? high : low;
+				}
+				if (func(mid)) {
+					return bisect(low, mid, func);
+				}
+				return bisect(mid, high, func);
+			}
+			try {
+				const dpi = bisect(0, 800, testDPI);
+				return dpi >= 96 ? dpi : 96;
+			} catch (e) {
+				return 96;
+			}
+		},
 	},
 	},
 
 
 	godot_js_display_is_swap_ok_cancel__sig: 'i',
 	godot_js_display_is_swap_ok_cancel__sig: 'i',
@@ -422,6 +443,11 @@ const GodotDisplay = {
 		window.alert(GodotRuntime.parseString(p_text)); // eslint-disable-line no-alert
 		window.alert(GodotRuntime.parseString(p_text)); // eslint-disable-line no-alert
 	},
 	},
 
 
+	godot_js_display_screen_dpi_get__sig: 'i',
+	godot_js_display_screen_dpi_get: function () {
+		return GodotDisplay.findDPI();
+	},
+
 	godot_js_display_pixel_ratio_get__sig: 'f',
 	godot_js_display_pixel_ratio_get__sig: 'f',
 	godot_js_display_pixel_ratio_get: function () {
 	godot_js_display_pixel_ratio_get: function () {
 		return window.devicePixelRatio || 1;
 		return window.devicePixelRatio || 1;

+ 14 - 1
platform/javascript/os_javascript.cpp

@@ -159,7 +159,8 @@ Size2 OS_JavaScript::get_screen_size(int p_screen) const {
 	EmscriptenFullscreenChangeEvent ev;
 	EmscriptenFullscreenChangeEvent ev;
 	EMSCRIPTEN_RESULT result = emscripten_get_fullscreen_status(&ev);
 	EMSCRIPTEN_RESULT result = emscripten_get_fullscreen_status(&ev);
 	ERR_FAIL_COND_V(result != EMSCRIPTEN_RESULT_SUCCESS, Size2());
 	ERR_FAIL_COND_V(result != EMSCRIPTEN_RESULT_SUCCESS, Size2());
-	return Size2(ev.screenWidth, ev.screenHeight);
+	double scale = godot_js_display_pixel_ratio_get();
+	return Size2(ev.screenWidth * scale, ev.screenHeight * scale);
 }
 }
 
 
 void OS_JavaScript::set_window_size(const Size2 p_size) {
 void OS_JavaScript::set_window_size(const Size2 p_size) {
@@ -1014,6 +1015,18 @@ bool OS_JavaScript::main_loop_iterate() {
 	return Main::iteration();
 	return Main::iteration();
 }
 }
 
 
+int OS_JavaScript::get_screen_dpi(int p_screen) const {
+	return godot_js_display_screen_dpi_get();
+}
+
+float OS_JavaScript::get_screen_scale(int p_screen) const {
+	return godot_js_display_pixel_ratio_get();
+}
+
+float OS_JavaScript::get_screen_max_scale() const {
+	return get_screen_scale();
+}
+
 void OS_JavaScript::delete_main_loop() {
 void OS_JavaScript::delete_main_loop() {
 
 
 	memdelete(main_loop);
 	memdelete(main_loop);

+ 3 - 0
platform/javascript/os_javascript.h

@@ -134,6 +134,9 @@ public:
 	virtual void set_window_fullscreen(bool p_enabled);
 	virtual void set_window_fullscreen(bool p_enabled);
 	virtual bool is_window_fullscreen() const;
 	virtual bool is_window_fullscreen() const;
 	virtual Size2 get_screen_size(int p_screen = -1) const;
 	virtual Size2 get_screen_size(int p_screen = -1) const;
+	virtual int get_screen_dpi(int p_screen = -1) const;
+	virtual float get_screen_scale(int p_screen = -1) const;
+	virtual float get_screen_max_scale() const;
 
 
 	virtual Point2 get_mouse_position() const;
 	virtual Point2 get_mouse_position() const;
 	virtual int get_mouse_button_state() const;
 	virtual int get_mouse_button_state() const;