summaryrefslogtreecommitdiff
path: root/modules/openxr/action_map/openxr_action_set.cpp
diff options
context:
space:
mode:
authorBastiaan Olij <mux213@gmail.com>2022-03-17 10:24:47 +1100
committerBastiaan Olij <mux213@gmail.com>2022-04-04 18:43:29 +1000
commit9b7b9de0e5e8bfe49ad9e02474eb9ddd5d7dacd5 (patch)
tree1b97984c4874521c4551c5d76ba03ef05b46abb3 /modules/openxr/action_map/openxr_action_set.cpp
parent7bb963efe9083662baa356f56a2d5c368b96a9a0 (diff)
Add action map editor for OpenXR
Diffstat (limited to 'modules/openxr/action_map/openxr_action_set.cpp')
-rw-r--r--modules/openxr/action_map/openxr_action_set.cpp44
1 files changed, 42 insertions, 2 deletions
diff --git a/modules/openxr/action_map/openxr_action_set.cpp b/modules/openxr/action_map/openxr_action_set.cpp
index 465a709b60..be45218300 100644
--- a/modules/openxr/action_map/openxr_action_set.cpp
+++ b/modules/openxr/action_map/openxr_action_set.cpp
@@ -39,6 +39,7 @@ void OpenXRActionSet::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_priority"), &OpenXRActionSet::get_priority);
ADD_PROPERTY(PropertyInfo(Variant::INT, "priority"), "set_priority", "get_priority");
+ ClassDB::bind_method(D_METHOD("get_action_count"), &OpenXRActionSet::get_action_count);
ClassDB::bind_method(D_METHOD("set_actions", "actions"), &OpenXRActionSet::set_actions);
ClassDB::bind_method(D_METHOD("get_actions"), &OpenXRActionSet::get_actions);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "actions", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRAction", PROPERTY_USAGE_NO_EDITOR), "set_actions", "get_actions");
@@ -75,18 +76,54 @@ int OpenXRActionSet::get_priority() const {
return priority;
}
+int OpenXRActionSet::get_action_count() const {
+ return actions.size();
+}
+
+void OpenXRActionSet::clear_actions() {
+ // Actions held within our action set should be released and destroyed but just in case they are still used some where else
+ for (int i = 0; i < actions.size(); i++) {
+ Ref<OpenXRAction> action = actions[i];
+ action->action_set = nullptr;
+ }
+ actions.clear();
+}
+
void OpenXRActionSet::set_actions(Array p_actions) {
- actions = p_actions;
+ // Any actions not retained in p_actions should be freed automatically, those held within our Array will have be relinked to our action set.
+ clear_actions();
+
+ for (int i = 0; i < p_actions.size(); i++) {
+ // add them anew so we verify our action_set pointer
+ add_action(p_actions[i]);
+ }
}
Array OpenXRActionSet::get_actions() const {
return actions;
}
+Ref<OpenXRAction> OpenXRActionSet::get_action(const String p_name) const {
+ for (int i = 0; i < actions.size(); i++) {
+ Ref<OpenXRAction> action = actions[i];
+ if (action->get_name() == p_name) {
+ return action;
+ }
+ }
+
+ return Ref<OpenXRAction>();
+}
+
void OpenXRActionSet::add_action(Ref<OpenXRAction> p_action) {
ERR_FAIL_COND(p_action.is_null());
if (actions.find(p_action) == -1) {
+ if (p_action->action_set && p_action->action_set != this) {
+ // action should only relate to our action set
+ p_action->action_set->remove_action(p_action);
+ }
+
+ p_action->action_set = this;
actions.push_back(p_action);
}
}
@@ -95,6 +132,9 @@ void OpenXRActionSet::remove_action(Ref<OpenXRAction> p_action) {
int idx = actions.find(p_action);
if (idx != -1) {
actions.remove_at(idx);
+
+ 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!
+ p_action->action_set = nullptr;
}
}
@@ -107,5 +147,5 @@ Ref<OpenXRAction> OpenXRActionSet::add_new_action(const char *p_name, const char
}
OpenXRActionSet::~OpenXRActionSet() {
- actions.clear();
+ clear_actions();
}