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