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