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