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