openxr_interaction_profile.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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("get_path_count"), &OpenXRIPBinding::get_path_count);
  36. ClassDB::bind_method(D_METHOD("set_paths", "paths"), &OpenXRIPBinding::set_paths);
  37. ClassDB::bind_method(D_METHOD("get_paths"), &OpenXRIPBinding::get_paths);
  38. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths"), "set_paths", "get_paths");
  39. ClassDB::bind_method(D_METHOD("has_path", "path"), &OpenXRIPBinding::has_path);
  40. ClassDB::bind_method(D_METHOD("add_path", "path"), &OpenXRIPBinding::add_path);
  41. ClassDB::bind_method(D_METHOD("remove_path", "path"), &OpenXRIPBinding::remove_path);
  42. }
  43. Ref<OpenXRIPBinding> OpenXRIPBinding::new_binding(const Ref<OpenXRAction> p_action, const char *p_paths) {
  44. // This is a helper function to help build our default action sets
  45. Ref<OpenXRIPBinding> binding;
  46. binding.instantiate();
  47. binding->set_action(p_action);
  48. binding->parse_paths(String(p_paths));
  49. return binding;
  50. }
  51. void OpenXRIPBinding::set_action(const Ref<OpenXRAction> p_action) {
  52. action = p_action;
  53. emit_changed();
  54. }
  55. Ref<OpenXRAction> OpenXRIPBinding::get_action() const {
  56. return action;
  57. }
  58. int OpenXRIPBinding::get_path_count() const {
  59. return paths.size();
  60. }
  61. void OpenXRIPBinding::set_paths(const PackedStringArray p_paths) {
  62. paths = p_paths;
  63. emit_changed();
  64. }
  65. PackedStringArray OpenXRIPBinding::get_paths() const {
  66. return paths;
  67. }
  68. void OpenXRIPBinding::parse_paths(const String p_paths) {
  69. paths = p_paths.split(",", false);
  70. emit_changed();
  71. }
  72. bool OpenXRIPBinding::has_path(const String p_path) const {
  73. return paths.has(p_path);
  74. }
  75. void OpenXRIPBinding::add_path(const String p_path) {
  76. if (!paths.has(p_path)) {
  77. paths.push_back(p_path);
  78. emit_changed();
  79. }
  80. }
  81. void OpenXRIPBinding::remove_path(const String p_path) {
  82. if (paths.has(p_path)) {
  83. paths.erase(p_path);
  84. emit_changed();
  85. }
  86. }
  87. OpenXRIPBinding::~OpenXRIPBinding() {
  88. action.unref();
  89. }
  90. void OpenXRInteractionProfile::_bind_methods() {
  91. ClassDB::bind_method(D_METHOD("set_interaction_profile_path", "interaction_profile_path"), &OpenXRInteractionProfile::set_interaction_profile_path);
  92. ClassDB::bind_method(D_METHOD("get_interaction_profile_path"), &OpenXRInteractionProfile::get_interaction_profile_path);
  93. ADD_PROPERTY(PropertyInfo(Variant::STRING, "interaction_profile_path"), "set_interaction_profile_path", "get_interaction_profile_path");
  94. ClassDB::bind_method(D_METHOD("get_binding_count"), &OpenXRInteractionProfile::get_binding_count);
  95. ClassDB::bind_method(D_METHOD("get_binding", "index"), &OpenXRInteractionProfile::get_binding);
  96. ClassDB::bind_method(D_METHOD("set_bindings", "bindings"), &OpenXRInteractionProfile::set_bindings);
  97. ClassDB::bind_method(D_METHOD("get_bindings"), &OpenXRInteractionProfile::get_bindings);
  98. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "bindings", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRIPBinding", PROPERTY_USAGE_NO_EDITOR), "set_bindings", "get_bindings");
  99. }
  100. Ref<OpenXRInteractionProfile> OpenXRInteractionProfile::new_profile(const char *p_input_profile_path) {
  101. Ref<OpenXRInteractionProfile> profile;
  102. profile.instantiate();
  103. profile->set_interaction_profile_path(String(p_input_profile_path));
  104. return profile;
  105. }
  106. void OpenXRInteractionProfile::set_interaction_profile_path(const String p_input_profile_path) {
  107. OpenXRInteractionProfileMetadata *pmd = OpenXRInteractionProfileMetadata::get_singleton();
  108. if (pmd) {
  109. interaction_profile_path = pmd->check_profile_name(p_input_profile_path);
  110. } else {
  111. // OpenXR module not enabled, ignore checks.
  112. interaction_profile_path = p_input_profile_path;
  113. }
  114. emit_changed();
  115. }
  116. String OpenXRInteractionProfile::get_interaction_profile_path() const {
  117. return interaction_profile_path;
  118. }
  119. int OpenXRInteractionProfile::get_binding_count() const {
  120. return bindings.size();
  121. }
  122. Ref<OpenXRIPBinding> OpenXRInteractionProfile::get_binding(int p_index) const {
  123. ERR_FAIL_INDEX_V(p_index, bindings.size(), Ref<OpenXRIPBinding>());
  124. return bindings[p_index];
  125. }
  126. void OpenXRInteractionProfile::set_bindings(Array p_bindings) {
  127. // TODO add check here that our bindings don't contain duplicate actions
  128. bindings = p_bindings;
  129. emit_changed();
  130. }
  131. Array OpenXRInteractionProfile::get_bindings() const {
  132. return bindings;
  133. }
  134. Ref<OpenXRIPBinding> OpenXRInteractionProfile::get_binding_for_action(const Ref<OpenXRAction> p_action) const {
  135. for (int i = 0; i < bindings.size(); i++) {
  136. Ref<OpenXRIPBinding> binding = bindings[i];
  137. if (binding->get_action() == p_action) {
  138. return binding;
  139. }
  140. }
  141. return Ref<OpenXRIPBinding>();
  142. }
  143. void OpenXRInteractionProfile::add_binding(Ref<OpenXRIPBinding> p_binding) {
  144. ERR_FAIL_COND(p_binding.is_null());
  145. if (!bindings.has(p_binding)) {
  146. ERR_FAIL_COND_MSG(get_binding_for_action(p_binding->get_action()).is_valid(), "There is already a binding for this action in this interaction profile");
  147. bindings.push_back(p_binding);
  148. emit_changed();
  149. }
  150. }
  151. void OpenXRInteractionProfile::remove_binding(Ref<OpenXRIPBinding> p_binding) {
  152. int idx = bindings.find(p_binding);
  153. if (idx != -1) {
  154. bindings.remove_at(idx);
  155. emit_changed();
  156. }
  157. }
  158. void OpenXRInteractionProfile::add_new_binding(const Ref<OpenXRAction> p_action, const char *p_paths) {
  159. // This is a helper function to help build our default action sets
  160. Ref<OpenXRIPBinding> binding = OpenXRIPBinding::new_binding(p_action, p_paths);
  161. add_binding(binding);
  162. }
  163. void OpenXRInteractionProfile::remove_binding_for_action(const Ref<OpenXRAction> p_action) {
  164. for (int i = bindings.size() - 1; i >= 0; i--) {
  165. Ref<OpenXRIPBinding> binding = bindings[i];
  166. if (binding->get_action() == p_action) {
  167. remove_binding(binding);
  168. }
  169. }
  170. }
  171. bool OpenXRInteractionProfile::has_binding_for_action(const Ref<OpenXRAction> p_action) {
  172. for (int i = bindings.size() - 1; i >= 0; i--) {
  173. Ref<OpenXRIPBinding> binding = bindings[i];
  174. if (binding->get_action() == p_action) {
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. OpenXRInteractionProfile::~OpenXRInteractionProfile() {
  181. bindings.clear();
  182. }