openxr_editor_plugin.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**************************************************************************/
  2. /* openxr_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "openxr_editor_plugin.h"
  31. #include "../action_map/openxr_action_map.h"
  32. #include "../openxr_api.h"
  33. #include "editor/docks/editor_dock_manager.h"
  34. #include "editor/editor_node.h"
  35. #include "platform/android/export/export_plugin.h"
  36. #include <openxr/openxr.h>
  37. ////////////////////////////////////////////////////////////////////////////
  38. // OpenXRExportPlugin
  39. bool OpenXRExportPlugin::supports_platform(const Ref<EditorExportPlatform> &p_export_platform) const {
  40. return p_export_platform->is_class(EditorExportPlatformAndroid::get_class_static());
  41. }
  42. bool OpenXRExportPlugin::is_openxr_mode() const {
  43. // Check if OpenXR is enabled using `EditorExportPlatform::get_project_settings()` because that'll
  44. // take into account the feature tags on the specific export preset that is being exported.
  45. bool openxr_enabled = (bool)get_export_platform()->get_project_setting(get_export_preset(), "xr/openxr/enabled");
  46. int xr_mode_index = get_option("xr_features/xr_mode");
  47. return openxr_enabled && xr_mode_index == XR_MODE_OPENXR;
  48. }
  49. String OpenXRExportPlugin::_get_export_option_warning(const Ref<EditorExportPlatform> &p_export_platform, const String &p_option_name) const {
  50. if (!supports_platform(p_export_platform)) {
  51. return String();
  52. }
  53. bool gradle_build = get_option("gradle_build/use_gradle_build");
  54. if (is_openxr_mode() && !gradle_build) {
  55. return "\"Use Gradle Build\" must be enabled when xr_mode is set to \"OpenXR\".";
  56. }
  57. return String();
  58. }
  59. PackedStringArray OpenXRExportPlugin::get_android_dependencies(const Ref<EditorExportPlatform> &p_export_platform, bool p_debug) const {
  60. PackedStringArray ret;
  61. if (!supports_platform(p_export_platform)) {
  62. return ret;
  63. }
  64. if (is_openxr_mode()) {
  65. // Loader is always identified by the full API version even if we're initializing for OpenXR 1.0.
  66. int major = XR_VERSION_MAJOR(XR_CURRENT_API_VERSION);
  67. int minor = XR_VERSION_MINOR(XR_CURRENT_API_VERSION);
  68. int patch = XR_VERSION_PATCH(XR_CURRENT_API_VERSION);
  69. String openxr_loader = "org.khronos.openxr:openxr_loader_for_android:" + String::num_int64(major) + "." + String::num_int64(minor) + "." + String::num_int64(patch);
  70. ret.push_back(openxr_loader);
  71. }
  72. return ret;
  73. }
  74. PackedStringArray OpenXRExportPlugin::_get_export_features(const Ref<EditorExportPlatform> &p_export_platform, bool p_debug) const {
  75. PackedStringArray features;
  76. if (!supports_platform(p_export_platform) || !is_openxr_mode()) {
  77. return features;
  78. }
  79. // Placeholder for now
  80. return features;
  81. }
  82. String OpenXRExportPlugin::get_android_manifest_element_contents(const Ref<EditorExportPlatform> &p_export_platform, bool p_debug) const {
  83. String contents;
  84. if (!supports_platform(p_export_platform) || !is_openxr_mode()) {
  85. return contents;
  86. }
  87. contents += R"n(
  88. <uses-permission android:name="org.khronos.openxr.permission.OPENXR" />
  89. <uses-permission android:name="org.khronos.openxr.permission.OPENXR_SYSTEM" />
  90. <queries>
  91. <provider android:authorities="org.khronos.openxr.runtime_broker;org.khronos.openxr.system_runtime_broker" />
  92. <intent>
  93. <action android:name="org.khronos.openxr.OpenXRRuntimeService" />
  94. </intent>
  95. <intent>
  96. <action android:name="org.khronos.openxr.OpenXRApiLayerService" />
  97. </intent>
  98. </queries>
  99. )n";
  100. #ifndef DISABLE_DEPRECATED
  101. // This logic addresses the issue from https://github.com/GodotVR/godot_openxr_vendors/issues/429.
  102. // The issue is caused by this plugin and the vendors plugin adding the same `uses-feature` tag to the generated
  103. // manifest, causing a duplicate error at build time.
  104. // In order to maintain backward compatibility, we fix the issue by disabling the addition of the `uses-feature`
  105. // tag from this plugin when specific conditions are met.
  106. bool meta_plugin_enabled = get_option("xr_features/enable_meta_plugin");
  107. int meta_boundary_mode = get_option("meta_xr_features/boundary_mode");
  108. if (!meta_plugin_enabled || meta_boundary_mode != 1 /* BOUNDARY_DISABLED_VALUE */) {
  109. #endif // DISABLE_DEPRECATED
  110. contents += " <uses-feature android:name=\"android.hardware.vr.headtracking\" android:required=\"false\" android:version=\"1\" />\n";
  111. #ifndef DISABLE_DEPRECATED
  112. }
  113. #endif // DISABLE_DEPRECATED
  114. return contents;
  115. }
  116. String OpenXRExportPlugin::get_android_manifest_activity_element_contents(const Ref<EditorExportPlatform> &p_export_platform, bool p_debug) const {
  117. String contents;
  118. if (!supports_platform(p_export_platform) || !is_openxr_mode()) {
  119. return contents;
  120. }
  121. contents += R"n(
  122. <intent-filter>
  123. <action android:name="android.intent.action.MAIN" />
  124. <category android:name="android.intent.category.DEFAULT" />
  125. <category android:name="org.khronos.openxr.intent.category.IMMERSIVE_HMD" />
  126. </intent-filter>
  127. )n";
  128. return contents;
  129. }
  130. ////////////////////////////////////////////////////////////////////////////
  131. // OpenXREditorPlugin
  132. void OpenXREditorPlugin::edit(Object *p_node) {
  133. if (action_map_editor && Object::cast_to<OpenXRActionMap>(p_node)) {
  134. String path = Object::cast_to<OpenXRActionMap>(p_node)->get_path();
  135. if (path.is_resource_file()) {
  136. action_map_editor->open_action_map(path);
  137. }
  138. }
  139. }
  140. bool OpenXREditorPlugin::handles(Object *p_node) const {
  141. if (action_map_editor) {
  142. return (Object::cast_to<OpenXRActionMap>(p_node) != nullptr);
  143. }
  144. return false;
  145. }
  146. void OpenXREditorPlugin::make_visible(bool p_visible) {
  147. }
  148. OpenXREditorPlugin::OpenXREditorPlugin() {
  149. // Only add our OpenXR action map editor if OpenXR is enabled for the whole project.
  150. if (OpenXRAPI::openxr_is_enabled(false)) {
  151. action_map_editor = memnew(OpenXRActionMapEditor);
  152. EditorDockManager::get_singleton()->add_dock(action_map_editor);
  153. binding_modifier_inspector_plugin = Ref<EditorInspectorPluginBindingModifier>(memnew(EditorInspectorPluginBindingModifier));
  154. EditorInspector::add_inspector_plugin(binding_modifier_inspector_plugin);
  155. #ifndef ANDROID_ENABLED
  156. select_runtime = memnew(OpenXRSelectRuntime);
  157. add_control_to_container(CONTAINER_TOOLBAR, select_runtime);
  158. #endif
  159. }
  160. }
  161. void OpenXREditorPlugin::_notification(int p_what) {
  162. switch (p_what) {
  163. case NOTIFICATION_ENTER_TREE: {
  164. // Initialize our export plugin
  165. openxr_export_plugin.instantiate();
  166. add_export_plugin(openxr_export_plugin);
  167. } break;
  168. case NOTIFICATION_EXIT_TREE: {
  169. // Clean up our export plugin
  170. remove_export_plugin(openxr_export_plugin);
  171. openxr_export_plugin.unref();
  172. }
  173. }
  174. }