soc-2008-mxcurioni: merged changes to revision 23516
[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 only if property exist in operator. Otherwise, assume it's not supported */
982         if (op && RNA_struct_find_property(op->ptr, "proportional"))
983         {
984                 if (RNA_property_is_set(op->ptr, "proportional"))
985                 {
986                         switch(RNA_enum_get(op->ptr, "proportional"))
987                         {
988                         case 2: /* XXX connected constant */
989                                 t->flag |= T_PROP_CONNECTED;
990                         case 1: /* XXX prop on constant */
991                                 t->flag |= T_PROP_EDIT;
992                                 break;
993                         }
994                 }
995                 else
996                 {
997                         if ((t->options & CTX_NO_PET) == 0 && (ts->proportional)) {
998                                 t->flag |= T_PROP_EDIT;
999
1000                                 if(ts->proportional == 2)
1001                                         t->flag |= T_PROP_CONNECTED;    // yes i know, has to become define
1002                         }
1003                 }
1004
1005                 if (op && RNA_struct_find_property(op->ptr, "proportional_size") && RNA_property_is_set(op->ptr, "proportional_size"))
1006                 {
1007                         t->prop_size = RNA_float_get(op->ptr, "proportional_size");
1008                 }
1009                 else
1010                 {
1011                         t->prop_size = ts->proportional_size;
1012                 }
1013
1014
1015                 /* TRANSFORM_FIX_ME rna restrictions */
1016                 if (t->prop_size <= 0)
1017                 {
1018                         t->prop_size = 1.0f;
1019                 }
1020                 
1021                 if (op && RNA_struct_find_property(op->ptr, "proportional_editing_falloff") && RNA_property_is_set(op->ptr, "proportional_editing_falloff"))
1022                 {
1023                         t->prop_mode = RNA_enum_get(op->ptr, "proportional_editing_falloff");
1024                 }
1025                 else
1026                 {
1027                         t->prop_mode = ts->prop_mode;
1028                 }
1029         }
1030         else /* add not pet option to context when not available */
1031         {
1032                 t->options |= CTX_NO_PET;
1033         }
1034
1035
1036         setTransformViewMatrices(t);
1037         initNumInput(&t->num);
1038         initNDofInput(&t->ndof);
1039
1040         return 1;
1041 }
1042
1043 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
1044 void postTrans (TransInfo *t)
1045 {
1046         TransData *td;
1047
1048         if (t->draw_handle)
1049         {
1050                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle);
1051         }
1052
1053         /* postTrans can be called when nothing is selected, so data is NULL already */
1054         if (t->data) {
1055                 int a;
1056
1057                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
1058                 for(a=0, td= t->data; a<t->total; a++, td++) {
1059                         if(td->tdi) MEM_freeN(td->tdi);
1060                         if (td->flag & TD_BEZTRIPLE) MEM_freeN(td->hdata);
1061                 }
1062                 MEM_freeN(t->data);
1063         }
1064         
1065         if (t->ext) MEM_freeN(t->ext);
1066         if (t->data2d) {
1067                 MEM_freeN(t->data2d);
1068                 t->data2d= NULL;
1069         }
1070
1071         if(t->spacetype==SPACE_IMAGE) {
1072                 SpaceImage *sima= t->sa->spacedata.first;
1073                 if(sima->flag & SI_LIVE_UNWRAP)
1074                         ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1075         }
1076         else if(ELEM(t->spacetype, SPACE_ACTION, SPACE_NLA)) {
1077         }
1078
1079         if (t->mouse.data)
1080         {
1081                 MEM_freeN(t->mouse.data);
1082         }
1083
1084         if (t->customFree) {
1085                 t->customFree(t);
1086         }
1087         else if (t->customData) {
1088                 MEM_freeN(t->customData);
1089         }
1090 }
1091
1092 void applyTransObjects(TransInfo *t)
1093 {
1094         TransData *td;
1095
1096         for (td = t->data; td < t->data + t->total; td++) {
1097                 VECCOPY(td->iloc, td->loc);
1098                 if (td->ext->rot) {
1099                         VECCOPY(td->ext->irot, td->ext->rot);
1100                 }
1101                 if (td->ext->size) {
1102                         VECCOPY(td->ext->isize, td->ext->size);
1103                 }
1104         }
1105         recalcData(t);
1106 }
1107
1108 /* helper for below */
1109 static void restore_ipokey(float *poin, float *old)
1110 {
1111         if(poin) {
1112                 poin[0]= old[0];
1113                 poin[-3]= old[3];
1114                 poin[3]= old[6];
1115         }
1116 }
1117
1118 static void restoreElement(TransData *td) {
1119         /* TransData for crease has no loc */
1120         if (td->loc) {
1121                 VECCOPY(td->loc, td->iloc);
1122         }
1123         if (td->val) {
1124                 *td->val = td->ival;
1125         }
1126         if (td->ext && (td->flag&TD_NO_EXT)==0) {
1127                 if (td->ext->rot) {
1128                         VECCOPY(td->ext->rot, td->ext->irot);
1129                 }
1130                 if (td->ext->size) {
1131                         VECCOPY(td->ext->size, td->ext->isize);
1132                 }
1133                 if(td->flag & TD_USEQUAT) {
1134                         if (td->ext->quat) {
1135                                 QUATCOPY(td->ext->quat, td->ext->iquat);
1136                         }
1137                 }
1138         }
1139
1140         if (td->flag & TD_BEZTRIPLE) {
1141                 *(td->hdata->h1) = td->hdata->ih1;
1142                 *(td->hdata->h2) = td->hdata->ih2;
1143         }
1144
1145         if(td->tdi) {
1146                 TransDataIpokey *tdi= td->tdi;
1147
1148                 restore_ipokey(tdi->locx, tdi->oldloc);
1149                 restore_ipokey(tdi->locy, tdi->oldloc+1);
1150                 restore_ipokey(tdi->locz, tdi->oldloc+2);
1151
1152                 restore_ipokey(tdi->rotx, tdi->oldrot);
1153                 restore_ipokey(tdi->roty, tdi->oldrot+1);
1154                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
1155
1156                 restore_ipokey(tdi->sizex, tdi->oldsize);
1157                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
1158                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
1159         }
1160 }
1161
1162 void restoreTransObjects(TransInfo *t)
1163 {
1164         TransData *td;
1165
1166         for (td = t->data; td < t->data + t->total; td++) {
1167                 restoreElement(td);
1168         }
1169
1170         Mat3One(t->mat);
1171
1172         recalcData(t);
1173 }
1174
1175 void calculateCenter2D(TransInfo *t)
1176 {
1177         if (t->flag & (T_EDIT|T_POSE)) {
1178                 Object *ob= t->obedit?t->obedit:t->poseobj;
1179                 float vec[3];
1180
1181                 VECCOPY(vec, t->center);
1182                 Mat4MulVecfl(ob->obmat, vec);
1183                 projectIntView(t, vec, t->center2d);
1184         }
1185         else {
1186                 projectIntView(t, t->center, t->center2d);
1187         }
1188 }
1189
1190 void calculateCenterCursor(TransInfo *t)
1191 {
1192         float *cursor;
1193
1194         cursor = give_cursor(t->scene, t->view);
1195         VECCOPY(t->center, cursor);
1196
1197         /* If edit or pose mode, move cursor in local space */
1198         if (t->flag & (T_EDIT|T_POSE)) {
1199                 Object *ob = t->obedit?t->obedit:t->poseobj;
1200                 float mat[3][3], imat[3][3];
1201
1202                 VecSubf(t->center, t->center, ob->obmat[3]);
1203                 Mat3CpyMat4(mat, ob->obmat);
1204                 Mat3Inv(imat, mat);
1205                 Mat3MulVecfl(imat, t->center);
1206         }
1207
1208         calculateCenter2D(t);
1209 }
1210
1211 void calculateCenterCursor2D(TransInfo *t)
1212 {
1213         View2D *v2d= t->view;
1214         float aspx=1.0, aspy=1.0;
1215
1216         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
1217                 ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
1218
1219         if (v2d) {
1220                 t->center[0] = v2d->cursor[0] * aspx;
1221                 t->center[1] = v2d->cursor[1] * aspy;
1222         }
1223
1224         calculateCenter2D(t);
1225 }
1226
1227 void calculateCenterMedian(TransInfo *t)
1228 {
1229         float partial[3] = {0.0f, 0.0f, 0.0f};
1230         int total = 0;
1231         int i;
1232
1233         for(i = 0; i < t->total; i++) {
1234                 if (t->data[i].flag & TD_SELECTED) {
1235                         if (!(t->data[i].flag & TD_NOCENTER))
1236                         {
1237                                 VecAddf(partial, partial, t->data[i].center);
1238                                 total++;
1239                         }
1240                 }
1241                 else {
1242                         /*
1243                            All the selected elements are at the head of the array
1244                            which means we can stop when it finds unselected data
1245                         */
1246                         break;
1247                 }
1248         }
1249         if(i)
1250                 VecMulf(partial, 1.0f / total);
1251         VECCOPY(t->center, partial);
1252
1253         calculateCenter2D(t);
1254 }
1255
1256 void calculateCenterBound(TransInfo *t)
1257 {
1258         float max[3];
1259         float min[3];
1260         int i;
1261         for(i = 0; i < t->total; i++) {
1262                 if (i) {
1263                         if (t->data[i].flag & TD_SELECTED) {
1264                                 if (!(t->data[i].flag & TD_NOCENTER))
1265                                         MinMax3(min, max, t->data[i].center);
1266                         }
1267                         else {
1268                                 /*
1269                                    All the selected elements are at the head of the array
1270                                    which means we can stop when it finds unselected data
1271                                 */
1272                                 break;
1273                         }
1274                 }
1275                 else {
1276                         VECCOPY(max, t->data[i].center);
1277                         VECCOPY(min, t->data[i].center);
1278                 }
1279         }
1280         VecAddf(t->center, min, max);
1281         VecMulf(t->center, 0.5);
1282
1283         calculateCenter2D(t);
1284 }
1285
1286 void calculateCenter(TransInfo *t)
1287 {
1288         switch(t->around) {
1289         case V3D_CENTER:
1290                 calculateCenterBound(t);
1291                 break;
1292         case V3D_CENTROID:
1293                 calculateCenterMedian(t);
1294                 break;
1295         case V3D_CURSOR:
1296                 if(t->spacetype==SPACE_IMAGE)
1297                         calculateCenterCursor2D(t);
1298                 else
1299                         calculateCenterCursor(t);
1300                 break;
1301         case V3D_LOCAL:
1302                 /* Individual element center uses median center for helpline and such */
1303                 calculateCenterMedian(t);
1304                 break;
1305         case V3D_ACTIVE:
1306                 {
1307                 /* set median, and if if if... do object center */
1308 #if 0 // TRANSFORM_FIX_ME
1309                 EditSelection ese;
1310                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1311
1312                 if (t->obedit && t->obedit->type == OB_MESH && EM_get_actSelection(&ese)) {
1313                         EM_editselection_center(t->center, &ese);
1314                         calculateCenter2D(t);
1315                         break;
1316                 } /* END EDIT MODE ACTIVE ELEMENT */
1317 #endif
1318
1319                 calculateCenterMedian(t);
1320                 if((t->flag & (T_EDIT|T_POSE))==0)
1321                 {
1322                         Scene *scene = t->scene;
1323                         Object *ob= OBACT;
1324                         if(ob)
1325                         {
1326                                 VECCOPY(t->center, ob->obmat[3]);
1327                                 projectIntView(t, t->center, t->center2d);
1328                         }
1329                 }
1330
1331                 }
1332         }
1333
1334         /* setting constraint center */
1335         VECCOPY(t->con.center, t->center);
1336         if(t->flag & (T_EDIT|T_POSE))
1337         {
1338                 Object *ob= t->obedit?t->obedit:t->poseobj;
1339                 Mat4MulVecfl(ob->obmat, t->con.center);
1340         }
1341
1342         /* voor panning from cameraview */
1343         if(t->flag & T_OBJECT)
1344         {
1345                 if(t->spacetype==SPACE_VIEW3D && t->ar->regiontype == RGN_TYPE_WINDOW)
1346                 {
1347                         View3D *v3d = t->view;
1348                         Scene *scene = t->scene;
1349                         RegionView3D *rv3d = t->ar->regiondata;
1350
1351                         if(v3d->camera == OBACT && rv3d->persp==V3D_CAMOB)
1352                         {
1353                                 float axis[3];
1354                                 /* persinv is nasty, use viewinv instead, always right */
1355                                 VECCOPY(axis, t->viewinv[2]);
1356                                 Normalize(axis);
1357
1358                                 /* 6.0 = 6 grid units */
1359                                 axis[0]= t->center[0]- 6.0f*axis[0];
1360                                 axis[1]= t->center[1]- 6.0f*axis[1];
1361                                 axis[2]= t->center[2]- 6.0f*axis[2];
1362
1363                                 projectIntView(t, axis, t->center2d);
1364
1365                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1366                                 if(t->mode==TFM_TRANSLATION)
1367                                 {
1368                                         VECCOPY(t->center, axis);
1369                                         VECCOPY(t->con.center, t->center);
1370                                 }
1371                         }
1372                 }
1373         }
1374
1375         if(t->spacetype==SPACE_VIEW3D)
1376         {
1377                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1378                 if(t->flag & (T_EDIT|T_POSE)) {
1379                         Object *ob= t->obedit?t->obedit:t->poseobj;
1380                         float vec[3];
1381
1382                         VECCOPY(vec, t->center);
1383                         Mat4MulVecfl(ob->obmat, vec);
1384                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1385                 }
1386                 else {
1387                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1388                 }
1389         }
1390 }
1391
1392 void calculatePropRatio(TransInfo *t)
1393 {
1394         TransData *td = t->data;
1395         int i;
1396         float dist;
1397         short connected = t->flag & T_PROP_CONNECTED;
1398
1399         if (t->flag & T_PROP_EDIT) {
1400                 for(i = 0 ; i < t->total; i++, td++) {
1401                         if (td->flag & TD_SELECTED) {
1402                                 td->factor = 1.0f;
1403                         }
1404                         else if ((connected &&
1405                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size))
1406                                 ||
1407                                         (connected == 0 &&
1408                                                 td->rdist > t->prop_size)) {
1409                                 /*
1410                                    The elements are sorted according to their dist member in the array,
1411                                    that means we can stop when it finds one element outside of the propsize.
1412                                 */
1413                                 td->flag |= TD_NOACTION;
1414                                 td->factor = 0.0f;
1415                                 restoreElement(td);
1416                         }
1417                         else {
1418                                 /* Use rdist for falloff calculations, it is the real distance */
1419                                 td->flag &= ~TD_NOACTION;
1420                                 dist= (t->prop_size-td->rdist)/t->prop_size;
1421
1422                                 /*
1423                                  * Clamp to positive numbers.
1424                                  * Certain corner cases with connectivity and individual centers
1425                                  * can give values of rdist larger than propsize.
1426                                  */
1427                                 if (dist < 0.0f)
1428                                         dist = 0.0f;
1429
1430                                 switch(t->prop_mode) {
1431                                 case PROP_SHARP:
1432                                         td->factor= dist*dist;
1433                                         break;
1434                                 case PROP_SMOOTH:
1435                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1436                                         break;
1437                                 case PROP_ROOT:
1438                                         td->factor = (float)sqrt(dist);
1439                                         break;
1440                                 case PROP_LIN:
1441                                         td->factor = dist;
1442                                         break;
1443                                 case PROP_CONST:
1444                                         td->factor = 1.0f;
1445                                         break;
1446                                 case PROP_SPHERE:
1447                                         td->factor = (float)sqrt(2*dist - dist * dist);
1448                                         break;
1449                                 case PROP_RANDOM:
1450                                         BLI_srand( BLI_rand() ); /* random seed */
1451                                         td->factor = BLI_frand()*dist;
1452                                         break;
1453                                 default:
1454                                         td->factor = 1;
1455                                 }
1456                         }
1457                 }
1458                 switch(t->prop_mode) {
1459                 case PROP_SHARP:
1460                         strcpy(t->proptext, "(Sharp)");
1461                         break;
1462                 case PROP_SMOOTH:
1463                         strcpy(t->proptext, "(Smooth)");
1464                         break;
1465                 case PROP_ROOT:
1466                         strcpy(t->proptext, "(Root)");
1467                         break;
1468                 case PROP_LIN:
1469                         strcpy(t->proptext, "(Linear)");
1470                         break;
1471                 case PROP_CONST:
1472                         strcpy(t->proptext, "(Constant)");
1473                         break;
1474                 case PROP_SPHERE:
1475                         strcpy(t->proptext, "(Sphere)");
1476                         break;
1477                 case PROP_RANDOM:
1478                         strcpy(t->proptext, "(Random)");
1479                         break;
1480                 default:
1481                         strcpy(t->proptext, "");
1482                 }
1483         }
1484         else {
1485                 for(i = 0 ; i < t->total; i++, td++) {
1486                         td->factor = 1.0;
1487                 }
1488                 strcpy(t->proptext, "");
1489         }
1490 }
1491
1492 float get_drawsize(ARegion *ar, float *co)
1493 {
1494         RegionView3D *rv3d= ar->regiondata;
1495         float size, vec[3], len1, len2;
1496
1497         /* size calculus, depending ortho/persp settings, like initgrabz() */
1498         size= rv3d->persmat[0][3]*co[0]+ rv3d->persmat[1][3]*co[1]+ rv3d->persmat[2][3]*co[2]+ rv3d->persmat[3][3];
1499
1500         VECCOPY(vec, rv3d->persinv[0]);
1501         len1= Normalize(vec);
1502         VECCOPY(vec, rv3d->persinv[1]);
1503         len2= Normalize(vec);
1504
1505         size*= 0.01f*(len1>len2?len1:len2);
1506
1507         /* correct for window size to make widgets appear fixed size */
1508         if(ar->winx > ar->winy) size*= 1000.0f/(float)ar->winx;
1509         else size*= 1000.0f/(float)ar->winy;
1510
1511         return size;
1512 }