Gizmo: multi-object pose-mode gizmo positioning
[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_iter->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(tbounds, pchan->pose_head);
1035                                                 calc_tw_center_with_matrix(tbounds, pchan->pose_head, use_mat_local, mat_local);
1036                                                 protectflag_to_drawflags_pchan(rv3d, pchan);
1037                                         }
1038                                 }
1039                                 totsel += totsel_iter;
1040                         }
1041                 }
1042                 MEM_freeN(objects);
1043
1044                 if (totsel) {
1045                         mul_v3_fl(tbounds->center, 1.0f / (float)totsel);   // centroid!
1046                         mul_m4_v3(ob->obmat, tbounds->center);
1047                         mul_m4_v3(ob->obmat, tbounds->min);
1048                         mul_m4_v3(ob->obmat, tbounds->max);
1049                 }
1050         }
1051         else if (ob && (ob->mode & OB_MODE_ALL_PAINT)) {
1052                 /* pass */
1053         }
1054         else if (ob && ob->mode & OB_MODE_PARTICLE_EDIT) {
1055                 PTCacheEdit *edit = PE_get_current(scene, ob);
1056                 PTCacheEditPoint *point;
1057                 PTCacheEditKey *ek;
1058                 int k;
1059
1060                 if (edit) {
1061                         point = edit->points;
1062                         for (a = 0; a < edit->totpoint; a++, point++) {
1063                                 if (point->flag & PEP_HIDE) continue;
1064
1065                                 for (k = 0, ek = point->keys; k < point->totkey; k++, ek++) {
1066                                         if (ek->flag & PEK_SELECT) {
1067                                                 calc_tw_center(tbounds, (ek->flag & PEK_USE_WCO) ? ek->world_co : ek->co);
1068                                                 totsel++;
1069                                         }
1070                                 }
1071                         }
1072
1073                         /* selection center */
1074                         if (totsel)
1075                                 mul_v3_fl(tbounds->center, 1.0f / (float)totsel);  // centroid!
1076                 }
1077         }
1078         else {
1079
1080                 /* we need the one selected object, if its not active */
1081                 base = BASACT(view_layer);
1082                 ob = OBACT(view_layer);
1083                 if (base && ((base->flag & BASE_SELECTED) == 0)) ob = NULL;
1084
1085                 for (base = view_layer->object_bases.first; base; base = base->next) {
1086                         if (!TESTBASELIB(v3d, base)) {
1087                                 continue;
1088                         }
1089                         if (ob == NULL) {
1090                                 ob = base->object;
1091                         }
1092
1093                         /* Get the boundbox out of the evaluated object. */
1094                         const BoundBox *bb = NULL;
1095                         if (params->use_only_center == false) {
1096                                 bb = BKE_object_boundbox_get(base->object);
1097                         }
1098
1099                         if (params->use_only_center || (bb == NULL)) {
1100                                 calc_tw_center(tbounds, base->object->obmat[3]);
1101                         }
1102                         else {
1103                                 for (uint j = 0; j < 8; j++) {
1104                                         float co[3];
1105                                         mul_v3_m4v3(co, base->object->obmat, bb->vec[j]);
1106                                         calc_tw_center(tbounds, co);
1107                                 }
1108                         }
1109                         protectflag_to_drawflags(base->object->protectflag, &rv3d->twdrawflag);
1110                         totsel++;
1111                 }
1112
1113                 /* selection center */
1114                 if (totsel) {
1115                         mul_v3_fl(tbounds->center, 1.0f / (float)totsel);   // centroid!
1116                 }
1117         }
1118
1119         if (totsel == 0) {
1120                 unit_m4(rv3d->twmat);
1121         }
1122         else {
1123                 copy_v3_v3(rv3d->tw_axis_min, tbounds->axis_min);
1124                 copy_v3_v3(rv3d->tw_axis_max, tbounds->axis_max);
1125                 copy_m3_m3(rv3d->tw_axis_matrix, tbounds->axis);
1126         }
1127
1128         return totsel;
1129 }
1130
1131 static void gizmo_get_idot(RegionView3D *rv3d, float r_idot[3])
1132 {
1133         float view_vec[3], axis_vec[3];
1134         ED_view3d_global_to_vector(rv3d, rv3d->twmat[3], view_vec);
1135         for (int i = 0; i < 3; i++) {
1136                 normalize_v3_v3(axis_vec, rv3d->twmat[i]);
1137                 r_idot[i] = 1.0f - fabsf(dot_v3v3(view_vec, axis_vec));
1138         }
1139 }
1140
1141 static void gizmo_prepare_mat(
1142         const bContext *C, RegionView3D *rv3d, const struct TransformBounds *tbounds)
1143 {
1144         Scene *scene = CTX_data_scene(C);
1145         ViewLayer *view_layer = CTX_data_view_layer(C);
1146
1147         switch (scene->toolsettings->transform_pivot_point) {
1148                 case V3D_AROUND_CENTER_BOUNDS:
1149                 case V3D_AROUND_ACTIVE:
1150                 {
1151                         mid_v3_v3v3(rv3d->twmat[3], tbounds->min, tbounds->max);
1152
1153                         if (scene->toolsettings->transform_pivot_point == V3D_AROUND_ACTIVE) {
1154                                 bGPdata *gpd = CTX_data_gpencil_data(C);
1155                                 Object *ob = OBACT(view_layer);
1156                                 if (gpd && (gpd->flag & GP_DATA_STROKE_EDITMODE)) {
1157                                         /* pass */
1158                                 }
1159                                 else if (ob != NULL) {
1160                                         ED_object_calc_active_center(ob, false, rv3d->twmat[3]);
1161                                 }
1162                         }
1163                         break;
1164                 }
1165                 case V3D_AROUND_LOCAL_ORIGINS:
1166                 case V3D_AROUND_CENTER_MEAN:
1167                         copy_v3_v3(rv3d->twmat[3], tbounds->center);
1168                         break;
1169                 case V3D_AROUND_CURSOR:
1170                         copy_v3_v3(rv3d->twmat[3], scene->cursor.location);
1171                         break;
1172         }
1173 }
1174
1175 /**
1176  * Sets up \a r_start and \a r_len to define arrow line range.
1177  * Needed to adjust line drawing for combined gizmo axis types.
1178  */
1179 static void gizmo_line_range(const int twtype, const short axis_type, float *r_start, float *r_len)
1180 {
1181         const float ofs = 0.2f;
1182
1183         *r_start = 0.2f;
1184         *r_len = 1.0f;
1185
1186         switch (axis_type) {
1187                 case MAN_AXES_TRANSLATE:
1188                         if (twtype & SCE_GIZMO_SHOW_SCALE) {
1189                                 *r_start = *r_len - ofs + 0.075f;
1190                         }
1191                         if (twtype & SCE_GIZMO_SHOW_ROTATE) {
1192                                 *r_len += ofs;
1193                         }
1194                         break;
1195                 case MAN_AXES_SCALE:
1196                         if (twtype & (SCE_GIZMO_SHOW_TRANSLATE | SCE_GIZMO_SHOW_ROTATE)) {
1197                                 *r_len -= ofs + 0.025f;
1198                         }
1199                         break;
1200         }
1201
1202         *r_len -= *r_start;
1203 }
1204
1205 static void gizmo_xform_message_subscribe(
1206         wmGizmoGroup *gzgroup, struct wmMsgBus *mbus,
1207         Scene *scene, bScreen *UNUSED(screen), ScrArea *UNUSED(sa), ARegion *ar, const void *type_fn)
1208 {
1209         /* Subscribe to view properties */
1210         wmMsgSubscribeValue msg_sub_value_gz_tag_refresh = {
1211                 .owner = ar,
1212                 .user_data = gzgroup->parent_gzmap,
1213                 .notify = WM_gizmo_do_msg_notify_tag_refresh,
1214         };
1215
1216         PointerRNA scene_ptr;
1217         RNA_id_pointer_create(&scene->id, &scene_ptr);
1218
1219         {
1220                 extern PropertyRNA rna_Scene_transform_orientation;
1221                 extern PropertyRNA rna_Scene_cursor_location;
1222                 const PropertyRNA *props[] = {
1223                         &rna_Scene_transform_orientation,
1224                         (scene->toolsettings->transform_pivot_point == V3D_AROUND_CURSOR) ? &rna_Scene_cursor_location : NULL,
1225                 };
1226                 for (int i = 0; i < ARRAY_SIZE(props); i++) {
1227                         if (props[i]) {
1228                                 WM_msg_subscribe_rna(mbus, &scene_ptr, props[i], &msg_sub_value_gz_tag_refresh, __func__);
1229                         }
1230                 }
1231         }
1232
1233         PointerRNA toolsettings_ptr;
1234         RNA_pointer_create(&scene->id, &RNA_ToolSettings, scene->toolsettings, &toolsettings_ptr);
1235
1236         if (type_fn == TRANSFORM_GGT_gizmo) {
1237                 extern PropertyRNA rna_ToolSettings_transform_pivot_point;
1238                 extern PropertyRNA rna_ToolSettings_use_gizmo_mode;
1239                 const PropertyRNA *props[] = {
1240                         &rna_ToolSettings_transform_pivot_point,
1241                         &rna_ToolSettings_use_gizmo_mode,
1242                 };
1243                 for (int i = 0; i < ARRAY_SIZE(props); i++) {
1244                         WM_msg_subscribe_rna(mbus, &toolsettings_ptr, props[i], &msg_sub_value_gz_tag_refresh, __func__);
1245                 }
1246         }
1247         else if (type_fn == VIEW3D_GGT_xform_cage) {
1248                 /* pass */
1249         }
1250         else if (type_fn == VIEW3D_GGT_xform_shear) {
1251                 /* pass */
1252         }
1253         else {
1254                 BLI_assert(0);
1255         }
1256
1257         WM_msg_subscribe_rna_anon_prop(mbus, Window, view_layer, &msg_sub_value_gz_tag_refresh);
1258 }
1259
1260
1261 void drawDial3d(const TransInfo *t)
1262 {
1263         if (t->mode == TFM_ROTATION && t->spacetype == SPACE_VIEW3D) {
1264                 wmGizmo *gz = wm_gizmomap_highlight_get(t->ar->gizmo_map);
1265                 if (gz == NULL) {
1266                         /* We only draw Dial3d if the operator has been called by a gizmo. */
1267                         return;
1268                 }
1269
1270                 float mat_basis[4][4];
1271                 float mat_final[4][4];
1272                 float color[4];
1273                 float increment;
1274                 float line_with = GIZMO_AXIS_LINE_WIDTH + 1.0f;
1275                 float scale = UI_DPI_FAC * U.gizmo_size;
1276
1277                 int axis_idx;
1278
1279                 const TransCon *tc = &(t->con);
1280                 if (tc->mode & CON_APPLY) {
1281                         if (tc->mode & CON_AXIS0) {
1282                                 axis_idx = MAN_AXIS_ROT_X;
1283                                 negate_v3_v3(mat_basis[2], tc->mtx[0]);
1284                         }
1285                         else if (tc->mode &  CON_AXIS1) {
1286                                 axis_idx = MAN_AXIS_ROT_Y;
1287                                 negate_v3_v3(mat_basis[2], tc->mtx[1]);
1288                         }
1289                         else {
1290                                 BLI_assert((tc->mode & CON_AXIS2) != 0);
1291                                 axis_idx = MAN_AXIS_ROT_Z;
1292                                 negate_v3_v3(mat_basis[2], tc->mtx[2]);
1293                         }
1294                 }
1295                 else {
1296                         axis_idx = MAN_AXIS_ROT_C;
1297                         negate_v3_v3(mat_basis[2], t->axis);
1298                         scale *= 1.2f;
1299                         line_with -= 1.0f;
1300                 }
1301
1302                 copy_v3_v3(mat_basis[3], t->center_global);
1303                 mat_basis[2][3] = -dot_v3v3(mat_basis[2], mat_basis[3]);
1304
1305                 if (ED_view3d_win_to_3d_on_plane(
1306                         t->ar, mat_basis[2], (float[2]){UNPACK2(t->mouse.imval)},
1307                         false, mat_basis[1]))
1308                 {
1309                         sub_v3_v3(mat_basis[1], mat_basis[3]);
1310                         normalize_v3(mat_basis[1]);
1311                         cross_v3_v3v3(mat_basis[0], mat_basis[1], mat_basis[2]);
1312                 }
1313                 else {
1314                         /* The plane and the mouse direction are parallel.
1315                          * Calculate a matrix orthogonal to the axis. */
1316                         ortho_basis_v3v3_v3(mat_basis[0], mat_basis[1], mat_basis[2]);
1317                 }
1318
1319                 mat_basis[0][3] = 0.0f;
1320                 mat_basis[1][3] = 0.0f;
1321                 mat_basis[2][3] = 0.0f;
1322                 mat_basis[3][3] = 1.0f;
1323
1324                 copy_m4_m4(mat_final, mat_basis);
1325                 scale *= ED_view3d_pixel_size_no_ui_scale(t->ar->regiondata, mat_final[3]);
1326                 mul_mat3_m4_fl(mat_final, scale);
1327
1328                 if ((t->tsnap.mode & (SCE_SNAP_MODE_INCREMENT | SCE_SNAP_MODE_GRID)) &&
1329                     activeSnap(t))
1330                 {
1331                         increment = (t->modifiers & MOD_PRECISION) ? t->snap[2] : t->snap[1];
1332                 }
1333                 else {
1334                         increment = t->snap[0];
1335                 }
1336
1337                 BLI_assert(axis_idx >= MAN_AXIS_RANGE_ROT_START && axis_idx < MAN_AXIS_RANGE_ROT_END);
1338                 gizmo_get_axis_color(axis_idx, NULL, color, color);
1339
1340                 GPU_depth_test(false);
1341                 GPU_blend(true);
1342                 GPU_line_smooth(true);
1343
1344                 ED_gizmotypes_dial_3d_draw_util(
1345                         mat_basis, mat_final, line_with, color,
1346                         &(struct Dial3dParams){
1347                             .draw_options = ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_VALUE,
1348                             .angle_delta = t->values[0],
1349                             .angle_increment = increment,
1350                         });
1351
1352                 GPU_line_smooth(false);
1353                 GPU_depth_test(true);
1354                 GPU_blend(false);
1355         }
1356 }
1357
1358 /** \} */
1359
1360
1361 /* -------------------------------------------------------------------- */
1362 /** \name Transform Gizmo
1363  * \{ */
1364
1365 static GizmoGroup *gizmogroup_init(wmGizmoGroup *gzgroup)
1366 {
1367         GizmoGroup *ggd;
1368
1369         ggd = MEM_callocN(sizeof(GizmoGroup), "gizmo_data");
1370
1371         const wmGizmoType *gzt_arrow = WM_gizmotype_find("GIZMO_GT_arrow_3d", true);
1372         const wmGizmoType *gzt_dial = WM_gizmotype_find("GIZMO_GT_dial_3d", true);
1373         const wmGizmoType *gzt_prim = WM_gizmotype_find("GIZMO_GT_primitive_3d", true);
1374
1375 #define GIZMO_NEW_ARROW(v, draw_style) { \
1376         ggd->gizmos[v] = WM_gizmo_new_ptr(gzt_arrow, gzgroup, NULL); \
1377         RNA_enum_set(ggd->gizmos[v]->ptr, "draw_style", draw_style); \
1378 } ((void)0)
1379 #define GIZMO_NEW_DIAL(v, draw_options) { \
1380         ggd->gizmos[v] = WM_gizmo_new_ptr(gzt_dial, gzgroup, NULL); \
1381         RNA_enum_set(ggd->gizmos[v]->ptr, "draw_options", draw_options); \
1382 } ((void)0)
1383 #define GIZMO_NEW_PRIM(v, draw_style) { \
1384         ggd->gizmos[v] = WM_gizmo_new_ptr(gzt_prim, gzgroup, NULL); \
1385         RNA_enum_set(ggd->gizmos[v]->ptr, "draw_style", draw_style); \
1386 } ((void)0)
1387
1388         /* add/init widgets - order matters! */
1389         GIZMO_NEW_DIAL(MAN_AXIS_ROT_T, ED_GIZMO_DIAL_DRAW_FLAG_FILL);
1390
1391         GIZMO_NEW_DIAL(MAN_AXIS_SCALE_C, ED_GIZMO_DIAL_DRAW_FLAG_NOP);
1392
1393         GIZMO_NEW_ARROW(MAN_AXIS_SCALE_X, ED_GIZMO_ARROW_STYLE_BOX);
1394         GIZMO_NEW_ARROW(MAN_AXIS_SCALE_Y, ED_GIZMO_ARROW_STYLE_BOX);
1395         GIZMO_NEW_ARROW(MAN_AXIS_SCALE_Z, ED_GIZMO_ARROW_STYLE_BOX);
1396
1397         GIZMO_NEW_PRIM(MAN_AXIS_SCALE_XY, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1398         GIZMO_NEW_PRIM(MAN_AXIS_SCALE_YZ, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1399         GIZMO_NEW_PRIM(MAN_AXIS_SCALE_ZX, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1400
1401         GIZMO_NEW_DIAL(MAN_AXIS_ROT_X, ED_GIZMO_DIAL_DRAW_FLAG_CLIP);
1402         GIZMO_NEW_DIAL(MAN_AXIS_ROT_Y, ED_GIZMO_DIAL_DRAW_FLAG_CLIP);
1403         GIZMO_NEW_DIAL(MAN_AXIS_ROT_Z, ED_GIZMO_DIAL_DRAW_FLAG_CLIP);
1404
1405         /* init screen aligned widget last here, looks better, behaves better */
1406         GIZMO_NEW_DIAL(MAN_AXIS_ROT_C, ED_GIZMO_DIAL_DRAW_FLAG_NOP);
1407
1408         GIZMO_NEW_DIAL(MAN_AXIS_TRANS_C, ED_GIZMO_DIAL_DRAW_FLAG_NOP);
1409
1410         GIZMO_NEW_ARROW(MAN_AXIS_TRANS_X, ED_GIZMO_ARROW_STYLE_NORMAL);
1411         GIZMO_NEW_ARROW(MAN_AXIS_TRANS_Y, ED_GIZMO_ARROW_STYLE_NORMAL);
1412         GIZMO_NEW_ARROW(MAN_AXIS_TRANS_Z, ED_GIZMO_ARROW_STYLE_NORMAL);
1413
1414         GIZMO_NEW_PRIM(MAN_AXIS_TRANS_XY, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1415         GIZMO_NEW_PRIM(MAN_AXIS_TRANS_YZ, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1416         GIZMO_NEW_PRIM(MAN_AXIS_TRANS_ZX, ED_GIZMO_PRIMITIVE_STYLE_PLANE);
1417
1418         ggd->gizmos[MAN_AXIS_ROT_T]->flag |= WM_GIZMO_SELECT_BACKGROUND;
1419
1420         return ggd;
1421 }
1422
1423 /**
1424  * Custom handler for gizmo widgets
1425  */
1426 static int gizmo_modal(
1427         bContext *C, wmGizmo *widget, const wmEvent *event,
1428         eWM_GizmoFlagTweak UNUSED(tweak_flag))
1429 {
1430         /* Avoid unnecessary updates, partially address: T55458. */
1431         if (ELEM(event->type, TIMER, INBETWEEN_MOUSEMOVE)) {
1432                 return OPERATOR_RUNNING_MODAL;
1433         }
1434
1435         ARegion *ar = CTX_wm_region(C);
1436         RegionView3D *rv3d = ar->regiondata;
1437         struct TransformBounds tbounds;
1438
1439
1440         if (ED_transform_calc_gizmo_stats(
1441                     C, &(struct TransformCalcParams){
1442                         .use_only_center = true,
1443                     }, &tbounds))
1444         {
1445                 gizmo_prepare_mat(C, rv3d, &tbounds);
1446                 WM_gizmo_set_matrix_location(widget, rv3d->twmat[3]);
1447         }
1448
1449         ED_region_tag_redraw(ar);
1450
1451         return OPERATOR_RUNNING_MODAL;
1452 }
1453
1454 static void gizmogroup_init_properties_from_twtype(wmGizmoGroup *gzgroup)
1455 {
1456         struct {
1457                 wmOperatorType *translate, *rotate, *trackball, *resize;
1458         } ot_store = {NULL};
1459         GizmoGroup *ggd = gzgroup->customdata;
1460
1461         MAN_ITER_AXES_BEGIN(axis, axis_idx)
1462         {
1463                 const short axis_type = gizmo_get_axis_type(axis_idx);
1464                 bool constraint_axis[3] = {1, 0, 0};
1465                 PointerRNA *ptr = NULL;
1466
1467                 gizmo_get_axis_constraint(axis_idx, constraint_axis);
1468
1469                 /* custom handler! */
1470                 WM_gizmo_set_fn_custom_modal(axis, gizmo_modal);
1471
1472                 switch (axis_idx) {
1473                         case MAN_AXIS_TRANS_X:
1474                         case MAN_AXIS_TRANS_Y:
1475                         case MAN_AXIS_TRANS_Z:
1476                         case MAN_AXIS_SCALE_X:
1477                         case MAN_AXIS_SCALE_Y:
1478                         case MAN_AXIS_SCALE_Z:
1479                                 if (axis_idx >= MAN_AXIS_RANGE_TRANS_START && axis_idx < MAN_AXIS_RANGE_TRANS_END) {
1480                                         int draw_options = 0;
1481                                         if ((ggd->twtype & (SCE_GIZMO_SHOW_ROTATE | SCE_GIZMO_SHOW_SCALE)) == 0) {
1482                                                 draw_options |= ED_GIZMO_ARROW_DRAW_FLAG_STEM;
1483                                         }
1484                                         RNA_enum_set(axis->ptr, "draw_options", draw_options);
1485                                 }
1486
1487                                 WM_gizmo_set_line_width(axis, GIZMO_AXIS_LINE_WIDTH);
1488                                 break;
1489                         case MAN_AXIS_ROT_X:
1490                         case MAN_AXIS_ROT_Y:
1491                         case MAN_AXIS_ROT_Z:
1492                                 /* increased line width for better display */
1493                                 WM_gizmo_set_line_width(axis, GIZMO_AXIS_LINE_WIDTH + 1.0f);
1494                                 WM_gizmo_set_flag(axis, WM_GIZMO_DRAW_VALUE, true);
1495                                 break;
1496                         case MAN_AXIS_TRANS_XY:
1497                         case MAN_AXIS_TRANS_YZ:
1498                         case MAN_AXIS_TRANS_ZX:
1499                         case MAN_AXIS_SCALE_XY:
1500                         case MAN_AXIS_SCALE_YZ:
1501                         case MAN_AXIS_SCALE_ZX:
1502                         {
1503                                 const float ofs_ax = 7.0f;
1504                                 const float ofs[3] = {ofs_ax, ofs_ax, 0.0f};
1505                                 WM_gizmo_set_scale(axis, 0.07f);
1506                                 WM_gizmo_set_matrix_offset_location(axis, ofs);
1507                                 WM_gizmo_set_flag(axis, WM_GIZMO_DRAW_OFFSET_SCALE, true);
1508                                 break;
1509                         }
1510                         case MAN_AXIS_TRANS_C:
1511                         case MAN_AXIS_ROT_C:
1512                         case MAN_AXIS_SCALE_C:
1513                         case MAN_AXIS_ROT_T:
1514                                 WM_gizmo_set_line_width(axis, GIZMO_AXIS_LINE_WIDTH);
1515                                 if (axis_idx == MAN_AXIS_ROT_T) {
1516                                         WM_gizmo_set_flag(axis, WM_GIZMO_DRAW_HOVER, true);
1517                                 }
1518                                 else if (axis_idx == MAN_AXIS_ROT_C) {
1519                                         WM_gizmo_set_flag(axis, WM_GIZMO_DRAW_VALUE, true);
1520                                         WM_gizmo_set_scale(axis, 1.2f);
1521                                 }
1522                                 else {
1523                                         WM_gizmo_set_scale(axis, 0.2f);
1524                                 }
1525                                 break;
1526                 }
1527
1528                 switch (axis_type) {
1529                         case MAN_AXES_TRANSLATE:
1530                                 if (ot_store.translate == NULL) {
1531                                         ot_store.translate = WM_operatortype_find("TRANSFORM_OT_translate", true);
1532                                 }
1533                                 ptr = WM_gizmo_operator_set(axis, 0, ot_store.translate, NULL);
1534                                 break;
1535                         case MAN_AXES_ROTATE:
1536                         {
1537                                 wmOperatorType *ot_rotate;
1538                                 if (axis_idx == MAN_AXIS_ROT_T) {
1539                                         if (ot_store.trackball == NULL) {
1540                                                 ot_store.trackball = WM_operatortype_find("TRANSFORM_OT_trackball", true);
1541                                         }
1542                                         ot_rotate = ot_store.trackball;
1543                                 }
1544                                 else {
1545                                         if (ot_store.rotate == NULL) {
1546                                                 ot_store.rotate = WM_operatortype_find("TRANSFORM_OT_rotate", true);
1547                                         }
1548                                         ot_rotate = ot_store.rotate;
1549                                 }
1550                                 ptr = WM_gizmo_operator_set(axis, 0, ot_rotate, NULL);
1551                                 break;
1552                         }
1553                         case MAN_AXES_SCALE:
1554                         {
1555                                 if (ot_store.resize == NULL) {
1556                                         ot_store.resize = WM_operatortype_find("TRANSFORM_OT_resize", true);
1557                                 }
1558                                 ptr = WM_gizmo_operator_set(axis, 0, ot_store.resize, NULL);
1559                                 break;
1560                         }
1561                 }
1562
1563                 if (ptr) {
1564                         PropertyRNA *prop;
1565                         if (ELEM(true, UNPACK3(constraint_axis))) {
1566                                 if ((prop = RNA_struct_find_property(ptr, "constraint_axis"))) {
1567                                         RNA_property_boolean_set_array(ptr, prop, constraint_axis);
1568                                 }
1569                         }
1570
1571                         RNA_boolean_set(ptr, "release_confirm", 1);
1572                 }
1573         }
1574         MAN_ITER_AXES_END;
1575 }
1576
1577 static void WIDGETGROUP_gizmo_setup(const bContext *C, wmGizmoGroup *gzgroup)
1578 {
1579         GizmoGroup *ggd = gizmogroup_init(gzgroup);
1580
1581         gzgroup->customdata = ggd;
1582
1583         {
1584                 ggd->twtype = 0;
1585                 ScrArea *sa = CTX_wm_area(C);
1586                 const bToolRef *tref = sa->runtime.tool;
1587
1588                 if (tref == NULL || STREQ(tref->idname, "Transform")) {
1589                         /* Setup all gizmos, they can be toggled via 'ToolSettings.gizmo_flag' */
1590                         ggd->twtype = SCE_GIZMO_SHOW_TRANSLATE | SCE_GIZMO_SHOW_ROTATE | SCE_GIZMO_SHOW_SCALE;
1591                         ggd->use_twtype_refresh = true;
1592                 }
1593                 else if (STREQ(tref->idname, "Move")) {
1594                         ggd->twtype |= SCE_GIZMO_SHOW_TRANSLATE;
1595                 }
1596                 else if (STREQ(tref->idname, "Rotate")) {
1597                         ggd->twtype |= SCE_GIZMO_SHOW_ROTATE;
1598                 }
1599                 else if (STREQ(tref->idname, "Scale")) {
1600                         ggd->twtype |= SCE_GIZMO_SHOW_SCALE;
1601                 }
1602                 BLI_assert(ggd->twtype != 0);
1603                 ggd->twtype_init = ggd->twtype;
1604         }
1605
1606         /* *** set properties for axes *** */
1607         gizmogroup_init_properties_from_twtype(gzgroup);
1608 }
1609
1610 static void WIDGETGROUP_gizmo_refresh(const bContext *C, wmGizmoGroup *gzgroup)
1611 {
1612         GizmoGroup *ggd = gzgroup->customdata;
1613         ARegion *ar = CTX_wm_region(C);
1614         RegionView3D *rv3d = ar->regiondata;
1615         struct TransformBounds tbounds;
1616
1617         if (ggd->use_twtype_refresh) {
1618                 Scene *scene = CTX_data_scene(C);
1619                 ggd->twtype = scene->toolsettings->gizmo_flag & ggd->twtype_init;
1620                 if (ggd->twtype != ggd->twtype_prev) {
1621                         ggd->twtype_prev = ggd->twtype;
1622                         gizmogroup_init_properties_from_twtype(gzgroup);
1623                 }
1624         }
1625
1626         /* skip, we don't draw anything anyway */
1627         if ((ggd->all_hidden =
1628              (ED_transform_calc_gizmo_stats(
1629                      C, &(struct TransformCalcParams){
1630                          .use_only_center = true,
1631                      }, &tbounds) == 0)))
1632         {
1633                 return;
1634         }
1635
1636         gizmo_prepare_mat(C, rv3d, &tbounds);
1637
1638         /* *** set properties for axes *** */
1639
1640         MAN_ITER_AXES_BEGIN(axis, axis_idx)
1641         {
1642                 const short axis_type = gizmo_get_axis_type(axis_idx);
1643                 const int aidx_norm = gizmo_orientation_axis(axis_idx, NULL);
1644
1645                 WM_gizmo_set_matrix_location(axis, rv3d->twmat[3]);
1646
1647                 switch (axis_idx) {
1648                         case MAN_AXIS_TRANS_X:
1649                         case MAN_AXIS_TRANS_Y:
1650                         case MAN_AXIS_TRANS_Z:
1651                         case MAN_AXIS_SCALE_X:
1652                         case MAN_AXIS_SCALE_Y:
1653                         case MAN_AXIS_SCALE_Z:
1654                         {
1655                                 float start_co[3] = {0.0f, 0.0f, 0.0f};
1656                                 float len;
1657
1658                                 gizmo_line_range(ggd->twtype, axis_type, &start_co[2], &len);
1659
1660                                 WM_gizmo_set_matrix_rotation_from_z_axis(axis, rv3d->twmat[aidx_norm]);
1661                                 RNA_float_set(axis->ptr, "length", len);
1662
1663                                 if (axis_idx >= MAN_AXIS_RANGE_TRANS_START && axis_idx < MAN_AXIS_RANGE_TRANS_END) {
1664                                         if (ggd->twtype & SCE_GIZMO_SHOW_ROTATE) {
1665                                                 /* Avoid rotate and translate arrows overlap. */
1666                                                 start_co[2] += 0.215f;
1667                                         }
1668                                 }
1669                                 WM_gizmo_set_matrix_offset_location(axis, start_co);
1670                                 WM_gizmo_set_flag(axis, WM_GIZMO_DRAW_OFFSET_SCALE, true);
1671                                 break;
1672                         }
1673                         case MAN_AXIS_ROT_X:
1674                         case MAN_AXIS_ROT_Y:
1675                         case MAN_AXIS_ROT_Z:
1676                                 WM_gizmo_set_matrix_rotation_from_z_axis(axis, rv3d->twmat[aidx_norm]);
1677                                 break;
1678                         case MAN_AXIS_TRANS_XY:
1679                         case MAN_AXIS_TRANS_YZ:
1680                         case MAN_AXIS_TRANS_ZX:
1681                         case MAN_AXIS_SCALE_XY:
1682                         case MAN_AXIS_SCALE_YZ:
1683                         case MAN_AXIS_SCALE_ZX:
1684                         {
1685                                 const float *y_axis = rv3d->twmat[aidx_norm - 1 < 0 ? 2 : aidx_norm - 1];
1686                                 const float *z_axis = rv3d->twmat[aidx_norm];
1687                                 WM_gizmo_set_matrix_rotation_from_yz_axis(axis, y_axis, z_axis);
1688                                 break;
1689                         }
1690                 }
1691         }
1692         MAN_ITER_AXES_END;
1693 }
1694
1695 static void WIDGETGROUP_gizmo_message_subscribe(
1696         const bContext *C, wmGizmoGroup *gzgroup, struct wmMsgBus *mbus)
1697 {
1698         Scene *scene = CTX_data_scene(C);
1699         bScreen *screen = CTX_wm_screen(C);
1700         ScrArea *sa = CTX_wm_area(C);
1701         ARegion *ar = CTX_wm_region(C);
1702         gizmo_xform_message_subscribe(gzgroup, mbus, scene, screen, sa, ar, TRANSFORM_GGT_gizmo);
1703 }
1704
1705 static void WIDGETGROUP_gizmo_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
1706 {
1707         GizmoGroup *ggd = gzgroup->customdata;
1708         // ScrArea *sa = CTX_wm_area(C);
1709         ARegion *ar = CTX_wm_region(C);
1710         // View3D *v3d = sa->spacedata.first;
1711         RegionView3D *rv3d = ar->regiondata;
1712         float viewinv_m3[3][3];
1713         copy_m3_m4(viewinv_m3, rv3d->viewinv);
1714         float idot[3];
1715
1716         /* when looking through a selected camera, the gizmo can be at the
1717          * exact same position as the view, skip so we don't break selection */
1718         if (ggd->all_hidden || fabsf(ED_view3d_pixel_size(rv3d, rv3d->twmat[3])) < 1e-6f) {
1719                 MAN_ITER_AXES_BEGIN(axis, axis_idx)
1720                 {
1721                         WM_gizmo_set_flag(axis, WM_GIZMO_HIDDEN, true);
1722                 }
1723                 MAN_ITER_AXES_END;
1724                 return;
1725         }
1726         gizmo_get_idot(rv3d, idot);
1727
1728         /* *** set properties for axes *** */
1729         MAN_ITER_AXES_BEGIN(axis, axis_idx) {
1730                 const short axis_type = gizmo_get_axis_type(axis_idx);
1731                 /* XXX maybe unset _HIDDEN flag on redraw? */
1732
1733                 if (gizmo_is_axis_visible(rv3d, ggd->twtype, idot, axis_type, axis_idx)) {
1734                         WM_gizmo_set_flag(axis, WM_GIZMO_HIDDEN, false);
1735                 }
1736                 else {
1737                         WM_gizmo_set_flag(axis, WM_GIZMO_HIDDEN, true);
1738                         continue;
1739                 }
1740
1741                 float color[4], color_hi[4];
1742                 gizmo_get_axis_color(axis_idx, idot, color, color_hi);
1743                 WM_gizmo_set_color(axis, color);
1744                 WM_gizmo_set_color_highlight(axis, color_hi);
1745
1746                 switch (axis_idx) {
1747                         case MAN_AXIS_TRANS_C:
1748                         case MAN_AXIS_ROT_C:
1749                         case MAN_AXIS_SCALE_C:
1750                         case MAN_AXIS_ROT_T:
1751                                 WM_gizmo_set_matrix_rotation_from_z_axis(axis, rv3d->viewinv[2]);
1752                                 break;
1753                 }
1754         } MAN_ITER_AXES_END;
1755
1756         /* Refresh handled above when using view orientation. */
1757         if (!equals_m3m3(viewinv_m3, ggd->prev.viewinv_m3)) {
1758                 {
1759                         Scene *scene = CTX_data_scene(C);
1760                         switch (scene->orientation_type) {
1761                                 case V3D_MANIP_VIEW:
1762                                 {
1763                                         WIDGETGROUP_gizmo_refresh(C, gzgroup);
1764                                         break;
1765                                 }
1766                         }
1767                 }
1768                 copy_m3_m4(ggd->prev.viewinv_m3, rv3d->viewinv);
1769         }
1770
1771 }
1772
1773 static void WIDGETGROUP_gizmo_invoke_prepare(
1774         const bContext *C, wmGizmoGroup *gzgroup, wmGizmo *gz)
1775 {
1776
1777         /* Support shift click to constrain axis. */
1778         GizmoGroup *ggd = gzgroup->customdata;
1779         const int axis_idx = BLI_array_findindex(ggd->gizmos, ARRAY_SIZE(ggd->gizmos), &gz);
1780         int axis = -1;
1781         switch (axis_idx) {
1782                 case MAN_AXIS_TRANS_X:
1783                 case MAN_AXIS_TRANS_Y:
1784                 case MAN_AXIS_TRANS_Z:
1785                         axis = axis_idx - MAN_AXIS_TRANS_X; break;
1786                 case MAN_AXIS_SCALE_X:
1787                 case MAN_AXIS_SCALE_Y:
1788                 case MAN_AXIS_SCALE_Z:
1789                         axis = axis_idx - MAN_AXIS_SCALE_X; break;
1790         }
1791
1792         if (axis != -1) {
1793                 wmWindow *win = CTX_wm_window(C);
1794                 /* Swap single axis for two-axis constraint. */
1795                 bool flip = win->eventstate->shift;
1796                 BLI_assert(axis_idx != -1);
1797                 const short axis_type = gizmo_get_axis_type(axis_idx);
1798                 if (axis_type != MAN_AXES_ROTATE) {
1799                         wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, 0);
1800                         PointerRNA *ptr = &gzop->ptr;
1801                         PropertyRNA *prop_constraint_axis = RNA_struct_find_property(ptr, "constraint_axis");
1802                         if (prop_constraint_axis) {
1803                                 bool constraint[3] = {false};
1804                                 constraint[axis] = true;
1805                                 if (flip) {
1806                                         for (int i = 0; i < ARRAY_SIZE(constraint); i++) {
1807                                                 constraint[i] = !constraint[i];
1808                                         }
1809                                 }
1810                                 RNA_property_boolean_set_array(ptr, prop_constraint_axis, constraint);
1811                         }
1812                 }
1813         }
1814 }
1815
1816 static bool WIDGETGROUP_gizmo_poll(const struct bContext *C, struct wmGizmoGroupType *gzgt)
1817 {
1818         if (!ED_gizmo_poll_or_unlink_delayed_from_tool(C, gzgt)) {
1819                 return false;
1820         }
1821         View3D *v3d = CTX_wm_view3d(C);
1822         if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_TOOL)) {
1823                 return false;
1824         }
1825         return true;
1826 }
1827
1828 void TRANSFORM_GGT_gizmo(wmGizmoGroupType *gzgt)
1829 {
1830         gzgt->name = "Transform Gizmo";
1831         gzgt->idname = "TRANSFORM_GGT_gizmo";
1832
1833         gzgt->flag |= WM_GIZMOGROUPTYPE_3D;
1834
1835         gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
1836         gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
1837
1838         gzgt->poll = WIDGETGROUP_gizmo_poll;
1839         gzgt->setup = WIDGETGROUP_gizmo_setup;
1840         gzgt->refresh = WIDGETGROUP_gizmo_refresh;
1841         gzgt->message_subscribe = WIDGETGROUP_gizmo_message_subscribe;
1842         gzgt->draw_prepare = WIDGETGROUP_gizmo_draw_prepare;
1843         gzgt->invoke_prepare = WIDGETGROUP_gizmo_invoke_prepare;
1844
1845         static const EnumPropertyItem rna_enum_gizmo_items[] = {
1846                 {SCE_GIZMO_SHOW_TRANSLATE, "TRANSLATE", 0, "Move", ""},
1847                 {SCE_GIZMO_SHOW_ROTATE, "ROTATE", 0, "Rotate", ""},
1848                 {SCE_GIZMO_SHOW_SCALE, "SCALE", 0, "Scale", ""},
1849                 {0, "NONE", 0, "None", ""},
1850                 {0, NULL, 0, NULL, NULL}
1851         };
1852         RNA_def_enum(gzgt->srna, "drag_action", rna_enum_gizmo_items, SCE_GIZMO_SHOW_TRANSLATE, "Drag Action", "");
1853 }
1854
1855 /** \} */
1856
1857
1858 /* -------------------------------------------------------------------- */
1859 /** \name Scale Cage Gizmo
1860  * \{ */
1861
1862 struct XFormCageWidgetGroup {
1863         wmGizmo *gizmo;
1864         /* Only for view orientation. */
1865         struct {
1866                 float viewinv_m3[3][3];
1867         } prev;
1868 };
1869
1870 static bool WIDGETGROUP_xform_cage_poll(const bContext *C, wmGizmoGroupType *gzgt)
1871 {
1872         if (!ED_gizmo_poll_or_unlink_delayed_from_tool(C, gzgt)) {
1873                 return false;
1874         }
1875         View3D *v3d = CTX_wm_view3d(C);
1876         if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_TOOL)) {
1877                 return false;
1878         }
1879         return true;
1880 }
1881
1882 static void WIDGETGROUP_xform_cage_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
1883 {
1884         struct XFormCageWidgetGroup *xgzgroup = MEM_mallocN(sizeof(struct XFormCageWidgetGroup), __func__);
1885         const wmGizmoType *gzt_cage = WM_gizmotype_find("GIZMO_GT_cage_3d", true);
1886         xgzgroup->gizmo = WM_gizmo_new_ptr(gzt_cage, gzgroup, NULL);
1887         wmGizmo *gz = xgzgroup->gizmo;
1888
1889         RNA_enum_set(gz->ptr, "transform",
1890                      ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE |
1891                      ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE);
1892
1893         gz->color[0] = 1;
1894         gz->color_hi[0] = 1;
1895
1896         gzgroup->customdata = xgzgroup;
1897
1898         {
1899                 wmOperatorType *ot_resize = WM_operatortype_find("TRANSFORM_OT_resize", true);
1900                 PointerRNA *ptr;
1901
1902                 /* assign operator */
1903                 PropertyRNA *prop_release_confirm = NULL;
1904                 PropertyRNA *prop_constraint_axis = NULL;
1905
1906                 int i = ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z;
1907                 for (int x = 0; x < 3; x++) {
1908                         for (int y = 0; y < 3; y++) {
1909                                 for (int z = 0; z < 3; z++) {
1910                                         bool constraint[3] = {x != 1, y != 1, z != 1};
1911                                         ptr = WM_gizmo_operator_set(gz, i, ot_resize, NULL);
1912                                         if (prop_release_confirm == NULL) {
1913                                                 prop_release_confirm = RNA_struct_find_property(ptr, "release_confirm");
1914                                                 prop_constraint_axis = RNA_struct_find_property(ptr, "constraint_axis");
1915                                         }
1916                                         RNA_property_boolean_set(ptr, prop_release_confirm, true);
1917                                         RNA_property_boolean_set_array(ptr, prop_constraint_axis, constraint);
1918                                         i++;
1919                                 }
1920                         }
1921                 }
1922         }
1923 }
1924
1925 static void WIDGETGROUP_xform_cage_refresh(const bContext *C, wmGizmoGroup *gzgroup)
1926 {
1927         ARegion *ar = CTX_wm_region(C);
1928         RegionView3D *rv3d = ar->regiondata;
1929
1930         struct XFormCageWidgetGroup *xgzgroup = gzgroup->customdata;
1931         wmGizmo *gz = xgzgroup->gizmo;
1932
1933         struct TransformBounds tbounds;
1934
1935         if ((ED_transform_calc_gizmo_stats(
1936                      C, &(struct TransformCalcParams) {
1937                          .use_local_axis = true,
1938                      }, &tbounds) == 0) ||
1939             equals_v3v3(rv3d->tw_axis_min, rv3d->tw_axis_max))
1940         {
1941                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, true);
1942         }
1943         else {
1944                 gizmo_prepare_mat(C, rv3d, &tbounds);
1945
1946                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
1947                 WM_gizmo_set_flag(gz, WM_GIZMO_MOVE_CURSOR, true);
1948
1949                 float dims[3];
1950                 sub_v3_v3v3(dims, rv3d->tw_axis_max, rv3d->tw_axis_min);
1951                 RNA_float_set_array(gz->ptr, "dimensions", dims);
1952                 mul_v3_fl(dims, 0.5f);
1953
1954                 copy_m4_m3(gz->matrix_offset, rv3d->tw_axis_matrix);
1955                 mid_v3_v3v3(gz->matrix_offset[3], rv3d->tw_axis_max, rv3d->tw_axis_min);
1956                 mul_m3_v3(rv3d->tw_axis_matrix, gz->matrix_offset[3]);
1957
1958                 float matrix_offset_global[4][4];
1959                 mul_m4_m4m4(matrix_offset_global, gz->matrix_space, gz->matrix_offset);
1960
1961                 PropertyRNA *prop_center_override = NULL;
1962                 float center[3];
1963                 float center_global[3];
1964                 int i = ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z;
1965                 for (int x = 0; x < 3; x++) {
1966                         center[0] = (float)(1 - x) * dims[0];
1967                         for (int y = 0; y < 3; y++) {
1968                                 center[1] = (float)(1 - y) * dims[1];
1969                                 for (int z = 0; z < 3; z++) {
1970                                         center[2] = (float)(1 - z) * dims[2];
1971                                         struct wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, i);
1972                                         if (prop_center_override == NULL) {
1973                                                 prop_center_override = RNA_struct_find_property(&gzop->ptr, "center_override");
1974                                         }
1975                                         mul_v3_m4v3(center_global, matrix_offset_global, center);
1976                                         RNA_property_float_set_array(&gzop->ptr, prop_center_override, center_global);
1977                                         i++;
1978                                 }
1979                         }
1980                 }
1981         }
1982
1983         /* Needed to test view orientation changes. */
1984         copy_m3_m4(xgzgroup->prev.viewinv_m3, rv3d->viewinv);
1985 }
1986
1987 static void WIDGETGROUP_xform_cage_message_subscribe(
1988         const bContext *C, wmGizmoGroup *gzgroup, struct wmMsgBus *mbus)
1989 {
1990         Scene *scene = CTX_data_scene(C);
1991         bScreen *screen = CTX_wm_screen(C);
1992         ScrArea *sa = CTX_wm_area(C);
1993         ARegion *ar = CTX_wm_region(C);
1994         gizmo_xform_message_subscribe(gzgroup, mbus, scene, screen, sa, ar, VIEW3D_GGT_xform_cage);
1995 }
1996
1997 static void WIDGETGROUP_xform_cage_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
1998 {
1999         struct XFormCageWidgetGroup *xgzgroup = gzgroup->customdata;
2000         wmGizmo *gz = xgzgroup->gizmo;
2001         ViewLayer *view_layer = CTX_data_view_layer(C);
2002         Object *ob = OBACT(view_layer);
2003         if (ob && ob->mode & OB_MODE_EDIT) {
2004                 copy_m4_m4(gz->matrix_space, ob->obmat);
2005         }
2006         else {
2007                 unit_m4(gz->matrix_space);
2008         }
2009
2010         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2011         {
2012                 Scene *scene = CTX_data_scene(C);
2013                 switch (scene->orientation_type) {
2014                         case V3D_MANIP_VIEW:
2015                         {
2016                                 float viewinv_m3[3][3];
2017                                 copy_m3_m4(viewinv_m3, rv3d->viewinv);
2018                                 if (!equals_m3m3(viewinv_m3, xgzgroup->prev.viewinv_m3)) {
2019                                         /* Take care calling refresh from draw_prepare,
2020                                          * this should be OK because it's only adjusting the cage orientation. */
2021                                         WIDGETGROUP_xform_cage_refresh(C, gzgroup);
2022                                 }
2023                                 break;
2024                         }
2025                 }
2026         }
2027 }
2028
2029 void VIEW3D_GGT_xform_cage(wmGizmoGroupType *gzgt)
2030 {
2031         gzgt->name = "Transform Cage";
2032         gzgt->idname = "VIEW3D_GGT_xform_cage";
2033
2034         gzgt->flag |= WM_GIZMOGROUPTYPE_3D;
2035
2036         gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
2037         gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
2038
2039         gzgt->poll = WIDGETGROUP_xform_cage_poll;
2040         gzgt->setup = WIDGETGROUP_xform_cage_setup;
2041         gzgt->refresh = WIDGETGROUP_xform_cage_refresh;
2042         gzgt->message_subscribe = WIDGETGROUP_xform_cage_message_subscribe;
2043         gzgt->draw_prepare = WIDGETGROUP_xform_cage_draw_prepare;
2044 }
2045
2046 /** \} */
2047
2048 /* -------------------------------------------------------------------- */
2049 /** \name Transform Shear Gizmo
2050  * \{ */
2051
2052 struct XFormShearWidgetGroup {
2053         wmGizmo *gizmo[3][2];
2054         /* Only for view orientation. */
2055         struct {
2056                 float viewinv_m3[3][3];
2057         } prev;
2058 };
2059
2060 static bool WIDGETGROUP_xform_shear_poll(const bContext *C, wmGizmoGroupType *gzgt)
2061 {
2062         if (!ED_gizmo_poll_or_unlink_delayed_from_tool(C, gzgt)) {
2063                 return false;
2064         }
2065         View3D *v3d = CTX_wm_view3d(C);
2066         if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_TOOL)) {
2067                 return false;
2068         }
2069         return true;
2070 }
2071
2072 static void WIDGETGROUP_xform_shear_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
2073 {
2074         struct XFormShearWidgetGroup *xgzgroup = MEM_mallocN(sizeof(struct XFormShearWidgetGroup), __func__);
2075         const wmGizmoType *gzt_arrow = WM_gizmotype_find("GIZMO_GT_arrow_3d", true);
2076         wmOperatorType *ot_shear = WM_operatortype_find("TRANSFORM_OT_shear", true);
2077
2078         float axis_color[3][3];
2079         for (int i = 0; i < 3; i++) {
2080                 UI_GetThemeColor3fv(TH_AXIS_X + i, axis_color[i]);
2081         }
2082
2083         for (int i = 0; i < 3; i++) {
2084                 for (int j = 0; j < 2; j++) {
2085                         wmGizmo *gz = WM_gizmo_new_ptr(gzt_arrow, gzgroup, NULL);
2086                         RNA_enum_set(gz->ptr, "draw_style", ED_GIZMO_ARROW_STYLE_BOX);
2087                         const int i_ortho_a = (i + j + 1) % 3;
2088                         const int i_ortho_b = (i + (1 - j) + 1) % 3;
2089                         interp_v3_v3v3(gz->color, axis_color[i_ortho_a], axis_color[i_ortho_b], 0.75f);
2090                         gz->color[3] = 0.5f;
2091                         PointerRNA *ptr = WM_gizmo_operator_set(gz, 0, ot_shear, NULL);
2092                         RNA_enum_set(ptr, "shear_axis", 0);
2093                         RNA_boolean_set(ptr, "release_confirm", 1);
2094                         xgzgroup->gizmo[i][j] = gz;
2095                 }
2096         }
2097
2098         gzgroup->customdata = xgzgroup;
2099 }
2100
2101 static void WIDGETGROUP_xform_shear_refresh(const bContext *C, wmGizmoGroup *gzgroup)
2102 {
2103         ARegion *ar = CTX_wm_region(C);
2104         RegionView3D *rv3d = ar->regiondata;
2105
2106         struct XFormShearWidgetGroup *xgzgroup = gzgroup->customdata;
2107         struct TransformBounds tbounds;
2108
2109         if (ED_transform_calc_gizmo_stats(
2110                     C, &(struct TransformCalcParams) {
2111                         .use_local_axis = false,
2112                     }, &tbounds) == 0)
2113         {
2114                 for (int i = 0; i < 3; i++) {
2115                         for (int j = 0; j < 2; j++) {
2116                                 wmGizmo *gz = xgzgroup->gizmo[i][j];
2117                                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, true);
2118                         }
2119                 }
2120         }
2121         else {
2122                 gizmo_prepare_mat(C, rv3d, &tbounds);
2123                 for (int i = 0; i < 3; i++) {
2124                         for (int j = 0; j < 2; j++) {
2125                                 wmGizmo *gz = xgzgroup->gizmo[i][j];
2126                                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
2127                                 WM_gizmo_set_flag(gz, WM_GIZMO_MOVE_CURSOR, true);
2128
2129                                 wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, 0);
2130                                 const int i_ortho_a = (i + j + 1) % 3;
2131                                 const int i_ortho_b = (i + (1 - j) + 1) % 3;
2132                                 WM_gizmo_set_matrix_rotation_from_yz_axis(gz, rv3d->twmat[i_ortho_a], rv3d->twmat[i]);
2133                                 WM_gizmo_set_matrix_location(gz, rv3d->twmat[3]);
2134
2135                                 float axis[3];
2136                                 if (j == 0) {
2137                                         copy_v3_v3(axis, tbounds.axis[i_ortho_b]);
2138                                 }
2139                                 else {
2140                                         negate_v3_v3(axis, tbounds.axis[i_ortho_b]);
2141                                 }
2142                                 RNA_float_set_array(&gzop->ptr, "axis", axis);
2143                                 RNA_float_set_array(&gzop->ptr, "axis_ortho", tbounds.axis[i_ortho_a]);
2144                                 mul_v3_fl(gz->matrix_basis[0], 0.5f);
2145                                 mul_v3_fl(gz->matrix_basis[1], 6.0f);
2146                         }
2147                 }
2148         }
2149
2150         /* Needed to test view orientation changes. */
2151         copy_m3_m4(xgzgroup->prev.viewinv_m3, rv3d->viewinv);
2152 }
2153
2154 static void WIDGETGROUP_xform_shear_message_subscribe(
2155         const bContext *C, wmGizmoGroup *gzgroup, struct wmMsgBus *mbus)
2156 {
2157         Scene *scene = CTX_data_scene(C);
2158         bScreen *screen = CTX_wm_screen(C);
2159         ScrArea *sa = CTX_wm_area(C);
2160         ARegion *ar = CTX_wm_region(C);
2161         gizmo_xform_message_subscribe(gzgroup, mbus, scene, screen, sa, ar, VIEW3D_GGT_xform_shear);
2162 }
2163
2164 static void WIDGETGROUP_xform_shear_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
2165 {
2166         struct XFormShearWidgetGroup *xgzgroup = gzgroup->customdata;
2167         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2168         {
2169                 Scene *scene = CTX_data_scene(C);
2170                 switch (scene->orientation_type) {
2171                         case V3D_MANIP_VIEW:
2172                         {
2173                                 float viewinv_m3[3][3];
2174                                 copy_m3_m4(viewinv_m3, rv3d->viewinv);
2175                                 if (!equals_m3m3(viewinv_m3, xgzgroup->prev.viewinv_m3)) {
2176                                         /* Take care calling refresh from draw_prepare,
2177                                          * this should be OK because it's only adjusting the cage orientation. */
2178                                         WIDGETGROUP_xform_shear_refresh(C, gzgroup);
2179                                 }
2180                                 break;
2181                         }
2182                 }
2183         }
2184
2185         /* Basic ordering for drawing only. */
2186         {
2187                 LISTBASE_FOREACH (wmGizmo *, gz, &gzgroup->gizmos) {
2188                         /* Since we have two pairs of each axis,
2189                          * bias the values so gizmos that are orthogonal to the view get priority.
2190                          * This means we never default to shearing along the view axis in the case of an overlap. */
2191                         float axis_order[3], axis_bias[3];
2192                         copy_v3_v3(axis_order, gz->matrix_basis[2]);
2193                         copy_v3_v3(axis_bias, gz->matrix_basis[1]);
2194                         if (dot_v3v3(axis_bias, rv3d->viewinv[2]) < 0.0f) {
2195                                 negate_v3(axis_bias);
2196                         }
2197                         madd_v3_v3fl(axis_order, axis_bias, 0.01f);
2198                         gz->temp.f = dot_v3v3(rv3d->viewinv[2], axis_order);
2199                 }
2200                 BLI_listbase_sort(&gzgroup->gizmos, WM_gizmo_cmp_temp_fl_reverse);
2201         }
2202 }
2203
2204 void VIEW3D_GGT_xform_shear(wmGizmoGroupType *gzgt)
2205 {
2206         gzgt->name = "Transform Shear";
2207         gzgt->idname = "VIEW3D_GGT_xform_shear";
2208
2209         gzgt->flag |= WM_GIZMOGROUPTYPE_3D;
2210
2211         gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
2212         gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
2213
2214         gzgt->poll = WIDGETGROUP_xform_shear_poll;
2215         gzgt->setup = WIDGETGROUP_xform_shear_setup;
2216         gzgt->refresh = WIDGETGROUP_xform_shear_refresh;
2217         gzgt->message_subscribe = WIDGETGROUP_xform_shear_message_subscribe;
2218         gzgt->draw_prepare = WIDGETGROUP_xform_shear_draw_prepare;
2219 }
2220
2221 /** \} */