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