openxr_interaction_profile.cpp 17 KB

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