svn merge -r 30954:31059 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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 #include <string.h>
31 #include <math.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLO_sys_types.h" // for intptr_t support
36
37 #include "DNA_anim_types.h"
38 #include "DNA_armature_types.h"
39 #include "DNA_lattice_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_space_types.h"
43 #include "DNA_view3d_types.h"
44
45 #include "RNA_access.h"
46
47 //#include "BIF_screen.h"
48 //#include "BIF_mywindow.h"
49 #include "BIF_gl.h"
50 #include "BIF_glutil.h"
51 //#include "BIF_editmesh.h"
52 //#include "BIF_editsima.h"
53 //#include "BIF_editparticle.h"
54 //#include "BIF_meshtools.h"
55
56 #include "BKE_animsys.h"
57 #include "BKE_action.h"
58 #include "BKE_armature.h"
59 #include "BKE_cloth.h"
60 #include "BKE_curve.h"
61 #include "BKE_depsgraph.h"
62 #include "BKE_displist.h"
63 #include "BKE_depsgraph.h"
64 #include "BKE_fcurve.h"
65 #include "BKE_global.h"
66 #include "BKE_group.h"
67 #include "BKE_lattice.h"
68 #include "BKE_key.h"
69 #include "BKE_mesh.h"
70 #include "BKE_modifier.h"
71 #include "BKE_nla.h"
72 #include "BKE_object.h"
73 #include "BKE_utildefines.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
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(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         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         Scene *scene = t->scene;
330         Base *base = scene->basact;
331
332         if (t->spacetype==SPACE_NODE) {
333                 flushTransNodes(t);
334         }
335         else if (t->spacetype==SPACE_SEQ) {
336                 flushTransSeq(t);
337         }
338         else if (t->spacetype == SPACE_ACTION) {
339                 Scene *scene= t->scene;
340                 SpaceAction *saction= (SpaceAction *)t->sa->spacedata.first;
341                 
342                 bAnimContext ac;
343                 ListBase anim_data = {NULL, NULL};
344                 bAnimListElem *ale;
345                 int filter;
346                 
347                 /* initialise relevant anim-context 'context' data from TransInfo data */
348                         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
349                 memset(&ac, 0, sizeof(bAnimContext));
350                 
351                 ac.scene= t->scene;
352                 ac.obact= OBACT;
353                 ac.sa= t->sa;
354                 ac.ar= t->ar;
355                 ac.spacetype= (t->sa)? t->sa->spacetype : 0;
356                 ac.regiontype= (t->ar)? t->ar->regiontype : 0;
357                 
358                 ANIM_animdata_context_getdata(&ac);
359                 
360                 /* get animdata blocks visible in editor, assuming that these will be the ones where things changed */
361                 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_ANIMDATA);
362                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
363                 
364                 /* just tag these animdata-blocks to recalc, assuming that some data there changed 
365                  * BUT only do this if realtime updates are enabled
366                  */
367                 if ((saction->flag & SACTION_NOREALTIMEUPDATES) == 0) {
368                         for (ale= anim_data.first; ale; ale= ale->next) {
369                                 /* set refresh tags for objects using this animation */
370                                 ANIM_list_elem_update(t->scene, ale);
371                         }
372                 }
373                 
374                 /* now free temp channels */
375                 BLI_freelistN(&anim_data);
376         }
377         else if (t->spacetype == SPACE_IPO) {
378                 Scene *scene;
379                 SpaceIpo *sipo= (SpaceIpo *)t->sa->spacedata.first;
380                 
381                 ListBase anim_data = {NULL, NULL};
382                 bAnimContext ac;
383                 int filter;
384                 
385                 bAnimListElem *ale;
386                 int dosort = 0;
387                 
388                 
389                 /* initialise relevant anim-context 'context' data from TransInfo data */
390                         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
391                 memset(&ac, 0, sizeof(bAnimContext));
392                 
393                 scene= ac.scene= t->scene;
394                 ac.obact= OBACT;
395                 ac.sa= t->sa;
396                 ac.ar= t->ar;
397                 ac.spacetype= (t->sa)? t->sa->spacetype : 0;
398                 ac.regiontype= (t->ar)? t->ar->regiontype : 0;
399                 
400                 ANIM_animdata_context_getdata(&ac);
401                 
402                 /* do the flush first */
403                 flushTransGraphData(t);
404                 
405                 /* get curves to check if a re-sort is needed */
406                 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY | ANIMFILTER_CURVEVISIBLE);
407                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
408                 
409                 /* now test if there is a need to re-sort */
410                 for (ale= anim_data.first; ale; ale= ale->next) {
411                         FCurve *fcu= (FCurve *)ale->key_data;
412                         
413                         /* ignore unselected fcurves */
414                         if (!fcu_test_selected(fcu))
415                                 continue;
416                         
417                         // fixme: only do this for selected verts...
418                         ANIM_unit_mapping_apply_fcurve(ac.scene, ale->id, ale->key_data, ANIM_UNITCONV_ONLYSEL|ANIM_UNITCONV_SELVERTS|ANIM_UNITCONV_RESTORE);
419                         
420                         
421                         /* watch it: if the time is wrong: do not correct handles yet */
422                         if (test_time_fcurve(fcu))
423                                 dosort++;
424                         else
425                                 calchandles_fcurve(fcu);
426                         
427                         /* set refresh tags for objects using this animation,
428                          * BUT only if realtime updates are enabled  
429                          */
430                         if ((sipo->flag & SIPO_NOREALTIMEUPDATES) == 0)
431                                 ANIM_list_elem_update(t->scene, ale);
432                 }
433                 
434                 /* do resort and other updates? */
435                 if (dosort) remake_graph_transdata(t, &anim_data);
436                 
437                 /* now free temp channels */
438                 BLI_freelistN(&anim_data);
439         }
440         else if (t->spacetype == SPACE_NLA) {
441                 TransDataNla *tdn= (TransDataNla *)t->customData;
442                 SpaceNla *snla= (SpaceNla *)t->sa->spacedata.first;
443                 Scene *scene= t->scene;
444                 double secf= FPS;
445                 int i;
446                 
447                 /* for each strip we've got, perform some additional validation of the values that got set before
448                  * using RNA to set the value (which does some special operations when setting these values to make
449                  * sure that everything works ok)
450                  */
451                 for (i = 0; i < t->total; i++, tdn++) {
452                         NlaStrip *strip= tdn->strip;
453                         PointerRNA strip_ptr;
454                         short pExceeded, nExceeded, iter;
455                         int delta_y1, delta_y2;
456                         
457                         /* if this tdn has no handles, that means it is just a dummy that should be skipped */
458                         if (tdn->handle == 0)
459                                 continue;
460                         
461                         /* set refresh tags for objects using this animation,
462                          * BUT only if realtime updates are enabled  
463                          */
464                         if ((snla->flag & SNLA_NOREALTIMEUPDATES) == 0)
465                                 ANIM_id_update(t->scene, tdn->id);
466                         
467                         /* if cancelling transform, just write the values without validating, then move on */
468                         if (t->state == TRANS_CANCEL) {
469                                 /* clear the values by directly overwriting the originals, but also need to restore
470                                  * endpoints of neighboring transition-strips
471                                  */
472                                 
473                                 /* start */
474                                 strip->start= tdn->h1[0];
475                                 
476                                 if ((strip->prev) && (strip->prev->type == NLASTRIP_TYPE_TRANSITION))
477                                         strip->prev->end= tdn->h1[0];
478                                 
479                                 /* end */
480                                 strip->end= tdn->h2[0];
481                                 
482                                 if ((strip->next) && (strip->next->type == NLASTRIP_TYPE_TRANSITION))
483                                         strip->next->start= tdn->h2[0];
484                                 
485                                 /* flush transforms to child strips (since this should be a meta) */
486                                 BKE_nlameta_flush_transforms(strip);
487                                 
488                                 /* restore to original track (if needed) */
489                                 if (tdn->oldTrack != tdn->nlt) {
490                                         /* just append to end of list for now, since strips get sorted in special_aftertrans_update() */
491                                         BLI_remlink(&tdn->nlt->strips, strip);
492                                         BLI_addtail(&tdn->oldTrack->strips, strip);
493                                 }
494                                 
495                                 continue;
496                         }
497                         
498                         /* firstly, check if the proposed transform locations would overlap with any neighbouring strips
499                          * (barring transitions) which are absolute barriers since they are not being moved
500                          *
501                          * this is done as a iterative procedure (done 5 times max for now)
502                          */
503                         for (iter=0; iter < 5; iter++) {
504                                 pExceeded= ((strip->prev) && (strip->prev->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h1[0] < strip->prev->end));
505                                 nExceeded= ((strip->next) && (strip->next->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h2[0] > strip->next->start));
506                                 
507                                 if ((pExceeded && nExceeded) || (iter == 4) ) {
508                                         /* both endpoints exceeded (or iteration ping-pong'd meaning that we need a compromise)
509                                          *      - simply crop strip to fit within the bounds of the strips bounding it
510                                          *      - if there were no neighbours, clear the transforms (make it default to the strip's current values)
511                                          */
512                                         if (strip->prev && strip->next) {
513                                                 tdn->h1[0]= strip->prev->end;
514                                                 tdn->h2[0]= strip->next->start;
515                                         }
516                                         else {
517                                                 tdn->h1[0]= strip->start;
518                                                 tdn->h2[0]= strip->end;
519                                         }
520                                 }
521                                 else if (nExceeded) {
522                                         /* move backwards */
523                                         float offset= tdn->h2[0] - strip->next->start;
524                                         
525                                         tdn->h1[0] -= offset;
526                                         tdn->h2[0] -= offset;
527                                 }
528                                 else if (pExceeded) {
529                                         /* more forwards */
530                                         float offset= strip->prev->end - tdn->h1[0];
531                                         
532                                         tdn->h1[0] += offset;
533                                         tdn->h2[0] += offset;
534                                 }
535                                 else /* all is fine and well */
536                                         break;
537                         }
538                         
539                         /* handle auto-snapping */
540                         switch (snla->autosnap) {
541                                 case SACTSNAP_FRAME: /* snap to nearest frame/time  */
542                                         if (snla->flag & SNLA_DRAWTIME) {
543                                                 tdn->h1[0]= (float)( floor((tdn->h1[0]/secf) + 0.5f) * secf );
544                                                 tdn->h2[0]= (float)( floor((tdn->h2[0]/secf) + 0.5f) * secf );
545                                         }
546                                         else {
547                                                 tdn->h1[0]= (float)( floor(tdn->h1[0]+0.5f) );
548                                                 tdn->h2[0]= (float)( floor(tdn->h2[0]+0.5f) );
549                                         }
550                                         break;
551                                 
552                                 case SACTSNAP_MARKER: /* snap to nearest marker */
553                                         tdn->h1[0]= (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h1[0]);
554                                         tdn->h2[0]= (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h2[0]);
555                                         break;
556                         }
557                         
558                         /* use RNA to write the values... */
559                         // TODO: do we need to write in 2 passes to make sure that no truncation goes on?
560                         RNA_pointer_create(NULL, &RNA_NlaStrip, strip, &strip_ptr);
561                         
562                         RNA_float_set(&strip_ptr, "frame_start", tdn->h1[0]);
563                         RNA_float_set(&strip_ptr, "frame_end", tdn->h2[0]);
564                         
565                         /* flush transforms to child strips (since this should be a meta) */
566                         BKE_nlameta_flush_transforms(strip);
567                         
568                         
569                         /* now, check if we need to try and move track
570                          *      - we need to calculate both, as only one may have been altered by transform if only 1 handle moved
571                          */
572                         delta_y1= ((int)tdn->h1[1] / NLACHANNEL_STEP - tdn->trackIndex);
573                         delta_y2= ((int)tdn->h2[1] / NLACHANNEL_STEP - tdn->trackIndex);
574                         
575                         if (delta_y1 || delta_y2) {
576                                 NlaTrack *track;
577                                 int delta = (delta_y2) ? delta_y2 : delta_y1;
578                                 int n;
579                                 
580                                 /* move in the requested direction, checking at each layer if there's space for strip to pass through,
581                                  * stopping on the last track available or that we're able to fit in
582                                  */
583                                 if (delta > 0) {
584                                         for (track=tdn->nlt->next, n=0; (track) && (n < delta); track=track->next, n++) {
585                                                 /* check if space in this track for the strip */
586                                                 if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
587                                                         /* move strip to this track */
588                                                         BLI_remlink(&tdn->nlt->strips, strip);
589                                                         BKE_nlatrack_add_strip(track, strip);
590                                                         
591                                                         tdn->nlt= track;
592                                                         tdn->trackIndex += (n + 1); /* + 1, since n==0 would mean that we didn't change track */
593                                                 }
594                                                 else /* can't move any further */
595                                                         break;
596                                         }
597                                 }
598                                 else {
599                                         /* make delta 'positive' before using it, since we now know to go backwards */
600                                         delta= -delta;
601                                         
602                                         for (track=tdn->nlt->prev, n=0; (track) && (n < delta); track=track->prev, n++) {
603                                                 /* check if space in this track for the strip */
604                                                 if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
605                                                         /* move strip to this track */
606                                                         BLI_remlink(&tdn->nlt->strips, strip);
607                                                         BKE_nlatrack_add_strip(track, strip);
608                                                         
609                                                         tdn->nlt= track;
610                                                         tdn->trackIndex -= (n - 1); /* - 1, since n==0 would mean that we didn't change track */
611                                                 }
612                                                 else /* can't move any further */
613                                                         break;
614                                         }
615                                 }
616                         }
617                 }
618         }
619         else if (t->spacetype == SPACE_IMAGE) {
620                 if (t->obedit && t->obedit->type == OB_MESH) {
621                         SpaceImage *sima= t->sa->spacedata.first;
622                         
623                         flushTransUVs(t);
624                         if(sima->flag & SI_LIVE_UNWRAP)
625                                 ED_uvedit_live_unwrap_re_solve();
626                         
627                         DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);
628                 }
629         }
630         else if (t->spacetype == SPACE_VIEW3D) {
631                 
632                 /* project */
633                 if(t->state != TRANS_CANCEL) {
634                         applyProject(t);
635                 }
636                 
637                 if (t->obedit) {
638                         if ELEM(t->obedit->type, OB_CURVE, OB_SURF) {
639                                 Curve *cu= t->obedit->data;
640                                 ListBase *nurbs= ED_curve_editnurbs(cu);
641                                 Nurb *nu= nurbs->first;
642
643                                 if(t->state != TRANS_CANCEL) {
644                                         clipMirrorModifier(t, t->obedit);
645                                 }
646
647                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* 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                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* sets recalc flags */
666         
667                                 if(la->editlatt->flag & LT_OUTSIDE) outside_lattice(la->editlatt);
668                         }
669                         else if (t->obedit->type == OB_MESH) {
670                                 EditMesh *em = ((Mesh*)t->obedit->data)->edit_mesh;
671                                 /* mirror modifier clipping? */
672                                 if(t->state != TRANS_CANCEL) {
673                                         clipMirrorModifier(t, t->obedit);
674                                 }
675                                 if((t->options & CTX_NO_MIRROR) == 0 && (t->flag & T_MIRROR))
676                                         editmesh_apply_to_mirror(t);
677                                         
678                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* sets recalc flags */
679                                 
680                                 recalc_editnormals(em);
681                         }
682                         else if(t->obedit->type==OB_ARMATURE) { /* no recalc flag, does pose */
683                                 bArmature *arm= t->obedit->data;
684                                 ListBase *edbo = arm->edbo;
685                                 EditBone *ebo;
686                                 TransData *td = t->data;
687                                 int i;
688                                 
689                                 /* Ensure all bones are correctly adjusted */
690                                 for (ebo = edbo->first; ebo; ebo = ebo->next){
691                                         
692                                         if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
693                                                 /* If this bone has a parent tip that has been moved */
694                                                 if (ebo->parent->flag & BONE_TIPSEL){
695                                                         VECCOPY (ebo->head, ebo->parent->tail);
696                                                         if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
697                                                 }
698                                                 /* If this bone has a parent tip that has NOT been moved */
699                                                 else{
700                                                         VECCOPY (ebo->parent->tail, ebo->head);
701                                                         if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
702                                                 }
703                                         }
704                                         
705                                         /* on extrude bones, oldlength==0.0f, so we scale radius of points */
706                                         ebo->length= len_v3v3(ebo->head, ebo->tail);
707                                         if(ebo->oldlength==0.0f) {
708                                                 ebo->rad_head= 0.25f*ebo->length;
709                                                 ebo->rad_tail= 0.10f*ebo->length;
710                                                 ebo->dist= 0.25f*ebo->length;
711                                                 if(ebo->parent) {
712                                                         if(ebo->rad_head > ebo->parent->rad_tail)
713                                                                 ebo->rad_head= ebo->parent->rad_tail;
714                                                 }
715                                         }
716                                         else if(t->mode!=TFM_BONE_ENVELOPE) {
717                                                 /* if bones change length, lets do that for the deform distance as well */
718                                                 ebo->dist*= ebo->length/ebo->oldlength;
719                                                 ebo->rad_head*= ebo->length/ebo->oldlength;
720                                                 ebo->rad_tail*= ebo->length/ebo->oldlength;
721                                                 ebo->oldlength= ebo->length;
722                                         }
723                                 }
724                                 
725                                 
726                                 if (t->mode != TFM_BONE_ROLL)
727                                 {
728                                         /* fix roll */
729                                         for(i = 0; i < t->total; i++, td++)
730                                         {
731                                                 if (td->extra)
732                                                 {
733                                                         float vec[3], up_axis[3];
734                                                         float qrot[4];
735                                                         
736                                                         ebo = td->extra;
737                                                         VECCOPY(up_axis, td->axismtx[2]);
738                                                         
739                                                         if (t->mode != TFM_ROTATION)
740                                                         {
741                                                                 sub_v3_v3v3(vec, ebo->tail, ebo->head);
742                                                                 normalize_v3(vec);
743                                                                 rotation_between_vecs_to_quat(qrot, td->axismtx[1], vec);
744                                                                 mul_qt_v3(qrot, up_axis);
745                                                         }
746                                                         else
747                                                         {
748                                                                 mul_m3_v3(t->mat, up_axis);
749                                                         }
750                                                         
751                                                         ebo->roll = ED_rollBoneToVector(ebo, up_axis);
752                                                 }
753                                         }
754                                 }
755                                 
756                                 if(arm->flag & ARM_MIRROR_EDIT)
757                                         transform_armature_mirror_update(t->obedit);
758                                 
759                         }
760                         else
761                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* sets recalc flags */
762                 }
763                 else if( (t->flag & T_POSE) && t->poseobj) {
764                         Object *ob= t->poseobj;
765                         bArmature *arm= ob->data;
766                         
767                         /* if animtimer is running, and the object already has animation data,
768                          * check if the auto-record feature means that we should record 'samples'
769                          * (i.e. uneditable animation values)
770                          */
771                         // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
772                         if ((t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
773                                 int targetless_ik= (t->flag & T_AUTOIK); // XXX this currently doesn't work, since flags aren't set yet!
774                                 
775                                 animrecord_check_state(t->scene, &ob->id, t->animtimer);
776                                 autokeyframe_pose_cb_func(NULL, t->scene, (View3D *)t->view, ob, t->mode, targetless_ik);
777                         }
778                         
779                         /* old optimize trick... this enforces to bypass the depgraph */
780                         if (!(arm->flag & ARM_DELAYDEFORM)) {
781                                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);  /* sets recalc flags */
782                         }
783                         else
784                                 where_is_pose(scene, ob);
785                 }
786                 else if(base && (base->object->mode & OB_MODE_PARTICLE_EDIT) && PE_get_current(scene, base->object)) {
787                         flushTransParticles(t);
788                 }
789                 else {
790                         int i;
791                         
792                         for (i = 0; i < t->total; i++) {
793                                 TransData *td = t->data + i;
794                                 Object *ob = td->ob;
795                                 
796                                 if (td->flag & TD_NOACTION)
797                                         break;
798                                 
799                                 if (td->flag & TD_SKIP)
800                                         continue;
801                                 
802                                 /* if animtimer is running, and the object already has animation data,
803                                  * check if the auto-record feature means that we should record 'samples'
804                                  * (i.e. uneditable animation values)
805                                  */
806                                 // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
807                                 if ((t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
808                                         animrecord_check_state(t->scene, &ob->id, t->animtimer);
809                                         autokeyframe_ob_cb_func(NULL, t->scene, (View3D *)t->view, ob, t->mode);
810                                 }
811                                 
812                                 /* sets recalc flags fully, instead of flushing existing ones 
813                                  * otherwise proxies don't function correctly
814                                  */
815                                 DAG_id_flush_update(&ob->id, OB_RECALC_OB);
816                         }
817                 }
818                 
819                 if(((View3D*)t->view)->drawtype == OB_SHADED)
820                         reshadeall_displist(t->scene);
821         }
822 }
823
824 void drawLine(TransInfo *t, float *center, float *dir, char axis, short options)
825 {
826         float v1[3], v2[3], v3[3];
827         char col[3], col2[3];
828
829         if (t->spacetype == SPACE_VIEW3D)
830         {
831                 View3D *v3d = t->view;
832                 
833                 glPushMatrix();
834                 
835                 //if(t->obedit) glLoadMatrixf(t->obedit->obmat);        // sets opengl viewing
836                 
837                 
838                 copy_v3_v3(v3, dir);
839                 mul_v3_fl(v3, v3d->far);
840                 
841                 sub_v3_v3v3(v2, center, v3);
842                 add_v3_v3v3(v1, center, v3);
843                 
844                 if (options & DRAWLIGHT) {
845                         col[0] = col[1] = col[2] = 220;
846                 }
847                 else {
848                         UI_GetThemeColor3ubv(TH_GRID, col);
849                 }
850                 UI_make_axis_color(col, col2, axis);
851                 glColor3ubv((GLubyte *)col2);
852                 
853                 setlinestyle(0);
854                 glBegin(GL_LINE_STRIP);
855                         glVertex3fv(v1);
856                         glVertex3fv(v2);
857                 glEnd();
858                 
859                 glPopMatrix();
860         }
861 }
862
863 void resetTransRestrictions(TransInfo *t)
864 {
865         t->flag &= ~T_ALL_RESTRICTIONS;
866 }
867
868 int initTransInfo (bContext *C, TransInfo *t, wmOperator *op, wmEvent *event)
869 {
870         Scene *sce = CTX_data_scene(C);
871         ToolSettings *ts = CTX_data_tool_settings(C);
872         ARegion *ar = CTX_wm_region(C);
873         ScrArea *sa = CTX_wm_area(C);
874         Object *obedit = CTX_data_edit_object(C);
875         
876         /* moving: is shown in drawobject() (transform color) */
877 //  TRANSFORM_FIX_ME
878 //      if(obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
879 //      else if(G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
880 //      else G.moving= G_TRANSFORM_OBJ;
881         
882         t->scene = sce;
883         t->sa = sa;
884         t->ar = ar;
885         t->obedit = obedit;
886         t->settings = ts;
887         
888         t->data = NULL;
889         t->ext = NULL;
890         
891         t->helpline = HLP_NONE;
892         
893         t->flag = 0;
894         
895         t->redraw = 1; /* redraw first time */
896         
897         if (event)
898         {
899                 t->imval[0] = event->x - t->ar->winrct.xmin;
900                 t->imval[1] = event->y - t->ar->winrct.ymin;
901                 
902                 t->event_type = event->type;
903         }
904         else
905         {
906                 t->imval[0] = 0;
907                 t->imval[1] = 0;
908         }
909         
910         t->con.imval[0] = t->imval[0];
911         t->con.imval[1] = t->imval[1];
912         
913         t->mval[0] = t->imval[0];
914         t->mval[1] = t->imval[1];
915         
916         t->transform            = NULL;
917         t->handleEvent          = NULL;
918         
919         t->total                        = 0;
920         
921         t->val = 0.0f;
922         
923         t->vec[0]                       =
924                 t->vec[1]               =
925                 t->vec[2]               = 0.0f;
926         
927         t->center[0]            =
928                 t->center[1]    =
929                 t->center[2]    = 0.0f;
930         
931         unit_m3(t->mat);
932         
933         /* if there's an event, we're modal */
934         if (event) {
935                 t->flag |= T_MODAL;
936         }
937
938         /* Crease needs edge flag */
939         if (t->mode == TFM_CREASE) {
940                 t->options |= CTX_EDGE;
941         }
942
943         t->spacetype = sa->spacetype;
944         if(t->spacetype == SPACE_VIEW3D)
945         {
946                 View3D *v3d = sa->spacedata.first;
947                 
948                 t->view = v3d;
949                 t->animtimer= CTX_wm_screen(C)->animtimer;
950                 
951                 /* turn manipulator off during transform */
952                 // FIXME: but don't do this when USING the manipulator...
953                 if (t->flag & T_MODAL) {
954                         t->twtype = v3d->twtype;
955                         v3d->twtype = 0;
956                 }
957
958                 if(v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
959                 t->around = v3d->around;
960                 
961                 if (op && RNA_struct_find_property(op->ptr, "constraint_orientation") && RNA_property_is_set(op->ptr, "constraint_orientation"))
962                 {
963                         t->current_orientation = RNA_enum_get(op->ptr, "constraint_orientation");
964                         
965                         if (t->current_orientation >= V3D_MANIP_CUSTOM + BIF_countTransformOrientation(C))
966                         {
967                                 t->current_orientation = V3D_MANIP_GLOBAL;
968                         }
969                 }
970                 else
971                 {
972                         t->current_orientation = v3d->twmode;
973                 }
974         }
975         else if(t->spacetype==SPACE_IMAGE || t->spacetype==SPACE_NODE)
976         {
977                 SpaceImage *sima = sa->spacedata.first;
978                 // XXX for now, get View2D  from the active region
979                 t->view = &ar->v2d;
980                 t->around = sima->around;
981         }
982         else if(t->spacetype==SPACE_IPO) 
983         {
984                 SpaceIpo *sipo= sa->spacedata.first;
985                 t->view = &ar->v2d;
986                 t->around = sipo->around;
987         }
988         else
989         {
990                 // XXX for now, get View2D  from the active region
991                 t->view = &ar->v2d;
992                 // XXX for now, the center point is the midpoint of the data
993                 t->around = V3D_CENTER;
994         }
995         
996         if (op && RNA_property_is_set(op->ptr, "release_confirm"))
997         {
998                 if (RNA_boolean_get(op->ptr, "release_confirm"))
999                 {
1000                         t->flag |= T_RELEASE_CONFIRM;
1001                 }
1002         }
1003         else
1004         {
1005                 if (U.flag & USER_RELEASECONFIRM)
1006                 {
1007                         t->flag |= T_RELEASE_CONFIRM;
1008                 }
1009         }
1010
1011         if (op && RNA_struct_find_property(op->ptr, "mirror") && RNA_property_is_set(op->ptr, "mirror"))
1012         {
1013                 if (RNA_boolean_get(op->ptr, "mirror"))
1014                 {
1015                         t->flag |= T_MIRROR;
1016                         t->mirror = 1;
1017                 }
1018         }
1019         // Need stuff to take it from edit mesh or whatnot here
1020         else
1021         {
1022                 if (t->obedit && t->obedit->type == OB_MESH && (((Mesh *)t->obedit->data)->editflag & ME_EDIT_MIRROR_X))
1023                 {
1024                         t->flag |= T_MIRROR;
1025                         t->mirror = 1;
1026                 }
1027         }
1028         
1029         /* setting PET flag only if property exist in operator. Otherwise, assume it's not supported */
1030         if (op && RNA_struct_find_property(op->ptr, "proportional"))
1031         {
1032                 if (RNA_property_is_set(op->ptr, "proportional"))
1033                 {
1034                         switch(RNA_enum_get(op->ptr, "proportional"))
1035                         {
1036                         case PROP_EDIT_CONNECTED:
1037                                 t->flag |= T_PROP_CONNECTED;
1038                         case PROP_EDIT_ON:
1039                                 t->flag |= T_PROP_EDIT;
1040                                 break;
1041                         }
1042                 }
1043                 else
1044                 {
1045                         /* use settings from scene only if modal */
1046                         if (t->flag & T_MODAL)
1047                         {
1048                                 if ((t->options & CTX_NO_PET) == 0)
1049                                 {
1050                                         if (t->obedit && ts->proportional != PROP_EDIT_OFF)
1051                                         {
1052                                                 t->flag |= T_PROP_EDIT;
1053
1054                                                 if(ts->proportional == PROP_EDIT_CONNECTED)
1055                                                         t->flag |= T_PROP_CONNECTED;
1056                                         }
1057                                         else if (t->obedit == NULL && ts->proportional_objects)
1058                                         {
1059                                                 t->flag |= T_PROP_EDIT;
1060                                         }
1061                                 }
1062                         }
1063                 }
1064                 
1065                 if (op && RNA_struct_find_property(op->ptr, "proportional_size") && RNA_property_is_set(op->ptr, "proportional_size"))
1066                 {
1067                         t->prop_size = RNA_float_get(op->ptr, "proportional_size");
1068                 }
1069                 else
1070                 {
1071                         t->prop_size = ts->proportional_size;
1072                 }
1073                 
1074                 
1075                 /* TRANSFORM_FIX_ME rna restrictions */
1076                 if (t->prop_size <= 0)
1077                 {
1078                         t->prop_size = 1.0f;
1079                 }
1080                 
1081                 if (op && RNA_struct_find_property(op->ptr, "proportional_editing_falloff") && RNA_property_is_set(op->ptr, "proportional_editing_falloff"))
1082                 {
1083                         t->prop_mode = RNA_enum_get(op->ptr, "proportional_editing_falloff");
1084                 }
1085                 else
1086                 {
1087                         t->prop_mode = ts->prop_mode;
1088                 }
1089         }
1090         else /* add not pet option to context when not available */
1091         {
1092                 t->options |= CTX_NO_PET;
1093         }
1094         
1095         setTransformViewMatrices(t);
1096         initNumInput(&t->num);
1097         initNDofInput(&t->ndof);
1098         
1099         return 1;
1100 }
1101
1102 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
1103 void postTrans (bContext *C, TransInfo *t)
1104 {
1105         TransData *td;
1106         
1107         if (t->draw_handle_view)
1108                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_view);
1109         if (t->draw_handle_apply)
1110                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_apply);
1111         if (t->draw_handle_pixel)
1112                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_pixel);
1113         if (t->draw_handle_cursor)
1114                 WM_paint_cursor_end(CTX_wm_manager(C), t->draw_handle_cursor);
1115
1116         if (t->customFree) {
1117                 /* Can take over freeing t->data and data2d etc... */
1118                 t->customFree(t);
1119         }
1120         else if (t->customData) {
1121                 MEM_freeN(t->customData);
1122         }
1123
1124         /* postTrans can be called when nothing is selected, so data is NULL already */
1125         if (t->data) {
1126                 int a;
1127                 
1128                 /* free data malloced per trans-data */
1129                 for(a=0, td= t->data; a<t->total; a++, td++) {
1130                         if (td->flag & TD_BEZTRIPLE) 
1131                                 MEM_freeN(td->hdata);
1132                 }
1133                 MEM_freeN(t->data);
1134         }
1135         
1136         BLI_freelistN(&t->tsnap.points);
1137
1138         if (t->ext) MEM_freeN(t->ext);
1139         if (t->data2d) {
1140                 MEM_freeN(t->data2d);
1141                 t->data2d= NULL;
1142         }
1143         
1144         if(t->spacetype==SPACE_IMAGE) {
1145                 SpaceImage *sima= t->sa->spacedata.first;
1146                 if(sima->flag & SI_LIVE_UNWRAP)
1147                         ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1148         }
1149         
1150         if (t->mouse.data)
1151         {
1152                 MEM_freeN(t->mouse.data);
1153         }
1154 }
1155
1156 void applyTransObjects(TransInfo *t)
1157 {
1158         TransData *td;
1159         
1160         for (td = t->data; td < t->data + t->total; td++) {
1161                 VECCOPY(td->iloc, td->loc);
1162                 if (td->ext->rot) {
1163                         VECCOPY(td->ext->irot, td->ext->rot);
1164                 }
1165                 if (td->ext->size) {
1166                         VECCOPY(td->ext->isize, td->ext->size);
1167                 }
1168         }
1169         recalcData(t);
1170 }
1171
1172 static void restoreElement(TransData *td) {
1173         /* TransData for crease has no loc */
1174         if (td->loc) {
1175                 VECCOPY(td->loc, td->iloc);
1176         }
1177         if (td->val) {
1178                 *td->val = td->ival;
1179         }
1180
1181         if (td->ext && (td->flag&TD_NO_EXT)==0) {
1182                 if (td->ext->rot) {
1183                         VECCOPY(td->ext->rot, td->ext->irot);
1184                 }
1185                 if (td->ext->size) {
1186                         VECCOPY(td->ext->size, td->ext->isize);
1187                 }
1188                 if (td->ext->quat) {
1189                         QUATCOPY(td->ext->quat, td->ext->iquat);
1190                 }
1191         }
1192         
1193         if (td->flag & TD_BEZTRIPLE) {
1194                 *(td->hdata->h1) = td->hdata->ih1;
1195                 *(td->hdata->h2) = td->hdata->ih2;
1196         }
1197 }
1198
1199 void restoreTransObjects(TransInfo *t)
1200 {
1201         TransData *td;
1202         TransData2D *td2d;
1203
1204         for (td = t->data; td < t->data + t->total; td++) {
1205                 restoreElement(td);
1206         }
1207         
1208         for (td2d=t->data2d; t->data2d && td2d < t->data2d + t->total; td2d++) {
1209                 if (td2d->h1) {
1210                         td2d->h1[0] = td2d->ih1[0];
1211                         td2d->h1[1] = td2d->ih1[1];
1212                 }
1213                 if (td2d->h2) {
1214                         td2d->h2[0] = td2d->ih2[0];
1215                         td2d->h2[1] = td2d->ih2[1];
1216                 }
1217         }
1218
1219         unit_m3(t->mat);
1220         
1221         recalcData(t);
1222 }
1223
1224 void calculateCenter2D(TransInfo *t)
1225 {
1226         if (t->flag & (T_EDIT|T_POSE)) {
1227                 Object *ob= t->obedit?t->obedit:t->poseobj;
1228                 float vec[3];
1229                 
1230                 VECCOPY(vec, t->center);
1231                 mul_m4_v3(ob->obmat, vec);
1232                 projectIntView(t, vec, t->center2d);
1233         }
1234         else {
1235                 projectIntView(t, t->center, t->center2d);
1236         }
1237 }
1238
1239 void calculateCenterCursor(TransInfo *t)
1240 {
1241         float *cursor;
1242         
1243         cursor = give_cursor(t->scene, t->view);
1244         VECCOPY(t->center, cursor);
1245         
1246         /* If edit or pose mode, move cursor in local space */
1247         if (t->flag & (T_EDIT|T_POSE)) {
1248                 Object *ob = t->obedit?t->obedit:t->poseobj;
1249                 float mat[3][3], imat[3][3];
1250                 
1251                 sub_v3_v3v3(t->center, t->center, ob->obmat[3]);
1252                 copy_m3_m4(mat, ob->obmat);
1253                 invert_m3_m3(imat, mat);
1254                 mul_m3_v3(imat, t->center);
1255         }
1256         
1257         calculateCenter2D(t);
1258 }
1259
1260 void calculateCenterCursor2D(TransInfo *t)
1261 {
1262         float aspx=1.0, aspy=1.0;
1263         float *cursor= NULL;
1264         
1265         if(t->spacetype==SPACE_IMAGE) {
1266                 SpaceImage *sima= (SpaceImage *)t->sa->spacedata.first;
1267                 /* only space supported right now but may change */
1268                 ED_space_image_uv_aspect(sima, &aspx, &aspy);
1269                 cursor = sima->cursor;
1270         }
1271         
1272         if (cursor) {
1273                 t->center[0] = cursor[0] * aspx;
1274                 t->center[1] = cursor[1] * aspy;
1275         }
1276         
1277         calculateCenter2D(t);
1278 }
1279
1280 void calculateCenterCursorGraph2D(TransInfo *t)
1281 {
1282         SpaceIpo *sipo= (SpaceIpo *)t->sa->spacedata.first;
1283         Scene *scene= t->scene;
1284         
1285         /* cursor is combination of current frame, and graph-editor cursor value */
1286         t->center[0]= (float)(scene->r.cfra);
1287         t->center[1]= sipo->cursorVal;
1288         
1289         calculateCenter2D(t);
1290 }
1291
1292 void calculateCenterMedian(TransInfo *t)
1293 {
1294         float partial[3] = {0.0f, 0.0f, 0.0f};
1295         int total = 0;
1296         int i;
1297         
1298         for(i = 0; i < t->total; i++) {
1299                 if (t->data[i].flag & TD_SELECTED) {
1300                         if (!(t->data[i].flag & TD_NOCENTER))
1301                         {
1302                                 add_v3_v3(partial, t->data[i].center);
1303                                 total++;
1304                         }
1305                 }
1306                 else {
1307                         /*
1308                            All the selected elements are at the head of the array
1309                            which means we can stop when it finds unselected data
1310                         */
1311                         break;
1312                 }
1313         }
1314         if(i)
1315                 mul_v3_fl(partial, 1.0f / total);
1316         VECCOPY(t->center, partial);
1317         
1318         calculateCenter2D(t);
1319 }
1320
1321 void calculateCenterBound(TransInfo *t)
1322 {
1323         float max[3];
1324         float min[3];
1325         int i;
1326         for(i = 0; i < t->total; i++) {
1327                 if (i) {
1328                         if (t->data[i].flag & TD_SELECTED) {
1329                                 if (!(t->data[i].flag & TD_NOCENTER))
1330                                         minmax_v3_v3v3(min, max, t->data[i].center);
1331                         }
1332                         else {
1333                                 /*
1334                                    All the selected elements are at the head of the array
1335                                    which means we can stop when it finds unselected data
1336                                 */
1337                                 break;
1338                         }
1339                 }
1340                 else {
1341                         VECCOPY(max, t->data[i].center);
1342                         VECCOPY(min, t->data[i].center);
1343                 }
1344         }
1345         add_v3_v3v3(t->center, min, max);
1346         mul_v3_fl(t->center, 0.5);
1347         
1348         calculateCenter2D(t);
1349 }
1350
1351 void calculateCenter(TransInfo *t)
1352 {
1353         switch(t->around) {
1354         case V3D_CENTER:
1355                 calculateCenterBound(t);
1356                 break;
1357         case V3D_CENTROID:
1358                 calculateCenterMedian(t);
1359                 break;
1360         case V3D_CURSOR:
1361                 if(t->spacetype==SPACE_IMAGE)
1362                         calculateCenterCursor2D(t);
1363                 else if(t->spacetype==SPACE_IPO)
1364                         calculateCenterCursorGraph2D(t);
1365                 else
1366                         calculateCenterCursor(t);
1367                 break;
1368         case V3D_LOCAL:
1369                 /* Individual element center uses median center for helpline and such */
1370                 calculateCenterMedian(t);
1371                 break;
1372         case V3D_ACTIVE:
1373                 {
1374                 /* set median, and if if if... do object center */
1375                 
1376                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1377
1378                 if (t->obedit && t->obedit->type == OB_MESH) {
1379                         EditSelection ese;
1380                         EditMesh *em = BKE_mesh_get_editmesh(t->obedit->data);
1381                         
1382                         if (EM_get_actSelection(em, &ese)) {
1383                                 EM_editselection_center(t->center, &ese);
1384                                 calculateCenter2D(t);
1385                                 break;
1386                         }
1387                 } /* END EDIT MODE ACTIVE ELEMENT */
1388                 
1389                 calculateCenterMedian(t);
1390                 if((t->flag & (T_EDIT|T_POSE))==0)
1391                 {
1392                         Scene *scene = t->scene;
1393                         Object *ob= OBACT;
1394                         if(ob)
1395                         {
1396                                 VECCOPY(t->center, ob->obmat[3]);
1397                                 projectIntView(t, t->center, t->center2d);
1398                         }
1399                 }
1400                 
1401                 }
1402         }
1403         
1404         /* setting constraint center */
1405         VECCOPY(t->con.center, t->center);
1406         if(t->flag & (T_EDIT|T_POSE))
1407         {
1408                 Object *ob= t->obedit?t->obedit:t->poseobj;
1409                 mul_m4_v3(ob->obmat, t->con.center);
1410         }
1411         
1412         /* for panning from cameraview */
1413         if(t->flag & T_OBJECT)
1414         {
1415                 if(t->spacetype==SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW)
1416                 {
1417                         View3D *v3d = t->view;
1418                         Scene *scene = t->scene;
1419                         RegionView3D *rv3d = t->ar->regiondata;
1420                         
1421                         if(v3d->camera == OBACT && rv3d->persp==RV3D_CAMOB)
1422                         {
1423                                 float axis[3];
1424                                 /* persinv is nasty, use viewinv instead, always right */
1425                                 VECCOPY(axis, t->viewinv[2]);
1426                                 normalize_v3(axis);
1427                                 
1428                                 /* 6.0 = 6 grid units */
1429                                 axis[0]= t->center[0]- 6.0f*axis[0];
1430                                 axis[1]= t->center[1]- 6.0f*axis[1];
1431                                 axis[2]= t->center[2]- 6.0f*axis[2];
1432                                 
1433                                 projectIntView(t, axis, t->center2d);
1434                                 
1435                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1436                                 if(t->mode==TFM_TRANSLATION)
1437                                 {
1438                                         VECCOPY(t->center, axis);
1439                                         VECCOPY(t->con.center, t->center);
1440                                 }
1441                         }
1442                 }
1443         }
1444         
1445         if(t->spacetype==SPACE_VIEW3D)
1446         {
1447                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1448                 if(t->flag & (T_EDIT|T_POSE)) {
1449                         Object *ob= t->obedit?t->obedit:t->poseobj;
1450                         float vec[3];
1451                         
1452                         VECCOPY(vec, t->center);
1453                         mul_m4_v3(ob->obmat, vec);
1454                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1455                 }
1456                 else {
1457                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1458                 }
1459         }
1460 }
1461
1462 void calculatePropRatio(TransInfo *t)
1463 {
1464         TransData *td = t->data;
1465         int i;
1466         float dist;
1467         short connected = t->flag & T_PROP_CONNECTED;
1468
1469         if (t->flag & T_PROP_EDIT) {
1470                 for(i = 0 ; i < t->total; i++, td++) {
1471                         if (td->flag & TD_SELECTED) {
1472                                 td->factor = 1.0f;
1473                         }
1474                         else if (t->flag & T_MIRROR && td->loc[0] * t->mirror < -0.00001f)
1475                         {
1476                                 td->flag |= TD_SKIP;
1477                                 td->factor = 0.0f;
1478                                 restoreElement(td);
1479                         }
1480                         else if ((connected &&
1481                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size))
1482                                 ||
1483                                         (connected == 0 &&
1484                                                 td->rdist > t->prop_size)) {
1485                                 /*
1486                                    The elements are sorted according to their dist member in the array,
1487                                    that means we can stop when it finds one element outside of the propsize.
1488                                 */
1489                                 td->flag |= TD_NOACTION;
1490                                 td->factor = 0.0f;
1491                                 restoreElement(td);
1492                         }
1493                         else {
1494                                 /* Use rdist for falloff calculations, it is the real distance */
1495                                 td->flag &= ~TD_NOACTION;
1496                                 
1497                                 if (connected)
1498                                         dist= (t->prop_size-td->dist)/t->prop_size;
1499                                 else
1500                                         dist= (t->prop_size-td->rdist)/t->prop_size;
1501
1502                                 /*
1503                                  * Clamp to positive numbers.
1504                                  * Certain corner cases with connectivity and individual centers
1505                                  * can give values of rdist larger than propsize.
1506                                  */
1507                                 if (dist < 0.0f)
1508                                         dist = 0.0f;
1509                                 
1510                                 switch(t->prop_mode) {
1511                                 case PROP_SHARP:
1512                                         td->factor= dist*dist;
1513                                         break;
1514                                 case PROP_SMOOTH:
1515                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1516                                         break;
1517                                 case PROP_ROOT:
1518                                         td->factor = (float)sqrt(dist);
1519                                         break;
1520                                 case PROP_LIN:
1521                                         td->factor = dist;
1522                                         break;
1523                                 case PROP_CONST:
1524                                         td->factor = 1.0f;
1525                                         break;
1526                                 case PROP_SPHERE:
1527                                         td->factor = (float)sqrt(2*dist - dist * dist);
1528                                         break;
1529                                 case PROP_RANDOM:
1530                                         BLI_srand( BLI_rand() ); /* random seed */
1531                                         td->factor = BLI_frand()*dist;
1532                                         break;
1533                                 default:
1534                                         td->factor = 1;
1535                                 }
1536                         }
1537                 }
1538                 switch(t->prop_mode) {
1539                 case PROP_SHARP:
1540                         strcpy(t->proptext, "(Sharp)");
1541                         break;
1542                 case PROP_SMOOTH:
1543                         strcpy(t->proptext, "(Smooth)");
1544                         break;
1545                 case PROP_ROOT:
1546                         strcpy(t->proptext, "(Root)");
1547                         break;
1548                 case PROP_LIN:
1549                         strcpy(t->proptext, "(Linear)");
1550                         break;
1551                 case PROP_CONST:
1552                         strcpy(t->proptext, "(Constant)");
1553                         break;
1554                 case PROP_SPHERE:
1555                         strcpy(t->proptext, "(Sphere)");
1556                         break;
1557                 case PROP_RANDOM:
1558                         strcpy(t->proptext, "(Random)");
1559                         break;
1560                 default:
1561                         strcpy(t->proptext, "");
1562                 }
1563         }
1564         else {
1565                 for(i = 0 ; i < t->total; i++, td++) {
1566                         td->factor = 1.0;
1567                 }
1568                 strcpy(t->proptext, "");
1569         }
1570 }
1571
1572 float get_drawsize(ARegion *ar, float *co)
1573 {
1574         RegionView3D *rv3d= ar->regiondata;
1575         float size, vec[3], len1, len2;
1576         
1577         /* size calculus, depending ortho/persp settings, like initgrabz() */
1578         size= rv3d->persmat[0][3]*co[0]+ rv3d->persmat[1][3]*co[1]+ rv3d->persmat[2][3]*co[2]+ rv3d->persmat[3][3];
1579         
1580         VECCOPY(vec, rv3d->persinv[0]);
1581         len1= normalize_v3(vec);
1582         VECCOPY(vec, rv3d->persinv[1]);
1583         len2= normalize_v3(vec);
1584         
1585         size*= 0.01f*(len1>len2?len1:len2);
1586         
1587         /* correct for window size to make widgets appear fixed size */
1588         if(ar->winx > ar->winy) size*= 1000.0f/(float)ar->winx;
1589         else size*= 1000.0f/(float)ar->winy;
1590         
1591         return size;
1592 }