c3ceea1a0c8311dc7a0676a70bae17576b79ef40
[blender-staging.git] / source / blender / editors / transform / transform_generics.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <string.h>
31 #include <math.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLO_sys_types.h" // for intptr_t support
36
37 #include "DNA_anim_types.h"
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_nla_types.h"
46 #include "DNA_node_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_object_force.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_userdef_types.h"
54 #include "DNA_view3d_types.h"
55 #include "DNA_windowmanager_types.h"
56
57 #include "RNA_access.h"
58
59 //#include "BIF_screen.h"
60 //#include "BIF_mywindow.h"
61 #include "BIF_gl.h"
62 //#include "BIF_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_particle.h"
96 #include "ED_screen_types.h"
97 #include "ED_space_api.h"
98 #include "ED_uvedit.h"
99 #include "ED_view3d.h"
100
101 //#include "BDR_unwrapper.h"
102
103 #include "BLI_arithb.h"
104 #include "BLI_blenlib.h"
105 #include "BLI_editVert.h"
106 #include "BLI_rand.h"
107
108 #include "RNA_access.h"
109
110 #include "WM_types.h"
111
112 #include "UI_resources.h"
113
114 //#include "blendef.h"
115 //
116 //#include "mydevice.h"
117
118 #include "transform.h"
119
120 extern ListBase editelems;
121
122 /* ************************** Functions *************************** */
123
124 void getViewVector(TransInfo *t, float coord[3], float vec[3])
125 {
126         if (t->persp != V3D_ORTHO)
127         {
128                 float p1[4], p2[4];
129                 
130                 VECCOPY(p1, coord);
131                 p1[3] = 1.0f;
132                 VECCOPY(p2, p1);
133                 p2[3] = 1.0f;
134                 Mat4MulVec4fl(t->viewmat, p2);
135                 
136                 p2[0] = 2.0f * p2[0];
137                 p2[1] = 2.0f * p2[1];
138                 p2[2] = 2.0f * p2[2];
139                 
140                 Mat4MulVec4fl(t->viewinv, p2);
141                 
142                 VecSubf(vec, p1, p2);
143         }
144         else {
145                 VECCOPY(vec, t->viewinv[2]);
146         }
147         Normalize(vec);
148 }
149
150 /* ************************** GENERICS **************************** */
151
152 static void clipMirrorModifier(TransInfo *t, Object *ob)
153 {
154         ModifierData *md= ob->modifiers.first;
155         float tolerance[3] = {0.0f, 0.0f, 0.0f};
156         int axis = 0;
157         
158         for (; md; md=md->next) {
159                 if (md->type==eModifierType_Mirror) {
160                         MirrorModifierData *mmd = (MirrorModifierData*) md;
161                         
162                         if(mmd->flag & MOD_MIR_CLIPPING) {
163                                 axis = 0;
164                                 if(mmd->flag & MOD_MIR_AXIS_X) {
165                                         axis |= 1;
166                                         tolerance[0] = mmd->tolerance;
167                                 }
168                                 if(mmd->flag & MOD_MIR_AXIS_Y) {
169                                         axis |= 2;
170                                         tolerance[1] = mmd->tolerance;
171                                 }
172                                 if(mmd->flag & MOD_MIR_AXIS_Z) {
173                                         axis |= 4;
174                                         tolerance[2] = mmd->tolerance;
175                                 }
176                                 if (axis) {
177                                         float mtx[4][4], imtx[4][4];
178                                         int i;
179                                         TransData *td = t->data;
180                                         
181                                         if (mmd->mirror_ob) {
182                                                 float obinv[4][4];
183                                                 
184                                                 Mat4Invert(obinv, mmd->mirror_ob->obmat);
185                                                 Mat4MulMat4(mtx, ob->obmat, obinv);
186                                                 Mat4Invert(imtx, mtx);
187                                         }
188                                         
189                                         for(i = 0 ; i < t->total; i++, td++) {
190                                                 int clip;
191                                                 float loc[3], iloc[3];
192                                                 
193                                                 if (td->flag & TD_NOACTION)
194                                                         break;
195                                                 if (td->loc==NULL)
196                                                         break;
197                                                 
198                                                 if (td->flag & TD_SKIP)
199                                                         continue;
200                                                 
201                                                 VecCopyf(loc,  td->loc);
202                                                 VecCopyf(iloc, td->iloc);
203                                                 
204                                                 if (mmd->mirror_ob) {
205                                                         VecMat4MulVecfl(loc, mtx, loc);
206                                                         VecMat4MulVecfl(iloc, mtx, iloc);
207                                                 }
208                                                 
209                                                 clip = 0;
210                                                 if(axis & 1) {
211                                                         if(fabs(iloc[0])<=tolerance[0] ||
212                                                            loc[0]*iloc[0]<0.0f) {
213                                                                 loc[0]= 0.0f;
214                                                                 clip = 1;
215                                                         }
216                                                 }
217                                                 
218                                                 if(axis & 2) {
219                                                         if(fabs(iloc[1])<=tolerance[1] ||
220                                                            loc[1]*iloc[1]<0.0f) {
221                                                                 loc[1]= 0.0f;
222                                                                 clip = 1;
223                                                         }
224                                                 }
225                                                 if(axis & 4) {
226                                                         if(fabs(iloc[2])<=tolerance[2] ||
227                                                            loc[2]*iloc[2]<0.0f) {
228                                                                 loc[2]= 0.0f;
229                                                                 clip = 1;
230                                                         }
231                                                 }
232                                                 if (clip) {
233                                                         if (mmd->mirror_ob) {
234                                                                 VecMat4MulVecfl(loc, imtx, loc);
235                                                         }
236                                                         VecCopyf(td->loc, loc);
237                                                 }
238                                         }
239                                 }
240                                 
241                         }
242                 }
243         }
244 }
245
246 /* assumes obedit set to mesh object */
247 static void editmesh_apply_to_mirror(TransInfo *t)
248 {
249         TransData *td = t->data;
250         EditVert *eve;
251         int i;
252         
253         for(i = 0 ; i < t->total; i++, td++) {
254                 if (td->flag & TD_NOACTION)
255                         break;
256                 if (td->loc==NULL)
257                         break;
258                 if (td->flag & TD_SKIP)
259                         continue;
260                 
261                 eve = td->extra;
262                 if(eve) {
263                         eve->co[0]= -td->loc[0];
264                         eve->co[1]= td->loc[1];
265                         eve->co[2]= td->loc[2];
266                 }
267         }
268 }
269
270 /* tags the given ID block for refreshes (if applicable) due to 
271  * Animation Editor editing
272  */
273 static void animedit_refresh_id_tags (Scene *scene, ID *id)
274 {
275         if (id) {
276                 AnimData *adt= BKE_animdata_from_id(id);
277                 
278                 /* tag AnimData for refresh so that other views will update in realtime with these changes */
279                 if (adt)
280                         adt->recalc |= ADT_RECALC_ANIM;
281                         
282                 /* if ID-block is Object, set recalc flags */
283                 switch (GS(id->name)) {
284                         case ID_OB:
285                         {
286                                 Object *ob= (Object *)id;
287                                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);  /* sets recalc flags */
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) && PE_get_current(scene, base->object)) {
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(t->scene, 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(t->scene, 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(t->scene, 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_id_flush_update(t->obedit->data, 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_id_flush_update(t->obedit->data, 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_id_flush_update(t->obedit->data, 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_id_flush_update(t->obedit->data, 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_id_flush_update(t->obedit->data, 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_id_flush_update(&ob->id, 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_orientation") && 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 only if property exist in operator. Otherwise, assume it's not supported */
983         if (op && RNA_struct_find_property(op->ptr, "proportional"))
984         {
985                 if (RNA_property_is_set(op->ptr, "proportional"))
986                 {
987                         switch(RNA_enum_get(op->ptr, "proportional"))
988                         {
989                         case 2: /* XXX connected constant */
990                                 t->flag |= T_PROP_CONNECTED;
991                         case 1: /* XXX prop on constant */
992                                 t->flag |= T_PROP_EDIT;
993                                 break;
994                         }
995                 }
996                 else
997                 {
998                         if ((t->options & CTX_NO_PET) == 0 && (ts->proportional)) {
999                                 t->flag |= T_PROP_EDIT;
1000                                 
1001                                 if(ts->proportional == 2)
1002                                         t->flag |= T_PROP_CONNECTED;    // yes i know, has to become define
1003                         }
1004                 }
1005                 
1006                 if (op && RNA_struct_find_property(op->ptr, "proportional_size") && RNA_property_is_set(op->ptr, "proportional_size"))
1007                 {
1008                         t->prop_size = RNA_float_get(op->ptr, "proportional_size");
1009                 }
1010                 else
1011                 {
1012                         t->prop_size = ts->proportional_size;
1013                 }
1014                 
1015                 
1016                 /* TRANSFORM_FIX_ME rna restrictions */
1017                 if (t->prop_size <= 0)
1018                 {
1019                         t->prop_size = 1.0f;
1020                 }
1021                 
1022                 if (op && RNA_struct_find_property(op->ptr, "proportional_editing_falloff") && RNA_property_is_set(op->ptr, "proportional_editing_falloff"))
1023                 {
1024                         t->prop_mode = RNA_enum_get(op->ptr, "proportional_editing_falloff");
1025                 }
1026                 else
1027                 {
1028                         t->prop_mode = ts->prop_mode;
1029                 }
1030         }
1031         else /* add not pet option to context when not available */
1032         {
1033                 t->options |= CTX_NO_PET;
1034         }
1035         
1036         
1037         setTransformViewMatrices(t);
1038         initNumInput(&t->num);
1039         initNDofInput(&t->ndof);
1040         
1041         return 1;
1042 }
1043
1044 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
1045 void postTrans (TransInfo *t)
1046 {
1047         TransData *td;
1048         
1049         if (t->draw_handle)
1050         {
1051                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle);
1052         }
1053         
1054         /* postTrans can be called when nothing is selected, so data is NULL already */
1055         if (t->data) {
1056                 int a;
1057                 
1058                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
1059                 for(a=0, td= t->data; a<t->total; a++, td++) {
1060                         if (td->flag & TD_BEZTRIPLE) 
1061                                 MEM_freeN(td->hdata);
1062                 }
1063                 MEM_freeN(t->data);
1064         }
1065         
1066         if (t->ext) MEM_freeN(t->ext);
1067         if (t->data2d) {
1068                 MEM_freeN(t->data2d);
1069                 t->data2d= NULL;
1070         }
1071         
1072         if(t->spacetype==SPACE_IMAGE) {
1073                 SpaceImage *sima= t->sa->spacedata.first;
1074                 if(sima->flag & SI_LIVE_UNWRAP)
1075                         ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1076         }
1077         
1078         if (t->mouse.data)
1079         {
1080                 MEM_freeN(t->mouse.data);
1081         }
1082         
1083         if (t->customFree) {
1084                 t->customFree(t);
1085         }
1086         else if (t->customData) {
1087                 MEM_freeN(t->customData);
1088         }
1089 }
1090
1091 void applyTransObjects(TransInfo *t)
1092 {
1093         TransData *td;
1094         
1095         for (td = t->data; td < t->data + t->total; td++) {
1096                 VECCOPY(td->iloc, td->loc);
1097                 if (td->ext->rot) {
1098                         VECCOPY(td->ext->irot, td->ext->rot);
1099                 }
1100                 if (td->ext->size) {
1101                         VECCOPY(td->ext->isize, td->ext->size);
1102                 }
1103         }
1104         recalcData(t);
1105 }
1106
1107 static void restoreElement(TransData *td) {
1108         /* TransData for crease has no loc */
1109         if (td->loc) {
1110                 VECCOPY(td->loc, td->iloc);
1111         }
1112         if (td->val) {
1113                 *td->val = td->ival;
1114         }
1115         if (td->ext && (td->flag&TD_NO_EXT)==0) {
1116                 if (td->ext->rot) {
1117                         VECCOPY(td->ext->rot, td->ext->irot);
1118                 }
1119                 if (td->ext->size) {
1120                         VECCOPY(td->ext->size, td->ext->isize);
1121                 }
1122                 if (td->ext->quat) {
1123                         QUATCOPY(td->ext->quat, td->ext->iquat);
1124                 }
1125         }
1126         
1127         if (td->flag & TD_BEZTRIPLE) {
1128                 *(td->hdata->h1) = td->hdata->ih1;
1129                 *(td->hdata->h2) = td->hdata->ih2;
1130         }
1131 }
1132
1133 void restoreTransObjects(TransInfo *t)
1134 {
1135         TransData *td;
1136         
1137         for (td = t->data; td < t->data + t->total; td++) {
1138                 restoreElement(td);
1139         }
1140         
1141         Mat3One(t->mat);
1142         
1143         recalcData(t);
1144 }
1145
1146 void calculateCenter2D(TransInfo *t)
1147 {
1148         if (t->flag & (T_EDIT|T_POSE)) {
1149                 Object *ob= t->obedit?t->obedit:t->poseobj;
1150                 float vec[3];
1151                 
1152                 VECCOPY(vec, t->center);
1153                 Mat4MulVecfl(ob->obmat, vec);
1154                 projectIntView(t, vec, t->center2d);
1155         }
1156         else {
1157                 projectIntView(t, t->center, t->center2d);
1158         }
1159 }
1160
1161 void calculateCenterCursor(TransInfo *t)
1162 {
1163         float *cursor;
1164         
1165         cursor = give_cursor(t->scene, t->view);
1166         VECCOPY(t->center, cursor);
1167         
1168         /* If edit or pose mode, move cursor in local space */
1169         if (t->flag & (T_EDIT|T_POSE)) {
1170                 Object *ob = t->obedit?t->obedit:t->poseobj;
1171                 float mat[3][3], imat[3][3];
1172                 
1173                 VecSubf(t->center, t->center, ob->obmat[3]);
1174                 Mat3CpyMat4(mat, ob->obmat);
1175                 Mat3Inv(imat, mat);
1176                 Mat3MulVecfl(imat, t->center);
1177         }
1178         
1179         calculateCenter2D(t);
1180 }
1181
1182 void calculateCenterCursor2D(TransInfo *t)
1183 {
1184         View2D *v2d= t->view;
1185         float aspx=1.0, aspy=1.0;
1186         
1187         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
1188                 ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
1189         
1190         if (v2d) {
1191                 t->center[0] = v2d->cursor[0] * aspx;
1192                 t->center[1] = v2d->cursor[1] * aspy;
1193         }
1194         
1195         calculateCenter2D(t);
1196 }
1197
1198 void calculateCenterMedian(TransInfo *t)
1199 {
1200         float partial[3] = {0.0f, 0.0f, 0.0f};
1201         int total = 0;
1202         int i;
1203         
1204         for(i = 0; i < t->total; i++) {
1205                 if (t->data[i].flag & TD_SELECTED) {
1206                         if (!(t->data[i].flag & TD_NOCENTER))
1207                         {
1208                                 VecAddf(partial, partial, t->data[i].center);
1209                                 total++;
1210                         }
1211                 }
1212                 else {
1213                         /*
1214                            All the selected elements are at the head of the array
1215                            which means we can stop when it finds unselected data
1216                         */
1217                         break;
1218                 }
1219         }
1220         if(i)
1221                 VecMulf(partial, 1.0f / total);
1222         VECCOPY(t->center, partial);
1223         
1224         calculateCenter2D(t);
1225 }
1226
1227 void calculateCenterBound(TransInfo *t)
1228 {
1229         float max[3];
1230         float min[3];
1231         int i;
1232         for(i = 0; i < t->total; i++) {
1233                 if (i) {
1234                         if (t->data[i].flag & TD_SELECTED) {
1235                                 if (!(t->data[i].flag & TD_NOCENTER))
1236                                         MinMax3(min, max, t->data[i].center);
1237                         }
1238                         else {
1239                                 /*
1240                                    All the selected elements are at the head of the array
1241                                    which means we can stop when it finds unselected data
1242                                 */
1243                                 break;
1244                         }
1245                 }
1246                 else {
1247                         VECCOPY(max, t->data[i].center);
1248                         VECCOPY(min, t->data[i].center);
1249                 }
1250         }
1251         VecAddf(t->center, min, max);
1252         VecMulf(t->center, 0.5);
1253         
1254         calculateCenter2D(t);
1255 }
1256
1257 void calculateCenter(TransInfo *t)
1258 {
1259         switch(t->around) {
1260         case V3D_CENTER:
1261                 calculateCenterBound(t);
1262                 break;
1263         case V3D_CENTROID:
1264                 calculateCenterMedian(t);
1265                 break;
1266         case V3D_CURSOR:
1267                 if(t->spacetype==SPACE_IMAGE)
1268                         calculateCenterCursor2D(t);
1269                 else
1270                         calculateCenterCursor(t);
1271                 break;
1272         case V3D_LOCAL:
1273                 /* Individual element center uses median center for helpline and such */
1274                 calculateCenterMedian(t);
1275                 break;
1276         case V3D_ACTIVE:
1277                 {
1278                 /* set median, and if if if... do object center */
1279 #if 0 // TRANSFORM_FIX_ME
1280                 EditSelection ese;
1281                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1282
1283                 if (t->obedit && t->obedit->type == OB_MESH && EM_get_actSelection(&ese)) {
1284                         EM_editselection_center(t->center, &ese);
1285                         calculateCenter2D(t);
1286                         break;
1287                 } /* END EDIT MODE ACTIVE ELEMENT */
1288 #endif
1289                 
1290                 calculateCenterMedian(t);
1291                 if((t->flag & (T_EDIT|T_POSE))==0)
1292                 {
1293                         Scene *scene = t->scene;
1294                         Object *ob= OBACT;
1295                         if(ob)
1296                         {
1297                                 VECCOPY(t->center, ob->obmat[3]);
1298                                 projectIntView(t, t->center, t->center2d);
1299                         }
1300                 }
1301                 
1302                 }
1303         }
1304         
1305         /* setting constraint center */
1306         VECCOPY(t->con.center, t->center);
1307         if(t->flag & (T_EDIT|T_POSE))
1308         {
1309                 Object *ob= t->obedit?t->obedit:t->poseobj;
1310                 Mat4MulVecfl(ob->obmat, t->con.center);
1311         }
1312         
1313         /* for panning from cameraview */
1314         if(t->flag & T_OBJECT)
1315         {
1316                 if(t->spacetype==SPACE_VIEW3D && t->ar->regiontype == RGN_TYPE_WINDOW)
1317                 {
1318                         View3D *v3d = t->view;
1319                         Scene *scene = t->scene;
1320                         RegionView3D *rv3d = t->ar->regiondata;
1321                         
1322                         if(v3d->camera == OBACT && rv3d->persp==V3D_CAMOB)
1323                         {
1324                                 float axis[3];
1325                                 /* persinv is nasty, use viewinv instead, always right */
1326                                 VECCOPY(axis, t->viewinv[2]);
1327                                 Normalize(axis);
1328                                 
1329                                 /* 6.0 = 6 grid units */
1330                                 axis[0]= t->center[0]- 6.0f*axis[0];
1331                                 axis[1]= t->center[1]- 6.0f*axis[1];
1332                                 axis[2]= t->center[2]- 6.0f*axis[2];
1333                                 
1334                                 projectIntView(t, axis, t->center2d);
1335                                 
1336                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1337                                 if(t->mode==TFM_TRANSLATION)
1338                                 {
1339                                         VECCOPY(t->center, axis);
1340                                         VECCOPY(t->con.center, t->center);
1341                                 }
1342                         }
1343                 }
1344         }
1345         
1346         if(t->spacetype==SPACE_VIEW3D)
1347         {
1348                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1349                 if(t->flag & (T_EDIT|T_POSE)) {
1350                         Object *ob= t->obedit?t->obedit:t->poseobj;
1351                         float vec[3];
1352                         
1353                         VECCOPY(vec, t->center);
1354                         Mat4MulVecfl(ob->obmat, vec);
1355                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1356                 }
1357                 else {
1358                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1359                 }
1360         }
1361 }
1362
1363 void calculatePropRatio(TransInfo *t)
1364 {
1365         TransData *td = t->data;
1366         int i;
1367         float dist;
1368         short connected = t->flag & T_PROP_CONNECTED;
1369
1370         if (t->flag & T_PROP_EDIT) {
1371                 for(i = 0 ; i < t->total; i++, td++) {
1372                         if (td->flag & TD_SELECTED) {
1373                                 td->factor = 1.0f;
1374                         }
1375                         else if ((connected &&
1376                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size))
1377                                 ||
1378                                         (connected == 0 &&
1379                                                 td->rdist > t->prop_size)) {
1380                                 /*
1381                                    The elements are sorted according to their dist member in the array,
1382                                    that means we can stop when it finds one element outside of the propsize.
1383                                 */
1384                                 td->flag |= TD_NOACTION;
1385                                 td->factor = 0.0f;
1386                                 restoreElement(td);
1387                         }
1388                         else {
1389                                 /* Use rdist for falloff calculations, it is the real distance */
1390                                 td->flag &= ~TD_NOACTION;
1391                                 dist= (t->prop_size-td->rdist)/t->prop_size;
1392                                 
1393                                 /*
1394                                  * Clamp to positive numbers.
1395                                  * Certain corner cases with connectivity and individual centers
1396                                  * can give values of rdist larger than propsize.
1397                                  */
1398                                 if (dist < 0.0f)
1399                                         dist = 0.0f;
1400                                 
1401                                 switch(t->prop_mode) {
1402                                 case PROP_SHARP:
1403                                         td->factor= dist*dist;
1404                                         break;
1405                                 case PROP_SMOOTH:
1406                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1407                                         break;
1408                                 case PROP_ROOT:
1409                                         td->factor = (float)sqrt(dist);
1410                                         break;
1411                                 case PROP_LIN:
1412                                         td->factor = dist;
1413                                         break;
1414                                 case PROP_CONST:
1415                                         td->factor = 1.0f;
1416                                         break;
1417                                 case PROP_SPHERE:
1418                                         td->factor = (float)sqrt(2*dist - dist * dist);
1419                                         break;
1420                                 case PROP_RANDOM:
1421                                         BLI_srand( BLI_rand() ); /* random seed */
1422                                         td->factor = BLI_frand()*dist;
1423                                         break;
1424                                 default:
1425                                         td->factor = 1;
1426                                 }
1427                         }
1428                 }
1429                 switch(t->prop_mode) {
1430                 case PROP_SHARP:
1431                         strcpy(t->proptext, "(Sharp)");
1432                         break;
1433                 case PROP_SMOOTH:
1434                         strcpy(t->proptext, "(Smooth)");
1435                         break;
1436                 case PROP_ROOT:
1437                         strcpy(t->proptext, "(Root)");
1438                         break;
1439                 case PROP_LIN:
1440                         strcpy(t->proptext, "(Linear)");
1441                         break;
1442                 case PROP_CONST:
1443                         strcpy(t->proptext, "(Constant)");
1444                         break;
1445                 case PROP_SPHERE:
1446                         strcpy(t->proptext, "(Sphere)");
1447                         break;
1448                 case PROP_RANDOM:
1449                         strcpy(t->proptext, "(Random)");
1450                         break;
1451                 default:
1452                         strcpy(t->proptext, "");
1453                 }
1454         }
1455         else {
1456                 for(i = 0 ; i < t->total; i++, td++) {
1457                         td->factor = 1.0;
1458                 }
1459                 strcpy(t->proptext, "");
1460         }
1461 }
1462
1463 float get_drawsize(ARegion *ar, float *co)
1464 {
1465         RegionView3D *rv3d= ar->regiondata;
1466         float size, vec[3], len1, len2;
1467         
1468         /* size calculus, depending ortho/persp settings, like initgrabz() */
1469         size= rv3d->persmat[0][3]*co[0]+ rv3d->persmat[1][3]*co[1]+ rv3d->persmat[2][3]*co[2]+ rv3d->persmat[3][3];
1470         
1471         VECCOPY(vec, rv3d->persinv[0]);
1472         len1= Normalize(vec);
1473         VECCOPY(vec, rv3d->persinv[1]);
1474         len2= Normalize(vec);
1475         
1476         size*= 0.01f*(len1>len2?len1:len2);
1477         
1478         /* correct for window size to make widgets appear fixed size */
1479         if(ar->winx > ar->winy) size*= 1000.0f/(float)ar->winx;
1480         else size*= 1000.0f/(float)ar->winy;
1481         
1482         return size;
1483 }