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