Welcome to mirror list, hosted at ThFree Co, Russian Federation.

wm_gizmo_target_props.c « intern « gizmo « windowmanager « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 80876dfd798abe71577515216f093f80a773fe8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup wm
 */

#include "BLI_listbase.h"
#include "BLI_math.h"

#include "BKE_context.h"

#include "MEM_guardedalloc.h"

#include "RNA_access.h"

#include "WM_api.h"
#include "WM_message.h"
#include "WM_types.h"

#include "wm.h"

#include "ED_keyframing.h"
#include "ED_screen.h"
#include "ED_view3d.h"

/* own includes */
#include "wm_gizmo_intern.h"
#include "wm_gizmo_wmapi.h"

/* -------------------------------------------------------------------- */
/** \name Property Definition
 * \{ */

BLI_INLINE wmGizmoProperty *wm_gizmo_target_property_array(wmGizmo *gz)
{
  return (wmGizmoProperty *)(POINTER_OFFSET(gz, gz->type->struct_size));
}

wmGizmoProperty *WM_gizmo_target_property_array(wmGizmo *gz)
{
  return wm_gizmo_target_property_array(gz);
}

wmGizmoProperty *WM_gizmo_target_property_at_index(wmGizmo *gz, int index)
{
  BLI_assert(index < gz->type->target_property_defs_len);
  BLI_assert(index != -1);
  wmGizmoProperty *gz_prop_array = wm_gizmo_target_property_array(gz);
  return &gz_prop_array[index];
}

wmGizmoProperty *WM_gizmo_target_property_find(wmGizmo *gz, const char *idname)
{
  int index = BLI_findstringindex(
      &gz->type->target_property_defs, idname, offsetof(wmGizmoPropertyType, idname));
  if (index != -1) {
    return WM_gizmo_target_property_at_index(gz, index);
  }
  return NULL;
}

void WM_gizmo_target_property_def_rna_ptr(wmGizmo *gz,
                                          const wmGizmoPropertyType *gz_prop_type,
                                          PointerRNA *ptr,
                                          PropertyRNA *prop,
                                          int index)
{
  wmGizmoProperty *gz_prop = WM_gizmo_target_property_at_index(gz, gz_prop_type->index_in_type);

  /* if gizmo evokes an operator we cannot use it for property manipulation */
  BLI_assert(gz->op_data == NULL);
  BLI_assert(prop != NULL);

  gz_prop->type = gz_prop_type;

  gz_prop->ptr = *ptr;
  gz_prop->prop = prop;
  gz_prop->index = index;

  if (gz->type->property_update) {
    gz->type->property_update(gz, gz_prop);
  }
}

void WM_gizmo_target_property_def_rna(
    wmGizmo *gz, const char *idname, PointerRNA *ptr, const char *propname, int index)
{
  const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
  PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
  if (prop == NULL) {
    RNA_warning("%s: %s.%s not found", __func__, RNA_struct_identifier(ptr->type), propname);
  }
  WM_gizmo_target_property_def_rna_ptr(gz, gz_prop_type, ptr, prop, index);
}

void WM_gizmo_target_property_def_func_ptr(wmGizmo *gz,
                                           const wmGizmoPropertyType *gz_prop_type,
                                           const wmGizmoPropertyFnParams *params)
{
  wmGizmoProperty *gz_prop = WM_gizmo_target_property_at_index(gz, gz_prop_type->index_in_type);

  /* if gizmo evokes an operator we cannot use it for property manipulation */
  BLI_assert(gz->op_data == NULL);

  gz_prop->type = gz_prop_type;

  gz_prop->custom_func.value_get_fn = params->value_get_fn;
  gz_prop->custom_func.value_set_fn = params->value_set_fn;
  gz_prop->custom_func.range_get_fn = params->range_get_fn;
  gz_prop->custom_func.free_fn = params->free_fn;
  gz_prop->custom_func.user_data = params->user_data;

  if (gz->type->property_update) {
    gz->type->property_update(gz, gz_prop);
  }
}

