Proxies building progress bar used to be displayed wrong in cases
[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 "BLO_sys_types.h" /* for intptr_t support */
38
39 #include "DNA_anim_types.h"
40 #include "DNA_armature_types.h"
41 #include "DNA_lattice_types.h"
42 #include "DNA_screen_types.h"
43 #include "DNA_space_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_view3d_types.h"
49 #include "DNA_modifier_types.h"
50 #include "DNA_movieclip_types.h"
51 #include "DNA_mask_types.h"
52
53 #include "BLI_math.h"
54 #include "BLI_blenlib.h"
55 #include "BLI_rand.h"
56 #include "BLI_utildefines.h"
57
58 #include "RNA_access.h"
59
60 #include "BIF_gl.h"
61 #include "BIF_glutil.h"
62
63 #include "BKE_animsys.h"
64 #include "BKE_action.h"
65 #include "BKE_armature.h"
66 #include "BKE_curve.h"
67 #include "BKE_depsgraph.h"
68 #include "BKE_displist.h"
69 #include "BKE_fcurve.h"
70 #include "BKE_lattice.h"
71 #include "BKE_mesh.h"
72 #include "BKE_nla.h"
73 #include "BKE_context.h"
74 #include "BKE_tessmesh.h"
75 #include "BKE_tracking.h"
76
77 #include "ED_anim_api.h"
78 #include "ED_armature.h"
79 #include "ED_image.h"
80 #include "ED_keyframing.h"
81 #include "ED_markers.h"
82 #include "ED_mesh.h"
83 #include "ED_particle.h"
84 #include "ED_screen_types.h"
85 #include "ED_space_api.h"
86 #include "ED_uvedit.h"
87 #include "ED_view3d.h"
88 #include "ED_curve.h" /* for curve_editnurbs */
89 #include "ED_clip.h"
90 #include "ED_screen.h"
91
92 #include "WM_types.h"
93 #include "WM_api.h"
94
95 #include "UI_resources.h"
96
97 #include "transform.h"
98
99 /* ************************** Functions *************************** */
100
101 void getViewVector(TransInfo *t, float coord[3], float vec[3])
102 {
103         if (t->persp != RV3D_ORTHO) {
104                 float p1[4], p2[4];
105                 
106                 copy_v3_v3(p1, coord);
107                 p1[3] = 1.0f;
108                 copy_v3_v3(p2, p1);
109                 p2[3] = 1.0f;
110                 mul_m4_v4(t->viewmat, p2);
111                 
112                 p2[0] = 2.0f * p2[0];
113                 p2[1] = 2.0f * p2[1];
114                 p2[2] = 2.0f * p2[2];
115                 
116                 mul_m4_v4(t->viewinv, p2);
117                 
118                 sub_v3_v3v3(vec, p1, p2);
119         }
120         else {
121                 copy_v3_v3(vec, t->viewinv[2]);
122         }
123         normalize_v3(vec);
124 }
125
126 /* ************************** GENERICS **************************** */
127
128 static void clipMirrorModifier(TransInfo *t, Object *ob)
129 {
130         ModifierData *md = ob->modifiers.first;
131         float tolerance[3] = {0.0f, 0.0f, 0.0f};
132         int axis = 0;
133         
134         for (; md; md = md->next) {
135                 if ((md->type == eModifierType_Mirror) && (md->mode & eModifierMode_Realtime)) {
136                         MirrorModifierData *mmd = (MirrorModifierData *) md;
137                         
138                         if (mmd->flag & MOD_MIR_CLIPPING) {
139                                 axis = 0;
140                                 if (mmd->flag & MOD_MIR_AXIS_X) {
141                                         axis |= 1;
142                                         tolerance[0] = mmd->tolerance;
143                                 }
144                                 if (mmd->flag & MOD_MIR_AXIS_Y) {
145                                         axis |= 2;
146                                         tolerance[1] = mmd->tolerance;
147                                 }
148                                 if (mmd->flag & MOD_MIR_AXIS_Z) {
149                                         axis |= 4;
150                                         tolerance[2] = mmd->tolerance;
151                                 }
152                                 if (axis) {
153                                         float mtx[4][4], imtx[4][4];
154                                         int i;
155                                         TransData *td = t->data;
156                                         
157                                         if (mmd->mirror_ob) {
158                                                 float obinv[4][4];
159                                                 
160                                                 invert_m4_m4(obinv, mmd->mirror_ob->obmat);
161                                                 mult_m4_m4m4(mtx, obinv, ob->obmat);
162                                                 invert_m4_m4(imtx, mtx);
163                                         }
164                                         
165                                         for (i = 0; i < t->total; i++, td++) {
166                                                 int clip;
167                                                 float loc[3], iloc[3];
168                                                 
169                                                 if (td->flag & TD_NOACTION)
170                                                         break;
171                                                 if (td->loc == NULL)
172                                                         break;
173                                                 
174                                                 if (td->flag & TD_SKIP)
175                                                         continue;
176                                                 
177                                                 copy_v3_v3(loc,  td->loc);
178                                                 copy_v3_v3(iloc, td->iloc);
179                                                 
180                                                 if (mmd->mirror_ob) {
181                                                         mul_m4_v3(mtx, loc);
182                                                         mul_m4_v3(mtx, iloc);
183                                                 }
184                                                 
185                                                 clip = 0;
186                                                 if (axis & 1) {
187                                                         if (fabsf(iloc[0]) <= tolerance[0] ||
188                                                             loc[0] * iloc[0] < 0.0f)
189                                                         {
190                                                                 loc[0] = 0.0f;
191                                                                 clip = 1;
192                                                         }
193                                                 }
194                                                 
195                                                 if (axis & 2) {
196                                                         if (fabsf(iloc[1]) <= tolerance[1] ||
197                                                             loc[1] * iloc[1] < 0.0f)
198                                                         {
199                                                                 loc[1] = 0.0f;
200                                                                 clip = 1;
201                                                         }
202                                                 }
203                                                 if (axis & 4) {
204                                                         if (fabsf(iloc[2]) <= tolerance[2] ||
205                                                             loc[2] * iloc[2] < 0.0f)
206                                                         {
207                                                                 loc[2] = 0.0f;
208                                                                 clip = 1;
209                                                         }
210                                                 }
211                                                 if (clip) {
212                                                         if (mmd->mirror_ob) {
213                                                                 mul_m4_v3(imtx, loc);
214                                                         }
215                                                         copy_v3_v3(td->loc, loc);
216                                                 }
217                                         }
218                                 }
219                                 
220                         }
221                 }
222         }
223 }
224
225 /* assumes obedit set to mesh object */
226 static void editbmesh_apply_to_mirror(TransInfo *t)
227 {
228         TransData *td = t->data;
229         BMVert *eve;
230         int i;
231         
232         for (i = 0; i < t->total; i++, td++) {
233                 if (td->flag & TD_NOACTION)
234                         break;
235                 if (td->loc == NULL)
236                         break;
237                 if (td->flag & TD_SKIP)
238                         continue;
239                 
240                 eve = td->extra;
241                 if (eve) {
242                         eve->co[0] = -td->loc[0];
243                         eve->co[1] = td->loc[1];
244                         eve->co[2] = td->loc[2];
245                 }
246                 
247                 if (td->flag & TD_MIRROR_EDGE) {
248                         td->loc[0] = 0;
249                 }
250         }
251 }
252
253 /* for the realtime animation recording feature, handle overlapping data */
254 static void animrecord_check_state(Scene *scene, ID *id, wmTimer *animtimer)
255 {
256         ScreenAnimData *sad = (animtimer) ? animtimer->customdata : NULL;
257         
258         /* sanity checks */
259         if (ELEM3(NULL, scene, id, sad))
260                 return;
261         
262         /* check if we need a new strip if:
263          *  - if animtimer is running
264          *      - we're not only keying for available channels
265          *      - the option to add new actions for each round is not enabled
266          */
267         if (IS_AUTOKEY_FLAG(scene, INSERTAVAIL) == 0 && (scene->toolsettings->autokey_flag & ANIMRECORD_FLAG_WITHNLA)) {
268                 /* if playback has just looped around, we need to add a new NLA track+strip to allow a clean pass to occur */
269                 if ((sad) && (sad->flag & ANIMPLAY_FLAG_JUMPED)) {
270                         AnimData *adt = BKE_animdata_from_id(id);
271                         
272                         /* perform push-down manually with some differences 
273                          * NOTE: BKE_nla_action_pushdown() sync warning...
274                          */
275                         if ((adt->action) && !(adt->flag & ADT_NLA_EDIT_ON)) {
276                                 float astart, aend;
277                                 
278                                 /* only push down if action is more than 1-2 frames long */
279                                 calc_action_range(adt->action, &astart, &aend, 1);
280                                 if (aend > astart + 2.0f) {
281                                         NlaStrip *strip = add_nlastrip_to_stack(adt, adt->action);
282                                         
283                                         /* clear reference to action now that we've pushed it onto the stack */
284                                         adt->action->id.us--;
285                                         adt->action = NULL;
286                                         
287                                         /* adjust blending + extend so that they will behave correctly */
288                                         strip->extendmode = NLASTRIP_EXTEND_NOTHING;
289                                         strip->flag &= ~(NLASTRIP_FLAG_AUTO_BLENDS | NLASTRIP_FLAG_SELECT | NLASTRIP_FLAG_ACTIVE);
290                                         
291                                         /* also, adjust the AnimData's action extend mode to be on 
292                                          * 'nothing' so that previous result still play 
293                                          */
294                                         adt->act_extendmode = NLASTRIP_EXTEND_NOTHING;
295                                 }
296                         }
297                 }
298         }
299 }
300
301 static int fcu_test_selected(FCurve *fcu)
302 {
303         BezTriple *bezt = fcu->bezt;
304         unsigned int i;
305
306         if (bezt == NULL) /* ignore baked */
307                 return 0;
308
309         for (i = 0; i < fcu->totvert; i++, bezt++) {
310                 if (BEZSELECTED(bezt)) return 1;
311         }
312
313         return 0;
314 }
315
316 /* helper for recalcData() - for Action Editor transforms */
317 static void recalcData_actedit(TransInfo *t)
318 {
319         Scene *scene = t->scene;
320         SpaceAction *saction = (SpaceAction *)t->sa->spacedata.first;
321         
322         bAnimContext ac = {NULL};
323         ListBase anim_data = {NULL, NULL};
324         bAnimListElem *ale;
325         int filter;
326         
327         /* initialize relevant anim-context 'context' data from TransInfo data */
328         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
329         ac.scene = t->scene;
330         ac.obact = OBACT;
331         ac.sa = t->sa;
332         ac.ar = t->ar;
333         ac.sl = (t->sa) ? t->sa->spacedata.first : NULL;
334         ac.spacetype = (t->sa) ? t->sa->spacetype : 0;
335         ac.regiontype = (t->ar) ? t->ar->regiontype : 0;
336         
337         ANIM_animdata_context_getdata(&ac);
338         
339         /* perform flush */
340         if (ELEM(ac.datatype, ANIMCONT_GPENCIL, ANIMCONT_MASK)) {
341                 /* flush transform values back to actual coordinates */
342                 flushTransIntFrameActionData(t);
343         }
344         else {
345                 /* get animdata blocks visible in editor, assuming that these will be the ones where things changed */
346                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_ANIMDATA);
347                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
348                 
349                 /* just tag these animdata-blocks to recalc, assuming that some data there changed 
350                  * BUT only do this if realtime updates are enabled
351                  */
352                 if ((saction->flag & SACTION_NOREALTIMEUPDATES) == 0) {
353                         for (ale = anim_data.first; ale; ale = ale->next) {
354                                 /* set refresh tags for objects using this animation */
355                                 ANIM_list_elem_update(t->scene, ale);
356                         }
357                 }
358                 
359                 /* now free temp channels */
360                 BLI_freelistN(&anim_data);
361         }
362 }
363 /* helper for recalcData() - for Graph Editor transforms */
364 static void recalcData_graphedit(TransInfo *t)
365 {
366         SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
367         Scene *scene;
368         
369         ListBase anim_data = {NULL, NULL};
370         bAnimContext ac = {NULL};
371         int filter;
372         
373         bAnimListElem *ale;
374         int dosort = 0;
375         
376         
377         /* initialize relevant anim-context 'context' data from TransInfo data */
378         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
379         scene = ac.scene = t->scene;
380         ac.obact = OBACT;
381         ac.sa = t->sa;
382         ac.ar = t->ar;
383         ac.sl = (t->sa) ? t->sa->spacedata.first : NULL;
384         ac.spacetype = (t->sa) ? t->sa->spacetype : 0;
385         ac.regiontype = (t->ar) ? t->ar->regiontype : 0;
386         
387         ANIM_animdata_context_getdata(&ac);
388         
389         /* do the flush first */
390         flushTransGraphData(t);
391         
392         /* get curves to check if a re-sort is needed */
393         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVE_VISIBLE);
394         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
395         
396         /* now test if there is a need to re-sort */
397         for (ale = anim_data.first; ale; ale = ale->next) {
398                 FCurve *fcu = (FCurve *)ale->key_data;
399                 
400                 /* ignore unselected fcurves */
401                 if (!fcu_test_selected(fcu))
402                         continue;
403                 
404                 // fixme: only do this for selected verts...
405                 ANIM_unit_mapping_apply_fcurve(ac.scene, ale->id, ale->key_data, ANIM_UNITCONV_ONLYSEL | ANIM_UNITCONV_SELVERTS | ANIM_UNITCONV_RESTORE);
406                 
407                 
408                 /* watch it: if the time is wrong: do not correct handles yet */
409                 if (test_time_fcurve(fcu))
410                         dosort++;
411                 else
412                         calchandles_fcurve(fcu);
413                 
414                 /* set refresh tags for objects using this animation,
415                  * BUT only if realtime updates are enabled  
416                  */
417                 if ((sipo->flag & SIPO_NOREALTIMEUPDATES) == 0)
418                         ANIM_list_elem_update(t->scene, ale);
419         }
420         
421         /* do resort and other updates? */
422         if (dosort) remake_graph_transdata(t, &anim_data);
423         
424         /* now free temp channels */
425         BLI_freelistN(&anim_data);
426 }
427
428 /* helper for recalcData() - for NLA Editor transforms */
429 static void recalcData_nla(TransInfo *t)
430 {
431         TransDataNla *tdn = (TransDataNla *)t->customData;
432         SpaceNla *snla = (SpaceNla *)t->sa->spacedata.first;
433         Scene *scene = t->scene;
434         double secf = FPS;
435         int i;
436         
437         /* for each strip we've got, perform some additional validation of the values that got set before
438          * using RNA to set the value (which does some special operations when setting these values to make
439          * sure that everything works ok)
440          */
441         for (i = 0; i < t->total; i++, tdn++) {
442                 NlaStrip *strip = tdn->strip;
443                 PointerRNA strip_ptr;
444                 short pExceeded, nExceeded, iter;
445                 int delta_y1, delta_y2;
446                 
447                 /* if this tdn has no handles, that means it is just a dummy that should be skipped */
448                 if (tdn->handle == 0)
449                         continue;
450                 
451                 /* set refresh tags for objects using this animation,
452                  * BUT only if realtime updates are enabled  
453                  */
454                 if ((snla->flag & SNLA_NOREALTIMEUPDATES) == 0)
455                         ANIM_id_update(t->scene, tdn->id);
456                 
457                 /* if canceling transform, just write the values without validating, then move on */
458                 if (t->state == TRANS_CANCEL) {
459                         /* clear the values by directly overwriting the originals, but also need to restore
460                          * endpoints of neighboring transition-strips
461                          */
462                         
463                         /* start */
464                         strip->start = tdn->h1[0];
465                         
466                         if ((strip->prev) && (strip->prev->type == NLASTRIP_TYPE_TRANSITION))
467                                 strip->prev->end = tdn->h1[0];
468                         
469                         /* end */
470                         strip->end = tdn->h2[0];
471                         
472                         if ((strip->next) && (strip->next->type == NLASTRIP_TYPE_TRANSITION))
473                                 strip->next->start = tdn->h2[0];
474                         
475                         /* flush transforms to child strips (since this should be a meta) */
476                         BKE_nlameta_flush_transforms(strip);
477                         
478                         /* restore to original track (if needed) */
479                         if (tdn->oldTrack != tdn->nlt) {
480                                 /* just append to end of list for now, since strips get sorted in special_aftertrans_update() */
481                                 BLI_remlink(&tdn->nlt->strips, strip);
482                                 BLI_addtail(&tdn->oldTrack->strips, strip);
483                         }
484                         
485                         continue;
486                 }
487                 
488                 /* firstly, check if the proposed transform locations would overlap with any neighboring strips
489                  * (barring transitions) which are absolute barriers since they are not being moved
490                  *
491                  * this is done as a iterative procedure (done 5 times max for now)
492                  */
493                 for (iter = 0; iter < 5; iter++) {
494                         pExceeded = ((strip->prev) && (strip->prev->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h1[0] < strip->prev->end));
495                         nExceeded = ((strip->next) && (strip->next->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h2[0] > strip->next->start));
496                         
497                         if ((pExceeded && nExceeded) || (iter == 4) ) {
498                                 /* both endpoints exceeded (or iteration ping-pong'd meaning that we need a compromise)
499                                  *      - simply crop strip to fit within the bounds of the strips bounding it
500                                  *      - if there were no neighbors, clear the transforms (make it default to the strip's current values)
501                                  */
502                                 if (strip->prev && strip->next) {
503                                         tdn->h1[0] = strip->prev->end;
504                                         tdn->h2[0] = strip->next->start;
505                                 }
506                                 else {
507                                         tdn->h1[0] = strip->start;
508                                         tdn->h2[0] = strip->end;
509                                 }
510                         }
511                         else if (nExceeded) {
512                                 /* move backwards */
513                                 float offset = tdn->h2[0] - strip->next->start;
514                                 
515                                 tdn->h1[0] -= offset;
516                                 tdn->h2[0] -= offset;
517                         }
518                         else if (pExceeded) {
519                                 /* more forwards */
520                                 float offset = strip->prev->end - tdn->h1[0];
521                                 
522                                 tdn->h1[0] += offset;
523                                 tdn->h2[0] += offset;
524                         }
525                         else /* all is fine and well */
526                                 break;
527                 }
528                 
529                 /* handle auto-snapping */
530                 switch (snla->autosnap) {
531                         case SACTSNAP_FRAME: /* snap to nearest frame/time  */
532                                 if (snla->flag & SNLA_DRAWTIME) {
533                                         tdn->h1[0] = (float)(floor(((double)tdn->h1[0] / secf) + 0.5) * secf);
534                                         tdn->h2[0] = (float)(floor(((double)tdn->h2[0] / secf) + 0.5) * secf);
535                                 }
536                                 else {
537                                         tdn->h1[0] = floorf(tdn->h1[0] + 0.5f);
538                                         tdn->h2[0] = floorf(tdn->h2[0] + 0.5f);
539                                 }
540                                 break;
541                         
542                         case SACTSNAP_MARKER: /* snap to nearest marker */
543                                 tdn->h1[0] = (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h1[0]);
544                                 tdn->h2[0] = (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h2[0]);
545                                 break;
546                 }
547                 
548                 /* use RNA to write the values... */
549                 // TODO: do we need to write in 2 passes to make sure that no truncation goes on?
550                 RNA_pointer_create(NULL, &RNA_NlaStrip, strip, &strip_ptr);
551                 
552                 RNA_float_set(&strip_ptr, "frame_start", tdn->h1[0]);
553                 RNA_float_set(&strip_ptr, "frame_end", tdn->h2[0]);
554                 
555                 /* flush transforms to child strips (since this should be a meta) */
556                 BKE_nlameta_flush_transforms(strip);
557                 
558                 
559                 /* now, check if we need to try and move track
560                  *      - we need to calculate both, as only one may have been altered by transform if only 1 handle moved
561                  */
562                 delta_y1 = ((int)tdn->h1[1] / NLACHANNEL_STEP(snla) - tdn->trackIndex);
563                 delta_y2 = ((int)tdn->h2[1] / NLACHANNEL_STEP(snla) - tdn->trackIndex);
564                 
565                 if (delta_y1 || delta_y2) {
566                         NlaTrack *track;
567                         int delta = (delta_y2) ? delta_y2 : delta_y1;
568                         int n;
569                         
570                         /* move in the requested direction, checking at each layer if there's space for strip to pass through,
571                          * stopping on the last track available or that we're able to fit in
572                          */
573                         if (delta > 0) {
574                                 for (track = tdn->nlt->next, n = 0; (track) && (n < delta); track = track->next, n++) {
575                                         /* check if space in this track for the strip */
576                                         if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
577                                                 /* move strip to this track */
578                                                 BLI_remlink(&tdn->nlt->strips, strip);
579                                                 BKE_nlatrack_add_strip(track, strip);
580                                                 
581                                                 tdn->nlt = track;
582                                                 tdn->trackIndex++;
583                                         }
584                                         else /* can't move any further */
585                                                 break;
586                                 }
587                         }
588                         else {
589                                 /* make delta 'positive' before using it, since we now know to go backwards */
590                                 delta = -delta;
591                                 
592                                 for (track = tdn->nlt->prev, n = 0; (track) && (n < delta); track = track->prev, n++) {
593                                         /* check if space in this track for the strip */
594                                         if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
595                                                 /* move strip to this track */
596                                                 BLI_remlink(&tdn->nlt->strips, strip);
597                                                 BKE_nlatrack_add_strip(track, strip);
598                                                 
599                                                 tdn->nlt = track;
600                                                 tdn->trackIndex--;
601                                         }
602                                         else /* can't move any further */
603                                                 break;
604                                 }
605                         }
606                 }
607         }
608 }
609
610 /* helper for recalcData() - for Image Editor transforms */
611 static void recalcData_image(TransInfo *t)
612 {
613         if (t->obedit && t->obedit->type == OB_MESH) {
614                 SpaceImage *sima = t->sa->spacedata.first;
615                 
616                 flushTransUVs(t);
617                 if (sima->flag & SI_LIVE_UNWRAP)
618                         ED_uvedit_live_unwrap_re_solve();
619                 
620                 DAG_id_tag_update(t->obedit->data, 0);
621         }
622 }
623
624 /* helper for recalcData() - for Movie Clip transforms */
625 static void recalcData_spaceclip(TransInfo *t)
626 {
627         SpaceClip *sc = t->sa->spacedata.first;
628
629         if (ED_space_clip_check_show_trackedit(sc)) {
630                 MovieClip *clip = ED_space_clip_get_clip(sc);
631                 ListBase *tracksbase = BKE_tracking_get_active_tracks(&clip->tracking);
632                 MovieTrackingTrack *track;
633                 int framenr = ED_space_clip_get_clip_frame_number(sc);
634
635                 flushTransTracking(t);
636
637                 track = tracksbase->first;
638                 while (track) {
639                         if (TRACK_VIEW_SELECTED(sc, track) && (track->flag & TRACK_LOCKED) == 0) {
640                                 MovieTrackingMarker *marker = BKE_tracking_marker_get(track, framenr);
641
642                                 if (t->mode == TFM_TRANSLATION) {
643                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
644                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_POS);
645                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_SEARCH))
646                                                 BKE_tracking_marker_clamp(marker, CLAMP_SEARCH_POS);
647                                 }
648                                 else if (t->mode == TFM_RESIZE) {
649                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
650                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_DIM);
651                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_SEARCH))
652                                                 BKE_tracking_marker_clamp(marker, CLAMP_SEARCH_DIM);
653                                 }
654                                 else if (t->mode == TFM_ROTATION) {
655                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
656                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_POS);
657                                 }
658                         }
659
660                         track = track->next;
661                 }
662
663                 DAG_id_tag_update(&clip->id, 0);
664         }
665         else if (ED_space_clip_check_show_maskedit(sc)) {
666                 Mask *mask = ED_space_clip_get_mask(sc);
667
668                 flushTransMasking(t);
669
670                 DAG_id_tag_update(&mask->id, 0);
671         }
672 }
673
674 /* helper for recalcData() - for 3d-view transforms */
675 static void recalcData_view3d(TransInfo *t)
676 {
677         Base *base = t->scene->basact;
678         
679         if (t->obedit) {
680                 if (ELEM(t->obedit->type, OB_CURVE, OB_SURF)) {
681                         Curve *cu = t->obedit->data;
682                         ListBase *nurbs = BKE_curve_editNurbs_get(cu);
683                         Nurb *nu = nurbs->first;
684                         
685                         if (t->state != TRANS_CANCEL) {
686                                 clipMirrorModifier(t, t->obedit);
687                                 applyProject(t);
688                         }
689                         
690                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
691                                 
692                         if (t->state == TRANS_CANCEL) {
693                                 while (nu) {
694                                         BKE_nurb_handles_calc(nu); /* Cant do testhandlesNurb here, it messes up the h1 and h2 flags */
695                                         nu = nu->next;
696                                 }
697                         } 
698                         else {
699                                 /* Normal updating */
700                                 while (nu) {
701                                         BKE_nurb_test2D(nu);
702                                         BKE_nurb_handles_calc(nu);
703                                         nu = nu->next;
704                                 }
705                         }
706                 }
707                 else if (t->obedit->type == OB_LATTICE) {
708                         Lattice *la = t->obedit->data;
709                         
710                         if (t->state != TRANS_CANCEL) {
711                                 applyProject(t);
712                         }
713                         
714                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
715                         
716                         if (la->editlatt->latt->flag & LT_OUTSIDE) outside_lattice(la->editlatt->latt);
717                 }
718                 else if (t->obedit->type == OB_MESH) {
719                         BMEditMesh *em = BMEdit_FromObject(t->obedit);
720                         /* mirror modifier clipping? */
721                         if (t->state != TRANS_CANCEL) {
722                                 /* apply clipping after so we never project past the clip plane [#25423] */
723                                 applyProject(t);
724                                 clipMirrorModifier(t, t->obedit);
725                         }
726                         if ((t->options & CTX_NO_MIRROR) == 0 && (t->flag & T_MIRROR))
727                                 editbmesh_apply_to_mirror(t);
728                                 
729                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
730                         
731                         EDBM_mesh_normals_update(em);
732                         BMEdit_RecalcTessellation(em);
733                 }
734                 else if (t->obedit->type == OB_ARMATURE) { /* no recalc flag, does pose */
735                         bArmature *arm = t->obedit->data;
736                         ListBase *edbo = arm->edbo;
737                         EditBone *ebo;
738                         TransData *td = t->data;
739                         int i;
740                         
741                         if (t->state != TRANS_CANCEL) {
742                                 applyProject(t);
743                         }
744                         
745                         /* Ensure all bones are correctly adjusted */
746                         for (ebo = edbo->first; ebo; ebo = ebo->next) {
747                                 
748                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent) {
749                                         /* If this bone has a parent tip that has been moved */
750                                         if (ebo->parent->flag & BONE_TIPSEL) {
751                                                 copy_v3_v3(ebo->head, ebo->parent->tail);
752                                                 if (t->mode == TFM_BONE_ENVELOPE) ebo->rad_head = ebo->parent->rad_tail;
753                                         }
754                                         /* If this bone has a parent tip that has NOT been moved */
755                                         else {
756                                                 copy_v3_v3(ebo->parent->tail, ebo->head);
757                                                 if (t->mode == TFM_BONE_ENVELOPE) ebo->parent->rad_tail = ebo->rad_head;
758                                         }
759                                 }
760                                 
761                                 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
762                                 ebo->length = len_v3v3(ebo->head, ebo->tail);
763                                 if (ebo->oldlength == 0.0f) {
764                                         ebo->rad_head = 0.25f * ebo->length;
765                                         ebo->rad_tail = 0.10f * ebo->length;
766                                         ebo->dist = 0.25f * ebo->length;
767                                         if (ebo->parent) {
768                                                 if (ebo->rad_head > ebo->parent->rad_tail)
769                                                         ebo->rad_head = ebo->parent->rad_tail;
770                                         }
771                                 }
772                                 else if (t->mode != TFM_BONE_ENVELOPE) {
773                                         /* if bones change length, lets do that for the deform distance as well */
774                                         ebo->dist *= ebo->length / ebo->oldlength;
775                                         ebo->rad_head *= ebo->length / ebo->oldlength;
776                                         ebo->rad_tail *= ebo->length / ebo->oldlength;
777                                         ebo->oldlength = ebo->length;
778                                 }
779                         }
780                         
781                         
782                         if (t->mode != TFM_BONE_ROLL) {
783                                 /* fix roll */
784                                 for (i = 0; i < t->total; i++, td++) {
785                                         if (td->extra) {
786                                                 float vec[3], up_axis[3];
787                                                 float qrot[4];
788                                                 
789                                                 ebo = td->extra;
790                                                 copy_v3_v3(up_axis, td->axismtx[2]);
791                                                 
792                                                 if (t->mode != TFM_ROTATION) {
793                                                         sub_v3_v3v3(vec, ebo->tail, ebo->head);
794                                                         normalize_v3(vec);
795                                                         rotation_between_vecs_to_quat(qrot, td->axismtx[1], vec);
796                                                         mul_qt_v3(qrot, up_axis);
797                                                 }
798                                                 else {
799                                                         mul_m3_v3(t->mat, up_axis);
800                                                 }
801                                                 
802                                                 ebo->roll = ED_rollBoneToVector(ebo, up_axis, FALSE);
803                                         }
804                                 }
805                         }
806                         
807                         if (arm->flag & ARM_MIRROR_EDIT)
808                                 transform_armature_mirror_update(t->obedit);
809                         
810                 }
811                 else {
812                         if (t->state != TRANS_CANCEL) {
813                                 applyProject(t);
814                         }
815                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
816                 }
817         }
818         else if ((t->flag & T_POSE) && t->poseobj) {
819                 Object *ob = t->poseobj;
820                 bArmature *arm = ob->data;
821                 
822                 /* if animtimer is running, and the object already has animation data,
823                  * check if the auto-record feature means that we should record 'samples'
824                  * (i.e. uneditable animation values)
825                  *
826                  * context is needed for keying set poll() functions.
827                  */
828                 // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
829                 if ((t->animtimer) && (t->context) && IS_AUTOKEY_ON(t->scene)) {
830                         int targetless_ik = (t->flag & T_AUTOIK); // XXX this currently doesn't work, since flags aren't set yet!
831                         
832                         animrecord_check_state(t->scene, &ob->id, t->animtimer);
833                         autokeyframe_pose_cb_func(t->context, t->scene, (View3D *)t->view, ob, t->mode, targetless_ik);
834                 }
835                 
836                 /* old optimize trick... this enforces to bypass the depgraph */
837                 if (!(arm->flag & ARM_DELAYDEFORM)) {
838                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);  /* sets recalc flags */
839                 }
840                 else
841                         BKE_pose_where_is(t->scene, ob);
842         }
843         else if (base && (base->object->mode & OB_MODE_PARTICLE_EDIT) && PE_get_current(t->scene, base->object)) {
844                 if (t->state != TRANS_CANCEL) {
845                         applyProject(t);
846                 }
847                 flushTransParticles(t);
848         }
849         else {
850                 int i;
851                 
852                 if (t->state != TRANS_CANCEL) {
853                         applyProject(t);
854                 }
855                 
856                 for (i = 0; i < t->total; i++) {
857                         TransData *td = t->data + i;
858                         Object *ob = td->ob;
859                         
860                         if (td->flag & TD_NOACTION)
861                                 break;
862                         
863                         if (td->flag & TD_SKIP)
864                                 continue;
865                         
866                         /* if animtimer is running, and the object already has animation data,
867                          * check if the auto-record feature means that we should record 'samples'
868                          * (i.e. uneditable animation values)
869                          */
870                         // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
871                         if ((t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
872                                 animrecord_check_state(t->scene, &ob->id, t->animtimer);
873                                 autokeyframe_ob_cb_func(t->context, t->scene, (View3D *)t->view, ob, t->mode);
874                         }
875                         
876                         /* sets recalc flags fully, instead of flushing existing ones 
877                          * otherwise proxies don't function correctly
878                          */
879                         DAG_id_tag_update(&ob->id, OB_RECALC_OB);
880                 }
881         }
882 }
883
884 /* called for updating while transform acts, once per redraw */
885 void recalcData(TransInfo *t)
886 {
887         if (t->spacetype == SPACE_NODE) {
888                 flushTransNodes(t);
889         }
890         else if (t->spacetype == SPACE_SEQ) {
891                 flushTransSeq(t);
892         }
893         else if (t->spacetype == SPACE_ACTION) {
894                 recalcData_actedit(t);
895         }
896         else if (t->spacetype == SPACE_IPO) {
897                 recalcData_graphedit(t);
898         }
899         else if (t->spacetype == SPACE_NLA) {
900                 recalcData_nla(t);
901         }
902         else if (t->spacetype == SPACE_IMAGE) {
903                 recalcData_image(t);
904         }
905         else if (t->spacetype == SPACE_VIEW3D) {
906                 recalcData_view3d(t);
907         }
908         else if (t->spacetype == SPACE_CLIP) {
909                 recalcData_spaceclip(t);
910         }
911 }
912
913 void drawLine(TransInfo *t, float *center, float *dir, char axis, short options)
914 {
915         float v1[3], v2[3], v3[3];
916         unsigned char col[3], col2[3];
917
918         if (t->spacetype == SPACE_VIEW3D) {
919                 View3D *v3d = t->view;
920                 
921                 glPushMatrix();
922                 
923                 //if (t->obedit) glLoadMatrixf(t->obedit->obmat);       // sets opengl viewing
924                 
925                 
926                 copy_v3_v3(v3, dir);
927                 mul_v3_fl(v3, v3d->far);
928                 
929                 sub_v3_v3v3(v2, center, v3);
930                 add_v3_v3v3(v1, center, v3);
931                 
932                 if (options & DRAWLIGHT) {
933                         col[0] = col[1] = col[2] = 220;
934                 }
935                 else {
936                         UI_GetThemeColor3ubv(TH_GRID, col);
937                 }
938                 UI_make_axis_color(col, col2, axis);
939                 glColor3ubv(col2);
940                 
941                 setlinestyle(0);
942                 glBegin(GL_LINE_STRIP);
943                 glVertex3fv(v1);
944                 glVertex3fv(v2);
945                 glEnd();
946                 
947                 glPopMatrix();
948         }
949 }
950
951 void resetTransRestrictions(TransInfo *t)
952 {
953         t->flag &= ~T_ALL_RESTRICTIONS;
954 }
955
956 /* the *op can be NULL */
957 int initTransInfo(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event)
958 {
959         Scene *sce = CTX_data_scene(C);
960         ToolSettings *ts = CTX_data_tool_settings(C);
961         ARegion *ar = CTX_wm_region(C);
962         ScrArea *sa = CTX_wm_area(C);
963         Object *obedit = CTX_data_edit_object(C);
964         
965         /* moving: is shown in drawobject() (transform color) */
966 //  TRANSFORM_FIX_ME
967 //      if (obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
968 //      else if (G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
969 //      else G.moving= G_TRANSFORM_OBJ;
970         
971         t->scene = sce;
972         t->sa = sa;
973         t->ar = ar;
974         t->obedit = obedit;
975         t->settings = ts;
976         
977         t->data = NULL;
978         t->ext = NULL;
979         
980         t->helpline = HLP_NONE;
981         
982         t->flag = 0;
983         
984         t->redraw = 1; /* redraw first time */
985         
986         if (event) {
987                 copy_v2_v2_int(t->imval, event->mval);
988                 t->event_type = event->type;
989         }
990         else {
991                 t->imval[0] = 0;
992                 t->imval[1] = 0;
993         }
994         
995         t->con.imval[0] = t->imval[0];
996         t->con.imval[1] = t->imval[1];
997         
998         t->mval[0] = t->imval[0];
999         t->mval[1] = t->imval[1];
1000         
1001         t->transform        = NULL;
1002         t->handleEvent      = NULL;
1003         
1004         t->total            = 0;
1005         
1006         t->val = 0.0f;
1007         
1008         t->vec[0]           =
1009             t->vec[1]       =
1010                 t->vec[2]       = 0.0f;
1011
1012         t->center[0]        =
1013             t->center[1]    =
1014                 t->center[2]    = 0.0f;
1015         
1016         unit_m3(t->mat);
1017         
1018         /* if there's an event, we're modal */
1019         if (event) {
1020                 t->flag |= T_MODAL;
1021         }
1022
1023         /* Crease needs edge flag */
1024         if (ELEM(t->mode, TFM_CREASE, TFM_BWEIGHT)) {
1025                 t->options |= CTX_EDGE;
1026         }
1027
1028
1029         /* Assign the space type, some exceptions for running in different mode */
1030         if (sa == NULL) {
1031                 /* background mode */
1032                 t->spacetype = SPACE_EMPTY;
1033         }
1034         else if ((ar == NULL) && (sa->spacetype == SPACE_VIEW3D)) {
1035                 /* running in the text editor */
1036                 t->spacetype = SPACE_EMPTY;
1037         }
1038         else {
1039                 /* normal operation */
1040                 t->spacetype = sa->spacetype;
1041         }
1042
1043
1044         if (t->spacetype == SPACE_VIEW3D) {
1045                 View3D *v3d = sa->spacedata.first;
1046                 bScreen *animscreen = ED_screen_animation_playing(CTX_wm_manager(C));
1047                 
1048                 t->view = v3d;
1049                 t->animtimer = (animscreen) ? animscreen->animtimer : NULL;
1050                 
1051                 /* turn manipulator off during transform */
1052                 // FIXME: but don't do this when USING the manipulator...
1053                 if (t->flag & T_MODAL) {
1054                         t->twtype = v3d->twtype;
1055                         v3d->twtype = 0;
1056                 }
1057
1058                 if (v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
1059                 t->around = v3d->around;
1060                 
1061                 if (op && (RNA_struct_find_property(op->ptr, "constraint_orientation") &&
1062                            RNA_struct_property_is_set(op->ptr, "constraint_orientation")))
1063                 {
1064                         t->current_orientation = RNA_enum_get(op->ptr, "constraint_orientation");
1065
1066                         if (t->current_orientation >= V3D_MANIP_CUSTOM + BIF_countTransformOrientation(C)) {
1067                                 t->current_orientation = V3D_MANIP_GLOBAL;
1068                         }
1069                 }
1070                 else {
1071                         t->current_orientation = v3d->twmode;
1072                 }
1073
1074                 /* exceptional case */
1075                 if (t->around == V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
1076                         if (ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL)) {
1077                                 t->options |= CTX_NO_PET;
1078                         }
1079                 }
1080
1081                 /* initialize UV transform from */
1082                 if (op && RNA_struct_find_property(op->ptr, "correct_uv")) {
1083                         if (RNA_struct_property_is_set(op->ptr, "correct_uv")) {
1084                                 if (RNA_boolean_get(op->ptr, "correct_uv")) {
1085                                         t->settings->uvcalc_flag |= UVCALC_TRANSFORM_CORRECT;
1086                                 }
1087                                 else {
1088                                         t->settings->uvcalc_flag &= ~UVCALC_TRANSFORM_CORRECT;
1089                                 }
1090                         }
1091                         else {
1092                                 RNA_boolean_set(op->ptr, "correct_uv", t->settings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT);
1093                         }
1094                 }
1095
1096         }
1097         else if (t->spacetype == SPACE_IMAGE) {
1098                 SpaceImage *sima = sa->spacedata.first;
1099                 // XXX for now, get View2D from the active region
1100                 t->view = &ar->v2d;
1101                 t->around = sima->around;
1102         }
1103         else if (t->spacetype == SPACE_NODE) {
1104                 // XXX for now, get View2D from the active region
1105                 t->view = &ar->v2d;
1106                 t->around = V3D_CENTER;
1107         }
1108         else if (t->spacetype == SPACE_IPO) {
1109                 SpaceIpo *sipo = sa->spacedata.first;
1110                 t->view = &ar->v2d;
1111                 t->around = sipo->around;
1112         }
1113         else if (t->spacetype == SPACE_CLIP) {
1114                 SpaceClip *sclip = sa->spacedata.first;
1115                 t->view = &ar->v2d;
1116                 t->around = sclip->around;
1117
1118                 if (ED_space_clip_check_show_trackedit(sclip))
1119                         t->options |= CTX_MOVIECLIP;
1120                 else if (ED_space_clip_check_show_maskedit(sclip))
1121                         t->options |= CTX_MASK;
1122         }
1123         else {
1124                 if (ar) {
1125                         // XXX for now, get View2D  from the active region
1126                         t->view = &ar->v2d;
1127                         // XXX for now, the center point is the midpoint of the data
1128                 }
1129                 else {
1130                         t->view = NULL;
1131                 }
1132                 t->around = V3D_CENTER;
1133         }
1134         
1135         if (op && RNA_struct_property_is_set(op->ptr, "release_confirm")) {
1136                 if (RNA_boolean_get(op->ptr, "release_confirm")) {
1137                         t->flag |= T_RELEASE_CONFIRM;
1138                 }
1139         }
1140         else {
1141                 if (U.flag & USER_RELEASECONFIRM) {
1142                         t->flag |= T_RELEASE_CONFIRM;
1143                 }
1144         }
1145
1146         if (op && (RNA_struct_find_property(op->ptr, "mirror") &&
1147                    RNA_struct_property_is_set(op->ptr, "mirror")))
1148         {
1149                 if (RNA_boolean_get(op->ptr, "mirror")) {
1150                         t->flag |= T_MIRROR;
1151                         t->mirror = 1;
1152                 }
1153         }
1154         // Need stuff to take it from edit mesh or whatnot here
1155         else if (t->spacetype == SPACE_VIEW3D) {
1156                 if (t->obedit && t->obedit->type == OB_MESH && (((Mesh *)t->obedit->data)->editflag & ME_EDIT_MIRROR_X)) {
1157                         t->flag |= T_MIRROR;
1158                         t->mirror = 1;
1159                 }
1160         }
1161         
1162         /* setting PET flag only if property exist in operator. Otherwise, assume it's not supported */
1163         if (op && RNA_struct_find_property(op->ptr, "proportional")) {
1164                 if (RNA_struct_property_is_set(op->ptr, "proportional")) {
1165                         switch (RNA_enum_get(op->ptr, "proportional")) {
1166                                 case PROP_EDIT_CONNECTED:
1167                                         t->flag |= T_PROP_CONNECTED;
1168                                 case PROP_EDIT_ON:
1169                                         t->flag |= T_PROP_EDIT;
1170                                         break;
1171                         }
1172                 }
1173                 else {
1174                         /* use settings from scene only if modal */
1175                         if (t->flag & T_MODAL) {
1176                                 if ((t->options & CTX_NO_PET) == 0) {
1177                                         if (t->obedit && ts->proportional != PROP_EDIT_OFF) {
1178                                                 t->flag |= T_PROP_EDIT;
1179
1180                                                 if (ts->proportional == PROP_EDIT_CONNECTED) {
1181                                                         t->flag |= T_PROP_CONNECTED;
1182                                                 }
1183                                         }
1184                                         else if (t->options & CTX_MASK) {
1185                                                 if (ts->proportional_mask) {
1186                                                         t->flag |= T_PROP_EDIT;
1187
1188                                                         if (ts->proportional == PROP_EDIT_CONNECTED) {
1189                                                                 t->flag |= T_PROP_CONNECTED;
1190                                                         }
1191                                                 }
1192                                         }
1193                                         else if (t->obedit == NULL && ts->proportional_objects) {
1194                                                 t->flag |= T_PROP_EDIT;
1195                                         }
1196                                 }
1197                         }
1198                 }
1199                 
1200                 if (op && (RNA_struct_find_property(op->ptr, "proportional_size") &&
1201                            RNA_struct_property_is_set(op->ptr, "proportional_size")))
1202                 {
1203                         t->prop_size = RNA_float_get(op->ptr, "proportional_size");
1204                 }
1205                 else {
1206                         t->prop_size = ts->proportional_size;
1207                 }
1208                 
1209                 
1210                 /* TRANSFORM_FIX_ME rna restrictions */
1211                 if (t->prop_size <= 0.00001f) {
1212                         printf("Proportional size (%f) under 0.00001, reseting to 1!\n", t->prop_size);
1213                         t->prop_size = 1.0f;
1214                 }
1215                 
1216                 if (op && (RNA_struct_find_property(op->ptr, "proportional_edit_falloff") &&
1217                            RNA_struct_property_is_set(op->ptr, "proportional_edit_falloff")))
1218                 {
1219                         t->prop_mode = RNA_enum_get(op->ptr, "proportional_edit_falloff");
1220                 }
1221                 else {
1222                         t->prop_mode = ts->prop_mode;
1223                 }
1224         }
1225         else { /* add not pet option to context when not available */
1226                 t->options |= CTX_NO_PET;
1227         }
1228         
1229         // Mirror is not supported with PET, turn it off.
1230         if (t->flag & T_PROP_EDIT) {
1231                 t->flag &= ~T_MIRROR;
1232         }
1233
1234         setTransformViewMatrices(t);
1235         initNumInput(&t->num);
1236         
1237         return 1;
1238 }
1239
1240 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
1241 void postTrans(bContext *C, TransInfo *t)
1242 {
1243         TransData *td;
1244         
1245         if (t->draw_handle_view)
1246                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_view);
1247         if (t->draw_handle_apply)
1248                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_apply);
1249         if (t->draw_handle_pixel)
1250                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_pixel);
1251         if (t->draw_handle_cursor)
1252                 WM_paint_cursor_end(CTX_wm_manager(C), t->draw_handle_cursor);
1253
1254         if (t->customFree) {
1255                 /* Can take over freeing t->data and data2d etc... */
1256                 t->customFree(t);
1257         }
1258         else if ((t->customData != NULL) && (t->flag & T_FREE_CUSTOMDATA)) {
1259                 MEM_freeN(t->customData);
1260         }
1261
1262         /* postTrans can be called when nothing is selected, so data is NULL already */
1263         if (t->data) {
1264                 int a;
1265                 
1266                 /* free data malloced per trans-data */
1267                 for (a = 0, td = t->data; a < t->total; a++, td++) {
1268                         if (td->flag & TD_BEZTRIPLE) 
1269                                 MEM_freeN(td->hdata);
1270                 }
1271                 MEM_freeN(t->data);
1272         }
1273         
1274         BLI_freelistN(&t->tsnap.points);
1275
1276         if (t->ext) MEM_freeN(t->ext);
1277         if (t->data2d) {
1278                 MEM_freeN(t->data2d);
1279                 t->data2d = NULL;
1280         }
1281         
1282         if (t->spacetype == SPACE_IMAGE) {
1283                 SpaceImage *sima = t->sa->spacedata.first;
1284                 if (sima->flag & SI_LIVE_UNWRAP)
1285                         ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1286         }
1287         else if (t->spacetype == SPACE_VIEW3D) {
1288                 View3D *v3d = t->sa->spacedata.first;
1289                 /* restore manipulator */
1290                 if (t->flag & T_MODAL) {
1291                         v3d->twtype = t->twtype;
1292                 }
1293         }
1294         
1295         if (t->mouse.data) {
1296                 MEM_freeN(t->mouse.data);
1297         }
1298 }
1299
1300 void applyTransObjects(TransInfo *t)
1301 {
1302         TransData *td;
1303         
1304         for (td = t->data; td < t->data + t->total; td++) {
1305                 copy_v3_v3(td->iloc, td->loc);
1306                 if (td->ext->rot) {
1307                         copy_v3_v3(td->ext->irot, td->ext->rot);
1308                 }
1309                 if (td->ext->size) {
1310                         copy_v3_v3(td->ext->isize, td->ext->size);
1311                 }
1312         }
1313         recalcData(t);
1314 }
1315
1316 static void restoreElement(TransData *td)
1317 {
1318         /* TransData for crease has no loc */
1319         if (td->loc) {
1320                 copy_v3_v3(td->loc, td->iloc);
1321         }
1322         if (td->val) {
1323                 *td->val = td->ival;
1324         }
1325
1326         if (td->ext && (td->flag & TD_NO_EXT) == 0) {
1327                 if (td->ext->rot) {
1328                         copy_v3_v3(td->ext->rot, td->ext->irot);
1329                 }
1330                 if (td->ext->rotAngle) {
1331                         *td->ext->rotAngle = td->ext->irotAngle;
1332                 }
1333                 if (td->ext->rotAxis) {
1334                         copy_v3_v3(td->ext->rotAxis, td->ext->irotAxis);
1335                 }
1336                 /* XXX, drotAngle & drotAxis not used yet */
1337                 if (td->ext->size) {
1338                         copy_v3_v3(td->ext->size, td->ext->isize);
1339                 }
1340                 if (td->ext->quat) {
1341                         copy_qt_qt(td->ext->quat, td->ext->iquat);
1342                 }
1343         }
1344         
1345         if (td->flag & TD_BEZTRIPLE) {
1346                 *(td->hdata->h1) = td->hdata->ih1;
1347                 *(td->hdata->h2) = td->hdata->ih2;
1348         }
1349 }
1350
1351 void restoreTransObjects(TransInfo *t)
1352 {
1353         TransData *td;
1354         TransData2D *td2d;
1355
1356         for (td = t->data; td < t->data + t->total; td++) {
1357                 restoreElement(td);
1358         }
1359         
1360         for (td2d = t->data2d; t->data2d && td2d < t->data2d + t->total; td2d++) {
1361                 if (td2d->h1) {
1362                         td2d->h1[0] = td2d->ih1[0];
1363                         td2d->h1[1] = td2d->ih1[1];
1364                 }
1365                 if (td2d->h2) {
1366                         td2d->h2[0] = td2d->ih2[0];
1367                         td2d->h2[1] = td2d->ih2[1];
1368                 }
1369         }
1370
1371         unit_m3(t->mat);
1372         
1373         recalcData(t);
1374 }
1375
1376 void calculateCenter2D(TransInfo *t)
1377 {
1378         if (t->flag & (T_EDIT | T_POSE)) {
1379                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1380                 float vec[3];
1381                 
1382                 copy_v3_v3(vec, t->center);
1383                 mul_m4_v3(ob->obmat, vec);
1384                 projectIntView(t, vec, t->center2d);
1385         }
1386         else {
1387                 projectIntView(t, t->center, t->center2d);
1388         }
1389 }
1390
1391 void calculateCenterCursor(TransInfo *t)
1392 {
1393         float *cursor;
1394         
1395         cursor = give_cursor(t->scene, t->view);
1396         copy_v3_v3(t->center, cursor);
1397         
1398         /* If edit or pose mode, move cursor in local space */
1399         if (t->flag & (T_EDIT | T_POSE)) {
1400                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1401                 float mat[3][3], imat[3][3];
1402                 
1403                 sub_v3_v3v3(t->center, t->center, ob->obmat[3]);
1404                 copy_m3_m4(mat, ob->obmat);
1405                 invert_m3_m3(imat, mat);
1406                 mul_m3_v3(imat, t->center);
1407         }
1408         
1409         calculateCenter2D(t);
1410 }
1411
1412 void calculateCenterCursor2D(TransInfo *t)
1413 {
1414         float aspx = 1.0, aspy = 1.0;
1415         float *cursor = NULL;
1416         
1417         if (t->spacetype == SPACE_IMAGE) {
1418                 SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
1419                 /* only space supported right now but may change */
1420                 ED_space_image_uv_aspect(sima, &aspx, &aspy);
1421                 cursor = sima->cursor;
1422         }
1423         
1424         if (cursor) {
1425                 t->center[0] = cursor[0] * aspx;
1426                 t->center[1] = cursor[1] * aspy;
1427         }
1428         
1429         calculateCenter2D(t);
1430 }
1431
1432 static void calculateCenterCursorGraph2D(TransInfo *t)
1433 {
1434         SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
1435         Scene *scene = t->scene;
1436         
1437         /* cursor is combination of current frame, and graph-editor cursor value */
1438         t->center[0] = (float)(scene->r.cfra);
1439         t->center[1] = sipo->cursorVal;
1440         
1441         calculateCenter2D(t);
1442 }
1443
1444 void calculateCenterMedian(TransInfo *t)
1445 {
1446         float partial[3] = {0.0f, 0.0f, 0.0f};
1447         int total = 0;
1448         int i;
1449         
1450         for (i = 0; i < t->total; i++) {
1451                 if (t->data[i].flag & TD_SELECTED) {
1452                         if (!(t->data[i].flag & TD_NOCENTER)) {
1453                                 add_v3_v3(partial, t->data[i].center);
1454                                 total++;
1455                         }
1456                 }
1457                 else {
1458                         /*
1459                          * All the selected elements are at the head of the array
1460                          * which means we can stop when it finds unselected data
1461                          */
1462                         break;
1463                 }
1464         }
1465         if (i)
1466                 mul_v3_fl(partial, 1.0f / total);
1467         copy_v3_v3(t->center, partial);
1468         
1469         calculateCenter2D(t);
1470 }
1471
1472 void calculateCenterBound(TransInfo *t)
1473 {
1474         float max[3];
1475         float min[3];
1476         int i;
1477         for (i = 0; i < t->total; i++) {
1478                 if (i) {
1479                         if (t->data[i].flag & TD_SELECTED) {
1480                                 if (!(t->data[i].flag & TD_NOCENTER))
1481                                         minmax_v3v3_v3(min, max, t->data[i].center);
1482                         }
1483                         else {
1484                                 /*
1485                                  * All the selected elements are at the head of the array
1486                                  * which means we can stop when it finds unselected data
1487                                  */
1488                                 break;
1489                         }
1490                 }
1491                 else {
1492                         copy_v3_v3(max, t->data[i].center);
1493                         copy_v3_v3(min, t->data[i].center);
1494                 }
1495         }
1496         add_v3_v3v3(t->center, min, max);
1497         mul_v3_fl(t->center, 0.5);
1498         
1499         calculateCenter2D(t);
1500 }
1501
1502 void calculateCenter(TransInfo *t)
1503 {
1504         switch (t->around) {
1505                 case V3D_CENTER:
1506                         calculateCenterBound(t);
1507                         break;
1508                 case V3D_CENTROID:
1509                         calculateCenterMedian(t);
1510                         break;
1511                 case V3D_CURSOR:
1512                         if (t->spacetype == SPACE_IMAGE)
1513                                 calculateCenterCursor2D(t);
1514                         else if (t->spacetype == SPACE_IPO)
1515                                 calculateCenterCursorGraph2D(t);
1516                         else
1517                                 calculateCenterCursor(t);
1518                         break;
1519                 case V3D_LOCAL:
1520                         /* Individual element center uses median center for helpline and such */
1521                         calculateCenterMedian(t);
1522                         break;
1523                 case V3D_ACTIVE:
1524                 {
1525                         /* set median, and if if if... do object center */
1526                 
1527                         /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1528
1529                         if (t->obedit) {
1530                                 if (t->obedit && t->obedit->type == OB_MESH) {
1531                                         BMEditSelection ese;
1532                                         BMEditMesh *em = BMEdit_FromObject(t->obedit);
1533
1534                                         if (BM_select_history_active_get(em->bm, &ese)) {
1535                                                 BM_editselection_center(&ese, t->center);
1536                                                 calculateCenter2D(t);
1537                                                 break;
1538                                         }
1539                                 }
1540                                 else if (ELEM(t->obedit->type, OB_CURVE, OB_SURF)) {
1541                                         float center[3];
1542                                         Curve *cu = (Curve *)t->obedit->data;
1543
1544                                         if (ED_curve_actSelection(cu, center)) {
1545                                                 copy_v3_v3(t->center, center);
1546                                                 calculateCenter2D(t);
1547                                                 break;
1548                                         }
1549                                 }
1550                         } /* END EDIT MODE ACTIVE ELEMENT */
1551
1552                         calculateCenterMedian(t);
1553                         if ((t->flag & (T_EDIT | T_POSE)) == 0) {
1554                                 Scene *scene = t->scene;
1555                                 Object *ob = OBACT;
1556                                 if (ob) {
1557                                         copy_v3_v3(t->center, ob->obmat[3]);
1558                                         projectIntView(t, t->center, t->center2d);
1559                                 }
1560                         }
1561                 
1562                 }
1563         }
1564         
1565         /* setting constraint center */
1566         copy_v3_v3(t->con.center, t->center);
1567         if (t->flag & (T_EDIT | T_POSE)) {
1568                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1569                 mul_m4_v3(ob->obmat, t->con.center);
1570         }
1571         
1572         /* for panning from cameraview */
1573         if (t->flag & T_OBJECT) {
1574                 if (t->spacetype == SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW) {
1575                         View3D *v3d = t->view;
1576                         Scene *scene = t->scene;
1577                         RegionView3D *rv3d = t->ar->regiondata;
1578                         
1579                         if (v3d->camera == OBACT && rv3d->persp == RV3D_CAMOB) {
1580                                 float axis[3];
1581                                 /* persinv is nasty, use viewinv instead, always right */
1582                                 copy_v3_v3(axis, t->viewinv[2]);
1583                                 normalize_v3(axis);
1584                                 
1585                                 /* 6.0 = 6 grid units */
1586                                 axis[0] = t->center[0] - 6.0f * axis[0];
1587                                 axis[1] = t->center[1] - 6.0f * axis[1];
1588                                 axis[2] = t->center[2] - 6.0f * axis[2];
1589                                 
1590                                 projectIntView(t, axis, t->center2d);
1591                                 
1592                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1593                                 if (t->mode == TFM_TRANSLATION) {
1594                                         copy_v3_v3(t->center, axis);
1595                                         copy_v3_v3(t->con.center, t->center);
1596                                 }
1597                         }
1598                 }
1599         }
1600         
1601         if (t->spacetype == SPACE_VIEW3D) {
1602                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1603                 if (t->flag & (T_EDIT | T_POSE)) {
1604                         Object *ob = t->obedit ? t->obedit : t->poseobj;
1605                         float vec[3];
1606                         
1607                         copy_v3_v3(vec, t->center);
1608                         mul_m4_v3(ob->obmat, vec);
1609                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1610                 }
1611                 else {
1612                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1613                 }
1614         }
1615 }
1616
1617 void calculatePropRatio(TransInfo *t)
1618 {
1619         TransData *td = t->data;
1620         int i;
1621         float dist;
1622         short connected = t->flag & T_PROP_CONNECTED;
1623
1624         if (t->flag & T_PROP_EDIT) {
1625                 for (i = 0; i < t->total; i++, td++) {
1626                         if (td->flag & TD_SELECTED) {
1627                                 td->factor = 1.0f;
1628                         }
1629                         else if (t->flag & T_MIRROR && td->loc[0] * t->mirror < -0.00001f) {
1630                                 td->flag |= TD_SKIP;
1631                                 td->factor = 0.0f;
1632                                 restoreElement(td);
1633                         }
1634                         else if ((connected && (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size)) ||
1635                                  (connected == 0 && td->rdist > t->prop_size))
1636                         {
1637                                 /*
1638                                  * The elements are sorted according to their dist member in the array,
1639                                  * that means we can stop when it finds one element outside of the propsize.
1640                                  */
1641                                 td->flag |= TD_NOACTION;
1642                                 td->factor = 0.0f;
1643                                 restoreElement(td);
1644                         }
1645                         else {
1646                                 /* Use rdist for falloff calculations, it is the real distance */
1647                                 td->flag &= ~TD_NOACTION;
1648                                 
1649                                 if (connected)
1650                                         dist = (t->prop_size - td->dist) / t->prop_size;
1651                                 else
1652                                         dist = (t->prop_size - td->rdist) / t->prop_size;
1653
1654                                 /*
1655                                  * Clamp to positive numbers.
1656                                  * Certain corner cases with connectivity and individual centers
1657                                  * can give values of rdist larger than propsize.
1658                                  */
1659                                 if (dist < 0.0f)
1660                                         dist = 0.0f;
1661                                 
1662                                 switch (t->prop_mode) {
1663                                         case PROP_SHARP:
1664                                                 td->factor = dist * dist;
1665                                                 break;
1666                                         case PROP_SMOOTH:
1667                                                 td->factor = 3.0f * dist * dist - 2.0f * dist * dist * dist;
1668                                                 break;
1669                                         case PROP_ROOT:
1670                                                 td->factor = (float)sqrt(dist);
1671                                                 break;
1672                                         case PROP_LIN:
1673                                                 td->factor = dist;
1674                                                 break;
1675                                         case PROP_CONST:
1676                                                 td->factor = 1.0f;
1677                                                 break;
1678                                         case PROP_SPHERE:
1679                                                 td->factor = (float)sqrt(2 * dist - dist * dist);
1680                                                 break;
1681                                         case PROP_RANDOM:
1682                                                 BLI_srand(BLI_rand()); /* random seed */
1683                                                 td->factor = BLI_frand() * dist;
1684                                                 break;
1685                                         default:
1686                                                 td->factor = 1;
1687                                 }
1688                         }
1689                 }
1690                 switch (t->prop_mode) {
1691                         case PROP_SHARP:
1692                                 strcpy(t->proptext, "(Sharp)");
1693                                 break;
1694                         case PROP_SMOOTH:
1695                                 strcpy(t->proptext, "(Smooth)");
1696                                 break;
1697                         case PROP_ROOT:
1698                                 strcpy(t->proptext, "(Root)");
1699                                 break;
1700                         case PROP_LIN:
1701                                 strcpy(t->proptext, "(Linear)");
1702                                 break;
1703                         case PROP_CONST:
1704                                 strcpy(t->proptext, "(Constant)");
1705                                 break;
1706                         case PROP_SPHERE:
1707                                 strcpy(t->proptext, "(Sphere)");
1708                                 break;
1709                         case PROP_RANDOM:
1710                                 strcpy(t->proptext, "(Random)");
1711                                 break;
1712                         default:
1713                                 t->proptext[0] = '\0';
1714                 }
1715         }
1716         else {
1717                 for (i = 0; i < t->total; i++, td++) {
1718                         td->factor = 1.0;
1719                 }
1720                 t->proptext[0] = '\0';
1721         }
1722 }