Blender V5.0
wm_gizmo_type.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
8
9#include <cstdio>
10
11#include "BLI_listbase.h"
12#include "BLI_vector_set.hh"
13
14#include "BKE_main.hh"
15#include "BKE_screen.hh"
16
17#include "DNA_screen_types.h"
18#include "DNA_space_types.h"
19
20#include "MEM_guardedalloc.h"
21
22#include "RNA_access.hh"
23#include "RNA_define.hh"
24#include "RNA_prototypes.hh"
25
26#include "WM_types.hh"
27
28#include "ED_screen.hh"
29
30/* Own includes. */
31#include "wm_gizmo_intern.hh"
32#include "wm_gizmo_wmapi.hh"
33
34/* -------------------------------------------------------------------- */
39
41
42static auto &get_gizmo_type_map()
43{
44 struct IDNameGetter {
45 StringRef operator()(const wmGizmoType *value) const
46 {
47 return StringRef(value->idname);
48 }
49 };
51 return map;
52}
53
54const wmGizmoType *WM_gizmotype_find(const StringRef idname, bool quiet)
55{
56 if (!idname.is_empty()) {
57 if (wmGizmoType *const *gzt = get_gizmo_type_map().lookup_key_ptr_as(idname)) {
58 return *gzt;
59 }
60
61 if (!quiet) {
62 printf("search for unknown gizmo '%s'\n", std::string(idname).c_str());
63 }
64 }
65 else {
66 if (!quiet) {
67 printf("search for empty gizmo\n");
68 }
69 }
70
71 return nullptr;
72}
73
75{
76 wmGizmoType *gzt = MEM_callocN<wmGizmoType>("gizmotype");
77 gzt->srna = RNA_def_struct_ptr(&BLENDER_RNA, "", &RNA_GizmoProperties);
78#if 0
79 /* Set the default i18n context now, so that opfunc can redefine it if needed! */
81 ot->translation_context = BLT_I18NCONTEXT_OPERATOR_DEFAULT;
82#endif
83 return gzt;
84}
86{
87 BLI_assert(gzt->struct_size >= sizeof(wmGizmo));
88
90
91 get_gizmo_type_map().add(gzt);
92}
93
94void WM_gizmotype_append(void (*gtfunc)(wmGizmoType *))
95{
97 gtfunc(gzt);
99}
100
101void WM_gizmotype_append_ptr(void (*gtfunc)(wmGizmoType *, void *), void *userdata)
102{
104 gtfunc(mt, userdata);
106}
107
109{
110 /* Python gizmo, allocates its own string. */
111 if (gzt->rna_ext.srna) {
112 MEM_freeN(gzt->idname);
113 }
114
116 MEM_freeN(gzt);
117}
118
122static void gizmotype_unlink(bContext *C, Main *bmain, wmGizmoType *gzt)
123{
124 /* Free instances. */
125 for (bScreen *screen = static_cast<bScreen *>(bmain->screens.first); screen;
126 screen = static_cast<bScreen *>(screen->id.next))
127 {
128 LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
129 LISTBASE_FOREACH (SpaceLink *, sl, &area->spacedata) {
130 ListBase *lb = (sl == area->spacedata.first) ? &area->regionbase : &sl->regionbase;
131 LISTBASE_FOREACH (ARegion *, region, lb) {
132 wmGizmoMap *gzmap = region->runtime->gizmo_map;
133 if (gzmap) {
134 LISTBASE_FOREACH (wmGizmoGroup *, gzgroup, &gzmap->groups) {
135 for (wmGizmo *gz = static_cast<wmGizmo *>(gzgroup->gizmos.first), *gz_next; gz;
136 gz = gz_next)
137 {
138 gz_next = gz->next;
139 BLI_assert(gzgroup->parent_gzmap == gzmap);
140 if (gz->type == gzt) {
141 WM_gizmo_unlink(&gzgroup->gizmos, gzgroup->parent_gzmap, gz, C);
143 }
144 }
145 }
146 }
147 }
148 }
149 }
150 }
151}
152
154{
155 BLI_assert(gzt == WM_gizmotype_find(gzt->idname, false));
156
157 get_gizmo_type_map().remove(gzt);
158
159 gizmotype_unlink(C, bmain, gzt);
160}
161
162bool WM_gizmotype_remove(bContext *C, Main *bmain, const StringRef idname)
163{
164 wmGizmoType *const *gzt = get_gizmo_type_map().lookup_key_ptr_as(idname);
165 if (gzt == nullptr) {
166 return false;
167 }
168
169 WM_gizmotype_remove_ptr(C, bmain, *gzt);
170
171 return true;
172}
173
175{
176 for (wmGizmoType *gzt : get_gizmo_type_map()) {
178 }
179 get_gizmo_type_map().clear();
180}
181
183{
184 /* Reserve size is set based on blender default setup. */
185 get_gizmo_type_map().reserve(128);
186}
187
#define BLI_assert(a)
Definition BLI_assert.h:46
#define LISTBASE_FOREACH(type, var, list)
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:497
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
void ED_region_tag_redraw_editor_overlays(ARegion *region)
Definition area.cc:654
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
constexpr bool is_empty() const
#define printf(...)
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
VectorSet< T, InlineBufferCapacity, DefaultProbingStrategy, CustomIDHash< T, GetIDFn >, CustomIDEqual< T, GetIDFn > > CustomIDVectorSet
void RNA_def_struct_identifier(BlenderRNA *brna, StructRNA *srna, const char *identifier)
StructRNA * RNA_def_struct_ptr(BlenderRNA *brna, const char *identifier, StructRNA *srnafrom)
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
BlenderRNA BLENDER_RNA
StructRNA * srna
void * first
ListBase screens
Definition BKE_main.hh:292
StructRNA * srna
ExtensionRNA rna_ext
ListBase target_property_defs
const char * idname
wmOperatorType * ot
Definition wm_files.cc:4237
void WM_gizmo_unlink(ListBase *gizmolist, wmGizmoMap *gzmap, wmGizmo *gz, bContext *C)
Definition wm_gizmo.cc:165
void WM_gizmotype_append_ptr(void(*gtfunc)(wmGizmoType *, void *), void *userdata)
static void wm_gizmotype_append__end(wmGizmoType *gzt)
void WM_gizmotype_append(void(*gtfunc)(wmGizmoType *))
void WM_gizmotype_remove_ptr(bContext *C, Main *bmain, wmGizmoType *gzt)
const wmGizmoType * WM_gizmotype_find(const StringRef idname, bool quiet)
static void gizmotype_unlink(bContext *C, Main *bmain, wmGizmoType *gzt)
void wm_gizmotype_init()
bool WM_gizmotype_remove(bContext *C, Main *bmain, const StringRef idname)
void wm_gizmotype_free()
static auto & get_gizmo_type_map()
static wmGizmoType * wm_gizmotype_append__begin()
void WM_gizmotype_free_ptr(wmGizmoType *gzt)