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