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