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