match function names for clip/image spaces
[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                 if (t->obedit) {
1104                         /* UV transform */
1105                 }
1106                 else if (sima->mode == SI_MODE_MASK) {
1107                         t->options |= CTX_MASK;
1108                 }
1109                 else {
1110                         BLI_assert(0);
1111                 }
1112         }
1113         else if (t->spacetype == SPACE_NODE) {
1114                 // XXX for now, get View2D from the active region
1115                 t->view = &ar->v2d;
1116                 t->around = V3D_CENTER;
1117         }
1118         else if (t->spacetype == SPACE_IPO) {
1119                 SpaceIpo *sipo = sa->spacedata.first;
1120                 t->view = &ar->v2d;
1121                 t->around = sipo->around;
1122         }
1123         else if (t->spacetype == SPACE_CLIP) {
1124                 SpaceClip *sclip = sa->spacedata.first;
1125                 t->view = &ar->v2d;
1126                 t->around = sclip->around;
1127
1128                 if (ED_space_clip_check_show_trackedit(sclip))
1129                         t->options |= CTX_MOVIECLIP;
1130                 else if (ED_space_clip_check_show_maskedit(sclip))
1131                         t->options |= CTX_MASK;
1132         }
1133         else {
1134                 if (ar) {
1135                         // XXX for now, get View2D  from the active region
1136                         t->view = &ar->v2d;
1137                         // XXX for now, the center point is the midpoint of the data
1138                 }
1139                 else {
1140                         t->view = NULL;
1141                 }
1142                 t->around = V3D_CENTER;
1143         }
1144         
1145         if (op && RNA_struct_property_is_set(op->ptr, "release_confirm")) {
1146                 if (RNA_boolean_get(op->ptr, "release_confirm")) {
1147                         t->flag |= T_RELEASE_CONFIRM;
1148                 }
1149         }
1150         else {
1151                 if (U.flag & USER_RELEASECONFIRM) {
1152                         t->flag |= T_RELEASE_CONFIRM;
1153                 }
1154         }
1155
1156         if (op && (RNA_struct_find_property(op->ptr, "mirror") &&
1157                    RNA_struct_property_is_set(op->ptr, "mirror")))
1158         {
1159                 if (RNA_boolean_get(op->ptr, "mirror")) {
1160                         t->flag |= T_MIRROR;
1161                         t->mirror = 1;
1162                 }
1163         }
1164         // Need stuff to take it from edit mesh or whatnot here
1165         else if (t->spacetype == SPACE_VIEW3D) {
1166                 if (t->obedit && t->obedit->type == OB_MESH && (((Mesh *)t->obedit->data)->editflag & ME_EDIT_MIRROR_X)) {
1167                         t->flag |= T_MIRROR;
1168                         t->mirror = 1;
1169                 }
1170         }
1171         
1172         /* setting PET flag only if property exist in operator. Otherwise, assume it's not supported */
1173         if (op && RNA_struct_find_property(op->ptr, "proportional")) {
1174                 if (RNA_struct_property_is_set(op->ptr, "proportional")) {
1175                         switch (RNA_enum_get(op->ptr, "proportional")) {
1176                                 case PROP_EDIT_CONNECTED:
1177                                         t->flag |= T_PROP_CONNECTED;
1178                                 case PROP_EDIT_ON:
1179                                         t->flag |= T_PROP_EDIT;
1180                                         break;
1181                         }
1182                 }
1183                 else {
1184                         /* use settings from scene only if modal */
1185                         if (t->flag & T_MODAL) {
1186                                 if ((t->options & CTX_NO_PET) == 0) {
1187                                         if (t->obedit && ts->proportional != PROP_EDIT_OFF) {
1188                                                 t->flag |= T_PROP_EDIT;
1189
1190                                                 if (ts->proportional == PROP_EDIT_CONNECTED) {
1191                                                         t->flag |= T_PROP_CONNECTED;
1192                                                 }
1193                                         }
1194                                         else if (t->options & CTX_MASK) {
1195                                                 if (ts->proportional_mask) {
1196                                                         t->flag |= T_PROP_EDIT;
1197
1198                                                         if (ts->proportional == PROP_EDIT_CONNECTED) {
1199                                                                 t->flag |= T_PROP_CONNECTED;
1200                                                         }
1201                                                 }
1202                                         }
1203                                         else if (t->obedit == NULL && ts->proportional_objects) {
1204                                                 t->flag |= T_PROP_EDIT;
1205                                         }
1206                                 }
1207                         }
1208                 }
1209                 
1210                 if (op && (RNA_struct_find_property(op->ptr, "proportional_size") &&
1211                            RNA_struct_property_is_set(op->ptr, "proportional_size")))
1212                 {
1213                         t->prop_size = RNA_float_get(op->ptr, "proportional_size");
1214                 }
1215                 else {
1216                         t->prop_size = ts->proportional_size;
1217                 }
1218                 
1219                 
1220                 /* TRANSFORM_FIX_ME rna restrictions */
1221                 if (t->prop_size <= 0.00001f) {
1222                         printf("Proportional size (%f) under 0.00001, reseting to 1!\n", t->prop_size);
1223                         t->prop_size = 1.0f;
1224                 }
1225                 
1226                 if (op && (RNA_struct_find_property(op->ptr, "proportional_edit_falloff") &&
1227                            RNA_struct_property_is_set(op->ptr, "proportional_edit_falloff")))
1228                 {
1229                         t->prop_mode = RNA_enum_get(op->ptr, "proportional_edit_falloff");
1230                 }
1231                 else {
1232                         t->prop_mode = ts->prop_mode;
1233                 }
1234         }
1235         else { /* add not pet option to context when not available */
1236                 t->options |= CTX_NO_PET;
1237         }
1238         
1239         // Mirror is not supported with PET, turn it off.
1240         if (t->flag & T_PROP_EDIT) {
1241                 t->flag &= ~T_MIRROR;
1242         }
1243
1244         setTransformViewMatrices(t);
1245         initNumInput(&t->num);
1246         
1247         return 1;
1248 }
1249
1250 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
1251 void postTrans(bContext *C, TransInfo *t)
1252 {
1253         TransData *td;
1254         
1255         if (t->draw_handle_view)
1256                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_view);
1257         if (t->draw_handle_apply)
1258                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_apply);
1259         if (t->draw_handle_pixel)
1260                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_pixel);
1261         if (t->draw_handle_cursor)
1262                 WM_paint_cursor_end(CTX_wm_manager(C), t->draw_handle_cursor);
1263
1264         if (t->customFree) {
1265                 /* Can take over freeing t->data and data2d etc... */
1266                 t->customFree(t);
1267         }
1268         else if ((t->customData != NULL) && (t->flag & T_FREE_CUSTOMDATA)) {
1269                 MEM_freeN(t->customData);
1270         }
1271
1272         /* postTrans can be called when nothing is selected, so data is NULL already */
1273         if (t->data) {
1274                 int a;
1275                 
1276                 /* free data malloced per trans-data */
1277                 for (a = 0, td = t->data; a < t->total; a++, td++) {
1278                         if (td->flag & TD_BEZTRIPLE) 
1279                                 MEM_freeN(td->hdata);
1280                 }
1281                 MEM_freeN(t->data);
1282         }
1283         
1284         BLI_freelistN(&t->tsnap.points);
1285
1286         if (t->ext) MEM_freeN(t->ext);
1287         if (t->data2d) {
1288                 MEM_freeN(t->data2d);
1289                 t->data2d = NULL;
1290         }
1291         
1292         if (t->spacetype == SPACE_IMAGE) {
1293                 if (t->options & CTX_MASK) {
1294                         /* pass */
1295                 }
1296                 else {
1297                         SpaceImage *sima = t->sa->spacedata.first;
1298                         if (sima->flag & SI_LIVE_UNWRAP)
1299                                 ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1300                 }
1301         }
1302         else if (t->spacetype == SPACE_VIEW3D) {
1303                 View3D *v3d = t->sa->spacedata.first;
1304                 /* restore manipulator */
1305                 if (t->flag & T_MODAL) {
1306                         v3d->twtype = t->twtype;
1307                 }
1308         }
1309         
1310         if (t->mouse.data) {
1311                 MEM_freeN(t->mouse.data);
1312         }
1313 }
1314
1315 void applyTransObjects(TransInfo *t)
1316 {
1317         TransData *td;
1318         
1319         for (td = t->data; td < t->data + t->total; td++) {
1320                 copy_v3_v3(td->iloc, td->loc);
1321                 if (td->ext->rot) {
1322                         copy_v3_v3(td->ext->irot, td->ext->rot);
1323                 }
1324                 if (td->ext->size) {
1325                         copy_v3_v3(td->ext->isize, td->ext->size);
1326                 }
1327         }
1328         recalcData(t);
1329 }
1330
1331 static void restoreElement(TransData *td)
1332 {
1333         /* TransData for crease has no loc */
1334         if (td->loc) {
1335                 copy_v3_v3(td->loc, td->iloc);
1336         }
1337         if (td->val) {
1338                 *td->val = td->ival;
1339         }
1340
1341         if (td->ext && (td->flag & TD_NO_EXT) == 0) {
1342                 if (td->ext->rot) {
1343                         copy_v3_v3(td->ext->rot, td->ext->irot);
1344                 }
1345                 if (td->ext->rotAngle) {
1346                         *td->ext->rotAngle = td->ext->irotAngle;
1347                 }
1348                 if (td->ext->rotAxis) {
1349                         copy_v3_v3(td->ext->rotAxis, td->ext->irotAxis);
1350                 }
1351                 /* XXX, drotAngle & drotAxis not used yet */
1352                 if (td->ext->size) {
1353                         copy_v3_v3(td->ext->size, td->ext->isize);
1354                 }
1355                 if (td->ext->quat) {
1356                         copy_qt_qt(td->ext->quat, td->ext->iquat);
1357                 }
1358         }
1359         
1360         if (td->flag & TD_BEZTRIPLE) {
1361                 *(td->hdata->h1) = td->hdata->ih1;
1362                 *(td->hdata->h2) = td->hdata->ih2;
1363         }
1364 }
1365
1366 void restoreTransObjects(TransInfo *t)
1367 {
1368         TransData *td;
1369         TransData2D *td2d;
1370
1371         for (td = t->data; td < t->data + t->total; td++) {
1372                 restoreElement(td);
1373         }
1374         
1375         for (td2d = t->data2d; t->data2d && td2d < t->data2d + t->total; td2d++) {
1376                 if (td2d->h1) {
1377                         td2d->h1[0] = td2d->ih1[0];
1378                         td2d->h1[1] = td2d->ih1[1];
1379                 }
1380                 if (td2d->h2) {
1381                         td2d->h2[0] = td2d->ih2[0];
1382                         td2d->h2[1] = td2d->ih2[1];
1383                 }
1384         }
1385
1386         unit_m3(t->mat);
1387         
1388         recalcData(t);
1389 }
1390
1391 void calculateCenter2D(TransInfo *t)
1392 {
1393         if (t->flag & (T_EDIT | T_POSE)) {
1394                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1395                 float vec[3];
1396                 
1397                 copy_v3_v3(vec, t->center);
1398                 mul_m4_v3(ob->obmat, vec);
1399                 projectIntView(t, vec, t->center2d);
1400         }
1401         else {
1402                 projectIntView(t, t->center, t->center2d);
1403         }
1404 }
1405
1406 void calculateCenterCursor(TransInfo *t)
1407 {
1408         float *cursor;
1409         
1410         cursor = give_cursor(t->scene, t->view);
1411         copy_v3_v3(t->center, cursor);
1412         
1413         /* If edit or pose mode, move cursor in local space */
1414         if (t->flag & (T_EDIT | T_POSE)) {
1415                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1416                 float mat[3][3], imat[3][3];
1417                 
1418                 sub_v3_v3v3(t->center, t->center, ob->obmat[3]);
1419                 copy_m3_m4(mat, ob->obmat);
1420                 invert_m3_m3(imat, mat);
1421                 mul_m3_v3(imat, t->center);
1422         }
1423         
1424         calculateCenter2D(t);
1425 }
1426
1427 void calculateCenterCursor2D(TransInfo *t)
1428 {
1429         float aspx = 1.0, aspy = 1.0;
1430         float *cursor = NULL;
1431         
1432         if (t->spacetype == SPACE_IMAGE) {
1433                 SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
1434                 /* only space supported right now but may change */
1435                 ED_space_image_get_uv_aspect(sima, &aspx, &aspy);
1436                 cursor = sima->cursor;
1437         }
1438         
1439         if (cursor) {
1440                 t->center[0] = cursor[0] * aspx;
1441                 t->center[1] = cursor[1] * aspy;
1442         }
1443         
1444         calculateCenter2D(t);
1445 }
1446
1447 static void calculateCenterCursorGraph2D(TransInfo *t)
1448 {
1449         SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
1450         Scene *scene = t->scene;
1451         
1452         /* cursor is combination of current frame, and graph-editor cursor value */
1453         t->center[0] = (float)(scene->r.cfra);
1454         t->center[1] = sipo->cursorVal;
1455         
1456         calculateCenter2D(t);
1457 }
1458
1459 void calculateCenterMedian(TransInfo *t)
1460 {
1461         float partial[3] = {0.0f, 0.0f, 0.0f};
1462         int total = 0;
1463         int i;
1464         
1465         for (i = 0; i < t->total; i++) {
1466                 if (t->data[i].flag & TD_SELECTED) {
1467                         if (!(t->data[i].flag & TD_NOCENTER)) {
1468                                 add_v3_v3(partial, t->data[i].center);
1469                                 total++;
1470                         }
1471                 }
1472                 else {
1473                         /*
1474                          * All the selected elements are at the head of the array
1475                          * which means we can stop when it finds unselected data
1476                          */
1477                         break;
1478                 }
1479         }
1480         if (i)
1481                 mul_v3_fl(partial, 1.0f / total);
1482         copy_v3_v3(t->center, partial);
1483         
1484         calculateCenter2D(t);
1485 }
1486
1487 void calculateCenterBound(TransInfo *t)
1488 {
1489         float max[3];
1490         float min[3];
1491         int i;
1492         for (i = 0; i < t->total; i++) {
1493                 if (i) {
1494                         if (t->data[i].flag & TD_SELECTED) {
1495                                 if (!(t->data[i].flag & TD_NOCENTER))
1496                                         minmax_v3v3_v3(min, max, t->data[i].center);
1497                         }
1498                         else {
1499                                 /*
1500                                  * All the selected elements are at the head of the array
1501                                  * which means we can stop when it finds unselected data
1502                                  */
1503                                 break;
1504                         }
1505                 }
1506                 else {
1507                         copy_v3_v3(max, t->data[i].center);
1508                         copy_v3_v3(min, t->data[i].center);
1509                 }
1510         }
1511         add_v3_v3v3(t->center, min, max);
1512         mul_v3_fl(t->center, 0.5);
1513         
1514         calculateCenter2D(t);
1515 }
1516
1517 void calculateCenter(TransInfo *t)
1518 {
1519         switch (t->around) {
1520                 case V3D_CENTER:
1521                         calculateCenterBound(t);
1522                         break;
1523                 case V3D_CENTROID:
1524                         calculateCenterMedian(t);
1525                         break;
1526                 case V3D_CURSOR:
1527                         if (t->spacetype == SPACE_IMAGE)
1528                                 calculateCenterCursor2D(t);
1529                         else if (t->spacetype == SPACE_IPO)
1530                                 calculateCenterCursorGraph2D(t);
1531                         else
1532                                 calculateCenterCursor(t);
1533                         break;
1534                 case V3D_LOCAL:
1535                         /* Individual element center uses median center for helpline and such */
1536                         calculateCenterMedian(t);
1537                         break;
1538                 case V3D_ACTIVE:
1539                 {
1540                         /* set median, and if if if... do object center */
1541                 
1542                         /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1543
1544                         if (t->obedit) {
1545                                 if (t->obedit && t->obedit->type == OB_MESH) {
1546                                         BMEditSelection ese;
1547                                         BMEditMesh *em = BMEdit_FromObject(t->obedit);
1548
1549                                         if (BM_select_history_active_get(em->bm, &ese)) {
1550                                                 BM_editselection_center(&ese, t->center);
1551                                                 calculateCenter2D(t);
1552                                                 break;
1553                                         }
1554                                 }
1555                                 else if (ELEM(t->obedit->type, OB_CURVE, OB_SURF)) {
1556                                         float center[3];
1557                                         Curve *cu = (Curve *)t->obedit->data;
1558
1559                                         if (ED_curve_actSelection(cu, center)) {
1560                                                 copy_v3_v3(t->center, center);
1561                                                 calculateCenter2D(t);
1562                                                 break;
1563                                         }
1564                                 }
1565                         } /* END EDIT MODE ACTIVE ELEMENT */
1566
1567                         calculateCenterMedian(t);
1568                         if ((t->flag & (T_EDIT | T_POSE)) == 0) {
1569                                 Scene *scene = t->scene;
1570                                 Object *ob = OBACT;
1571                                 if (ob) {
1572                                         copy_v3_v3(t->center, ob->obmat[3]);
1573                                         projectIntView(t, t->center, t->center2d);
1574                                 }
1575                         }
1576                 
1577                 }
1578         }
1579         
1580         /* setting constraint center */
1581         copy_v3_v3(t->con.center, t->center);
1582         if (t->flag & (T_EDIT | T_POSE)) {
1583                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1584                 mul_m4_v3(ob->obmat, t->con.center);
1585         }
1586         
1587         /* for panning from cameraview */
1588         if (t->flag & T_OBJECT) {
1589                 if (t->spacetype == SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW) {
1590                         View3D *v3d = t->view;
1591                         Scene *scene = t->scene;
1592                         RegionView3D *rv3d = t->ar->regiondata;
1593                         
1594                         if (v3d->camera == OBACT && rv3d->persp == RV3D_CAMOB) {
1595                                 float axis[3];
1596                                 /* persinv is nasty, use viewinv instead, always right */
1597                                 copy_v3_v3(axis, t->viewinv[2]);
1598                                 normalize_v3(axis);
1599                                 
1600                                 /* 6.0 = 6 grid units */
1601                                 axis[0] = t->center[0] - 6.0f * axis[0];
1602                                 axis[1] = t->center[1] - 6.0f * axis[1];
1603                                 axis[2] = t->center[2] - 6.0f * axis[2];
1604                                 
1605                                 projectIntView(t, axis, t->center2d);
1606                                 
1607                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1608                                 if (t->mode == TFM_TRANSLATION) {
1609                                         copy_v3_v3(t->center, axis);
1610                                         copy_v3_v3(t->con.center, t->center);
1611                                 }
1612                         }
1613                 }
1614         }
1615         
1616         if (t->spacetype == SPACE_VIEW3D) {
1617                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1618                 if (t->flag & (T_EDIT | T_POSE)) {
1619                         Object *ob = t->obedit ? t->obedit : t->poseobj;
1620                         float vec[3];
1621                         
1622                         copy_v3_v3(vec, t->center);
1623                         mul_m4_v3(ob->obmat, vec);
1624                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1625                 }
1626                 else {
1627                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1628                 }
1629         }
1630 }
1631
1632 void calculatePropRatio(TransInfo *t)
1633 {
1634         TransData *td = t->data;
1635         int i;
1636         float dist;
1637         short connected = t->flag & T_PROP_CONNECTED;
1638
1639         if (t->flag & T_PROP_EDIT) {
1640                 for (i = 0; i < t->total; i++, td++) {
1641                         if (td->flag & TD_SELECTED) {
1642                                 td->factor = 1.0f;
1643                         }
1644                         else if (t->flag & T_MIRROR && td->loc[0] * t->mirror < -0.00001f) {
1645                                 td->flag |= TD_SKIP;
1646                                 td->factor = 0.0f;
1647                                 restoreElement(td);
1648                         }
1649                         else if ((connected && (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size)) ||
1650                                  (connected == 0 && td->rdist > t->prop_size))
1651                         {
1652                                 /*
1653                                  * The elements are sorted according to their dist member in the array,
1654                                  * that means we can stop when it finds one element outside of the propsize.
1655                                  */
1656                                 td->flag |= TD_NOACTION;
1657                                 td->factor = 0.0f;
1658                                 restoreElement(td);
1659                         }
1660                         else {
1661                                 /* Use rdist for falloff calculations, it is the real distance */
1662                                 td->flag &= ~TD_NOACTION;
1663                                 
1664                                 if (connected)
1665                                         dist = (t->prop_size - td->dist) / t->prop_size;
1666                                 else
1667                                         dist = (t->prop_size - td->rdist) / t->prop_size;
1668
1669                                 /*
1670                                  * Clamp to positive numbers.
1671                                  * Certain corner cases with connectivity and individual centers
1672                                  * can give values of rdist larger than propsize.
1673                                  */
1674                                 if (dist < 0.0f)
1675                                         dist = 0.0f;
1676                                 
1677                                 switch (t->prop_mode) {
1678                                         case PROP_SHARP:
1679                                                 td->factor = dist * dist;
1680                                                 break;
1681                                         case PROP_SMOOTH:
1682                                                 td->factor = 3.0f * dist * dist - 2.0f * dist * dist * dist;
1683                                                 break;
1684                                         case PROP_ROOT:
1685                                                 td->factor = (float)sqrt(dist);
1686                                                 break;
1687                                         case PROP_LIN:
1688                                                 td->factor = dist;
1689                                                 break;
1690                                         case PROP_CONST:
1691                                                 td->factor = 1.0f;
1692                                                 break;
1693                                         case PROP_SPHERE:
1694                                                 td->factor = (float)sqrt(2 * dist - dist * dist);
1695                                                 break;
1696                                         case PROP_RANDOM:
1697                                                 BLI_srand(BLI_rand()); /* random seed */
1698                                                 td->factor = BLI_frand() * dist;
1699                                                 break;
1700                                         default:
1701                                                 td->factor = 1;
1702                                 }
1703                         }
1704                 }
1705                 switch (t->prop_mode) {
1706                         case PROP_SHARP:
1707                                 strcpy(t->proptext, "(Sharp)");
1708                                 break;
1709                         case PROP_SMOOTH:
1710                                 strcpy(t->proptext, "(Smooth)");
1711                                 break;
1712                         case PROP_ROOT:
1713                                 strcpy(t->proptext, "(Root)");
1714                                 break;
1715                         case PROP_LIN:
1716                                 strcpy(t->proptext, "(Linear)");
1717                                 break;
1718                         case PROP_CONST:
1719                                 strcpy(t->proptext, "(Constant)");
1720                                 break;
1721                         case PROP_SPHERE:
1722                                 strcpy(t->proptext, "(Sphere)");
1723                                 break;
1724                         case PROP_RANDOM:
1725                                 strcpy(t->proptext, "(Random)");
1726                                 break;
1727                         default:
1728                                 t->proptext[0] = '\0';
1729                 }
1730         }
1731         else {
1732                 for (i = 0; i < t->total; i++, td++) {
1733                         td->factor = 1.0;
1734                 }
1735                 t->proptext[0] = '\0';
1736         }
1737 }