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