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