84d9930871ac00b4d2f5188eaa1800f58cfa69d6
[blender.git] / source / blender / editors / transform / transform_gizmo_3d.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  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/editors/transform/transform_gizmo_3d.c
22  *  \ingroup edtransform
23  *
24  * \name 3D Transform Gizmo
25  *
26  * Used for 3D View
27  */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
32 #include <float.h>
33
34 #include "DNA_armature_types.h"
35 #include "DNA_curve_types.h"
36 #include "DNA_gpencil_types.h"
37 #include "DNA_lattice_types.h"
38 #include "DNA_meta_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_view3d_types.h"
42
43 #include "BLI_array_utils.h"
44 #include "BLI_listbase.h"
45 #include "BLI_math.h"
46 #include "BLI_utildefines.h"
47
48 #include "BKE_action.h"
49 #include "BKE_context.h"
50 #include "BKE_curve.h"
51 #include "BKE_global.h"
52 #include "BKE_layer.h"
53 #include "BKE_particle.h"
54 #include "BKE_pointcache.h"
55 #include "BKE_editmesh.h"
56 #include "BKE_lattice.h"
57 #include "BKE_gpencil.h"
58 #include "BKE_scene.h"
59 #include "BKE_workspace.h"
60 #include "BKE_object.h"
61
62 #include "BIF_gl.h"
63
64 #include "DEG_depsgraph.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68 #include "WM_message.h"
69 #include "WM_toolsystem.h"
70 #include "wm.h"
71
72 #include "ED_armature.h"
73 #include "ED_curve.h"
74 #include "ED_object.h"
75 #include "ED_particle.h"
76 #include "ED_view3d.h"
77 #include "ED_gpencil.h"
78 #include "ED_screen.h"
79 #include "ED_gizmo_library.h"
80 #include "ED_gizmo_utils.h"
81
82 #include "UI_interface.h"
83 #include "UI_resources.h"
84
85 #include "RNA_access.h"
86 #include "RNA_define.h"
87
88 /* local module include */
89 #include "transform.h"
90
91 #include "MEM_guardedalloc.h"
92
93 #include "GPU_select.h"
94 #include "GPU_state.h"
95 #include "GPU_immediate.h"
96 #include "GPU_matrix.h"
97
98 #include "DEG_depsgraph_query.h"
99
100 /* return codes for select, and drawing flags */
101
102 #define MAN_TRANS_X             (1 << 0)
103 #define MAN_TRANS_Y             (1 << 1)
104 #define MAN_TRANS_Z             (1 << 2)
105 #define MAN_TRANS_C             (MAN_TRANS_X | MAN_TRANS_Y | MAN_TRANS_Z)
106
107 #define MAN_ROT_X               (1 << 3)
108 #define MAN_ROT_Y               (1 << 4)
109 #define MAN_ROT_Z               (1 << 5)
110 #define MAN_ROT_C               (MAN_ROT_X | MAN_ROT_Y | MAN_ROT_Z)
111
112 #define MAN_SCALE_X             (1 << 8)
113 #define MAN_SCALE_Y             (1 << 9)
114 #define MAN_SCALE_Z             (1 << 10)
115 #define MAN_SCALE_C             (MAN_SCALE_X | MAN_SCALE_Y | MAN_SCALE_Z)
116
117 /* threshold for testing view aligned gizmo axis */
118 static struct {
119         float min, max;
120 } g_tw_axis_range[2] = {
121         /* Regular range */
122         {0.02f, 0.1f},
123         /* Use a different range because we flip the dot product,
124          * also the view aligned planes are harder to see so hiding early is preferred. */
125         {0.175f,  0.25f},
126 };
127
128 /* axes as index */
129 enum {
130         MAN_AXIS_TRANS_X = 0,
131         MAN_AXIS_TRANS_Y,
132         MAN_AXIS_TRANS_Z,
133         MAN_AXIS_TRANS_C,
134
135         MAN_AXIS_TRANS_XY,
136         MAN_AXIS_TRANS_YZ,
137         MAN_AXIS_TRANS_ZX,
138 #define MAN_AXIS_RANGE_TRANS_START MAN_AXIS_TRANS_X
139 #define MAN_AXIS_RANGE_TRANS_END (MAN_AXIS_TRANS_ZX + 1)
140
141         MAN_AXIS_ROT_X,
142         MAN_AXIS_ROT_Y,
143         MAN_AXIS_ROT_Z,
144         MAN_AXIS_ROT_C,
145         MAN_AXIS_ROT_T, /* trackball rotation */
146 #define MAN_AXIS_RANGE_ROT_START MAN_AXIS_ROT_X
147 #define MAN_AXIS_RANGE_ROT_END (MAN_AXIS_ROT_T + 1)
148
149         MAN_AXIS_SCALE_X,
150         MAN_AXIS_SCALE_Y,
151         MAN_AXIS_SCALE_Z,
152         MAN_AXIS_SCALE_C,
153         MAN_AXIS_SCALE_XY,
154         MAN_AXIS_SCALE_YZ,
155         MAN_AXIS_SCALE_ZX,
156 #define MAN_AXIS_RANGE_SCALE_START MAN_AXIS_SCALE_X
157 #define MAN_AXIS_RANGE_SCALE_END (MAN_AXIS_SCALE_ZX + 1)
158
159         MAN_AXIS_LAST = MAN_AXIS_SCALE_ZX + 1,
160 };
161
162 /* axis types */
163 enum {
164         MAN_AXES_ALL = 0,
165         MAN_AXES_TRANSLATE,
166         MAN_AXES_ROTATE,
167         MAN_AXES_SCALE,
168 };
169
170 typedef struct GizmoGroup {
171         bool all_hidden;
172         int twtype;
173
174         /* Users may change the twtype, detect changes to re-setup gizmo options. */
175         int twtype_init;
176         int twtype_prev;
177         int use_twtype_refresh;
178
179         /* Only for view orientation. */
180         struct {
181                 float viewinv_m3[3][3];
182         } prev;
183
184         struct wmGizmo *gizmos[MAN_AXIS_LAST];
185 } GizmoGroup;
186
187 /* -------------------------------------------------------------------- */
188 /** \name Utilities
189  * \{ */
190
191 /* loop over axes */
192 #define MAN_ITER_AXES_BEGIN(axis, axis_idx) \
193         { \
194                 wmGizmo *axis; \
195                 int axis_idx; \
196                 for (axis_idx = 0; axis_idx < MAN_AXIS_LAST; axis_idx++) { \
197                         axis = gizmo_get_axis_from_index(ggd, axis_idx);
198
199 #define MAN_ITER_AXES_END \
200                 } \
201         } ((void)0)
202
203 static wmGizmo *gizmo_get_axis_from_index(const GizmoGroup *ggd, const short axis_idx)
204 {
205         BLI_assert(IN_RANGE_INCL(axis_idx, (float)MAN_AXIS_TRANS_X, (float)MAN_AXIS_LAST));
206         return ggd->gizmos[axis_idx];
207 }
208
209 static short gizmo_get_axis_type(const int axis_idx)
210 {
211         if (axis_idx >= MAN_AXIS_RANGE_TRANS_START && axis_idx < MAN_AXIS_RANGE_TRANS_END) {
212                 return MAN_AXES_TRANSLATE;
213         }
214         if (axis_idx >= MAN_AXIS_RANGE_ROT_START && axis_idx < MAN_AXIS_RANGE_ROT_END) {
215                 return MAN_AXES_ROTATE;
216         }
217         if (axis_idx >= MAN_AXIS_RANGE_SCALE_START && axis_idx < MAN_AXIS_RANGE_SCALE_END) {
218                 return MAN_AXES_SCALE;
219         }
220         BLI_assert(0);
221         return -1;
222 }
223
224 static uint gizmo_orientation_axis(const int axis_idx, bool *r_is_plane)
225 {
226         switch (axis_idx) {
227                 case MAN_AXIS_TRANS_YZ:
228                 case MAN_AXIS_SCALE_YZ:
229                         if (r_is_plane) {
230                                 *r_is_plane = true;
231                         }
232                         ATTR_FALLTHROUGH;
233                 case MAN_AXIS_TRANS_X:
234                 case MAN_AXIS_ROT_X:
235                 case MAN_AXIS_SCALE_X:
236                         return 0;
237
238                 case MAN_AXIS_TRANS_ZX:
239                 case MAN_AXIS_SCALE_ZX:
240                         if (r_is_plane) {
241                                 *r_is_plane = true;
242                         }
243                         ATTR_FALLTHROUGH;
244                 case MAN_AXIS_TRANS_Y:
245                 case MAN_AXIS_ROT_Y:
246                 case MAN_AXIS_SCALE_Y:
247                         return 1;
248
249                 case MAN_AXIS_TRANS_XY:
250                 case MAN_AXIS_SCALE_XY:
251                         if (r_is_plane) {
252                                 *r_is_plane = true;
253                         }
254                         ATTR_FALLTHROUGH;
255                 case MAN_AXIS_TRANS_Z:
256                 case MAN_AXIS_ROT_Z:
257                 case MAN_AXIS_SCALE_Z:
258                         return 2;
259         }
260         return 3;
261 }
262
263 static bool gizmo_is_axis_visible(
264         const RegionView3D *rv3d, const int twtype,
265         const float idot[3], const int axis_type, const int axis_idx)
266 {
267         if ((axis_idx >= MAN_AXIS_RANGE_ROT_START && axis_idx < MAN_AXIS_RANGE_ROT_END) == 0) {
268                 bool is_plane = false;
269                 const uint aidx_norm = gizmo_orientation_axis(axis_idx, &is_plane);
270                 /* don't draw axis perpendicular to the view */
271                 if (aidx_norm < 3) {
272                         float idot_axis = idot[aidx_norm];
273                         if (is_plane) {
274                                 idot_axis = 1.0f - idot_axis;
275                         }
276                         if (idot_axis < g_tw_axis_range[is_plane].min) {
277                                 return false;
278                         }
279                 }
280         }
281
282         if ((axis_type == MAN_AXES_TRANSLATE && !(twtype & SCE_GIZMO_SHOW_TRANSLATE)) ||
283             (axis_type == MAN_AXES_ROTATE && !(twtype & SCE_GIZMO_SHOW_ROTATE)) ||
284             (axis_type == MAN_AXES_SCALE && !(twtype & SCE_GIZMO_SHOW_SCALE)))
285         {
286                 return false;
287         }
288
289         switch (axis_idx) {
290                 case MAN_AXIS_TRANS_X:
291                         return (rv3d->twdrawflag & MAN_TRANS_X);
292                 case MAN_AXIS_TRANS_Y:
293                         return (rv3d->twdrawflag & MAN_TRANS_Y);
294                 case MAN_AXIS_TRANS_Z:
295                         return (rv3d->twdrawflag & MAN_TRANS_Z);
296                 case MAN_AXIS_TRANS_C:
297                         return (rv3d->twdrawflag & MAN_TRANS_C);
298                 case MAN_AXIS_ROT_X:
299                         return (rv3d->twdrawflag & MAN_ROT_X);
300                 case MAN_AXIS_ROT_Y:
301                         return (rv3d->twdrawflag & MAN_ROT_Y);
302                 case MAN_AXIS_ROT_Z:
303                         return (rv3d->twdrawflag & MAN_ROT_Z);
304                 case MAN_AXIS_ROT_C:
305                 case MAN_AXIS_ROT_T:
306                         return (rv3d->twdrawflag & MAN_ROT_C);
307                 case MAN_AXIS_SCALE_X:
308                         return (rv3d->twdrawflag & MAN_SCALE_X);
309                 case MAN_AXIS_SCALE_Y:
310                         return (rv3d->twdrawflag & MAN_SCALE_Y);
311                 case MAN_AXIS_SCALE_Z:
312                         return (rv3d->twdrawflag & MAN_SCALE_Z);
313                 case MAN_AXIS_SCALE_C:
314                         return (rv3d->twdrawflag & MAN_SCALE_C && (twtype & SCE_GIZMO_SHOW_TRANSLATE) == 0);
315                 case MAN_AXIS_TRANS_XY:
316                         return (rv3d->twdrawflag & MAN_TRANS_X &&
317                                 rv3d->twdrawflag & MAN_TRANS_Y &&
318                                 (twtype & SCE_GIZMO_SHOW_ROTATE) == 0);
319                 case MAN_AXIS_TRANS_YZ:
320                         return (rv3d->twdrawflag & MAN_TRANS_Y &&
321                                 rv3d->twdrawflag & MAN_TRANS_Z &&
322                                 (twtype & SCE_GIZMO_SHOW_ROTATE) == 0);
323                 case MAN_AXIS_TRANS_ZX:
324                         return (rv3d->twdrawflag & MAN_TRANS_Z &&
325                                 rv3d->twdrawflag & MAN_TRANS_X &&
326                                 (twtype & SCE_GIZMO_SHOW_ROTATE) == 0);
327                 case MAN_AXIS_SCALE_XY:
328                         return (rv3d->twdrawflag & MAN_SCALE_X &&
329                                 rv3d->twdrawflag & MAN_SCALE_Y &&
330                                 (twtype & SCE_GIZMO_SHOW_TRANSLATE) == 0 &&
331                                 (twtype & SCE_GIZMO_SHOW_ROTATE) == 0);
332                 case MAN_AXIS_SCALE_YZ:
333                         return (rv3d->twdrawflag & MAN_SCALE_Y &&
334                                 rv3d->twdrawflag & MAN_SCALE_Z &&
335                                 (twtype & SCE_GIZMO_SHOW_TRANSLATE) == 0 &&
336                                 (twtype & SCE_GIZMO_SHOW_ROTATE) == 0);
337                 case MAN_AXIS_SCALE_ZX:
338                         return (rv3d->twdrawflag & MAN_SCALE_Z &&
339                                 rv3d->twdrawflag & MAN_SCALE_X &&
340                                 (twtype & SCE_GIZMO_SHOW_TRANSLATE) == 0 &&
341                                 (twtype & SCE_GIZMO_SHOW_ROTATE) == 0);
342         }
343         return false;
344 }
345
346 static void gizmo_get_axis_color(
347         const int axis_idx, const float idot[3],
348         float r_col[4], float r_col_hi[4])
349 {
350         /* alpha values for normal/highlighted states */
351         const float alpha = 0.6f;
352         const float alpha_hi = 1.0f;
353         float alpha_fac;
354
355         if (axis_idx >= MAN_AXIS_RANGE_ROT_START && axis_idx < MAN_AXIS_RANGE_ROT_END) {
356                 /* Never fade rotation rings. */
357                 /* trackball rotation axis is a special case, we only draw a slight overlay */
358                 alpha_fac = (axis_idx == MAN_AXIS_ROT_T) ? 0.1f : 1.0f;
359         }
360         else {
361                 bool is_plane = false;
362                 const int axis_idx_norm = gizmo_orientation_axis(axis_idx, &is_plane);
363                 /* get alpha fac based on axis angle, to fade axis out when hiding it because it points towards view */
364                 if (axis_idx_norm < 3) {
365                         const float idot_min = g_tw_axis_range[is_plane].min;
366                         const float idot_max = g_tw_axis_range[is_plane].max;
367                         float idot_axis = idot[axis_idx_norm];
368                         if (is_plane) {
369                                 idot_axis = 1.0f - idot_axis;
370                         }
371                         alpha_fac = (
372                                 (idot_axis > idot_max) ?
373                                 1.0f : (idot_axis < idot_min) ?
374                                 0.0f : ((idot_axis - idot_min) / (idot_max - idot_min)));
375                 }
376                 else {
377                         alpha_fac = 1.0f;
378                 }
379         }
380
381         switch (axis_idx) {
382                 case MAN_AXIS_TRANS_X:
383                 case MAN_AXIS_ROT_X:
384                 case MAN_AXIS_SCALE_X:
385                 case MAN_AXIS_TRANS_YZ:
386                 case MAN_AXIS_SCALE_YZ:
387                         UI_GetThemeColor4fv(TH_AXIS_X, r_col);
388                         break;
389                 case MAN_AXIS_TRANS_Y:
390                 case MAN_AXIS_ROT_Y:
391                 case MAN_AXIS_SCALE_Y:
392                 case MAN_AXIS_TRANS_ZX:
393                 case MAN_AXIS_SCALE_ZX:
394                         UI_GetThemeColor4fv(TH_AXIS_Y, r_col);
395                         break;
396                 case MAN_AXIS_TRANS_Z:
397                 case MAN_AXIS_ROT_Z:
398                 case MAN_AXIS_SCALE_Z:
399                 case MAN_AXIS_TRANS_XY:
400                 case MAN_AXIS_SCALE_XY:
401                         UI_GetThemeColor4fv(TH_AXIS_Z, r_col);
402                         break;
403                 case MAN_AXIS_TRANS_C:
404                 case MAN_AXIS_ROT_C:
405                 case MAN_AXIS_SCALE_C:
406                 case MAN_AXIS_ROT_T:
407                         copy_v4_fl(r_col, 1.0f);
408                         break;
409         }
410
411         copy_v4_v4(r_col_hi, r_col);
412
413         r_col[3] = alpha * alpha_fac;
414         r_col_hi[3] = alpha_hi * alpha_fac;
415 }
416
417 static void gizmo_get_axis_constraint(const int axis_idx, bool r_axis[3])
418 {
419         ARRAY_SET_ITEMS(r_axis, 0, 0, 0);
420
421         switch (axis_idx) {
422                 case MAN_AXIS_TRANS_X:
423                 case MAN_AXIS_ROT_X:
424                 case MAN_AXIS_SCALE_X:
425                         r_axis[0] = 1;
426                         break;
427                 case MAN_AXIS_TRANS_Y:
428                 case MAN_AXIS_ROT_Y:
429                 case MAN_AXIS_SCALE_Y:
430                         r_axis[1] = 1;
431                         break;
432                 case MAN_AXIS_TRANS_Z:
433                 case MAN_AXIS_ROT_Z:
434                 case MAN_AXIS_SCALE_Z:
435                         r_axis[2] = 1;
436                         break;
437                 case MAN_AXIS_TRANS_XY:
438                 case MAN_AXIS_SCALE_XY:
439                         r_axis[0] = r_axis[1] = 1;
440                         break;
441                 case MAN_AXIS_TRANS_YZ:
442                 case MAN_AXIS_SCALE_YZ:
443                         r_axis[1] = r_axis[2] = 1;
444                         break;
445                 case MAN_AXIS_TRANS_ZX:
446                 case MAN_AXIS_SCALE_ZX:
447                         r_axis[2] = r_axis[0] = 1;
448                         break;
449                 default:
450                         break;
451         }
452 }
453
454
455 /* **************** Preparation Stuff **************** */
456
457 static void reset_tw_center(struct TransformBounds *tbounds)
458 {
459         INIT_MINMAX(tbounds->min, tbounds->max);
460         zero_v3(tbounds->center);
461
462         for (int i = 0; i < 3; i++) {
463                 tbounds->axis_min[i] = +FLT_MAX;
464                 tbounds->axis_max[i] = -FLT_MAX;
465         }
466 }
467
468 /* transform widget center calc helper for below */
469 static void calc_tw_center(struct TransformBounds *tbounds, const float co[3])
470 {
471         minmax_v3v3_v3(tbounds->min, tbounds->max, co);
472         add_v3_v3(tbounds->center, co);
473
474         for (int i = 0; i < 3; i++) {
475                 const float d = dot_v3v3(tbounds->axis[i], co);
476                 tbounds->axis_min[i] = min_ff(d, tbounds->axis_min[i]);
477                 tbounds->axis_max[i] = max_ff(d, tbounds->axis_max[i]);
478         }
479 }
480
481 static void calc_tw_center_with_matrix(
482         struct TransformBounds *tbounds, const float co[3],
483         const bool use_matrix, const float matrix[4][4])
484 {
485         float co_world[3];
486         if (use_matrix) {
487                 mul_v3_m4v3(co_world, matrix, co);
488                 co = co_world;
489         }
490         calc_tw_center(tbounds, co);
491 }
492
493 static void protectflag_to_drawflags(short protectflag, short *drawflags)
494 {
495         if (protectflag & OB_LOCK_LOCX)
496                 *drawflags &= ~MAN_TRANS_X;
497         if (protectflag & OB_LOCK_LOCY)
498                 *drawflags &= ~MAN_TRANS_Y;
499         if (protectflag & OB_LOCK_LOCZ)
500                 *drawflags &= ~MAN_TRANS_Z;
501
502         if (protectflag & OB_LOCK_ROTX)
503                 *drawflags &= ~MAN_ROT_X;
504         if (protectflag & OB_LOCK_ROTY)
505                 *drawflags &= ~MAN_ROT_Y;
506         if (protectflag & OB_LOCK_ROTZ)
507                 *drawflags &= ~MAN_ROT_Z;
508
509         if (protectflag & OB_LOCK_SCALEX)
510                 *drawflags &= ~MAN_SCALE_X;
511         if (protectflag & OB_LOCK_SCALEY)
512                 *drawflags &= ~MAN_SCALE_Y;
513         if (protectflag & OB_LOCK_SCALEZ)
514                 *drawflags &= ~MAN_SCALE_Z;
515 }
516
517 /* for pose mode */
518 static void protectflag_to_drawflags_pchan(RegionView3D *rv3d, const bPoseChannel *pchan)
519 {
520         protectflag_to_drawflags(pchan->protectflag, &rv3d->twdrawflag);
521 }
522
523 /* for editmode*/
524 static void protectflag_to_drawflags_ebone(RegionView3D *rv3d, const EditBone *ebo)
525 {
526         if (ebo->flag & BONE_EDITMODE_LOCKED) {
527                 protectflag_to_drawflags(OB_LOCK_LOC | OB_LOCK_ROT | OB_LOCK_SCALE, &rv3d->twdrawflag);
528         }
529 }
530
531 /* could move into BLI_math however this is only useful for display/editing purposes */
532 static void axis_angle_to_gimbal_axis(float gmat[3][3], const float axis[3], const float angle)
533 {
534         /* X/Y are arbitrary axies, most importantly Z is the axis of rotation */
535
536         float cross_vec[3];
537         float quat[4];
538
539         /* this is an un-scientific method to get a vector to cross with
540          * XYZ intentionally YZX */
541         cross_vec[0] = axis[1];
542         cross_vec[1] = axis[2];
543         cross_vec[2] = axis[0];
544
545         /* X-axis */
546         cross_v3_v3v3(gmat[0], cross_vec, axis);
547         normalize_v3(gmat[0]);
548         axis_angle_to_quat(quat, axis, angle);
549         mul_qt_v3(quat, gmat[0]);
550
551         /* Y-axis */
552         axis_angle_to_quat(quat, axis, M_PI_2);
553         copy_v3_v3(gmat[1], gmat[0]);
554         mul_qt_v3(quat, gmat[1]);
555
556         /* Z-axis */
557         copy_v3_v3(gmat[2], axis);
558
559         normalize_m3(gmat);
560 }
561
562
563 static bool test_rotmode_euler(short rotmode)
564 {
565         return (ELEM(rotmode, ROT_MODE_AXISANGLE, ROT_MODE_QUAT)) ? 0 : 1;
566 }
567
568 bool gimbal_axis(Object *ob, float gmat[3][3])
569 {
570         if (ob->mode & OB_MODE_POSE) {
571                 bPoseChannel *pchan = BKE_pose_channel_active(ob);
572
573                 if (pchan) {
574                         float mat[3][3], tmat[3][3], obmat[3][3];
575                         if (test_rotmode_euler(pchan->rotmode)) {
576                                 eulO_to_gimbal_axis(mat, pchan->eul, pchan->rotmode);
577                         }
578                         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
579                                 axis_angle_to_gimbal_axis(mat, pchan->rotAxis, pchan->rotAngle);
580                         }
581                         else { /* quat */
582                                 return 0;
583                         }
584
585
586                         /* apply bone transformation */
587                         mul_m3_m3m3(tmat, pchan->bone->bone_mat, mat);
588
589                         if (pchan->parent) {
590                                 float parent_mat[3][3];
591
592                                 copy_m3_m4(parent_mat, pchan->parent->pose_mat);
593                                 mul_m3_m3m3(mat, parent_mat, tmat);
594
595                                 /* needed if object transformation isn't identity */
596                                 copy_m3_m4(obmat, ob->obmat);
597                                 mul_m3_m3m3(gmat, obmat, mat);
598                         }
599                         else {
600                                 /* needed if object transformation isn't identity */
601                                 copy_m3_m4(obmat, ob->obmat);
602                                 mul_m3_m3m3(gmat, obmat, tmat);
603                         }
604
605                         normalize_m3(gmat);
606                         return 1;
607                 }
608         }
609         else {
610                 if (test_rotmode_euler(ob->rotmode)) {
611                         eulO_to_gimbal_axis(gmat, ob->rot, ob->rotmode);
612                 }
613                 else if (ob->rotmode == ROT_MODE_AXISANGLE) {
614                         axis_angle_to_gimbal_axis(gmat, ob->rotAxis, ob->rotAngle);
615                 }
616                 else { /* quat */
617                         return 0;
618                 }
619
620                 if (ob->parent) {
621                         float parent_mat[3][3];
622                         copy_m3_m4(parent_mat, ob->parent->obmat);
623                         normalize_m3(parent_mat);
624                         mul_m3_m3m3(gmat, parent_mat, gmat);
625                 }
626                 return 1;
627         }
628
629         return 0;
630 }
631
632 void ED_transform_calc_orientation_from_type(
633         const bContext *C, float r_mat[3][3])
634 {
635         ARegion *ar = CTX_wm_region(C);
636         Scene *scene = CTX_data_scene(C);
637         ViewLayer *view_layer = CTX_data_view_layer(C);
638         Object *obedit = CTX_data_edit_object(C);
639         RegionView3D *rv3d = ar->regiondata;
640         Object *ob = OBACT(view_layer);
641         const short orientation_type = scene->orientation_type;
642         const int pivot_point = scene->toolsettings->transform_pivot_point;
643
644         ED_transform_calc_orientation_from_type_ex(
645                 C, r_mat,
646                 scene, rv3d, ob, obedit, orientation_type, pivot_point);
647 }
648
649 void ED_transform_calc_orientation_from_type_ex(
650         const bContext *C, float r_mat[3][3],
651         /* extra args (can be accessed from context) */
652         Scene *scene, RegionView3D *rv3d, Object *ob, Object *obedit,
653         const short orientation_type, const int pivot_point)
654 {
655         bool ok = false;
656
657         switch (orientation_type) {
658                 case V3D_MANIP_GLOBAL:
659                 {
660                         break; /* nothing to do */
661                 }
662                 case V3D_MANIP_GIMBAL:
663                 {
664                         if (gimbal_axis(ob, r_mat)) {
665                                 ok = true;
666                                 break;
667                         }
668                         /* if not gimbal, fall through to normal */
669                         ATTR_FALLTHROUGH;
670                 }
671                 case V3D_MANIP_NORMAL:
672                 {
673                         if (obedit || ob->mode & OB_MODE_POSE) {
674                                 ED_getTransformOrientationMatrix(C, r_mat, pivot_point);
675                                 ok = true;
676                                 break;
677                         }
678                         /* no break we define 'normal' as 'local' in Object mode */
679                         ATTR_FALLTHROUGH;
680                 }
681                 case V3D_MANIP_LOCAL:
682                 {
683                         if (ob->mode & OB_MODE_POSE) {
684                                 /* each bone moves on its own local axis, but  to avoid confusion,
685                                  * use the active pones axis for display [#33575], this works as expected on a single bone
686                                  * and users who select many bones will understand what's going on and what local means
687                                  * when they start transforming */
688                                 ED_getTransformOrientationMatrix(C, r_mat, pivot_point);
689                                 ok = true;
690                                 break;
691                         }
692                         copy_m3_m4(r_mat, ob->obmat);
693                         normalize_m3(r_mat);
694                         ok = true;
695                         break;
696                 }
697                 case V3D_MANIP_VIEW:
698                 {
699                         if (rv3d != NULL) {
700                                 copy_m3_m4(r_mat, rv3d->viewinv);
701                                 normalize_m3(r_mat);
702                                 ok = true;
703                         }
704                         break;
705                 }
706                 case V3D_MANIP_CURSOR:
707                 {
708                         ED_view3d_cursor3d_calc_mat3(scene, r_mat);
709                         ok = true;
710                         break;
711                 }
712                 case V3D_MANIP_CUSTOM:
713                 {
714                         TransformOrientation *custom_orientation = BKE_scene_transform_orientation_find(
715                                 scene, scene->orientation_index_custom);
716                         if (applyTransformOrientation(custom_orientation, r_mat, NULL)) {
717                                 ok = true;
718                         }
719                         break;
720                 }
721         }
722
723         if (!ok) {
724                 unit_m3(r_mat);
725         }
726 }
727
728 /* centroid, boundbox, of selection */
729 /* returns total items selected */
730 int ED_transform_calc_gizmo_stats(
731         const bContext *C,
732         const struct TransformCalcParams *params,
733         struct TransformBounds *tbounds)
734 {
735         ScrArea *sa = CTX_wm_area(C);
736         ARegion *ar = CTX_wm_region(C);
737         Scene *scene = CTX_data_scene(C);
738         Depsgraph *depsgraph = CTX_data_depsgraph(C);
739         ViewLayer *view_layer = CTX_data_view_layer(C);
740         View3D *v3d = sa->spacedata.first;
741         Object *obedit = CTX_data_edit_object(C);
742         RegionView3D *rv3d = ar->regiondata;
743         Base *base;
744         Object *ob = OBACT(view_layer);
745         bGPdata *gpd = CTX_data_gpencil_data(C);
746         const bool is_gp_edit = GPENCIL_ANY_MODE(gpd);
747         int a, totsel = 0;
748         const int pivot_point = scene->toolsettings->transform_pivot_point;
749
750         /* transform widget matrix */
751         unit_m4(rv3d->twmat);
752
753         unit_m3(rv3d->tw_axis_matrix);
754         zero_v3(rv3d->tw_axis_min);
755         zero_v3(rv3d->tw_axis_max);
756
757         rv3d->twdrawflag = 0xFFFF;
758
759         /* global, local or normal orientation?
760          * if we could check 'totsel' now, this should be skipped with no selection. */
761         if (ob) {
762                 const short orientation_type = params->orientation_type ? (params->orientation_type - 1) : scene->orientation_type;
763                 float mat[3][3];
764                 ED_transform_calc_orientation_from_type_ex(
765                         C, mat,
766                         scene, rv3d, ob, obedit, orientation_type, pivot_point);
767                 copy_m4_m3(rv3d->twmat, mat);
768         }
769
770         /* transform widget centroid/center */
771         reset_tw_center(tbounds);
772
773         copy_m3_m4(tbounds->axis, rv3d->twmat);
774         if (params->use_local_axis && (ob && ob->mode & OB_MODE_EDIT)) {
775                 float diff_mat[3][3];
776                 copy_m3_m4(diff_mat, ob->obmat);
777                 normalize_m3(diff_mat);
778                 invert_m3(diff_mat);
779                 mul_m3_m3m3(tbounds->axis, tbounds->axis, diff_mat);
780                 normalize_m3(tbounds->axis);
781         }
782
783         if (is_gp_edit) {
784                 float diff_mat[4][4];
785                 for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
786                         /* only editable and visible layers are considered */
787                         const bool use_mat_local = gpl->parent != NULL;
788                         if (gpencil_layer_is_editable(gpl) && (gpl->actframe != NULL)) {
789
790                                 /* calculate difference matrix */
791                                 ED_gpencil_parent_location(depsgraph, ob, gpd, gpl, diff_mat);
792
793                                 for (bGPDstroke *gps = gpl->actframe->strokes.first; gps; gps = gps->next) {
794                                         /* skip strokes that are invalid for current view */
795                                         if (ED_gpencil_stroke_can_use(C, gps) == false) {
796                                                 continue;
797                                         }
798
799                                         /* we're only interested in selected points here... */
800                                         if (gps->flag & GP_STROKE_SELECT) {
801                                                 bGPDspoint *pt;
802                                                 int i;
803
804                                                 /* Change selection status of all points, then make the stroke match */
805                                                 for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
806                                                         if (pt->flag & GP_SPOINT_SELECT) {
807                                                                 calc_tw_center_with_matrix(tbounds, &pt->x, use_mat_local, diff_mat);
808                                                                 totsel++;
809                                                         }
810                                                 }
811                                         }
812                                 }
813                         }
814                 }
815
816
817                 /* selection center */
818                 if (totsel) {
819                         mul_v3_fl(tbounds->center, 1.0f / (float)totsel);   /* centroid! */
820                 }
821         }
822         else if (obedit) {
823
824 #define FOREACH_EDIT_OBJECT_BEGIN(ob_iter, use_mat_local) \
825                 { \
826                         invert_m4_m4(obedit->imat, obedit->obmat); \
827                         uint objects_len = 0; \
828                         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode(view_layer, CTX_wm_view3d(C), &objects_len); \
829                         for (uint ob_index = 0; ob_index < objects_len; ob_index++) { \
830                                 Object *ob_iter = objects[ob_index]; \
831                                 const bool use_mat_local = (ob_iter != obedit);
832
833 #define FOREACH_EDIT_OBJECT_END() \
834                         } \
835                         MEM_freeN(objects); \
836                 } ((void)0)
837
838                 ob = obedit;
839                 if (obedit->type == OB_MESH) {
840                         FOREACH_EDIT_OBJECT_BEGIN(ob_iter, use_mat_local) {
841                                 BMEditMesh *em_iter = BKE_editmesh_from_object(ob_iter);
842                                 BMesh *bm = em_iter->bm;
843
844                                 if (bm->totvertsel == 0) {
845                                         continue;
846                                 }
847
848                                 BMVert *eve;
849                                 BMIter iter;
850
851                                 float mat_local[4][4];
852                                 if (use_mat_local) {
853                                         mul_m4_m4m4(mat_local, obedit->imat, ob_iter->obmat);
854                                 }
855
856                                 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
857                                         if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
858                                                 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
859                                                         calc_tw_center_with_matrix(tbounds, eve->co, use_mat_local, mat_local);
860                                                         totsel++;
861                                                 }
862                                         }
863                                 }
864                         } FOREACH_EDIT_OBJECT_END();
865                 } /* end editmesh */
866                 else if (obedit->type == OB_ARMATURE) {
867                         FOREACH_EDIT_OBJECT_BEGIN(ob_iter, use_mat_local) {
868                                 bArmature *arm = ob_iter->data;
869
870                                 float mat_local[4][4];
871                                 if (use_mat_local) {
872                                         mul_m4_m4m4(mat_local, obedit->imat, ob_iter->obmat);
873                                 }
874                                 for (EditBone *ebo = arm->edbo->first; ebo; ebo = ebo->next) {
875                                         if (EBONE_VISIBLE(arm, ebo)) {
876                                                 if (ebo->flag & BONE_TIPSEL) {
877                                                         calc_tw_center_with_matrix(tbounds, ebo->tail, use_mat_local, mat_local);
878                                                         totsel++;
879                                                 }
880                                                 if ((ebo->flag & BONE_ROOTSEL) &&
881                                                     /* don't include same point multiple times */
882                                                     ((ebo->flag & BONE_CONNECTED) &&
883                                                      (ebo->parent != NULL) &&
884                                                      (ebo->parent->flag & BONE_TIPSEL) &&
885                                                      EBONE_VISIBLE(arm, ebo->parent)) == 0)
886                                                 {
887                                                         calc_tw_center_with_matrix(tbounds, ebo->head, use_mat_local, mat_local);
888                                                         totsel++;
889                                                 }
890                                                 if (ebo->flag & BONE_SELECTED) {
891                                                         protectflag_to_drawflags_ebone(rv3d, ebo);
892                                                 }
893                                         }
894                                 }
895                         } FOREACH_EDIT_OBJECT_END();
896                 }
897                 else if (ELEM(obedit->type, OB_CURVE, OB_SURF)) {
898                         FOREACH_EDIT_OBJECT_BEGIN(ob_iter, use_mat_local) {
899                                 Curve *cu = ob_iter->data;
900                                 Nurb *nu;
901                                 BezTriple *bezt;
902                                 BPoint *bp;
903                                 ListBase *nurbs = BKE_curve_editNurbs_get(cu);
904
905                                 float mat_local[4][4];
906                                 if (use_mat_local) {
907                                         mul_m4_m4m4(mat_local, obedit->imat, ob_iter->obmat);
908                                 }
909
910                                 nu = nurbs->first;
911                                 while (nu) {
912                                         if (nu->type == CU_BEZIER) {
913                                                 bezt = nu->bezt;
914                                                 a = nu->pntsu;
915                                                 while (a--) {
916                                                         /* exceptions
917                                                          * if handles are hidden then only check the center points.
918                                                          * If the center knot is selected then only use this as the center point.
919                                                          */
920                                                         if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_CU_HANDLES) == 0) {
921                                                                 if (bezt->f2 & SELECT) {
922                                                                         calc_tw_center_with_matrix(tbounds, bezt->vec[1], use_mat_local, mat_local);
923                                                                         totsel++;
924                                                                 }
925                                                         }
926                                                         else if (bezt->f2 & SELECT) {
927                                                                 calc_tw_center_with_matrix(tbounds, bezt->vec[1], use_mat_local, mat_local);
928                                                                 totsel++;
929                                                         }
930                                                         else {
931                                                                 if (bezt->f1 & SELECT) {
932                                                                         const float *co = bezt->vec[(pivot_point == V3D_AROUND_LOCAL_ORIGINS) ? 1 : 0];
933                                                                         calc_tw_center_with_matrix(tbounds, co, use_mat_local, mat_local);
934                                                                         totsel++;
935                                                                 }
936                                                                 if (bezt->f3 & SELECT) {
937                                                                         const float *co = bezt->vec[(pivot_point == V3D_AROUND_LOCAL_ORIGINS) ? 1 : 2];
938                                                                         calc_tw_center_with_matrix(tbounds, co, use_mat_local, mat_local);
939                                                                         totsel++;
940                                                                 }
941                                                         }
942                                                         bezt++;
943                                                 }
944                                         }
945                                         else {
946                                                 bp = nu->bp;
947                                                 a = nu->pntsu * nu->pntsv;
948                                                 while (a--) {
949                                                         if (bp->f1 & SELECT) {
950                                                                 calc_tw_center_with_matrix(tbounds, bp->vec, use_mat_local, mat_local);
951                                                                 totsel++;
952                                                         }
953                                                         bp++;
954                                                 }
955                                         }
956                                         nu = nu->next;
957                                 }
958                         } FOREACH_EDIT_OBJECT_END();
959                 }
960                 else if (obedit->type == OB_MBALL) {
961                         FOREACH_EDIT_OBJECT_BEGIN(ob_iter, use_mat_local) {
962                                 MetaBall *mb = (MetaBall *)ob_iter->data;
963
964                                 float mat_local[4][4];
965                                 if (use_mat_local) {
966                                         mul_m4_m4m4(mat_local, obedit->imat, ob_iter->obmat);
967                                 }
968
969                                 for (MetaElem *ml = mb->editelems->first; ml; ml = ml->next) {
970                                         if (ml->flag & SELECT) {
971                                                 calc_tw_center_with_matrix(tbounds, &ml->x, use_mat_local, mat_local);
972                                                 totsel++;
973                                         }
974                                 }
975                         } FOREACH_EDIT_OBJECT_END();
976                 }
977                 else if (obedit->type == OB_LATTICE) {
978                         FOREACH_EDIT_OBJECT_BEGIN(ob_iter, use_mat_local) {
979                                 Lattice *lt = ((Lattice *)ob_iter->data)->editlatt->latt;
980                                 BPoint *bp = lt->def;
981                                 a = lt->pntsu * lt->pntsv * lt->pntsw;
982
983                                 float mat_local[4][4];
984                                 if (use_mat_local) {
985                                         mul_m4_m4m4(mat_local, obedit->imat, ob_iter->obmat);
986                                 }
987
988                                 while (a--) {
989                                         if (bp->f1 & SELECT) {
990                                                 calc_tw_center_with_matrix(tbounds, bp->vec, use_mat_local, mat_local);
991                                                 totsel++;
992                                         }
993                                         bp++;
994                                 }
995                         } FOREACH_EDIT_OBJECT_END();
996                 }
997
998 #undef FOREACH_EDIT_OBJECT_BEGIN
999 #undef FOREACH_EDIT_OBJECT_END
1000
1001                 /* selection center */
1002                 if (totsel) {
1003                         mul_v3_fl(tbounds->center, 1.0f / (float)totsel);   // centroid!
1004                         mul_m4_v3(obedit->obmat, tbounds->center);
1005                         mul_m4_v3(obedit->obmat, tbounds->min);
1006                         mul_m4_v3(obedit->obmat, tbounds->max);
1007                 }
1008         }
1009         else if (ob && (ob->mode & OB_MODE_POSE)) {
1010                 invert_m4_m4(ob->imat, ob->obmat);
1011                 uint objects_len = 0;
1012                 Object **objects = BKE_view_layer_array_from_objects_in_mode(
1013                         view_layer, v3d, &objects_len, {.object_mode = OB_MODE_POSE});
1014                 for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
1015                         Object *ob_iter = objects[ob_index];
1016                         const bool use_mat_local = (ob_iter != ob);
1017                         bPoseChannel *pchan;
1018
1019                         /* mislead counting bones... bah. We don't know the gizmo mode, could be mixed */
1020                         const int mode = TFM_ROTATION;
1021
1022                         const int totsel_iter = count_set_pose_transflags(ob_iter, mode, V3D_AROUND_CENTER_BOUNDS, NULL);
1023
1024                         if (totsel_iter) {
1025                                 float mat_local[4][4];
1026                                 if (use_mat_local) {
1027                                         mul_m4_m4m4(mat_local, ob->imat, ob_iter->obmat);
1028                                 }
1029
1030                                 /* use channels to get stats */
1031                                 for (pchan = ob_iter->pose->chanbase.first; pchan; pchan = pchan->next) {
1032                                         Bone *bone = pchan->bone;
1033                                         if (bone && (bone->flag & BONE_TRANSFORM)) {
1034                                                 calc_tw_center_with_matrix(tbounds, pchan->pose_head, use_mat_local, mat_local);
1035                                                 protectflag_to_drawflags_pchan(rv3d, pchan);
1036                                         }
1037                                 }
1038                                 totsel += totsel_iter;
1039                         }
1040                 }
1041                 MEM_freeN(objects);
1042
1043                 if (totsel) {
1044                         mul_v3_fl(tbounds->center, 1.0f / (float)totsel);   // centroid!
1045                         mul_m4_v3(ob->obmat, tbounds->center);
1046                         mul_m4_v3(ob->obmat, tbounds->min);
1047                         mul_m4_v3(ob->obmat, tbounds->max);
1048                 }
1049         }
1050         else if (ob && (ob->mode & OB_MODE_ALL_PAINT)) {
1051                 /* pass */
1052         }
1053         else if (ob && ob->mode & OB_MODE_PARTICLE_EDIT) {
1054                 PTCacheEdit *edit = PE_get_current(scene, ob);
1055                 PTCacheEditPoint *point;
1056                 PTCacheEditKey *ek;
1057                 int k;
1058
1059                 if (edit) {
1060                         point = edit->points;
1061                         for (a = 0; a < edit->totpoint; a++, point++) {
1062                                 if (point->flag & PEP_HIDE) continue;
1063
1064                                 for (k = 0, ek = point->keys; k < point->totkey; k++, ek++) {
1065                                         if (ek->flag & PEK_SELECT) {
1066                                                 calc_tw_center(tbounds, (ek->flag & PEK_USE_WCO) ? ek->world_co : ek->co);
1067                                                 totsel++;
1068                                         }
1069                                 }
1070                         }
1071
1072                         /* selection center */
1073                         if (totsel)
1074                                 mul_v3_fl(tbounds->center, 1.0f / (float)totsel);  // centroid!
1075                 }
1076         }
1077         else {
1078
1079                 /* we need the one selected object, if its not active */
1080                 base = BASACT(view_layer);
1081                 ob = OBACT(view_layer);
1082                 if (base && ((base->flag & BASE_SELECTED) == 0)) ob = NULL;
1083
1084                 for (base = view_layer->object_bases.first; base; base = base->next) {
1085                         if (!TESTBASELIB(v3d, base)) {
1086                                 continue;
1087                         }
1088                         if (ob == NULL) {
1089                                 ob = base->object;
1090                         }
1091
1092                         /* Get the boundbox out of the evaluated object. */
1093                         const BoundBox *bb = NULL;
1094                         if (params->use_only_center == false) {
1095                                 bb = BKE_object_boundbox_get(base->object);
1096                         }
1097
1098                         if (params->use_only_center || (bb == NULL)) {
1099                                 calc_tw_center(tbounds, base->object->obmat[3]);
1100                         }
1101                         else {
1102                                 for (uint j = 0; j < 8; j++) {
1103                                         float co[3];
1104                                         mul_v3_m4v3(co, base->object->obmat, bb->vec[j]);
1105                                         calc_tw_center(tbounds, co);
1106                                 }
1107                         }
1108                         protectflag_to_drawflags(base->object->protectflag, &rv3d->twdrawflag);
1109                         totsel++;
1110                 }
1111
1112                 /* selection center */
1113                 if (totsel) {
1114                         mul_v3_fl(tbounds->center, 1.0f / (float)totsel);   // centroid!
1115                 }
1116         }
1117
1118         if (totsel == 0) {
1119                 unit_m4(rv3d->twmat);
1120         }
1121         else {
1122                 copy_v3_v3(rv3d->tw_axis_min, tbounds->axis_min);
1123                 copy_v3_v3(rv3d->tw_axis_max, tbounds->axis_max);
1124                 copy_m3_m3(rv3d->tw_axis_matrix, tbounds->axis);
1125         }
1126
1127         return totsel;
1128 }
1129
1130 static void gizmo_get_idot(RegionView3D *rv3d, float r_idot[3])
1131 {
1132         float view_vec[3], axis_vec[3];
1133         ED_view3d_global_to_vector(rv3d, rv3d->twmat[3], view_vec);
1134         for (int i = 0; i < 3; i++) {
1135                 normalize_v3_v3(axis_vec, rv3d->twmat[i]);
1136                 r_idot[i] = 1.0f - fabsf(dot_v3v3(view_vec, axis_vec));
1137         }
1138 }
1139
1140 static void gizmo_prepare_mat(
1141         const bContext *C, RegionView3D *rv3d, const struct TransformBounds *tbounds)
1142 {
1143         Scene *scene = CTX_data_scene(C);
1144         ViewLayer *view_layer = CTX_data_view_layer(C);
1145
1146         switch (scene->toolsettings->transform_pivot_point) {
1147                 case V3D_AROUND_CENTER_BOUNDS:
1148                 case V3D_AROUND_ACTIVE:
1149                 {
1150                         mid_v3_v3v3(rv3d->twmat[3], tbounds->min, tbounds->max);
1151
1152                         if (scene->toolsettings->transform_pivot_point == V3D_AROUND_ACTIVE) {
1153                                 bGPdata *gpd = CTX_data_gpencil_data(C);
1154                                 Object *ob = OBACT(view_layer);
1155                                 if (gpd && (gpd->flag & GP_DATA_STROKE_EDITMODE)) {
1156                                         /* pass */
1157                                 }
1158                                 else if (ob != NULL) {
1159                                         ED_object_calc_active_center(ob, false, rv3d->twmat[3]);
1160                                 }
1161                         }
1162                         break;
1163                 }
1164                 case V3D_AROUND_LOCAL_ORIGINS:
1165                 case V3D_AROUND_CENTER_MEDIAN:
1166                         copy_v3_v3(rv3d->twmat[3], tbounds->center);
1167                         break;
1168                 case V3D_AROUND_CURSOR:
1169                         copy_v3_v3(rv3d->twmat[3], scene->cursor.location);
1170                         break;
1171         }
1172 }
1173
1174 /**
1175  * Sets up \a r_start and \a r_len to define arrow line range.
1176  * Needed to adjust line drawing for combined gizmo axis types.
1177  */
1178 static void gizmo_line_range(const int twtype, const short axis_type, float *r_start, float *r_len)
1179 {
1180         const float ofs = 0.2f;
1181
1182         *r_start = 0.2f;
1183         *r_len = 1.0f;
1184
1185         switch (axis_type) {
1186                 case MAN_AXES_TRANSLATE:
1187                         if (twtype & SCE_GIZMO_SHOW_SCALE) {
1188                                 *r_start = *r_len - ofs + 0.075f;
1189                         }
1190                         if (twtype & SCE_GIZMO_SHOW_ROTATE) {
1191                                 *r_len += ofs;
1192                         }
1193                         break;
1194                 case MAN_AXES_SCALE:
1195                         if (twtype & (SCE_GIZMO_SHOW_TRANSLATE | SCE_GIZMO_SHOW_ROTATE)) {
1196                                 *r_len -= ofs + 0.025f;
1197                         }
1198                         break;
1199         }
1200
1201         *r_len -= *r_start;
1202 }
1203
1204 static void gizmo_xform_message_subscribe(
1205         wmGizmoGroup *gzgroup, struct wmMsgBus *mbus,
1206         Scene *scene, bScreen *UNUSED(screen), ScrArea *UNUSED(sa), ARegion *ar, const void *type_fn)
1207 {
1208         /* Subscribe to view properties */
1209         wmMsgSubscribeValue msg_sub_value_gz_tag_refresh = {
1210                 .owner = ar,
1211                 .user_data = gzgroup->parent_gzmap,
1212                 .notify = WM_gizmo_do_msg_notify_tag_refresh,
1213         };
1214
1215         PointerRNA scene_ptr;
1216         RNA_id_pointer_create(&scene->id, &scene_ptr);
1217
1218         {
1219                 extern PropertyRNA rna_Scene_transform_orientation;
1220                 extern PropertyRNA rna_Scene_cursor_location;
1221                 const PropertyRNA *props[] = {
1222                         &rna_Scene_transform_orientation,
1223                         (scene->toolsettings->transform_pivot_point == V3D_AROUND_CURSOR) ? &rna_Scene_cursor_location : NULL,
1224                 };
1225                 for (int i = 0; i < ARRAY_SIZE(props); i++) {
1226                         if (props[i]) {
1227                                 WM_msg_subscribe_rna(mbus, &scene_ptr, props[i], &msg_sub_value_gz_tag_refresh, __func__);
1228                         }
1229                 }
1230         }
1231
1232         PointerRNA toolsettings_ptr;
1233         RNA_pointer_create(&scene->id, &RNA_ToolSettings, scene->toolsettings, &toolsettings_ptr);
1234
1235         if (type_fn == TRANSFORM_GGT_gizmo) {
1236                 extern PropertyRNA rna_ToolSettings_transform_pivot_point;
1237                 extern PropertyRNA rna_ToolSettings_use_gizmo_mode;
1238                 const PropertyRNA *props[] = {
1239                         &rna_ToolSettings_transform_pivot_point,
1240                         &rna_ToolSettings_use_gizmo_mode,
1241                 };
1242                 for (int i = 0; i < ARRAY_SIZE(props); i++) {
1243                         WM_msg_subscribe_rna(mbus, &toolsettings_ptr, props[i], &msg_sub_value_gz_tag_refresh, __func__);
1244                 }
1245         }
1246         else if (type_fn == VIEW3D_GGT_xform_cage) {
1247                 /* pass */
1248         }
1249         else if (type_fn == VIEW3D_GGT_xform_shear) {
1250                 /* pass */
1251         }
1252         else {
1253                 BLI_assert(0);
1254         }
1255
1256         WM_msg_subscribe_rna_anon_prop(mbus, Window, view_layer, &msg_sub_value_gz_tag_refresh);
1257 }
1258
1259
1260 void drawDial3d(const TransInfo *t)
1261 {
1262         if (t->mode == TFM_ROTATION && t->spacetype == SPACE_VIEW3D) {
1263                 wmGizmo *gz = wm_gizmomap_highlight_get(t->ar->gizmo_map);
1264                 if (gz == NULL) {
1265                         /* We only draw Dial3d if the operator has been called by a gizmo. */
1266                         return;
1267                 }
1268
1269                 float mat_basis[4][4];
1270                 float mat_final[4][4];
1271                 float color[4];
1272                 float increment;
1273                 float line_with = GIZMO_AXIS_LINE_WIDTH + 1.0f;
1274                 float scale = UI_DPI_FAC * U.gizmo_size;
1275
1276                 int axis_idx;
1277
1278                 const TransCon *tc = &(t->con);
1279                 if (tc->mode & CON_APPLY) {
1280                         if (tc->mode & CON_AXIS0) {
1281                                 axis_idx = MAN_AXIS_ROT_X;
1282                                 negate_v3_v3(mat_basis[2], tc->mtx[0]);
1283                         }
1284                         else if (tc->mode &  CON_AXIS1) {
1285                                 axis_idx = MAN_AXIS_ROT_Y;
1286                                 negate_v3_v3(mat_basis[2], tc->mtx[1]);
1287                         }
1288                         else {
1289                                 BLI_assert((tc->mode & CON_AXIS2) != 0);
1290                                 axis_idx = MAN_AXIS_ROT_Z;
1291                                 negate_v3_v3(mat_basis[2], tc->mtx[2]);
1292                         }
1293                 }
1294                 else {
1295                         axis_idx = MAN_AXIS_ROT_C;
1296                         negate_v3_v3(mat_basis[2], t->axis);
1297                         scale *= 1.2f;
1298                         line_with -= 1.0f;
1299                 }
1300
1301                 copy_v3_v3(mat_basis[3], t->center_global);
1302                 mat_basis[2][3] = -dot_v3v3(mat_basis[2], mat_basis[3]);
1303
1304                 if (ED_view3d_win_to_3d_on_plane(
1305                             t->ar, mat_basis[2], (float[2]){UNPACK2(t->mouse.imval)},
1306                             false, mat_basis[1]))
1307                 {
1308                         sub_v3_v3(mat_basis[1], mat_basis[3]);
1309                         normalize_v3(mat_basis[1]);
1310                         cross_v3_v3v3(mat_basis[0], mat_basis[1], mat_basis[2]);
1311                 }
1312                 else {
1313                         /* The plane and the mouse direction are parallel.
1314                          * Calculate a matrix orthogonal to the axis. */
1315                         ortho_basis_v3v3_v3(mat_basis[0], mat_basis[1], mat_basis[2]);
1316                 }
1317
1318                 mat_basis[0][3] = 0.0f;
1319                 mat_basis[1][3] = 0.0f;
1320                 mat_basis[2][3] = 0.0f;
1321                 mat_basis[3][3] = 1.0f;
1322
1323                 copy_m4_m4(mat_final, mat_basis);
1324                 scale *= ED_view3d_pixel_size_no_ui_scale(t->ar->regiondata, mat_final[3]);
1325                 mul_mat3_m4_fl(mat_final, scale);
1326
1327                 if ((t->tsnap.mode & (SCE_SNAP_MODE_INCREMENT | SCE_SNAP_MODE_GRID)) &&
1328                     activeSnap(t))
1329                 {
1330                         increment = (t->modifiers & MOD_PRECISION) ? t->snap[2] : t->snap[1];
1331                 }
1332                 else {
1333                         increment = t->snap[0];
1334                 }
1335
1336                 BLI_assert(axis_idx >= MAN_AXIS_RANGE_ROT_START && axis_idx < MAN_AXIS_RANGE_ROT_END);
1337                 gizmo_get_axis_color(axis_idx, NULL, color, color);
1338
1339                 GPU_depth_test(false);
1340                 GPU_blend(true);
1341                 GPU_line_smooth(true);
1342
1343                 ED_gizmotypes_dial_3d_draw_util(
1344                         mat_basis, mat_final, line_with, color,
1345                         &(struct Dial3dParams){
1346                             .draw_options = ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_VALUE,
1347                             .angle_delta = t->values[0],
1348                             .angle_increment = increment,
1349                         });
1350
1351                 GPU_line_smooth(false);
1352                 GPU_depth_test(true);
1353                 GPU_blend(false);
1354         }
1355 }
1356
1357 /** \} */
1358
1359
1360 /* -------------------------------------------------------------------- */
1361 /** \name Transform Gizmo
1362  * \{ */
1363
1364 static GizmoGroup *gizmogroup_init(wmGizmoGroup *gzgroup)
1365 {
1366         GizmoGroup *ggd;
1367
1368         ggd = MEM_callocN(sizeof(GizmoGroup), "gizmo_data");
1369
1370         const wmGizmoType *gzt_arrow = WM_gizmotype_find("GIZMO_GT_arrow_3d", true);
1371         const wmGizmoType *gzt_dial = WM_gizmotype_find("GIZMO_GT_dial_3d", true);
1372         const wmGizmoType *gzt_prim = WM_gizmotype_find("GIZMO_GT_primitive_3d", true);
1373
1374 #define GIZMO_NEW_ARROW(v, draw_style) { \
1375         ggd->gizmos[v] = WM_gizmo_new_ptr(gzt_arrow, gzgroup, NULL); \
1376         RNA_enum_set(ggd->gizmos[v]->ptr, "draw_style", draw_style); \
1377 } ((void)0)
1378 #define GIZMO_NEW_DIAL(v, draw_options) { \
1379         ggd->gizmos[v] = WM_gizmo_new_ptr(gzt_dial, gzgroup, NULL); \
1380         RNA_enum_set(ggd->gizmos[v]->ptr, "draw_options", draw_options); \
1381 } ((void)0)
1382 #define GIZMO_NEW_PRIM(v, draw_style) { \
1383         ggd->gizmos[v] = WM_gizmo_new_ptr(gzt_prim, gzgroup, NULL); \
1384         RNA_enum_set(ggd->gizmos[v]->ptr, "draw_style", draw_style); \
1385 } ((void)0)
1386
1387         /* add/init widgets - order matters! */
1388         GIZMO_NEW_DIAL(MAN_AXIS_ROT_T, ED_GIZMO_DIAL_DRAW_FLAG_FILL);
1389
1390         GIZMO_NEW_DIAL(MAN_AXIS_SCALE_C, ED_GIZMO_DIAL_DRAW_FLAG_NOP);
1391
1392         GIZMO_NEW_ARROW(MAN_AXIS_SCALE_X, ED_GIZMO_ARROW_STYLE_BOX);
1393         GIZMO_NEW_ARROW(MAN_AXIS_SCALE_Y, ED_GIZMO_ARROW_STYLE_BOX);
1394         GIZMO_NEW_ARROW(MAN_AXIS_SCALE_Z, ED_GIZMO_ARROW_STYLE_BOX);
1395
1396         GIZMO_NEW_PRIM(MAN_AXIS_SCALE_XY, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1397         GIZMO_NEW_PRIM(MAN_AXIS_SCALE_YZ, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1398         GIZMO_NEW_PRIM(MAN_AXIS_SCALE_ZX, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1399
1400         GIZMO_NEW_DIAL(MAN_AXIS_ROT_X, ED_GIZMO_DIAL_DRAW_FLAG_CLIP);
1401         GIZMO_NEW_DIAL(MAN_AXIS_ROT_Y, ED_GIZMO_DIAL_DRAW_FLAG_CLIP);
1402         GIZMO_NEW_DIAL(MAN_AXIS_ROT_Z, ED_GIZMO_DIAL_DRAW_FLAG_CLIP);
1403
1404         /* init screen aligned widget last here, looks better, behaves better */
1405         GIZMO_NEW_DIAL(MAN_AXIS_ROT_C, ED_GIZMO_DIAL_DRAW_FLAG_NOP);
1406
1407         GIZMO_NEW_DIAL(MAN_AXIS_TRANS_C, ED_GIZMO_DIAL_DRAW_FLAG_NOP);
1408
1409         GIZMO_NEW_ARROW(MAN_AXIS_TRANS_X, ED_GIZMO_ARROW_STYLE_NORMAL);
1410         GIZMO_NEW_ARROW(MAN_AXIS_TRANS_Y, ED_GIZMO_ARROW_STYLE_NORMAL);
1411         GIZMO_NEW_ARROW(MAN_AXIS_TRANS_Z, ED_GIZMO_ARROW_STYLE_NORMAL);
1412
1413         GIZMO_NEW_PRIM(MAN_AXIS_TRANS_XY, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1414         GIZMO_NEW_PRIM(MAN_AXIS_TRANS_YZ, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1415         GIZMO_NEW_PRIM(MAN_AXIS_TRANS_ZX, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1416
1417         ggd->gizmos[MAN_AXIS_ROT_T]->flag |= WM_GIZMO_SELECT_BACKGROUND;
1418
1419         return ggd;
1420 }
1421
1422 /**
1423  * Custom handler for gizmo widgets
1424  */
1425 static int gizmo_modal(
1426         bContext *C, wmGizmo *widget, const wmEvent *event,
1427         eWM_GizmoFlagTweak UNUSED(tweak_flag))
1428 {
1429         /* Avoid unnecessary updates, partially address: T55458. */
1430         if (ELEM(event->type, TIMER, INBETWEEN_MOUSEMOVE)) {
1431                 return OPERATOR_RUNNING_MODAL;
1432         }
1433
1434         ARegion *ar = CTX_wm_region(C);
1435         RegionView3D *rv3d = ar->regiondata;
1436         struct TransformBounds tbounds;
1437
1438
1439         if (ED_transform_calc_gizmo_stats(
1440                     C, &(struct TransformCalcParams){
1441                         .use_only_center = true,
1442                     }, &tbounds))
1443         {
1444                 gizmo_prepare_mat(C, rv3d, &tbounds);
1445                 WM_gizmo_set_matrix_location(widget, rv3d->twmat[3]);
1446         }
1447
1448         ED_region_tag_redraw(ar);
1449
1450         return OPERATOR_RUNNING_MODAL;
1451 }
1452
1453 static void gizmogroup_init_properties_from_twtype(wmGizmoGroup *gzgroup)
1454 {
1455         struct {
1456                 wmOperatorType *translate, *rotate, *trackball, *resize;
1457         } ot_store = {NULL};
1458         GizmoGroup *ggd = gzgroup->customdata;
1459
1460         MAN_ITER_AXES_BEGIN(axis, axis_idx)
1461         {
1462                 const short axis_type = gizmo_get_axis_type(axis_idx);
1463                 bool constraint_axis[3] = {1, 0, 0};
1464                 PointerRNA *ptr = NULL;
1465
1466                 gizmo_get_axis_constraint(axis_idx, constraint_axis);
1467
1468                 /* custom handler! */
1469                 WM_gizmo_set_fn_custom_modal(axis, gizmo_modal);
1470
1471                 switch (axis_idx) {
1472                         case MAN_AXIS_TRANS_X:
1473                         case MAN_AXIS_TRANS_Y:
1474                         case MAN_AXIS_TRANS_Z:
1475                         case MAN_AXIS_SCALE_X:
1476                         case MAN_AXIS_SCALE_Y:
1477                         case MAN_AXIS_SCALE_Z:
1478                                 if (axis_idx >= MAN_AXIS_RANGE_TRANS_START && axis_idx < MAN_AXIS_RANGE_TRANS_END) {
1479                                         int draw_options = 0;
1480                                         if ((ggd->twtype & (SCE_GIZMO_SHOW_ROTATE | SCE_GIZMO_SHOW_SCALE)) == 0) {
1481                                                 draw_options |= ED_GIZMO_ARROW_DRAW_FLAG_STEM;
1482                                         }
1483                                         RNA_enum_set(axis->ptr, "draw_options", draw_options);
1484                                 }
1485
1486                                 WM_gizmo_set_line_width(axis, GIZMO_AXIS_LINE_WIDTH);
1487                                 break;
1488                         case MAN_AXIS_ROT_X:
1489                         case MAN_AXIS_ROT_Y:
1490                         case MAN_AXIS_ROT_Z:
1491                                 /* increased line width for better display */
1492                                 WM_gizmo_set_line_width(axis, GIZMO_AXIS_LINE_WIDTH + 1.0f);
1493                                 WM_gizmo_set_flag(axis, WM_GIZMO_DRAW_VALUE, true);
1494                                 break;
1495                         case MAN_AXIS_TRANS_XY:
1496                         case MAN_AXIS_TRANS_YZ:
1497                         case MAN_AXIS_TRANS_ZX:
1498                         case MAN_AXIS_SCALE_XY:
1499                         case MAN_AXIS_SCALE_YZ:
1500                         case MAN_AXIS_SCALE_ZX:
1501                         {
1502                                 const float ofs_ax = 7.0f;
1503                                 const float ofs[3] = {ofs_ax, ofs_ax, 0.0f};
1504                                 WM_gizmo_set_scale(axis, 0.07f);
1505                                 WM_gizmo_set_matrix_offset_location(axis, ofs);
1506                                 WM_gizmo_set_flag(axis, WM_GIZMO_DRAW_OFFSET_SCALE, true);
1507                                 break;
1508                         }
1509                         case MAN_AXIS_TRANS_C:
1510                         case MAN_AXIS_ROT_C:
1511                         case MAN_AXIS_SCALE_C:
1512                         case MAN_AXIS_ROT_T:
1513                                 WM_gizmo_set_line_width(axis, GIZMO_AXIS_LINE_WIDTH);
1514                                 if (axis_idx == MAN_AXIS_ROT_T) {
1515                                         WM_gizmo_set_flag(axis, WM_GIZMO_DRAW_HOVER, true);
1516                                 }
1517                                 else if (axis_idx == MAN_AXIS_ROT_C) {
1518                                         WM_gizmo_set_flag(axis, WM_GIZMO_DRAW_VALUE, true);
1519                                         WM_gizmo_set_scale(axis, 1.2f);
1520                                 }
1521                                 else {
1522                                         WM_gizmo_set_scale(axis, 0.2f);
1523                                 }
1524                                 break;
1525                 }
1526
1527                 switch (axis_type) {
1528                         case MAN_AXES_TRANSLATE:
1529                                 if (ot_store.translate == NULL) {
1530                                         ot_store.translate = WM_operatortype_find("TRANSFORM_OT_translate", true);
1531                                 }
1532                                 ptr = WM_gizmo_operator_set(axis, 0, ot_store.translate, NULL);
1533                                 break;
1534                         case MAN_AXES_ROTATE:
1535                         {
1536                                 wmOperatorType *ot_rotate;
1537                                 if (axis_idx == MAN_AXIS_ROT_T) {
1538                                         if (ot_store.trackball == NULL) {
1539                                                 ot_store.trackball = WM_operatortype_find("TRANSFORM_OT_trackball", true);
1540                                         }
1541                                         ot_rotate = ot_store.trackball;
1542                                 }
1543                                 else {
1544                                         if (ot_store.rotate == NULL) {
1545                                                 ot_store.rotate = WM_operatortype_find("TRANSFORM_OT_rotate", true);
1546                                         }
1547                                         ot_rotate = ot_store.rotate;
1548                                 }
1549                                 ptr = WM_gizmo_operator_set(axis, 0, ot_rotate, NULL);
1550                                 break;
1551                         }
1552                         case MAN_AXES_SCALE:
1553                         {
1554                                 if (ot_store.resize == NULL) {
1555                                         ot_store.resize = WM_operatortype_find("TRANSFORM_OT_resize", true);
1556                                 }
1557                                 ptr = WM_gizmo_operator_set(axis, 0, ot_store.resize, NULL);
1558                                 break;
1559                         }
1560                 }
1561
1562                 if (ptr) {
1563                         PropertyRNA *prop;
1564                         if (ELEM(true, UNPACK3(constraint_axis))) {
1565                                 if ((prop = RNA_struct_find_property(ptr, "constraint_axis"))) {
1566                                         RNA_property_boolean_set_array(ptr, prop, constraint_axis);
1567                                 }
1568                         }
1569
1570                         RNA_boolean_set(ptr, "release_confirm", 1);
1571                 }
1572         }
1573         MAN_ITER_AXES_END;
1574 }
1575
1576 static void WIDGETGROUP_gizmo_setup(const bContext *C, wmGizmoGroup *gzgroup)
1577 {
1578         GizmoGroup *ggd = gizmogroup_init(gzgroup);
1579
1580         gzgroup->customdata = ggd;
1581
1582         {
1583                 ggd->twtype = 0;
1584                 ScrArea *sa = CTX_wm_area(C);
1585                 const bToolRef *tref = sa->runtime.tool;
1586
1587                 if (tref == NULL || STREQ(tref->idname, "Transform")) {
1588                         /* Setup all gizmos, they can be toggled via 'ToolSettings.gizmo_flag' */
1589                         ggd->twtype = SCE_GIZMO_SHOW_TRANSLATE | SCE_GIZMO_SHOW_ROTATE | SCE_GIZMO_SHOW_SCALE;
1590                         ggd->use_twtype_refresh = true;
1591                 }
1592                 else if (STREQ(tref->idname, "Move")) {
1593                         ggd->twtype |= SCE_GIZMO_SHOW_TRANSLATE;
1594                 }
1595                 else if (STREQ(tref->idname, "Rotate")) {
1596                         ggd->twtype |= SCE_GIZMO_SHOW_ROTATE;
1597                 }
1598                 else if (STREQ(tref->idname, "Scale")) {
1599                         ggd->twtype |= SCE_GIZMO_SHOW_SCALE;
1600                 }
1601                 BLI_assert(ggd->twtype != 0);
1602                 ggd->twtype_init = ggd->twtype;
1603         }
1604
1605         /* *** set properties for axes *** */
1606         gizmogroup_init_properties_from_twtype(gzgroup);
1607 }
1608
1609 static void WIDGETGROUP_gizmo_refresh(const bContext *C, wmGizmoGroup *gzgroup)
1610 {
1611         GizmoGroup *ggd = gzgroup->customdata;
1612         ARegion *ar = CTX_wm_region(C);
1613         RegionView3D *rv3d = ar->regiondata;
1614         struct TransformBounds tbounds;
1615
1616         if (ggd->use_twtype_refresh) {
1617                 Scene *scene = CTX_data_scene(C);
1618                 ggd->twtype = scene->toolsettings->gizmo_flag & ggd->twtype_init;
1619                 if (ggd->twtype != ggd->twtype_prev) {
1620                         ggd->twtype_prev = ggd->twtype;
1621                         gizmogroup_init_properties_from_twtype(gzgroup);
1622                 }
1623         }
1624
1625         /* skip, we don't draw anything anyway */
1626         if ((ggd->all_hidden =
1627              (ED_transform_calc_gizmo_stats(
1628                      C, &(struct TransformCalcParams){
1629                          .use_only_center = true,
1630                      }, &tbounds) == 0)))
1631         {
1632                 return;
1633         }
1634
1635         gizmo_prepare_mat(C, rv3d, &tbounds);
1636
1637         /* *** set properties for axes *** */
1638
1639         MAN_ITER_AXES_BEGIN(axis, axis_idx)
1640         {
1641                 const short axis_type = gizmo_get_axis_type(axis_idx);
1642                 const int aidx_norm = gizmo_orientation_axis(axis_idx, NULL);
1643
1644                 WM_gizmo_set_matrix_location(axis, rv3d->twmat[3]);
1645
1646                 switch (axis_idx) {
1647                         case MAN_AXIS_TRANS_X:
1648                         case MAN_AXIS_TRANS_Y:
1649                         case MAN_AXIS_TRANS_Z:
1650                         case MAN_AXIS_SCALE_X:
1651                         case MAN_AXIS_SCALE_Y:
1652                         case MAN_AXIS_SCALE_Z:
1653                         {
1654                                 float start_co[3] = {0.0f, 0.0f, 0.0f};
1655                                 float len;
1656
1657                                 gizmo_line_range(ggd->twtype, axis_type, &start_co[2], &len);
1658
1659                                 WM_gizmo_set_matrix_rotation_from_z_axis(axis, rv3d->twmat[aidx_norm]);
1660                                 RNA_float_set(axis->ptr, "length", len);
1661
1662                                 if (axis_idx >= MAN_AXIS_RANGE_TRANS_START && axis_idx < MAN_AXIS_RANGE_TRANS_END) {
1663                                         if (ggd->twtype & SCE_GIZMO_SHOW_ROTATE) {
1664                                                 /* Avoid rotate and translate arrows overlap. */
1665                                                 start_co[2] += 0.215f;
1666                                         }
1667                                 }
1668                                 WM_gizmo_set_matrix_offset_location(axis, start_co);
1669                                 WM_gizmo_set_flag(axis, WM_GIZMO_DRAW_OFFSET_SCALE, true);
1670                                 break;
1671                         }
1672                         case MAN_AXIS_ROT_X:
1673                         case MAN_AXIS_ROT_Y:
1674                         case MAN_AXIS_ROT_Z:
1675                                 WM_gizmo_set_matrix_rotation_from_z_axis(axis, rv3d->twmat[aidx_norm]);
1676                                 break;
1677                         case MAN_AXIS_TRANS_XY:
1678                         case MAN_AXIS_TRANS_YZ:
1679                         case MAN_AXIS_TRANS_ZX:
1680                         case MAN_AXIS_SCALE_XY:
1681                         case MAN_AXIS_SCALE_YZ:
1682                         case MAN_AXIS_SCALE_ZX:
1683                         {
1684                                 const float *y_axis = rv3d->twmat[aidx_norm - 1 < 0 ? 2 : aidx_norm - 1];
1685                                 const float *z_axis = rv3d->twmat[aidx_norm];
1686                                 WM_gizmo_set_matrix_rotation_from_yz_axis(axis, y_axis, z_axis);
1687                                 break;
1688                         }
1689                 }
1690         }
1691         MAN_ITER_AXES_END;
1692 }
1693
1694 static void WIDGETGROUP_gizmo_message_subscribe(
1695         const bContext *C, wmGizmoGroup *gzgroup, struct wmMsgBus *mbus)
1696 {
1697         Scene *scene = CTX_data_scene(C);
1698         bScreen *screen = CTX_wm_screen(C);
1699         ScrArea *sa = CTX_wm_area(C);
1700         ARegion *ar = CTX_wm_region(C);
1701         gizmo_xform_message_subscribe(gzgroup, mbus, scene, screen, sa, ar, TRANSFORM_GGT_gizmo);
1702 }
1703
1704 static void WIDGETGROUP_gizmo_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
1705 {
1706         GizmoGroup *ggd = gzgroup->customdata;
1707         // ScrArea *sa = CTX_wm_area(C);
1708         ARegion *ar = CTX_wm_region(C);
1709         // View3D *v3d = sa->spacedata.first;
1710         RegionView3D *rv3d = ar->regiondata;
1711         float viewinv_m3[3][3];
1712         copy_m3_m4(viewinv_m3, rv3d->viewinv);
1713         float idot[3];
1714
1715         /* when looking through a selected camera, the gizmo can be at the
1716          * exact same position as the view, skip so we don't break selection */
1717         if (ggd->all_hidden || fabsf(ED_view3d_pixel_size(rv3d, rv3d->twmat[3])) < 1e-6f) {
1718                 MAN_ITER_AXES_BEGIN(axis, axis_idx)
1719                 {
1720                         WM_gizmo_set_flag(axis, WM_GIZMO_HIDDEN, true);
1721                 }
1722                 MAN_ITER_AXES_END;
1723                 return;
1724         }
1725         gizmo_get_idot(rv3d, idot);
1726
1727         /* *** set properties for axes *** */
1728         MAN_ITER_AXES_BEGIN(axis, axis_idx) {
1729                 const short axis_type = gizmo_get_axis_type(axis_idx);
1730                 /* XXX maybe unset _HIDDEN flag on redraw? */
1731
1732                 if (gizmo_is_axis_visible(rv3d, ggd->twtype, idot, axis_type, axis_idx)) {
1733                         WM_gizmo_set_flag(axis, WM_GIZMO_HIDDEN, false);
1734                 }
1735                 else {
1736                         WM_gizmo_set_flag(axis, WM_GIZMO_HIDDEN, true);
1737                         continue;
1738                 }
1739
1740                 float color[4], color_hi[4];
1741                 gizmo_get_axis_color(axis_idx, idot, color, color_hi);
1742                 WM_gizmo_set_color(axis, color);
1743                 WM_gizmo_set_color_highlight(axis, color_hi);
1744
1745                 switch (axis_idx) {
1746                         case MAN_AXIS_TRANS_C:
1747                         case MAN_AXIS_ROT_C:
1748                         case MAN_AXIS_SCALE_C:
1749                         case MAN_AXIS_ROT_T:
1750                                 WM_gizmo_set_matrix_rotation_from_z_axis(axis, rv3d->viewinv[2]);
1751                                 break;
1752                 }
1753         } MAN_ITER_AXES_END;
1754
1755         /* Refresh handled above when using view orientation. */
1756         if (!equals_m3m3(viewinv_m3, ggd->prev.viewinv_m3)) {
1757                 {
1758                         Scene *scene = CTX_data_scene(C);
1759                         switch (scene->orientation_type) {
1760                                 case V3D_MANIP_VIEW:
1761                                 {
1762                                         WIDGETGROUP_gizmo_refresh(C, gzgroup);
1763                                         break;
1764                                 }
1765                         }
1766                 }
1767                 copy_m3_m4(ggd->prev.viewinv_m3, rv3d->viewinv);
1768         }
1769
1770 }
1771
1772 static void WIDGETGROUP_gizmo_invoke_prepare(
1773         const bContext *C, wmGizmoGroup *gzgroup, wmGizmo *gz)
1774 {
1775
1776         /* Support shift click to constrain axis. */
1777         GizmoGroup *ggd = gzgroup->customdata;
1778         const int axis_idx = BLI_array_findindex(ggd->gizmos, ARRAY_SIZE(ggd->gizmos), &gz);
1779         int axis = -1;
1780         switch (axis_idx) {
1781                 case MAN_AXIS_TRANS_X:
1782                 case MAN_AXIS_TRANS_Y:
1783                 case MAN_AXIS_TRANS_Z:
1784                         axis = axis_idx - MAN_AXIS_TRANS_X; break;
1785                 case MAN_AXIS_SCALE_X:
1786                 case MAN_AXIS_SCALE_Y:
1787                 case MAN_AXIS_SCALE_Z:
1788                         axis = axis_idx - MAN_AXIS_SCALE_X; break;
1789         }
1790
1791         if (axis != -1) {
1792                 wmWindow *win = CTX_wm_window(C);
1793                 /* Swap single axis for two-axis constraint. */
1794                 bool flip = win->eventstate->shift;
1795                 BLI_assert(axis_idx != -1);
1796                 const short axis_type = gizmo_get_axis_type(axis_idx);
1797                 if (axis_type != MAN_AXES_ROTATE) {
1798                         wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, 0);
1799                         PointerRNA *ptr = &gzop->ptr;
1800                         PropertyRNA *prop_constraint_axis = RNA_struct_find_property(ptr, "constraint_axis");
1801                         if (prop_constraint_axis) {
1802                                 bool constraint[3] = {false};
1803                                 constraint[axis] = true;
1804                                 if (flip) {
1805                                         for (int i = 0; i < ARRAY_SIZE(constraint); i++) {
1806                                                 constraint[i] = !constraint[i];
1807                                         }
1808                                 }
1809                                 RNA_property_boolean_set_array(ptr, prop_constraint_axis, constraint);
1810                         }
1811                 }
1812         }
1813 }
1814
1815 static bool WIDGETGROUP_gizmo_poll(const struct bContext *C, struct wmGizmoGroupType *gzgt)
1816 {
1817         if (!ED_gizmo_poll_or_unlink_delayed_from_tool(C, gzgt)) {
1818                 return false;
1819         }
1820         View3D *v3d = CTX_wm_view3d(C);
1821         if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_TOOL)) {
1822                 return false;
1823         }
1824         return true;
1825 }
1826
1827 void TRANSFORM_GGT_gizmo(wmGizmoGroupType *gzgt)
1828 {
1829         gzgt->name = "Transform Gizmo";
1830         gzgt->idname = "TRANSFORM_GGT_gizmo";
1831
1832         gzgt->flag |= WM_GIZMOGROUPTYPE_3D;
1833
1834         gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
1835         gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
1836
1837         gzgt->poll = WIDGETGROUP_gizmo_poll;
1838         gzgt->setup = WIDGETGROUP_gizmo_setup;
1839         gzgt->refresh = WIDGETGROUP_gizmo_refresh;
1840         gzgt->message_subscribe = WIDGETGROUP_gizmo_message_subscribe;
1841         gzgt->draw_prepare = WIDGETGROUP_gizmo_draw_prepare;
1842         gzgt->invoke_prepare = WIDGETGROUP_gizmo_invoke_prepare;
1843
1844         static const EnumPropertyItem rna_enum_gizmo_items[] = {
1845                 {SCE_GIZMO_SHOW_TRANSLATE, "TRANSLATE", 0, "Move", ""},
1846                 {SCE_GIZMO_SHOW_ROTATE, "ROTATE", 0, "Rotate", ""},
1847                 {SCE_GIZMO_SHOW_SCALE, "SCALE", 0, "Scale", ""},
1848                 {0, "NONE", 0, "None", ""},
1849                 {0, NULL, 0, NULL, NULL}
1850         };
1851         RNA_def_enum(gzgt->srna, "drag_action", rna_enum_gizmo_items, SCE_GIZMO_SHOW_TRANSLATE, "Drag Action", "");
1852 }
1853
1854 /** \} */
1855
1856
1857 /* -------------------------------------------------------------------- */
1858 /** \name Scale Cage Gizmo
1859  * \{ */
1860
1861 struct XFormCageWidgetGroup {
1862         wmGizmo *gizmo;
1863         /* Only for view orientation. */
1864         struct {
1865                 float viewinv_m3[3][3];
1866         } prev;
1867 };
1868
1869 static bool WIDGETGROUP_xform_cage_poll(const bContext *C, wmGizmoGroupType *gzgt)
1870 {
1871         if (!ED_gizmo_poll_or_unlink_delayed_from_tool(C, gzgt)) {
1872                 return false;
1873         }
1874         View3D *v3d = CTX_wm_view3d(C);
1875         if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_TOOL)) {
1876                 return false;
1877         }
1878         return true;
1879 }
1880
1881 static void WIDGETGROUP_xform_cage_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
1882 {
1883         struct XFormCageWidgetGroup *xgzgroup = MEM_mallocN(sizeof(struct XFormCageWidgetGroup), __func__);
1884         const wmGizmoType *gzt_cage = WM_gizmotype_find("GIZMO_GT_cage_3d", true);
1885         xgzgroup->gizmo = WM_gizmo_new_ptr(gzt_cage, gzgroup, NULL);
1886         wmGizmo *gz = xgzgroup->gizmo;
1887
1888         RNA_enum_set(gz->ptr, "transform",
1889                      ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE |
1890                      ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE);
1891
1892         gz->color[0] = 1;
1893         gz->color_hi[0] = 1;
1894
1895         gzgroup->customdata = xgzgroup;
1896
1897         {
1898                 wmOperatorType *ot_resize = WM_operatortype_find("TRANSFORM_OT_resize", true);
1899                 PointerRNA *ptr;
1900
1901                 /* assign operator */
1902                 PropertyRNA *prop_release_confirm = NULL;
1903                 PropertyRNA *prop_constraint_axis = NULL;
1904
1905                 int i = ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z;
1906                 for (int x = 0; x < 3; x++) {
1907                         for (int y = 0; y < 3; y++) {
1908                                 for (int z = 0; z < 3; z++) {
1909                                         bool constraint[3] = {x != 1, y != 1, z != 1};
1910                                         ptr = WM_gizmo_operator_set(gz, i, ot_resize, NULL);
1911                                         if (prop_release_confirm == NULL) {
1912                                                 prop_release_confirm = RNA_struct_find_property(ptr, "release_confirm");
1913                                                 prop_constraint_axis = RNA_struct_find_property(ptr, "constraint_axis");
1914                                         }
1915                                         RNA_property_boolean_set(ptr, prop_release_confirm, true);
1916                                         RNA_property_boolean_set_array(ptr, prop_constraint_axis, constraint);
1917                                         i++;
1918                                 }
1919                         }
1920                 }
1921         }
1922 }
1923
1924 static void WIDGETGROUP_xform_cage_refresh(const bContext *C, wmGizmoGroup *gzgroup)
1925 {
1926         ARegion *ar = CTX_wm_region(C);
1927         RegionView3D *rv3d = ar->regiondata;
1928
1929         struct XFormCageWidgetGroup *xgzgroup = gzgroup->customdata;
1930         wmGizmo *gz = xgzgroup->gizmo;
1931
1932         struct TransformBounds tbounds;
1933
1934         if ((ED_transform_calc_gizmo_stats(
1935                      C, &(struct TransformCalcParams) {
1936                          .use_local_axis = true,
1937                      }, &tbounds) == 0) ||
1938             equals_v3v3(rv3d->tw_axis_min, rv3d->tw_axis_max))
1939         {
1940                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, true);
1941         }
1942         else {
1943                 gizmo_prepare_mat(C, rv3d, &tbounds);
1944
1945                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
1946                 WM_gizmo_set_flag(gz, WM_GIZMO_MOVE_CURSOR, true);
1947
1948                 float dims[3];
1949                 sub_v3_v3v3(dims, rv3d->tw_axis_max, rv3d->tw_axis_min);
1950                 RNA_float_set_array(gz->ptr, "dimensions", dims);
1951                 mul_v3_fl(dims, 0.5f);
1952
1953                 copy_m4_m3(gz->matrix_offset, rv3d->tw_axis_matrix);
1954                 mid_v3_v3v3(gz->matrix_offset[3], rv3d->tw_axis_max, rv3d->tw_axis_min);
1955                 mul_m3_v3(rv3d->tw_axis_matrix, gz->matrix_offset[3]);
1956
1957                 float matrix_offset_global[4][4];
1958                 mul_m4_m4m4(matrix_offset_global, gz->matrix_space, gz->matrix_offset);
1959
1960                 PropertyRNA *prop_center_override = NULL;
1961                 float center[3];
1962                 float center_global[3];
1963                 int i = ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z;
1964                 for (int x = 0; x < 3; x++) {
1965                         center[0] = (float)(1 - x) * dims[0];
1966                         for (int y = 0; y < 3; y++) {
1967                                 center[1] = (float)(1 - y) * dims[1];
1968                                 for (int z = 0; z < 3; z++) {
1969                                         center[2] = (float)(1 - z) * dims[2];
1970                                         struct wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, i);
1971                                         if (prop_center_override == NULL) {
1972                                                 prop_center_override = RNA_struct_find_property(&gzop->ptr, "center_override");
1973                                         }
1974                                         mul_v3_m4v3(center_global, matrix_offset_global, center);
1975                                         RNA_property_float_set_array(&gzop->ptr, prop_center_override, center_global);
1976                                         i++;
1977                                 }
1978                         }
1979                 }
1980         }
1981
1982         /* Needed to test view orientation changes. */
1983         copy_m3_m4(xgzgroup->prev.viewinv_m3, rv3d->viewinv);
1984 }
1985
1986 static void WIDGETGROUP_xform_cage_message_subscribe(
1987         const bContext *C, wmGizmoGroup *gzgroup, struct wmMsgBus *mbus)
1988 {
1989         Scene *scene = CTX_data_scene(C);
1990         bScreen *screen = CTX_wm_screen(C);
1991         ScrArea *sa = CTX_wm_area(C);
1992         ARegion *ar = CTX_wm_region(C);
1993         gizmo_xform_message_subscribe(gzgroup, mbus, scene, screen, sa, ar, VIEW3D_GGT_xform_cage);
1994 }
1995
1996 static void WIDGETGROUP_xform_cage_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
1997 {
1998         struct XFormCageWidgetGroup *xgzgroup = gzgroup->customdata;
1999         wmGizmo *gz = xgzgroup->gizmo;
2000         ViewLayer *view_layer = CTX_data_view_layer(C);
2001         Object *ob = OBACT(view_layer);
2002         if (ob && ob->mode & OB_MODE_EDIT) {
2003                 copy_m4_m4(gz->matrix_space, ob->obmat);
2004         }
2005         else {
2006                 unit_m4(gz->matrix_space);
2007         }
2008
2009         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2010         {
2011                 Scene *scene = CTX_data_scene(C);
2012                 switch (scene->orientation_type) {
2013                         case V3D_MANIP_VIEW:
2014                         {
2015                                 float viewinv_m3[3][3];
2016                                 copy_m3_m4(viewinv_m3, rv3d->viewinv);
2017                                 if (!equals_m3m3(viewinv_m3, xgzgroup->prev.viewinv_m3)) {
2018                                         /* Take care calling refresh from draw_prepare,
2019                                          * this should be OK because it's only adjusting the cage orientation. */
2020                                         WIDGETGROUP_xform_cage_refresh(C, gzgroup);
2021                                 }
2022                                 break;
2023                         }
2024                 }
2025         }
2026 }
2027
2028 void VIEW3D_GGT_xform_cage(wmGizmoGroupType *gzgt)
2029 {
2030         gzgt->name = "Transform Cage";
2031         gzgt->idname = "VIEW3D_GGT_xform_cage";
2032
2033         gzgt->flag |= WM_GIZMOGROUPTYPE_3D;
2034
2035         gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
2036         gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
2037
2038         gzgt->poll = WIDGETGROUP_xform_cage_poll;
2039         gzgt->setup = WIDGETGROUP_xform_cage_setup;
2040         gzgt->refresh = WIDGETGROUP_xform_cage_refresh;
2041         gzgt->message_subscribe = WIDGETGROUP_xform_cage_message_subscribe;
2042         gzgt->draw_prepare = WIDGETGROUP_xform_cage_draw_prepare;
2043 }
2044
2045 /** \} */
2046
2047 /* -------------------------------------------------------------------- */
2048 /** \name Transform Shear Gizmo
2049  * \{ */
2050
2051 struct XFormShearWidgetGroup {
2052         wmGizmo *gizmo[3][2];
2053         /* Only for view orientation. */
2054         struct {
2055                 float viewinv_m3[3][3];
2056         } prev;
2057 };
2058
2059 static bool WIDGETGROUP_xform_shear_poll(const bContext *C, wmGizmoGroupType *gzgt)
2060 {
2061         if (!ED_gizmo_poll_or_unlink_delayed_from_tool(C, gzgt)) {
2062                 return false;
2063         }
2064         View3D *v3d = CTX_wm_view3d(C);
2065         if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_TOOL)) {
2066                 return false;
2067         }
2068         return true;
2069 }
2070
2071 static void WIDGETGROUP_xform_shear_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
2072 {
2073         struct XFormShearWidgetGroup *xgzgroup = MEM_mallocN(sizeof(struct XFormShearWidgetGroup), __func__);
2074         const wmGizmoType *gzt_arrow = WM_gizmotype_find("GIZMO_GT_arrow_3d", true);
2075         wmOperatorType *ot_shear = WM_operatortype_find("TRANSFORM_OT_shear", true);
2076
2077         float axis_color[3][3];
2078         for (int i = 0; i < 3; i++) {
2079                 UI_GetThemeColor3fv(TH_AXIS_X + i, axis_color[i]);
2080         }
2081
2082         for (int i = 0; i < 3; i++) {
2083                 for (int j = 0; j < 2; j++) {
2084                         wmGizmo *gz = WM_gizmo_new_ptr(gzt_arrow, gzgroup, NULL);
2085                         RNA_enum_set(gz->ptr, "draw_style", ED_GIZMO_ARROW_STYLE_BOX);
2086                         const int i_ortho_a = (i + j + 1) % 3;
2087                         const int i_ortho_b = (i + (1 - j) + 1) % 3;
2088                         interp_v3_v3v3(gz->color, axis_color[i_ortho_a], axis_color[i_ortho_b], 0.75f);
2089                         gz->color[3] = 0.5f;
2090                         PointerRNA *ptr = WM_gizmo_operator_set(gz, 0, ot_shear, NULL);
2091                         RNA_enum_set(ptr, "shear_axis", 0);
2092                         RNA_boolean_set(ptr, "release_confirm", 1);
2093                         xgzgroup->gizmo[i][j] = gz;
2094                 }
2095         }
2096
2097         gzgroup->customdata = xgzgroup;
2098 }
2099
2100 static void WIDGETGROUP_xform_shear_refresh(const bContext *C, wmGizmoGroup *gzgroup)
2101 {
2102         ARegion *ar = CTX_wm_region(C);
2103         RegionView3D *rv3d = ar->regiondata;
2104
2105         struct XFormShearWidgetGroup *xgzgroup = gzgroup->customdata;
2106         struct TransformBounds tbounds;
2107
2108         if (ED_transform_calc_gizmo_stats(
2109                     C, &(struct TransformCalcParams) {
2110                         .use_local_axis = false,
2111                     }, &tbounds) == 0)
2112         {
2113                 for (int i = 0; i < 3; i++) {
2114                         for (int j = 0; j < 2; j++) {
2115                                 wmGizmo *gz = xgzgroup->gizmo[i][j];
2116                                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, true);
2117                         }
2118                 }
2119         }
2120         else {
2121                 gizmo_prepare_mat(C, rv3d, &tbounds);
2122                 for (int i = 0; i < 3; i++) {
2123                         for (int j = 0; j < 2; j++) {
2124                                 wmGizmo *gz = xgzgroup->gizmo[i][j];
2125                                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
2126                                 WM_gizmo_set_flag(gz, WM_GIZMO_MOVE_CURSOR, true);
2127
2128                                 wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, 0);
2129                                 const int i_ortho_a = (i + j + 1) % 3;
2130                                 const int i_ortho_b = (i + (1 - j) + 1) % 3;
2131                                 WM_gizmo_set_matrix_rotation_from_yz_axis(gz, rv3d->twmat[i_ortho_a], rv3d->twmat[i]);
2132                                 WM_gizmo_set_matrix_location(gz, rv3d->twmat[3]);
2133
2134                                 float axis[3];
2135                                 if (j == 0) {
2136                                         copy_v3_v3(axis, tbounds.axis[i_ortho_b]);
2137                                 }
2138                                 else {
2139                                         negate_v3_v3(axis, tbounds.axis[i_ortho_b]);
2140                                 }
2141                                 RNA_float_set_array(&gzop->ptr, "axis", axis);
2142                                 RNA_float_set_array(&gzop->ptr, "axis_ortho", tbounds.axis[i_ortho_a]);
2143                                 mul_v3_fl(gz->matrix_basis[0], 0.5f);
2144                                 mul_v3_fl(gz->matrix_basis[1], 6.0f);
2145                         }
2146                 }
2147         }
2148
2149         /* Needed to test view orientation changes. */
2150         copy_m3_m4(xgzgroup->prev.viewinv_m3, rv3d->viewinv);
2151 }
2152
2153 static void WIDGETGROUP_xform_shear_message_subscribe(
2154         const bContext *C, wmGizmoGroup *gzgroup, struct wmMsgBus *mbus)
2155 {
2156         Scene *scene = CTX_data_scene(C);
2157         bScreen *screen = CTX_wm_screen(C);
2158         ScrArea *sa = CTX_wm_area(C);
2159         ARegion *ar = CTX_wm_region(C);
2160         gizmo_xform_message_subscribe(gzgroup, mbus, scene, screen, sa, ar, VIEW3D_GGT_xform_shear);
2161 }
2162
2163 static void WIDGETGROUP_xform_shear_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
2164 {
2165         struct XFormShearWidgetGroup *xgzgroup = gzgroup->customdata;
2166         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2167         {
2168                 Scene *scene = CTX_data_scene(C);
2169                 switch (scene->orientation_type) {
2170                         case V3D_MANIP_VIEW:
2171                         {
2172                                 float viewinv_m3[3][3];
2173                                 copy_m3_m4(viewinv_m3, rv3d->viewinv);
2174                                 if (!equals_m3m3(viewinv_m3, xgzgroup->prev.viewinv_m3)) {
2175                                         /* Take care calling refresh from draw_prepare,
2176                                          * this should be OK because it's only adjusting the cage orientation. */
2177                                         WIDGETGROUP_xform_shear_refresh(C, gzgroup);
2178                                 }
2179                                 break;
2180                         }
2181                 }
2182         }
2183
2184         /* Basic ordering for drawing only. */
2185         {
2186                 LISTBASE_FOREACH (wmGizmo *, gz, &gzgroup->gizmos) {
2187                         /* Since we have two pairs of each axis,
2188                          * bias the values so gizmos that are orthogonal to the view get priority.
2189                          * This means we never default to shearing along the view axis in the case of an overlap. */
2190                         float axis_order[3], axis_bias[3];
2191                         copy_v3_v3(axis_order, gz->matrix_basis[2]);
2192                         copy_v3_v3(axis_bias, gz->matrix_basis[1]);
2193                         if (dot_v3v3(axis_bias, rv3d->viewinv[2]) < 0.0f) {
2194                                 negate_v3(axis_bias);
2195                         }
2196                         madd_v3_v3fl(axis_order, axis_bias, 0.01f);
2197                         gz->temp.f = dot_v3v3(rv3d->viewinv[2], axis_order);
2198                 }
2199                 BLI_listbase_sort(&gzgroup->gizmos, WM_gizmo_cmp_temp_fl_reverse);
2200         }
2201 }
2202
2203 void VIEW3D_GGT_xform_shear(wmGizmoGroupType *gzgt)
2204 {
2205         gzgt->name = "Transform Shear";
2206         gzgt->idname = "VIEW3D_GGT_xform_shear";
2207
2208         gzgt->flag |= WM_GIZMOGROUPTYPE_3D;
2209
2210         gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
2211         gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
2212
2213         gzgt->poll = WIDGETGROUP_xform_shear_poll;
2214         gzgt->setup = WIDGETGROUP_xform_shear_setup;
2215         gzgt->refresh = WIDGETGROUP_xform_shear_refresh;
2216         gzgt->message_subscribe = WIDGETGROUP_xform_shear_message_subscribe;
2217         gzgt->draw_prepare = WIDGETGROUP_xform_shear_draw_prepare;
2218 }
2219
2220 /** \} */