svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / blender / editors / transform / transform_generics.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <string.h>
31 #include <math.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLO_sys_types.h" // for intptr_t support
36
37 #include "DNA_anim_types.h"
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_nla_types.h"
46 #include "DNA_node_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_object_force.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_userdef_types.h"
54 #include "DNA_view3d_types.h"
55 #include "DNA_windowmanager_types.h"
56
57 #include "RNA_access.h"
58
59 //#include "BIF_screen.h"
60 //#include "BIF_mywindow.h"
61 #include "BIF_gl.h"
62 //#include "BIF_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         float v1[3], v2[3], v3[3];
624         char col[3], col2[3];
625         
626         if (t->spacetype == SPACE_VIEW3D)
627         {
628                 View3D *v3d = t->view;
629                 
630                 glPushMatrix();
631                 
632                 //if(t->obedit) glLoadMatrixf(t->obedit->obmat);        // sets opengl viewing
633                 
634         
635                 VecCopyf(v3, dir);
636                 VecMulf(v3, v3d->far);
637                 
638                 VecSubf(v2, center, v3);
639                 VecAddf(v1, center, v3);
640         
641                 if (options & DRAWLIGHT) {
642                         col[0] = col[1] = col[2] = 220;
643                 }
644                 else {
645                         UI_GetThemeColor3ubv(TH_GRID, col);
646                 }
647                 UI_make_axis_color(col, col2, axis);
648                 glColor3ubv((GLubyte *)col2);
649         
650                 setlinestyle(0);
651                 glBegin(GL_LINE_STRIP); 
652                         glVertex3fv(v1); 
653                         glVertex3fv(v2); 
654                 glEnd();
655                 
656                 glPopMatrix();
657         }
658 }
659
660 void resetTransRestrictions(TransInfo *t)
661 {
662         t->flag &= ~T_ALL_RESTRICTIONS;
663 }
664
665 int initTransInfo (bContext *C, TransInfo *t, wmOperator *op, wmEvent *event)
666 {
667         Scene *sce = CTX_data_scene(C);
668         ToolSettings *ts = CTX_data_tool_settings(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         t->settings = ts;
684
685         t->data = NULL;
686         t->ext = NULL;
687
688         t->helpline = HLP_NONE;
689
690         t->flag = 0;
691         
692         t->redraw = 1; /* redraw first time */
693         
694         if (event)
695         {
696                 t->imval[0] = event->x - t->ar->winrct.xmin;
697                 t->imval[1] = event->y - t->ar->winrct.ymin;
698                 
699                 t->event_type = event->type;
700         }
701         else
702         {
703                 t->imval[0] = 0;
704                 t->imval[1] = 0;
705         }
706         
707         t->con.imval[0] = t->imval[0];
708         t->con.imval[1] = t->imval[1];
709
710         t->mval[0] = t->imval[0];
711         t->mval[1] = t->imval[1];
712
713         t->transform            = NULL;
714         t->handleEvent          = NULL;
715
716         t->total                        = 0;
717
718         t->val = 0.0f;
719
720         t->vec[0]                       =
721                 t->vec[1]               =
722                 t->vec[2]               = 0.0f;
723         
724         t->center[0]            =
725                 t->center[1]    =
726                 t->center[2]    = 0.0f;
727         
728         Mat3One(t->mat);
729         
730         t->spacetype = sa->spacetype;
731         if(t->spacetype == SPACE_VIEW3D)
732         {
733                 View3D *v3d = sa->spacedata.first;
734                 
735                 t->view = v3d;
736                 t->animtimer= CTX_wm_screen(C)->animtimer;
737                 
738                 if(v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
739                 t->around = v3d->around;
740
741                 if (op && RNA_struct_find_property(op->ptr, "constraint_axis") && RNA_property_is_set(op->ptr, "constraint_orientation"))
742                 {
743                         t->current_orientation = RNA_int_get(op->ptr, "constraint_orientation");
744
745                         if (t->current_orientation >= V3D_MANIP_CUSTOM + BIF_countTransformOrientation(C) - 1)
746                         {
747                                 t->current_orientation = V3D_MANIP_GLOBAL;
748                         }
749                 }
750                 else
751                 {
752                         t->current_orientation = v3d->twmode;
753                 }
754         }
755         else if(t->spacetype==SPACE_IMAGE || t->spacetype==SPACE_NODE)
756         {
757                 SpaceImage *sima = sa->spacedata.first;
758                 // XXX for now, get View2D  from the active region
759                 t->view = &ar->v2d;
760                 t->around = sima->around;
761         }
762         else
763         {
764                 // XXX for now, get View2D  from the active region
765                 t->view = &ar->v2d;
766                 
767                 t->around = V3D_CENTER;
768         }
769
770         if (op && RNA_struct_find_property(op->ptr, "mirror") && RNA_property_is_set(op->ptr, "mirror"))
771         {
772                 if (RNA_boolean_get(op->ptr, "mirror"))
773                 {
774                         t->flag |= T_MIRROR;
775                 }
776         }
777         // Need stuff to take it from edit mesh or whatnot here
778         else
779         {
780                 if (t->obedit && t->obedit->type == OB_MESH && ts->editbutflag & B_MESH_X_MIRROR)
781                 {
782                         t->flag |= T_MIRROR;
783                 }
784         }
785
786         /* setting PET flag */
787         if (op && RNA_struct_find_property(op->ptr, "proportional") && RNA_property_is_set(op->ptr, "proportional"))
788         {
789                 switch(RNA_enum_get(op->ptr, "proportional"))
790                 {
791                 case 2: /* XXX connected constant */
792                         t->flag |= T_PROP_CONNECTED;
793                 case 1: /* XXX prop on constant */
794                         t->flag |= T_PROP_EDIT;
795                         break;
796                 }
797         }
798         else
799         {
800                 if ((t->options & CTX_NO_PET) == 0 && (ts->proportional)) {
801                         t->flag |= T_PROP_EDIT;
802                         
803                         if(ts->proportional == 2)
804                                 t->flag |= T_PROP_CONNECTED;    // yes i know, has to become define
805                 }
806         }
807
808         if (op && RNA_struct_find_property(op->ptr, "proportional_size") && RNA_property_is_set(op->ptr, "proportional_size"))
809         {
810                 t->prop_size = RNA_float_get(op->ptr, "proportional_size");
811         }
812         else
813         {
814                 t->prop_size = ts->proportional_size;
815         }
816         
817         if (op && RNA_struct_find_property(op->ptr, "proportional_editing_falloff") && RNA_property_is_set(op->ptr, "proportional_editing_falloff"))
818         {
819                 t->prop_mode = RNA_enum_get(op->ptr, "proportional_editing_falloff");
820         }
821         else
822         {
823                 t->prop_mode = ts->prop_mode;
824         }
825
826         /* TRANSFORM_FIX_ME rna restrictions */
827         if (t->prop_size <= 0)
828         {
829                 t->prop_size = 1.0f;
830         }
831
832         setTransformViewMatrices(t);
833         initNumInput(&t->num);
834         initNDofInput(&t->ndof);
835         
836         return 1;
837 }
838
839 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
840 void postTrans (TransInfo *t) 
841 {
842         TransData *td;
843
844         if (t->draw_handle)
845         {
846                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle);
847         }
848         
849         /* postTrans can be called when nothing is selected, so data is NULL already */
850         if (t->data) {
851                 int a;
852
853                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
854                 for(a=0, td= t->data; a<t->total; a++, td++) {
855                         if(td->tdi) MEM_freeN(td->tdi);
856                         if (td->flag & TD_BEZTRIPLE) MEM_freeN(td->hdata); 
857                 }
858                 MEM_freeN(t->data);
859         }
860
861         if (t->ext) MEM_freeN(t->ext);
862         if (t->data2d) {
863                 MEM_freeN(t->data2d);
864                 t->data2d= NULL;
865         }
866
867         if(t->spacetype==SPACE_IMAGE) {
868                 SpaceImage *sima= t->sa->spacedata.first;
869                 if(sima->flag & SI_LIVE_UNWRAP)
870                         ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
871         }
872         else if(t->spacetype==SPACE_ACTION) {
873                 if (t->customData)
874                         MEM_freeN(t->customData);
875         }
876 }
877
878 void applyTransObjects(TransInfo *t)
879 {
880         TransData *td;
881         
882         for (td = t->data; td < t->data + t->total; td++) {
883                 VECCOPY(td->iloc, td->loc);
884                 if (td->ext->rot) {
885                         VECCOPY(td->ext->irot, td->ext->rot);
886                 }
887                 if (td->ext->size) {
888                         VECCOPY(td->ext->isize, td->ext->size);
889                 }
890         }       
891         recalcData(t);
892
893
894 /* helper for below */
895 static void restore_ipokey(float *poin, float *old)
896 {
897         if(poin) {
898                 poin[0]= old[0];
899                 poin[-3]= old[3];
900                 poin[3]= old[6];
901         }
902 }
903
904 static void restoreElement(TransData *td) {
905         /* TransData for crease has no loc */
906         if (td->loc) {
907                 VECCOPY(td->loc, td->iloc);
908         }
909         if (td->val) {
910                 *td->val = td->ival;
911         }
912         if (td->ext && (td->flag&TD_NO_EXT)==0) {
913                 if (td->ext->rot) {
914                         VECCOPY(td->ext->rot, td->ext->irot);
915                 }
916                 if (td->ext->size) {
917                         VECCOPY(td->ext->size, td->ext->isize);
918                 }
919                 if(td->flag & TD_USEQUAT) {
920                         if (td->ext->quat) {
921                                 QUATCOPY(td->ext->quat, td->ext->iquat);
922                         }
923                 }
924         }
925         
926         if (td->flag & TD_BEZTRIPLE) {
927                 *(td->hdata->h1) = td->hdata->ih1;
928                 *(td->hdata->h2) = td->hdata->ih2;
929         }
930         
931         if(td->tdi) {
932                 TransDataIpokey *tdi= td->tdi;
933                 
934                 restore_ipokey(tdi->locx, tdi->oldloc);
935                 restore_ipokey(tdi->locy, tdi->oldloc+1);
936                 restore_ipokey(tdi->locz, tdi->oldloc+2);
937
938                 restore_ipokey(tdi->rotx, tdi->oldrot);
939                 restore_ipokey(tdi->roty, tdi->oldrot+1);
940                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
941                 
942                 restore_ipokey(tdi->sizex, tdi->oldsize);
943                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
944                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
945         }
946 }
947
948 void restoreTransObjects(TransInfo *t)
949 {
950         TransData *td;
951         
952         for (td = t->data; td < t->data + t->total; td++) {
953                 restoreElement(td);
954         }
955         
956         Mat3One(t->mat);
957         
958         recalcData(t);
959 }
960
961 void calculateCenter2D(TransInfo *t)
962 {
963         if (t->flag & (T_EDIT|T_POSE)) {
964                 Object *ob= t->obedit?t->obedit:t->poseobj;
965                 float vec[3];
966                 
967                 VECCOPY(vec, t->center);
968                 Mat4MulVecfl(ob->obmat, vec);
969                 projectIntView(t, vec, t->center2d);
970         }
971         else {
972                 projectIntView(t, t->center, t->center2d);
973         }
974 }
975
976 void calculateCenterCursor(TransInfo *t)
977 {
978         float *cursor;
979
980         cursor = give_cursor(t->scene, t->view);
981         VECCOPY(t->center, cursor);
982
983         /* If edit or pose mode, move cursor in local space */
984         if (t->flag & (T_EDIT|T_POSE)) {
985                 Object *ob = t->obedit?t->obedit:t->poseobj;
986                 float mat[3][3], imat[3][3];
987                 
988                 VecSubf(t->center, t->center, ob->obmat[3]);
989                 Mat3CpyMat4(mat, ob->obmat);
990                 Mat3Inv(imat, mat);
991                 Mat3MulVecfl(imat, t->center);
992         }
993         
994         calculateCenter2D(t);
995 }
996
997 void calculateCenterCursor2D(TransInfo *t)
998 {
999         View2D *v2d= t->view;
1000         float aspx=1.0, aspy=1.0;
1001         
1002         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
1003                 ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
1004
1005         if (v2d) {
1006                 t->center[0] = v2d->cursor[0] * aspx; 
1007                 t->center[1] = v2d->cursor[1] * aspy; 
1008         }
1009
1010         calculateCenter2D(t);
1011 }
1012
1013 void calculateCenterMedian(TransInfo *t)
1014 {
1015         float partial[3] = {0.0f, 0.0f, 0.0f};
1016         int total = 0;
1017         int i;
1018         
1019         for(i = 0; i < t->total; i++) {
1020                 if (t->data[i].flag & TD_SELECTED) {
1021                         if (!(t->data[i].flag & TD_NOCENTER))
1022                         {
1023                                 VecAddf(partial, partial, t->data[i].center);
1024                                 total++;
1025                         }
1026                 }
1027                 else {
1028                         /* 
1029                            All the selected elements are at the head of the array 
1030                            which means we can stop when it finds unselected data
1031                         */
1032                         break;
1033                 }
1034         }
1035         if(i)
1036                 VecMulf(partial, 1.0f / total);
1037         VECCOPY(t->center, partial);
1038
1039         calculateCenter2D(t);
1040 }
1041
1042 void calculateCenterBound(TransInfo *t)
1043 {
1044         float max[3];
1045         float min[3];
1046         int i;
1047         for(i = 0; i < t->total; i++) {
1048                 if (i) {
1049                         if (t->data[i].flag & TD_SELECTED) {
1050                                 if (!(t->data[i].flag & TD_NOCENTER))
1051                                         MinMax3(min, max, t->data[i].center);
1052                         }
1053                         else {
1054                                 /* 
1055                                    All the selected elements are at the head of the array 
1056                                    which means we can stop when it finds unselected data
1057                                 */
1058                                 break;
1059                         }
1060                 }
1061                 else {
1062                         VECCOPY(max, t->data[i].center);
1063                         VECCOPY(min, t->data[i].center);
1064                 }
1065         }
1066         VecAddf(t->center, min, max);
1067         VecMulf(t->center, 0.5);
1068
1069         calculateCenter2D(t);
1070 }
1071
1072 void calculateCenter(TransInfo *t) 
1073 {
1074         switch(t->around) {
1075         case V3D_CENTER:
1076                 calculateCenterBound(t);
1077                 break;
1078         case V3D_CENTROID:
1079                 calculateCenterMedian(t);
1080                 break;
1081         case V3D_CURSOR:
1082                 if(t->spacetype==SPACE_IMAGE)
1083                         calculateCenterCursor2D(t);
1084                 else
1085                         calculateCenterCursor(t);
1086                 break;
1087         case V3D_LOCAL:
1088                 /* Individual element center uses median center for helpline and such */
1089                 calculateCenterMedian(t);
1090                 break;
1091         case V3D_ACTIVE:
1092                 {
1093                 /* set median, and if if if... do object center */
1094 #if 0 // TRANSFORM_FIX_ME
1095                 EditSelection ese;
1096                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1097
1098                 if (t->obedit && t->obedit->type == OB_MESH && EM_get_actSelection(&ese)) {
1099                         EM_editselection_center(t->center, &ese);
1100                         calculateCenter2D(t);
1101                         break;
1102                 } /* END EDIT MODE ACTIVE ELEMENT */
1103 #endif
1104                 
1105                 calculateCenterMedian(t);
1106                 if((t->flag & (T_EDIT|T_POSE))==0)
1107                 {
1108                         Scene *scene = t->scene;
1109                         Object *ob= OBACT;
1110                         if(ob)
1111                         {
1112                                 VECCOPY(t->center, ob->obmat[3]);
1113                                 projectIntView(t, t->center, t->center2d);
1114                         }
1115                 }
1116                 
1117                 }
1118         }
1119
1120         /* setting constraint center */
1121         VECCOPY(t->con.center, t->center);
1122         if(t->flag & (T_EDIT|T_POSE))
1123         {
1124                 Object *ob= t->obedit?t->obedit:t->poseobj;
1125                 Mat4MulVecfl(ob->obmat, t->con.center);
1126         }
1127
1128         /* voor panning from cameraview */
1129         if(t->flag & T_OBJECT)
1130         {
1131                 if(t->spacetype==SPACE_VIEW3D)
1132                 {
1133                         View3D *v3d = t->view;
1134                         Scene *scene = t->scene;
1135                         RegionView3D *rv3d = t->ar->regiondata;
1136                         
1137                         if(v3d->camera == OBACT && rv3d->persp==V3D_CAMOB)
1138                         {
1139                                 float axis[3];
1140                                 /* persinv is nasty, use viewinv instead, always right */
1141                                 VECCOPY(axis, t->viewinv[2]);
1142                                 Normalize(axis);
1143         
1144                                 /* 6.0 = 6 grid units */
1145                                 axis[0]= t->center[0]- 6.0f*axis[0];
1146                                 axis[1]= t->center[1]- 6.0f*axis[1];
1147                                 axis[2]= t->center[2]- 6.0f*axis[2];
1148                                 
1149                                 projectIntView(t, axis, t->center2d);
1150                                 
1151                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1152                                 if(t->mode==TFM_TRANSLATION)
1153                                 {
1154                                         VECCOPY(t->center, axis);
1155                                         VECCOPY(t->con.center, t->center);
1156                                 }
1157                         }
1158                 }
1159         }       
1160
1161         if(t->spacetype==SPACE_VIEW3D)
1162         {
1163                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1164                 if(t->flag & (T_EDIT|T_POSE)) {
1165                         Object *ob= t->obedit?t->obedit:t->poseobj;
1166                         float vec[3];
1167                         
1168                         VECCOPY(vec, t->center);
1169                         Mat4MulVecfl(ob->obmat, vec);
1170                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1171                 }
1172                 else {
1173                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1174                 } 
1175         }
1176 }
1177
1178 void calculatePropRatio(TransInfo *t)
1179 {
1180         TransData *td = t->data;
1181         int i;
1182         float dist;
1183         short connected = t->flag & T_PROP_CONNECTED;
1184         
1185         if (t->flag & T_PROP_EDIT) {
1186                 for(i = 0 ; i < t->total; i++, td++) {
1187                         if (td->flag & TD_SELECTED) {
1188                                 td->factor = 1.0f;
1189                         }
1190                         else if ((connected && 
1191                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size))
1192                                 ||
1193                                         (connected == 0 &&
1194                                                 td->rdist > t->prop_size)) {
1195                                 /* 
1196                                    The elements are sorted according to their dist member in the array,
1197                                    that means we can stop when it finds one element outside of the propsize.
1198                                 */
1199                                 td->flag |= TD_NOACTION;
1200                                 td->factor = 0.0f;
1201                                 restoreElement(td);
1202                         }
1203                         else {
1204                                 /* Use rdist for falloff calculations, it is the real distance */
1205                                 td->flag &= ~TD_NOACTION;
1206                                 dist= (t->prop_size-td->rdist)/t->prop_size;
1207                                 
1208                                 /*
1209                                  * Clamp to positive numbers.
1210                                  * Certain corner cases with connectivity and individual centers
1211                                  * can give values of rdist larger than propsize.
1212                                  */
1213                                 if (dist < 0.0f)
1214                                         dist = 0.0f;
1215                                 
1216                                 switch(t->prop_mode) {
1217                                 case PROP_SHARP:
1218                                         td->factor= dist*dist;
1219                                         break;
1220                                 case PROP_SMOOTH:
1221                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1222                                         break;
1223                                 case PROP_ROOT:
1224                                         td->factor = (float)sqrt(dist);
1225                                         break;
1226                                 case PROP_LIN:
1227                                         td->factor = dist;
1228                                         break;
1229                                 case PROP_CONST:
1230                                         td->factor = 1.0f;
1231                                         break;
1232                                 case PROP_SPHERE:
1233                                         td->factor = (float)sqrt(2*dist - dist * dist);
1234                                         break;
1235                                 case PROP_RANDOM:
1236                                         BLI_srand( BLI_rand() ); /* random seed */
1237                                         td->factor = BLI_frand()*dist;
1238                                         break;
1239                                 default:
1240                                         td->factor = 1;
1241                                 }
1242                         }
1243                 }
1244                 switch(t->prop_mode) {
1245                 case PROP_SHARP:
1246                         strcpy(t->proptext, "(Sharp)");
1247                         break;
1248                 case PROP_SMOOTH:
1249                         strcpy(t->proptext, "(Smooth)");
1250                         break;
1251                 case PROP_ROOT:
1252                         strcpy(t->proptext, "(Root)");
1253                         break;
1254                 case PROP_LIN:
1255                         strcpy(t->proptext, "(Linear)");
1256                         break;
1257                 case PROP_CONST:
1258                         strcpy(t->proptext, "(Constant)");
1259                         break;
1260                 case PROP_SPHERE:
1261                         strcpy(t->proptext, "(Sphere)");
1262                         break;
1263                 case PROP_RANDOM:
1264                         strcpy(t->proptext, "(Random)");
1265                         break;
1266                 default:
1267                         strcpy(t->proptext, "");
1268                 }
1269         }
1270         else {
1271                 for(i = 0 ; i < t->total; i++, td++) {
1272                         td->factor = 1.0;
1273                 }
1274                 strcpy(t->proptext, "");
1275         }
1276 }
1277
1278 TransInfo *BIF_GetTransInfo()
1279 {
1280         return NULL;
1281 }
1282
1283 float get_drawsize(ARegion *ar, float *co)
1284 {
1285         RegionView3D *rv3d= ar->regiondata;
1286         float size, vec[3], len1, len2;
1287         
1288         /* size calculus, depending ortho/persp settings, like initgrabz() */
1289         size= rv3d->persmat[0][3]*co[0]+ rv3d->persmat[1][3]*co[1]+ rv3d->persmat[2][3]*co[2]+ rv3d->persmat[3][3];
1290         
1291         VECCOPY(vec, rv3d->persinv[0]);
1292         len1= Normalize(vec);
1293         VECCOPY(vec, rv3d->persinv[1]);
1294         len2= Normalize(vec);
1295         
1296         size*= 0.01f*(len1>len2?len1:len2);
1297
1298         /* correct for window size to make widgets appear fixed size */
1299         if(ar->winx > ar->winy) size*= 1000.0f/(float)ar->winx;
1300         else size*= 1000.0f/(float)ar->winy;
1301
1302         return size;
1303 }