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