Blender V5.3
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.hh"
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
34namespace blender {
35
36/* -------------------------------------------------------------------- */
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_new_zeroed<wmGizmoType>("gizmotype");
77 gzt->srna = RNA_def_struct_ptr(&RNA_blender_rna_get(), "", 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_delete(gzt->idname);
113 }
114
115 gzt->target_property_defs.free_no_destruct();
116 MEM_delete(gzt);
117}
118
122static void gizmotype_unlink(bContext *C, Main *bmain, wmGizmoType *gzt)
123{
124 /* Free instances. */
125 for (bScreen *screen = bmain->screens.first(); screen;
126 screen = static_cast<bScreen *>(screen->id.next))
127 {
128 for (ScrArea &area : screen->areabase) {
129 for (SpaceLink &sl : area.spacedata) {
130 ListBaseT<ARegion> *lb = (&sl == area.spacedata.first_) ? &area.regionbase :
131 &sl.regionbase;
132 for (ARegion &region : *lb) {
133 wmGizmoMap *gzmap = region.runtime->gizmo_map;
134 if (gzmap) {
135 for (wmGizmoGroup &gzgroup : gzmap->groups) {
136 for (wmGizmo *gz = gzgroup.gizmos.first(), *gz_next; gz; gz = gz_next) {
137 gz_next = gz->next;
138 BLI_assert(gzgroup.parent_gzmap == gzmap);
139 if (gz->type == gzt) {
140 WM_gizmo_unlink(&gzgroup.gizmos, gzgroup.parent_gzmap, gz, C);
142 }
143 }
144 }
145 }
146 }
147 }
148 }
149 }
150}
151
153{
154 BLI_assert(gzt == WM_gizmotype_find(gzt->idname, false));
155
156 get_gizmo_type_map().remove(gzt);
157
158 gizmotype_unlink(C, bmain, gzt);
159}
160
161bool WM_gizmotype_remove(bContext *C, Main *bmain, const StringRef idname)
162{
163 wmGizmoType *const *gzt = get_gizmo_type_map().lookup_key_ptr_as(idname);
164 if (gzt == nullptr) {
165 return false;
166 }
167
168 WM_gizmotype_remove_ptr(C, bmain, *gzt);
169
170 return true;
171}
172
174{
175 for (wmGizmoType *gzt : get_gizmo_type_map()) {
177 }
178 get_gizmo_type_map().clear();
179}
180
182{
183 /* Reserve size is set based on blender default setup. */
184 get_gizmo_type_map().reserve(128);
185}
186
188
189} // namespace blender
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
Read Blender memory allocation and freeing.
#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
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static auto & get_gizmo_type_map()
void RNA_def_struct_identifier(BlenderRNA *brna, StructRNA *srna, const char *identifier)
void WM_gizmotype_remove_ptr(bContext *C, Main *bmain, wmGizmoType *gzt)
void wm_gizmotype_free()
void WM_gizmotype_append(void(*gtfunc)(wmGizmoType *))
const wmGizmoType * WM_gizmotype_find(const StringRef idname, bool quiet)
static wmGizmoType * wm_gizmotype_append__begin()
bool WM_gizmotype_remove(bContext *C, Main *bmain, const StringRef idname)
void ED_region_tag_redraw_editor_overlays(ARegion *region)
Definition area.cc:745
void WM_gizmo_unlink(ListBaseT< wmGizmo > *gizmolist, wmGizmoMap *gzmap, wmGizmo *gz, bContext *C)
Definition wm_gizmo.cc:161
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
VectorSet< T, InlineBufferCapacity, DefaultProbingStrategy, CustomIDHash< T, GetIDFn >, CustomIDEqual< T, GetIDFn > > CustomIDVectorSet
void WM_gizmotype_free_ptr(wmGizmoType *gzt)
void WM_gizmotype_append_ptr(void(*gtfunc)(wmGizmoType *, void *), void *userdata)
void wm_gizmotype_init()
static void gizmotype_unlink(bContext *C, Main *bmain, wmGizmoType *gzt)
static void wm_gizmotype_append__end(wmGizmoType *gzt)
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
BlenderRNA RNA_blender_rna_get()
StructRNA * RNA_def_struct_ptr(BlenderRNA *brna, const char *identifier, StructRNA *srnafrom)
ListBaseT< bScreen > screens
Definition BKE_main.hh:400
ListBaseT< ARegion > regionbase
ListBaseT< SpaceLink > spacedata
ListBaseT< wmGizmo > gizmos
ListBaseT< wmGizmoGroup > groups
ListBaseT< wmGizmoPropertyType > target_property_defs