Assorted tweaks for animation editors:
[blender-staging.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_arithb.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
112 #include "UI_resources.h"
113
114 //#include "blendef.h"
115 //
116 //#include "mydevice.h"
117
118 #include "transform.h"
119
120 extern ListBase editelems;
121
122 /* ************************** Functions *************************** */
123
124 void getViewVector(TransInfo *t, float coord[3], float vec[3])
125 {
126         if (t->persp != V3D_ORTHO)
127         {
128                 float p1[4], p2[4];
129                 
130                 VECCOPY(p1, coord);
131                 p1[3] = 1.0f;
132                 VECCOPY(p2, p1);
133                 p2[3] = 1.0f;
134                 Mat4MulVec4fl(t->viewmat, p2);
135                 
136                 p2[0] = 2.0f * p2[0];
137                 p2[1] = 2.0f * p2[1];
138                 p2[2] = 2.0f * p2[2];
139                 
140                 Mat4MulVec4fl(t->viewinv, p2);
141                 
142                 VecSubf(vec, p1, p2);
143         }
144         else {
145                 VECCOPY(vec, t->viewinv[2]);
146         }
147         Normalize(vec);
148 }
149
150 /* ************************** GENERICS **************************** */
151
152 static void clipMirrorModifier(TransInfo *t, Object *ob)
153 {
154         ModifierData *md= ob->modifiers.first;
155         float tolerance[3] = {0.0f, 0.0f, 0.0f};
156         int axis = 0;
157         
158         for (; md; md=md->next) {
159                 if (md->type==eModifierType_Mirror) {
160                         MirrorModifierData *mmd = (MirrorModifierData*) md;
161                         
162                         if(mmd->flag & MOD_MIR_CLIPPING) {
163                                 axis = 0;
164                                 if(mmd->flag & MOD_MIR_AXIS_X) {
165                                         axis |= 1;
166                                         tolerance[0] = mmd->tolerance;
167                                 }
168                                 if(mmd->flag & MOD_MIR_AXIS_Y) {
169                                         axis |= 2;
170                                         tolerance[1] = mmd->tolerance;
171                                 }
172                                 if(mmd->flag & MOD_MIR_AXIS_Z) {
173                                         axis |= 4;
174                                         tolerance[2] = mmd->tolerance;
175                                 }
176                                 if (axis) {
177                                         float mtx[4][4], imtx[4][4];
178                                         int i;
179                                         TransData *td = t->data;
180                                         
181                                         if (mmd->mirror_ob) {
182                                                 float obinv[4][4];
183                                                 
184                                                 Mat4Invert(obinv, mmd->mirror_ob->obmat);
185                                                 Mat4MulMat4(mtx, ob->obmat, obinv);
186                                                 Mat4Invert(imtx, mtx);
187                                         }
188                                         
189                                         for(i = 0 ; i < t->total; i++, td++) {
190                                                 int clip;
191                                                 float loc[3], iloc[3];
192                                                 
193                                                 if (td->flag & TD_NOACTION)
194                                                         break;
195                                                 if (td->loc==NULL)
196                                                         break;
197                                                 
198                                                 if (td->flag & TD_SKIP)
199                                                         continue;
200                                                 
201                                                 VecCopyf(loc,  td->loc);
202                                                 VecCopyf(iloc, td->iloc);
203                                                 
204                                                 if (mmd->mirror_ob) {
205                                                         VecMat4MulVecfl(loc, mtx, loc);
206                                                         VecMat4MulVecfl(iloc, mtx, iloc);
207                                                 }
208                                                 
209                                                 clip = 0;
210                                                 if(axis & 1) {
211                                                         if(fabs(iloc[0])<=tolerance[0] ||
212                                                            loc[0]*iloc[0]<0.0f) {
213                                                                 loc[0]= 0.0f;
214                                                                 clip = 1;
215                                                         }
216                                                 }
217                                                 
218                                                 if(axis & 2) {
219                                                         if(fabs(iloc[1])<=tolerance[1] ||
220                                                            loc[1]*iloc[1]<0.0f) {
221                                                                 loc[1]= 0.0f;
222                                                                 clip = 1;
223                                                         }
224                                                 }
225                                                 if(axis & 4) {
226                                                         if(fabs(iloc[2])<=tolerance[2] ||
227                                                            loc[2]*iloc[2]<0.0f) {
228                                                                 loc[2]= 0.0f;
229                                                                 clip = 1;
230                                                         }
231                                                 }
232                                                 if (clip) {
233                                                         if (mmd->mirror_ob) {
234                                                                 VecMat4MulVecfl(loc, imtx, loc);
235                                                         }
236                                                         VecCopyf(td->loc, loc);
237                                                 }
238                                         }
239                                 }
240                                 
241                         }
242                 }
243         }
244 }
245
246 /* assumes obedit set to mesh object */
247 static void editmesh_apply_to_mirror(TransInfo *t)
248 {
249         TransData *td = t->data;
250         EditVert *eve;
251         int i;
252         
253         for(i = 0 ; i < t->total; i++, td++) {
254                 if (td->flag & TD_NOACTION)
255                         break;
256                 if (td->loc==NULL)
257                         break;
258                 if (td->flag & TD_SKIP)
259                         continue;
260                 
261                 eve = td->extra;
262                 if (eve) {
263                         eve->co[0]= -td->loc[0];
264                         eve->co[1]= td->loc[1];
265                         eve->co[2]= td->loc[2];
266                 }
267                 
268                 if (td->flag & TD_MIRROR_EDGE)
269                 {
270                         td->loc[0] = 0;
271                 }
272         }
273 }
274
275 /* tags the given ID block for refreshes (if applicable) due to 
276  * Animation Editor editing
277  */
278 static void animedit_refresh_id_tags (Scene *scene, ID *id)
279 {
280         if (id) {
281                 AnimData *adt= BKE_animdata_from_id(id);
282                 
283                 /* tag AnimData for refresh so that other views will update in realtime with these changes */
284                 if (adt)
285                         adt->recalc |= ADT_RECALC_ANIM;
286                         
287                 /* set recalc flags */
288                 DAG_id_flush_update(id, OB_RECALC); // XXX or do we want something more restrictive?
289         }
290 }
291
292 /* for the realtime animation recording feature, handle overlapping data */
293 static void animrecord_check_state (Scene *scene, ID *id, wmTimer *animtimer)
294 {
295         ScreenAnimData *sad= (animtimer) ? animtimer->customdata : NULL;
296         
297         /* sanity checks */
298         if ELEM3(NULL, scene, id, sad)
299                 return;
300         
301         /* check if we need a new strip if:
302          *      - if animtimer is running 
303          *      - we're not only keying for available channels
304          *      - the option to add new actions for each round is not enabled
305          */
306         if (IS_AUTOKEY_FLAG(INSERTAVAIL)==0 && (scene->toolsettings->autokey_flag & ANIMRECORD_FLAG_WITHNLA)) {
307                 /* if playback has just looped around, we need to add a new NLA track+strip to allow a clean pass to occur */
308                 if ((sad) && (sad->flag & ANIMPLAY_FLAG_JUMPED)) {
309                         AnimData *adt= BKE_animdata_from_id(id);
310                         
311                         /* perform push-down manually with some differences 
312                          * NOTE: BKE_nla_action_pushdown() sync warning...
313                          */
314                         if ((adt->action) && !(adt->flag & ADT_NLA_EDIT_ON)) {
315                                 float astart, aend;
316                                 
317                                 /* only push down if action is more than 1-2 frames long */
318                                 calc_action_range(adt->action, &astart, &aend, 1);
319                                 if (aend > astart+2.0f) {
320                                         NlaStrip *strip= add_nlastrip_to_stack(adt, adt->action);
321                                         
322                                         /* clear reference to action now that we've pushed it onto the stack */
323                                         adt->action->id.us--;
324                                         adt->action= NULL;
325                                         
326                                         /* adjust blending + extend so that they will behave correctly */
327                                         strip->extendmode= NLASTRIP_EXTEND_NOTHING;
328                                         strip->flag &= ~(NLASTRIP_FLAG_AUTO_BLENDS|NLASTRIP_FLAG_SELECT|NLASTRIP_FLAG_ACTIVE);
329                                         
330                                         /* also, adjust the AnimData's action extend mode to be on 
331                                          * 'nothing' so that previous result still play 
332                                          */
333                                         adt->act_extendmode= NLASTRIP_EXTEND_NOTHING;
334                                 }
335                         }
336                 }
337         }
338 }
339
340 /* called for updating while transform acts, once per redraw */
341 void recalcData(TransInfo *t)
342 {
343         Scene *scene = t->scene;
344         Base *base = scene->basact;
345
346         if (t->obedit) {
347         }
348         else if(base && (base->object->mode & OB_MODE_PARTICLE_EDIT) && PE_get_current(scene, base->object)) {
349                 flushTransParticles(t);
350         }
351         if (t->spacetype==SPACE_NODE) {
352                 flushTransNodes(t);
353         }
354         else if (t->spacetype==SPACE_SEQ) {
355                 flushTransSeq(t);
356         }
357         else if (t->spacetype == SPACE_ACTION) {
358                 Scene *scene= t->scene;
359                 
360                 bAnimContext ac;
361                 ListBase anim_data = {NULL, NULL};
362                 bAnimListElem *ale;
363                 int filter;
364                 
365                 /* initialise relevant anim-context 'context' data from TransInfo data */
366                         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
367                 memset(&ac, 0, sizeof(bAnimContext));
368                 
369                 ac.scene= t->scene;
370                 ac.obact= OBACT;
371                 ac.sa= t->sa;
372                 ac.ar= t->ar;
373                 ac.spacetype= (t->sa)? t->sa->spacetype : 0;
374                 ac.regiontype= (t->ar)? t->ar->regiontype : 0;
375                 
376                 ANIM_animdata_context_getdata(&ac);
377                 
378                 /* get animdata blocks visible in editor, assuming that these will be the ones where things changed */
379                 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_ANIMDATA);
380                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
381                 
382                 /* just tag these animdata-blocks to recalc, assuming that some data there changed */
383                 for (ale= anim_data.first; ale; ale= ale->next) {
384                         /* set refresh tags for objects using this animation */
385                         animedit_refresh_id_tags(t->scene, ale->id);
386                 }
387                 
388                 /* now free temp channels */
389                 BLI_freelistN(&anim_data);
390         }
391         else if (t->spacetype == SPACE_IPO) {
392                 Scene *scene;
393                 
394                 ListBase anim_data = {NULL, NULL};
395                 bAnimContext ac;
396                 int filter;
397                 
398                 bAnimListElem *ale;
399                 int dosort = 0;
400                 
401                 
402                 /* initialise relevant anim-context 'context' data from TransInfo data */
403                         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
404                 memset(&ac, 0, sizeof(bAnimContext));
405                 
406                 scene= ac.scene= t->scene;
407                 ac.obact= OBACT;
408                 ac.sa= t->sa;
409                 ac.ar= t->ar;
410                 ac.spacetype= (t->sa)? t->sa->spacetype : 0;
411                 ac.regiontype= (t->ar)? t->ar->regiontype : 0;
412                 
413                 ANIM_animdata_context_getdata(&ac);
414                 
415                 /* do the flush first */
416                 flushTransGraphData(t);
417                 
418                 /* get curves to check if a re-sort is needed */
419                 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY | ANIMFILTER_CURVEVISIBLE);
420                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
421                 
422                 /* now test if there is a need to re-sort */
423                 for (ale= anim_data.first; ale; ale= ale->next) {
424                         FCurve *fcu= (FCurve *)ale->key_data;
425                         
426                         /* watch it: if the time is wrong: do not correct handles yet */
427                         if (test_time_fcurve(fcu))
428                                 dosort++;
429                         else
430                                 calchandles_fcurve(fcu);
431                                 
432                         /* set refresh tags for objects using this animation */
433                         animedit_refresh_id_tags(t->scene, ale->id);
434                 }
435                 
436                 /* do resort and other updates? */
437                 if (dosort) remake_graph_transdata(t, &anim_data);
438                 
439                 /* now free temp channels */
440                 BLI_freelistN(&anim_data);
441         }
442         else if (t->spacetype == SPACE_NLA) {
443                 TransDataNla *tdn= (TransDataNla *)t->customData;
444                 SpaceNla *snla= (SpaceNla *)t->sa->spacedata.first;
445                 Scene *scene= t->scene;
446                 double secf= FPS;
447                 int i;
448                 
449                 /* for each strip we've got, perform some additional validation of the values that got set before
450                  * using RNA to set the value (which does some special operations when setting these values to make
451                  * sure that everything works ok)
452                  */
453                 for (i = 0; i < t->total; i++, tdn++) {
454                         NlaStrip *strip= tdn->strip;
455                         PointerRNA strip_ptr;
456                         short pExceeded, nExceeded, iter;
457                         int delta_y1, delta_y2;
458                         
459                         /* if this tdn has no handles, that means it is just a dummy that should be skipped */
460                         if (tdn->handle == 0)
461                                 continue;
462                         
463                         /* set refresh tags for objects using this animation */
464                         animedit_refresh_id_tags(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, "start_frame", tdn->h1[0]);
562                         RNA_float_set(&strip_ptr, "end_frame", 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                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* sets recalc flags */
642                                 
643                                 if (t->state == TRANS_CANCEL) {
644                                         while(nu) {
645                                                 calchandlesNurb(nu); /* Cant do testhandlesNurb here, it messes up the h1 and h2 flags */
646                                                 nu= nu->next;
647                                         }
648                                 } else {
649                                         /* Normal updating */
650                                         while(nu) {
651                                                 test2DNurb(nu);
652                                                 calchandlesNurb(nu);
653                                                 nu= nu->next;
654                                         }
655                                 }
656                         }
657                         else if(t->obedit->type==OB_LATTICE) {
658                                 Lattice *la= t->obedit->data;
659                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* sets recalc flags */
660         
661                                 if(la->editlatt->flag & LT_OUTSIDE) outside_lattice(la->editlatt);
662                         }
663                         else if (t->obedit->type == OB_MESH) {
664                                 EditMesh *em = ((Mesh*)t->obedit->data)->edit_mesh;
665                                 /* mirror modifier clipping? */
666                                 if(t->state != TRANS_CANCEL) {
667                                         clipMirrorModifier(t, t->obedit);
668                                 }
669                                 if((t->options & CTX_NO_MIRROR) == 0 && (t->flag & T_MIRROR))
670                                         editmesh_apply_to_mirror(t);
671                                         
672                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* sets recalc flags */
673                                 
674                                 recalc_editnormals(em);
675                         }
676                         else if(t->obedit->type==OB_ARMATURE) { /* no recalc flag, does pose */
677                                 bArmature *arm= t->obedit->data;
678                                 ListBase *edbo = arm->edbo;
679                                 EditBone *ebo;
680                                 TransData *td = t->data;
681                                 int i;
682                                 
683                                 /* Ensure all bones are correctly adjusted */
684                                 for (ebo = edbo->first; ebo; ebo = ebo->next){
685                                         
686                                         if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
687                                                 /* If this bone has a parent tip that has been moved */
688                                                 if (ebo->parent->flag & BONE_TIPSEL){
689                                                         VECCOPY (ebo->head, ebo->parent->tail);
690                                                         if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
691                                                 }
692                                                 /* If this bone has a parent tip that has NOT been moved */
693                                                 else{
694                                                         VECCOPY (ebo->parent->tail, ebo->head);
695                                                         if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
696                                                 }
697                                         }
698                                         
699                                         /* on extrude bones, oldlength==0.0f, so we scale radius of points */
700                                         ebo->length= VecLenf(ebo->head, ebo->tail);
701                                         if(ebo->oldlength==0.0f) {
702                                                 ebo->rad_head= 0.25f*ebo->length;
703                                                 ebo->rad_tail= 0.10f*ebo->length;
704                                                 ebo->dist= 0.25f*ebo->length;
705                                                 if(ebo->parent) {
706                                                         if(ebo->rad_head > ebo->parent->rad_tail)
707                                                                 ebo->rad_head= ebo->parent->rad_tail;
708                                                 }
709                                         }
710                                         else if(t->mode!=TFM_BONE_ENVELOPE) {
711                                                 /* if bones change length, lets do that for the deform distance as well */
712                                                 ebo->dist*= ebo->length/ebo->oldlength;
713                                                 ebo->rad_head*= ebo->length/ebo->oldlength;
714                                                 ebo->rad_tail*= ebo->length/ebo->oldlength;
715                                                 ebo->oldlength= ebo->length;
716                                         }
717                                 }
718                                 
719                                 
720                                 if (t->mode != TFM_BONE_ROLL)
721                                 {
722                                         /* fix roll */
723                                         for(i = 0; i < t->total; i++, td++)
724                                         {
725                                                 if (td->extra)
726                                                 {
727                                                         float vec[3], up_axis[3];
728                                                         float qrot[4];
729                                                         
730                                                         ebo = td->extra;
731                                                         VECCOPY(up_axis, td->axismtx[2]);
732                                                         
733                                                         if (t->mode != TFM_ROTATION)
734                                                         {
735                                                                 VecSubf(vec, ebo->tail, ebo->head);
736                                                                 Normalize(vec);
737                                                                 RotationBetweenVectorsToQuat(qrot, td->axismtx[1], vec);
738                                                                 QuatMulVecf(qrot, up_axis);
739                                                         }
740                                                         else
741                                                         {
742                                                                 Mat3MulVecfl(t->mat, up_axis);
743                                                         }
744                                                         
745                                                         ebo->roll = ED_rollBoneToVector(ebo, up_axis);
746                                                 }
747                                         }
748                                 }
749                                 
750                                 if(arm->flag & ARM_MIRROR_EDIT)
751                                         transform_armature_mirror_update(t->obedit);
752                                 
753                         }
754                         else
755                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* sets recalc flags */
756                 }
757                 else if( (t->flag & T_POSE) && t->poseobj) {
758                         Object *ob= t->poseobj;
759                         bArmature *arm= ob->data;
760                         
761                         /* if animtimer is running, and the object already has animation data,
762                          * check if the auto-record feature means that we should record 'samples'
763                          * (i.e. uneditable animation values)
764                          */
765                         // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
766                         if ((t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
767                                 int targetless_ik= (t->flag & T_AUTOIK); // XXX this currently doesn't work, since flags aren't set yet!
768                                 
769                                 animrecord_check_state(t->scene, &ob->id, t->animtimer);
770                                 autokeyframe_pose_cb_func(t->scene, (View3D *)t->view, ob, t->mode, targetless_ik);
771                         }
772                         
773                         /* old optimize trick... this enforces to bypass the depgraph */
774                         if (!(arm->flag & ARM_DELAYDEFORM)) {
775                                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);  /* sets recalc flags */
776                         }
777                         else
778                                 where_is_pose(scene, ob);
779                 }
780                 else {
781                         for(base= FIRSTBASE; base; base= base->next) {
782                                 Object *ob= base->object;
783                                 
784                                 /* this flag is from depgraph, was stored in initialize phase, handled in drawview.c */
785                                 if(base->flag & BA_HAS_RECALC_OB)
786                                         ob->recalc |= OB_RECALC_OB;
787                                 if(base->flag & BA_HAS_RECALC_DATA)
788                                         ob->recalc |= OB_RECALC_DATA;
789                                 
790                                 /* if object/base is selected */
791                                 if ((base->flag & SELECT) || (ob->flag & SELECT)) {
792                                         /* if animtimer is running, and the object already has animation data,
793                                          * check if the auto-record feature means that we should record 'samples'
794                                          * (i.e. uneditable animation values)
795                                          */
796                                         // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
797                                         if ((t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
798                                                 animrecord_check_state(t->scene, &ob->id, t->animtimer);
799                                                 autokeyframe_ob_cb_func(t->scene, (View3D *)t->view, ob, t->mode);
800                                         }
801                                 }
802                                 
803                                 /* proxy exception */
804                                 if(ob->proxy)
805                                         ob->proxy->recalc |= ob->recalc;
806                                 if(ob->proxy_group)
807                                         group_tag_recalc(ob->proxy_group->dup_group);
808                         }
809                 }
810                 
811                 if(((View3D*)t->view)->drawtype == OB_SHADED)
812                         reshadeall_displist(t->scene);
813         }
814 }
815
816 void drawLine(TransInfo *t, float *center, float *dir, char axis, short options)
817 {
818         float v1[3], v2[3], v3[3];
819         char col[3], col2[3];
820
821         if (t->spacetype == SPACE_VIEW3D)
822         {
823                 View3D *v3d = t->view;
824                 
825                 glPushMatrix();
826                 
827                 //if(t->obedit) glLoadMatrixf(t->obedit->obmat);        // sets opengl viewing
828                 
829                 
830                 VecCopyf(v3, dir);
831                 VecMulf(v3, v3d->far);
832                 
833                 VecSubf(v2, center, v3);
834                 VecAddf(v1, center, v3);
835                 
836                 if (options & DRAWLIGHT) {
837                         col[0] = col[1] = col[2] = 220;
838                 }
839                 else {
840                         UI_GetThemeColor3ubv(TH_GRID, col);
841                 }
842                 UI_make_axis_color(col, col2, axis);
843                 glColor3ubv((GLubyte *)col2);
844                 
845                 setlinestyle(0);
846                 glBegin(GL_LINE_STRIP);
847                         glVertex3fv(v1);
848                         glVertex3fv(v2);
849                 glEnd();
850                 
851                 glPopMatrix();
852         }
853 }
854
855 void resetTransRestrictions(TransInfo *t)
856 {
857         t->flag &= ~T_ALL_RESTRICTIONS;
858 }
859
860 int initTransInfo (bContext *C, TransInfo *t, wmOperator *op, wmEvent *event)
861 {
862         Scene *sce = CTX_data_scene(C);
863         ToolSettings *ts = CTX_data_tool_settings(C);
864         ARegion *ar = CTX_wm_region(C);
865         ScrArea *sa = CTX_wm_area(C);
866         Object *obedit = CTX_data_edit_object(C);
867         
868         /* moving: is shown in drawobject() (transform color) */
869 //  TRANSFORM_FIX_ME
870 //      if(obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
871 //      else if(G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
872 //      else G.moving= G_TRANSFORM_OBJ;
873         
874         t->scene = sce;
875         t->sa = sa;
876         t->ar = ar;
877         t->obedit = obedit;
878         t->settings = ts;
879         
880         t->data = NULL;
881         t->ext = NULL;
882         
883         t->helpline = HLP_NONE;
884         
885         t->flag = 0;
886         
887         t->redraw = 1; /* redraw first time */
888         
889         if (event)
890         {
891                 t->imval[0] = event->x - t->ar->winrct.xmin;
892                 t->imval[1] = event->y - t->ar->winrct.ymin;
893                 
894                 t->event_type = event->type;
895         }
896         else
897         {
898                 t->imval[0] = 0;
899                 t->imval[1] = 0;
900         }
901         
902         t->con.imval[0] = t->imval[0];
903         t->con.imval[1] = t->imval[1];
904         
905         t->mval[0] = t->imval[0];
906         t->mval[1] = t->imval[1];
907         
908         t->transform            = NULL;
909         t->handleEvent          = NULL;
910         
911         t->total                        = 0;
912         
913         t->val = 0.0f;
914         
915         t->vec[0]                       =
916                 t->vec[1]               =
917                 t->vec[2]               = 0.0f;
918         
919         t->center[0]            =
920                 t->center[1]    =
921                 t->center[2]    = 0.0f;
922         
923         Mat3One(t->mat);
924         
925         t->spacetype = sa->spacetype;
926         if(t->spacetype == SPACE_VIEW3D)
927         {
928                 View3D *v3d = sa->spacedata.first;
929                 
930                 t->view = v3d;
931                 t->animtimer= CTX_wm_screen(C)->animtimer;
932                 
933                 if(v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
934                 t->around = v3d->around;
935                 
936                 if (op && RNA_struct_find_property(op->ptr, "constraint_orientation") && RNA_property_is_set(op->ptr, "constraint_orientation"))
937                 {
938                         t->current_orientation = RNA_enum_get(op->ptr, "constraint_orientation");
939                         
940                         if (t->current_orientation >= V3D_MANIP_CUSTOM + BIF_countTransformOrientation(C) - 1)
941                         {
942                                 t->current_orientation = V3D_MANIP_GLOBAL;
943                         }
944                 }
945                 else
946                 {
947                         t->current_orientation = v3d->twmode;
948                 }
949         }
950         else if(t->spacetype==SPACE_IMAGE || t->spacetype==SPACE_NODE)
951         {
952                 SpaceImage *sima = sa->spacedata.first;
953                 // XXX for now, get View2D  from the active region
954                 t->view = &ar->v2d;
955                 t->around = sima->around;
956         }
957         else
958         {
959                 // XXX for now, get View2D  from the active region
960                 t->view = &ar->v2d;
961                 
962                 t->around = V3D_CENTER;
963         }
964         
965         if (op && RNA_struct_find_property(op->ptr, "mirror") && RNA_property_is_set(op->ptr, "mirror"))
966         {
967                 if (RNA_boolean_get(op->ptr, "mirror"))
968                 {
969                         t->flag |= T_MIRROR;
970                         t->mirror = 1;
971                 }
972         }
973         // Need stuff to take it from edit mesh or whatnot here
974         else
975         {
976                 if (t->obedit && t->obedit->type == OB_MESH && (((Mesh *)t->obedit->data)->editflag & ME_EDIT_MIRROR_X))
977                 {
978                         t->flag |= T_MIRROR;
979                         t->mirror = 1;
980                 }
981         }
982         
983         /* setting PET flag only if property exist in operator. Otherwise, assume it's not supported */
984         if (op && RNA_struct_find_property(op->ptr, "proportional"))
985         {
986                 if (RNA_property_is_set(op->ptr, "proportional"))
987                 {
988                         switch(RNA_enum_get(op->ptr, "proportional"))
989                         {
990                         case 2: /* XXX connected constant */
991                                 t->flag |= T_PROP_CONNECTED;
992                         case 1: /* XXX prop on constant */
993                                 t->flag |= T_PROP_EDIT;
994                                 break;
995                         }
996                 }
997                 else
998                 {
999                         if ((t->options & CTX_NO_PET) == 0 && (ts->proportional != PROP_EDIT_OFF)) {
1000                                 t->flag |= T_PROP_EDIT;
1001                                 
1002                                 if(ts->proportional == PROP_EDIT_CONNECTED)
1003                                         t->flag |= T_PROP_CONNECTED;    // yes i know, has to become define
1004                         }
1005                 }
1006                 
1007                 if (op && RNA_struct_find_property(op->ptr, "proportional_size") && RNA_property_is_set(op->ptr, "proportional_size"))
1008                 {
1009                         t->prop_size = RNA_float_get(op->ptr, "proportional_size");
1010                 }
1011                 else
1012                 {
1013                         t->prop_size = ts->proportional_size;
1014                 }
1015                 
1016                 
1017                 /* TRANSFORM_FIX_ME rna restrictions */
1018                 if (t->prop_size <= 0)
1019                 {
1020                         t->prop_size = 1.0f;
1021                 }
1022                 
1023                 if (op && RNA_struct_find_property(op->ptr, "proportional_editing_falloff") && RNA_property_is_set(op->ptr, "proportional_editing_falloff"))
1024                 {
1025                         t->prop_mode = RNA_enum_get(op->ptr, "proportional_editing_falloff");
1026                 }
1027                 else
1028                 {
1029                         t->prop_mode = ts->prop_mode;
1030                 }
1031         }
1032         else /* add not pet option to context when not available */
1033         {
1034                 t->options |= CTX_NO_PET;
1035         }
1036         
1037         
1038         setTransformViewMatrices(t);
1039         initNumInput(&t->num);
1040         initNDofInput(&t->ndof);
1041         
1042         return 1;
1043 }
1044
1045 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
1046 void postTrans (TransInfo *t)
1047 {
1048         TransData *td;
1049         
1050         if (t->draw_handle)
1051         {
1052                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle);
1053         }
1054         
1055         /* postTrans can be called when nothing is selected, so data is NULL already */
1056         if (t->data) {
1057                 int a;
1058                 
1059                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
1060                 for(a=0, td= t->data; a<t->total; a++, td++) {
1061                         if (td->flag & TD_BEZTRIPLE) 
1062                                 MEM_freeN(td->hdata);
1063                 }
1064                 MEM_freeN(t->data);
1065         }
1066         
1067         if (t->ext) MEM_freeN(t->ext);
1068         if (t->data2d) {
1069                 MEM_freeN(t->data2d);
1070                 t->data2d= NULL;
1071         }
1072         
1073         if(t->spacetype==SPACE_IMAGE) {
1074                 SpaceImage *sima= t->sa->spacedata.first;
1075                 if(sima->flag & SI_LIVE_UNWRAP)
1076                         ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1077         }
1078         
1079         if (t->mouse.data)
1080         {
1081                 MEM_freeN(t->mouse.data);
1082         }
1083         
1084         if (t->customFree) {
1085                 t->customFree(t);
1086         }
1087         else if (t->customData) {
1088                 MEM_freeN(t->customData);
1089         }
1090 }
1091
1092 void applyTransObjects(TransInfo *t)
1093 {
1094         TransData *td;
1095         
1096         for (td = t->data; td < t->data + t->total; td++) {
1097                 VECCOPY(td->iloc, td->loc);
1098                 if (td->ext->rot) {
1099                         VECCOPY(td->ext->irot, td->ext->rot);
1100                 }
1101                 if (td->ext->size) {
1102                         VECCOPY(td->ext->isize, td->ext->size);
1103                 }
1104         }
1105         recalcData(t);
1106 }
1107
1108 static void restoreElement(TransData *td) {
1109         /* TransData for crease has no loc */
1110         if (td->loc) {
1111                 VECCOPY(td->loc, td->iloc);
1112         }
1113         if (td->val) {
1114                 *td->val = td->ival;
1115         }
1116         if (td->ext && (td->flag&TD_NO_EXT)==0) {
1117                 if (td->ext->rot) {
1118                         VECCOPY(td->ext->rot, td->ext->irot);
1119                 }
1120                 if (td->ext->size) {
1121                         VECCOPY(td->ext->size, td->ext->isize);
1122                 }
1123                 if (td->ext->quat) {
1124                         QUATCOPY(td->ext->quat, td->ext->iquat);
1125                 }
1126         }
1127         
1128         if (td->flag & TD_BEZTRIPLE) {
1129                 *(td->hdata->h1) = td->hdata->ih1;
1130                 *(td->hdata->h2) = td->hdata->ih2;
1131         }
1132 }
1133
1134 void restoreTransObjects(TransInfo *t)
1135 {
1136         TransData *td;
1137         
1138         for (td = t->data; td < t->data + t->total; td++) {
1139                 restoreElement(td);
1140         }
1141         
1142         Mat3One(t->mat);
1143         
1144         recalcData(t);
1145 }
1146
1147 void calculateCenter2D(TransInfo *t)
1148 {
1149         if (t->flag & (T_EDIT|T_POSE)) {
1150                 Object *ob= t->obedit?t->obedit:t->poseobj;
1151                 float vec[3];
1152                 
1153                 VECCOPY(vec, t->center);
1154                 Mat4MulVecfl(ob->obmat, vec);
1155                 projectIntView(t, vec, t->center2d);
1156         }
1157         else {
1158                 projectIntView(t, t->center, t->center2d);
1159         }
1160 }
1161
1162 void calculateCenterCursor(TransInfo *t)
1163 {
1164         float *cursor;
1165         
1166         cursor = give_cursor(t->scene, t->view);
1167         VECCOPY(t->center, cursor);
1168         
1169         /* If edit or pose mode, move cursor in local space */
1170         if (t->flag & (T_EDIT|T_POSE)) {
1171                 Object *ob = t->obedit?t->obedit:t->poseobj;
1172                 float mat[3][3], imat[3][3];
1173                 
1174                 VecSubf(t->center, t->center, ob->obmat[3]);
1175                 Mat3CpyMat4(mat, ob->obmat);
1176                 Mat3Inv(imat, mat);
1177                 Mat3MulVecfl(imat, t->center);
1178         }
1179         
1180         calculateCenter2D(t);
1181 }
1182
1183 void calculateCenterCursor2D(TransInfo *t)
1184 {
1185         View2D *v2d= t->view;
1186         float aspx=1.0, aspy=1.0;
1187         
1188         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
1189                 ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
1190         
1191         if (v2d) {
1192                 t->center[0] = v2d->cursor[0] * aspx;
1193                 t->center[1] = v2d->cursor[1] * aspy;
1194         }
1195         
1196         calculateCenter2D(t);
1197 }
1198
1199 void calculateCenterMedian(TransInfo *t)
1200 {
1201         float partial[3] = {0.0f, 0.0f, 0.0f};
1202         int total = 0;
1203         int i;
1204         
1205         for(i = 0; i < t->total; i++) {
1206                 if (t->data[i].flag & TD_SELECTED) {
1207                         if (!(t->data[i].flag & TD_NOCENTER))
1208                         {
1209                                 VecAddf(partial, partial, t->data[i].center);
1210                                 total++;
1211                         }
1212                 }
1213                 else {
1214                         /*
1215                            All the selected elements are at the head of the array
1216                            which means we can stop when it finds unselected data
1217                         */
1218                         break;
1219                 }
1220         }
1221         if(i)
1222                 VecMulf(partial, 1.0f / total);
1223         VECCOPY(t->center, partial);
1224         
1225         calculateCenter2D(t);
1226 }
1227
1228 void calculateCenterBound(TransInfo *t)
1229 {
1230         float max[3];
1231         float min[3];
1232         int i;
1233         for(i = 0; i < t->total; i++) {
1234                 if (i) {
1235                         if (t->data[i].flag & TD_SELECTED) {
1236                                 if (!(t->data[i].flag & TD_NOCENTER))
1237                                         MinMax3(min, max, t->data[i].center);
1238                         }
1239                         else {
1240                                 /*
1241                                    All the selected elements are at the head of the array
1242                                    which means we can stop when it finds unselected data
1243                                 */
1244                                 break;
1245                         }
1246                 }
1247                 else {
1248                         VECCOPY(max, t->data[i].center);
1249                         VECCOPY(min, t->data[i].center);
1250                 }
1251         }
1252         VecAddf(t->center, min, max);
1253         VecMulf(t->center, 0.5);
1254         
1255         calculateCenter2D(t);
1256 }
1257
1258 void calculateCenter(TransInfo *t)
1259 {
1260         switch(t->around) {
1261         case V3D_CENTER:
1262                 calculateCenterBound(t);
1263                 break;
1264         case V3D_CENTROID:
1265                 calculateCenterMedian(t);
1266                 break;
1267         case V3D_CURSOR:
1268                 if(t->spacetype==SPACE_IMAGE)
1269                         calculateCenterCursor2D(t);
1270                 else
1271                         calculateCenterCursor(t);
1272                 break;
1273         case V3D_LOCAL:
1274                 /* Individual element center uses median center for helpline and such */
1275                 calculateCenterMedian(t);
1276                 break;
1277         case V3D_ACTIVE:
1278                 {
1279                 /* set median, and if if if... do object center */
1280 #if 0 // TRANSFORM_FIX_ME
1281                 EditSelection ese;
1282                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1283
1284                 if (t->obedit && t->obedit->type == OB_MESH && EM_get_actSelection(&ese)) {
1285                         EM_editselection_center(t->center, &ese);
1286                         calculateCenter2D(t);
1287                         break;
1288                 } /* END EDIT MODE ACTIVE ELEMENT */
1289 #endif
1290                 
1291                 calculateCenterMedian(t);
1292                 if((t->flag & (T_EDIT|T_POSE))==0)
1293                 {
1294                         Scene *scene = t->scene;
1295                         Object *ob= OBACT;
1296                         if(ob)
1297                         {
1298                                 VECCOPY(t->center, ob->obmat[3]);
1299                                 projectIntView(t, t->center, t->center2d);
1300                         }
1301                 }
1302                 
1303                 }
1304         }
1305         
1306         /* setting constraint center */
1307         VECCOPY(t->con.center, t->center);
1308         if(t->flag & (T_EDIT|T_POSE))
1309         {
1310                 Object *ob= t->obedit?t->obedit:t->poseobj;
1311                 Mat4MulVecfl(ob->obmat, t->con.center);
1312         }
1313         
1314         /* for panning from cameraview */
1315         if(t->flag & T_OBJECT)
1316         {
1317                 if(t->spacetype==SPACE_VIEW3D && t->ar->regiontype == RGN_TYPE_WINDOW)
1318                 {
1319                         View3D *v3d = t->view;
1320                         Scene *scene = t->scene;
1321                         RegionView3D *rv3d = t->ar->regiondata;
1322                         
1323                         if(v3d->camera == OBACT && rv3d->persp==V3D_CAMOB)
1324                         {
1325                                 float axis[3];
1326                                 /* persinv is nasty, use viewinv instead, always right */
1327                                 VECCOPY(axis, t->viewinv[2]);
1328                                 Normalize(axis);
1329                                 
1330                                 /* 6.0 = 6 grid units */
1331                                 axis[0]= t->center[0]- 6.0f*axis[0];
1332                                 axis[1]= t->center[1]- 6.0f*axis[1];
1333                                 axis[2]= t->center[2]- 6.0f*axis[2];
1334                                 
1335                                 projectIntView(t, axis, t->center2d);
1336                                 
1337                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1338                                 if(t->mode==TFM_TRANSLATION)
1339                                 {
1340                                         VECCOPY(t->center, axis);
1341                                         VECCOPY(t->con.center, t->center);
1342                                 }
1343                         }
1344                 }
1345         }
1346         
1347         if(t->spacetype==SPACE_VIEW3D)
1348         {
1349                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1350                 if(t->flag & (T_EDIT|T_POSE)) {
1351                         Object *ob= t->obedit?t->obedit:t->poseobj;
1352                         float vec[3];
1353                         
1354                         VECCOPY(vec, t->center);
1355                         Mat4MulVecfl(ob->obmat, vec);
1356                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1357                 }
1358                 else {
1359                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1360                 }
1361         }
1362 }
1363
1364 void calculatePropRatio(TransInfo *t)
1365 {
1366         TransData *td = t->data;
1367         int i;
1368         float dist;
1369         short connected = t->flag & T_PROP_CONNECTED;
1370
1371         if (t->flag & T_PROP_EDIT) {
1372                 for(i = 0 ; i < t->total; i++, td++) {
1373                         if (td->flag & TD_SELECTED) {
1374                                 td->factor = 1.0f;
1375                         }
1376                         else if (t->flag & T_MIRROR && td->loc[0] * t->mirror < -0.00001f)
1377                         {
1378                                 td->flag |= TD_SKIP;
1379                                 td->factor = 0.0f;
1380                                 restoreElement(td);
1381                         }
1382                         else if ((connected &&
1383                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size))
1384                                 ||
1385                                         (connected == 0 &&
1386                                                 td->rdist > t->prop_size)) {
1387                                 /*
1388                                    The elements are sorted according to their dist member in the array,
1389                                    that means we can stop when it finds one element outside of the propsize.
1390                                 */
1391                                 td->flag |= TD_NOACTION;
1392                                 td->factor = 0.0f;
1393                                 restoreElement(td);
1394                         }
1395                         else {
1396                                 /* Use rdist for falloff calculations, it is the real distance */
1397                                 td->flag &= ~TD_NOACTION;
1398                                 dist= (t->prop_size-td->rdist)/t->prop_size;
1399                                 
1400                                 /*
1401                                  * Clamp to positive numbers.
1402                                  * Certain corner cases with connectivity and individual centers
1403                                  * can give values of rdist larger than propsize.
1404                                  */
1405                                 if (dist < 0.0f)
1406                                         dist = 0.0f;
1407                                 
1408                                 switch(t->prop_mode) {
1409                                 case PROP_SHARP:
1410                                         td->factor= dist*dist;
1411                                         break;
1412                                 case PROP_SMOOTH:
1413                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1414                                         break;
1415                                 case PROP_ROOT:
1416                                         td->factor = (float)sqrt(dist);
1417                                         break;
1418                                 case PROP_LIN:
1419                                         td->factor = dist;
1420                                         break;
1421                                 case PROP_CONST:
1422                                         td->factor = 1.0f;
1423                                         break;
1424                                 case PROP_SPHERE:
1425                                         td->factor = (float)sqrt(2*dist - dist * dist);
1426                                         break;
1427                                 case PROP_RANDOM:
1428                                         BLI_srand( BLI_rand() ); /* random seed */
1429                                         td->factor = BLI_frand()*dist;
1430                                         break;
1431                                 default:
1432                                         td->factor = 1;
1433                                 }
1434                         }
1435                 }
1436                 switch(t->prop_mode) {
1437                 case PROP_SHARP:
1438                         strcpy(t->proptext, "(Sharp)");
1439                         break;
1440                 case PROP_SMOOTH:
1441                         strcpy(t->proptext, "(Smooth)");
1442                         break;
1443                 case PROP_ROOT:
1444                         strcpy(t->proptext, "(Root)");
1445                         break;
1446                 case PROP_LIN:
1447                         strcpy(t->proptext, "(Linear)");
1448                         break;
1449                 case PROP_CONST:
1450                         strcpy(t->proptext, "(Constant)");
1451                         break;
1452                 case PROP_SPHERE:
1453                         strcpy(t->proptext, "(Sphere)");
1454                         break;
1455                 case PROP_RANDOM:
1456                         strcpy(t->proptext, "(Random)");
1457                         break;
1458                 default:
1459                         strcpy(t->proptext, "");
1460                 }
1461         }
1462         else {
1463                 for(i = 0 ; i < t->total; i++, td++) {
1464                         td->factor = 1.0;
1465                 }
1466                 strcpy(t->proptext, "");
1467         }
1468 }
1469
1470 float get_drawsize(ARegion *ar, float *co)
1471 {
1472         RegionView3D *rv3d= ar->regiondata;
1473         float size, vec[3], len1, len2;
1474         
1475         /* size calculus, depending ortho/persp settings, like initgrabz() */
1476         size= rv3d->persmat[0][3]*co[0]+ rv3d->persmat[1][3]*co[1]+ rv3d->persmat[2][3]*co[2]+ rv3d->persmat[3][3];
1477         
1478         VECCOPY(vec, rv3d->persinv[0]);
1479         len1= Normalize(vec);
1480         VECCOPY(vec, rv3d->persinv[1]);
1481         len2= Normalize(vec);
1482         
1483         size*= 0.01f*(len1>len2?len1:len2);
1484         
1485         /* correct for window size to make widgets appear fixed size */
1486         if(ar->winx > ar->winy) size*= 1000.0f/(float)ar->winx;
1487         else size*= 1000.0f/(float)ar->winy;
1488         
1489         return size;
1490 }