void WM_gizmo_target_property_def_func(wmGizmo *gz,
                                       const char *idname,
                                       const wmGizmoPropertyFnParams *params)
{
  const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
  WM_gizmo_target_property_def_func_ptr(gz, gz_prop_type, params);
}

void WM_gizmo_target_property_clear_rna_ptr(wmGizmo *gz, const wmGizmoPropertyType *gz_prop_type)
{
  wmGizmoProperty *gz_prop = WM_gizmo_target_property_at_index(gz, gz_prop_type->index_in_type);

  /* if gizmo evokes an operator we cannot use it for property manipulation */
  BLI_assert(gz->op_data == NULL);

  gz_prop->type = NULL;

  gz_prop->ptr = PointerRNA_NULL;
  gz_prop->prop = NULL;
  gz_prop->index = -1;
}

void WM_gizmo_target_property_clear_rna(wmGizmo *gz, const char *idname)
{
  const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type, idname);
  WM_gizmo_target_property_clear_rna_ptr(gz, gz_prop_type);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Property Access
 * \{ */

bool WM_gizmo_target_property_is_valid_any(wmGizmo *gz)
{
  wmGizmoProperty *gz_prop_array = wm_gizmo_target_property_array(gz);
  for (int i = 0; i < gz->type->target_property_defs_len; i++) {
    wmGizmoProperty *gz_prop = &gz_prop_array[i];
    if (WM_gizmo_target_property_is_valid(gz_prop)) {
      return true;
    }
  }
  return false;
}

bool WM_gizmo_target_property_is_valid(const wmGizmoProperty *gz_prop)
{
  return ((gz_prop->prop != NULL) ||
          (gz_prop->custom_func.value_get_fn && gz_prop->custom_func.value_set_fn));
}

float WM_gizmo_target_property_float_get(const wmGizmo *gz, wmGizmoProperty *gz_prop)
{
  if (gz_prop->custom_func.value_get_fn) {
    float value = 0.0f;
    BLI_assert(gz_prop->type->array_length == 1);
    gz_prop->custom_func.value_get_fn(gz, gz_prop, &value);
    return value;
  }

  if (gz_prop->index == -1) {
    return RNA_property_float_get(&gz_prop->ptr, gz_prop->prop);
  }
  return RNA_property_float_get_index(&gz_prop->ptr, gz_prop->prop, gz_prop->index);
}

void WM_gizmo_target_property_float_set(bContext *C,
                                        const wmGizmo *gz,
                                        wmGizmoProperty *gz_prop,
                                        const float value)
{
  if (gz_prop->custom_func.value_set_fn) {
    BLI_assert(gz_prop->type->array_length == 1);
    gz_prop->custom_func.value_set_fn(gz, gz_prop, &value);
    return;
  }

  /* reset property */
  if (gz_prop->index == -1) {
    RNA_property_float_set(&gz_prop->ptr, gz_prop->prop, value);
  }
  else {
    RNA_property_float_set_index(&gz_prop->ptr, gz_prop->prop, gz_prop->index, value);
  }
  RNA_property_update(C, &gz_prop->ptr, gz_prop->prop);
}

void WM_gizmo_target_property_float_get_array(const wmGizmo *gz,
                                              wmGizmoProperty *gz_prop,
                                              float *value)
{
  if (gz_prop->custom_func.value_get_fn) {
    gz_prop->custom_func.value_get_fn(gz, gz_prop, value);
    return;
  }
  RNA_property_float_get_array(&gz_prop->ptr, gz_prop->prop, value);
}

void WM_gizmo_target_property_float_set_array(bContext *C,
                                              const wmGizmo *gz,
                                              wmGizmoProperty *gz_prop,
                                              const float *value)
{
  if (gz_prop->custom_func.value_set_fn) {
    gz_prop->custom_func.value_set_fn(gz, gz_prop, value);
    return;
  }
  RNA_property_float_set_array(&gz_prop->ptr, gz_prop->prop, value);

  RNA_property_update(C, &gz_prop->ptr, gz_prop->prop);
}

bool WM_gizmo_target_property_float_range_get(const wmGizmo *gz,
                                              wmGizmoProperty *gz_prop,
                                              float range[2])
{
  if (gz_prop->custom_func.value_get_fn) {
    if (gz_prop->custom_func.range_get_fn) {
      gz_prop->custom_func.range_get_fn(gz, gz_prop, range);
      return true;
    }
    return false;
  }

  float step, precision;
  RNA_property_float_ui_range(
      &gz_prop->ptr, gz_prop->prop, &range[0], &range[1], &step, &precision);
  return true;
}

int WM_gizmo_target_property_array_length(const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop)
{
  if (gz_prop->custom_func.value_get_fn) {
    return gz_prop->type->array_length;
  }
  return RNA_property_array_length(&gz_prop->ptr, gz_prop->prop);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Property Define
 * \{ */

const wmGizmoPropertyType *WM_gizmotype_target_property_find(const wmGizmoType *gzt,
                                                             const char *idname)
{
  return BLI_findstring(&gzt->target_property_defs, idname, offsetof(wmGizmoPropertyType, idname));
}

void WM_gizmotype_target_property_def(wmGizmoType *gzt,
                                      const char *idname,
                                      int data_type,
                                      int array_length)
{
  wmGizmoPropertyType *mpt;

  BLI_assert(WM_gizmotype_target_property_find(gzt, idname) == NULL);

  const uint idname_size = strlen(idname) + 1;
  mpt = MEM_callocN(sizeof(wmGizmoPropertyType) + idname_size, __func__);
  memcpy(mpt->idname, idname, idname_size);
  mpt->data_type = data_type;
  mpt->array_length = array_length;
  mpt->index_in_type = gzt->target_property_defs_len;
  gzt->target_property_defs_len += 1;
  BLI_addtail(&gzt->target_property_defs, mpt);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Property Utilities
 * \{ */

void WM_gizmo_do_msg_notify_tag_refresh(bContext *UNUSED(C),
                                        wmMsgSubscribeKey *UNUSED(msg_key),
                                        wmMsgSubscribeValue *msg_val)
{
  ARegion *region = msg_val->owner;
  wmGizmoMap *gzmap = msg_val->user_data;

  /* Could possibly avoid a full redraw and only tag for editor overlays
   * redraw in some cases, see #ED_region_tag_redraw_editor_overlays(). */
  ED_region_tag_redraw(region);

  WM_gizmomap_tag_refresh(gzmap);
}

void WM_gizmo_target_property_subscribe_all(wmGizmo *gz, struct wmMsgBus *mbus, ARegion *region)
{
  if (gz->type->target_property_defs_len) {
    wmGizmoProperty *gz_prop_array = WM_gizmo_target_property_array(gz);
    for (int i = 0; i < gz->type->target_property_defs_len; i++) {
      wmGizmoProperty *gz_prop = &gz_prop_array[i];
      if (WM_gizmo_target_property_is_valid(gz_prop)) {
        if (gz_prop->prop) {
          WM_msg_subscribe_rna(mbus,
                               &gz_prop->ptr,
                               gz_prop->prop,
                               &(const wmMsgSubscribeValue){
                                   .owner = region,
                                   .user_data = region,
                                   .notify = ED_region_do_msg_notify_tag_redraw,
                               },
                               __func__);
          WM_msg_subscribe_rna(mbus,
                               &gz_prop->ptr,
                               gz_prop->prop,
                               &(const wmMsgSubscribeValue){
                                   .owner = region,
                                   .user_data = gz->parent_gzgroup->parent_gzmap,
                                   .notify = WM_gizmo_do_msg_notify_tag_refresh,
                               },
                               __func__);
        }
      }
    }
  }
}

void WM_gizmo_target_property_anim_autokey(bContext *C,
                                           const wmGizmo *UNUSED(gz),
                                           wmGizmoProperty *gz_prop)
{
  if (gz_prop->prop != NULL) {
    Scene *scene = CTX_data_scene(C);
    const float cfra = (float)scene->r.cfra;
    const int index = gz_prop->index == -1 ? 0 : gz_prop->index;
    ED_autokeyframe_property(C, scene, &gz_prop->ptr, gz_prop->prop, index, cfra, false);
  }
}

/** \} */