276b09563a7327de95d5ea2793cfee6b79297d7a
[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
56 //#include "BIF_screen.h"
57 //#include "BIF_mywindow.h"
58 #include "BIF_gl.h"
59 //#include "BIF_editaction.h"
60 //#include "BIF_editmesh.h"
61 //#include "BIF_editnla.h"
62 //#include "BIF_editsima.h"
63 //#include "BIF_editparticle.h"
64 //#include "BIF_meshtools.h"
65 #include "BIF_retopo.h"
66
67 #include "BKE_action.h"
68 #include "BKE_anim.h"
69 #include "BKE_armature.h"
70 #include "BKE_cloth.h"
71 #include "BKE_curve.h"
72 #include "BKE_depsgraph.h"
73 #include "BKE_displist.h"
74 #include "BKE_depsgraph.h"
75 #include "BKE_fcurve.h"
76 #include "BKE_global.h"
77 #include "BKE_group.h"
78 #include "BKE_lattice.h"
79 #include "BKE_key.h"
80 #include "BKE_mesh.h"
81 #include "BKE_modifier.h"
82 #include "BKE_object.h"
83 #include "BKE_utildefines.h"
84 #include "BKE_context.h"
85
86 #include "ED_anim_api.h"
87 #include "ED_armature.h"
88 #include "ED_image.h"
89 #include "ED_keyframing.h"
90 #include "ED_mesh.h"
91 #include "ED_space_api.h"
92 #include "ED_uvedit.h"
93 #include "ED_view3d.h"
94
95 //#include "BDR_unwrapper.h"
96
97 #include "BLI_arithb.h"
98 #include "BLI_blenlib.h"
99 #include "BLI_editVert.h"
100 #include "BLI_rand.h"
101
102 #include "WM_types.h"
103
104 #include "UI_resources.h"
105
106 //#include "blendef.h"
107 //
108 //#include "mydevice.h"
109
110 #include "transform.h"
111
112 extern ListBase editelems;
113
114 extern TransInfo Trans; /* From transform.c */
115
116 /* ************************** Functions *************************** */
117
118 void getViewVector(TransInfo *t, float coord[3], float vec[3])
119 {
120         if (t->persp != V3D_ORTHO)
121         {
122                 float p1[4], p2[4];
123
124                 VECCOPY(p1, coord);
125                 p1[3] = 1.0f;
126                 VECCOPY(p2, p1);
127                 p2[3] = 1.0f;
128                 Mat4MulVec4fl(t->viewmat, p2);
129
130                 p2[0] = 2.0f * p2[0];
131                 p2[1] = 2.0f * p2[1];
132                 p2[2] = 2.0f * p2[2];
133
134                 Mat4MulVec4fl(t->viewinv, p2);
135
136                 VecSubf(vec, p1, p2);
137         }
138         else {
139                 VECCOPY(vec, t->viewinv[2]);
140         }
141         Normalize(vec);
142 }
143
144 /* ************************** GENERICS **************************** */
145
146 static void clipMirrorModifier(TransInfo *t, Object *ob)
147 {
148         ModifierData *md= ob->modifiers.first;
149         float tolerance[3] = {0.0f, 0.0f, 0.0f};
150         int axis = 0;
151
152         for (; md; md=md->next) {
153                 if (md->type==eModifierType_Mirror) {
154                         MirrorModifierData *mmd = (MirrorModifierData*) md;     
155                 
156                         if(mmd->flag & MOD_MIR_CLIPPING) {
157                                 axis = 0;
158                                 if(mmd->flag & MOD_MIR_AXIS_X) {
159                                         axis |= 1;
160                                         tolerance[0] = mmd->tolerance;
161                                 }
162                                 if(mmd->flag & MOD_MIR_AXIS_Y) {
163                                         axis |= 2;
164                                         tolerance[1] = mmd->tolerance;
165                                 }
166                                 if(mmd->flag & MOD_MIR_AXIS_Z) {
167                                         axis |= 4;
168                                         tolerance[2] = mmd->tolerance;
169                                 }
170                                 if (axis) {
171                                         float mtx[4][4], imtx[4][4];
172                                         int i;
173                                         TransData *td = t->data;
174                 
175                                         if (mmd->mirror_ob) {
176                                                 float obinv[4][4];
177
178                                                 Mat4Invert(obinv, mmd->mirror_ob->obmat);
179                                                 Mat4MulMat4(mtx, ob->obmat, obinv);
180                                                 Mat4Invert(imtx, mtx);
181                                         }
182
183                                         for(i = 0 ; i < t->total; i++, td++) {
184                                                 int clip;
185                                                 float loc[3], iloc[3];
186
187                                                 if (td->flag & TD_NOACTION)
188                                                         break;
189                                                 if (td->loc==NULL)
190                                                         break;
191                                                         
192                                                 if (td->flag & TD_SKIP)
193                                                         continue;
194                         
195                                                 VecCopyf(loc,  td->loc);
196                                                 VecCopyf(iloc, td->iloc);
197
198                                                 if (mmd->mirror_ob) {
199                                                         VecMat4MulVecfl(loc, mtx, loc);
200                                                         VecMat4MulVecfl(iloc, mtx, iloc);
201                                                 }
202
203                                                 clip = 0;
204                                                 if(axis & 1) {
205                                                         if(fabs(iloc[0])<=tolerance[0] || 
206                                                            loc[0]*iloc[0]<0.0f) {
207                                                                 loc[0]= 0.0f;
208                                                                 clip = 1;
209                                                         }
210                                                 }
211                         
212                                                 if(axis & 2) {
213                                                         if(fabs(iloc[1])<=tolerance[1] || 
214                                                            loc[1]*iloc[1]<0.0f) {
215                                                                 loc[1]= 0.0f;
216                                                                 clip = 1;
217                                                         }
218                                                 }
219                                                 if(axis & 4) {
220                                                         if(fabs(iloc[2])<=tolerance[2] || 
221                                                            loc[2]*iloc[2]<0.0f) {
222                                                                 loc[2]= 0.0f;
223                                                                 clip = 1;
224                                                         }
225                                                 }
226                                                 if (clip) {
227                                                         if (mmd->mirror_ob) {
228                                                                 VecMat4MulVecfl(loc, imtx, loc);
229                                                         }
230                                                         VecCopyf(td->loc, loc);
231                                                 }
232                                         }
233                                 }
234
235                         }
236                 }
237         }
238 }
239
240 /* assumes obedit set to mesh object */
241 static void editmesh_apply_to_mirror(TransInfo *t)
242 {
243         TransData *td = t->data;
244         EditVert *eve;
245         int i;
246         
247         for(i = 0 ; i < t->total; i++, td++) {
248                 if (td->flag & TD_NOACTION)
249                         break;
250                 if (td->loc==NULL)
251                         break;
252                 if (td->flag & TD_SKIP)
253                         continue;
254                 
255                 eve = td->extra;
256                 if(eve) {
257                         eve->co[0]= -td->loc[0];
258                         eve->co[1]= td->loc[1];
259                         eve->co[2]= td->loc[2];
260                 }               
261         }               
262 }
263
264 /* called for updating while transform acts, once per redraw */
265 void recalcData(TransInfo *t)
266 {
267         Scene *scene = t->scene;
268         Base *base;
269         
270 #if 0 // TRANSFORM_FIX_ME
271         if (t->spacetype == SPACE_ACTION) {
272                 Object *ob= OBACT;
273                 void *data;
274                 short context;
275                 
276                 /* determine what type of data we are operating on */
277                 data = get_action_context(&context);
278                 if (data == NULL) return;
279                 
280                 /* always flush data if gpencil context */
281                 if (context == ACTCONT_GPENCIL) {
282                         flushTransGPactionData(t);
283                 }
284                 
285                 if (G.saction->lock) {
286                         if (context == ACTCONT_ACTION) {
287                                 if(ob) {
288                                         ob->ctime= -1234567.0f;
289                                         if(ob->pose || ob_get_key(ob))
290                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC);
291                                         else
292                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
293                                 }
294                         }
295                         else if (context == ACTCONT_SHAPEKEY) {
296                                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_OB|OB_RECALC_DATA);
297                         }
298                 }
299         }       
300         else if (t->spacetype == SPACE_NLA) {
301                 if (G.snla->lock) {
302                         for (base=G.scene->base.first; base; base=base->next) {
303                                 if (base->flag & BA_HAS_RECALC_OB)
304                                         base->object->recalc |= OB_RECALC_OB;
305                                 if (base->flag & BA_HAS_RECALC_DATA)
306                                         base->object->recalc |= OB_RECALC_DATA;
307                                 
308                                 if (base->object->recalc) 
309                                         base->object->ctime= -1234567.0f;       // eveil! 
310                                 
311                                 /* recalculate scale of selected nla-strips */
312                                 if (base->object->nlastrips.first) {
313                                         Object *bob= base->object;
314                                         bActionStrip *strip;
315                                         
316                                         for (strip= bob->nlastrips.first; strip; strip= strip->next) {
317                                                 if (strip->flag & ACTSTRIP_SELECT) {
318                                                         float actlen= strip->actend - strip->actstart;
319                                                         float len= strip->end - strip->start;
320                                                         
321                                                         strip->scale= len / (actlen * strip->repeat);
322                                                 }
323                                         }
324                                 }
325                         }
326                         
327                         DAG_scene_flush_update(G.scene, screen_view3d_layers(), 0);
328                 }
329                 else {
330                         for (base=G.scene->base.first; base; base=base->next) {
331                                 /* recalculate scale of selected nla-strips */
332                                 if (base->object && base->object->nlastrips.first) {
333                                         Object *bob= base->object;
334                                         bActionStrip *strip;
335                                         
336                                         for (strip= bob->nlastrips.first; strip; strip= strip->next) {
337                                                 if (strip->flag & ACTSTRIP_SELECT) {
338                                                         float actlen= strip->actend - strip->actstart;
339                                                         float len= strip->end - strip->start;
340                                                         
341                                                         /* prevent 'negative' scaling */
342                                                         if (len < 0) {
343                                                                 SWAP(float, strip->start, strip->end);
344                                                                 len= fabs(len);
345                                                         }
346                                                         
347                                                         /* calculate new scale */
348                                                         strip->scale= len / (actlen * strip->repeat);
349                                                 }
350                                         }
351                                 }
352                         }
353                 }
354         }
355 #endif
356         if (t->obedit) {
357         }
358         else if(G.f & G_PARTICLEEDIT) {
359                 flushTransParticles(t);
360         }
361         if (t->spacetype==SPACE_NODE) {
362                 flushTransNodes(t);
363         }
364         else if (t->spacetype==SPACE_SEQ) {
365                 flushTransSeq(t);
366         }
367         else if (t->spacetype == SPACE_IPO) {
368                 SpaceIpo *sipo= (SpaceIpo *)t->sa->spacedata.first;
369                 Scene *scene;
370                 
371                 ListBase anim_data = {NULL, NULL};
372                 bAnimContext ac;
373                 int filter;
374                 
375                 bAnimListElem *ale;
376                 int dosort = 0;
377                 
378                 
379                 /* initialise relevant anim-context 'context' data from TransInfo data */
380                         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
381                 memset(&ac, 0, sizeof(bAnimContext));
382                 
383                 scene= ac.scene= t->scene;
384                 ac.obact= OBACT;
385                 ac.sa= t->sa;
386                 ac.ar= t->ar;
387                 ac.spacetype= (t->sa)? t->sa->spacetype : 0;
388                 ac.regiontype= (t->ar)? t->ar->regiontype : 0;
389                 
390                 ANIM_animdata_context_getdata(&ac);
391                 
392                 /* do the flush first */
393                 flushTransGraphData(t);
394                 
395                 /* get curves to check if a re-sort is needed */
396                 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY | ANIMFILTER_CURVEVISIBLE);
397                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
398                 
399                 /* now test if there is a need to re-sort */
400                 for (ale= anim_data.first; ale; ale= ale->next) {
401                         FCurve *fcu= (FCurve *)ale->key_data;
402                         
403                         /* watch it: if the time is wrong: do not correct handles yet */
404                         if (test_time_fcurve(fcu))
405                                 dosort++;
406                         else
407                                 calchandles_fcurve(fcu);
408                 }
409                 
410                 /* do resort and other updates? */
411                 if (dosort) remake_graph_transdata(t, &anim_data);
412                 //if (sipo->showkey) update_ipokey_val();
413                 
414                 /* now free temp channels */
415                 BLI_freelistN(&anim_data);
416                 
417                 /* update realtime - not working? */
418                 if (sipo->lock) {
419                 
420                 }
421         }
422         else if (t->obedit) {
423                 if ELEM(t->obedit->type, OB_CURVE, OB_SURF) {
424                         Curve *cu= t->obedit->data;
425                         Nurb *nu= cu->editnurb->first;
426                         
427                         DAG_object_flush_update(scene, t->obedit, OB_RECALC_DATA);  /* sets recalc flags */
428                         
429                         if (t->state == TRANS_CANCEL) {
430                                 while(nu) {
431                                         calchandlesNurb(nu); /* Cant do testhandlesNurb here, it messes up the h1 and h2 flags */
432                                         nu= nu->next;
433                                 }
434                         } else {
435                                 /* Normal updating */
436                                 while(nu) {
437                                         test2DNurb(nu);
438                                         calchandlesNurb(nu);
439                                         nu= nu->next;
440                                 }
441                                 /* TRANSFORM_FIX_ME */
442                                 // retopo_do_all();
443                         }
444                 }
445                 else if(t->obedit->type==OB_LATTICE) {
446                         Lattice *la= t->obedit->data;
447                         DAG_object_flush_update(scene, t->obedit, OB_RECALC_DATA);  /* sets recalc flags */
448                         
449                         if(la->editlatt->flag & LT_OUTSIDE) outside_lattice(la->editlatt);
450                 }
451                 else if (t->obedit->type == OB_MESH) {
452                         if(t->spacetype==SPACE_IMAGE) {
453                                 SpaceImage *sima= t->sa->spacedata.first;
454
455                                 flushTransUVs(t);
456                                 if(sima->flag & SI_LIVE_UNWRAP)
457                                         ED_uvedit_live_unwrap_re_solve();
458         
459                                 DAG_object_flush_update(scene, t->obedit, OB_RECALC_DATA);
460                         } else {
461                                 EditMesh *em = ((Mesh*)t->obedit->data)->edit_mesh;
462                                 /* mirror modifier clipping? */
463                                 if(t->state != TRANS_CANCEL) {
464                                         /* TRANSFORM_FIX_ME */
465 //                                      if ((G.qual & LR_CTRLKEY)==0) {
466 //                                              /* Only retopo if not snapping, Note, this is the only case of G.qual being used, but we have no T_SHIFT_MOD - Campbell */
467 //                                              retopo_do_all();
468 //                                      }
469                                         clipMirrorModifier(t, t->obedit);
470                                 }
471                                 if((t->options & CTX_NO_MIRROR) == 0 && (scene->toolsettings->editbutflag & B_MESH_X_MIRROR))
472                                         editmesh_apply_to_mirror(t);
473                                 
474                                 DAG_object_flush_update(scene, t->obedit, OB_RECALC_DATA);  /* sets recalc flags */
475                                 
476                                 recalc_editnormals(em);
477                         }
478                 }
479                 else if(t->obedit->type==OB_ARMATURE) { /* no recalc flag, does pose */
480                         bArmature *arm= t->obedit->data;
481                         ListBase *edbo = arm->edbo;
482                         EditBone *ebo;
483                         TransData *td = t->data;
484                         int i;
485                         
486                         /* Ensure all bones are correctly adjusted */
487                         for (ebo = edbo->first; ebo; ebo = ebo->next){
488                                 
489                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
490                                         /* If this bone has a parent tip that has been moved */
491                                         if (ebo->parent->flag & BONE_TIPSEL){
492                                                 VECCOPY (ebo->head, ebo->parent->tail);
493                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
494                                         }
495                                         /* If this bone has a parent tip that has NOT been moved */
496                                         else{
497                                                 VECCOPY (ebo->parent->tail, ebo->head);
498                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
499                                         }
500                                 }
501                                 
502                                 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
503                                 ebo->length= VecLenf(ebo->head, ebo->tail);
504                                 if(ebo->oldlength==0.0f) {
505                                         ebo->rad_head= 0.25f*ebo->length;
506                                         ebo->rad_tail= 0.10f*ebo->length;
507                                         ebo->dist= 0.25f*ebo->length;
508                                         if(ebo->parent) {
509                                                 if(ebo->rad_head > ebo->parent->rad_tail)
510                                                         ebo->rad_head= ebo->parent->rad_tail;
511                                         }
512                                 }
513                                 else if(t->mode!=TFM_BONE_ENVELOPE) {
514                                         /* if bones change length, lets do that for the deform distance as well */
515                                         ebo->dist*= ebo->length/ebo->oldlength;
516                                         ebo->rad_head*= ebo->length/ebo->oldlength;
517                                         ebo->rad_tail*= ebo->length/ebo->oldlength;
518                                         ebo->oldlength= ebo->length;
519                                 }
520                         }
521                         
522                         
523                         if (t->mode != TFM_BONE_ROLL)
524                         {
525                                 /* fix roll */
526                                 for(i = 0; i < t->total; i++, td++)
527                                 {
528                                         if (td->extra)
529                                         {
530                                                 float vec[3], up_axis[3];
531                                                 float qrot[4];
532                                                 
533                                                 ebo = td->extra;
534                                                 VECCOPY(up_axis, td->axismtx[2]);
535                                                 
536                                                 if (t->mode != TFM_ROTATION)
537                                                 {
538                                                         VecSubf(vec, ebo->tail, ebo->head);
539                                                         Normalize(vec);
540                                                         RotationBetweenVectorsToQuat(qrot, td->axismtx[1], vec);
541                                                         QuatMulVecf(qrot, up_axis);
542                                                 }
543                                                 else
544                                                 {
545                                                         Mat3MulVecfl(t->mat, up_axis);
546                                                 }
547                                                 
548                                                 ebo->roll = ED_rollBoneToVector(ebo, up_axis);
549                                         }
550                                 }
551                         }
552                         
553                         if(arm->flag & ARM_MIRROR_EDIT) 
554                                 transform_armature_mirror_update(t->obedit);
555                         
556                 }
557                 else
558                         DAG_object_flush_update(scene, t->obedit, OB_RECALC_DATA);  /* sets recalc flags */
559         }
560         else if( (t->flag & T_POSE) && t->poseobj) {
561                 Object *ob= t->poseobj;
562                 bArmature *arm= ob->data;
563                 
564                 /* if animtimer is running, and the object already has animation data,
565                  * check if the auto-record feature means that we should record 'samples'
566                  * (i.e. uneditable animation values)
567                  */
568                 // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
569                 // TODO: maybe the ob->adt check isn't really needed? makes it too difficult to use...
570                 if (/*(ob->adt) && */(t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
571                         short targetless_ik= (t->flag & T_AUTOIK); // XXX this currently doesn't work, since flags aren't set yet!
572                         autokeyframe_pose_cb_func(t->scene, (View3D *)t->view, ob, t->mode, targetless_ik);
573                 }
574                 
575                 /* old optimize trick... this enforces to bypass the depgraph */
576                 if (!(arm->flag & ARM_DELAYDEFORM)) {
577                         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
578                 }
579                 else
580                         where_is_pose(scene, ob);
581         }
582         else {
583                 for(base= FIRSTBASE; base; base= base->next) {
584                         Object *ob= base->object;
585                         
586                         /* this flag is from depgraph, was stored in initialize phase, handled in drawview.c */
587                         if(base->flag & BA_HAS_RECALC_OB)
588                                 ob->recalc |= OB_RECALC_OB;
589                         if(base->flag & BA_HAS_RECALC_DATA)
590                                 ob->recalc |= OB_RECALC_DATA;
591                         
592                         /* if object/base is selected */
593                         if ((base->flag & SELECT) || (ob->flag & SELECT)) {
594                                 /* if animtimer is running, and the object already has animation data,
595                                  * check if the auto-record feature means that we should record 'samples'
596                                  * (i.e. uneditable animation values)
597                                  */
598                                 // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
599                                 // TODO: maybe the ob->adt check isn't really needed? makes it too difficult to use...
600                                 if (/*(ob->adt) && */(t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
601                                         autokeyframe_ob_cb_func(t->scene, (View3D *)t->view, ob, t->mode);
602                                 }
603                         }
604                         
605                         /* proxy exception */
606                         if(ob->proxy)
607                                 ob->proxy->recalc |= ob->recalc;
608                         if(ob->proxy_group)
609                                 group_tag_recalc(ob->proxy_group->dup_group);
610                 } 
611         }
612
613         /* update shaded drawmode while transform */
614         if(t->spacetype==SPACE_VIEW3D && ((View3D*)t->view)->drawtype == OB_SHADED)
615                 reshadeall_displist(t->scene);
616 }
617
618 void drawLine(TransInfo *t, float *center, float *dir, char axis, short options)
619 {
620         extern void make_axis_color(char *col, char *col2, char axis);  // view3d_draw.c
621         float v1[3], v2[3], v3[3];
622         char col[3], col2[3];
623         
624         if (t->spacetype == SPACE_VIEW3D)
625         {
626                 View3D *v3d = t->view;
627                 
628                 glPushMatrix();
629                 
630                 //if(t->obedit) glLoadMatrixf(t->obedit->obmat);        // sets opengl viewing
631                 
632         
633                 VecCopyf(v3, dir);
634                 VecMulf(v3, v3d->far);
635                 
636                 VecSubf(v2, center, v3);
637                 VecAddf(v1, center, v3);
638         
639                 if (options & DRAWLIGHT) {
640                         col[0] = col[1] = col[2] = 220;
641                 }
642                 else {
643                         UI_GetThemeColor3ubv(TH_GRID, col);
644                 }
645                 make_axis_color(col, col2, axis);
646                 glColor3ubv((GLubyte *)col2);
647         
648                 setlinestyle(0);
649                 glBegin(GL_LINE_STRIP); 
650                         glVertex3fv(v1); 
651                         glVertex3fv(v2); 
652                 glEnd();
653                 
654                 glPopMatrix();
655         }
656 }
657
658 void resetTransRestrictions(TransInfo *t)
659 {
660         t->flag &= ~T_ALL_RESTRICTIONS;
661 }
662
663 void initTransInfo (bContext *C, TransInfo *t, wmEvent *event)
664 {
665         Scene *sce = CTX_data_scene(C);
666         ARegion *ar = CTX_wm_region(C);
667         ScrArea *sa = CTX_wm_area(C);
668         Object *obedit = CTX_data_edit_object(C);
669         
670         /* moving: is shown in drawobject() (transform color) */
671 //  TRANSFORM_FIX_ME    
672 //      if(obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
673 //      else if(G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
674 //      else G.moving= G_TRANSFORM_OBJ;
675         
676         t->scene = sce;
677         t->sa = sa;
678         t->ar = ar;
679         t->obedit = obedit;
680
681         t->data = NULL;
682         t->ext = NULL;
683
684         t->flag = 0;
685         
686         t->redraw = 1; /* redraw first time */
687         
688         t->propsize = 1.0f; /* TRANSFORM_FIX_ME this needs to be saved in scene or something */
689
690         /* setting PET flag */
691         if ((t->options & CTX_NO_PET) == 0 && (sce->proportional)) {
692                 t->flag |= T_PROP_EDIT;
693                 
694                 if(sce->proportional == 2)
695                         t->flag |= T_PROP_CONNECTED;    // yes i know, has to become define
696         }
697
698         if (event)
699         {
700                 t->imval[0] = event->x - t->ar->winrct.xmin;
701                 t->imval[1] = event->y - t->ar->winrct.ymin;
702                 
703                 t->event_type = event->type;
704         }
705         else
706         {
707                 t->imval[0] = 0;
708                 t->imval[1] = 0;
709         }
710         
711         t->con.imval[0] = t->imval[0];
712         t->con.imval[1] = t->imval[1];
713
714         t->mval[0] = t->imval[0];
715         t->mval[1] = t->imval[1];
716
717         t->transform            = NULL;
718         t->handleEvent          = NULL;
719
720         t->total                        = 0;
721
722         t->val = 0.0f;
723
724         t->vec[0]                       =
725                 t->vec[1]               =
726                 t->vec[2]               = 0.0f;
727         
728         t->center[0]            =
729                 t->center[1]    =
730                 t->center[2]    = 0.0f;
731         
732         Mat3One(t->mat);
733         
734         t->spacetype = sa->spacetype;
735         if(t->spacetype == SPACE_VIEW3D)
736         {
737                 View3D *v3d = sa->spacedata.first;
738                 
739                 t->view = v3d;
740                 t->animtimer= CTX_wm_screen(C)->animtimer;
741                 
742                 if(v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
743                 t->around = v3d->around;
744         }
745         else if(t->spacetype==SPACE_IMAGE || t->spacetype==SPACE_NODE)
746         {
747                 // XXX for now, get View2D  from the active region
748                 t->view = &ar->v2d;
749                 t->around = ar->v2d.around;
750         }
751         else
752         {
753                 // XXX for now, get View2D  from the active region
754                 t->view = &ar->v2d;
755                 
756                 t->around = V3D_CENTER;
757         }
758
759         setTransformViewMatrices(t);
760         initNumInput(&t->num);
761         initNDofInput(&t->ndof);
762 }
763
764 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
765 void postTrans (TransInfo *t) 
766 {
767         TransData *td;
768
769         if (t->draw_handle)
770         {
771                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle);
772         }
773         
774         /* postTrans can be called when nothing is selected, so data is NULL already */
775         if (t->data) {
776                 int a;
777
778                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
779                 for(a=0, td= t->data; a<t->total; a++, td++) {
780                         if(td->tdi) MEM_freeN(td->tdi);
781                         if (td->flag & TD_BEZTRIPLE) MEM_freeN(td->hdata); 
782                 }
783                 MEM_freeN(t->data);
784         }
785
786         if (t->ext) MEM_freeN(t->ext);
787         if (t->data2d) {
788                 MEM_freeN(t->data2d);
789                 t->data2d= NULL;
790         }
791
792         if(t->spacetype==SPACE_IMAGE) {
793                 SpaceImage *sima= t->sa->spacedata.first;
794                 if(sima->flag & SI_LIVE_UNWRAP)
795                         ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
796         }
797         else if(t->spacetype==SPACE_ACTION) {
798                 if (t->customData)
799                         MEM_freeN(t->customData);
800         }
801 }
802
803 void applyTransObjects(TransInfo *t)
804 {
805         TransData *td;
806         
807         for (td = t->data; td < t->data + t->total; td++) {
808                 VECCOPY(td->iloc, td->loc);
809                 if (td->ext->rot) {
810                         VECCOPY(td->ext->irot, td->ext->rot);
811                 }
812                 if (td->ext->size) {
813                         VECCOPY(td->ext->isize, td->ext->size);
814                 }
815         }       
816         recalcData(t);
817
818
819 /* helper for below */
820 static void restore_ipokey(float *poin, float *old)
821 {
822         if(poin) {
823                 poin[0]= old[0];
824                 poin[-3]= old[3];
825                 poin[3]= old[6];
826         }
827 }
828
829 static void restoreElement(TransData *td) {
830         /* TransData for crease has no loc */
831         if (td->loc) {
832                 VECCOPY(td->loc, td->iloc);
833         }
834         if (td->val) {
835                 *td->val = td->ival;
836         }
837         if (td->ext && (td->flag&TD_NO_EXT)==0) {
838                 if (td->ext->rot) {
839                         VECCOPY(td->ext->rot, td->ext->irot);
840                 }
841                 if (td->ext->size) {
842                         VECCOPY(td->ext->size, td->ext->isize);
843                 }
844                 if(td->flag & TD_USEQUAT) {
845                         if (td->ext->quat) {
846                                 QUATCOPY(td->ext->quat, td->ext->iquat);
847                         }
848                 }
849         }
850         
851         if (td->flag & TD_BEZTRIPLE) {
852                 *(td->hdata->h1) = td->hdata->ih1;
853                 *(td->hdata->h2) = td->hdata->ih2;
854         }
855         
856         if(td->tdi) {
857                 TransDataIpokey *tdi= td->tdi;
858                 
859                 restore_ipokey(tdi->locx, tdi->oldloc);
860                 restore_ipokey(tdi->locy, tdi->oldloc+1);
861                 restore_ipokey(tdi->locz, tdi->oldloc+2);
862
863                 restore_ipokey(tdi->rotx, tdi->oldrot);
864                 restore_ipokey(tdi->roty, tdi->oldrot+1);
865                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
866                 
867                 restore_ipokey(tdi->sizex, tdi->oldsize);
868                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
869                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
870         }
871 }
872
873 void restoreTransObjects(TransInfo *t)
874 {
875         TransData *td;
876         
877         for (td = t->data; td < t->data + t->total; td++) {
878                 restoreElement(td);
879         }
880         
881         Mat3One(t->mat);
882         
883         recalcData(t);
884 }
885
886 void calculateCenter2D(TransInfo *t)
887 {
888         if (t->flag & (T_EDIT|T_POSE)) {
889                 Object *ob= t->obedit?t->obedit:t->poseobj;
890                 float vec[3];
891                 
892                 VECCOPY(vec, t->center);
893                 Mat4MulVecfl(ob->obmat, vec);
894                 projectIntView(t, vec, t->center2d);
895         }
896         else {
897                 projectIntView(t, t->center, t->center2d);
898         }
899 }
900
901 void calculateCenterCursor(TransInfo *t)
902 {
903         float *cursor;
904
905         cursor = give_cursor(t->scene, t->view);
906         VECCOPY(t->center, cursor);
907
908         /* If edit or pose mode, move cursor in local space */
909         if (t->flag & (T_EDIT|T_POSE)) {
910                 Object *ob = t->obedit?t->obedit:t->poseobj;
911                 float mat[3][3], imat[3][3];
912                 
913                 VecSubf(t->center, t->center, ob->obmat[3]);
914                 Mat3CpyMat4(mat, ob->obmat);
915                 Mat3Inv(imat, mat);
916                 Mat3MulVecfl(imat, t->center);
917         }
918         
919         calculateCenter2D(t);
920 }
921
922 void calculateCenterCursor2D(TransInfo *t)
923 {
924         View2D *v2d= t->view;
925         float aspx=1.0, aspy=1.0;
926         
927         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
928                 ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
929
930         if (v2d) {
931                 t->center[0] = v2d->cursor[0] * aspx; 
932                 t->center[1] = v2d->cursor[1] * aspy; 
933         }
934
935         calculateCenter2D(t);
936 }
937
938 void calculateCenterMedian(TransInfo *t)
939 {
940         float partial[3] = {0.0f, 0.0f, 0.0f};
941         int total = 0;
942         int i;
943         
944         for(i = 0; i < t->total; i++) {
945                 if (t->data[i].flag & TD_SELECTED) {
946                         if (!(t->data[i].flag & TD_NOCENTER))
947                         {
948                                 VecAddf(partial, partial, t->data[i].center);
949                                 total++;
950                         }
951                 }
952                 else {
953                         /* 
954                            All the selected elements are at the head of the array 
955                            which means we can stop when it finds unselected data
956                         */
957                         break;
958                 }
959         }
960         if(i)
961                 VecMulf(partial, 1.0f / total);
962         VECCOPY(t->center, partial);
963
964         calculateCenter2D(t);
965 }
966
967 void calculateCenterBound(TransInfo *t)
968 {
969         float max[3];
970         float min[3];
971         int i;
972         for(i = 0; i < t->total; i++) {
973                 if (i) {
974                         if (t->data[i].flag & TD_SELECTED) {
975                                 if (!(t->data[i].flag & TD_NOCENTER))
976                                         MinMax3(min, max, t->data[i].center);
977                         }
978                         else {
979                                 /* 
980                                    All the selected elements are at the head of the array 
981                                    which means we can stop when it finds unselected data
982                                 */
983                                 break;
984                         }
985                 }
986                 else {
987                         VECCOPY(max, t->data[i].center);
988                         VECCOPY(min, t->data[i].center);
989                 }
990         }
991         VecAddf(t->center, min, max);
992         VecMulf(t->center, 0.5);
993
994         calculateCenter2D(t);
995 }
996
997 void calculateCenter(TransInfo *t) 
998 {
999         switch(t->around) {
1000         case V3D_CENTER:
1001                 calculateCenterBound(t);
1002                 break;
1003         case V3D_CENTROID:
1004                 calculateCenterMedian(t);
1005                 break;
1006         case V3D_CURSOR:
1007                 if(t->spacetype==SPACE_IMAGE)
1008                         calculateCenterCursor2D(t);
1009                 else
1010                         calculateCenterCursor(t);
1011                 break;
1012         case V3D_LOCAL:
1013                 /* Individual element center uses median center for helpline and such */
1014                 calculateCenterMedian(t);
1015                 break;
1016         case V3D_ACTIVE:
1017                 {
1018                 /* set median, and if if if... do object center */
1019 #if 0 // TRANSFORM_FIX_ME
1020                 EditSelection ese;
1021                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1022
1023                 if (t->obedit && t->obedit->type == OB_MESH && EM_get_actSelection(&ese)) {
1024                         EM_editselection_center(t->center, &ese);
1025                         calculateCenter2D(t);
1026                         break;
1027                 } /* END EDIT MODE ACTIVE ELEMENT */
1028 #endif
1029                 
1030                 calculateCenterMedian(t);
1031                 if((t->flag & (T_EDIT|T_POSE))==0)
1032                 {
1033                         Scene *scene = t->scene;
1034                         Object *ob= OBACT;
1035                         if(ob)
1036                         {
1037                                 VECCOPY(t->center, ob->obmat[3]);
1038                                 projectIntView(t, t->center, t->center2d);
1039                         }
1040                 }
1041                 
1042                 }
1043         }
1044
1045         /* setting constraint center */
1046         VECCOPY(t->con.center, t->center);
1047         if(t->flag & (T_EDIT|T_POSE))
1048         {
1049                 Object *ob= t->obedit?t->obedit:t->poseobj;
1050                 Mat4MulVecfl(ob->obmat, t->con.center);
1051         }
1052
1053         /* voor panning from cameraview */
1054         if(t->flag & T_OBJECT)
1055         {
1056                 if(t->spacetype==SPACE_VIEW3D)
1057                 {
1058                         View3D *v3d = t->view;
1059                         Scene *scene = t->scene;
1060                         RegionView3D *rv3d = t->ar->regiondata;
1061                         
1062                         if(v3d->camera == OBACT && rv3d->persp==V3D_CAMOB)
1063                         {
1064                                 float axis[3];
1065                                 /* persinv is nasty, use viewinv instead, always right */
1066                                 VECCOPY(axis, t->viewinv[2]);
1067                                 Normalize(axis);
1068         
1069                                 /* 6.0 = 6 grid units */
1070                                 axis[0]= t->center[0]- 6.0f*axis[0];
1071                                 axis[1]= t->center[1]- 6.0f*axis[1];
1072                                 axis[2]= t->center[2]- 6.0f*axis[2];
1073                                 
1074                                 projectIntView(t, axis, t->center2d);
1075                                 
1076                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1077                                 if(t->mode==TFM_TRANSLATION)
1078                                 {
1079                                         VECCOPY(t->center, axis);
1080                                         VECCOPY(t->con.center, t->center);
1081                                 }
1082                         }
1083                 }
1084         }       
1085
1086         if(t->spacetype==SPACE_VIEW3D)
1087         {
1088                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1089                 if(t->flag & (T_EDIT|T_POSE)) {
1090                         Object *ob= t->obedit?t->obedit:t->poseobj;
1091                         float vec[3];
1092                         
1093                         VECCOPY(vec, t->center);
1094                         Mat4MulVecfl(ob->obmat, vec);
1095                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1096                 }
1097                 else {
1098                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1099                 } 
1100         }
1101 }
1102
1103 void calculatePropRatio(TransInfo *t)
1104 {
1105         TransData *td = t->data;
1106         int i;
1107         float dist;
1108         short connected = t->flag & T_PROP_CONNECTED;
1109         
1110         if (t->flag & T_PROP_EDIT) {
1111                 for(i = 0 ; i < t->total; i++, td++) {
1112                         if (td->flag & TD_SELECTED) {
1113                                 td->factor = 1.0f;
1114                         }
1115                         else if ((connected && 
1116                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
1117                                 ||
1118                                         (connected == 0 &&
1119                                                 td->rdist > t->propsize)) {
1120                                 /* 
1121                                    The elements are sorted according to their dist member in the array,
1122                                    that means we can stop when it finds one element outside of the propsize.
1123                                 */
1124                                 td->flag |= TD_NOACTION;
1125                                 td->factor = 0.0f;
1126                                 restoreElement(td);
1127                         }
1128                         else {
1129                                 /* Use rdist for falloff calculations, it is the real distance */
1130                                 td->flag &= ~TD_NOACTION;
1131                                 dist= (t->propsize-td->rdist)/t->propsize;
1132                                 
1133                                 /*
1134                                  * Clamp to positive numbers.
1135                                  * Certain corner cases with connectivity and individual centers
1136                                  * can give values of rdist larger than propsize.
1137                                  */
1138                                 if (dist < 0.0f)
1139                                         dist = 0.0f;
1140                                 
1141                                 switch(t->scene->prop_mode) {
1142                                 case PROP_SHARP:
1143                                         td->factor= dist*dist;
1144                                         break;
1145                                 case PROP_SMOOTH:
1146                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1147                                         break;
1148                                 case PROP_ROOT:
1149                                         td->factor = (float)sqrt(dist);
1150                                         break;
1151                                 case PROP_LIN:
1152                                         td->factor = dist;
1153                                         break;
1154                                 case PROP_CONST:
1155                                         td->factor = 1.0f;
1156                                         break;
1157                                 case PROP_SPHERE:
1158                                         td->factor = (float)sqrt(2*dist - dist * dist);
1159                                         break;
1160                                 case PROP_RANDOM:
1161                                         BLI_srand( BLI_rand() ); /* random seed */
1162                                         td->factor = BLI_frand()*dist;
1163                                         break;
1164                                 default:
1165                                         td->factor = 1;
1166                                 }
1167                         }
1168                 }
1169                 switch(t->scene->prop_mode) {
1170                 case PROP_SHARP:
1171                         strcpy(t->proptext, "(Sharp)");
1172                         break;
1173                 case PROP_SMOOTH:
1174                         strcpy(t->proptext, "(Smooth)");
1175                         break;
1176                 case PROP_ROOT:
1177                         strcpy(t->proptext, "(Root)");
1178                         break;
1179                 case PROP_LIN:
1180                         strcpy(t->proptext, "(Linear)");
1181                         break;
1182                 case PROP_CONST:
1183                         strcpy(t->proptext, "(Constant)");
1184                         break;
1185                 case PROP_SPHERE:
1186                         strcpy(t->proptext, "(Sphere)");
1187                         break;
1188                 case PROP_RANDOM:
1189                         strcpy(t->proptext, "(Random)");
1190                         break;
1191                 default:
1192                         strcpy(t->proptext, "");
1193                 }
1194         }
1195         else {
1196                 for(i = 0 ; i < t->total; i++, td++) {
1197                         td->factor = 1.0;
1198                 }
1199                 strcpy(t->proptext, "");
1200         }
1201 }
1202
1203 TransInfo *BIF_GetTransInfo()
1204 {
1205         return NULL;
1206 }
1207
1208 float get_drawsize(ARegion *ar, float *co)
1209 {
1210         RegionView3D *rv3d= ar->regiondata;
1211         float size, vec[3], len1, len2;
1212         
1213         /* size calculus, depending ortho/persp settings, like initgrabz() */
1214         size= rv3d->persmat[0][3]*co[0]+ rv3d->persmat[1][3]*co[1]+ rv3d->persmat[2][3]*co[2]+ rv3d->persmat[3][3];
1215         
1216         VECCOPY(vec, rv3d->persinv[0]);
1217         len1= Normalize(vec);
1218         VECCOPY(vec, rv3d->persinv[1]);
1219         len2= Normalize(vec);
1220         
1221         size*= 0.01f*(len1>len2?len1:len2);
1222
1223         /* correct for window size to make widgets appear fixed size */
1224         if(ar->winx > ar->winy) size*= 1000.0f/(float)ar->winx;
1225         else size*= 1000.0f/(float)ar->winy;
1226
1227         return size;
1228 }