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