Cleanup: rename gizmo access functions
[blender.git] / source / blender / editors / gizmo_library / gizmo_types / cage2d_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 cage2d_gizmo.c
27  *  \ingroup wm
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_dial_2d.h"
41 #include "BLI_rect.h"
42
43 #include "BKE_context.h"
44
45 #include "BIF_gl.h"
46
47 #include "GPU_matrix.h"
48 #include "GPU_shader.h"
49 #include "GPU_immediate.h"
50 #include "GPU_immediate_util.h"
51 #include "GPU_select.h"
52 #include "GPU_state.h"
53
54 #include "RNA_access.h"
55 #include "RNA_define.h"
56
57 #include "WM_api.h"
58 #include "WM_types.h"
59
60 #include "ED_screen.h"
61 #include "ED_view3d.h"
62 #include "ED_gizmo_library.h"
63
64 /* own includes */
65 #include "../gizmo_library_intern.h"
66
67 #define GIZMO_RESIZER_SIZE 10.0f
68 #define GIZMO_MARGIN_OFFSET_SCALE 1.5f
69
70 static void gizmo_calc_rect_view_scale(
71         const wmGizmo *gz, const float dims[2], float scale[2])
72 {
73         float matrix_final_no_offset[4][4];
74         float asp[2] = {1.0f, 1.0f};
75         if (dims[0] > dims[1]) {
76                 asp[0] = dims[1] / dims[0];
77         }
78         else {
79                 asp[1] = dims[0] / dims[1];
80         }
81         float x_axis[3], y_axis[3];
82         WM_gizmo_calc_matrix_final_no_offset(gz, matrix_final_no_offset);
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
86         mul_v2_v2(x_axis, asp);
87         mul_v2_v2(y_axis, asp);
88
89         scale[0] = 1.0f / len_v3(x_axis);
90         scale[1] = 1.0f / len_v3(y_axis);
91 }
92
93 static void gizmo_calc_rect_view_margin(
94         const wmGizmo *gz, const float dims[2], float margin[2])
95 {
96         float handle_size;
97         if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) {
98                 handle_size = 0.15f;
99         }
100         else {
101                 handle_size = GIZMO_RESIZER_SIZE;
102         }
103         handle_size *= gz->scale_final;
104         float scale_xy[2];
105         gizmo_calc_rect_view_scale(gz, dims, scale_xy);
106         margin[0] = ((handle_size * scale_xy[0]));
107         margin[1] = ((handle_size * scale_xy[1]));
108 }
109
110 /* -------------------------------------------------------------------- */
111
112 static void gizmo_rect_pivot_from_scale_part(int part, float r_pt[2], bool r_constrain_axis[2])
113 {
114         bool x = true, y = true;
115         switch (part) {
116                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X: { ARRAY_SET_ITEMS(r_pt,  0.5,  0.0); x = false; break; }
117                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X: { ARRAY_SET_ITEMS(r_pt, -0.5,  0.0); x = false; break; }
118                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y: { ARRAY_SET_ITEMS(r_pt,  0.0,  0.5); y = false; break; }
119                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y: { ARRAY_SET_ITEMS(r_pt,  0.0, -0.5); y = false; break; }
120                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y: { ARRAY_SET_ITEMS(r_pt,  0.5,  0.5); x = y = false; break; }
121                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y: { ARRAY_SET_ITEMS(r_pt,  0.5, -0.5); x = y = false; break; }
122                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y: { ARRAY_SET_ITEMS(r_pt, -0.5,  0.5); x = y = false; break; }
123                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y: { ARRAY_SET_ITEMS(r_pt, -0.5, -0.5); x = y = false; break; }
124                 default: BLI_assert(0);
125         }
126         r_constrain_axis[0] = x;
127         r_constrain_axis[1] = y;
128 }
129
130 /* -------------------------------------------------------------------- */
131 /** \name Box Draw Style
132  *
133  * Useful for 3D views, see: #ED_GIZMO_CAGE2D_STYLE_BOX
134  * \{ */
135
136 static void cage2d_draw_box_corners(
137         const rctf *r, const float margin[2], const float color[3])
138 {
139         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
140
141         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
142         immUniformColor3fv(color);
143
144         immBegin(GPU_PRIM_LINES, 16);
145
146         immVertex2f(pos, r->xmin, r->ymin + margin[1]);
147         immVertex2f(pos, r->xmin, r->ymin);
148         immVertex2f(pos, r->xmin, r->ymin);
149         immVertex2f(pos, r->xmin + margin[0], r->ymin);
150
151         immVertex2f(pos, r->xmax, r->ymin + margin[1]);
152         immVertex2f(pos, r->xmax, r->ymin);
153         immVertex2f(pos, r->xmax, r->ymin);
154         immVertex2f(pos, r->xmax - margin[0], r->ymin);
155
156         immVertex2f(pos, r->xmax, r->ymax - margin[1]);
157         immVertex2f(pos, r->xmax, r->ymax);
158         immVertex2f(pos, r->xmax, r->ymax);
159         immVertex2f(pos, r->xmax - margin[0], r->ymax);
160
161         immVertex2f(pos, r->xmin, r->ymax - margin[1]);
162         immVertex2f(pos, r->xmin, r->ymax);
163         immVertex2f(pos, r->xmin, r->ymax);
164         immVertex2f(pos, r->xmin + margin[0], r->ymax);
165
166         immEnd();
167
168         immUnbindProgram();
169 }
170
171 static void cage2d_draw_box_interaction(
172         const float color[4], const int highlighted,
173         const float size[2], const float margin[2],
174         const float line_width, const bool is_solid, const int draw_options)
175 {
176         /* 4 verts for translate, otherwise only 3 are used. */
177         float verts[4][2];
178         uint verts_len = 0;
179         GPUPrimType prim_type = GPU_PRIM_NONE;
180
181         switch (highlighted) {
182                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X:
183                 {
184                         rctf r = {
185                                 .xmin = -size[0], .xmax = -size[0] + margin[0],
186                                 .ymin = -size[1] + margin[1], .ymax = size[1] - margin[1],
187                         };
188                         ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymin);
189                         ARRAY_SET_ITEMS(verts[1], r.xmin, r.ymax);
190                         verts_len = 2;
191                         if (is_solid) {
192                                 ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymax);
193                                 ARRAY_SET_ITEMS(verts[3], r.xmax, r.ymin);
194                                 verts_len += 2;
195                                 prim_type = GPU_PRIM_TRI_FAN;
196                         }
197                         else {
198                                 prim_type = GPU_PRIM_LINE_STRIP;
199                         }
200                         break;
201                 }
202                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X:
203                 {
204                         rctf r = {
205                                 .xmin = size[0] - margin[0], .xmax = size[0],
206                                 .ymin = -size[1] + margin[1], .ymax = size[1] - margin[1],
207                         };
208                         ARRAY_SET_ITEMS(verts[0], r.xmax, r.ymin);
209                         ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymax);
210                         verts_len = 2;
211                         if (is_solid) {
212                                 ARRAY_SET_ITEMS(verts[2], r.xmin, r.ymax);
213                                 ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymin);
214                                 verts_len += 2;
215                                 prim_type = GPU_PRIM_TRI_FAN;
216                         }
217                         else {
218                                 prim_type = GPU_PRIM_LINE_STRIP;
219                         }
220                         break;
221                 }
222                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y:
223                 {
224                         rctf r = {
225                                 .xmin = -size[0] + margin[0], .xmax = size[0] - margin[0],
226                                 .ymin = -size[1], .ymax = -size[1] + margin[1],
227                         };
228                         ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymin);
229                         ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymin);
230                         verts_len = 2;
231                         if (is_solid) {
232                                 ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymax);
233                                 ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymax);
234                                 verts_len += 2;
235                                 prim_type = GPU_PRIM_TRI_FAN;
236                         }
237                         else {
238                                 prim_type = GPU_PRIM_LINE_STRIP;
239                         }
240                         break;
241                 }
242                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y:
243                 {
244                         rctf r = {
245                                 .xmin = -size[0] + margin[0], .xmax = size[0] - margin[0],
246                                 .ymin = size[1] - margin[1], .ymax = size[1],
247                         };
248                         ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymax);
249                         ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymax);
250                         verts_len = 2;
251                         if (is_solid) {
252                                 ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymin);
253                                 ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymin);
254                                 verts_len += 2;
255                                 prim_type = GPU_PRIM_TRI_FAN;
256                         }
257                         else {
258                                 prim_type = GPU_PRIM_LINE_STRIP;
259                         }
260                         break;
261                 }
262                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y:
263                 {
264                         rctf r = {
265                                 .xmin = -size[0], .xmax = -size[0] + margin[0],
266                                 .ymin = -size[1], .ymax = -size[1] + margin[1],
267                         };
268                         ARRAY_SET_ITEMS(verts[0], r.xmax, r.ymin);
269                         ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymax);
270                         ARRAY_SET_ITEMS(verts[2], r.xmin, r.ymax);
271                         verts_len = 3;
272                         if (is_solid) {
273                                 ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymin);
274                                 verts_len += 1;
275                                 prim_type = GPU_PRIM_TRI_FAN;
276                         }
277                         else {
278                                 prim_type = GPU_PRIM_LINE_STRIP;
279                         }
280                         break;
281                 }
282                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y:
283                 {
284                         rctf r = {
285                                 .xmin = -size[0], .xmax = -size[0] + margin[0],
286                                 .ymin = size[1] - margin[1], .ymax = size[1],
287                         };
288                         ARRAY_SET_ITEMS(verts[0], r.xmax, r.ymax);
289                         ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymin);
290                         ARRAY_SET_ITEMS(verts[2], r.xmin, r.ymin);
291                         verts_len = 3;
292                         if (is_solid) {
293                                 ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymax);
294                                 verts_len += 1;
295                                 prim_type = GPU_PRIM_TRI_FAN;
296                         }
297                         else {
298                                 prim_type = GPU_PRIM_LINE_STRIP;
299                         }
300                         break;
301                 }
302                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y:
303                 {
304                         rctf r = {
305                                 .xmin = size[0] - margin[0], .xmax = size[0],
306                                 .ymin = -size[1], .ymax = -size[1] + margin[1],
307                         };
308                         ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymin);
309                         ARRAY_SET_ITEMS(verts[1], r.xmin, r.ymax);
310                         ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymax);
311                         verts_len = 3;
312                         if (is_solid) {
313                                 ARRAY_SET_ITEMS(verts[3], r.xmax, r.ymin);
314                                 verts_len += 1;
315                                 prim_type = GPU_PRIM_TRI_FAN;
316                         }
317                         else {
318                                 prim_type = GPU_PRIM_LINE_STRIP;
319                         }
320                         break;
321                 }
322                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y:
323                 {
324                         rctf r = {
325                                 .xmin = size[0] - margin[0], .xmax = size[0],
326                                 .ymin = size[1] - margin[1], .ymax = size[1],
327                         };
328                         ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymax);
329                         ARRAY_SET_ITEMS(verts[1], r.xmin, r.ymin);
330                         ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymin);
331                         verts_len = 3;
332                         if (is_solid) {
333                                 ARRAY_SET_ITEMS(verts[3], r.xmax, r.ymax);
334                                 verts_len += 1;
335                                 prim_type = GPU_PRIM_TRI_FAN;
336                         }
337                         else {
338                                 prim_type = GPU_PRIM_LINE_STRIP;
339                         }
340                         break;
341                 }
342                 case ED_GIZMO_CAGE2D_PART_ROTATE:
343                 {
344                         const float rotate_pt[2] = {0.0f, size[1] + margin[1]};
345                         const rctf r_rotate = {
346                                 .xmin = rotate_pt[0] - margin[0] / 2.0f,
347                                 .xmax = rotate_pt[0] + margin[0] / 2.0f,
348                                 .ymin = rotate_pt[1] - margin[1] / 2.0f,
349                                 .ymax = rotate_pt[1] + margin[1] / 2.0f,
350                         };
351
352                         ARRAY_SET_ITEMS(verts[0], r_rotate.xmin, r_rotate.ymin);
353                         ARRAY_SET_ITEMS(verts[1], r_rotate.xmin, r_rotate.ymax);
354                         ARRAY_SET_ITEMS(verts[2], r_rotate.xmax, r_rotate.ymax);
355                         ARRAY_SET_ITEMS(verts[3], r_rotate.xmax, r_rotate.ymin);
356                         verts_len = 4;
357                         if (is_solid) {
358                                 prim_type = GPU_PRIM_TRI_FAN;
359                         }
360                         else {
361                                 prim_type = GPU_PRIM_LINE_STRIP;
362                         }
363                         break;
364                 }
365
366                 case ED_GIZMO_CAGE2D_PART_TRANSLATE:
367                         if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
368                                 ARRAY_SET_ITEMS(verts[0], -margin[0] / 2, -margin[1] / 2);
369                                 ARRAY_SET_ITEMS(verts[1],  margin[0] / 2,  margin[1] / 2);
370                                 ARRAY_SET_ITEMS(verts[2], -margin[0] / 2,  margin[1] / 2);
371                                 ARRAY_SET_ITEMS(verts[3],  margin[0] / 2, -margin[1] / 2);
372                                 verts_len = 4;
373                                 if (is_solid) {
374                                         prim_type = GPU_PRIM_TRI_FAN;
375                                 }
376                                 else {
377                                         prim_type = GPU_PRIM_LINES;
378                                 }
379                         }
380                         else {
381                                 /* Only used for 3D view selection, never displayed to the user. */
382                                 ARRAY_SET_ITEMS(verts[0], -size[0], -size[1]);
383                                 ARRAY_SET_ITEMS(verts[1], -size[0],  size[1]);
384                                 ARRAY_SET_ITEMS(verts[2], size[0],   size[1]);
385                                 ARRAY_SET_ITEMS(verts[3], size[0],  -size[1]);
386                                 verts_len = 4;
387                                 if (is_solid) {
388                                         prim_type = GPU_PRIM_TRI_FAN;
389                                 }
390                                 else {
391                                         /* unreachable */
392                                         BLI_assert(0);
393                                         prim_type = GPU_PRIM_LINE_STRIP;
394                                 }
395                         }
396                         break;
397                 default:
398                         return;
399         }
400
401         BLI_assert(prim_type != GPU_PRIM_NONE);
402
403         GPUVertFormat *format = immVertexFormat();
404         struct {
405                 uint pos, col;
406         } attr_id = {
407                 .pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT),
408                 .col = GPU_vertformat_attr_add(format, "color", GPU_COMP_F32, 3, GPU_FETCH_FLOAT),
409         };
410         immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
411
412         {
413                 if (is_solid) {
414                         BLI_assert(ELEM(prim_type, GPU_PRIM_TRI_FAN));
415                         immBegin(prim_type, verts_len);
416                         immAttrib3f(attr_id.col, 0.0f, 0.0f, 0.0f);
417                         for (uint i = 0; i < verts_len; i++) {
418                                 immVertex2fv(attr_id.pos, verts[i]);
419                         }
420                         immEnd();
421                 }
422                 else {
423                         BLI_assert(ELEM(prim_type, GPU_PRIM_LINE_STRIP, GPU_PRIM_LINES));
424                         GPU_line_width(line_width + 3.0f);
425
426                         immBegin(prim_type, verts_len);
427                         immAttrib3f(attr_id.col, 0.0f, 0.0f, 0.0f);
428                         for (uint i = 0; i < verts_len; i++) {
429                                 immVertex2fv(attr_id.pos, verts[i]);
430                         }
431                         immEnd();
432
433                         GPU_line_width(line_width);
434
435                         immBegin(prim_type, verts_len);
436                         immAttrib3fv(attr_id.col, color);
437                         for (uint i = 0; i < verts_len; i++) {
438                                 immVertex2fv(attr_id.pos, verts[i]);
439                         }
440                         immEnd();
441                 }
442         }
443
444         immUnbindProgram();
445 }
446
447 /** \} */
448
449 /* -------------------------------------------------------------------- */
450 /** \name Circle Draw Style
451  *
452  * Useful for 2D views, see: #ED_GIZMO_CAGE2D_STYLE_CIRCLE
453  * \{ */
454
455 static void imm_draw_point_aspect_2d(
456         uint pos, float x, float y, float rad_x, float rad_y, bool solid)
457 {
458         immBegin(solid ? GPU_PRIM_TRI_FAN : GPU_PRIM_LINE_LOOP, 4);
459         immVertex2f(pos, x - rad_x, y - rad_y);
460         immVertex2f(pos, x - rad_x, y + rad_y);
461         immVertex2f(pos, x + rad_x, y + rad_y);
462         immVertex2f(pos, x + rad_x, y - rad_y);
463         immEnd();
464 }
465
466 static void cage2d_draw_circle_wire(
467         const rctf *r, const float margin[2], const float color[3],
468         const int transform_flag, const int draw_options)
469 {
470         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
471
472         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
473         immUniformColor3fv(color);
474
475         immBegin(GPU_PRIM_LINE_LOOP, 4);
476         immVertex2f(pos, r->xmin, r->ymin);
477         immVertex2f(pos, r->xmax, r->ymin);
478         immVertex2f(pos, r->xmax, r->ymax);
479         immVertex2f(pos, r->xmin, r->ymax);
480         immEnd();
481
482         if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE) {
483                 immBegin(GPU_PRIM_LINE_LOOP, 2);
484                 immVertex2f(pos, BLI_rctf_cent_x(r), r->ymax);
485                 immVertex2f(pos, BLI_rctf_cent_x(r), r->ymax + margin[1]);
486                 immEnd();
487         }
488
489         if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) {
490                 if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
491                         const float rad[2] = {margin[0] / 2, margin[1] / 2};
492                         const float center[2] = {BLI_rctf_cent_x(r), BLI_rctf_cent_y(r)};
493
494                         immBegin(GPU_PRIM_LINES, 4);
495                         immVertex2f(pos, center[0] - rad[0], center[1] - rad[1]);
496                         immVertex2f(pos, center[0] + rad[0], center[1] + rad[1]);
497                         immVertex2f(pos, center[0] + rad[0], center[1] - rad[1]);
498                         immVertex2f(pos, center[0] - rad[0], center[1] + rad[1]);
499                         immEnd();
500                 }
501         }
502
503         immUnbindProgram();
504 }
505
506 static void cage2d_draw_circle_handles(
507         const rctf *r, const float margin[2], const float color[3],
508         const int transform_flag,
509         bool solid)
510 {
511         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
512         void (*circle_fn)(uint, float, float, float, float, int) =
513                 (solid) ? imm_draw_circle_fill_aspect_2d : imm_draw_circle_wire_aspect_2d;
514         const int resolu = 12;
515         const float rad[2] = {margin[0] / 3, margin[1] / 3};
516
517         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
518         immUniformColor3fv(color);
519
520         /* should  really divide by two, but looks too bulky. */
521         {
522                 imm_draw_point_aspect_2d(pos, r->xmin, r->ymin, rad[0], rad[1], solid);
523                 imm_draw_point_aspect_2d(pos, r->xmax, r->ymin, rad[0], rad[1], solid);
524                 imm_draw_point_aspect_2d(pos, r->xmax, r->ymax, rad[0], rad[1], solid);
525                 imm_draw_point_aspect_2d(pos, r->xmin, r->ymax, rad[0], rad[1], solid);
526         }
527
528         if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE) {
529                 const float handle[2] = {BLI_rctf_cent_x(r), r->ymax + (margin[1] * GIZMO_MARGIN_OFFSET_SCALE)};
530                 circle_fn(pos, handle[0], handle[1], rad[0], rad[1], resolu);
531         }
532
533         immUnbindProgram();
534 }
535
536 /** \} */
537
538 static void gizmo_cage2d_draw_intern(
539         wmGizmo *gz, const bool select, const bool highlight, const int select_id)
540 {
541         // const bool use_clamp = (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) == 0;
542         float dims[2];
543         RNA_float_get_array(gz->ptr, "dimensions", dims);
544         float matrix_final[4][4];
545
546         const int transform_flag = RNA_enum_get(gz->ptr, "transform");
547         const int draw_style = RNA_enum_get(gz->ptr, "draw_style");
548         const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
549
550         const float size_real[2] = {dims[0] / 2.0f, dims[1] / 2.0f};
551
552         WM_gizmo_calc_matrix_final(gz, matrix_final);
553
554         GPU_matrix_push();
555         GPU_matrix_mul(matrix_final);
556
557         float margin[2];
558         gizmo_calc_rect_view_margin(gz, dims, margin);
559
560         /* Handy for quick testing draw (if it's outside bounds). */
561         if (false) {
562                 GPU_blend(true);
563                 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
564                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
565                 immUniformColor4fv((const float[4]){1, 1, 1, 0.5f});
566                 float s = 0.5f;
567                 immRectf(pos, -s, -s, s, s);
568                 immUnbindProgram();
569                 GPU_blend(false);
570         }
571
572         if (select) {
573                 /* expand for hotspot */
574                 const float size[2] = {size_real[0] + margin[0] / 2, size_real[1] + margin[1] / 2};
575
576                 if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE) {
577                         int scale_parts[] = {
578                                 ED_GIZMO_CAGE2D_PART_SCALE_MIN_X,
579                                 ED_GIZMO_CAGE2D_PART_SCALE_MAX_X,
580                                 ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y,
581                                 ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y,
582
583                                 ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y,
584                                 ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y,
585                                 ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y,
586                                 ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y,
587                         };
588                         for (int i = 0; i < ARRAY_SIZE(scale_parts); i++) {
589                                 GPU_select_load_id(select_id | scale_parts[i]);
590                                 cage2d_draw_box_interaction(
591                                         gz->color, scale_parts[i], size, margin, gz->line_width, true, draw_options);
592                         }
593                 }
594                 if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) {
595                         const int transform_part = ED_GIZMO_CAGE2D_PART_TRANSLATE;
596                         GPU_select_load_id(select_id | transform_part);
597                         cage2d_draw_box_interaction(
598                                 gz->color, transform_part, size, margin, gz->line_width, true, draw_options);
599                 }
600                 if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE) {
601                         cage2d_draw_box_interaction(
602                                 gz->color, ED_GIZMO_CAGE2D_PART_ROTATE, size_real, margin, gz->line_width, true, draw_options);
603                 }
604         }
605         else {
606                 const rctf r = {
607                         .xmin = -size_real[0],
608                         .ymin = -size_real[1],
609                         .xmax = size_real[0],
610                         .ymax = size_real[1],
611                 };
612                 if (draw_style == ED_GIZMO_CAGE2D_STYLE_BOX) {
613                         /* corner gizmos */
614                         GPU_line_width(gz->line_width + 3.0f);
615                         cage2d_draw_box_corners(&r, margin, (const float[3]){0, 0, 0});
616
617                         /* corner gizmos */
618                         float color[4];
619                         gizmo_color_get(gz, highlight, color);
620                         GPU_line_width(gz->line_width);
621                         cage2d_draw_box_corners(&r, margin, color);
622
623                         bool show = false;
624                         if (gz->highlight_part == ED_GIZMO_CAGE2D_PART_TRANSLATE) {
625                                 /* Only show if we're drawing the center handle
626                                  * otherwise the entire rectangle is the hotspot. */
627                                 if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
628                                         show = true;
629                                 }
630                         }
631                         else {
632                                 show = true;
633                         }
634
635                         if (show) {
636                                 cage2d_draw_box_interaction(
637                                         gz->color, gz->highlight_part, size_real, margin, gz->line_width, false, draw_options);
638                         }
639
640                         if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE) {
641                                 cage2d_draw_box_interaction(
642                                         gz->color, ED_GIZMO_CAGE2D_PART_ROTATE, size_real, margin, gz->line_width, false, draw_options);
643                         }
644                 }
645                 else if (draw_style == ED_GIZMO_CAGE2D_STYLE_CIRCLE) {
646                         float color[4];
647                         gizmo_color_get(gz, highlight, color);
648
649                         GPU_line_smooth(true);
650                         GPU_blend(true);
651
652                         GPU_line_width(gz->line_width + 3.0f);
653                         cage2d_draw_circle_wire(&r, margin, (const float[3]){0, 0, 0}, transform_flag, draw_options);
654                         GPU_line_width(gz->line_width);
655                         cage2d_draw_circle_wire(&r, margin, color, transform_flag, draw_options);
656
657
658                         /* corner gizmos */
659                         cage2d_draw_circle_handles(&r, margin, color, transform_flag, true);
660                         cage2d_draw_circle_handles(&r, margin, (const float[3]){0, 0, 0}, transform_flag, false);
661
662                         GPU_blend(false);
663                         GPU_line_smooth(false);
664                 }
665                 else {
666                         BLI_assert(0);
667                 }
668         }
669
670         GPU_line_width(1.0);
671         GPU_matrix_pop();
672 }
673
674 /**
675  * For when we want to draw 2d cage in 3d views.
676  */
677 static void gizmo_cage2d_draw_select(const bContext *UNUSED(C), wmGizmo *gz, int select_id)
678 {
679         gizmo_cage2d_draw_intern(gz, true, false, select_id);
680 }
681
682 static void gizmo_cage2d_draw(const bContext *UNUSED(C), wmGizmo *gz)
683 {
684         const bool is_highlight = (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0;
685         gizmo_cage2d_draw_intern(gz, false, is_highlight, -1);
686 }
687
688 static int gizmo_cage2d_get_cursor(wmGizmo *gz)
689 {
690         int highlight_part = gz->highlight_part;
691
692         if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) {
693                 return BC_NSEW_SCROLLCURSOR;
694         }
695
696         switch (highlight_part) {
697                 case ED_GIZMO_CAGE2D_PART_TRANSLATE:
698                         return BC_NSEW_SCROLLCURSOR;
699                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X:
700                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X:
701                         return CURSOR_X_MOVE;
702                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y:
703                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y:
704                         return CURSOR_Y_MOVE;
705
706                         /* TODO diagonal cursor */
707                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y:
708                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y:
709                         return BC_NSEW_SCROLLCURSOR;
710                 case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y:
711                 case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y:
712                         return BC_NSEW_SCROLLCURSOR;
713                 case ED_GIZMO_CAGE2D_PART_ROTATE:
714                         return BC_CROSSCURSOR;
715                 default:
716                         return CURSOR_STD;
717         }
718 }
719
720 static int gizmo_cage2d_test_select(
721         bContext *C, wmGizmo *gz, const int mval[2])
722 {
723         float point_local[2];
724         float dims[2];
725         RNA_float_get_array(gz->ptr, "dimensions", dims);
726         const float size_real[2] = {dims[0] / 2.0f, dims[1] / 2.0f};
727
728         if (gizmo_window_project_2d(
729                 C, gz, (const float[2]){UNPACK2(mval)}, 2, true, point_local) == false)
730         {
731                 return -1;
732         }
733
734         float margin[2];
735         gizmo_calc_rect_view_margin(gz, dims, margin);
736         /* expand for hotspot */
737         const float size[2] = {size_real[0] + margin[0] / 2, size_real[1] + margin[1] / 2};
738
739         const int transform_flag = RNA_enum_get(gz->ptr, "transform");
740         const int draw_options = RNA_enum_get(gz->ptr, "draw_options");
741
742         if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) {
743                 rctf r;
744                 if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
745                         r.xmin = -margin[0] / 2;
746                         r.ymin = -margin[1] / 2;
747                         r.xmax =  margin[0] / 2;
748                         r.ymax =  margin[1] / 2;
749                 }
750                 else {
751                         r.xmin = -size[0] + margin[0];
752                         r.ymin = -size[1] + margin[1];
753                         r.xmax =  size[0] - margin[0];
754                         r.ymax =  size[1] - margin[1];
755                 };
756                 bool isect = BLI_rctf_isect_pt_v(&r, point_local);
757                 if (isect) {
758                         return ED_GIZMO_CAGE2D_PART_TRANSLATE;
759                 }
760         }
761
762         /* if gizmo does not have a scale intersection, don't do it */
763         if (transform_flag & (ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE | ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM)) {
764                 const rctf r_xmin = {.xmin = -size[0], .ymin = -size[1], .xmax = -size[0] + margin[0], .ymax = size[1]};
765                 const rctf r_xmax = {.xmin = size[0] - margin[0], .ymin = -size[1], .xmax = size[0], .ymax = size[1]};
766                 const rctf r_ymin = {.xmin = -size[0], .ymin = -size[1], .xmax = size[0], .ymax = -size[1] + margin[1]};
767                 const rctf r_ymax = {.xmin = -size[0], .ymin = size[1] - margin[1], .xmax = size[0], .ymax = size[1]};
768
769                 if (BLI_rctf_isect_pt_v(&r_xmin, point_local)) {
770                         if (BLI_rctf_isect_pt_v(&r_ymin, point_local)) {
771                                 return ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y;
772                         }
773                         if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) {
774                                 return ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y;
775                         }
776                         return ED_GIZMO_CAGE2D_PART_SCALE_MIN_X;
777                 }
778                 if (BLI_rctf_isect_pt_v(&r_xmax, point_local)) {
779                         if (BLI_rctf_isect_pt_v(&r_ymin, point_local)) {
780                                 return ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y;
781                         }
782                         if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) {
783                                 return ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y;
784                         }
785                         return ED_GIZMO_CAGE2D_PART_SCALE_MAX_X;
786                 }
787                 if (BLI_rctf_isect_pt_v(&r_ymin, point_local)) {
788                         return ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y;
789                 }
790                 if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) {
791                         return ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y;
792                 }
793         }
794
795         if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE) {
796                 /* Rotate:
797                  *  (*) <-- hot spot is here!
798                  * +---+
799                  * |   |
800                  * +---+ */
801                 const float r_rotate_pt[2] = {0.0f, size_real[1] + (margin[1] * GIZMO_MARGIN_OFFSET_SCALE)};
802                 const rctf r_rotate = {
803                         .xmin = r_rotate_pt[0] - margin[0] / 2.0f,
804                         .xmax = r_rotate_pt[0] + margin[0] / 2.0f,
805                         .ymin = r_rotate_pt[1] - margin[1] / 2.0f,
806                         .ymax = r_rotate_pt[1] + margin[1] / 2.0f,
807                 };
808
809                 if (BLI_rctf_isect_pt_v(&r_rotate, point_local)) {
810                         return ED_GIZMO_CAGE2D_PART_ROTATE;
811                 }
812         }
813
814         return -1;
815 }
816
817 typedef struct RectTransformInteraction {
818         float orig_mouse[2];
819         float orig_matrix_offset[4][4];
820         float orig_matrix_final_no_offset[4][4];
821         Dial *dial;
822 } RectTransformInteraction;
823
824 static void gizmo_cage2d_setup(wmGizmo *gz)
825 {
826         gz->flag |= WM_GIZMO_DRAW_MODAL | WM_GIZMO_DRAW_NO_SCALE;
827 }
828
829 static int gizmo_cage2d_invoke(
830         bContext *C, wmGizmo *gz, const wmEvent *event)
831 {
832         RectTransformInteraction *data = MEM_callocN(sizeof(RectTransformInteraction), "cage_interaction");
833
834         copy_m4_m4(data->orig_matrix_offset, gz->matrix_offset);
835         WM_gizmo_calc_matrix_final_no_offset(gz, data->orig_matrix_final_no_offset);
836
837         if (gizmo_window_project_2d(
838                 C, gz, (const float[2]){UNPACK2(event->mval)}, 2, false, data->orig_mouse) == 0)
839         {
840                 zero_v2(data->orig_mouse);
841         }
842
843         gz->interaction_data = data;
844
845         return OPERATOR_RUNNING_MODAL;
846 }
847
848 static int gizmo_cage2d_modal(
849         bContext *C, wmGizmo *gz, const wmEvent *event,
850         eWM_GizmoFlagTweak UNUSED(tweak_flag))
851 {
852         /* For transform logic to be managable we operate in -0.5..0.5 2D space,
853          * no matter the size of the rectangle, mouse coorts are scaled to unit space.
854          * The mouse coords have been projected into the matrix so we don't need to worry about axis alignment.
855          *
856          * - The cursor offset are multiplied by 'dims'.
857          * - Matrix translation is also multiplied by 'dims'.
858          */
859         RectTransformInteraction *data = gz->interaction_data;
860         float point_local[2];
861
862         float dims[2];
863         RNA_float_get_array(gz->ptr, "dimensions", dims);
864
865         {
866                 float matrix_back[4][4];
867                 copy_m4_m4(matrix_back, gz->matrix_offset);
868                 copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset);
869
870                 bool ok = gizmo_window_project_2d(
871                         C, gz, (const float[2]){UNPACK2(event->mval)}, 2, false, point_local);
872                 copy_m4_m4(gz->matrix_offset, matrix_back);
873                 if (!ok) {
874                         return OPERATOR_RUNNING_MODAL;
875                 }
876         }
877
878         const int transform_flag = RNA_enum_get(gz->ptr, "transform");
879         wmGizmoProperty *gz_prop;
880
881         gz_prop = WM_gizmo_target_property_find(gz, "matrix");
882         if (gz_prop->type != NULL) {
883                 WM_gizmo_target_property_float_get_array(gz, gz_prop, &gz->matrix_offset[0][0]);
884         }
885
886         if (gz->highlight_part == ED_GIZMO_CAGE2D_PART_TRANSLATE) {
887                 /* do this to prevent clamping from changing size */
888                 copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset);
889                 gz->matrix_offset[3][0] = data->orig_matrix_offset[3][0] + (point_local[0] - data->orig_mouse[0]);
890                 gz->matrix_offset[3][1] = data->orig_matrix_offset[3][1] + (point_local[1] - data->orig_mouse[1]);
891         }
892         else if (gz->highlight_part == ED_GIZMO_CAGE2D_PART_ROTATE) {
893
894 #define MUL_V2_V3_M4_FINAL(test_co, mouse_co) \
895                 mul_v3_m4v3(test_co, data->orig_matrix_final_no_offset, ((const float[3]){UNPACK2(mouse_co), 0.0}))
896
897                 float test_co[3];
898
899                 if (data->dial == NULL) {
900                         MUL_V2_V3_M4_FINAL(test_co, data->orig_matrix_offset[3]);
901
902                         data->dial = BLI_dial_initialize(test_co, FLT_EPSILON);
903
904                         MUL_V2_V3_M4_FINAL(test_co, data->orig_mouse);
905                         BLI_dial_angle(data->dial, test_co);
906                 }
907
908                 /* rotate */
909                 MUL_V2_V3_M4_FINAL(test_co, point_local);
910                 const float angle =  BLI_dial_angle(data->dial, test_co);
911
912                 float matrix_space_inv[4][4];
913                 float matrix_rotate[4][4];
914                 float pivot[3];
915
916                 copy_v3_v3(pivot, data->orig_matrix_offset[3]);
917
918                 invert_m4_m4(matrix_space_inv, gz->matrix_space);
919
920                 unit_m4(matrix_rotate);
921                 mul_m4_m4m4(matrix_rotate, matrix_rotate, matrix_space_inv);
922                 rotate_m4(matrix_rotate, 'Z', -angle);
923                 mul_m4_m4m4(matrix_rotate, matrix_rotate, gz->matrix_space);
924
925                 zero_v3(matrix_rotate[3]);
926                 transform_pivot_set_m4(matrix_rotate, pivot);
927
928                 mul_m4_m4m4(gz->matrix_offset, matrix_rotate, data->orig_matrix_offset);
929
930 #undef MUL_V2_V3_M4_FINAL
931         }
932         else {
933                 /* scale */
934                 copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset);
935                 float pivot[2];
936                 bool constrain_axis[2] = {false};
937
938                 if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) {
939                         gizmo_rect_pivot_from_scale_part(gz->highlight_part, pivot, constrain_axis);
940                 }
941                 else {
942                         zero_v2(pivot);
943                 }
944
945                 /* Cursor deltas scaled to (-0.5..0.5). */
946                 float delta_orig[2], delta_curr[2];
947                 for (int i = 0; i < 2; i++) {
948                         delta_orig[i] = ((data->orig_mouse[i] - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i];
949                         delta_curr[i] = ((point_local[i]      - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i];
950                 }
951
952                 float scale[2] = {1.0f, 1.0f};
953                 for (int i = 0; i < 2; i++) {
954                         if (constrain_axis[i] == false) {
955                                 if (delta_orig[i] < 0.0f) {
956                                         delta_orig[i] *= -1.0f;
957                                         delta_curr[i] *= -1.0f;
958                                 }
959                                 const int sign = signum_i(scale[i]);
960
961                                 scale[i] = 1.0f + ((delta_curr[i] - delta_orig[i]) / len_v3(data->orig_matrix_offset[i]));
962
963                                 if ((transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_SIGNED) == 0) {
964                                         if (sign != signum_i(scale[i])) {
965                                                 scale[i] = 0.0f;
966                                         }
967                                 }
968                         }
969                 }
970
971                 if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM) {
972                         if (constrain_axis[0] == false && constrain_axis[1] == false) {
973                                 scale[1] = scale[0] = (scale[1] + scale[0]) / 2.0f;
974                         }
975                         else if (constrain_axis[0] == false) {
976                                 scale[1] = scale[0];
977                         }
978                         else if (constrain_axis[1] == false) {
979                                 scale[0] = scale[1];
980                         }
981                         else {
982                                 BLI_assert(0);
983                         }
984                 }
985
986                 /* scale around pivot */
987                 float matrix_scale[4][4];
988                 unit_m4(matrix_scale);
989
990                 mul_v3_fl(matrix_scale[0], scale[0]);
991                 mul_v3_fl(matrix_scale[1], scale[1]);
992
993                 transform_pivot_set_m4(matrix_scale, (const float[3]){pivot[0] * dims[0], pivot[1] * dims[1], 0.0f});
994                 mul_m4_m4m4(gz->matrix_offset, data->orig_matrix_offset, matrix_scale);
995         }
996
997         if (gz_prop->type != NULL) {
998                 WM_gizmo_target_property_float_set_array(C, gz, gz_prop, &gz->matrix_offset[0][0]);
999         }
1000
1001         /* tag the region for redraw */
1002         ED_region_tag_redraw(CTX_wm_region(C));
1003         WM_event_add_mousemove(C);
1004
1005         return OPERATOR_RUNNING_MODAL;
1006 }
1007
1008 static void gizmo_cage2d_property_update(wmGizmo *gz, wmGizmoProperty *gz_prop)
1009 {
1010         if (STREQ(gz_prop->type->idname, "matrix")) {
1011                 if (WM_gizmo_target_property_array_length(gz, gz_prop) == 16) {
1012                         WM_gizmo_target_property_float_get_array(gz, gz_prop, &gz->matrix_offset[0][0]);
1013                 }
1014                 else {
1015                         BLI_assert(0);
1016                 }
1017         }
1018         else {
1019                 BLI_assert(0);
1020         }
1021 }
1022
1023 static void gizmo_cage2d_exit(bContext *C, wmGizmo *gz, const bool cancel)
1024 {
1025         RectTransformInteraction *data = gz->interaction_data;
1026
1027         MEM_SAFE_FREE(data->dial);
1028
1029         if (!cancel)
1030                 return;
1031
1032         wmGizmoProperty *gz_prop;
1033
1034         /* reset properties */
1035         gz_prop = WM_gizmo_target_property_find(gz, "matrix");
1036         if (gz_prop->type != NULL) {
1037                 WM_gizmo_target_property_float_set_array(C, gz, gz_prop, &data->orig_matrix_offset[0][0]);
1038         }
1039
1040         copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset);
1041 }
1042
1043
1044 /* -------------------------------------------------------------------- */
1045 /** \name Cage Gizmo API
1046  *
1047  * \{ */
1048
1049 static void GIZMO_GT_cage_2d(wmGizmoType *gzt)
1050 {
1051         /* identifiers */
1052         gzt->idname = "GIZMO_GT_cage_2d";
1053
1054         /* api callbacks */
1055         gzt->draw = gizmo_cage2d_draw;
1056         gzt->draw_select = gizmo_cage2d_draw_select;
1057         gzt->test_select = gizmo_cage2d_test_select;
1058         gzt->setup = gizmo_cage2d_setup;
1059         gzt->invoke = gizmo_cage2d_invoke;
1060         gzt->property_update = gizmo_cage2d_property_update;
1061         gzt->modal = gizmo_cage2d_modal;
1062         gzt->exit = gizmo_cage2d_exit;
1063         gzt->cursor_get = gizmo_cage2d_get_cursor;
1064
1065         gzt->struct_size = sizeof(wmGizmo);
1066
1067         /* rna */
1068         static EnumPropertyItem rna_enum_draw_style[] = {
1069                 {ED_GIZMO_CAGE2D_STYLE_BOX, "BOX", 0, "Box", ""},
1070                 {ED_GIZMO_CAGE2D_STYLE_CIRCLE, "CIRCLE", 0, "Circle", ""},
1071                 {0, NULL, 0, NULL, NULL}
1072         };
1073         static EnumPropertyItem rna_enum_transform[] = {
1074                 {ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE, "TRANSLATE", 0, "Translate", ""},
1075                 {ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE, "ROTATE", 0, "Rotate", ""},
1076                 {ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE, "SCALE", 0, "Scale", ""},
1077                 {ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM, "SCALE_UNIFORM", 0, "Scale Uniform", ""},
1078                 {0, NULL, 0, NULL, NULL}
1079         };
1080         static EnumPropertyItem rna_enum_draw_options[] = {
1081                 {ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "XFORM_CENTER_HANDLE", 0, "Center Handle", ""},
1082                 {0, NULL, 0, NULL, NULL}
1083         };
1084         static float unit_v2[2] = {1.0f, 1.0f};
1085         RNA_def_float_vector(gzt->srna, "dimensions", 2, unit_v2, 0, FLT_MAX, "Dimensions", "", 0.0f, FLT_MAX);
1086         RNA_def_enum_flag(gzt->srna, "transform", rna_enum_transform, 0, "Transform Options", "");
1087         RNA_def_enum(gzt->srna, "draw_style", rna_enum_draw_style, ED_GIZMO_CAGE2D_STYLE_CIRCLE, "Draw Style", "");
1088         RNA_def_enum_flag(
1089                 gzt->srna, "draw_options", rna_enum_draw_options,
1090                 ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "Draw Options", "");
1091
1092         WM_gizmotype_target_property_def(gzt, "matrix", PROP_FLOAT, 16);
1093 }
1094
1095 void ED_gizmotypes_cage_2d(void)
1096 {
1097         WM_gizmotype_append(GIZMO_GT_cage_2d);
1098 }
1099
1100 /** \} */