register_types.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*************************************************************************/
  2. /* register_types.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "register_types.h"
  31. #include "main/main.h"
  32. #include "openxr_interface.h"
  33. #include "action_map/openxr_action.h"
  34. #include "action_map/openxr_action_map.h"
  35. #include "action_map/openxr_action_set.h"
  36. #include "action_map/openxr_interaction_profile.h"
  37. #ifdef TOOLS_ENABLED
  38. #include "editor/editor_node.h"
  39. #include "editor/openxr_editor_plugin.h"
  40. static void _editor_init() {
  41. if (OpenXRAPI::openxr_is_enabled(false)) {
  42. // Only add our OpenXR action map editor if OpenXR is enabled for our project
  43. OpenXREditorPlugin *openxr_plugin = memnew(OpenXREditorPlugin());
  44. EditorNode::get_singleton()->add_editor_plugin(openxr_plugin);
  45. }
  46. }
  47. #endif
  48. static OpenXRAPI *openxr_api = nullptr;
  49. static Ref<OpenXRInterface> openxr_interface;
  50. void initialize_openxr_module(ModuleInitializationLevel p_level) {
  51. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  52. // For now we create our openxr device here. If we merge it with openxr_interface we'll create that here soon.
  53. if (OpenXRAPI::openxr_is_enabled()) {
  54. openxr_api = memnew(OpenXRAPI);
  55. ERR_FAIL_NULL(openxr_api);
  56. if (!openxr_api->initialize(Main::get_rendering_driver_name())) {
  57. memdelete(openxr_api);
  58. openxr_api = nullptr;
  59. return;
  60. }
  61. }
  62. }
  63. if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
  64. GDREGISTER_CLASS(OpenXRInterface);
  65. GDREGISTER_CLASS(OpenXRAction);
  66. GDREGISTER_CLASS(OpenXRActionSet);
  67. GDREGISTER_CLASS(OpenXRActionMap);
  68. GDREGISTER_CLASS(OpenXRIPBinding);
  69. GDREGISTER_CLASS(OpenXRInteractionProfile);
  70. XRServer *xr_server = XRServer::get_singleton();
  71. if (xr_server) {
  72. openxr_interface.instantiate();
  73. xr_server->add_interface(openxr_interface);
  74. if (openxr_interface->initialize_on_startup()) {
  75. openxr_interface->initialize();
  76. }
  77. }
  78. #ifdef TOOLS_ENABLED
  79. EditorNode::add_init_callback(_editor_init);
  80. #endif
  81. }
  82. }
  83. void uninitialize_openxr_module(ModuleInitializationLevel p_level) {
  84. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
  85. return;
  86. }
  87. if (openxr_interface.is_valid()) {
  88. // uninitialize just in case
  89. if (openxr_interface->is_initialized()) {
  90. openxr_interface->uninitialize();
  91. }
  92. // unregister our interface from the XR server
  93. XRServer *xr_server = XRServer::get_singleton();
  94. if (xr_server) {
  95. if (xr_server->get_primary_interface() == openxr_interface) {
  96. xr_server->set_primary_interface(Ref<XRInterface>());
  97. }
  98. xr_server->remove_interface(openxr_interface);
  99. }
  100. // and release
  101. openxr_interface.unref();
  102. }
  103. if (openxr_api) {
  104. openxr_api->finish();
  105. memdelete(openxr_api);
  106. openxr_api = nullptr;
  107. }
  108. }