Debian Codegraph @

modules/openxr/action_map/openxr_interaction_profile.cpp

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