Blender  V2.93
wm_gizmo_target_props.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "BLI_listbase.h"
22 #include "BLI_math.h"
23 
24 #include "BKE_context.h"
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "RNA_access.h"
29 
30 #include "WM_api.h"
31 #include "WM_message.h"
32 #include "WM_types.h"
33 
34 #include "wm.h"
35 
36 #include "ED_keyframing.h"
37 #include "ED_screen.h"
38 #include "ED_view3d.h"
39 
40 /* own includes */
41 #include "wm_gizmo_intern.h"
42 #include "wm_gizmo_wmapi.h"
43 
44 /* -------------------------------------------------------------------- */
49 {
50  return (wmGizmoProperty *)(POINTER_OFFSET(gz, gz->type->struct_size));
51 }
52 
54 {
56 }
57 
59 {
60  BLI_assert(index < gz->type->target_property_defs_len);
61  BLI_assert(index != -1);
63  return &gz_prop_array[index];
64 }
65 
67 {
68  int index = BLI_findstringindex(
69  &gz->type->target_property_defs, idname, offsetof(wmGizmoPropertyType, idname));
70  if (index != -1) {
71  return WM_gizmo_target_property_at_index(gz, index);
72  }
73  return NULL;
74 }
75 
77  const wmGizmoPropertyType *gz_prop_type,
78  PointerRNA *ptr,
79  PropertyRNA *prop,
80  int index)
81 {
83 
84  /* if gizmo evokes an operator we cannot use it for property manipulation */
85  BLI_assert(gz->op_data == NULL);
86  BLI_assert(prop != NULL);
87 
88  gz_prop->type = gz_prop_type;
89 
90  gz_prop->ptr = *ptr;
91  gz_prop->prop = prop;
92  gz_prop->index = index;
93 
94  if (gz->type->property_update) {
95  gz->type->property_update(gz, gz_prop);
96  }
97 }
98 
100  wmGizmo *gz, const char *idname, PointerRNA *ptr, const char *propname, int index)
101 {
102  const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
103  PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
104  if (prop == NULL) {
105  RNA_warning("%s: %s.%s not found", __func__, RNA_struct_identifier(ptr->type), propname);
106  }
107  WM_gizmo_target_property_def_rna_ptr(gz, gz_prop_type, ptr, prop, index);
108 }
109 
111  const wmGizmoPropertyType *gz_prop_type,
113 {
115 
116  /* if gizmo evokes an operator we cannot use it for property manipulation */
117  BLI_assert(gz->op_data == NULL);
118 
119  gz_prop->type = gz_prop_type;
120 
121  gz_prop->custom_func.value_get_fn = params->value_get_fn;
122  gz_prop->custom_func.value_set_fn = params->value_set_fn;
123  gz_prop->custom_func.range_get_fn = params->range_get_fn;
124  gz_prop->custom_func.free_fn = params->free_fn;
125  gz_prop->custom_func.user_data = params->user_data;
126 
127  if (gz->type->property_update) {
128  gz->type->property_update(gz, gz_prop);
129  }
130 }
131 
133  const char *idname,
135 {
136  const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
138 }
139 
141 {
143 
144  /* if gizmo evokes an operator we cannot use it for property manipulation */
145  BLI_assert(gz->op_data == NULL);
146 
147  gz_prop->type = NULL;
148 
149  gz_prop->ptr = PointerRNA_NULL;
150  gz_prop->prop = NULL;
151  gz_prop->index = -1;
152 }
153 
154 void WM_gizmo_target_property_clear_rna(wmGizmo *gz, const char *idname)
155 {
156  const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
157  WM_gizmo_target_property_clear_rna_ptr(gz, gz_prop_type);
158 }
159 
162 /* -------------------------------------------------------------------- */
167 {
168  wmGizmoProperty *gz_prop_array = wm_gizmo_target_property_array(gz);
169  for (int i = 0; i < gz->type->target_property_defs_len; i++) {
170  wmGizmoProperty *gz_prop = &gz_prop_array[i];
171  if (WM_gizmo_target_property_is_valid(gz_prop)) {
172  return true;
173  }
174  }
175  return false;
176 }
177 
179 {
180  return ((gz_prop->prop != NULL) ||
181  (gz_prop->custom_func.value_get_fn && gz_prop->custom_func.value_set_fn));
182 }
183 
185 {
186  if (gz_prop->custom_func.value_get_fn) {
187  float value = 0.0f;
188  BLI_assert(gz_prop->type->array_length == 1);
189  gz_prop->custom_func.value_get_fn(gz, gz_prop, &value);
190  return value;
191  }
192 
193  if (gz_prop->index == -1) {
194  return RNA_property_float_get(&gz_prop->ptr, gz_prop->prop);
195  }
196  return RNA_property_float_get_index(&gz_prop->ptr, gz_prop->prop, gz_prop->index);
197 }
198 
200  const wmGizmo *gz,
201  wmGizmoProperty *gz_prop,
202  const float value)
203 {
204  if (gz_prop->custom_func.value_set_fn) {
205  BLI_assert(gz_prop->type->array_length == 1);
206  gz_prop->custom_func.value_set_fn(gz, gz_prop, &value);
207  return;
208  }
209 
210  /* reset property */
211  if (gz_prop->index == -1) {
212  RNA_property_float_set(&gz_prop->ptr, gz_prop->prop, value);
213  }
214  else {
215  RNA_property_float_set_index(&gz_prop->ptr, gz_prop->prop, gz_prop->index, value);
216  }
217  RNA_property_update(C, &gz_prop->ptr, gz_prop->prop);
218 }
219 
221  wmGizmoProperty *gz_prop,
222  float *value)
223 {
224  if (gz_prop->custom_func.value_get_fn) {
225  gz_prop->custom_func.value_get_fn(gz, gz_prop, value);
226  return;
227  }
228  RNA_property_float_get_array(&gz_prop->ptr, gz_prop->prop, value);
229 }
230 
232  const wmGizmo *gz,
233  wmGizmoProperty *gz_prop,
234  const float *value)
235 {
236  if (gz_prop->custom_func.value_set_fn) {
237  gz_prop->custom_func.value_set_fn(gz, gz_prop, value);
238  return;
239  }
240  RNA_property_float_set_array(&gz_prop->ptr, gz_prop->prop, value);
241 
242  RNA_property_update(C, &gz_prop->ptr, gz_prop->prop);
243 }
244 
246  wmGizmoProperty *gz_prop,
247  float range[2])
248 {
249  if (gz_prop->custom_func.value_get_fn) {
250  if (gz_prop->custom_func.range_get_fn) {
251  gz_prop->custom_func.range_get_fn(gz, gz_prop, range);
252  return true;
253  }
254  return false;
255  }
256 
257  float step, precision;
259  &gz_prop->ptr, gz_prop->prop, &range[0], &range[1], &step, &precision);
260  return true;
261 }
262 
264 {
265  if (gz_prop->custom_func.value_get_fn) {
266  return gz_prop->type->array_length;
267  }
268  return RNA_property_array_length(&gz_prop->ptr, gz_prop->prop);
269 }
270 
273 /* -------------------------------------------------------------------- */
278  const char *idname)
279 {
280  return BLI_findstring(&gzt->target_property_defs, idname, offsetof(wmGizmoPropertyType, idname));
281 }
282 
284  const char *idname,
285  int data_type,
286  int array_length)
287 {
288  wmGizmoPropertyType *mpt;
289 
291 
292  const uint idname_size = strlen(idname) + 1;
293  mpt = MEM_callocN(sizeof(wmGizmoPropertyType) + idname_size, __func__);
294  memcpy(mpt->idname, idname, idname_size);
295  mpt->data_type = data_type;
296  mpt->array_length = array_length;
298  gzt->target_property_defs_len += 1;
299  BLI_addtail(&gzt->target_property_defs, mpt);
300 }
301 
304 /* -------------------------------------------------------------------- */
309  wmMsgSubscribeKey *UNUSED(msg_key),
310  wmMsgSubscribeValue *msg_val)
311 {
312  ARegion *region = msg_val->owner;
313  wmGizmoMap *gzmap = msg_val->user_data;
314 
316  region); /* Could possibly avoid a full redraw and only tag for editor overlays
317  * redraw in some cases, see #ED_region_tag_redraw_editor_overlays(). */
319 }
320 
326 {
327  if (gz->type->target_property_defs_len) {
328  wmGizmoProperty *gz_prop_array = WM_gizmo_target_property_array(gz);
329  for (int i = 0; i < gz->type->target_property_defs_len; i++) {
330  wmGizmoProperty *gz_prop = &gz_prop_array[i];
331  if (WM_gizmo_target_property_is_valid(gz_prop)) {
332  if (gz_prop->prop) {
334  &gz_prop->ptr,
335  gz_prop->prop,
336  &(const wmMsgSubscribeValue){
337  .owner = region,
338  .user_data = region,
339  .notify = ED_region_do_msg_notify_tag_redraw,
340  },
341  __func__);
343  &gz_prop->ptr,
344  gz_prop->prop,
345  &(const wmMsgSubscribeValue){
346  .owner = region,
347  .user_data = gz->parent_gzgroup->parent_gzmap,
348  .notify = WM_gizmo_do_msg_notify_tag_refresh,
349  },
350  __func__);
351  }
352  }
353  }
354  }
355 }
356 
361  const wmGizmo *UNUSED(gz),
362  wmGizmoProperty *gz_prop)
363 {
364  if (gz_prop->prop != NULL) {
366  const float cfra = (float)CFRA;
367  const int index = gz_prop->index == -1 ? 0 : gz_prop->index;
368  ED_autokeyframe_property(C, scene, &gz_prop->ptr, gz_prop->prop, index, cfra);
369  }
370 }
371 
typedef float(TangentPoint)[2]
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define BLI_INLINE
void * BLI_findstring(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
int BLI_findstringindex(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
#define POINTER_OFFSET(v, ofs)
#define CFRA
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:667
_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:1437
#define C
Definition: RandGen.cpp:39
Scene scene
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
bool ED_autokeyframe_property(bContext *C, Scene *scene, PointerRNA *ptr, PropertyRNA *prop, int rnaindex, float cfra)
Definition: keyframing.c:3047
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2941
const char * RNA_struct_identifier(const StructRNA *type)
Definition: rna_access.c:723
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
Definition: rna_access.c:3033
void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision)
Definition: rna_access.c:1466
void RNA_property_float_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, float value)
Definition: rna_access.c:3190
float RNA_property_float_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
Definition: rna_access.c:3108
const PointerRNA PointerRNA_NULL
Definition: rna_access.c:71
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:866
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2317
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:1218
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
Definition: rna_access.c:3132
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
Definition: rna_access.c:2964
struct StructRNA * type
Definition: RNA_types.h:51
wmGizmoPropertyFnRangeGet range_get_fn
PropertyRNA * prop
wmGizmoPropertyFnGet value_get_fn
wmGizmoPropertyFnSet value_set_fn
struct wmGizmoProperty::@1149 custom_func
wmGizmoPropertyFnFree free_fn
const struct wmGizmoPropertyType * type
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:3157
void WM_gizmomap_tag_refresh(wmGizmoMap *gzmap)
Definition: wm_gizmo_map.c:323
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)