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