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