From e18107a57ccaf3d93a8893fe36f6d9010e2cdef1 Mon Sep 17 00:00:00 2001
From: Markus Sauermann <6299227+Sauermann@users.noreply.github.com>
Date: Mon, 17 Oct 2022 00:59:51 +0200
Subject: [PATCH] Fix Determining Window for Touchscreen
DisplayServer::screen_is_touchscreen will likely never utilize its
parameter p_screen.
This PR replaces this function by DisplayServer::is_touchscreen_available()
with the same functionality.
This solves the problem, that a SubViewport was used for determining
the screen, which resulted in error messages.
---
doc/classes/DisplayServer.xml | 13 ++++++-------
editor/editor_node.cpp | 9 +--------
editor/plugins/node_3d_editor_plugin.cpp | 2 +-
main/main.cpp | 8 +-------
platform/android/display_server_android.cpp | 2 +-
platform/android/display_server_android.h | 2 +-
platform/ios/display_server_ios.h | 2 +-
platform/ios/display_server_ios.mm | 2 +-
platform/web/display_server_web.cpp | 4 ++--
platform/web/display_server_web.h | 2 +-
scene/2d/touch_screen_button.cpp | 4 ++--
scene/gui/scroll_bar.cpp | 2 +-
scene/gui/scroll_container.cpp | 4 ++--
scene/gui/tree.cpp | 2 +-
servers/display_server.cpp | 5 ++---
servers/display_server.h | 2 +-
16 files changed, 25 insertions(+), 40 deletions(-)
diff --git a/doc/classes/DisplayServer.xml b/doc/classes/DisplayServer.xml
index c59bfbfe4f4..4081afa13f2 100644
--- a/doc/classes/DisplayServer.xml
+++ b/doc/classes/DisplayServer.xml
@@ -719,6 +719,12 @@
[b]Note:[/b] This method is implemented on macOS, Windows and Linux (X11).
+
+
+
+ Returns [code]true[/code] if touch events are available (Android or iOS), the capability is detected on the Webplatform or if [member ProjectSettings.input_devices/pointing/emulate_touch_from_mouse] is [code]true[/code].
+
+
@@ -889,13 +895,6 @@
Returns [code]true[/code] if the screen should never be turned off by the operating system's power-saving measures. See also [method screen_set_keep_on].
-
-
-
-
- Returns [code]true[/code] if the screen can send touch events or if [member ProjectSettings.input_devices/pointing/emulate_touch_from_mouse] is [code]true[/code].
-
-
diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp
index 06e1e8b22eb..463e8f6bdcc 100644
--- a/editor/editor_node.cpp
+++ b/editor/editor_node.cpp
@@ -6038,14 +6038,7 @@ EditorNode::EditorNode() {
Input *id = Input::get_singleton();
if (id) {
- bool found_touchscreen = false;
- for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
- if (DisplayServer::get_singleton()->screen_is_touchscreen(i)) {
- found_touchscreen = true;
- }
- }
-
- if (!found_touchscreen && Input::get_singleton()) {
+ if (!DisplayServer::get_singleton()->is_touchscreen_available() && Input::get_singleton()) {
// Only if no touchscreen ui hint, disable emulation just in case.
id->set_emulate_touch_from_mouse(false);
}
diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp
index c97de80a768..75ef679deda 100644
--- a/editor/plugins/node_3d_editor_plugin.cpp
+++ b/editor/plugins/node_3d_editor_plugin.cpp
@@ -8420,7 +8420,7 @@ Node3DEditor::Node3DEditor() {
EDITOR_DEF("editors/3d/manipulator_gizmo_opacity", 0.9);
EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::FLOAT, "editors/3d/manipulator_gizmo_opacity", PROPERTY_HINT_RANGE, "0,1,0.01"));
EDITOR_DEF_RST("editors/3d/navigation/show_viewport_rotation_gizmo", true);
- EDITOR_DEF_RST("editors/3d/navigation/show_viewport_navigation_gizmo", DisplayServer::get_singleton()->screen_is_touchscreen());
+ EDITOR_DEF_RST("editors/3d/navigation/show_viewport_navigation_gizmo", DisplayServer::get_singleton()->is_touchscreen_available());
current_hover_gizmo_handle = -1;
current_hover_gizmo_handle_secondary = false;
diff --git a/main/main.cpp b/main/main.cpp
index c2e2238482f..fd1dfabb1bc 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -2233,13 +2233,7 @@ Error Main::setup2(Thread::ID p_main_tid_override) {
if (bool(GLOBAL_DEF("input_devices/pointing/emulate_touch_from_mouse", false)) &&
!(editor || project_manager)) {
- bool found_touchscreen = false;
- for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
- if (DisplayServer::get_singleton()->screen_is_touchscreen(i)) {
- found_touchscreen = true;
- }
- }
- if (!found_touchscreen) {
+ if (!DisplayServer::get_singleton()->is_touchscreen_available()) {
//only if no touchscreen ui hint, set emulation
id->set_emulate_touch_from_mouse(true);
}
diff --git a/platform/android/display_server_android.cpp b/platform/android/display_server_android.cpp
index 937b929d622..1e2f35381bb 100644
--- a/platform/android/display_server_android.cpp
+++ b/platform/android/display_server_android.cpp
@@ -221,7 +221,7 @@ float DisplayServerAndroid::screen_get_refresh_rate(int p_screen) const {
return godot_io_java->get_screen_refresh_rate(SCREEN_REFRESH_RATE_FALLBACK);
}
-bool DisplayServerAndroid::screen_is_touchscreen(int p_screen) const {
+bool DisplayServerAndroid::is_touchscreen_available() const {
return true;
}
diff --git a/platform/android/display_server_android.h b/platform/android/display_server_android.h
index c7f4d8046fc..c1c11d6cbab 100644
--- a/platform/android/display_server_android.h
+++ b/platform/android/display_server_android.h
@@ -121,7 +121,7 @@ public:
virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
virtual float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
- virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
+ virtual bool is_touchscreen_available() const override;
virtual void virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2(), VirtualKeyboardType p_type = KEYBOARD_TYPE_DEFAULT, int p_max_length = -1, int p_cursor_start = -1, int p_cursor_end = -1) override;
virtual void virtual_keyboard_hide() override;
diff --git a/platform/ios/display_server_ios.h b/platform/ios/display_server_ios.h
index 447f9191399..53baaf86412 100644
--- a/platform/ios/display_server_ios.h
+++ b/platform/ios/display_server_ios.h
@@ -199,7 +199,7 @@ public:
virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override;
virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;
- virtual bool screen_is_touchscreen(int p_screen) const override;
+ virtual bool is_touchscreen_available() const override;
virtual void virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect, VirtualKeyboardType p_type, int p_max_length, int p_cursor_start, int p_cursor_end) override;
virtual void virtual_keyboard_hide() override;
diff --git a/platform/ios/display_server_ios.mm b/platform/ios/display_server_ios.mm
index 6793b40dd46..703a67d98c9 100644
--- a/platform/ios/display_server_ios.mm
+++ b/platform/ios/display_server_ios.mm
@@ -581,7 +581,7 @@ bool DisplayServerIOS::can_any_window_draw() const {
return true;
}
-bool DisplayServerIOS::screen_is_touchscreen(int p_screen) const {
+bool DisplayServerIOS::is_touchscreen_available() const {
return true;
}
diff --git a/platform/web/display_server_web.cpp b/platform/web/display_server_web.cpp
index f7041247041..180baff02ef 100644
--- a/platform/web/display_server_web.cpp
+++ b/platform/web/display_server_web.cpp
@@ -579,8 +579,8 @@ void DisplayServerWeb::touch_callback(int p_type, int p_count) {
}
}
-bool DisplayServerWeb::screen_is_touchscreen(int p_screen) const {
- return godot_js_display_touchscreen_is_available();
+bool DisplayServerWeb::is_touchscreen_available() const {
+ return godot_js_display_touchscreen_is_available() || (Input::get_singleton() && Input::get_singleton()->is_emulating_touch_from_mouse());
}
// Virtual Keyboard
diff --git a/platform/web/display_server_web.h b/platform/web/display_server_web.h
index 1919736802b..28e83474b97 100644
--- a/platform/web/display_server_web.h
+++ b/platform/web/display_server_web.h
@@ -143,7 +143,7 @@ public:
virtual Point2i mouse_get_position() const override;
// touch
- virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
+ virtual bool is_touchscreen_available() const override;
// clipboard
virtual void clipboard_set(const String &p_text) override;
diff --git a/scene/2d/touch_screen_button.cpp b/scene/2d/touch_screen_button.cpp
index a02f322ef12..e99821e9b93 100644
--- a/scene/2d/touch_screen_button.cpp
+++ b/scene/2d/touch_screen_button.cpp
@@ -100,7 +100,7 @@ void TouchScreenButton::_notification(int p_what) {
if (!is_inside_tree()) {
return;
}
- if (!Engine::get_singleton()->is_editor_hint() && !!DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id())) && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
+ if (!Engine::get_singleton()->is_editor_hint() && !!DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
return;
}
@@ -137,7 +137,7 @@ void TouchScreenButton::_notification(int p_what) {
} break;
case NOTIFICATION_ENTER_TREE: {
- if (!Engine::get_singleton()->is_editor_hint() && !!DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id())) && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
+ if (!Engine::get_singleton()->is_editor_hint() && !!DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
return;
}
queue_redraw();
diff --git a/scene/gui/scroll_bar.cpp b/scene/gui/scroll_bar.cpp
index 6899178885f..a44ddff5070 100644
--- a/scene/gui/scroll_bar.cpp
+++ b/scene/gui/scroll_bar.cpp
@@ -550,7 +550,7 @@ void ScrollBar::_drag_node_input(const Ref &p_input) {
drag_node_accum = Vector2();
last_drag_node_accum = Vector2();
drag_node_from = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0);
- drag_node_touching = DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id()));
+ drag_node_touching = DisplayServer::get_singleton()->is_touchscreen_available();
drag_node_touching_deaccel = false;
time_since_motion = 0;
diff --git a/scene/gui/scroll_container.cpp b/scene/gui/scroll_container.cpp
index 531226f938e..73d30b7568d 100644
--- a/scene/gui/scroll_container.cpp
+++ b/scene/gui/scroll_container.cpp
@@ -164,8 +164,8 @@ void ScrollContainer::gui_input(const Ref &p_gui_input) {
}
}
- bool screen_is_touchscreen = DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id()));
- if (!screen_is_touchscreen) {
+ bool is_touchscreen_available = DisplayServer::get_singleton()->is_touchscreen_available();
+ if (!is_touchscreen_available) {
return;
}
diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp
index c0990211aa8..3996d2d65d1 100644
--- a/scene/gui/tree.cpp
+++ b/scene/gui/tree.cpp
@@ -3671,7 +3671,7 @@ void Tree::gui_input(const Ref &p_event) {
drag_accum = 0;
//last_drag_accum=0;
drag_from = v_scroll->get_value();
- drag_touching = DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id()));
+ drag_touching = DisplayServer::get_singleton()->is_touchscreen_available();
drag_touching_deaccel = false;
if (drag_touching) {
set_physics_process_internal(true);
diff --git a/servers/display_server.cpp b/servers/display_server.cpp
index 52d7f66203f..897c685f253 100644
--- a/servers/display_server.cpp
+++ b/servers/display_server.cpp
@@ -367,8 +367,7 @@ float DisplayServer::screen_get_scale(int p_screen) const {
return 1.0f;
};
-bool DisplayServer::screen_is_touchscreen(int p_screen) const {
- //return false;
+bool DisplayServer::is_touchscreen_available() const {
return Input::get_singleton() && Input::get_singleton()->is_emulating_touch_from_mouse();
}
@@ -618,7 +617,7 @@ void DisplayServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("screen_get_usable_rect", "screen"), &DisplayServer::screen_get_usable_rect, DEFVAL(SCREEN_OF_MAIN_WINDOW));
ClassDB::bind_method(D_METHOD("screen_get_dpi", "screen"), &DisplayServer::screen_get_dpi, DEFVAL(SCREEN_OF_MAIN_WINDOW));
ClassDB::bind_method(D_METHOD("screen_get_scale", "screen"), &DisplayServer::screen_get_scale, DEFVAL(SCREEN_OF_MAIN_WINDOW));
- ClassDB::bind_method(D_METHOD("screen_is_touchscreen", "screen"), &DisplayServer::screen_is_touchscreen, DEFVAL(SCREEN_OF_MAIN_WINDOW));
+ ClassDB::bind_method(D_METHOD("is_touchscreen_available"), &DisplayServer::is_touchscreen_available, DEFVAL(SCREEN_OF_MAIN_WINDOW));
ClassDB::bind_method(D_METHOD("screen_get_max_scale"), &DisplayServer::screen_get_max_scale);
ClassDB::bind_method(D_METHOD("screen_get_refresh_rate", "screen"), &DisplayServer::screen_get_refresh_rate, DEFVAL(SCREEN_OF_MAIN_WINDOW));
diff --git a/servers/display_server.h b/servers/display_server.h
index a796377f88a..74b26046f83 100644
--- a/servers/display_server.h
+++ b/servers/display_server.h
@@ -262,7 +262,7 @@ public:
return scale;
}
virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const = 0;
- virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
+ virtual bool is_touchscreen_available() const;
// Keep the ScreenOrientation enum values in sync with the `display/window/handheld/orientation`
// project setting hint.