openxr_interaction_profile.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. ERR_FAIL_NULL(pmd);
  109. interaction_profile_path = pmd->check_profile_name(p_input_profile_path);
  110. emit_changed();
  111. }
  112. String OpenXRInteractionProfile::get_interaction_profile_path() const {
  113. return interaction_profile_path;
  114. }
  115. int OpenXRInteractionProfile::get_binding_count() const {
  116. return bindings.size();
  117. }
  118. Ref<OpenXRIPBinding> OpenXRInteractionProfile::get_binding(int p_index) const {
  119. ERR_FAIL_INDEX_V(p_index, bindings.size(), Ref<OpenXRIPBinding>());
  120. return bindings[p_index];
  121. }
  122. void OpenXRInteractionProfile::set_bindings(Array p_bindings) {
  123. // TODO add check here that our bindings don't contain duplicate actions
  124. bindings = p_bindings;
  125. emit_changed();
  126. }
  127. Array OpenXRInteractionProfile::get_bindings() const {
  128. return bindings;
  129. }
  130. Ref<OpenXRIPBinding> OpenXRInteractionProfile::get_binding_for_action(const Ref<OpenXRAction> p_action) const {
  131. for (int i = 0; i < bindings.size(); i++) {
  132. Ref<OpenXRIPBinding> binding = bindings[i];
  133. if (binding->get_action() == p_action) {
  134. return binding;
  135. }
  136. }
  137. return Ref<OpenXRIPBinding>();
  138. }
  139. void OpenXRInteractionProfile::add_binding(Ref<OpenXRIPBinding> p_binding) {
  140. ERR_FAIL_COND(p_binding.is_null());
  141. if (bindings.find(p_binding) == -1) {
  142. 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");
  143. bindings.push_back(p_binding);
  144. emit_changed();
  145. }
  146. }
  147. void OpenXRInteractionProfile::remove_binding(Ref<OpenXRIPBinding> p_binding) {
  148. int idx = bindings.find(p_binding);
  149. if (idx != -1) {
  150. bindings.remove_at(idx);
  151. emit_changed();
  152. }
  153. }
  154. void OpenXRInteractionProfile::add_new_binding(const Ref<OpenXRAction> p_action, const char *p_paths) {
  155. // This is a helper function to help build our default action sets
  156. Ref<OpenXRIPBinding> binding = OpenXRIPBinding::new_binding(p_action, p_paths);
  157. add_binding(binding);
  158. }
  159. void OpenXRInteractionProfile::remove_binding_for_action(const Ref<OpenXRAction> p_action) {
  160. for (int i = bindings.size() - 1; i >= 0; i--) {
  161. Ref<OpenXRIPBinding> binding = bindings[i];
  162. if (binding->get_action() == p_action) {
  163. remove_binding(binding);
  164. }
  165. }
  166. }
  167. bool OpenXRInteractionProfile::has_binding_for_action(const Ref<OpenXRAction> p_action) {
  168. for (int i = bindings.size() - 1; i >= 0; i--) {
  169. Ref<OpenXRIPBinding> binding = bindings[i];
  170. if (binding->get_action() == p_action) {
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. OpenXRInteractionProfile::~OpenXRInteractionProfile() {
  177. bindings.clear();
  178. }