openxr_interaction_profile.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**************************************************************************/
  2. /* openxr_interaction_profile.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_interaction_profile.h"
  31. void OpenXRIPBinding::_bind_methods() {
  32. ClassDB::bind_method(D_METHOD("set_action", "action"), &OpenXRIPBinding::set_action);
  33. ClassDB::bind_method(D_METHOD("get_action"), &OpenXRIPBinding::get_action);
  34. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "action", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRAction"), "set_action", "get_action");
  35. ClassDB::bind_method(D_METHOD("set_binding_path", "binding_path"), &OpenXRIPBinding::set_binding_path);
  36. ClassDB::bind_method(D_METHOD("get_binding_path"), &OpenXRIPBinding::get_binding_path);
  37. ADD_PROPERTY(PropertyInfo(Variant::STRING, "binding_path"), "set_binding_path", "get_binding_path");
  38. // Deprecated
  39. #ifndef DISABLE_DEPRECATED
  40. ClassDB::bind_method(D_METHOD("set_paths", "paths"), &OpenXRIPBinding::set_paths);
  41. ClassDB::bind_method(D_METHOD("get_paths"), &OpenXRIPBinding::get_paths);
  42. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_paths", "get_paths");
  43. ClassDB::bind_method(D_METHOD("get_path_count"), &OpenXRIPBinding::get_path_count);
  44. ClassDB::bind_method(D_METHOD("has_path", "path"), &OpenXRIPBinding::has_path);
  45. ClassDB::bind_method(D_METHOD("add_path", "path"), &OpenXRIPBinding::add_path);
  46. ClassDB::bind_method(D_METHOD("remove_path", "path"), &OpenXRIPBinding::remove_path);
  47. #endif // DISABLE_DEPRECATED
  48. }
  49. Ref<OpenXRIPBinding> OpenXRIPBinding::new_binding(const Ref<OpenXRAction> p_action, const String &p_binding_path) {
  50. // This is a helper function to help build our default action sets
  51. Ref<OpenXRIPBinding> binding;
  52. binding.instantiate();
  53. binding->set_action(p_action);
  54. binding->set_binding_path(p_binding_path);
  55. return binding;
  56. }
  57. void OpenXRIPBinding::set_action(const Ref<OpenXRAction> p_action) {
  58. action = p_action;
  59. emit_changed();
  60. }
  61. Ref<OpenXRAction> OpenXRIPBinding::get_action() const {
  62. return action;
  63. }
  64. void OpenXRIPBinding::set_binding_path(const String &path) {
  65. binding_path = path;
  66. emit_changed();
  67. }
  68. String OpenXRIPBinding::get_binding_path() const {
  69. return binding_path;
  70. }
  71. #ifndef DISABLE_DEPRECATED
  72. void OpenXRIPBinding::set_paths(const PackedStringArray p_paths) { // Deprecated, but needed for loading old action maps.
  73. // Fallback logic, this should ONLY be called when loading older action maps.
  74. // We'll parse this momentarily and extract individual bindings.
  75. binding_path = "";
  76. for (const String &path : p_paths) {
  77. if (!binding_path.is_empty()) {
  78. binding_path += ",";
  79. }
  80. binding_path += path;
  81. }
  82. }
  83. PackedStringArray OpenXRIPBinding::get_paths() const { // Deprecated, but needed for converting old action maps.
  84. // Fallback logic, return an array.
  85. // If we just loaded an old action map from disc, this will be a comma separated list of actions.
  86. // Once parsed there should be only one path in our array.
  87. PackedStringArray paths = binding_path.split(",", false);
  88. return paths;
  89. }
  90. int OpenXRIPBinding::get_path_count() const { // Deprecated.
  91. // Fallback logic, we only have one entry.
  92. return binding_path.is_empty() ? 0 : 1;
  93. }
  94. bool OpenXRIPBinding::has_path(const String p_path) const { // Deprecated.
  95. // Fallback logic, return true if this is our path.
  96. return binding_path == p_path;
  97. }
  98. void OpenXRIPBinding::add_path(const String p_path) { // Deprecated.
  99. // Fallback logic, only assign first time this is called.
  100. if (binding_path != p_path) {
  101. ERR_FAIL_COND_MSG(!binding_path.is_empty(), "Method add_path has been deprecated. A binding path was already set, create separate binding resources for each path and use set_binding_path instead.");
  102. binding_path = p_path;
  103. emit_changed();
  104. }
  105. }
  106. void OpenXRIPBinding::remove_path(const String p_path) { // Deprecated.
  107. ERR_FAIL_COND_MSG(binding_path != p_path, "Method remove_path has been deprecated. Attempt at removing a different binding path, remove the correct binding record from the interaction profile instead.");
  108. // Fallback logic, clear if this is our path.
  109. binding_path = p_path;
  110. emit_changed();
  111. }
  112. #endif // DISABLE_DEPRECATED
  113. OpenXRIPBinding::~OpenXRIPBinding() {
  114. action.unref();
  115. }
  116. void OpenXRInteractionProfile::_bind_methods() {
  117. ClassDB::bind_method(D_METHOD("set_interaction_profile_path", "interaction_profile_path"), &OpenXRInteractionProfile::set_interaction_profile_path);
  118. ClassDB::bind_method(D_METHOD("get_interaction_profile_path"), &OpenXRInteractionProfile::get_interaction_profile_path);
  119. ADD_PROPERTY(PropertyInfo(Variant::STRING, "interaction_profile_path"), "set_interaction_profile_path", "get_interaction_profile_path");
  120. ClassDB::bind_method(D_METHOD("get_binding_count"), &OpenXRInteractionProfile::get_binding_count);
  121. ClassDB::bind_method(D_METHOD("get_binding", "index"), &OpenXRInteractionProfile::get_binding);
  122. ClassDB::bind_method(D_METHOD("set_bindings", "bindings"), &OpenXRInteractionProfile::set_bindings);
  123. ClassDB::bind_method(D_METHOD("get_bindings"), &OpenXRInteractionProfile::get_bindings);
  124. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "bindings", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRIPBinding", PROPERTY_USAGE_NO_EDITOR), "set_bindings", "get_bindings");
  125. }
  126. Ref<OpenXRInteractionProfile> OpenXRInteractionProfile::new_profile(const char *p_input_profile_path) {
  127. Ref<OpenXRInteractionProfile> profile;
  128. profile.instantiate();
  129. profile->set_interaction_profile_path(String(p_input_profile_path));
  130. return profile;
  131. }
  132. void OpenXRInteractionProfile::set_interaction_profile_path(const String p_input_profile_path) {
  133. OpenXRInteractionProfileMetadata *pmd = OpenXRInteractionProfileMetadata::get_singleton();
  134. if (pmd) {
  135. interaction_profile_path = pmd->check_profile_name(p_input_profile_path);
  136. } else {
  137. // OpenXR module not enabled, ignore checks.
  138. interaction_profile_path = p_input_profile_path;
  139. }
  140. emit_changed();
  141. }
  142. String OpenXRInteractionProfile::get_interaction_profile_path() const {
  143. return interaction_profile_path;
  144. }
  145. int OpenXRInteractionProfile::get_binding_count() const {
  146. return bindings.size();
  147. }
  148. Ref<OpenXRIPBinding> OpenXRInteractionProfile::get_binding(int p_index) const {
  149. ERR_FAIL_INDEX_V(p_index, bindings.size(), Ref<OpenXRIPBinding>());
  150. return bindings[p_index];
  151. }
  152. void OpenXRInteractionProfile::set_bindings(Array p_bindings) {
  153. bindings.clear();
  154. for (Ref<OpenXRIPBinding> binding : p_bindings) {
  155. String binding_path = binding->get_binding_path();
  156. if (binding_path.find_char(',') >= 0) {
  157. // Convert old binding approach to new...
  158. add_new_binding(binding->get_action(), binding_path);
  159. } else {
  160. add_binding(binding);
  161. }
  162. }
  163. emit_changed();
  164. }
  165. Array OpenXRInteractionProfile::get_bindings() const {
  166. return bindings;
  167. }
  168. Ref<OpenXRIPBinding> OpenXRInteractionProfile::find_binding(const Ref<OpenXRAction> p_action, const String &p_binding_path) const {
  169. for (Ref<OpenXRIPBinding> binding : bindings) {
  170. if (binding->get_action() == p_action && binding->get_binding_path() == p_binding_path) {
  171. return binding;
  172. }
  173. }
  174. return Ref<OpenXRIPBinding>();
  175. }
  176. Vector<Ref<OpenXRIPBinding>> OpenXRInteractionProfile::get_bindings_for_action(const Ref<OpenXRAction> p_action) const {
  177. Vector<Ref<OpenXRIPBinding>> ret_bindings;
  178. for (Ref<OpenXRIPBinding> binding : bindings) {
  179. if (binding->get_action() == p_action) {
  180. ret_bindings.push_back(binding);
  181. }
  182. }
  183. return ret_bindings;
  184. }
  185. void OpenXRInteractionProfile::add_binding(Ref<OpenXRIPBinding> p_binding) {
  186. ERR_FAIL_COND(p_binding.is_null());
  187. if (!bindings.has(p_binding)) {
  188. ERR_FAIL_COND_MSG(find_binding(p_binding->get_action(), p_binding->get_binding_path()).is_valid(), "There is already a binding for this action and binding path in this interaction profile.");
  189. bindings.push_back(p_binding);
  190. emit_changed();
  191. }
  192. }
  193. void OpenXRInteractionProfile::remove_binding(Ref<OpenXRIPBinding> p_binding) {
  194. int idx = bindings.find(p_binding);
  195. if (idx != -1) {
  196. bindings.remove_at(idx);
  197. emit_changed();
  198. }
  199. }
  200. void OpenXRInteractionProfile::add_new_binding(const Ref<OpenXRAction> p_action, const String &p_paths) {
  201. // This is a helper function to help build our default action sets
  202. PackedStringArray paths = p_paths.split(",", false);
  203. for (const String &path : paths) {
  204. Ref<OpenXRIPBinding> binding = OpenXRIPBinding::new_binding(p_action, path);
  205. add_binding(binding);
  206. }
  207. }
  208. void OpenXRInteractionProfile::remove_binding_for_action(const Ref<OpenXRAction> p_action) {
  209. for (int i = bindings.size() - 1; i >= 0; i--) {
  210. Ref<OpenXRIPBinding> binding = bindings[i];
  211. if (binding->get_action() == p_action) {
  212. remove_binding(binding);
  213. }
  214. }
  215. }
  216. bool OpenXRInteractionProfile::has_binding_for_action(const Ref<OpenXRAction> p_action) {
  217. for (int i = bindings.size() - 1; i >= 0; i--) {
  218. Ref<OpenXRIPBinding> binding = bindings[i];
  219. if (binding->get_action() == p_action) {
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. OpenXRInteractionProfile::~OpenXRInteractionProfile() {
  226. bindings.clear();
  227. }