openxr_editor_plugin.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. <uses-feature android:name="android.hardware.vr.headtracking" android:required="false" android:version="1" />
  100. )n";
  101. return contents;
  102. }
  103. String OpenXRExportPlugin::get_android_manifest_activity_element_contents(const Ref<EditorExportPlatform> &p_export_platform, bool p_debug) const {
  104. String contents;
  105. if (!supports_platform(p_export_platform) || !is_openxr_mode()) {
  106. return contents;
  107. }
  108. contents += R"n(
  109. <intent-filter>
  110. <action android:name="android.intent.action.MAIN" />
  111. <category android:name="android.intent.category.DEFAULT" />
  112. <category android:name="org.khronos.openxr.intent.category.IMMERSIVE_HMD" />
  113. </intent-filter>
  114. )n";
  115. return contents;
  116. }
  117. ////////////////////////////////////////////////////////////////////////////
  118. // OpenXREditorPlugin
  119. void OpenXREditorPlugin::edit(Object *p_node) {
  120. if (action_map_editor && Object::cast_to<OpenXRActionMap>(p_node)) {
  121. String path = Object::cast_to<OpenXRActionMap>(p_node)->get_path();
  122. if (path.is_resource_file()) {
  123. action_map_editor->open_action_map(path);
  124. }
  125. }
  126. }
  127. bool OpenXREditorPlugin::handles(Object *p_node) const {
  128. if (action_map_editor) {
  129. return (Object::cast_to<OpenXRActionMap>(p_node) != nullptr);
  130. }
  131. return false;
  132. }
  133. void OpenXREditorPlugin::make_visible(bool p_visible) {
  134. }
  135. OpenXREditorPlugin::OpenXREditorPlugin() {
  136. // Only add our OpenXR action map editor if OpenXR is enabled for the whole project.
  137. if (OpenXRAPI::openxr_is_enabled(false)) {
  138. action_map_editor = memnew(OpenXRActionMapEditor);
  139. EditorDockManager::get_singleton()->add_dock(action_map_editor);
  140. binding_modifier_inspector_plugin = Ref<EditorInspectorPluginBindingModifier>(memnew(EditorInspectorPluginBindingModifier));
  141. EditorInspector::add_inspector_plugin(binding_modifier_inspector_plugin);
  142. #ifndef ANDROID_ENABLED
  143. select_runtime = memnew(OpenXRSelectRuntime);
  144. add_control_to_container(CONTAINER_TOOLBAR, select_runtime);
  145. #endif
  146. }
  147. }
  148. void OpenXREditorPlugin::_notification(int p_what) {
  149. switch (p_what) {
  150. case NOTIFICATION_ENTER_TREE: {
  151. // Initialize our export plugin
  152. openxr_export_plugin.instantiate();
  153. add_export_plugin(openxr_export_plugin);
  154. } break;
  155. case NOTIFICATION_EXIT_TREE: {
  156. // Clean up our export plugin
  157. remove_export_plugin(openxr_export_plugin);
  158. openxr_export_plugin.unref();
  159. }
  160. }
  161. }