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