openxr_action_set.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**************************************************************************/
  2. /* openxr_action_set.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_action_set.h"
  31. void OpenXRActionSet::_bind_methods() {
  32. ClassDB::bind_method(D_METHOD("set_localized_name", "localized_name"), &OpenXRActionSet::set_localized_name);
  33. ClassDB::bind_method(D_METHOD("get_localized_name"), &OpenXRActionSet::get_localized_name);
  34. ADD_PROPERTY(PropertyInfo(Variant::STRING, "localized_name"), "set_localized_name", "get_localized_name");
  35. ClassDB::bind_method(D_METHOD("set_priority", "priority"), &OpenXRActionSet::set_priority);
  36. ClassDB::bind_method(D_METHOD("get_priority"), &OpenXRActionSet::get_priority);
  37. ADD_PROPERTY(PropertyInfo(Variant::INT, "priority"), "set_priority", "get_priority");
  38. ClassDB::bind_method(D_METHOD("get_action_count"), &OpenXRActionSet::get_action_count);
  39. ClassDB::bind_method(D_METHOD("set_actions", "actions"), &OpenXRActionSet::set_actions);
  40. ClassDB::bind_method(D_METHOD("get_actions"), &OpenXRActionSet::get_actions);
  41. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "actions", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRAction", PROPERTY_USAGE_NO_EDITOR), "set_actions", "get_actions");
  42. ClassDB::bind_method(D_METHOD("add_action", "action"), &OpenXRActionSet::add_action);
  43. ClassDB::bind_method(D_METHOD("remove_action", "action"), &OpenXRActionSet::remove_action);
  44. }
  45. Ref<OpenXRActionSet> OpenXRActionSet::new_action_set(const char *p_name, const char *p_localized_name, const int p_priority) {
  46. // This is a helper function to help build our default action sets
  47. Ref<OpenXRActionSet> action_set;
  48. action_set.instantiate();
  49. action_set->set_name(String(p_name));
  50. action_set->set_localized_name(p_localized_name);
  51. action_set->set_priority(p_priority);
  52. return action_set;
  53. }
  54. void OpenXRActionSet::set_localized_name(const String p_localized_name) {
  55. localized_name = p_localized_name;
  56. emit_changed();
  57. }
  58. String OpenXRActionSet::get_localized_name() const {
  59. return localized_name;
  60. }
  61. void OpenXRActionSet::set_priority(const int p_priority) {
  62. priority = p_priority;
  63. emit_changed();
  64. }
  65. int OpenXRActionSet::get_priority() const {
  66. return priority;
  67. }
  68. int OpenXRActionSet::get_action_count() const {
  69. return actions.size();
  70. }
  71. void OpenXRActionSet::clear_actions() {
  72. // Actions held within our action set should be released and destroyed but just in case they are still used some where else
  73. if (actions.size() == 0) {
  74. return;
  75. }
  76. for (int i = 0; i < actions.size(); i++) {
  77. Ref<OpenXRAction> action = actions[i];
  78. action->action_set = nullptr;
  79. }
  80. actions.clear();
  81. emit_changed();
  82. }
  83. void OpenXRActionSet::set_actions(Array p_actions) {
  84. // Any actions not retained in p_actions should be freed automatically, those held within our Array will have be relinked to our action set.
  85. clear_actions();
  86. for (int i = 0; i < p_actions.size(); i++) {
  87. // add them anew so we verify our action_set pointer
  88. add_action(p_actions[i]);
  89. }
  90. }
  91. Array OpenXRActionSet::get_actions() const {
  92. return actions;
  93. }
  94. Ref<OpenXRAction> OpenXRActionSet::get_action(const String p_name) const {
  95. for (int i = 0; i < actions.size(); i++) {
  96. Ref<OpenXRAction> action = actions[i];
  97. if (action->get_name() == p_name) {
  98. return action;
  99. }
  100. }
  101. return Ref<OpenXRAction>();
  102. }
  103. void OpenXRActionSet::add_action(Ref<OpenXRAction> p_action) {
  104. ERR_FAIL_COND(p_action.is_null());
  105. if (!actions.has(p_action)) {
  106. if (p_action->action_set && p_action->action_set != this) {
  107. // action should only relate to our action set
  108. p_action->action_set->remove_action(p_action);
  109. }
  110. p_action->action_set = this;
  111. actions.push_back(p_action);
  112. emit_changed();
  113. }
  114. }
  115. void OpenXRActionSet::remove_action(Ref<OpenXRAction> p_action) {
  116. int idx = actions.find(p_action);
  117. if (idx != -1) {
  118. actions.remove_at(idx);
  119. ERR_FAIL_COND_MSG(p_action->action_set != this, "Removing action that belongs to this action set but had incorrect action set pointer."); // this should never happen!
  120. p_action->action_set = nullptr;
  121. emit_changed();
  122. }
  123. }
  124. Ref<OpenXRAction> OpenXRActionSet::add_new_action(const char *p_name, const char *p_localized_name, const OpenXRAction::ActionType p_action_type, const char *p_toplevel_paths) {
  125. // This is a helper function to help build our default action sets
  126. Ref<OpenXRAction> new_action = OpenXRAction::new_action(p_name, p_localized_name, p_action_type, p_toplevel_paths);
  127. add_action(new_action);
  128. return new_action;
  129. }
  130. OpenXRActionSet::~OpenXRActionSet() {
  131. clear_actions();
  132. }