Blender V5.3
wm_xr_actionmap.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#include <cmath>
14#include <cstring>
15
16#include "BKE_idprop.hh"
17
18#include "BLI_listbase.hh"
19#include "BLI_string_utf8.hh"
20
21#include "MEM_guardedalloc.h"
22
23#include "WM_api.hh"
24#include "WM_types.hh"
25
26#include "wm_xr_intern.hh"
27
28namespace blender {
29
30#define WM_XR_ACTIONMAP_STR_DEFAULT "actionmap"
31#define WM_XR_ACTIONMAP_ITEM_STR_DEFAULT "action"
32#define WM_XR_ACTIONMAP_BINDING_STR_DEFAULT "binding"
33
34/* -------------------------------------------------------------------- */
39
41 const char *name,
42 bool replace_existing)
43{
45 if (amb_prev && replace_existing) {
46 return amb_prev;
47 }
48
49 XrActionMapBinding *amb = MEM_new<XrActionMapBinding>(__func__);
50 STRNCPY_UTF8(amb->name, name);
51 if (amb_prev) {
53 }
54
55 BLI_addtail(&ami->bindings, amb);
56
57 /* Set non-zero threshold by default. */
58 amb->float_threshold = 0.3f;
59
60 return amb;
61}
62
64 const char *name,
65 XrActionMapBinding *ambexcept)
66{
67 for (XrActionMapBinding &amb : ami->bindings) {
68 if (STREQLEN(name, amb.name, MAX_NAME) && (&amb != ambexcept)) {
69 return &amb;
70 }
71 }
72 return nullptr;
73}
74
76{
77 char name[MAX_NAME];
78 char *suffix;
79 size_t baselen;
80 size_t idx = 0;
81
82 baselen = STRNCPY_UTF8_RLEN(name, amb->name);
83 suffix = &name[baselen];
84
85 while (wm_xr_actionmap_binding_find_except(ami, name, amb)) {
86 if ((baselen + 1) + (log10(++idx) + 1) > MAX_NAME) {
87 /* Use default base name. */
89 suffix = &name[baselen];
90 idx = 0;
91 }
92 else {
93 BLI_snprintf_utf8(suffix, MAX_NAME, "%zu", idx);
94 }
95 }
96
97 STRNCPY_UTF8(amb->name, name);
98}
99
101{
102 XrActionMapBinding *amb_dst = MEM_dupalloc(amb_src);
103 amb_dst->prev = amb_dst->next = nullptr;
104
105 amb_dst->component_paths.clear_no_delete();
106 for (XrComponentPath &path : amb_src->component_paths) {
107 XrComponentPath *path_new = MEM_dupalloc(&path);
108 BLI_addtail(&amb_dst->component_paths, path_new);
109 }
110
111 return amb_dst;
112}
113
115 XrActionMapBinding *amb_src)
116{
118
120
121 BLI_addtail(&ami->bindings, amb_dst);
122
123 return amb_dst;
124}
125
127{
128 amb->component_paths.free_no_destruct();
129}
130
132{
133 int idx = BLI_findindex(&ami->bindings, amb);
134
135 if (idx != -1) {
137 BLI_freelinkN(&ami->bindings, amb);
138
139 if (idx <= ami->selbinding) {
140 if (--ami->selbinding < 0) {
141 ami->selbinding = 0;
142 }
143 }
144
145 return true;
146 }
147
148 return false;
149}
150
152{
153 for (XrActionMapBinding &amb : ami->bindings) {
154 if (STREQLEN(name, amb.name, MAX_NAME)) {
155 return &amb;
156 }
157 }
158 return nullptr;
159}
160
162
163/* -------------------------------------------------------------------- */
168
174
176{
177 if (ami->op_properties_ptr) {
179 MEM_delete(ami->op_properties_ptr);
180 ami->op_properties_ptr = nullptr;
181 ami->op_properties = nullptr;
182 }
183 else {
184 BLI_assert(ami->op_properties == nullptr);
185 }
186}
187
189{
190 for (XrActionMapBinding &amb : ami->bindings) {
192 }
193 ami->bindings.free_no_destruct();
194 ami->selbinding = 0;
195
197
198 ami->user_paths.free_no_destruct();
199}
200
202{
203 switch (ami->type) {
204 case XR_BOOLEAN_INPUT:
205 case XR_FLOAT_INPUT:
207 break;
208 case XR_POSE_INPUT:
211 memset(ami->op, 0, sizeof(ami->op));
212 return;
213 }
214
215 if (ami->op[0] == 0) {
217 return;
218 }
219
220 if (ami->op_properties_ptr == nullptr) {
222 }
223 else {
224 wmOperatorType *ot = WM_operatortype_find(ami->op, false);
225 if (ot) {
226 if (ot->srna != ami->op_properties_ptr->type) {
227 /* Matches wm_xr_actionmap_item_properties_set() but doesn't alloc new ptr. */
229 if (ami->op_properties) {
231 }
233 }
234 }
235 else {
237 }
238 }
239}
240
242 const char *name,
243 bool replace_existing)
244{
245 XrActionMapItem *ami_prev = WM_xr_actionmap_item_find(actionmap, name);
246 if (ami_prev && replace_existing) {
248 return ami_prev;
249 }
250
251 XrActionMapItem *ami = MEM_new<XrActionMapItem>(__func__);
252 STRNCPY_UTF8(ami->name, name);
253 if (ami_prev) {
255 }
256
257 BLI_addtail(&actionmap->items, ami);
258
259 /* Set type to float (button) input by default. */
260 ami->type = XR_FLOAT_INPUT;
261
262 return ami;
263}
264
266 const char *name,
267 const XrActionMapItem *amiexcept)
268{
269 for (XrActionMapItem &ami : actionmap->items) {
270 if (STREQLEN(name, ami.name, MAX_NAME) && (&ami != amiexcept)) {
271 return &ami;
272 }
273 }
274 return nullptr;
275}
276
278{
279 char name[MAX_NAME];
280 char *suffix;
281 size_t baselen;
282 size_t idx = 0;
283
284 baselen = STRNCPY_UTF8_RLEN(name, ami->name);
285 suffix = &name[baselen];
286
287 while (wm_xr_actionmap_item_find_except(actionmap, name, ami)) {
288 if ((baselen + 1) + (log10(++idx) + 1) > MAX_NAME) {
289 /* Use default base name. */
291 suffix = &name[baselen];
292 idx = 0;
293 }
294 else {
295 BLI_snprintf_utf8(suffix, MAX_NAME, "%zu", idx);
296 }
297 }
298
299 STRNCPY_UTF8(ami->name, name);
300}
301
303{
304 XrActionMapItem *ami_dst = MEM_dupalloc(ami_src);
305 ami_dst->prev = ami_dst->next = nullptr;
306
307 ami_dst->bindings.clear_no_delete();
308 for (XrActionMapBinding &amb : ami_src->bindings) {
310 BLI_addtail(&ami_dst->bindings, amb_new);
311 }
312
313 if (ami_dst->op_properties) {
314 ami_dst->op_properties_ptr = MEM_new<PointerRNA>("wmOpItemPtr");
316 ami_dst->op_properties = IDP_CopyProperty(ami_src->op_properties);
317 ami_dst->op_properties_ptr->data = ami_dst->op_properties;
318 }
319 else {
320 ami_dst->op_properties = nullptr;
321 ami_dst->op_properties_ptr = nullptr;
322 }
323
324 ami_dst->user_paths.clear_no_delete();
325 for (XrUserPath &path : ami_src->user_paths) {
326 XrUserPath *path_new = MEM_dupalloc(&path);
327 BLI_addtail(&ami_dst->user_paths, path_new);
328 }
329
330 return ami_dst;
331}
332
334{
335 XrActionMapItem *ami_dst = wm_xr_actionmap_item_copy(ami_src);
336
337 WM_xr_actionmap_item_ensure_unique(actionmap, ami_dst);
338
339 BLI_addtail(&actionmap->items, ami_dst);
340
341 return ami_dst;
342}
343
345{
346 int idx = BLI_findindex(&actionmap->items, ami);
347
348 if (idx != -1) {
350 BLI_freelinkN(&actionmap->items, ami);
351
352 if (idx <= actionmap->selitem) {
353 if (--actionmap->selitem < 0) {
354 actionmap->selitem = 0;
355 }
356 }
357
358 return true;
359 }
360
361 return false;
362}
363
365{
366 for (XrActionMapItem &ami : actionmap->items) {
367 if (STREQLEN(name, ami.name, MAX_NAME)) {
368 return &ami;
369 }
370 }
371 return nullptr;
372}
373
375
376/* -------------------------------------------------------------------- */
381
382XrActionMap *WM_xr_actionmap_new(wmXrRuntimeData *runtime, const char *name, bool replace_existing)
383{
384 XrActionMap *am_prev = WM_xr_actionmap_find(runtime, name);
385 if (am_prev && replace_existing) {
386 WM_xr_actionmap_clear(am_prev);
387 return am_prev;
388 }
389
390 XrActionMap *am = MEM_new<XrActionMap>(__func__);
391 STRNCPY_UTF8(am->name, name);
392 if (am_prev) {
394 }
395
396 BLI_addtail(&runtime->actionmaps, am);
397
398 return am;
399}
400
402 const char *name,
403 const XrActionMap *am_except)
404{
405 for (XrActionMap &am : runtime->actionmaps) {
406 if (STREQLEN(name, am.name, MAX_NAME) && (&am != am_except)) {
407 return &am;
408 }
409 }
410
411 return nullptr;
412}
413
415{
416 char name[MAX_NAME];
417 char *suffix;
418 size_t baselen;
419 size_t idx = 0;
420
421 baselen = STRNCPY_UTF8_RLEN(name, actionmap->name);
422 suffix = &name[baselen];
423
424 while (wm_xr_actionmap_find_except(runtime, name, actionmap)) {
425 if ((baselen + 1) + (log10(++idx) + 1) > MAX_NAME) {
426 /* Use default base name. */
428 suffix = &name[baselen];
429 idx = 0;
430 }
431 else {
432 BLI_snprintf_utf8(suffix, MAX_NAME, "%zu", idx);
433 }
434 }
435
436 STRNCPY_UTF8(actionmap->name, name);
437}
438
440{
441 XrActionMap *am_dst = MEM_dupalloc(am_src);
442 am_dst->prev = am_dst->next = nullptr;
443
444 am_dst->items.clear_no_delete();
445 for (XrActionMapItem &ami : am_src->items) {
447 BLI_addtail(&am_dst->items, ami_new);
448 }
449
450 return am_dst;
451}
452
454{
455 XrActionMap *am_dst = wm_xr_actionmap_copy(am_src);
456
457 WM_xr_actionmap_ensure_unique(runtime, am_dst);
458
459 BLI_addtail(&runtime->actionmaps, am_dst);
460
461 return am_dst;
462}
463
465{
466 int idx = BLI_findindex(&runtime->actionmaps, actionmap);
467
468 if (idx != -1) {
469 WM_xr_actionmap_clear(actionmap);
470 BLI_freelinkN(&runtime->actionmaps, actionmap);
471
472 if (idx <= runtime->actactionmap) {
473 if (--runtime->actactionmap < 0) {
474 runtime->actactionmap = 0;
475 }
476 }
477 if (idx <= runtime->selactionmap) {
478 if (--runtime->selactionmap < 0) {
479 runtime->selactionmap = 0;
480 }
481 }
482
483 return true;
484 }
485
486 return false;
487}
488
490{
491 for (XrActionMap &am : runtime->actionmaps) {
492 if (STREQLEN(name, am.name, MAX_NAME)) {
493 return &am;
494 }
495 }
496 return nullptr;
497}
498
500{
501 for (XrActionMapItem &ami : actionmap->items) {
503 }
504 actionmap->items.free_no_destruct();
505 actionmap->selitem = 0;
506}
507
509{
510 for (XrActionMap &am : runtime->actionmaps) {
512 }
513 runtime->actionmaps.free_no_destruct();
514 runtime->actactionmap = runtime->selactionmap = 0;
515}
516
517ListBaseT<XrActionMap> *WM_xr_actionmaps_get(wmXrRuntimeData *runtime)
518{
519 return &runtime->actionmaps;
520}
521
523{
524 return runtime->actactionmap;
525}
526
528{
529 runtime->actactionmap = idx;
530}
531
533{
534 return runtime->selactionmap;
535}
536
538{
539 runtime->selactionmap = idx;
540}
541
543
544} // namespace blender
#define STRNCPY_UTF8_RLEN(dst, src)
#define STRNCPY_UTF8(dst, src)
#define MAX_NAME
Definition DNA_defs.h:63
Read Blender memory allocation and freeing.
#define STREQLEN(a, b, n)
BMesh Iterators.
XrActionMapBinding * WM_xr_actionmap_binding_new(XrActionMapItem *ami, const char *name, bool replace_existing)
XrActionMapItem * WM_xr_actionmap_item_find(XrActionMap *actionmap, const char *name)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ XR_POSE_INPUT
@ XR_BOOLEAN_INPUT
@ XR_FLOAT_INPUT
@ XR_VIBRATION_OUTPUT
@ XR_VECTOR2F_INPUT
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
ListBaseT< XrActionMap > * WM_xr_actionmaps_get(wmXrRuntimeData *runtime)
short WM_xr_actionmap_active_index_get(const wmXrRuntimeData *runtime)
XrActionMap * WM_xr_actionmap_new(wmXrRuntimeData *runtime, const char *name, bool replace_existing)
static XrActionMapBinding * wm_xr_actionmap_binding_find_except(XrActionMapItem *ami, const char *name, XrActionMapBinding *ambexcept)
void WM_xr_actionmap_ensure_unique(wmXrRuntimeData *runtime, XrActionMap *actionmap)
void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
void WM_operator_properties_sanitize(PointerRNA *ptr, const bool no_context)
void WM_xr_actionmap_item_ensure_unique(XrActionMap *actionmap, XrActionMapItem *ami)
void WM_operator_properties_free(PointerRNA *ptr)
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:986
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
void WM_xr_actionmap_active_index_set(wmXrRuntimeData *runtime, short idx)
static XrActionMapItem * wm_xr_actionmap_item_find_except(XrActionMap *actionmap, const char *name, const XrActionMapItem *amiexcept)
XrActionMap * WM_xr_actionmap_find(wmXrRuntimeData *runtime, const char *name)
PointerRNA WM_operator_properties_create(const char *opstring)
short WM_xr_actionmap_selected_index_get(const wmXrRuntimeData *runtime)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void WM_xr_actionmap_binding_ensure_unique(XrActionMapItem *ami, XrActionMapBinding *amb)
XrActionMapItem * WM_xr_actionmap_item_new(XrActionMap *actionmap, const char *name, bool replace_existing)
XrActionMap * WM_xr_actionmap_add_copy(wmXrRuntimeData *runtime, XrActionMap *am_src)
XrActionMapBinding * WM_xr_actionmap_binding_add_copy(XrActionMapItem *ami, XrActionMapBinding *amb_src)
static void wm_xr_actionmap_item_properties_free(XrActionMapItem *ami)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
static void wm_xr_actionmap_item_properties_set(XrActionMapItem *ami)
static XrActionMapItem * wm_xr_actionmap_item_copy(XrActionMapItem *ami_src)
XrActionMapBinding * WM_xr_actionmap_binding_find(XrActionMapItem *ami, const char *name)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
static XrActionMap * wm_xr_actionmap_find_except(wmXrRuntimeData *runtime, const char *name, const XrActionMap *am_except)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static XrActionMapBinding * wm_xr_actionmap_binding_copy(XrActionMapBinding *amb_src)
static void wm_xr_actionmap_binding_clear(XrActionMapBinding *amb)
void WM_xr_actionmap_item_properties_update_ot(XrActionMapItem *ami)
static XrActionMap * wm_xr_actionmap_copy(XrActionMap *am_src)
void WM_xr_actionmap_selected_index_set(wmXrRuntimeData *runtime, short idx)
XrActionMapItem * WM_xr_actionmap_item_add_copy(XrActionMap *actionmap, XrActionMapItem *ami_src)
void WM_xr_actionmap_clear(XrActionMap *actionmap)
bool WM_xr_actionmap_remove(wmXrRuntimeData *runtime, XrActionMap *actionmap)
bool WM_xr_actionmap_item_remove(XrActionMap *actionmap, XrActionMapItem *ami)
void WM_xr_actionmaps_clear(wmXrRuntimeData *runtime)
bool WM_xr_actionmap_binding_remove(XrActionMapItem *ami, XrActionMapBinding *amb)
static void wm_xr_actionmap_item_clear(XrActionMapItem *ami)
StructRNA * type
Definition RNA_types.hh:63
ListBaseT< XrComponentPath > component_paths
struct XrActionMapBinding * prev
struct XrActionMapBinding * next
struct XrActionMapItem * prev
struct PointerRNA * op_properties_ptr
struct XrActionMapItem * next
ListBaseT< XrActionMapBinding > bindings
ListBaseT< XrUserPath > user_paths
struct XrActionMap * next
ListBaseT< XrActionMapItem > items
struct XrActionMap * prev
ListBaseT< XrActionMap > actionmaps
#define WM_XR_ACTIONMAP_BINDING_STR_DEFAULT
#define WM_XR_ACTIONMAP_ITEM_STR_DEFAULT
#define WM_XR_ACTIONMAP_STR_DEFAULT