openxr_interaction_profile.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. ClassDB::bind_method(D_METHOD("get_binding_modifier_count"), &OpenXRIPBinding::get_binding_modifier_count);
  39. ClassDB::bind_method(D_METHOD("get_binding_modifier", "index"), &OpenXRIPBinding::get_binding_modifier);
  40. ClassDB::bind_method(D_METHOD("set_binding_modifiers", "binding_modifiers"), &OpenXRIPBinding::set_binding_modifiers);
  41. ClassDB::bind_method(D_METHOD("get_binding_modifiers"), &OpenXRIPBinding::get_binding_modifiers);
  42. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "binding_modifiers", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRActionBindingModifier", PROPERTY_USAGE_NO_EDITOR), "set_binding_modifiers", "get_binding_modifiers");
  43. // Deprecated
  44. #ifndef DISABLE_DEPRECATED
  45. ClassDB::bind_method(D_METHOD("set_paths", "paths"), &OpenXRIPBinding::set_paths);
  46. ClassDB::bind_method(D_METHOD("get_paths"), &OpenXRIPBinding::get_paths);
  47. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_paths", "get_paths");
  48. ClassDB::bind_method(D_METHOD("get_path_count"), &OpenXRIPBinding::get_path_count);
  49. ClassDB::bind_method(D_METHOD("has_path", "path"), &OpenXRIPBinding::has_path);
  50. ClassDB::bind_method(D_METHOD("add_path", "path"), &OpenXRIPBinding::add_path);
  51. ClassDB::bind_method(D_METHOD("remove_path", "path"), &OpenXRIPBinding::remove_path);
  52. #endif // DISABLE_DEPRECATED
  53. }
  54. Ref<OpenXRIPBinding> OpenXRIPBinding::new_binding(const Ref<OpenXRAction> p_action, const String &p_binding_path) {
  55. // This is a helper function to help build our default action sets
  56. Ref<OpenXRIPBinding> binding;
  57. binding.instantiate();
  58. binding->set_action(p_action);
  59. binding->set_binding_path(p_binding_path);
  60. return binding;
  61. }
  62. void OpenXRIPBinding::set_action(const Ref<OpenXRAction> &p_action) {
  63. action = p_action;
  64. emit_changed();
  65. }
  66. Ref<OpenXRAction> OpenXRIPBinding::get_action() const {
  67. return action;
  68. }
  69. void OpenXRIPBinding::set_binding_path(const String &path) {
  70. binding_path = path;
  71. emit_changed();
  72. }
  73. String OpenXRIPBinding::get_binding_path() const {
  74. return binding_path;
  75. }
  76. int OpenXRIPBinding::get_binding_modifier_count() const {
  77. return binding_modifiers.size();
  78. }
  79. Ref<OpenXRActionBindingModifier> OpenXRIPBinding::get_binding_modifier(int p_index) const {
  80. ERR_FAIL_INDEX_V(p_index, binding_modifiers.size(), nullptr);
  81. return binding_modifiers[p_index];
  82. }
  83. void OpenXRIPBinding::clear_binding_modifiers() {
  84. // Binding modifiers held within our interaction profile set should be released and destroyed but just in case they are still used some where else.
  85. if (binding_modifiers.is_empty()) {
  86. return;
  87. }
  88. for (int i = 0; i < binding_modifiers.size(); i++) {
  89. Ref<OpenXRActionBindingModifier> binding_modifier = binding_modifiers[i];
  90. binding_modifier->ip_binding = nullptr;
  91. }
  92. binding_modifiers.clear();
  93. emit_changed();
  94. }
  95. void OpenXRIPBinding::set_binding_modifiers(const Array &p_binding_modifiers) {
  96. clear_binding_modifiers();
  97. // Any binding modifier not retained in p_binding_modifiers should be freed automatically, those held within our Array will have be relinked to our interaction profile.
  98. for (int i = 0; i < p_binding_modifiers.size(); i++) {
  99. // Add them anew so we verify our binding modifier pointer.
  100. add_binding_modifier(p_binding_modifiers[i]);
  101. }
  102. }
  103. Array OpenXRIPBinding::get_binding_modifiers() const {
  104. Array ret;
  105. for (const Ref<OpenXRActionBindingModifier> &binding_modifier : binding_modifiers) {
  106. ret.push_back(binding_modifier);
  107. }
  108. return ret;
  109. }
  110. void OpenXRIPBinding::add_binding_modifier(const Ref<OpenXRActionBindingModifier> &p_binding_modifier) {
  111. ERR_FAIL_COND(p_binding_modifier.is_null());
  112. if (!binding_modifiers.has(p_binding_modifier)) {
  113. if (p_binding_modifier->ip_binding && p_binding_modifier->ip_binding != this) {
  114. // Binding modifier should only relate to our binding.
  115. p_binding_modifier->ip_binding->remove_binding_modifier(p_binding_modifier);
  116. }
  117. p_binding_modifier->ip_binding = this;
  118. binding_modifiers.push_back(p_binding_modifier);
  119. emit_changed();
  120. }
  121. }
  122. void OpenXRIPBinding::remove_binding_modifier(const Ref<OpenXRActionBindingModifier> &p_binding_modifier) {
  123. int idx = binding_modifiers.find(p_binding_modifier);
  124. if (idx != -1) {
  125. binding_modifiers.remove_at(idx);
  126. ERR_FAIL_COND_MSG(p_binding_modifier->ip_binding != this, "Removing binding modifier that belongs to this binding but had incorrect binding pointer."); // This should never happen!
  127. p_binding_modifier->ip_binding = nullptr;
  128. emit_changed();
  129. }
  130. }
  131. #ifndef DISABLE_DEPRECATED
  132. void OpenXRIPBinding::set_paths(const PackedStringArray p_paths) { // Deprecated, but needed for loading old action maps.
  133. // Fallback logic, this should ONLY be called when loading older action maps.
  134. // We'll parse this momentarily and extract individual bindings.
  135. binding_path = "";
  136. for (const String &path : p_paths) {
  137. if (!binding_path.is_empty()) {
  138. binding_path += ",";
  139. }
  140. binding_path += path;
  141. }
  142. }
  143. PackedStringArray OpenXRIPBinding::get_paths() const { // Deprecated, but needed for converting old action maps.
  144. // Fallback logic, return an array.
  145. // If we just loaded an old action map from disc, this will be a comma separated list of actions.
  146. // Once parsed there should be only one path in our array.
  147. PackedStringArray paths = binding_path.split(",", false);
  148. return paths;
  149. }
  150. int OpenXRIPBinding::get_path_count() const { // Deprecated.
  151. // Fallback logic, we only have one entry.
  152. return binding_path.is_empty() ? 0 : 1;
  153. }
  154. bool OpenXRIPBinding::has_path(const String p_path) const { // Deprecated.
  155. // Fallback logic, return true if this is our path.
  156. return binding_path == p_path;
  157. }
  158. void OpenXRIPBinding::add_path(const String p_path) { // Deprecated.
  159. // Fallback logic, only assign first time this is called.
  160. if (binding_path != p_path) {
  161. 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.");
  162. binding_path = p_path;
  163. emit_changed();
  164. }
  165. }
  166. void OpenXRIPBinding::remove_path(const String p_path) { // Deprecated.
  167. 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.");
  168. // Fallback logic, clear if this is our path.
  169. binding_path = p_path;
  170. emit_changed();
  171. }
  172. #endif // DISABLE_DEPRECATED
  173. OpenXRIPBinding::~OpenXRIPBinding() {
  174. action.unref();
  175. }
  176. void OpenXRInteractionProfile::_bind_methods() {
  177. ClassDB::bind_method(D_METHOD("set_interaction_profile_path", "interaction_profile_path"), &OpenXRInteractionProfile::set_interaction_profile_path);
  178. ClassDB::bind_method(D_METHOD("get_interaction_profile_path"), &OpenXRInteractionProfile::get_interaction_profile_path);
  179. ADD_PROPERTY(PropertyInfo(Variant::STRING, "interaction_profile_path"), "set_interaction_profile_path", "get_interaction_profile_path");
  180. ClassDB::bind_method(D_METHOD("get_binding_count"), &OpenXRInteractionProfile::get_binding_count);
  181. ClassDB::bind_method(D_METHOD("get_binding", "index"), &OpenXRInteractionProfile::get_binding);
  182. ClassDB::bind_method(D_METHOD("set_bindings", "bindings"), &OpenXRInteractionProfile::set_bindings);
  183. ClassDB::bind_method(D_METHOD("get_bindings"), &OpenXRInteractionProfile::get_bindings);
  184. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "bindings", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRIPBinding", PROPERTY_USAGE_NO_EDITOR), "set_bindings", "get_bindings");
  185. ClassDB::bind_method(D_METHOD("get_binding_modifier_count"), &OpenXRInteractionProfile::get_binding_modifier_count);
  186. ClassDB::bind_method(D_METHOD("get_binding_modifier", "index"), &OpenXRInteractionProfile::get_binding_modifier);
  187. ClassDB::bind_method(D_METHOD("set_binding_modifiers", "binding_modifiers"), &OpenXRInteractionProfile::set_binding_modifiers);
  188. ClassDB::bind_method(D_METHOD("get_binding_modifiers"), &OpenXRInteractionProfile::get_binding_modifiers);
  189. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "binding_modifiers", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRIPBindingModifier", PROPERTY_USAGE_NO_EDITOR), "set_binding_modifiers", "get_binding_modifiers");
  190. }
  191. Ref<OpenXRInteractionProfile> OpenXRInteractionProfile::new_profile(const char *p_input_profile_path) {
  192. Ref<OpenXRInteractionProfile> profile;
  193. profile.instantiate();
  194. profile->set_interaction_profile_path(String(p_input_profile_path));
  195. return profile;
  196. }
  197. void OpenXRInteractionProfile::set_interaction_profile_path(const String p_input_profile_path) {
  198. OpenXRInteractionProfileMetadata *pmd = OpenXRInteractionProfileMetadata::get_singleton();
  199. if (pmd) {
  200. interaction_profile_path = pmd->check_profile_name(p_input_profile_path);
  201. } else {
  202. // OpenXR module not enabled, ignore checks.
  203. interaction_profile_path = p_input_profile_path;
  204. }
  205. emit_changed();
  206. }
  207. String OpenXRInteractionProfile::get_interaction_profile_path() const {
  208. return interaction_profile_path;
  209. }
  210. int OpenXRInteractionProfile::get_binding_count() const {
  211. return bindings.size();
  212. }
  213. Ref<OpenXRIPBinding> OpenXRInteractionProfile::get_binding(int p_index) const {
  214. ERR_FAIL_INDEX_V(p_index, bindings.size(), Ref<OpenXRIPBinding>());
  215. return bindings[p_index];
  216. }
  217. void OpenXRInteractionProfile::set_bindings(const Array &p_bindings) {
  218. bindings.clear();
  219. for (Ref<OpenXRIPBinding> binding : p_bindings) {
  220. String binding_path = binding->get_binding_path();
  221. if (binding_path.find_char(',') >= 0) {
  222. // Convert old binding approach to new...
  223. add_new_binding(binding->get_action(), binding_path);
  224. } else {
  225. add_binding(binding);
  226. }
  227. }
  228. emit_changed();
  229. }
  230. Array OpenXRInteractionProfile::get_bindings() const {
  231. return bindings;
  232. }
  233. Ref<OpenXRIPBinding> OpenXRInteractionProfile::find_binding(const Ref<OpenXRAction> &p_action, const String &p_binding_path) const {
  234. for (Ref<OpenXRIPBinding> binding : bindings) {
  235. if (binding->get_action() == p_action && binding->get_binding_path() == p_binding_path) {
  236. return binding;
  237. }
  238. }
  239. return Ref<OpenXRIPBinding>();
  240. }
  241. Vector<Ref<OpenXRIPBinding>> OpenXRInteractionProfile::get_bindings_for_action(const Ref<OpenXRAction> &p_action) const {
  242. Vector<Ref<OpenXRIPBinding>> ret_bindings;
  243. for (Ref<OpenXRIPBinding> binding : bindings) {
  244. if (binding->get_action() == p_action) {
  245. ret_bindings.push_back(binding);
  246. }
  247. }
  248. return ret_bindings;
  249. }
  250. void OpenXRInteractionProfile::add_binding(const Ref<OpenXRIPBinding> &p_binding) {
  251. ERR_FAIL_COND(p_binding.is_null());
  252. if (!bindings.has(p_binding)) {
  253. 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.");
  254. bindings.push_back(p_binding);
  255. emit_changed();
  256. }
  257. }
  258. void OpenXRInteractionProfile::remove_binding(const Ref<OpenXRIPBinding> &p_binding) {
  259. int idx = bindings.find(p_binding);
  260. if (idx != -1) {
  261. bindings.remove_at(idx);
  262. emit_changed();
  263. }
  264. }
  265. void OpenXRInteractionProfile::add_new_binding(const Ref<OpenXRAction> &p_action, const String &p_paths) {
  266. // This is a helper function to help build our default action sets
  267. PackedStringArray paths = p_paths.split(",", false);
  268. for (const String &path : paths) {
  269. Ref<OpenXRIPBinding> binding = OpenXRIPBinding::new_binding(p_action, path);
  270. add_binding(binding);
  271. }
  272. }
  273. void OpenXRInteractionProfile::remove_binding_for_action(const Ref<OpenXRAction> &p_action) {
  274. for (int i = bindings.size() - 1; i >= 0; i--) {
  275. Ref<OpenXRIPBinding> binding = bindings[i];
  276. if (binding->get_action() == p_action) {
  277. remove_binding(binding);
  278. }
  279. }
  280. }
  281. bool OpenXRInteractionProfile::has_binding_for_action(const Ref<OpenXRAction> &p_action) {
  282. for (int i = bindings.size() - 1; i >= 0; i--) {
  283. Ref<OpenXRIPBinding> binding = bindings[i];
  284. if (binding->get_action() == p_action) {
  285. return true;
  286. }
  287. }
  288. return false;
  289. }
  290. int OpenXRInteractionProfile::get_binding_modifier_count() const {
  291. return binding_modifiers.size();
  292. }
  293. Ref<OpenXRIPBindingModifier> OpenXRInteractionProfile::get_binding_modifier(int p_index) const {
  294. ERR_FAIL_INDEX_V(p_index, binding_modifiers.size(), nullptr);
  295. return binding_modifiers[p_index];
  296. }
  297. void OpenXRInteractionProfile::clear_binding_modifiers() {
  298. // Binding modifiers held within our interaction profile set should be released and destroyed but just in case they are still used some where else.
  299. if (binding_modifiers.is_empty()) {
  300. return;
  301. }
  302. for (int i = 0; i < binding_modifiers.size(); i++) {
  303. Ref<OpenXRIPBindingModifier> binding_modifier = binding_modifiers[i];
  304. binding_modifier->interaction_profile = nullptr;
  305. }
  306. binding_modifiers.clear();
  307. emit_changed();
  308. }
  309. void OpenXRInteractionProfile::set_binding_modifiers(const Array &p_binding_modifiers) {
  310. clear_binding_modifiers();
  311. // Any binding modifier not retained in p_binding_modifiers should be freed automatically, those held within our Array will have be relinked to our interaction profile.
  312. for (int i = 0; i < p_binding_modifiers.size(); i++) {
  313. // Add them anew so we verify our binding modifier pointer.
  314. add_binding_modifier(p_binding_modifiers[i]);
  315. }
  316. }
  317. Array OpenXRInteractionProfile::get_binding_modifiers() const {
  318. Array ret;
  319. for (const Ref<OpenXRIPBindingModifier> &binding_modifier : binding_modifiers) {
  320. ret.push_back(binding_modifier);
  321. }
  322. return ret;
  323. }
  324. void OpenXRInteractionProfile::add_binding_modifier(const Ref<OpenXRIPBindingModifier> &p_binding_modifier) {
  325. ERR_FAIL_COND(p_binding_modifier.is_null());
  326. if (!binding_modifiers.has(p_binding_modifier)) {
  327. if (p_binding_modifier->interaction_profile && p_binding_modifier->interaction_profile != this) {
  328. // Binding modifier should only relate to our interaction profile.
  329. p_binding_modifier->interaction_profile->remove_binding_modifier(p_binding_modifier);
  330. }
  331. p_binding_modifier->interaction_profile = this;
  332. binding_modifiers.push_back(p_binding_modifier);
  333. emit_changed();
  334. }
  335. }
  336. void OpenXRInteractionProfile::remove_binding_modifier(const Ref<OpenXRIPBindingModifier> &p_binding_modifier) {
  337. int idx = binding_modifiers.find(p_binding_modifier);
  338. if (idx != -1) {
  339. binding_modifiers.remove_at(idx);
  340. ERR_FAIL_COND_MSG(p_binding_modifier->interaction_profile != this, "Removing binding modifier that belongs to this interaction profile but had incorrect interaction profile pointer."); // This should never happen!
  341. p_binding_modifier->interaction_profile = nullptr;
  342. emit_changed();
  343. }
  344. }
  345. OpenXRInteractionProfile::~OpenXRInteractionProfile() {
  346. bindings.clear();
  347. clear_binding_modifiers();
  348. }