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