openxr_interaction_profile.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. }
  54. Ref<OpenXRAction> OpenXRIPBinding::get_action() const {
  55. return action;
  56. }
  57. int OpenXRIPBinding::get_path_count() const {
  58. return paths.size();
  59. }
  60. void OpenXRIPBinding::set_paths(const PackedStringArray p_paths) {
  61. paths = p_paths;
  62. }
  63. PackedStringArray OpenXRIPBinding::get_paths() const {
  64. return paths;
  65. }
  66. void OpenXRIPBinding::parse_paths(const String p_paths) {
  67. paths = p_paths.split(",", false);
  68. }
  69. bool OpenXRIPBinding::has_path(const String p_path) const {
  70. return paths.has(p_path);
  71. }
  72. void OpenXRIPBinding::add_path(const String p_path) {
  73. if (!paths.has(p_path)) {
  74. paths.push_back(p_path);
  75. }
  76. }
  77. void OpenXRIPBinding::remove_path(const String p_path) {
  78. if (paths.has(p_path)) {
  79. paths.erase(p_path);
  80. }
  81. }
  82. OpenXRIPBinding::~OpenXRIPBinding() {
  83. action.unref();
  84. }
  85. void OpenXRInteractionProfile::_bind_methods() {
  86. ClassDB::bind_method(D_METHOD("set_interaction_profile_path", "interaction_profile_path"), &OpenXRInteractionProfile::set_interaction_profile_path);
  87. ClassDB::bind_method(D_METHOD("get_interaction_profile_path"), &OpenXRInteractionProfile::get_interaction_profile_path);
  88. ADD_PROPERTY(PropertyInfo(Variant::STRING, "interaction_profile_path"), "set_interaction_profile_path", "get_interaction_profile_path");
  89. ClassDB::bind_method(D_METHOD("get_binding_count"), &OpenXRInteractionProfile::get_binding_count);
  90. ClassDB::bind_method(D_METHOD("get_binding", "index"), &OpenXRInteractionProfile::get_binding);
  91. ClassDB::bind_method(D_METHOD("set_bindings", "bindings"), &OpenXRInteractionProfile::set_bindings);
  92. ClassDB::bind_method(D_METHOD("get_bindings"), &OpenXRInteractionProfile::get_bindings);
  93. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "bindings", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRIPBinding", PROPERTY_USAGE_NO_EDITOR), "set_bindings", "get_bindings");
  94. }
  95. Ref<OpenXRInteractionProfile> OpenXRInteractionProfile::new_profile(const char *p_input_profile_path) {
  96. Ref<OpenXRInteractionProfile> profile;
  97. profile.instantiate();
  98. profile->set_interaction_profile_path(String(p_input_profile_path));
  99. return profile;
  100. }
  101. void OpenXRInteractionProfile::set_interaction_profile_path(const String p_input_profile_path) {
  102. interaction_profile_path = p_input_profile_path;
  103. }
  104. String OpenXRInteractionProfile::get_interaction_profile_path() const {
  105. return interaction_profile_path;
  106. }
  107. int OpenXRInteractionProfile::get_binding_count() const {
  108. return bindings.size();
  109. }
  110. Ref<OpenXRIPBinding> OpenXRInteractionProfile::get_binding(int p_index) const {
  111. ERR_FAIL_INDEX_V(p_index, bindings.size(), Ref<OpenXRIPBinding>());
  112. return bindings[p_index];
  113. }
  114. void OpenXRInteractionProfile::set_bindings(Array p_bindings) {
  115. bindings = p_bindings;
  116. // TODO add check here that our bindings don't contain duplicate actions
  117. }
  118. Array OpenXRInteractionProfile::get_bindings() const {
  119. return bindings;
  120. }
  121. Ref<OpenXRIPBinding> OpenXRInteractionProfile::get_binding_for_action(const Ref<OpenXRAction> p_action) const {
  122. for (int i = 0; i < bindings.size(); i++) {
  123. Ref<OpenXRIPBinding> binding = bindings[i];
  124. if (binding->get_action() == p_action) {
  125. return binding;
  126. }
  127. }
  128. return Ref<OpenXRIPBinding>();
  129. }
  130. void OpenXRInteractionProfile::add_binding(Ref<OpenXRIPBinding> p_binding) {
  131. ERR_FAIL_COND(p_binding.is_null());
  132. if (bindings.find(p_binding) == -1) {
  133. 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");
  134. bindings.push_back(p_binding);
  135. }
  136. }
  137. void OpenXRInteractionProfile::remove_binding(Ref<OpenXRIPBinding> p_binding) {
  138. int idx = bindings.find(p_binding);
  139. if (idx != -1) {
  140. bindings.remove_at(idx);
  141. }
  142. }
  143. void OpenXRInteractionProfile::add_new_binding(const Ref<OpenXRAction> p_action, const char *p_paths) {
  144. // This is a helper function to help build our default action sets
  145. Ref<OpenXRIPBinding> binding = OpenXRIPBinding::new_binding(p_action, p_paths);
  146. add_binding(binding);
  147. }
  148. void OpenXRInteractionProfile::remove_binding_for_action(const Ref<OpenXRAction> p_action) {
  149. for (int i = bindings.size() - 1; i >= 0; i--) {
  150. Ref<OpenXRIPBinding> binding = bindings[i];
  151. if (binding->get_action() == p_action) {
  152. remove_binding(binding);
  153. }
  154. }
  155. }
  156. OpenXRInteractionProfile::~OpenXRInteractionProfile() {
  157. bindings.clear();
  158. }