Blender  V3.3
wm_gizmo_target_props.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BLI_listbase.h"
8 #include "BLI_math.h"
9 
10 #include "BKE_context.h"
11 
12 #include "MEM_guardedalloc.h"
13 
14 #include "RNA_access.h"
15 
16 #include "WM_api.h"
17 #include "WM_message.h"
18 #include "WM_types.h"
19 
20 #include "wm.h"
21 
22 #include "ED_keyframing.h"
23 #include "ED_screen.h"
24 #include "ED_view3d.h"
25 
26 /* own includes */
27 #include "wm_gizmo_intern.h"
28 #include "wm_gizmo_wmapi.h"
29 
30 /* -------------------------------------------------------------------- */
35 {
36  return (wmGizmoProperty *)(POINTER_OFFSET(gz, gz->type->struct_size));
37 }
38 
40 {
42 }
43 
45 {
46  BLI_assert(index < gz->type->target_property_defs_len);
47  BLI_assert(index != -1);
49  return &gz_prop_array[index];
50 }
51 
53 {
54  int index = BLI_findstringindex(
55  &gz->type->target_property_defs, idname, offsetof(wmGizmoPropertyType, idname));
56  if (index != -1) {
57  return WM_gizmo_target_property_at_index(gz, index);
58  }
59  return NULL;
60 }
61 
63  const wmGizmoPropertyType *gz_prop_type,
64  PointerRNA *ptr,
65  PropertyRNA *prop,
66  int index)
67 {
69 
70  /* if gizmo evokes an operator we cannot use it for property manipulation */
71  BLI_assert(gz->op_data == NULL);
72  BLI_assert(prop != NULL);
73 
74  gz_prop->type = gz_prop_type;
75 
76  gz_prop->ptr = *ptr;
77  gz_prop->prop = prop;
78  gz_prop->index = index;
79 
80  if (gz->type->property_update) {
81  gz->type->property_update(gz, gz_prop);
82  }
83 }
84 
86  wmGizmo *gz, const char *idname, PointerRNA *ptr, const char *propname, int index)
87 {
88  const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
89  PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
90  if (prop == NULL) {
91  RNA_warning("%s: %s.%s not found", __func__, RNA_struct_identifier(ptr->type), propname);
92  }
93  WM_gizmo_target_property_def_rna_ptr(gz, gz_prop_type, ptr, prop, index);
94 }
95 
97  const wmGizmoPropertyType *gz_prop_type,
99 {
101 
102  /* if gizmo evokes an operator we cannot use it for property manipulation */
103  BLI_assert(gz->op_data == NULL);
104 
105  gz_prop->type = gz_prop_type;
106 
107  gz_prop->custom_func.value_get_fn = params->value_get_fn;
108  gz_prop->custom_func.value_set_fn = params->value_set_fn;
109  gz_prop->custom_func.range_get_fn = params->range_get_fn;
110  gz_prop->custom_func.free_fn = params->free_fn;
111  gz_prop->custom_func.user_data = params->user_data;
112 
113  if (gz->type->property_update) {
114  gz->type->property_update(gz, gz_prop);
115  }
116 }
117 
119  const char *idname,
121 {
122  const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
124 }
125 
127 {
129 
130  /* if gizmo evokes an operator we cannot use it for property manipulation */
131  BLI_assert(gz->op_data == NULL);
132 
133  gz_prop->type = NULL;
134 
135  gz_prop->ptr = PointerRNA_NULL;
136  gz_prop->prop = NULL;
137  gz_prop->index = -1;
138 }
139 
140 void WM_gizmo_target_property_clear_rna(wmGizmo *gz, const char *idname)
141 {
142  const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
143  WM_gizmo_target_property_clear_rna_ptr(gz, gz_prop_type);
144 }
145 
148 /* -------------------------------------------------------------------- */
153 {
154  wmGizmoProperty *gz_prop_array = wm_gizmo_target_property_array(gz);
155  for (int i = 0; i < gz->type->target_property_defs_len; i++) {
156  wmGizmoProperty *gz_prop = &gz_prop_array[i];
157  if (WM_gizmo_target_property_is_valid(gz_prop)) {
158  return true;
159  }
160  }
161  return false;
162 }
163 
165 {
166  return ((gz_prop->prop != NULL) ||
167  (gz_prop->custom_func.value_get_fn && gz_prop->custom_func.value_set_fn));
168 }
169 
171 {
172  if (gz_prop->custom_func.value_get_fn) {
173  float value = 0.0f;
174  BLI_assert(gz_prop->type->array_length == 1);
175  gz_prop->custom_func.value_get_fn(gz, gz_prop, &value);
176  return value;
177  }
178 
179  if (gz_prop->index == -1) {
180  return RNA_property_float_get(&gz_prop->ptr, gz_prop->prop);
181  }
182  return RNA_property_float_get_index(&gz_prop->ptr, gz_prop->prop, gz_prop->index);
183 }
184 
186  const wmGizmo *gz,
187  wmGizmoProperty *gz_prop,
188  const float value)
189 {
190  if (gz_prop->custom_func.value_set_fn) {
191  BLI_assert(gz_prop->type->array_length == 1);
192  gz_prop->custom_func.value_set_fn(gz, gz_prop, &value);
193  return;
194  }
195 
196  /* reset property */
197  if (gz_prop->index == -1) {
198  RNA_property_float_set(&gz_prop->ptr, gz_prop->prop, value);
199  }
200  else {
201  RNA_property_float_set_index(&gz_prop->ptr, gz_prop->prop, gz_prop->index, value);
202  }
203  RNA_property_update(C, &gz_prop->ptr, gz_prop->prop);
204 }
205 
207  wmGizmoProperty *gz_prop,
208  float *value)
209 {
210  if (gz_prop->custom_func.value_get_fn) {
211  gz_prop->custom_func.value_get_fn(gz, gz_prop, value);
212  return;
213  }
214  RNA_property_float_get_array(&gz_prop->ptr, gz_prop->prop, value);
215 }
216 
218  const wmGizmo *gz,
219  wmGizmoProperty *gz_prop,
220  const float *value)
221 {
222  if (gz_prop->custom_func.value_set_fn) {
223  gz_prop->custom_func.value_set_fn(gz, gz_prop, value);
224  return;
225  }
226  RNA_property_float_set_array(&gz_prop->ptr, gz_prop->prop, value);
227 
228  RNA_property_update(C, &gz_prop->ptr, gz_prop->prop);
229 }
230 
232  wmGizmoProperty *gz_prop,
233  float range[2])
234 {
235  if (gz_prop->custom_func.value_get_fn) {
236  if (gz_prop->custom_func.range_get_fn) {
237  gz_prop->custom_func.range_get_fn(gz, gz_prop, range);
238  return true;
239  }
240  return false;
241  }
242 
243  float step, precision;
245  &gz_prop->ptr, gz_prop->prop, &range[0], &range[1], &step, &precision);
246  return true;
247 }
248 
250 {
251  if (gz_prop->custom_func.value_get_fn) {
252  return gz_prop->type->array_length;
253  }
254  return RNA_property_array_length(&gz_prop->ptr, gz_prop->prop);
255 }
256 
259 /* -------------------------------------------------------------------- */
264  const char *idname)
265 {
266  return BLI_findstring(&gzt->target_property_defs, idname, offsetof(wmGizmoPropertyType, idname));
267 }
268 
270  const char *idname,
271  int data_type,
272  int array_length)
273 {
274  wmGizmoPropertyType *mpt;
275 
277 
278  const uint idname_size = strlen(idname) + 1;
279  mpt = MEM_callocN(sizeof(wmGizmoPropertyType) + idname_size, __func__);
280  memcpy(mpt->idname, idname, idname_size);
281  mpt->data_type = data_type;
282  mpt->array_length = array_length;
284  gzt->target_property_defs_len += 1;
285  BLI_addtail(&gzt->target_property_defs, mpt);
286 }
287 
290 /* -------------------------------------------------------------------- */
295  wmMsgSubscribeKey *UNUSED(msg_key),
296  wmMsgSubscribeValue *msg_val)
297 {
298  ARegion *region = msg_val->owner;
299  wmGizmoMap *gzmap = msg_val->user_data;
300 
301  /* Could possibly avoid a full redraw and only tag for editor overlays
302  * redraw in some cases, see #ED_region_tag_redraw_editor_overlays(). */
303  ED_region_tag_redraw(region);
304 
306 }
307 
309 {
310  if (gz->type->target_property_defs_len) {
311  wmGizmoProperty *gz_prop_array = WM_gizmo_target_property_array(gz);
312  for (int i = 0; i < gz->type->target_property_defs_len; i++) {
313  wmGizmoProperty *gz_prop = &gz_prop_array[i];
314  if (WM_gizmo_target_property_is_valid(gz_prop)) {
315  if (gz_prop->prop) {
317  &gz_prop->ptr,
318  gz_prop->prop,
319  &(const wmMsgSubscribeValue){
320  .owner = region,
321  .user_data = region,
322  .notify = ED_region_do_msg_notify_tag_redraw,
323  },
324  __func__);
326  &gz_prop->ptr,
327  gz_prop->prop,
328  &(const wmMsgSubscribeValue){
329  .owner = region,
330  .user_data = gz->parent_gzgroup->parent_gzmap,
331  .notify = WM_gizmo_do_msg_notify_tag_refresh,
332  },
333  __func__);
334  }
335  }
336  }
337  }
338 }
339 
341  const wmGizmo *UNUSED(gz),
342  wmGizmoProperty *gz_prop)
343 {
344  if (gz_prop->prop != NULL) {
346  const float cfra = (float)scene->r.cfra;
347  const int index = gz_prop->index == -1 ? 0 : gz_prop->index;
348  ED_autokeyframe_property(C, scene, &gz_prop->ptr, gz_prop->prop, index, cfra, false);
349  }
350 }
351 
typedef float(TangentPoint)[2]
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_INLINE
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
int BLI_findstringindex(const struct ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void * BLI_findstring(const struct ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
unsigned int uint
Definition: BLI_sys_types.h:67
#define UNUSED(x)
#define POINTER_OFFSET(v, ofs)
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:655
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
#define RNA_warning(format,...)
Definition: RNA_access.h:756
#define C
Definition: RandGen.cpp:25
Scene scene
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
bool ED_autokeyframe_property(bContext *C, Scene *scene, PointerRNA *ptr, PropertyRNA *prop, int rnaindex, float cfra, const bool only_if_property_keyed)
Definition: keyframing.c:3111
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2767
const char * RNA_struct_identifier(const StructRNA *type)
Definition: rna_access.c:586
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
Definition: rna_access.c:2879
void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision)
Definition: rna_access.c:1311
void RNA_property_float_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, float value)
Definition: rna_access.c:3036
float RNA_property_float_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
Definition: rna_access.c:2954
const PointerRNA PointerRNA_NULL
Definition: rna_access.c:61
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:717
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2138
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:1075
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
Definition: rna_access.c:2978
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
Definition: rna_access.c:2790
struct StructRNA * type
Definition: RNA_types.h:37
struct RenderData r
wmGizmoPropertyFnRangeGet range_get_fn
PropertyRNA * prop
wmGizmoPropertyFnGet value_get_fn
wmGizmoPropertyFnSet value_set_fn
wmGizmoPropertyFnFree free_fn
const struct wmGizmoPropertyType * type
struct wmGizmoProperty::@1185 custom_func
ListBase target_property_defs
int target_property_defs_len
wmGizmoFnPropertyUpdate property_update
wmGizmoOpElem * op_data
const struct wmGizmoType * type
PointerRNA * ptr
Definition: wm_files.c:3480
void WM_gizmomap_tag_refresh(wmGizmoMap *gzmap)
Definition: wm_gizmo_map.c:308
void WM_gizmo_target_property_float_set(bContext *C, const wmGizmo *gz, wmGizmoProperty *gz_prop, const float value)
BLI_INLINE wmGizmoProperty * wm_gizmo_target_property_array(wmGizmo *gz)
wmGizmoProperty * WM_gizmo_target_property_find(wmGizmo *gz, const char *idname)
bool WM_gizmo_target_property_is_valid_any(wmGizmo *gz)
void WM_gizmo_do_msg_notify_tag_refresh(bContext *UNUSED(C), wmMsgSubscribeKey *UNUSED(msg_key), wmMsgSubscribeValue *msg_val)
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)
void WM_gizmotype_target_property_def(wmGizmoType *gzt, const char *idname, int data_type, int array_length)
wmGizmoProperty * WM_gizmo_target_property_at_index(wmGizmo *gz, int index)
void WM_gizmo_target_property_def_func_ptr(wmGizmo *gz, const wmGizmoPropertyType *gz_prop_type, const wmGizmoPropertyFnParams *params)
wmGizmoProperty * WM_gizmo_target_property_array(wmGizmo *gz)
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_anim_autokey(bContext *C, const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop)
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)
const wmGizmoPropertyType * WM_gizmotype_target_property_find(const wmGizmoType *gzt, const char *idname)
void WM_gizmo_target_property_subscribe_all(wmGizmo *gz, struct wmMsgBus *mbus, ARegion *region)
int WM_gizmo_target_property_array_length(const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop)
bool WM_gizmo_target_property_float_range_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, float range[2])
void WM_msg_subscribe_rna(struct wmMsgBus *mbus, PointerRNA *ptr, const PropertyRNA *prop, const wmMsgSubscribeValue *msg_val_params, const char *id_repr)