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