Blender V5.0
wm_gizmo_target_props.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 "BLI_listbase.h"
10
11#include "BKE_context.hh"
12
13#include "MEM_guardedalloc.h"
14
15#include "RNA_access.hh"
16
17#include "WM_message.hh"
18#include "WM_types.hh"
19
20#include "ED_keyframes_edit.hh"
21#include "ED_screen.hh"
22
23#include "ANIM_keyframing.hh"
24
25/* Own includes. */
26
27/* -------------------------------------------------------------------- */
30
32{
33 int index = BLI_findstringindex(
35 if (index != -1) {
36 return &gz->target_properties[index];
37 }
38 return nullptr;
39}
40
42 const wmGizmoPropertyType *gz_prop_type,
44 PropertyRNA *prop,
45 int index)
46{
47 wmGizmoProperty *gz_prop = &gz->target_properties[gz_prop_type->index_in_type];
48
49 /* If gizmo evokes an operator we cannot use it for property manipulation. */
51 BLI_assert(prop != nullptr);
52
53 gz_prop->type = gz_prop_type;
54
55 gz_prop->ptr = *ptr;
56 gz_prop->prop = prop;
57 gz_prop->index = index;
58
59 if (gz->type->property_update) {
60 gz->type->property_update(gz, gz_prop);
61 }
62}
63
65 wmGizmo *gz, const char *idname, PointerRNA *ptr, const char *propname, int index)
66{
67 const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
68 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
69 if (prop == nullptr) {
70 RNA_warning("%s: %s.%s not found", __func__, RNA_struct_identifier(ptr->type), propname);
71 }
72 WM_gizmo_target_property_def_rna_ptr(gz, gz_prop_type, ptr, prop, index);
73}
74
76 const wmGizmoPropertyType *gz_prop_type,
78{
79 wmGizmoProperty *gz_prop = &gz->target_properties[gz_prop_type->index_in_type];
80
81 /* If gizmo evokes an operator we cannot use it for property manipulation. */
83
84 gz_prop->type = gz_prop_type;
85
86 gz_prop->custom_func.value_get_fn = params->value_get_fn;
87 gz_prop->custom_func.value_set_fn = params->value_set_fn;
88 gz_prop->custom_func.range_get_fn = params->range_get_fn;
89 gz_prop->custom_func.free_fn = params->free_fn;
90 gz_prop->custom_func.foreach_rna_prop_fn = params->foreach_rna_prop_fn;
91 gz_prop->custom_func.user_data = params->user_data;
92
93 if (gz->type->property_update) {
94 gz->type->property_update(gz, gz_prop);
95 }
96}
97
99 const char *idname,
101{
102 const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
104}
105
107{
108 wmGizmoProperty *gz_prop = &gz->target_properties[gz_prop_type->index_in_type];
109
110 /* If gizmo evokes an operator we cannot use it for property manipulation. */
112
113 *gz_prop = {};
114}
115
116void WM_gizmo_target_property_clear_rna(wmGizmo *gz, const char *idname)
117{
118 const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
120}
121
123
124/* -------------------------------------------------------------------- */
127
129{
130 for (const wmGizmoProperty &gz_prop : gz->target_properties) {
131 if (WM_gizmo_target_property_is_valid(&gz_prop)) {
132 return true;
133 }
134 }
135 return false;
136}
137
139{
140 return ((gz_prop->prop != nullptr) ||
141 (gz_prop->custom_func.value_get_fn && gz_prop->custom_func.value_set_fn));
142}
143
145{
146 if (gz_prop->custom_func.value_get_fn) {
147 float value = 0.0f;
148 BLI_assert(gz_prop->type->array_length == 1);
149 gz_prop->custom_func.value_get_fn(gz, gz_prop, &value);
150 return value;
151 }
152
153 if (gz_prop->index == -1) {
154 return RNA_property_float_get(&gz_prop->ptr, gz_prop->prop);
155 }
156 return RNA_property_float_get_index(&gz_prop->ptr, gz_prop->prop, gz_prop->index);
157}
158
160 const wmGizmo *gz,
161 wmGizmoProperty *gz_prop,
162 const float value)
163{
164 if (gz_prop->custom_func.value_set_fn) {
165 BLI_assert(gz_prop->type->array_length == 1);
166 gz_prop->custom_func.value_set_fn(gz, gz_prop, &value);
167 return;
168 }
169
170 /* Reset property. */
171 if (gz_prop->index == -1) {
172 RNA_property_float_set(&gz_prop->ptr, gz_prop->prop, value);
173 }
174 else {
175 RNA_property_float_set_index(&gz_prop->ptr, gz_prop->prop, gz_prop->index, value);
176 }
177 RNA_property_update(C, &gz_prop->ptr, gz_prop->prop);
178}
179
181 wmGizmoProperty *gz_prop,
182 float *value)
183{
184 if (gz_prop->custom_func.value_get_fn) {
185 gz_prop->custom_func.value_get_fn(gz, gz_prop, value);
186 return;
187 }
188 RNA_property_float_get_array(&gz_prop->ptr, gz_prop->prop, value);
189}
190
192 const wmGizmo *gz,
193 wmGizmoProperty *gz_prop,
194 const float *value)
195{
196 if (gz_prop->custom_func.value_set_fn) {
197 gz_prop->custom_func.value_set_fn(gz, gz_prop, value);
198 return;
199 }
200 RNA_property_float_set_array(&gz_prop->ptr, gz_prop->prop, value);
201
202 RNA_property_update(C, &gz_prop->ptr, gz_prop->prop);
203}
204
206 wmGizmoProperty *gz_prop,
207 float range[2])
208{
209 if (gz_prop->custom_func.value_get_fn) {
210 if (gz_prop->custom_func.range_get_fn) {
211 gz_prop->custom_func.range_get_fn(gz, gz_prop, range);
212 return true;
213 }
214 return false;
215 }
216
217 float step, precision;
219 &gz_prop->ptr, gz_prop->prop, &range[0], &range[1], &step, &precision);
220 return true;
221}
222
224{
225 if (gz_prop->custom_func.value_get_fn) {
226 return gz_prop->type->array_length;
227 }
228 return RNA_property_array_length(&gz_prop->ptr, gz_prop->prop);
229}
230
232
233/* -------------------------------------------------------------------- */
236
238 const char *idname)
239{
240 return static_cast<const wmGizmoPropertyType *>(
242}
243
245 const char *idname,
246 int data_type,
247 int array_length)
248{
249
250 BLI_assert(WM_gizmotype_target_property_find(gzt, idname) == nullptr);
251
252 const uint idname_size = strlen(idname) + 1;
253 wmGizmoPropertyType *gz_prop_type = static_cast<wmGizmoPropertyType *>(
254 MEM_callocN(sizeof(wmGizmoPropertyType) + idname_size, __func__));
255 memcpy(gz_prop_type->idname, idname, idname_size);
256 gz_prop_type->data_type = data_type;
257 gz_prop_type->array_length = array_length;
258 gz_prop_type->index_in_type = gzt->target_property_defs_len;
259 gzt->target_property_defs_len += 1;
260 BLI_addtail(&gzt->target_property_defs, gz_prop_type);
261}
262
264
265/* -------------------------------------------------------------------- */
268
270 wmMsgSubscribeKey * /*msg_key*/,
271 wmMsgSubscribeValue *msg_val)
272{
273 ARegion *region = static_cast<ARegion *>(msg_val->owner);
274 wmGizmoMap *gzmap = static_cast<wmGizmoMap *>(msg_val->user_data);
275
276 /* Could possibly avoid a full redraw and only tag for editor overlays
277 * redraw in some cases, see #ED_region_tag_redraw_editor_overlays(). */
278 ED_region_tag_redraw(region);
279
281}
282
284{
285 for (wmGizmoProperty &gz_prop : gz->target_properties) {
286 if (WM_gizmo_target_property_is_valid(&gz_prop)) {
287 if (gz_prop.prop) {
288 {
289 wmMsgSubscribeValue value{};
290 value.owner = region;
291 value.user_data = region;
293 WM_msg_subscribe_rna(mbus, &gz_prop.ptr, gz_prop.prop, &value, __func__);
294 }
295 {
296 wmMsgSubscribeValue value{};
297 value.owner = region;
300 WM_msg_subscribe_rna(mbus, &gz_prop.ptr, gz_prop.prop, &value, __func__);
301 }
302 }
303 }
304 }
305}
306
308 const wmGizmo * /*gz*/,
309 wmGizmoProperty *gz_prop)
310{
311 if (gz_prop->prop != nullptr) {
312 Scene *scene = CTX_data_scene(C);
313 const float cfra = float(scene->r.cfra);
314 const int index = gz_prop->index == -1 ? 0 : gz_prop->index;
317 C, scene, &gz_prop->ptr, gz_prop->prop, index, cfra, false);
318 }
319 else if (gz_prop->custom_func.foreach_rna_prop_fn) {
320 Scene *scene = CTX_data_scene(C);
321 auto autokey_fn = [C, scene](PointerRNA &ptr, PropertyRNA *prop, int index) {
323 C, scene, &ptr, prop, index, float(scene->r.cfra), false);
324 };
326 gz_prop->custom_func.foreach_rna_prop_fn(gz_prop, autokey_fn);
327 }
328}
329
Functions to insert, delete or modify keyframes.
Scene * CTX_data_scene(const bContext *C)
#define BLI_assert(a)
Definition BLI_assert.h:46
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:608
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
int BLI_findstringindex(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:780
unsigned int uint
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:618
void ED_region_do_msg_notify_tag_redraw(bContext *C, wmMsgSubscribeKey *msg_key, wmMsgSubscribeValue *msg_val)
Definition area.cc:361
Read Guarded memory(de)allocation.
#define RNA_warning(format,...)
#define C
Definition RandGen.cpp:29
void ANIM_deselect_keys_in_animation_editors(bContext *C)
Definition anim_deps.cc:478
bool is_empty() const
nullptr float
#define offsetof(t, d)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
bool autokeyframe_property(bContext *C, Scene *scene, PointerRNA *ptr, PropertyRNA *prop, int rnaindex, float cfra, bool only_if_property_keyed)
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision)
void RNA_property_float_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, float value)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
float RNA_property_float_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
const char * RNA_struct_identifier(const StructRNA *type)
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
struct RenderData r
wmGizmoMap * parent_gzmap
wmGizmoPropertyFnRangeGet range_get_fn
const wmGizmoPropertyType * type
PropertyRNA * prop
wmGizmoPropertyFnGet value_get_fn
wmGizmoPropertyFnSet value_set_fn
struct wmGizmoProperty::@331027022007232055216276241130041346111314317052 custom_func
wmGizmoPropertyFnForeachRNAProp foreach_rna_prop_fn
wmGizmoPropertyFnFree free_fn
ListBase target_property_defs
int target_property_defs_len
wmGizmoFnPropertyUpdate property_update
wmGizmoGroup * parent_gzgroup
const wmGizmoType * type
blender::Vector< wmGizmoProperty, 0 > target_properties
blender::Vector< wmGizmoOpElem, 4 > op_data
PointerRNA * ptr
Definition wm_files.cc:4238
void WM_gizmomap_tag_refresh(wmGizmoMap *gzmap)
void WM_gizmo_target_property_float_set(bContext *C, const wmGizmo *gz, wmGizmoProperty *gz_prop, const float value)
void WM_gizmo_target_property_anim_autokey(bContext *C, const wmGizmo *, wmGizmoProperty *gz_prop)
const wmGizmoPropertyType * WM_gizmotype_target_property_find(const wmGizmoType *gzt, const char *idname)
bool WM_gizmo_target_property_is_valid(const wmGizmoProperty *gz_prop)
void WM_gizmo_target_property_float_get_array(const wmGizmo *gz, wmGizmoProperty *gz_prop, float *value)
void WM_gizmo_target_property_clear_rna_ptr(wmGizmo *gz, const wmGizmoPropertyType *gz_prop_type)
bool WM_gizmo_target_property_is_valid_any(const wmGizmo *gz)
int WM_gizmo_target_property_array_length(const wmGizmo *, wmGizmoProperty *gz_prop)
void WM_gizmo_do_msg_notify_tag_refresh(bContext *, wmMsgSubscribeKey *, wmMsgSubscribeValue *msg_val)
void WM_gizmotype_target_property_def(wmGizmoType *gzt, const char *idname, int data_type, int array_length)
void WM_gizmo_target_property_def_func_ptr(wmGizmo *gz, const wmGizmoPropertyType *gz_prop_type, const wmGizmoPropertyFnParams *params)
void WM_gizmo_target_property_clear_rna(wmGizmo *gz, const char *idname)
void WM_gizmo_target_property_def_rna(wmGizmo *gz, const char *idname, PointerRNA *ptr, const char *propname, int index)
float WM_gizmo_target_property_float_get(const wmGizmo *gz, wmGizmoProperty *gz_prop)
void WM_gizmo_target_property_float_set_array(bContext *C, const wmGizmo *gz, wmGizmoProperty *gz_prop, const float *value)
void WM_gizmo_target_property_subscribe_all(wmGizmo *gz, wmMsgBus *mbus, ARegion *region)
wmGizmoProperty * WM_gizmo_target_property_find(wmGizmo *gz, const char *idname)
void WM_gizmo_target_property_def_rna_ptr(wmGizmo *gz, const wmGizmoPropertyType *gz_prop_type, PointerRNA *ptr, PropertyRNA *prop, int index)
void WM_gizmo_target_property_def_func(wmGizmo *gz, const char *idname, const wmGizmoPropertyFnParams *params)
bool WM_gizmo_target_property_float_range_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, float range[2])
void WM_msg_subscribe_rna(wmMsgBus *mbus, PointerRNA *ptr, const PropertyRNA *prop, const wmMsgSubscribeValue *msg_val_params, const char *id_repr)