From 660952a85773e959664376faf5c09e63aa613092 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Fri, 21 May 2021 21:29:24 +0200 Subject: [PATCH] Use an enum to represent screen orientation in the Project Settings - Tweak the setting property hint to be more informative. - Make the setting a "basic" setting so it appears when Advanced Settings is disabled. - Remove redundant orientation setting in the iOS export preset. The project setting is now used (like on Android). Projects upgrading from a previous version will have to set the screen orientation again in the Project Settings if it wasn't set to the default value ("landscape"). --- core/config/project_settings.cpp | 3 +- doc/classes/ProjectSettings.xml | 5 +- main/main.cpp | 18 +------- platform/android/export/export.cpp | 3 +- platform/android/export/gradle_export_util.h | 24 +--------- platform/iphone/export/export.cpp | 48 +++++++++++++------- servers/display_server.h | 3 ++ 7 files changed, 44 insertions(+), 60 deletions(-) diff --git a/core/config/project_settings.cpp b/core/config/project_settings.cpp index 0d699cdacbc..53d13f74290 100644 --- a/core/config/project_settings.cpp +++ b/core/config/project_settings.cpp @@ -1114,7 +1114,8 @@ ProjectSettings::ProjectSettings() { _add_builtin_input_map(); - custom_prop_info["display/window/handheld/orientation"] = PropertyInfo(Variant::STRING, "display/window/handheld/orientation", PROPERTY_HINT_ENUM, "landscape,portrait,reverse_landscape,reverse_portrait,sensor_landscape,sensor_portrait,sensor"); + // Keep the enum values in sync with the `DisplayServer::ScreenOrientation` enum. + custom_prop_info["display/window/handheld/orientation"] = PropertyInfo(Variant::INT, "display/window/handheld/orientation", PROPERTY_HINT_ENUM, "Landscape,Portrait,Reverse Landscape,Reverse Portrait,Sensor Landscape,Sensor Portrait,Sensor"); custom_prop_info["rendering/driver/threads/thread_model"] = PropertyInfo(Variant::INT, "rendering/driver/threads/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded"); GLOBAL_DEF("physics/2d/run_on_thread", false); GLOBAL_DEF("physics/3d/run_on_thread", false); diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml index 24d4a8a46e0..5ac8fb45615 100644 --- a/doc/classes/ProjectSettings.xml +++ b/doc/classes/ProjectSettings.xml @@ -494,8 +494,9 @@ If [code]true[/code], keeps the screen on (even in case of inactivity), so the screensaver does not take over. Works on desktop and mobile platforms. - - Default orientation on mobile devices. + + The default screen orientation to use on mobile devices. See [enum DisplayServer.ScreenOrientation] for possible values. + [b]Note:[/b] When set to a portrait orientation, this project setting does not flip the project resolution's width and height automatically. Instead, you have to set [member display/window/size/width] and [member display/window/size/height] accordingly. If [code]true[/code], the home indicator is hidden automatically. This only affects iOS devices without a physical home button. diff --git a/main/main.cpp b/main/main.cpp index 67d8d93728f..2f191b5f63c 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1327,23 +1327,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } { - String orientation = GLOBAL_DEF("display/window/handheld/orientation", "landscape"); - - if (orientation == "portrait") { - window_orientation = DisplayServer::SCREEN_PORTRAIT; - } else if (orientation == "reverse_landscape") { - window_orientation = DisplayServer::SCREEN_REVERSE_LANDSCAPE; - } else if (orientation == "reverse_portrait") { - window_orientation = DisplayServer::SCREEN_REVERSE_PORTRAIT; - } else if (orientation == "sensor_landscape") { - window_orientation = DisplayServer::SCREEN_SENSOR_LANDSCAPE; - } else if (orientation == "sensor_portrait") { - window_orientation = DisplayServer::SCREEN_SENSOR_PORTRAIT; - } else if (orientation == "sensor") { - window_orientation = DisplayServer::SCREEN_SENSOR; - } else { - window_orientation = DisplayServer::SCREEN_LANDSCAPE; - } + window_orientation = DisplayServer::ScreenOrientation(int(GLOBAL_DEF_BASIC("display/window/handheld/orientation", DisplayServer::ScreenOrientation::SCREEN_LANDSCAPE))); } Engine::get_singleton()->set_iterations_per_second(GLOBAL_DEF_BASIC("physics/common/physics_fps", 60)); diff --git a/platform/android/export/export.cpp b/platform/android/export/export.cpp index cd3f00f9353..ec2618a3d5f 100644 --- a/platform/android/export/export.cpp +++ b/platform/android/export/export.cpp @@ -847,7 +847,8 @@ class EditorExportPlatformAndroid : public EditorExportPlatform { int version_code = p_preset->get("version/code"); String package_name = p_preset->get("package/unique_name"); - const int screen_orientation = _get_android_orientation_value(_get_screen_orientation()); + const int screen_orientation = + _get_android_orientation_value(DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation")))); bool screen_support_small = p_preset->get("screen/support_small"); bool screen_support_normal = p_preset->get("screen/support_normal"); diff --git a/platform/android/export/gradle_export_util.h b/platform/android/export/gradle_export_util.h index bbbb526af94..48e7fd46ace 100644 --- a/platform/android/export/gradle_export_util.h +++ b/platform/android/export/gradle_export_util.h @@ -44,28 +44,6 @@ const String godot_project_name_xml_string = R"( )"; -DisplayServer::ScreenOrientation _get_screen_orientation() { - String orientation_settings = ProjectSettings::get_singleton()->get("display/window/handheld/orientation"); - DisplayServer::ScreenOrientation screen_orientation; - if (orientation_settings == "portrait") { - screen_orientation = DisplayServer::SCREEN_PORTRAIT; - } else if (orientation_settings == "reverse_landscape") { - screen_orientation = DisplayServer::SCREEN_REVERSE_LANDSCAPE; - } else if (orientation_settings == "reverse_portrait") { - screen_orientation = DisplayServer::SCREEN_REVERSE_PORTRAIT; - } else if (orientation_settings == "sensor_landscape") { - screen_orientation = DisplayServer::SCREEN_SENSOR_LANDSCAPE; - } else if (orientation_settings == "sensor_portrait") { - screen_orientation = DisplayServer::SCREEN_SENSOR_PORTRAIT; - } else if (orientation_settings == "sensor") { - screen_orientation = DisplayServer::SCREEN_SENSOR; - } else { - screen_orientation = DisplayServer::SCREEN_LANDSCAPE; - } - - return screen_orientation; -} - int _get_android_orientation_value(DisplayServer::ScreenOrientation screen_orientation) { switch (screen_orientation) { case DisplayServer::SCREEN_PORTRAIT: @@ -266,7 +244,7 @@ String _get_instrumentation_tag(const Ref &p_preset) { String _get_activity_tag(const Ref &p_preset) { bool uses_xr = (int)(p_preset->get("xr_features/xr_mode")) == 1; - String orientation = _get_android_orientation_label(_get_screen_orientation()); + String orientation = _get_android_orientation_label(DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation")))); String manifest_activity_text = vformat( " *r_options) r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/microphone_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need to use the microphone"), "")); r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "privacy/photolibrary_usage_description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Provide a message if you need access to the photo library"), "")); - r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait"), true)); - r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape_left"), true)); - r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape_right"), true)); - r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait_upside_down"), true)); - r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "icons/generate_missing"), false)); r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "required_icons/iphone_120x120", PROPERTY_HINT_FILE, "*.png"), "")); // Home screen on iPhone/iPod Touch with retina display @@ -501,18 +496,39 @@ void EditorExportPlatformIOS::_fix_config_file(const Ref &p_ strnew += lines[i].replace("$required_device_capabilities", capabilities); } else if (lines[i].find("$interface_orientations") != -1) { String orientations; + const DisplayServer::ScreenOrientation screen_orientation = + DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation"))); - if ((bool)p_preset->get("orientation/portrait")) { - orientations += "UIInterfaceOrientationPortrait\n"; - } - if ((bool)p_preset->get("orientation/landscape_left")) { - orientations += "UIInterfaceOrientationLandscapeLeft\n"; - } - if ((bool)p_preset->get("orientation/landscape_right")) { - orientations += "UIInterfaceOrientationLandscapeRight\n"; - } - if ((bool)p_preset->get("orientation/portrait_upside_down")) { - orientations += "UIInterfaceOrientationPortraitUpsideDown\n"; + switch (screen_orientation) { + case DisplayServer::SCREEN_LANDSCAPE: + orientations += "UIInterfaceOrientationLandscapeLeft\n"; + break; + case DisplayServer::SCREEN_PORTRAIT: + orientations += "UIInterfaceOrientationPortrait\n"; + break; + case DisplayServer::SCREEN_REVERSE_LANDSCAPE: + orientations += "UIInterfaceOrientationLandscapeRight\n"; + break; + case DisplayServer::SCREEN_REVERSE_PORTRAIT: + orientations += "UIInterfaceOrientationPortraitUpsideDown\n"; + break; + case DisplayServer::SCREEN_SENSOR_LANDSCAPE: + // Allow both landscape orientations depending on sensor direction. + orientations += "UIInterfaceOrientationLandscapeLeft\n"; + orientations += "UIInterfaceOrientationLandscapeRight\n"; + break; + case DisplayServer::SCREEN_SENSOR_PORTRAIT: + // Allow both portrait orientations depending on sensor direction. + orientations += "UIInterfaceOrientationPortrait\n"; + orientations += "UIInterfaceOrientationPortraitUpsideDown\n"; + break; + case DisplayServer::SCREEN_SENSOR: + // Allow all screen orientations depending on sensor direction. + orientations += "UIInterfaceOrientationLandscapeLeft\n"; + orientations += "UIInterfaceOrientationLandscapeRight\n"; + orientations += "UIInterfaceOrientationPortrait\n"; + orientations += "UIInterfaceOrientationPortraitUpsideDown\n"; + break; } strnew += lines[i].replace("$interface_orientations", orientations); diff --git a/servers/display_server.h b/servers/display_server.h index f05aa1f59a2..c108281aff9 100644 --- a/servers/display_server.h +++ b/servers/display_server.h @@ -175,6 +175,9 @@ public: return scale; } virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const; + + // Keep the ScreenOrientation enum values in sync with the `display/window/handheld/orientation` + // project setting hint. enum ScreenOrientation { SCREEN_LANDSCAPE, SCREEN_PORTRAIT,