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