openxr_interaction_profile.cpp 8.1 KB

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