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