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