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