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