doxygen: add newline after \file
[blender.git] / source / blender / editors / gizmo_library / gizmo_types / cage3d_gizmo.c
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  * The Original Code is Copyright (C) 2014 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup edgizmolib
22  *
23  * \name Cage Gizmo
24  *
25  * 2D Gizmo
26  *
27  * \brief Rectangular gizmo acting as a 'cage' around its content.
28  * Interacting scales or translates the gizmo.
29  */
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_math.h"
34
35 #include "BKE_context.h"
36
37 #include "GPU_matrix.h"
38 #include "GPU_shader.h"
39 #include "GPU_immediate.h"
40 #include "GPU_immediate_util.h"
41 #include "GPU_select.h"
42 #include "GPU_state.h"
43
44 #include "RNA_access.h"
45 #include "RNA_define.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49
50 #include "ED_screen.h"
51 #include "ED_view3d.h"
52 #include "ED_gizmo_library.h"
53
54 /* own includes */
55 #include "../gizmo_library_intern.h"
56
57 #define GIZMO_RESIZER_SIZE 10.0f
58 #define GIZMO_MARGIN_OFFSET_SCALE 1.5f
59
60 static void gizmo_calc_matrix_final_no_offset(
61         const wmGizmo *gz, float orig_matrix_final_no_offset[4][4], bool use_space)
62 {
63         float mat_identity[4][4];
64         struct WM_GizmoMatrixParams params = {NULL};
65         unit_m4(mat_identity);
66         if (use_space == false) {
67                 params.matrix_basis = mat_identity;
68         }
69         params.matrix_offset = mat_identity;
70         WM_gizmo_calc_matrix_final_params(gz, &params, orig_matrix_final_no_offset);
71 }
72
73 static void gizmo_calc_rect_view_scale(
74         const wmGizmo *gz, const float dims[3], float scale[3])
75 {
76         UNUSED_VARS(dims);
77
78         /* Unlike cage2d, no need to correct for aspect. */
79         float matrix_final_no_offset[4][4];
80
81         float x_axis[3], y_axis[3], z_axis[3];
82         gizmo_calc_matrix_final_no_offset(gz, matrix_final_no_offset, false);
83         mul_v3_mat3_m4v3(x_axis, matrix_final_no_offset, gz->matrix_offset[0]);
84         mul_v3_mat3_m4v3(y_axis, matrix_final_no_offset, gz->matrix_offset[1]);
85         mul_v3_mat3_m4v3(z_axis, matrix_final_no_offset, gz->matrix_offset[2]);
86
87         scale[0] = 1.0f / len_v3(x_axis);
88         scale[1] = 1.0f / len_v3(y_axis);
89         scale[2] = 1.0f / len_v3(z_axis);
90 }
91
92 static void gizmo_calc_rect_view_margin(
93         const wmGizmo *gz, const float dims[3], float margin[3])
94 {
95         float handle_size;
96         if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) {
97                 handle_size = 0.15f;
98         }
99         else {
100                 handle_size = GIZMO_RESIZER_SIZE;
101         }
102         // XXX, the scale isn't taking offset into account, we need to calculate scale per handle!
103         // handle_size *= gz->scale_final;
104
105         float scale_xyz[3];
106         gizmo_calc_rect_view_scale(gz, dims, scale_xyz);
107         margin[0] = ((handle_size * scale_xyz[0]));
108         margin[1] = ((handle_size * scale_xyz[1]));
109         margin[2] = ((handle_size * scale_xyz[2]));
110 }
111
112 /* -------------------------------------------------------------------- */
113
114 static void gizmo_rect_pivot_from_scale_part(int part, float r_pt[3], bool r_constrain_axis[3])
115 {
116         if (part >= ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z &&
117             part <= ED_GIZMO_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z)
118         {
119                 int index = (part - ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z);
120                 int range[3];
121                 range[2] = index % 3;
122                 index    = index / 3;
123                 range[1] = index % 3;
124                 index    = index / 3;
125                 range[0] = index % 3;
126
127                 const float sign[3] = {0.5f, 0.0f, -0.5f};
128                 for (int i = 0; i < 3; i++) {
129                         r_pt[i] = sign[range[i]];
130                         r_constrain_axis[i] = (range[i] == 1);
131                 }
132         }
133 }
134
135 /* -------------------------------------------------------------------- */
136 /** \name Box Draw Style
137  *
138  * Useful for 3D views, see: #ED_GIZMO_CAGE2D_STYLE_BOX
139  * \{ */
140
141 static void cage3d_draw_box_corners(
142         const float r[3], const float margin[3], const float color[3])
143 {
144         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
145         UNUSED_VARS(margin);
146
147         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
148         immUniformColor3fv(color);
149
150         imm_draw_cube_wire_3d(pos, (float[3]){0}, r);
151
152         immUnbindProgram();
153 }
154
155 static void cage3d_draw_box_interaction(
156         const float color[4], const int highlighted,
157         const float size[3], const float margin[3])
158 {
159         if (highlighted >= ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z &&
160             highlighted <= ED_GIZMO_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z)
161         {
162                 int index = (highlighted - ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z);
163                 int range[3];
164                 range[2] = index % 3;
165                 index    = index / 3;
166                 range[1] = index % 3;
167                 index    = index / 3;
168                 range[0] = index % 3;
169
170                 const float sign[3] = {-1.0f, 0.0f, 1.0f};
171                 float co[3];
172
173                 for (int i = 0; i < 3; i++) {
174                         co[i] = size[i] * sign[range[i]];
175                 }
176                 const float rad[3] = {margin[0] / 3, margin[1] / 3, margin[2] / 3};
177
178                 {
179                         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
180                         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
181                         immUniformColor3fv(color);
182                         imm_draw_cube_fill_3d(pos, co, rad);
183                         immUnbindProgram();
184                 }
185         }
186 }
187
188 /** \} */
189
190 /* -------------------------------------------------------------------- */
191 /** \name Circle Draw Style
192  *
193  * Useful for 2D views, see: #ED_GIZMO_CAGE2D_STYLE_CIRCLE
194  * \{ */
195
196 static void imm_draw_point_aspect_3d(
197         uint pos, const float co[3], const float rad[3], bool solid)
198 {
199         if (solid) {
200                 imm_draw_cube_fill_3d(pos, co, rad);
201         }
202         else {
203                 imm_draw_cube_wire_3d(pos, co, rad);
204         }
205 }
206
207 static void cage3d_draw_circle_wire(
208         const float r[3], const float margin[3], const float color[3],
209         const int transform_flag, const int draw_options)
210 {
211         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
212
213         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
214         immUniformColor3fv(color);
215
216         imm_draw_cube_wire_3d(pos, (float[3]){0}, r);
217
218 #if 0
219         if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) {
220                 if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
221                         const float rad[2] = {margin[0] / 2, margin[1] / 2};
222                         const float center[2] = {0.0f, 0.0f};
223
224                         immBegin(GPU_PRIM_LINES, 4);
225                         immVertex2f(pos, center[0] - rad[0], center[1] - rad[1]);
226                         immVertex2f(pos, center[0] + rad[0], center[1] + rad[1]);
227                         immVertex2f(pos, center[0] + rad[0], center[1] - rad[1]);
228                         immVertex2f(pos, center[0] - rad[0], center[1] + rad[1]);
229                         immEnd();
230                 }
231         }
232 #else
233         UNUSED_VARS(margin, transform_flag, draw_options);
234 #endif
235
236
237         immUnbindProgram();
238 }
239
240 static void cage3d_draw_circle_handles(
241         const RegionView3D *rv3d, const float matrix_final[4][4],
242         const float r[3], const float margin[3], const float color[3],
243         bool solid, float scale)
244 {
245         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
246         const float rad[3] = {margin[0] / 3, margin[1] / 3, margin[2] / 3};
247
248         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
249         immUniformColor3fv(color);
250
251         float sign[3] = {-1.0f, 0.0f, 1.0f};
252         for (int x = 0; x < 3; x++) {
253                 for (int y = 0; y < 3; y++) {
254                         for (int z = 0; z < 3; z++) {
255                                 if (x == 1 && y == 1 && z == 1) {
256                                         continue;
257                                 }
258                                 const float co[3] = {r[0] * sign[x], r[1] * sign[y], r[2] * sign[z]};
259                                 float co_test[3];
260                                 mul_v3_m4v3(co_test, matrix_final, co);
261                                 float rad_scale[3];
262                                 mul_v3_v3fl(rad_scale, rad, ED_view3d_pixel_size(rv3d, co_test) * scale);
263                                 imm_draw_point_aspect_3d(pos, co, rad_scale, solid);
264                         }
265                 }
266         }
267
268         immUnbindProgram();
269 }
270
271 /** \} */
272
273 static void gizmo_cage3d_draw_intern(
274         RegionView3D *rv3d,
275         wmGizmo *gz, const bool select, const bool highlight, const int select_id)
276 {
277         // const bool use_clamp = (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) == 0;
278         float dims[3];
279         RNA_float_get_array(gz->ptr, "dimensions", dims);
280         float matrix_final[4][4];
281
282         const int transform_flag = RNA_enum_get(gz->ptr, "transform");
283         const int draw_style = RNA_enum_get(gz->ptr, "draw_style");
284         const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
285
286         const float size_real[3] = {dims[0] / 2.0f, dims[1] / 2.0f, dims[2] / 2.0f};
287
288         WM_gizmo_calc_matrix_final(gz, matrix_final);
289
290         GPU_matrix_push();
291         GPU_matrix_mul(matrix_final);
292
293         float margin[3];
294         gizmo_calc_rect_view_margin(gz, dims, margin);
295
296         /* Handy for quick testing draw (if it's outside bounds). */
297         if (false) {
298                 GPU_blend(true);
299                 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
300                 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
301                 immUniformColor4fv((const float[4]){1, 1, 1, 0.5f});
302                 float s = 0.5f;
303                 immRectf(pos, -s, -s, s, s);
304                 immUnbindProgram();
305                 GPU_blend(false);
306         }
307
308         if (select) {
309                 /* expand for hotspot */
310 #if 0
311                 const float size[3] = {
312                     size_real[0] + margin[0] / 2,
313                     size_real[1] + margin[1] / 2,
314                     size_real[2] + margin[2] / 2,
315                 };
316 #else
317                 /* just use same value for now. */
318                 const float size[3] = {UNPACK3(size_real)};
319 #endif
320
321
322                 if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE) {
323                         for (int i = ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z;
324                              i <= ED_GIZMO_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z;
325                              i++)
326                         {
327                                 if (i == ED_GIZMO_CAGE3D_PART_SCALE_MID_X_MID_Y_MID_Z) {
328                                         continue;
329                                 }
330                                 GPU_select_load_id(select_id | i);
331                                 cage3d_draw_box_interaction(
332                                         gz->color, i, size, margin);
333                         }
334                 }
335                 if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) {
336                         const int transform_part = ED_GIZMO_CAGE3D_PART_TRANSLATE;
337                         GPU_select_load_id(select_id | transform_part);
338                         cage3d_draw_box_interaction(
339                                 gz->color, transform_part, size, margin);
340                 }
341         }
342         else {
343 #if 0
344                 const rctf _r = {
345                         .xmin = -size_real[0],
346                         .ymin = -size_real[1],
347                         .xmax = size_real[0],
348                         .ymax = size_real[1],
349                 };
350 #endif
351                 if (draw_style == ED_GIZMO_CAGE2D_STYLE_BOX) {
352                         /* corner gizmos */
353                         GPU_line_width(gz->line_width + 3.0f);
354                         cage3d_draw_box_corners(size_real, margin, (const float[3]){0, 0, 0});
355
356                         /* corner gizmos */
357                         float color[4];
358                         gizmo_color_get(gz, highlight, color);
359                         GPU_line_width(gz->line_width);
360                         cage3d_draw_box_corners(size_real, margin, color);
361
362                         bool show = false;
363                         if (gz->highlight_part == ED_GIZMO_CAGE3D_PART_TRANSLATE) {
364                                 /* Only show if we're drawing the center handle
365                                  * otherwise the entire rectangle is the hotspot. */
366                                 if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
367                                         show = true;
368                                 }
369                         }
370                         else {
371                                 show = true;
372                         }
373
374                         if (show) {
375                                 cage3d_draw_box_interaction(
376                                         gz->color, gz->highlight_part, size_real, margin);
377                         }
378                 }
379                 else if (draw_style == ED_GIZMO_CAGE2D_STYLE_CIRCLE) {
380                         float color[4];
381                         gizmo_color_get(gz, highlight, color);
382
383                         GPU_line_smooth(true);
384                         GPU_polygon_smooth(true);
385                         GPU_blend(true);
386
387                         GPU_line_width(gz->line_width + 3.0f);
388                         cage3d_draw_circle_wire(size_real, margin, (const float[3]){0, 0, 0}, transform_flag, draw_options);
389                         GPU_line_width(gz->line_width);
390                         cage3d_draw_circle_wire(size_real, margin, color, transform_flag, draw_options);
391
392                         /* corner gizmos */
393                         cage3d_draw_circle_handles(rv3d, matrix_final, size_real, margin, (const float[3]){0, 0, 0}, true, 60);
394                         cage3d_draw_circle_handles(rv3d, matrix_final, size_real, margin, color, true, 40);
395
396                         GPU_blend(false);
397                         GPU_polygon_smooth(false);
398                         GPU_line_smooth(false);
399                 }
400                 else {
401                         BLI_assert(0);
402                 }
403         }
404
405         GPU_line_width(1.0);
406         GPU_matrix_pop();
407 }
408
409 /**
410  * For when we want to draw 3d cage in 3d views.
411  */
412 static void gizmo_cage3d_draw_select(const bContext *C, wmGizmo *gz, int select_id)
413 {
414         ARegion *ar = CTX_wm_region(C);
415         RegionView3D *rv3d = ar->regiondata;
416         gizmo_cage3d_draw_intern(rv3d, gz, true, false, select_id);
417 }
418
419 static void gizmo_cage3d_draw(const bContext *C, wmGizmo *gz)
420 {
421         ARegion *ar = CTX_wm_region(C);
422         RegionView3D *rv3d = ar->regiondata;
423         const bool is_highlight = (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0;
424         gizmo_cage3d_draw_intern(rv3d, gz, false, is_highlight, -1);
425 }
426
427 static int gizmo_cage3d_get_cursor(wmGizmo *gz)
428 {
429         if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) {
430                 return BC_NSEW_SCROLLCURSOR;
431         }
432
433         return CURSOR_STD;
434 }
435
436 typedef struct RectTransformInteraction {
437         float orig_mouse[3];
438         float orig_matrix_offset[4][4];
439         float orig_matrix_final_no_offset[4][4];
440 } RectTransformInteraction;
441
442 static void gizmo_cage3d_setup(wmGizmo *gz)
443 {
444         gz->flag |= /* WM_GIZMO_DRAW_MODAL | */ /* TODO */
445                      WM_GIZMO_DRAW_NO_SCALE;
446 }
447
448 static int gizmo_cage3d_invoke(
449         bContext *C, wmGizmo *gz, const wmEvent *event)
450 {
451         RectTransformInteraction *data = MEM_callocN(sizeof(RectTransformInteraction), "cage_interaction");
452
453         copy_m4_m4(data->orig_matrix_offset, gz->matrix_offset);
454         gizmo_calc_matrix_final_no_offset(gz, data->orig_matrix_final_no_offset, true);
455
456         if (gizmo_window_project_3d(
457                 C, gz, (const float[2]){UNPACK2(event->mval)}, false, data->orig_mouse) == 0)
458         {
459                 zero_v3(data->orig_mouse);
460         }
461
462         gz->interaction_data = data;
463
464         return OPERATOR_RUNNING_MODAL;
465 }
466
467 static int gizmo_cage3d_modal(
468         bContext *C, wmGizmo *gz, const wmEvent *event,
469         eWM_GizmoFlagTweak UNUSED(tweak_flag))
470 {
471         if (event->type != MOUSEMOVE) {
472                 return OPERATOR_RUNNING_MODAL;
473         }
474         /* For transform logic to be manageable we operate in -0.5..0.5 2D space,
475          * no matter the size of the rectangle, mouse coorts are scaled to unit space.
476          * The mouse coords have been projected into the matrix so we don't need to worry about axis alignment.
477          *
478          * - The cursor offset are multiplied by 'dims'.
479          * - Matrix translation is also multiplied by 'dims'.
480          */
481         RectTransformInteraction *data = gz->interaction_data;
482         float point_local[3];
483
484         float dims[3];
485         RNA_float_get_array(gz->ptr, "dimensions", dims);
486
487         {
488                 float matrix_back[4][4];
489                 copy_m4_m4(matrix_back, gz->matrix_offset);
490                 copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset);
491
492                 bool ok = gizmo_window_project_3d(
493                         C, gz, (const float[2]){UNPACK2(event->mval)}, false, point_local);
494                 copy_m4_m4(gz->matrix_offset, matrix_back);
495                 if (!ok) {
496                         return OPERATOR_RUNNING_MODAL;
497                 }
498         }
499
500         const int transform_flag = RNA_enum_get(gz->ptr, "transform");
501         wmGizmoProperty *gz_prop;
502
503         gz_prop = WM_gizmo_target_property_find(gz, "matrix");
504         if (gz_prop->type != NULL) {
505                 WM_gizmo_target_property_float_get_array(gz, gz_prop, &gz->matrix_offset[0][0]);
506         }
507
508         if (gz->highlight_part == ED_GIZMO_CAGE3D_PART_TRANSLATE) {
509                 /* do this to prevent clamping from changing size */
510                 copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset);
511                 gz->matrix_offset[3][0] = data->orig_matrix_offset[3][0] + (point_local[0] - data->orig_mouse[0]);
512                 gz->matrix_offset[3][1] = data->orig_matrix_offset[3][1] + (point_local[1] - data->orig_mouse[1]);
513                 gz->matrix_offset[3][2] = data->orig_matrix_offset[3][2] + (point_local[2] - data->orig_mouse[2]);
514         }
515         else if (gz->highlight_part == ED_GIZMO_CAGE3D_PART_ROTATE) {
516                 /* TODO (if needed) */
517         }
518         else {
519                 /* scale */
520                 copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset);
521                 float pivot[3];
522                 bool constrain_axis[3] = {false};
523
524                 if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) {
525                         gizmo_rect_pivot_from_scale_part(gz->highlight_part, pivot, constrain_axis);
526                 }
527                 else {
528                         zero_v3(pivot);
529                 }
530
531                 /* Cursor deltas scaled to (-0.5..0.5). */
532                 float delta_orig[3], delta_curr[3];
533
534                 for (int i = 0; i < 3; i++) {
535                         delta_orig[i] = ((data->orig_mouse[i] - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i];
536                         delta_curr[i] = ((point_local[i]      - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i];
537                 }
538
539                 float scale[3] = {1.0f, 1.0f, 1.0f};
540                 for (int i = 0; i < 3; i++) {
541                         if (constrain_axis[i] == false) {
542                                 if (delta_orig[i] < 0.0f) {
543                                         delta_orig[i] *= -1.0f;
544                                         delta_curr[i] *= -1.0f;
545                                 }
546                                 const int sign = signum_i(scale[i]);
547
548                                 scale[i] = 1.0f + ((delta_curr[i] - delta_orig[i]) / len_v3(data->orig_matrix_offset[i]));
549
550                                 if ((transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_SIGNED) == 0) {
551                                         if (sign != signum_i(scale[i])) {
552                                                 scale[i] = 0.0f;
553                                         }
554                                 }
555                         }
556                 }
557
558                 if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM) {
559                         if (constrain_axis[0] == false && constrain_axis[1] == false) {
560                                 scale[1] = scale[0] = (scale[1] + scale[0]) / 2.0f;
561                         }
562                         else if (constrain_axis[0] == false) {
563                                 scale[1] = scale[0];
564                         }
565                         else if (constrain_axis[1] == false) {
566                                 scale[0] = scale[1];
567                         }
568                         else {
569                                 BLI_assert(0);
570                         }
571                 }
572
573                 /* scale around pivot */
574                 float matrix_scale[4][4];
575                 unit_m4(matrix_scale);
576
577                 mul_v3_fl(matrix_scale[0], scale[0]);
578                 mul_v3_fl(matrix_scale[1], scale[1]);
579                 mul_v3_fl(matrix_scale[2], scale[2]);
580
581                 transform_pivot_set_m4(
582                         matrix_scale,
583                         (const float[3]){pivot[0] * dims[0], pivot[1] * dims[1], pivot[2] * dims[2]});
584                 mul_m4_m4m4(gz->matrix_offset, data->orig_matrix_offset, matrix_scale);
585         }
586
587         if (gz_prop->type != NULL) {
588                 WM_gizmo_target_property_float_set_array(C, gz, gz_prop, &gz->matrix_offset[0][0]);
589         }
590
591         /* tag the region for redraw */
592         ED_region_tag_redraw(CTX_wm_region(C));
593         WM_event_add_mousemove(C);
594
595         return OPERATOR_RUNNING_MODAL;
596 }
597
598 static void gizmo_cage3d_property_update(wmGizmo *gz, wmGizmoProperty *gz_prop)
599 {
600         if (STREQ(gz_prop->type->idname, "matrix")) {
601                 if (WM_gizmo_target_property_array_length(gz, gz_prop) == 16) {
602                         WM_gizmo_target_property_float_get_array(gz, gz_prop, &gz->matrix_offset[0][0]);
603                 }
604                 else {
605                         BLI_assert(0);
606                 }
607         }
608         else {
609                 BLI_assert(0);
610         }
611 }
612
613 static void gizmo_cage3d_exit(bContext *C, wmGizmo *gz, const bool cancel)
614 {
615         RectTransformInteraction *data = gz->interaction_data;
616
617         if (!cancel)
618                 return;
619
620         wmGizmoProperty *gz_prop;
621
622         /* reset properties */
623         gz_prop = WM_gizmo_target_property_find(gz, "matrix");
624         if (gz_prop->type != NULL) {
625                 WM_gizmo_target_property_float_set_array(C, gz, gz_prop, &data->orig_matrix_offset[0][0]);
626         }
627
628         copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset);
629 }
630
631
632 /* -------------------------------------------------------------------- */
633 /** \name Cage Gizmo API
634  *
635  * \{ */
636
637 static void GIZMO_GT_cage_3d(wmGizmoType *gzt)
638 {
639         /* identifiers */
640         gzt->idname = "GIZMO_GT_cage_3d";
641
642         /* api callbacks */
643         gzt->draw = gizmo_cage3d_draw;
644         gzt->draw_select = gizmo_cage3d_draw_select;
645         gzt->setup = gizmo_cage3d_setup;
646         gzt->invoke = gizmo_cage3d_invoke;
647         gzt->property_update = gizmo_cage3d_property_update;
648         gzt->modal = gizmo_cage3d_modal;
649         gzt->exit = gizmo_cage3d_exit;
650         gzt->cursor_get = gizmo_cage3d_get_cursor;
651
652         gzt->struct_size = sizeof(wmGizmo);
653
654         /* rna */
655         static EnumPropertyItem rna_enum_draw_style[] = {
656                 {ED_GIZMO_CAGE2D_STYLE_BOX, "BOX", 0, "Box", ""},
657                 {ED_GIZMO_CAGE2D_STYLE_CIRCLE, "CIRCLE", 0, "Circle", ""},
658                 {0, NULL, 0, NULL, NULL},
659         };
660         static EnumPropertyItem rna_enum_transform[] = {
661                 {ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE, "TRANSLATE", 0, "Move", ""},
662                 {ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE, "SCALE", 0, "Scale", ""},
663                 {ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM, "SCALE_UNIFORM", 0, "Scale Uniform", ""},
664                 {0, NULL, 0, NULL, NULL},
665         };
666         static EnumPropertyItem rna_enum_draw_options[] = {
667                 {ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "XFORM_CENTER_HANDLE", 0, "Center Handle", ""},
668                 {0, NULL, 0, NULL, NULL},
669         };
670         static float unit_v3[3] = {1.0f, 1.0f, 1.0f};
671         RNA_def_float_vector(gzt->srna, "dimensions", 3, unit_v3, 0, FLT_MAX, "Dimensions", "", 0.0f, FLT_MAX);
672         RNA_def_enum_flag(gzt->srna, "transform", rna_enum_transform, 0, "Transform Options", "");
673         RNA_def_enum(gzt->srna, "draw_style", rna_enum_draw_style, ED_GIZMO_CAGE2D_STYLE_CIRCLE, "Draw Style", "");
674         RNA_def_enum_flag(
675                 gzt->srna, "draw_options", rna_enum_draw_options,
676                 ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "Draw Options", "");
677
678         WM_gizmotype_target_property_def(gzt, "matrix", PROP_FLOAT, 16);
679 }
680
681 void ED_gizmotypes_cage_3d(void)
682 {
683         WM_gizmotype_append(GIZMO_GT_cage_3d);
684 }
685
686 /** \} */