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