Cleanup: comment line length (editors)
[blender.git] / source / blender / editors / transform / transform_generics.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/transform/transform_generics.c
29  *  \ingroup edtransform
30  */
31
32 #include <string.h>
33 #include <math.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_sys_types.h" /* for intptr_t support */
38
39 #include "DNA_anim_types.h"
40 #include "DNA_armature_types.h"
41 #include "DNA_brush_types.h"
42 #include "DNA_gpencil_types.h"
43 #include "DNA_lattice_types.h"
44 #include "DNA_screen_types.h"
45 #include "DNA_sequence_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_mesh_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_modifier_types.h"
52 #include "DNA_movieclip_types.h"
53 #include "DNA_mask_types.h"
54 #include "DNA_meta_types.h"
55
56 #include "BLI_math.h"
57 #include "BLI_blenlib.h"
58 #include "BLI_rand.h"
59 #include "BLI_utildefines.h"
60
61 #include "BLT_translation.h"
62
63 #include "RNA_access.h"
64
65 #include "BIF_gl.h"
66 #include "BIF_glutil.h"
67
68 #include "BIK_api.h"
69
70 #include "BKE_animsys.h"
71 #include "BKE_action.h"
72 #include "BKE_armature.h"
73 #include "BKE_curve.h"
74 #include "BKE_depsgraph.h"
75 #include "BKE_fcurve.h"
76 #include "BKE_lattice.h"
77 #include "BKE_library.h"
78 #include "BKE_main.h"
79 #include "BKE_nla.h"
80 #include "BKE_context.h"
81 #include "BKE_paint.h"
82 #include "BKE_sequencer.h"
83 #include "BKE_editmesh.h"
84 #include "BKE_tracking.h"
85 #include "BKE_mask.h"
86
87 #include "ED_anim_api.h"
88 #include "ED_armature.h"
89 #include "ED_image.h"
90 #include "ED_keyframing.h"
91 #include "ED_markers.h"
92 #include "ED_mesh.h"
93 #include "ED_object.h"
94 #include "ED_particle.h"
95 #include "ED_screen_types.h"
96 #include "ED_space_api.h"
97 #include "ED_uvedit.h"
98 #include "ED_view3d.h"
99 #include "ED_curve.h" /* for curve_editnurbs */
100 #include "ED_clip.h"
101 #include "ED_screen.h"
102
103 #include "WM_types.h"
104 #include "WM_api.h"
105
106 #include "UI_resources.h"
107 #include "UI_view2d.h"
108
109 #include "transform.h"
110
111 /* ************************** Functions *************************** */
112
113 void getViewVector(const TransInfo *t, const float coord[3], float vec[3])
114 {
115         if (t->persp != RV3D_ORTHO) {
116                 sub_v3_v3v3(vec, coord, t->viewinv[3]);
117         }
118         else {
119                 copy_v3_v3(vec, t->viewinv[2]);
120         }
121         normalize_v3(vec);
122 }
123
124 /* ************************** GENERICS **************************** */
125
126
127 static void clipMirrorModifier(TransInfo *t, Object *ob)
128 {
129         ModifierData *md = ob->modifiers.first;
130         float tolerance[3] = {0.0f, 0.0f, 0.0f};
131         int axis = 0;
132
133         for (; md; md = md->next) {
134                 if ((md->type == eModifierType_Mirror) && (md->mode & eModifierMode_Realtime)) {
135                         MirrorModifierData *mmd = (MirrorModifierData *) md;
136
137                         if (mmd->flag & MOD_MIR_CLIPPING) {
138                                 axis = 0;
139                                 if (mmd->flag & MOD_MIR_AXIS_X) {
140                                         axis |= 1;
141                                         tolerance[0] = mmd->tolerance;
142                                 }
143                                 if (mmd->flag & MOD_MIR_AXIS_Y) {
144                                         axis |= 2;
145                                         tolerance[1] = mmd->tolerance;
146                                 }
147                                 if (mmd->flag & MOD_MIR_AXIS_Z) {
148                                         axis |= 4;
149                                         tolerance[2] = mmd->tolerance;
150                                 }
151                                 if (axis) {
152                                         float mtx[4][4], imtx[4][4];
153                                         int i;
154                                         TransData *td = t->data;
155
156                                         if (mmd->mirror_ob) {
157                                                 float obinv[4][4];
158
159                                                 invert_m4_m4(obinv, mmd->mirror_ob->obmat);
160                                                 mul_m4_m4m4(mtx, obinv, ob->obmat);
161                                                 invert_m4_m4(imtx, mtx);
162                                         }
163
164                                         for (i = 0; i < t->total; i++, td++) {
165                                                 int clip;
166                                                 float loc[3], iloc[3];
167
168                                                 if (td->flag & TD_NOACTION)
169                                                         break;
170                                                 if (td->loc == NULL)
171                                                         break;
172
173                                                 if (td->flag & TD_SKIP)
174                                                         continue;
175
176                                                 copy_v3_v3(loc,  td->loc);
177                                                 copy_v3_v3(iloc, td->iloc);
178
179                                                 if (mmd->mirror_ob) {
180                                                         mul_m4_v3(mtx, loc);
181                                                         mul_m4_v3(mtx, iloc);
182                                                 }
183
184                                                 clip = 0;
185                                                 if (axis & 1) {
186                                                         if (fabsf(iloc[0]) <= tolerance[0] ||
187                                                             loc[0] * iloc[0] < 0.0f)
188                                                         {
189                                                                 loc[0] = 0.0f;
190                                                                 clip = 1;
191                                                         }
192                                                 }
193
194                                                 if (axis & 2) {
195                                                         if (fabsf(iloc[1]) <= tolerance[1] ||
196                                                             loc[1] * iloc[1] < 0.0f)
197                                                         {
198                                                                 loc[1] = 0.0f;
199                                                                 clip = 1;
200                                                         }
201                                                 }
202                                                 if (axis & 4) {
203                                                         if (fabsf(iloc[2]) <= tolerance[2] ||
204                                                             loc[2] * iloc[2] < 0.0f)
205                                                         {
206                                                                 loc[2] = 0.0f;
207                                                                 clip = 1;
208                                                         }
209                                                 }
210                                                 if (clip) {
211                                                         if (mmd->mirror_ob) {
212                                                                 mul_m4_v3(imtx, loc);
213                                                         }
214                                                         copy_v3_v3(td->loc, loc);
215                                                 }
216                                         }
217                                 }
218
219                         }
220                 }
221         }
222 }
223
224 /* assumes obedit set to mesh object */
225 static void editbmesh_apply_to_mirror(TransInfo *t)
226 {
227         TransData *td = t->data;
228         BMVert *eve;
229         int i;
230
231         for (i = 0; i < t->total; i++, td++) {
232                 if (td->flag & TD_NOACTION)
233                         break;
234                 if (td->loc == NULL)
235                         break;
236                 if (td->flag & TD_SKIP)
237                         continue;
238
239                 eve = td->extra;
240                 if (eve) {
241                         eve->co[0] = -td->loc[0];
242                         eve->co[1] = td->loc[1];
243                         eve->co[2] = td->loc[2];
244                 }
245
246                 if (td->flag & TD_MIRROR_EDGE) {
247                         td->loc[0] = 0;
248                 }
249         }
250 }
251
252 /* for the realtime animation recording feature, handle overlapping data */
253 static void animrecord_check_state(Scene *scene, ID *id, wmTimer *animtimer)
254 {
255         ScreenAnimData *sad = (animtimer) ? animtimer->customdata : NULL;
256
257         /* sanity checks */
258         if (ELEM(NULL, scene, id, sad))
259                 return;
260
261         /* check if we need a new strip if:
262          * - if animtimer is running
263          * - we're not only keying for available channels
264          * - the option to add new actions for each round is not enabled
265          */
266         if (IS_AUTOKEY_FLAG(scene, INSERTAVAIL) == 0 && (scene->toolsettings->autokey_flag & ANIMRECORD_FLAG_WITHNLA)) {
267                 /* if playback has just looped around,
268                  * we need to add a new NLA track+strip to allow a clean pass to occur */
269                 if ((sad) && (sad->flag & ANIMPLAY_FLAG_JUMPED)) {
270                         AnimData *adt = BKE_animdata_from_id(id);
271                         const bool is_first = (adt) && (adt->nla_tracks.first == NULL);
272
273                         /* perform push-down manually with some differences
274                          * NOTE: BKE_nla_action_pushdown() sync warning...
275                          */
276                         if ((adt->action) && !(adt->flag & ADT_NLA_EDIT_ON)) {
277                                 float astart, aend;
278
279                                 /* only push down if action is more than 1-2 frames long */
280                                 calc_action_range(adt->action, &astart, &aend, 1);
281                                 if (aend > astart + 2.0f) {
282                                         NlaStrip *strip = BKE_nlastack_add_strip(adt, adt->action);
283
284                                         /* clear reference to action now that we've pushed it onto the stack */
285                                         id_us_min(&adt->action->id);
286                                         adt->action = NULL;
287
288                                         /* adjust blending + extend so that they will behave correctly */
289                                         strip->extendmode = NLASTRIP_EXTEND_NOTHING;
290                                         strip->flag &= ~(NLASTRIP_FLAG_AUTO_BLENDS | NLASTRIP_FLAG_SELECT | NLASTRIP_FLAG_ACTIVE);
291
292                                         /* copy current "action blending" settings from adt to the strip,
293                                          * as it was keyframed with these settings, so omitting them will
294                                          * change the effect  [T54766]
295                                          */
296                                         if (is_first == false) {
297                                                 strip->blendmode = adt->act_blendmode;
298                                                 strip->influence = adt->act_influence;
299
300                                                 if (adt->act_influence < 1.0f) {
301                                                         /* enable "user-controlled" influence (which will insert a default keyframe)
302                                                          * so that the influence doesn't get lost on the new update
303                                                          *
304                                                          * NOTE: An alternative way would have been to instead hack the influence
305                                                          * to not get always get reset to full strength if NLASTRIP_FLAG_USR_INFLUENCE
306                                                          * is disabled but auto-blending isn't being used. However, that approach
307                                                          * is a bit hacky/hard to discover, and may cause backwards compatibility issues,
308                                                          * so it's better to just do it this way.
309                                                          */
310                                                         strip->flag |= NLASTRIP_FLAG_USR_INFLUENCE;
311                                                         BKE_nlastrip_validate_fcurves(strip);
312                                                 }
313                                         }
314
315                                         /* also, adjust the AnimData's action extend mode to be on
316                                          * 'nothing' so that previous result still play
317                                          */
318                                         adt->act_extendmode = NLASTRIP_EXTEND_NOTHING;
319                                 }
320                         }
321                 }
322         }
323 }
324
325 static bool fcu_test_selected(FCurve *fcu)
326 {
327         BezTriple *bezt = fcu->bezt;
328         unsigned int i;
329
330         if (bezt == NULL) /* ignore baked */
331                 return 0;
332
333         for (i = 0; i < fcu->totvert; i++, bezt++) {
334                 if (BEZT_ISSEL_ANY(bezt)) return 1;
335         }
336
337         return 0;
338 }
339
340 /* helper for recalcData() - for Action Editor transforms */
341 static void recalcData_actedit(TransInfo *t)
342 {
343         Scene *scene = t->scene;
344         SpaceAction *saction = (SpaceAction *)t->sa->spacedata.first;
345
346         bAnimContext ac = {NULL};
347         ListBase anim_data = {NULL, NULL};
348         bAnimListElem *ale;
349         int filter;
350
351         /* initialize relevant anim-context 'context' data from TransInfo data */
352         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
353         ac.bmain = CTX_data_main(t->context);
354         ac.scene = t->scene;
355         ac.obact = OBACT;
356         ac.sa = t->sa;
357         ac.ar = t->ar;
358         ac.sl = (t->sa) ? t->sa->spacedata.first : NULL;
359         ac.spacetype = (t->sa) ? t->sa->spacetype : 0;
360         ac.regiontype = (t->ar) ? t->ar->regiontype : 0;
361
362         ANIM_animdata_context_getdata(&ac);
363
364         /* perform flush */
365         if (ELEM(ac.datatype, ANIMCONT_GPENCIL, ANIMCONT_MASK)) {
366                 /* flush transform values back to actual coordinates */
367                 flushTransIntFrameActionData(t);
368         }
369         else {
370                 /* get animdata blocks visible in editor, assuming that these will be the ones where things changed */
371                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_ANIMDATA);
372                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
373
374                 /* just tag these animdata-blocks to recalc, assuming that some data there changed
375                  * BUT only do this if realtime updates are enabled
376                  */
377                 if ((saction->flag & SACTION_NOREALTIMEUPDATES) == 0) {
378                         for (ale = anim_data.first; ale; ale = ale->next) {
379                                 /* set refresh tags for objects using this animation */
380                                 ANIM_list_elem_update(CTX_data_main(t->context), scene, ale);
381                         }
382                 }
383
384                 /* now free temp channels */
385                 ANIM_animdata_freelist(&anim_data);
386         }
387 }
388 /* helper for recalcData() - for Graph Editor transforms */
389 static void recalcData_graphedit(TransInfo *t)
390 {
391         SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
392         Scene *scene;
393
394         ListBase anim_data = {NULL, NULL};
395         bAnimContext ac = {NULL};
396         int filter;
397
398         bAnimListElem *ale;
399         int dosort = 0;
400
401         /* initialize relevant anim-context 'context' data from TransInfo data */
402         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
403         ac.bmain = CTX_data_main(t->context);
404         scene = ac.scene = t->scene;
405         ac.obact = OBACT;
406         ac.sa = t->sa;
407         ac.ar = t->ar;
408         ac.sl = (t->sa) ? t->sa->spacedata.first : NULL;
409         ac.spacetype = (t->sa) ? t->sa->spacetype : 0;
410         ac.regiontype = (t->ar) ? t->ar->regiontype : 0;
411
412         ANIM_animdata_context_getdata(&ac);
413
414         /* do the flush first */
415         flushTransGraphData(t);
416
417         /* get curves to check if a re-sort is needed */
418         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVE_VISIBLE);
419         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
420
421         /* now test if there is a need to re-sort */
422         for (ale = anim_data.first; ale; ale = ale->next) {
423                 FCurve *fcu = (FCurve *)ale->key_data;
424
425                 /* ignore FC-Curves without any selected verts */
426                 if (!fcu_test_selected(fcu))
427                         continue;
428
429                 /* watch it: if the time is wrong: do not correct handles yet */
430                 if (test_time_fcurve(fcu))
431                         dosort++;
432                 else
433                         calchandles_fcurve(fcu);
434
435                 /* set refresh tags for objects using this animation,
436                  * BUT only if realtime updates are enabled
437                  */
438                 if ((sipo->flag & SIPO_NOREALTIMEUPDATES) == 0)
439                         ANIM_list_elem_update(CTX_data_main(t->context), t->scene, ale);
440         }
441
442         /* do resort and other updates? */
443         if (dosort) remake_graph_transdata(t, &anim_data);
444
445         /* now free temp channels */
446         ANIM_animdata_freelist(&anim_data);
447 }
448
449 /* helper for recalcData() - for NLA Editor transforms */
450 static void recalcData_nla(TransInfo *t)
451 {
452         TransDataNla *tdn = t->custom.type.data;
453         SpaceNla *snla = (SpaceNla *)t->sa->spacedata.first;
454         Scene *scene = t->scene;
455         double secf = FPS;
456         int i;
457
458         /* for each strip we've got, perform some additional validation of the values that got set before
459          * using RNA to set the value (which does some special operations when setting these values to make
460          * sure that everything works ok)
461          */
462         for (i = 0; i < t->total; i++, tdn++) {
463                 NlaStrip *strip = tdn->strip;
464                 PointerRNA strip_ptr;
465                 short pExceeded, nExceeded, iter;
466                 int delta_y1, delta_y2;
467
468                 /* if this tdn has no handles, that means it is just a dummy that should be skipped */
469                 if (tdn->handle == 0)
470                         continue;
471
472                 /* set refresh tags for objects using this animation,
473                  * BUT only if realtime updates are enabled
474                  */
475                 if ((snla->flag & SNLA_NOREALTIMEUPDATES) == 0)
476                         ANIM_id_update(t->scene, tdn->id);
477
478                 /* if canceling transform, just write the values without validating, then move on */
479                 if (t->state == TRANS_CANCEL) {
480                         /* clear the values by directly overwriting the originals, but also need to restore
481                          * endpoints of neighboring transition-strips
482                          */
483
484                         /* start */
485                         strip->start = tdn->h1[0];
486
487                         if ((strip->prev) && (strip->prev->type == NLASTRIP_TYPE_TRANSITION))
488                                 strip->prev->end = tdn->h1[0];
489
490                         /* end */
491                         strip->end = tdn->h2[0];
492
493                         if ((strip->next) && (strip->next->type == NLASTRIP_TYPE_TRANSITION))
494                                 strip->next->start = tdn->h2[0];
495
496                         /* flush transforms to child strips (since this should be a meta) */
497                         BKE_nlameta_flush_transforms(strip);
498
499                         /* restore to original track (if needed) */
500                         if (tdn->oldTrack != tdn->nlt) {
501                                 /* just append to end of list for now, since strips get sorted in special_aftertrans_update() */
502                                 BLI_remlink(&tdn->nlt->strips, strip);
503                                 BLI_addtail(&tdn->oldTrack->strips, strip);
504                         }
505
506                         continue;
507                 }
508
509                 /* firstly, check if the proposed transform locations would overlap with any neighboring strips
510                  * (barring transitions) which are absolute barriers since they are not being moved
511                  *
512                  * this is done as a iterative procedure (done 5 times max for now)
513                  */
514                 for (iter = 0; iter < 5; iter++) {
515                         pExceeded = ((strip->prev) && (strip->prev->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h1[0] < strip->prev->end));
516                         nExceeded = ((strip->next) && (strip->next->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h2[0] > strip->next->start));
517
518                         if ((pExceeded && nExceeded) || (iter == 4)) {
519                                 /* both endpoints exceeded (or iteration ping-pong'd meaning that we need a compromise)
520                                  * - simply crop strip to fit within the bounds of the strips bounding it
521                                  * - if there were no neighbors, clear the transforms (make it default to the strip's current values)
522                                  */
523                                 if (strip->prev && strip->next) {
524                                         tdn->h1[0] = strip->prev->end;
525                                         tdn->h2[0] = strip->next->start;
526                                 }
527                                 else {
528                                         tdn->h1[0] = strip->start;
529                                         tdn->h2[0] = strip->end;
530                                 }
531                         }
532                         else if (nExceeded) {
533                                 /* move backwards */
534                                 float offset = tdn->h2[0] - strip->next->start;
535
536                                 tdn->h1[0] -= offset;
537                                 tdn->h2[0] -= offset;
538                         }
539                         else if (pExceeded) {
540                                 /* more forwards */
541                                 float offset = strip->prev->end - tdn->h1[0];
542
543                                 tdn->h1[0] += offset;
544                                 tdn->h2[0] += offset;
545                         }
546                         else /* all is fine and well */
547                                 break;
548                 }
549
550                 /* handle auto-snapping
551                  * NOTE: only do this when transform is still running, or we can't restore
552                  */
553                 if (t->state != TRANS_CANCEL) {
554                         switch (snla->autosnap) {
555                                 case SACTSNAP_FRAME: /* snap to nearest frame */
556                                 case SACTSNAP_STEP: /* frame step - this is basically the same,
557                                                      * since we don't have any remapping going on */
558                                 {
559                                         tdn->h1[0] = floorf(tdn->h1[0] + 0.5f);
560                                         tdn->h2[0] = floorf(tdn->h2[0] + 0.5f);
561                                         break;
562                                 }
563
564                                 case SACTSNAP_SECOND: /* snap to nearest second */
565                                 case SACTSNAP_TSTEP: /* second step - this is basically the same,
566                                                       * since we don't have any remapping going on */
567                                 {
568                                         /* This case behaves differently from the rest, since lengths of strips
569                                          * may not be multiples of a second. If we just naively resize adjust
570                                          * the handles, things may not work correctly. Instead, we only snap
571                                          * the first handle, and move the other to fit.
572                                          *
573                                          * FIXME: we do run into problems here when user attempts to negatively
574                                          *        scale the strip, as it then just compresses down and refuses
575                                          *        to expand out the other end.
576                                          */
577                                         float h1_new = (float)(floor(((double)tdn->h1[0] / secf) + 0.5) * secf);
578                                         float delta  = h1_new - tdn->h1[0];
579
580                                         tdn->h1[0] = h1_new;
581                                         tdn->h2[0] += delta;
582                                         break;
583                                 }
584
585                                 case SACTSNAP_MARKER: /* snap to nearest marker */
586                                 {
587                                         tdn->h1[0] = (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h1[0]);
588                                         tdn->h2[0] = (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h2[0]);
589                                         break;
590                                 }
591                         }
592                 }
593
594                 /* Use RNA to write the values to ensure that constraints on these are obeyed
595                  * (e.g. for transition strips, the values are taken from the neighbors)
596                  *
597                  * NOTE: we write these twice to avoid truncation errors which can arise when
598                  * moving the strips a large distance using numeric input [#33852]
599                  */
600                 RNA_pointer_create(NULL, &RNA_NlaStrip, strip, &strip_ptr);
601
602                 RNA_float_set(&strip_ptr, "frame_start", tdn->h1[0]);
603                 RNA_float_set(&strip_ptr, "frame_end", tdn->h2[0]);
604
605                 RNA_float_set(&strip_ptr, "frame_start", tdn->h1[0]);
606                 RNA_float_set(&strip_ptr, "frame_end", tdn->h2[0]);
607
608                 /* flush transforms to child strips (since this should be a meta) */
609                 BKE_nlameta_flush_transforms(strip);
610
611
612                 /* now, check if we need to try and move track
613                  * - we need to calculate both, as only one may have been altered by transform if only 1 handle moved
614                  */
615                 delta_y1 = ((int)tdn->h1[1] / NLACHANNEL_STEP(snla) - tdn->trackIndex);
616                 delta_y2 = ((int)tdn->h2[1] / NLACHANNEL_STEP(snla) - tdn->trackIndex);
617
618                 if (delta_y1 || delta_y2) {
619                         NlaTrack *track;
620                         int delta = (delta_y2) ? delta_y2 : delta_y1;
621                         int n;
622
623                         /* move in the requested direction, checking at each layer if there's space for strip to pass through,
624                          * stopping on the last track available or that we're able to fit in
625                          */
626                         if (delta > 0) {
627                                 for (track = tdn->nlt->next, n = 0; (track) && (n < delta); track = track->next, n++) {
628                                         /* check if space in this track for the strip */
629                                         if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
630                                                 /* move strip to this track */
631                                                 BLI_remlink(&tdn->nlt->strips, strip);
632                                                 BKE_nlatrack_add_strip(track, strip);
633
634                                                 tdn->nlt = track;
635                                                 tdn->trackIndex++;
636                                         }
637                                         else /* can't move any further */
638                                                 break;
639                                 }
640                         }
641                         else {
642                                 /* make delta 'positive' before using it, since we now know to go backwards */
643                                 delta = -delta;
644
645                                 for (track = tdn->nlt->prev, n = 0; (track) && (n < delta); track = track->prev, n++) {
646                                         /* check if space in this track for the strip */
647                                         if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
648                                                 /* move strip to this track */
649                                                 BLI_remlink(&tdn->nlt->strips, strip);
650                                                 BKE_nlatrack_add_strip(track, strip);
651
652                                                 tdn->nlt = track;
653                                                 tdn->trackIndex--;
654                                         }
655                                         else /* can't move any further */
656                                                 break;
657                                 }
658                         }
659                 }
660         }
661 }
662
663 static void recalcData_mask_common(TransInfo *t)
664 {
665         Mask *mask = CTX_data_edit_mask(t->context);
666
667         flushTransMasking(t);
668
669         DAG_id_tag_update(&mask->id, 0);
670 }
671
672 /* helper for recalcData() - for Image Editor transforms */
673 static void recalcData_image(TransInfo *t)
674 {
675         if (t->options & CTX_MASK) {
676                 recalcData_mask_common(t);
677         }
678         else if (t->options & CTX_PAINT_CURVE) {
679                 flushTransPaintCurve(t);
680         }
681         else if (t->obedit && t->obedit->type == OB_MESH) {
682                 SpaceImage *sima = t->sa->spacedata.first;
683
684                 flushTransUVs(t);
685                 if (sima->flag & SI_LIVE_UNWRAP)
686                         ED_uvedit_live_unwrap_re_solve();
687
688                 DAG_id_tag_update(t->obedit->data, 0);
689         }
690 }
691
692 /* helper for recalcData() - for Movie Clip transforms */
693 static void recalcData_spaceclip(TransInfo *t)
694 {
695         SpaceClip *sc = t->sa->spacedata.first;
696
697         if (ED_space_clip_check_show_trackedit(sc)) {
698                 MovieClip *clip = ED_space_clip_get_clip(sc);
699                 ListBase *tracksbase = BKE_tracking_get_active_tracks(&clip->tracking);
700                 MovieTrackingTrack *track;
701                 int framenr = ED_space_clip_get_clip_frame_number(sc);
702
703                 flushTransTracking(t);
704
705                 track = tracksbase->first;
706                 while (track) {
707                         if (TRACK_VIEW_SELECTED(sc, track) && (track->flag & TRACK_LOCKED) == 0) {
708                                 MovieTrackingMarker *marker = BKE_tracking_marker_get(track, framenr);
709
710                                 if (t->mode == TFM_TRANSLATION) {
711                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
712                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_POS);
713                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_SEARCH))
714                                                 BKE_tracking_marker_clamp(marker, CLAMP_SEARCH_POS);
715                                 }
716                                 else if (t->mode == TFM_RESIZE) {
717                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
718                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_DIM);
719                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_SEARCH))
720                                                 BKE_tracking_marker_clamp(marker, CLAMP_SEARCH_DIM);
721                                 }
722                                 else if (t->mode == TFM_ROTATION) {
723                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
724                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_POS);
725                                 }
726                         }
727
728                         track = track->next;
729                 }
730
731                 DAG_id_tag_update(&clip->id, 0);
732         }
733         else if (t->options & CTX_MASK) {
734                 recalcData_mask_common(t);
735         }
736 }
737
738 /* helper for recalcData() - for object transforms, typically in the 3D view */
739 static void recalcData_objects(TransInfo *t)
740 {
741         Main *bmain = CTX_data_main(t->context);
742         Base *base = t->scene->basact;
743
744         if (t->obedit) {
745                 if (ELEM(t->obedit->type, OB_CURVE, OB_SURF)) {
746                         Curve *cu = t->obedit->data;
747                         ListBase *nurbs = BKE_curve_editNurbs_get(cu);
748                         Nurb *nu = nurbs->first;
749
750                         if (t->state != TRANS_CANCEL) {
751                                 clipMirrorModifier(t, t->obedit);
752                                 applyProject(t);
753                         }
754
755                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
756
757                         if (t->state == TRANS_CANCEL) {
758                                 while (nu) {
759                                         BKE_nurb_handles_calc(nu); /* Cant do testhandlesNurb here, it messes up the h1 and h2 flags */
760                                         nu = nu->next;
761                                 }
762                         }
763                         else {
764                                 /* Normal updating */
765                                 while (nu) {
766                                         BKE_nurb_test_2d(nu);
767                                         BKE_nurb_handles_calc(nu);
768                                         nu = nu->next;
769                                 }
770                         }
771                 }
772                 else if (t->obedit->type == OB_LATTICE) {
773                         Lattice *la = t->obedit->data;
774
775                         if (t->state != TRANS_CANCEL) {
776                                 applyProject(t);
777                         }
778
779                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
780
781                         if (la->editlatt->latt->flag & LT_OUTSIDE) outside_lattice(la->editlatt->latt);
782                 }
783                 else if (t->obedit->type == OB_MESH) {
784                         BMEditMesh *em = BKE_editmesh_from_object(t->obedit);
785                         /* mirror modifier clipping? */
786                         if (t->state != TRANS_CANCEL) {
787                                 /* apply clipping after so we never project past the clip plane [#25423] */
788                                 applyProject(t);
789                                 clipMirrorModifier(t, t->obedit);
790                         }
791                         if ((t->options & CTX_NO_MIRROR) == 0 && (t->flag & T_MIRROR))
792                                 editbmesh_apply_to_mirror(t);
793
794                         if (t->mode == TFM_EDGE_SLIDE) {
795                                 projectEdgeSlideData(t, false);
796                         }
797                         else if (t->mode == TFM_VERT_SLIDE) {
798                                 projectVertSlideData(t, false);
799                         }
800
801                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
802
803                         EDBM_mesh_normals_update(em);
804                         BKE_editmesh_tessface_calc(em);
805                 }
806                 else if (t->obedit->type == OB_ARMATURE) { /* no recalc flag, does pose */
807                         bArmature *arm = t->obedit->data;
808                         ListBase *edbo = arm->edbo;
809                         EditBone *ebo, *ebo_parent;
810                         TransData *td = t->data;
811                         int i;
812
813                         if (t->state != TRANS_CANCEL) {
814                                 applyProject(t);
815                         }
816
817                         /* Ensure all bones are correctly adjusted */
818                         for (ebo = edbo->first; ebo; ebo = ebo->next) {
819                                 ebo_parent = (ebo->flag & BONE_CONNECTED) ? ebo->parent : NULL;
820
821                                 if (ebo_parent) {
822                                         /* If this bone has a parent tip that has been moved */
823                                         if (ebo_parent->flag & BONE_TIPSEL) {
824                                                 copy_v3_v3(ebo->head, ebo_parent->tail);
825                                                 if (t->mode == TFM_BONE_ENVELOPE) ebo->rad_head = ebo_parent->rad_tail;
826                                         }
827                                         /* If this bone has a parent tip that has NOT been moved */
828                                         else {
829                                                 copy_v3_v3(ebo_parent->tail, ebo->head);
830                                                 if (t->mode == TFM_BONE_ENVELOPE) ebo_parent->rad_tail = ebo->rad_head;
831                                         }
832                                 }
833
834                                 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
835                                 ebo->length = len_v3v3(ebo->head, ebo->tail);
836                                 if (ebo->oldlength == 0.0f) {
837                                         ebo->rad_head = 0.25f * ebo->length;
838                                         ebo->rad_tail = 0.10f * ebo->length;
839                                         ebo->dist = 0.25f * ebo->length;
840                                         if (ebo->parent) {
841                                                 if (ebo->rad_head > ebo->parent->rad_tail)
842                                                         ebo->rad_head = ebo->parent->rad_tail;
843                                         }
844                                 }
845                                 else if (t->mode != TFM_BONE_ENVELOPE) {
846                                         /* if bones change length, lets do that for the deform distance as well */
847                                         ebo->dist *= ebo->length / ebo->oldlength;
848                                         ebo->rad_head *= ebo->length / ebo->oldlength;
849                                         ebo->rad_tail *= ebo->length / ebo->oldlength;
850                                         ebo->oldlength = ebo->length;
851
852                                         if (ebo_parent) {
853                                                 ebo_parent->rad_tail = ebo->rad_head;
854                                         }
855                                 }
856                         }
857
858                         if (!ELEM(t->mode, TFM_BONE_ROLL, TFM_BONE_ENVELOPE, TFM_BONE_ENVELOPE_DIST, TFM_BONESIZE)) {
859                                 /* fix roll */
860                                 for (i = 0; i < t->total; i++, td++) {
861                                         if (td->extra) {
862                                                 float vec[3], up_axis[3];
863                                                 float qrot[4];
864                                                 float roll;
865
866                                                 ebo = td->extra;
867
868                                                 if (t->state == TRANS_CANCEL) {
869                                                         /* restore roll */
870                                                         ebo->roll = td->ival;
871                                                 }
872                                                 else {
873                                                         copy_v3_v3(up_axis, td->axismtx[2]);
874
875                                                         sub_v3_v3v3(vec, ebo->tail, ebo->head);
876                                                         normalize_v3(vec);
877                                                         rotation_between_vecs_to_quat(qrot, td->axismtx[1], vec);
878                                                         mul_qt_v3(qrot, up_axis);
879
880                                                         /* roll has a tendency to flip in certain orientations - [#34283], [#33974] */
881                                                         roll = ED_armature_ebone_roll_to_vector(ebo, up_axis, false);
882                                                         ebo->roll = angle_compat_rad(roll, td->ival);
883                                                 }
884                                         }
885                                 }
886                         }
887
888                         if (arm->flag & ARM_MIRROR_EDIT) {
889                                 if (t->state != TRANS_CANCEL)
890                                         ED_armature_edit_transform_mirror_update(t->obedit);
891                                 else
892                                         restoreBones(t);
893                         }
894                 }
895                 else {
896                         if (t->state != TRANS_CANCEL) {
897                                 applyProject(t);
898                         }
899                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
900                 }
901         }
902         else if ((t->flag & T_POSE) && t->poseobj) {
903                 Object *ob = t->poseobj;
904                 bArmature *arm = ob->data;
905
906                 /* if animtimer is running, and the object already has animation data,
907                  * check if the auto-record feature means that we should record 'samples'
908                  * (i.e. uneditable animation values)
909                  *
910                  * context is needed for keying set poll() functions.
911                  */
912                 // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
913                 if ((t->animtimer) && (t->context) && IS_AUTOKEY_ON(t->scene)) {
914                         int targetless_ik = (t->flag & T_AUTOIK); // XXX this currently doesn't work, since flags aren't set yet!
915
916                         animrecord_check_state(t->scene, &ob->id, t->animtimer);
917                         autokeyframe_pose_cb_func(t->context, t->scene, (View3D *)t->view, ob, t->mode, targetless_ik);
918                 }
919
920                 /* old optimize trick... this enforces to bypass the depgraph */
921                 if (!(arm->flag & ARM_DELAYDEFORM)) {
922                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);  /* sets recalc flags */
923                         /* transformation of pose may affect IK tree, make sure it is rebuilt */
924                         BIK_clear_data(ob->pose);
925                 }
926                 else
927                         BKE_pose_where_is(t->scene, ob);
928         }
929         else if (base && (base->object->mode & OB_MODE_PARTICLE_EDIT) && PE_get_current(bmain, t->scene, base->object)) {
930                 if (t->state != TRANS_CANCEL) {
931                         applyProject(t);
932                 }
933                 flushTransParticles(t);
934         }
935         else {
936                 int i;
937
938                 if (t->state != TRANS_CANCEL) {
939                         applyProject(t);
940                 }
941
942                 for (i = 0; i < t->total; i++) {
943                         TransData *td = t->data + i;
944                         Object *ob = td->ob;
945
946                         if (td->flag & TD_NOACTION)
947                                 break;
948
949                         if (td->flag & TD_SKIP)
950                                 continue;
951
952                         /* if animtimer is running, and the object already has animation data,
953                          * check if the auto-record feature means that we should record 'samples'
954                          * (i.e. uneditable animation values)
955                          */
956                         // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
957                         if ((t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
958                                 animrecord_check_state(t->scene, &ob->id, t->animtimer);
959                                 autokeyframe_ob_cb_func(t->context, t->scene, (View3D *)t->view, ob, t->mode);
960                         }
961
962                         /* sets recalc flags fully, instead of flushing existing ones
963                          * otherwise proxies don't function correctly
964                          */
965                         DAG_id_tag_update(&ob->id, OB_RECALC_OB);
966
967                         if (t->flag & T_TEXTURE)
968                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
969                 }
970         }
971 }
972
973 /* helper for recalcData() - for sequencer transforms */
974 static void recalcData_sequencer(TransInfo *t)
975 {
976         TransData *td;
977         int a;
978         Sequence *seq_prev = NULL;
979
980         for (a = 0, td = t->data; a < t->total; a++, td++) {
981                 TransDataSeq *tdsq = (TransDataSeq *) td->extra;
982                 Sequence *seq = tdsq->seq;
983
984                 if (seq != seq_prev) {
985                         if (BKE_sequence_tx_fullupdate_test(seq)) {
986                                 /* A few effect strip types need a complete recache on transform. */
987                                 BKE_sequence_invalidate_cache(t->scene, seq);
988                         }
989                         else {
990                                 BKE_sequence_invalidate_dependent(t->scene, seq);
991                         }
992                 }
993
994                 seq_prev = seq;
995         }
996
997         BKE_sequencer_preprocessed_cache_cleanup();
998
999         flushTransSeq(t);
1000 }
1001
1002 /* force recalculation of triangles during transformation */
1003 static void recalcData_gpencil_strokes(TransInfo *t)
1004 {
1005         TransData *td = t->data;
1006         for (int i = 0; i < t->total; i++, td++) {
1007                 bGPDstroke *gps = td->extra;
1008                 if (gps != NULL) {
1009                         gps->flag |= GP_STROKE_RECALC_CACHES;
1010                 }
1011         }
1012 }
1013
1014 /* called for updating while transform acts, once per redraw */
1015 void recalcData(TransInfo *t)
1016 {
1017         /* if tests must match createTransData for correct updates */
1018         if (t->options & CTX_TEXTURE) {
1019                 recalcData_objects(t);
1020         }
1021         else if (t->options & CTX_EDGE) {
1022                 recalcData_objects(t);
1023         }
1024         else if (t->options & CTX_PAINT_CURVE) {
1025                 flushTransPaintCurve(t);
1026         }
1027         else if (t->options & CTX_GPENCIL_STROKES) {
1028                 /* set recalc triangle cache flag */
1029                 recalcData_gpencil_strokes(t);
1030         }
1031         else if (t->spacetype == SPACE_IMAGE) {
1032                 recalcData_image(t);
1033         }
1034         else if (t->spacetype == SPACE_ACTION) {
1035                 recalcData_actedit(t);
1036         }
1037         else if (t->spacetype == SPACE_NLA) {
1038                 recalcData_nla(t);
1039         }
1040         else if (t->spacetype == SPACE_SEQ) {
1041                 recalcData_sequencer(t);
1042         }
1043         else if (t->spacetype == SPACE_IPO) {
1044                 recalcData_graphedit(t);
1045         }
1046         else if (t->spacetype == SPACE_NODE) {
1047                 flushTransNodes(t);
1048         }
1049         else if (t->spacetype == SPACE_CLIP) {
1050                 recalcData_spaceclip(t);
1051         }
1052         else {
1053                 recalcData_objects(t);
1054         }
1055 }
1056
1057 void drawLine(TransInfo *t, const float center[3], const float dir[3], char axis, short options)
1058 {
1059         float v1[3], v2[3], v3[3];
1060         unsigned char col[3], col2[3];
1061
1062         if (t->spacetype == SPACE_VIEW3D) {
1063                 View3D *v3d = t->view;
1064
1065                 glPushMatrix();
1066
1067                 //if (t->obedit) glLoadMatrixf(t->obedit->obmat);       // sets opengl viewing
1068
1069
1070                 copy_v3_v3(v3, dir);
1071                 mul_v3_fl(v3, v3d->far);
1072
1073                 sub_v3_v3v3(v2, center, v3);
1074                 add_v3_v3v3(v1, center, v3);
1075
1076                 if (options & DRAWLIGHT) {
1077                         col[0] = col[1] = col[2] = 220;
1078                 }
1079                 else {
1080                         UI_GetThemeColor3ubv(TH_GRID, col);
1081                 }
1082                 UI_make_axis_color(col, col2, axis);
1083                 glColor3ubv(col2);
1084
1085                 setlinestyle(0);
1086                 glBegin(GL_LINES);
1087                 glVertex3fv(v1);
1088                 glVertex3fv(v2);
1089                 glEnd();
1090
1091                 glPopMatrix();
1092         }
1093 }
1094
1095 /**
1096  * Free data before switching to another mode.
1097  */
1098 void resetTransModal(TransInfo *t)
1099 {
1100         if (t->mode == TFM_EDGE_SLIDE) {
1101                 freeEdgeSlideVerts(t, &t->custom.mode);
1102         }
1103         else if (t->mode == TFM_VERT_SLIDE) {
1104                 freeVertSlideVerts(t, &t->custom.mode);
1105         }
1106 }
1107
1108 void resetTransRestrictions(TransInfo *t)
1109 {
1110         t->flag &= ~T_ALL_RESTRICTIONS;
1111 }
1112
1113 static int initTransInfo_edit_pet_to_flag(const int proportional)
1114 {
1115         switch (proportional) {
1116                 case PROP_EDIT_ON:
1117                         return T_PROP_EDIT;
1118                 case PROP_EDIT_CONNECTED:
1119                         return T_PROP_EDIT | T_PROP_CONNECTED;
1120                 case PROP_EDIT_PROJECTED:
1121                         return T_PROP_EDIT | T_PROP_PROJECTED;
1122                 default:
1123                         return 0;
1124         }
1125 }
1126
1127 /**
1128  * Setup internal data, mouse, vectors
1129  *
1130  * \note \a op and \a event can be NULL
1131  *
1132  * \see #saveTransform does the reverse.
1133  */
1134 void initTransInfo(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *event)
1135 {
1136         Scene *sce = CTX_data_scene(C);
1137         ToolSettings *ts = CTX_data_tool_settings(C);
1138         ARegion *ar = CTX_wm_region(C);
1139         ScrArea *sa = CTX_wm_area(C);
1140         Object *obedit = CTX_data_edit_object(C);
1141         Object *ob = CTX_data_active_object(C);
1142         bGPdata *gpd = CTX_data_gpencil_data(C);
1143         PropertyRNA *prop;
1144
1145         t->scene = sce;
1146         t->sa = sa;
1147         t->ar = ar;
1148         t->obedit = obedit;
1149         t->settings = ts;
1150         t->reports = op ? op->reports : NULL;
1151
1152         if (obedit) {
1153                 copy_m3_m4(t->obedit_mat, obedit->obmat);
1154                 normalize_m3(t->obedit_mat);
1155         }
1156
1157         t->data = NULL;
1158         t->ext = NULL;
1159
1160         t->helpline = HLP_NONE;
1161
1162         t->flag = 0;
1163
1164         t->redraw = TREDRAW_HARD;  /* redraw first time */
1165
1166         if (event) {
1167                 t->mouse.imval[0] = event->mval[0];
1168                 t->mouse.imval[1] = event->mval[1];
1169         }
1170         else {
1171                 t->mouse.imval[0] = 0;
1172                 t->mouse.imval[1] = 0;
1173         }
1174
1175         t->con.imval[0] = t->mouse.imval[0];
1176         t->con.imval[1] = t->mouse.imval[1];
1177
1178         t->mval[0] = t->mouse.imval[0];
1179         t->mval[1] = t->mouse.imval[1];
1180
1181         t->transform        = NULL;
1182         t->handleEvent      = NULL;
1183
1184         t->total            = 0;
1185
1186         t->val = 0.0f;
1187
1188         zero_v3(t->vec);
1189         zero_v3(t->center);
1190         zero_v3(t->center_global);
1191
1192         unit_m3(t->mat);
1193
1194         /* if there's an event, we're modal */
1195         if (event) {
1196                 t->flag |= T_MODAL;
1197         }
1198
1199         /* Crease needs edge flag */
1200         if (ELEM(t->mode, TFM_CREASE, TFM_BWEIGHT)) {
1201                 t->options |= CTX_EDGE;
1202         }
1203
1204         t->remove_on_cancel = false;
1205
1206         if (op && (prop = RNA_struct_find_property(op->ptr, "remove_on_cancel")) && RNA_property_is_set(op->ptr, prop)) {
1207                 if (RNA_property_boolean_get(op->ptr, prop)) {
1208                         t->remove_on_cancel = true;
1209                 }
1210         }
1211
1212         /* GPencil editing context */
1213         if ((gpd) && (gpd->flag & GP_DATA_STROKE_EDITMODE)) {
1214                 t->options |= CTX_GPENCIL_STROKES;
1215         }
1216
1217         /* Assign the space type, some exceptions for running in different mode */
1218         if (sa == NULL) {
1219                 /* background mode */
1220                 t->spacetype = SPACE_EMPTY;
1221         }
1222         else if ((ar == NULL) && (sa->spacetype == SPACE_VIEW3D)) {
1223                 /* running in the text editor */
1224                 t->spacetype = SPACE_EMPTY;
1225         }
1226         else {
1227                 /* normal operation */
1228                 t->spacetype = sa->spacetype;
1229         }
1230
1231         /* handle T_ALT_TRANSFORM initialization, we may use for different operators */
1232         if (op) {
1233                 const char *prop_id = NULL;
1234                 if (t->mode == TFM_SHRINKFATTEN) {
1235                         prop_id = "use_even_offset";
1236                 }
1237
1238                 if (prop_id && (prop = RNA_struct_find_property(op->ptr, prop_id)) &&
1239                     RNA_property_is_set(op->ptr, prop))
1240                 {
1241                         SET_FLAG_FROM_TEST(t->flag, RNA_property_boolean_get(op->ptr, prop), T_ALT_TRANSFORM);
1242                 }
1243         }
1244
1245         if (t->spacetype == SPACE_VIEW3D) {
1246                 View3D *v3d = sa->spacedata.first;
1247                 bScreen *animscreen = ED_screen_animation_playing(CTX_wm_manager(C));
1248
1249                 t->view = v3d;
1250                 t->animtimer = (animscreen) ? animscreen->animtimer : NULL;
1251
1252                 /* turn manipulator off during transform */
1253                 // FIXME: but don't do this when USING the manipulator...
1254                 if (t->flag & T_MODAL) {
1255                         t->twtype = v3d->twtype;
1256                         v3d->twtype = 0;
1257                 }
1258
1259                 if (v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
1260                 t->around = v3d->around;
1261
1262                 /* bend always uses the cursor */
1263                 if (t->mode == TFM_BEND) {
1264                         t->around = V3D_AROUND_CURSOR;
1265                 }
1266
1267                 t->current_orientation = v3d->twmode;
1268
1269                 /* exceptional case */
1270                 if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
1271                         if (ELEM(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL)) {
1272                                 const bool use_island = transdata_check_local_islands(t, t->around);
1273
1274                                 if (obedit && !use_island) {
1275                                         t->options |= CTX_NO_PET;
1276                                 }
1277                         }
1278                 }
1279
1280                 if (ob && ob->mode & OB_MODE_ALL_PAINT) {
1281                         Paint *p = BKE_paint_get_active_from_context(C);
1282                         if (p && p->brush && (p->brush->flag & BRUSH_CURVE)) {
1283                                 t->options |= CTX_PAINT_CURVE;
1284                         }
1285                 }
1286
1287                 /* initialize UV transform from */
1288                 if (op && ((prop = RNA_struct_find_property(op->ptr, "correct_uv")))) {
1289                         if (RNA_property_is_set(op->ptr, prop)) {
1290                                 if (RNA_property_boolean_get(op->ptr, prop)) {
1291                                         t->settings->uvcalc_flag |= UVCALC_TRANSFORM_CORRECT;
1292                                 }
1293                                 else {
1294                                         t->settings->uvcalc_flag &= ~UVCALC_TRANSFORM_CORRECT;
1295                                 }
1296                         }
1297                         else {
1298                                 RNA_property_boolean_set(op->ptr, prop, (t->settings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT) != 0);
1299                         }
1300                 }
1301
1302         }
1303         else if (t->spacetype == SPACE_IMAGE) {
1304                 SpaceImage *sima = sa->spacedata.first;
1305                 // XXX for now, get View2D from the active region
1306                 t->view = &ar->v2d;
1307                 t->around = sima->around;
1308
1309                 if (ED_space_image_show_uvedit(sima, t->obedit)) {
1310                         /* UV transform */
1311                 }
1312                 else if (sima->mode == SI_MODE_MASK) {
1313                         t->options |= CTX_MASK;
1314                 }
1315                 else if (sima->mode == SI_MODE_PAINT) {
1316                         Paint *p = &sce->toolsettings->imapaint.paint;
1317                         if (p->brush && (p->brush->flag & BRUSH_CURVE)) {
1318                                 t->options |= CTX_PAINT_CURVE;
1319                         }
1320                 }
1321                 /* image not in uv edit, nor in mask mode, can happen for some tools */
1322         }
1323         else if (t->spacetype == SPACE_NODE) {
1324                 // XXX for now, get View2D from the active region
1325                 t->view = &ar->v2d;
1326                 t->around = V3D_AROUND_CENTER_BOUNDS;
1327         }
1328         else if (t->spacetype == SPACE_IPO) {
1329                 SpaceIpo *sipo = sa->spacedata.first;
1330                 t->view = &ar->v2d;
1331                 t->around = sipo->around;
1332         }
1333         else if (t->spacetype == SPACE_CLIP) {
1334                 SpaceClip *sclip = sa->spacedata.first;
1335                 t->view = &ar->v2d;
1336                 t->around = sclip->around;
1337
1338                 if (ED_space_clip_check_show_trackedit(sclip))
1339                         t->options |= CTX_MOVIECLIP;
1340                 else if (ED_space_clip_check_show_maskedit(sclip))
1341                         t->options |= CTX_MASK;
1342         }
1343         else {
1344                 if (ar) {
1345                         // XXX for now, get View2D  from the active region
1346                         t->view = &ar->v2d;
1347                         // XXX for now, the center point is the midpoint of the data
1348                 }
1349                 else {
1350                         t->view = NULL;
1351                 }
1352                 t->around = V3D_AROUND_CENTER_BOUNDS;
1353         }
1354
1355         if (op && ((prop = RNA_struct_find_property(op->ptr, "constraint_orientation")) &&
1356                    RNA_property_is_set(op->ptr, prop)))
1357         {
1358                 t->current_orientation = RNA_property_enum_get(op->ptr, prop);
1359
1360                 if (t->current_orientation >= V3D_MANIP_CUSTOM + BIF_countTransformOrientation(C)) {
1361                         t->current_orientation = V3D_MANIP_GLOBAL;
1362                 }
1363         }
1364
1365         if (op && ((prop = RNA_struct_find_property(op->ptr, "release_confirm")) &&
1366                    RNA_property_is_set(op->ptr, prop)))
1367         {
1368                 if (RNA_property_boolean_get(op->ptr, prop)) {
1369                         t->flag |= T_RELEASE_CONFIRM;
1370                 }
1371         }
1372         else {
1373                 if (U.flag & USER_RELEASECONFIRM) {
1374                         t->flag |= T_RELEASE_CONFIRM;
1375                 }
1376         }
1377
1378         if (op && ((prop = RNA_struct_find_property(op->ptr, "mirror")) &&
1379                    RNA_property_is_set(op->ptr, prop)))
1380         {
1381                 if (RNA_property_boolean_get(op->ptr, prop)) {
1382                         t->flag |= T_MIRROR;
1383                         t->mirror = 1;
1384                 }
1385         }
1386         // Need stuff to take it from edit mesh or whatnot here
1387         else if (t->spacetype == SPACE_VIEW3D) {
1388                 if (t->obedit && t->obedit->type == OB_MESH && (((Mesh *)t->obedit->data)->editflag & ME_EDIT_MIRROR_X)) {
1389                         t->flag |= T_MIRROR;
1390                         t->mirror = 1;
1391                 }
1392         }
1393
1394         /* setting PET flag only if property exist in operator. Otherwise, assume it's not supported */
1395         if (op && (prop = RNA_struct_find_property(op->ptr, "proportional"))) {
1396                 if (RNA_property_is_set(op->ptr, prop)) {
1397                         t->flag |= initTransInfo_edit_pet_to_flag(RNA_property_enum_get(op->ptr, prop));
1398                 }
1399                 else {
1400                         /* use settings from scene only if modal */
1401                         if (t->flag & T_MODAL) {
1402                                 if ((t->options & CTX_NO_PET) == 0) {
1403                                         if (t->spacetype == SPACE_IPO) {
1404                                                 t->flag |= initTransInfo_edit_pet_to_flag(ts->proportional_fcurve);
1405                                         }
1406                                         else if (t->spacetype == SPACE_ACTION) {
1407                                                 t->flag |= initTransInfo_edit_pet_to_flag(ts->proportional_action);
1408                                         }
1409                                         else if (t->obedit) {
1410                                                 t->flag |= initTransInfo_edit_pet_to_flag(ts->proportional);
1411                                         }
1412                                         else if (t->options & CTX_GPENCIL_STROKES) {
1413                                                 t->flag |= initTransInfo_edit_pet_to_flag(ts->proportional);
1414                                         }
1415                                         else if (t->options & CTX_MASK) {
1416                                                 if (ts->proportional_mask) {
1417                                                         t->flag |= T_PROP_EDIT;
1418
1419                                                         if (ts->proportional == PROP_EDIT_CONNECTED) {
1420                                                                 t->flag |= T_PROP_CONNECTED;
1421                                                         }
1422                                                 }
1423                                         }
1424                                         else if (t->obedit == NULL && ts->proportional_objects) {
1425                                                 t->flag |= T_PROP_EDIT;
1426                                         }
1427                                 }
1428                         }
1429                 }
1430
1431                 if (op && ((prop = RNA_struct_find_property(op->ptr, "proportional_size")) &&
1432                            RNA_property_is_set(op->ptr, prop)))
1433                 {
1434                         t->prop_size = RNA_property_float_get(op->ptr, prop);
1435                 }
1436                 else {
1437                         t->prop_size = ts->proportional_size;
1438                 }
1439
1440
1441                 /* TRANSFORM_FIX_ME rna restrictions */
1442                 if (t->prop_size <= 0.00001f) {
1443                         printf("Proportional size (%f) under 0.00001, resetting to 1!\n", t->prop_size);
1444                         t->prop_size = 1.0f;
1445                 }
1446
1447                 if (op && ((prop = RNA_struct_find_property(op->ptr, "proportional_edit_falloff")) &&
1448                            RNA_property_is_set(op->ptr, prop)))
1449                 {
1450                         t->prop_mode = RNA_property_enum_get(op->ptr, prop);
1451                 }
1452                 else {
1453                         t->prop_mode = ts->prop_mode;
1454                 }
1455         }
1456         else { /* add not pet option to context when not available */
1457                 t->options |= CTX_NO_PET;
1458         }
1459
1460         // Mirror is not supported with PET, turn it off.
1461 #if 0
1462         if (t->flag & T_PROP_EDIT) {
1463                 t->flag &= ~T_MIRROR;
1464         }
1465 #endif
1466
1467         setTransformViewAspect(t, t->aspect);
1468
1469         if (op && (prop = RNA_struct_find_property(op->ptr, "center_override")) && RNA_property_is_set(op->ptr, prop)) {
1470                 RNA_property_float_get_array(op->ptr, prop, t->center);
1471                 mul_v3_v3(t->center, t->aspect);
1472                 t->flag |= T_OVERRIDE_CENTER;
1473         }
1474
1475         setTransformViewMatrices(t);
1476         initNumInput(&t->num);
1477 }
1478
1479 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
1480 void postTrans(bContext *C, TransInfo *t)
1481 {
1482         TransData *td;
1483
1484         if (t->draw_handle_view)
1485                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_view);
1486         if (t->draw_handle_apply)
1487                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_apply);
1488         if (t->draw_handle_pixel)
1489                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_pixel);
1490         if (t->draw_handle_cursor)
1491                 WM_paint_cursor_end(CTX_wm_manager(C), t->draw_handle_cursor);
1492
1493         /* Free all custom-data */
1494         {
1495                 TransCustomData *custom_data = &t->custom.first_elem;
1496                 for (int i = 0; i < TRANS_CUSTOM_DATA_ELEM_MAX; i++, custom_data++) {
1497                         if (custom_data->free_cb) {
1498                                 /* Can take over freeing t->data and data2d etc... */
1499                                 custom_data->free_cb(t, custom_data);
1500                                 BLI_assert(custom_data->data == NULL);
1501                         }
1502                         else if ((custom_data->data != NULL) && custom_data->use_free) {
1503                                 MEM_freeN(custom_data->data);
1504                                 custom_data->data = NULL;
1505                         }
1506                 }
1507         }
1508
1509         /* postTrans can be called when nothing is selected, so data is NULL already */
1510         if (t->data) {
1511
1512                 /* free data malloced per trans-data */
1513                 if ((t->obedit && ELEM(t->obedit->type, OB_CURVE, OB_SURF)) ||
1514                     (t->spacetype == SPACE_IPO))
1515                 {
1516                         int a;
1517                         for (a = 0, td = t->data; a < t->total; a++, td++) {
1518                                 if (td->flag & TD_BEZTRIPLE) {
1519                                         MEM_freeN(td->hdata);
1520                                 }
1521                         }
1522                 }
1523                 MEM_freeN(t->data);
1524         }
1525
1526         BLI_freelistN(&t->tsnap.points);
1527
1528         if (t->ext) MEM_freeN(t->ext);
1529         if (t->data2d) {
1530                 MEM_freeN(t->data2d);
1531                 t->data2d = NULL;
1532         }
1533
1534         if (t->spacetype == SPACE_IMAGE) {
1535                 if (t->options & (CTX_MASK | CTX_PAINT_CURVE)) {
1536                         /* pass */
1537                 }
1538                 else {
1539                         SpaceImage *sima = t->sa->spacedata.first;
1540                         if (sima->flag & SI_LIVE_UNWRAP)
1541                                 ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1542                 }
1543         }
1544         else if (t->spacetype == SPACE_VIEW3D) {
1545                 View3D *v3d = t->sa->spacedata.first;
1546                 /* restore manipulator */
1547                 if (t->flag & T_MODAL) {
1548                         v3d->twtype = t->twtype;
1549                 }
1550         }
1551
1552         if (t->mouse.data) {
1553                 MEM_freeN(t->mouse.data);
1554         }
1555
1556         freeSnapping(t);
1557 }
1558
1559 void applyTransObjects(TransInfo *t)
1560 {
1561         TransData *td;
1562
1563         for (td = t->data; td < t->data + t->total; td++) {
1564                 copy_v3_v3(td->iloc, td->loc);
1565                 if (td->ext->rot) {
1566                         copy_v3_v3(td->ext->irot, td->ext->rot);
1567                 }
1568                 if (td->ext->size) {
1569                         copy_v3_v3(td->ext->isize, td->ext->size);
1570                 }
1571         }
1572         recalcData(t);
1573 }
1574
1575 static void restoreElement(TransData *td)
1576 {
1577         /* TransData for crease has no loc */
1578         if (td->loc) {
1579                 copy_v3_v3(td->loc, td->iloc);
1580         }
1581         if (td->val) {
1582                 *td->val = td->ival;
1583         }
1584
1585         if (td->ext && (td->flag & TD_NO_EXT) == 0) {
1586                 if (td->ext->rot) {
1587                         copy_v3_v3(td->ext->rot, td->ext->irot);
1588                 }
1589                 if (td->ext->rotAngle) {
1590                         *td->ext->rotAngle = td->ext->irotAngle;
1591                 }
1592                 if (td->ext->rotAxis) {
1593                         copy_v3_v3(td->ext->rotAxis, td->ext->irotAxis);
1594                 }
1595                 /* XXX, drotAngle & drotAxis not used yet */
1596                 if (td->ext->size) {
1597                         copy_v3_v3(td->ext->size, td->ext->isize);
1598                 }
1599                 if (td->ext->quat) {
1600                         copy_qt_qt(td->ext->quat, td->ext->iquat);
1601                 }
1602         }
1603
1604         if (td->flag & TD_BEZTRIPLE) {
1605                 *(td->hdata->h1) = td->hdata->ih1;
1606                 *(td->hdata->h2) = td->hdata->ih2;
1607         }
1608 }
1609
1610 void restoreTransObjects(TransInfo *t)
1611 {
1612         TransData *td;
1613         TransData2D *td2d;
1614
1615         for (td = t->data; td < t->data + t->total; td++) {
1616                 restoreElement(td);
1617         }
1618
1619         for (td2d = t->data2d; t->data2d && td2d < t->data2d + t->total; td2d++) {
1620                 if (td2d->h1) {
1621                         td2d->h1[0] = td2d->ih1[0];
1622                         td2d->h1[1] = td2d->ih1[1];
1623                 }
1624                 if (td2d->h2) {
1625                         td2d->h2[0] = td2d->ih2[0];
1626                         td2d->h2[1] = td2d->ih2[1];
1627                 }
1628         }
1629
1630         unit_m3(t->mat);
1631
1632         recalcData(t);
1633 }
1634
1635 void calculateCenter2D(TransInfo *t)
1636 {
1637         BLI_assert(!is_zero_v3(t->aspect));
1638
1639         if (t->flag & (T_EDIT | T_POSE)) {
1640                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1641                 float vec[3];
1642
1643                 copy_v3_v3(vec, t->center);
1644                 mul_m4_v3(ob->obmat, vec);
1645                 projectFloatView(t, vec, t->center2d);
1646         }
1647         else {
1648                 projectFloatView(t, t->center, t->center2d);
1649         }
1650 }
1651
1652 void calculateCenterGlobal(
1653         TransInfo *t, const float center_local[3],
1654         float r_center_global[3])
1655 {
1656         /* setting constraint center */
1657         /* note, init functions may over-ride t->center */
1658         if (t->flag & (T_EDIT | T_POSE)) {
1659                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1660                 mul_v3_m4v3(r_center_global, ob->obmat, center_local);
1661         }
1662         else {
1663                 copy_v3_v3(r_center_global, center_local);
1664         }
1665 }
1666
1667 void calculateCenterCursor(TransInfo *t, float r_center[3])
1668 {
1669         const float *cursor;
1670
1671         cursor = ED_view3d_cursor3d_get(t->scene, t->view);
1672         copy_v3_v3(r_center, cursor);
1673
1674         /* If edit or pose mode, move cursor in local space */
1675         if (t->flag & (T_EDIT | T_POSE)) {
1676                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1677                 float mat[3][3], imat[3][3];
1678
1679                 sub_v3_v3v3(r_center, r_center, ob->obmat[3]);
1680                 copy_m3_m4(mat, ob->obmat);
1681                 invert_m3_m3(imat, mat);
1682                 mul_m3_v3(imat, r_center);
1683         }
1684         else if (t->options & CTX_PAINT_CURVE) {
1685                 if (ED_view3d_project_float_global(t->ar, cursor, r_center, V3D_PROJ_TEST_NOP) != V3D_PROJ_RET_OK) {
1686                         r_center[0] = t->ar->winx / 2.0f;
1687                         r_center[1] = t->ar->winy / 2.0f;
1688                 }
1689                 r_center[2] = 0.0f;
1690         }
1691 }
1692
1693 void calculateCenterCursor2D(TransInfo *t, float r_center[2])
1694 {
1695         const float *cursor = NULL;
1696
1697         if (t->spacetype == SPACE_IMAGE) {
1698                 SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
1699                 cursor = sima->cursor;
1700         }
1701         else if (t->spacetype == SPACE_CLIP) {
1702                 SpaceClip *space_clip = (SpaceClip *) t->sa->spacedata.first;
1703                 cursor = space_clip->cursor;
1704         }
1705
1706         if (cursor) {
1707                 if (t->options & CTX_MASK) {
1708                         float co[2];
1709
1710                         if (t->spacetype == SPACE_IMAGE) {
1711                                 SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
1712                                 BKE_mask_coord_from_image(sima->image, &sima->iuser, co, cursor);
1713                         }
1714                         else if (t->spacetype == SPACE_CLIP) {
1715                                 SpaceClip *space_clip = (SpaceClip *) t->sa->spacedata.first;
1716                                 BKE_mask_coord_from_movieclip(space_clip->clip, &space_clip->user, co, cursor);
1717                         }
1718                         else {
1719                                 BLI_assert(!"Shall not happen");
1720                         }
1721
1722                         r_center[0] = co[0] * t->aspect[0];
1723                         r_center[1] = co[1] * t->aspect[1];
1724                 }
1725                 else if (t->options & CTX_PAINT_CURVE) {
1726                         if (t->spacetype == SPACE_IMAGE) {
1727                                 r_center[0] = UI_view2d_view_to_region_x(&t->ar->v2d, cursor[0]);
1728                                 r_center[1] = UI_view2d_view_to_region_y(&t->ar->v2d, cursor[1]);
1729                         }
1730                 }
1731                 else {
1732                         r_center[0] = cursor[0] * t->aspect[0];
1733                         r_center[1] = cursor[1] * t->aspect[1];
1734                 }
1735         }
1736 }
1737
1738 void calculateCenterCursorGraph2D(TransInfo *t, float r_center[2])
1739 {
1740         SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
1741         Scene *scene = t->scene;
1742
1743         /* cursor is combination of current frame, and graph-editor cursor value */
1744         if (sipo->mode == SIPO_MODE_DRIVERS) {
1745                 r_center[0] = sipo->cursorTime;
1746                 r_center[1] = sipo->cursorVal;
1747         }
1748         else {
1749                 r_center[0] = (float)(scene->r.cfra);
1750                 r_center[1] = sipo->cursorVal;
1751         }
1752 }
1753
1754 void calculateCenterMedian(TransInfo *t, float r_center[3])
1755 {
1756         float partial[3] = {0.0f, 0.0f, 0.0f};
1757         int total = 0;
1758         int i;
1759
1760         for (i = 0; i < t->total; i++) {
1761                 if (t->data[i].flag & TD_SELECTED) {
1762                         if (!(t->data[i].flag & TD_NOCENTER)) {
1763                                 add_v3_v3(partial, t->data[i].center);
1764                                 total++;
1765                         }
1766                 }
1767         }
1768         if (total) {
1769                 mul_v3_fl(partial, 1.0f / (float)total);
1770         }
1771         copy_v3_v3(r_center, partial);
1772 }
1773
1774 void calculateCenterBound(TransInfo *t, float r_center[3])
1775 {
1776         float max[3];
1777         float min[3];
1778         int i;
1779         for (i = 0; i < t->total; i++) {
1780                 if (i) {
1781                         if (t->data[i].flag & TD_SELECTED) {
1782                                 if (!(t->data[i].flag & TD_NOCENTER))
1783                                         minmax_v3v3_v3(min, max, t->data[i].center);
1784                         }
1785                 }
1786                 else {
1787                         copy_v3_v3(max, t->data[i].center);
1788                         copy_v3_v3(min, t->data[i].center);
1789                 }
1790         }
1791         mid_v3_v3v3(r_center, min, max);
1792 }
1793
1794 /**
1795  * \param select_only: only get active center from data being transformed.
1796  */
1797 bool calculateCenterActive(TransInfo *t, bool select_only, float r_center[3])
1798 {
1799         bool ok = false;
1800
1801         if (t->obedit) {
1802                 if (ED_object_editmode_calc_active_center(t->obedit, select_only, r_center)) {
1803                         ok = true;
1804                 }
1805         }
1806         else if (t->flag & T_POSE) {
1807                 Scene *scene = t->scene;
1808                 Object *ob = OBACT;
1809                 if (ob) {
1810                         bPoseChannel *pchan = BKE_pose_channel_active(ob);
1811                         if (pchan && (!select_only || (pchan->bone->flag & BONE_SELECTED))) {
1812                                 copy_v3_v3(r_center, pchan->pose_head);
1813                                 ok = true;
1814                         }
1815                 }
1816         }
1817         else if (t->options & CTX_PAINT_CURVE) {
1818                 Paint *p = BKE_paint_get_active(t->scene);
1819                 Brush *br = p->brush;
1820                 PaintCurve *pc = br->paint_curve;
1821                 copy_v3_v3(r_center, pc->points[pc->add_index - 1].bez.vec[1]);
1822                 r_center[2] = 0.0f;
1823                 ok = true;
1824         }
1825         else {
1826                 /* object mode */
1827                 Scene *scene = t->scene;
1828                 Object *ob = OBACT;
1829                 if (ob && (!select_only || (ob->flag & SELECT))) {
1830                         copy_v3_v3(r_center, ob->obmat[3]);
1831                         ok = true;
1832                 }
1833         }
1834
1835         return ok;
1836 }
1837
1838 static void calculateCenter_FromAround(TransInfo *t, int around, float r_center[3])
1839 {
1840         switch (around) {
1841                 case V3D_AROUND_CENTER_BOUNDS:
1842                         calculateCenterBound(t, r_center);
1843                         break;
1844                 case V3D_AROUND_CENTER_MEDIAN:
1845                         calculateCenterMedian(t, r_center);
1846                         break;
1847                 case V3D_AROUND_CURSOR:
1848                         if (ELEM(t->spacetype, SPACE_IMAGE, SPACE_CLIP))
1849                                 calculateCenterCursor2D(t, r_center);
1850                         else if (t->spacetype == SPACE_IPO)
1851                                 calculateCenterCursorGraph2D(t, r_center);
1852                         else
1853                                 calculateCenterCursor(t, r_center);
1854                         break;
1855                 case V3D_AROUND_LOCAL_ORIGINS:
1856                         /* Individual element center uses median center for helpline and such */
1857                         calculateCenterMedian(t, r_center);
1858                         break;
1859                 case V3D_AROUND_ACTIVE:
1860                 {
1861                         if (calculateCenterActive(t, false, r_center)) {
1862                                 /* pass */
1863                         }
1864                         else {
1865                                 /* fallback */
1866                                 calculateCenterMedian(t, r_center);
1867                         }
1868                         break;
1869                 }
1870         }
1871 }
1872
1873 void calculateCenter(TransInfo *t)
1874 {
1875         if ((t->flag & T_OVERRIDE_CENTER) == 0) {
1876                 calculateCenter_FromAround(t, t->around, t->center);
1877         }
1878         calculateCenterGlobal(t, t->center, t->center_global);
1879
1880         /* avoid calculating again */
1881         {
1882                 TransCenterData *cd = &t->center_cache[t->around];
1883                 copy_v3_v3(cd->local, t->center);
1884                 copy_v3_v3(cd->global, t->center_global);
1885                 cd->is_set = true;
1886         }
1887
1888         calculateCenter2D(t);
1889
1890         /* for panning from cameraview */
1891         if ((t->flag & T_OBJECT) && (t->flag & T_OVERRIDE_CENTER) == 0) {
1892                 if (t->spacetype == SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW) {
1893
1894                         if (t->flag & T_CAMERA) {
1895                                 float axis[3];
1896                                 /* persinv is nasty, use viewinv instead, always right */
1897                                 copy_v3_v3(axis, t->viewinv[2]);
1898                                 normalize_v3(axis);
1899
1900                                 /* 6.0 = 6 grid units */
1901                                 axis[0] = t->center[0] - 6.0f * axis[0];
1902                                 axis[1] = t->center[1] - 6.0f * axis[1];
1903                                 axis[2] = t->center[2] - 6.0f * axis[2];
1904
1905                                 projectFloatView(t, axis, t->center2d);
1906
1907                                 /* rotate only needs correct 2d center, grab needs ED_view3d_calc_zfac() value */
1908                                 if (t->mode == TFM_TRANSLATION) {
1909                                         copy_v3_v3(t->center, axis);
1910                                         copy_v3_v3(t->center_global, t->center);
1911                                 }
1912                         }
1913                 }
1914         }
1915
1916         if (t->spacetype == SPACE_VIEW3D) {
1917                 /* ED_view3d_calc_zfac() defines a factor for perspective depth correction, used in ED_view3d_win_to_delta() */
1918
1919                 /* zfac is only used convertViewVec only in cases operator was invoked in RGN_TYPE_WINDOW
1920                  * and never used in other cases.
1921                  *
1922                  * We need special case here as well, since ED_view3d_calc_zfac will crash when called
1923                  * for a region different from RGN_TYPE_WINDOW.
1924                  */
1925                 if (t->ar->regiontype == RGN_TYPE_WINDOW) {
1926                         t->zfac = ED_view3d_calc_zfac(t->ar->regiondata, t->center_global, NULL);
1927                 }
1928                 else {
1929                         t->zfac = 0.0f;
1930                 }
1931         }
1932 }
1933
1934 BLI_STATIC_ASSERT(ARRAY_SIZE(((TransInfo *)NULL)->center_cache) == (V3D_AROUND_ACTIVE + 1), "test size");
1935
1936 /**
1937  * Lazy initialize transform center data, when we need to access center values from other types.
1938  */
1939 const TransCenterData *transformCenter_from_type(TransInfo *t, int around)
1940 {
1941         BLI_assert(around <= V3D_AROUND_ACTIVE);
1942         TransCenterData *cd = &t->center_cache[around];
1943         if (cd->is_set == false) {
1944                 calculateCenter_FromAround(t, around, cd->local);
1945                 calculateCenterGlobal(t, cd->local, cd->global);
1946                 cd->is_set = true;
1947         }
1948         return cd;
1949 }
1950
1951 void calculatePropRatio(TransInfo *t)
1952 {
1953         TransData *td = t->data;
1954         int i;
1955         float dist;
1956         const bool connected = (t->flag & T_PROP_CONNECTED) != 0;
1957
1958         t->proptext[0] = '\0';
1959
1960         if (t->flag & T_PROP_EDIT) {
1961                 const char *pet_id = NULL;
1962                 for (i = 0; i < t->total; i++, td++) {
1963                         if (td->flag & TD_SELECTED) {
1964                                 td->factor = 1.0f;
1965                         }
1966                         else if (t->flag & T_MIRROR && td->loc[0] * t->mirror < -0.00001f) {
1967                                 td->flag |= TD_SKIP;
1968                                 td->factor = 0.0f;
1969                                 restoreElement(td);
1970                         }
1971                         else if ((connected && (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size)) ||
1972                                  (connected == 0 && td->rdist > t->prop_size))
1973                         {
1974                                 /*
1975                                  * The elements are sorted according to their dist member in the array,
1976                                  * that means we can stop when it finds one element outside of the propsize.
1977                                  * do not set 'td->flag |= TD_NOACTION', the prop circle is being changed.
1978                                  */
1979
1980                                 td->factor = 0.0f;
1981                                 restoreElement(td);
1982                         }
1983                         else {
1984                                 /* Use rdist for falloff calculations, it is the real distance */
1985                                 td->flag &= ~TD_NOACTION;
1986
1987                                 if (connected)
1988                                         dist = (t->prop_size - td->dist) / t->prop_size;
1989                                 else
1990                                         dist = (t->prop_size - td->rdist) / t->prop_size;
1991
1992                                 /*
1993                                  * Clamp to positive numbers.
1994                                  * Certain corner cases with connectivity and individual centers
1995                                  * can give values of rdist larger than propsize.
1996                                  */
1997                                 if (dist < 0.0f)
1998                                         dist = 0.0f;
1999
2000                                 switch (t->prop_mode) {
2001                                         case PROP_SHARP:
2002                                                 td->factor = dist * dist;
2003                                                 break;
2004                                         case PROP_SMOOTH:
2005                                                 td->factor = 3.0f * dist * dist - 2.0f * dist * dist * dist;
2006                                                 break;
2007                                         case PROP_ROOT:
2008                                                 td->factor = sqrtf(dist);
2009                                                 break;
2010                                         case PROP_LIN:
2011                                                 td->factor = dist;
2012                                                 break;
2013                                         case PROP_CONST:
2014                                                 td->factor = 1.0f;
2015                                                 break;
2016                                         case PROP_SPHERE:
2017                                                 td->factor = sqrtf(2 * dist - dist * dist);
2018                                                 break;
2019                                         case PROP_RANDOM:
2020                                                 td->factor = BLI_frand() * dist;
2021                                                 break;
2022                                         case PROP_INVSQUARE:
2023                                                 td->factor = dist * (2.0f - dist);
2024                                                 break;
2025                                         default:
2026                                                 td->factor = 1;
2027                                                 break;
2028                                 }
2029                         }
2030                 }
2031                 switch (t->prop_mode) {
2032                         case PROP_SHARP:
2033                                 pet_id = N_("(Sharp)");
2034                                 break;
2035                         case PROP_SMOOTH:
2036                                 pet_id = N_("(Smooth)");
2037                                 break;
2038                         case PROP_ROOT:
2039                                 pet_id = N_("(Root)");
2040                                 break;
2041                         case PROP_LIN:
2042                                 pet_id = N_("(Linear)");
2043                                 break;
2044                         case PROP_CONST:
2045                                 pet_id = N_("(Constant)");
2046                                 break;
2047                         case PROP_SPHERE:
2048                                 pet_id = N_("(Sphere)");
2049                                 break;
2050                         case PROP_RANDOM:
2051                                 pet_id = N_("(Random)");
2052                                 break;
2053                         case PROP_INVSQUARE:
2054                                 pet_id = N_("(InvSquare)");
2055                                 break;
2056                         default:
2057                                 break;
2058                 }
2059
2060                 if (pet_id) {
2061                         BLI_strncpy(t->proptext, IFACE_(pet_id), sizeof(t->proptext));
2062                 }
2063         }
2064         else {
2065                 for (i = 0; i < t->total; i++, td++) {
2066                         td->factor = 1.0;
2067                 }
2068         }
2069 }
2070
2071 /**
2072  * Rotate an element, low level code, ignore protected channels.
2073  * (use for objects or pose-bones)
2074  * Similar to #ElementRotation.
2075  */
2076 void transform_data_ext_rotate(TransData *td, float mat[3][3], bool use_drot)
2077 {
2078         float totmat[3][3];
2079         float smat[3][3];
2080         float fmat[3][3];
2081         float obmat[3][3];
2082
2083         float dmat[3][3];  /* delta rotation */
2084         float dmat_inv[3][3];
2085
2086         mul_m3_m3m3(totmat, mat, td->mtx);
2087         mul_m3_m3m3(smat, td->smtx, mat);
2088
2089         /* logic from BKE_object_rot_to_mat3 */
2090         if (use_drot) {
2091                 if (td->ext->rotOrder > 0) {
2092                         eulO_to_mat3(dmat, td->ext->drot, td->ext->rotOrder);
2093                 }
2094                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2095 #if 0
2096                         axis_angle_to_mat3(dmat, td->ext->drotAxis, td->ext->drotAngle);
2097 #else
2098                         unit_m3(dmat);
2099 #endif
2100                 }
2101                 else {
2102                         float tquat[4];
2103                         normalize_qt_qt(tquat, td->ext->dquat);
2104                         quat_to_mat3(dmat, tquat);
2105                 }
2106
2107                 invert_m3_m3(dmat_inv, dmat);
2108         }
2109
2110
2111         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2112                 float quat[4];
2113
2114                 /* calculate the total rotatation */
2115                 quat_to_mat3(obmat, td->ext->iquat);
2116                 if (use_drot) {
2117                         mul_m3_m3m3(obmat, dmat, obmat);
2118                 }
2119
2120                 /* mat = transform, obmat = object rotation */
2121                 mul_m3_m3m3(fmat, smat, obmat);
2122
2123                 if (use_drot) {
2124                         mul_m3_m3m3(fmat, dmat_inv, fmat);
2125                 }
2126
2127                 mat3_to_quat(quat, fmat);
2128
2129                 /* apply */
2130                 copy_qt_qt(td->ext->quat, quat);
2131         }
2132         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2133                 float axis[3], angle;
2134
2135                 /* calculate the total rotatation */
2136                 axis_angle_to_mat3(obmat, td->ext->irotAxis, td->ext->irotAngle);
2137                 if (use_drot) {
2138                         mul_m3_m3m3(obmat, dmat, obmat);
2139                 }
2140
2141                 /* mat = transform, obmat = object rotation */
2142                 mul_m3_m3m3(fmat, smat, obmat);
2143
2144                 if (use_drot) {
2145                         mul_m3_m3m3(fmat, dmat_inv, fmat);
2146                 }
2147
2148                 mat3_to_axis_angle(axis, &angle, fmat);
2149
2150                 /* apply */
2151                 copy_v3_v3(td->ext->rotAxis, axis);
2152                 *td->ext->rotAngle = angle;
2153         }
2154         else {
2155                 float eul[3];
2156
2157                 /* calculate the total rotatation */
2158                 eulO_to_mat3(obmat, td->ext->irot, td->ext->rotOrder);
2159                 if (use_drot) {
2160                         mul_m3_m3m3(obmat, dmat, obmat);
2161                 }
2162
2163                 /* mat = transform, obmat = object rotation */
2164                 mul_m3_m3m3(fmat, smat, obmat);
2165
2166                 if (use_drot) {
2167                         mul_m3_m3m3(fmat, dmat_inv, fmat);
2168                 }
2169
2170                 mat3_to_compatible_eulO(eul, td->ext->rot, td->ext->rotOrder, fmat);
2171
2172                 /* apply */
2173                 copy_v3_v3(td->ext->rot, eul);
2174         }
2175 }