51a820dcdcb7e49d9bb73460687d8c07ab369545
[blender-staging.git] / source / blender / editors / transform / transform_manipulator.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) 2005 Blender Foundation
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 <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33
34 #ifndef WIN32
35 #include <unistd.h>
36 #else
37 #include <io.h>
38 #endif
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_armature_types.h"
43 #include "DNA_action_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meta_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_userdef_types.h"
54 #include "DNA_view3d_types.h"
55
56 #include "RNA_access.h"
57
58 #include "BKE_armature.h"
59 #include "BKE_context.h"
60 #include "BKE_global.h"
61 #include "BKE_lattice.h"
62 #include "BKE_mesh.h"
63 #include "BKE_object.h"
64 #include "BKE_particle.h"
65 #include "BKE_pointcache.h"
66 #include "BKE_utildefines.h"
67
68 #include "BLI_arithb.h"
69 #include "BLI_editVert.h"
70
71 #include "BIF_gl.h"
72
73 #include "WM_api.h"
74 #include "WM_types.h"
75
76 #include "ED_armature.h"
77 #include "ED_mesh.h"
78 #include "ED_particle.h"
79 #include "ED_space_api.h"
80 #include "ED_transform.h"
81 #include "ED_view3d.h"
82
83 #include "UI_resources.h"
84
85 /* local module include */
86 #include "transform.h"
87
88 /* return codes for select, and drawing flags */
89
90 #define MAN_TRANS_X             1
91 #define MAN_TRANS_Y             2
92 #define MAN_TRANS_Z             4
93 #define MAN_TRANS_C             7
94
95 #define MAN_ROT_X               8
96 #define MAN_ROT_Y               16
97 #define MAN_ROT_Z               32
98 #define MAN_ROT_V               64
99 #define MAN_ROT_T               128
100 #define MAN_ROT_C               248
101
102 #define MAN_SCALE_X             256
103 #define MAN_SCALE_Y             512
104 #define MAN_SCALE_Z             1024
105 #define MAN_SCALE_C             1792
106
107 /* color codes */
108
109 #define MAN_RGB         0
110 #define MAN_GHOST       1
111 #define MAN_MOVECOL     2
112
113
114 static int is_mat4_flipped(float mat[][4])
115 {
116         float vec[3];
117
118         Crossf(vec, mat[0], mat[1]);
119         if( Inpf(vec, mat[2]) < 0.0 ) return 1;
120         return 0;
121 }
122
123 /* transform widget center calc helper for below */
124 static void calc_tw_center(Scene *scene, float *co)
125 {
126         float *twcent= scene->twcent;
127         float *min= scene->twmin;
128         float *max= scene->twmax;
129
130         DO_MINMAX(co, min, max);
131         VecAddf(twcent, twcent, co);
132 }
133
134 static void protectflag_to_drawflags(short protectflag, short *drawflags)
135 {
136         if(protectflag & OB_LOCK_LOCX)
137                 *drawflags &= ~MAN_TRANS_X;
138         if(protectflag & OB_LOCK_LOCY)
139                 *drawflags &= ~MAN_TRANS_Y;
140         if(protectflag & OB_LOCK_LOCZ)
141                 *drawflags &= ~MAN_TRANS_Z;
142
143         if(protectflag & OB_LOCK_ROTX)
144                 *drawflags &= ~MAN_ROT_X;
145         if(protectflag & OB_LOCK_ROTY)
146                 *drawflags &= ~MAN_ROT_Y;
147         if(protectflag & OB_LOCK_ROTZ)
148                 *drawflags &= ~MAN_ROT_Z;
149
150         if(protectflag & OB_LOCK_SCALEX)
151                 *drawflags &= ~MAN_SCALE_X;
152         if(protectflag & OB_LOCK_SCALEY)
153                 *drawflags &= ~MAN_SCALE_Y;
154         if(protectflag & OB_LOCK_SCALEZ)
155                 *drawflags &= ~MAN_SCALE_Z;
156 }
157
158 /* for pose mode */
159 static void stats_pose(Scene *scene, View3D *v3d, bPoseChannel *pchan)
160 {
161         Bone *bone= pchan->bone;
162
163         if(bone) {
164                 if (bone->flag & BONE_TRANSFORM) {
165                         calc_tw_center(scene, pchan->pose_head);
166                         protectflag_to_drawflags(pchan->protectflag, &v3d->twdrawflag);
167                 }
168         }
169 }
170
171 /* for editmode*/
172 static void stats_editbone(View3D *v3d, EditBone *ebo)
173 {
174         if (ebo->flag & BONE_EDITMODE_LOCKED)
175                 protectflag_to_drawflags(OB_LOCK_LOC|OB_LOCK_ROT|OB_LOCK_SCALE, &v3d->twdrawflag);
176 }
177
178
179 static int test_rotmode_euler(short rotmode)
180 {
181         return (ELEM(rotmode, ROT_MODE_AXISANGLE, ROT_MODE_QUAT)) ? 0:1;
182 }
183
184 void gimbalAxis(Object *ob, float gmat[][3])
185 {
186         if(ob->mode & OB_MODE_POSE)
187         {
188                 bPoseChannel *pchan= NULL;
189
190                 /* use channels to get stats */
191                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
192                         if (pchan->bone && pchan->bone->flag & BONE_ACTIVE) {
193                                 if(test_rotmode_euler(pchan->rotmode)) {
194                                         break;
195                                 }
196                         }
197                 }
198
199                 if(pchan) {
200                         int i;
201
202                         EulToGimbalAxis(gmat, pchan->eul, pchan->rotmode);
203
204                         for (i=0; i<3; i++)
205                                 Mat3MulVecfl(pchan->bone->bone_mat, gmat[i]);
206
207                         if(pchan->parent) {
208                                 bPoseChannel *pchan_par= pchan->parent;
209
210                                 float tmat[3][3];
211                                 Mat3CpyMat4(tmat, pchan_par->pose_mat);
212
213                                 for (i=0; i<3; i++)
214                                         Mat3MulVecfl(tmat, gmat[i]);
215                         }
216
217                         /* needed if object trans isnt identity */
218                         for (i=0; i<3; i++) {
219                                 float tmat[3][3];
220                                 Mat3CpyMat4(tmat, ob->obmat);
221                                 Mat3MulVecfl(tmat, gmat[i]);
222                         }
223                 }
224         }
225         else {
226                 if(test_rotmode_euler(ob->rotmode)) {
227                         EulToGimbalAxis(gmat, ob->rot, ob->rotmode);
228                 }
229         }
230 }
231
232
233 /* centroid, boundbox, of selection */
234 /* returns total items selected */
235 int calc_manipulator_stats(const bContext *C)
236 {
237         ScrArea *sa= CTX_wm_area(C);
238         ARegion *ar= CTX_wm_region(C);
239         Scene *scene= CTX_data_scene(C);
240         Object *obedit= CTX_data_edit_object(C);
241         View3D *v3d= sa->spacedata.first;
242         RegionView3D *rv3d= ar->regiondata;
243         Base *base;
244         Object *ob= OBACT;
245         float normal[3]={0.0, 0.0, 0.0};
246         float plane[3]={0.0, 0.0, 0.0};
247         int a, totsel= 0;
248
249         /* transform widget matrix */
250         Mat4One(rv3d->twmat);
251
252         v3d->twdrawflag= 0xFFFF;
253
254         /* transform widget centroid/center */
255         scene->twcent[0]= scene->twcent[1]= scene->twcent[2]= 0.0f;
256         INIT_MINMAX(scene->twmin, scene->twmax);
257
258         if(obedit) {
259                 ob= obedit;
260                 if((ob->lay & v3d->lay)==0) return 0;
261
262                 if(obedit->type==OB_MESH) {
263                         EditMesh *em = BKE_mesh_get_editmesh(obedit->data);
264                         EditVert *eve;
265                         EditSelection ese;
266                         float vec[3]= {0,0,0};
267
268                         /* USE LAST SELECTE WITH ACTIVE */
269                         if (v3d->around==V3D_ACTIVE && EM_get_actSelection(em, &ese)) {
270                                 EM_editselection_center(vec, &ese);
271                                 calc_tw_center(scene, vec);
272                                 totsel= 1;
273                         } else {
274                                 /* do vertices for center, and if still no normal found, use vertex normals */
275                                 for(eve= em->verts.first; eve; eve= eve->next) {
276                                         if(eve->f & SELECT) {
277                                                 totsel++;
278                                                 calc_tw_center(scene, eve->co);
279                                         }
280                                 }
281                         }
282                 } /* end editmesh */
283                 else if (obedit->type==OB_ARMATURE){
284                         bArmature *arm= obedit->data;
285                         EditBone *ebo;
286                         for (ebo= arm->edbo->first; ebo; ebo=ebo->next){
287                                 if(ebo->layer & arm->layer) {
288                                         if (ebo->flag & BONE_TIPSEL) {
289                                                 calc_tw_center(scene, ebo->tail);
290                                                 totsel++;
291                                         }
292                                         if (ebo->flag & BONE_ROOTSEL) {
293                                                 calc_tw_center(scene, ebo->head);
294                                                 totsel++;
295                                         }
296                                         if (ebo->flag & BONE_SELECTED) {
297                                                 stats_editbone(v3d, ebo);
298                                         }
299                                 }
300                         }
301                 }
302                 else if ELEM(obedit->type, OB_CURVE, OB_SURF) {
303                         Curve *cu= obedit->data;
304                         Nurb *nu;
305                         BezTriple *bezt;
306                         BPoint *bp;
307
308                         nu= cu->editnurb->first;
309                         while(nu) {
310                                 if(nu->type == CU_BEZIER) {
311                                         bezt= nu->bezt;
312                                         a= nu->pntsu;
313                                         while(a--) {
314                                                 /* exceptions
315                                                  * if handles are hidden then only check the center points.
316                                                  * If 2 or more are selected then only use the center point too.
317                                                  */
318                                                 if (cu->drawflag & CU_HIDE_HANDLES) {
319                                                         if (bezt->f2 & SELECT) {
320                                                                 calc_tw_center(scene, bezt->vec[1]);
321                                                                 totsel++;
322                                                         }
323                                                 }
324                                                 else if ( (bezt->f1 & SELECT) + (bezt->f2 & SELECT) + (bezt->f3 & SELECT) > SELECT ) {
325                                                         calc_tw_center(scene, bezt->vec[1]);
326                                                         totsel++;
327                                                 }
328                                                 else {
329                                                         if(bezt->f1) {
330                                                                 calc_tw_center(scene, bezt->vec[0]);
331                                                                 totsel++;
332                                                         }
333                                                         if(bezt->f2) {
334                                                                 calc_tw_center(scene, bezt->vec[1]);
335                                                                 totsel++;
336                                                         }
337                                                         if(bezt->f3) {
338                                                                 calc_tw_center(scene, bezt->vec[2]);
339                                                                 totsel++;
340                                                         }
341                                                 }
342                                                 bezt++;
343                                         }
344                                 }
345                                 else {
346                                         bp= nu->bp;
347                                         a= nu->pntsu*nu->pntsv;
348                                         while(a--) {
349                                                 if(bp->f1 & SELECT) {
350                                                         calc_tw_center(scene, bp->vec);
351                                                         totsel++;
352                                                 }
353                                                 bp++;
354                                         }
355                                 }
356                                 nu= nu->next;
357                         }
358                 }
359                 else if(obedit->type==OB_MBALL) {
360                         MetaBall *mb = (MetaBall*)obedit->data;
361                         MetaElem *ml, *ml_sel=NULL;
362
363                         ml= mb->editelems->first;
364                         while(ml) {
365                                 if(ml->flag & SELECT) {
366                                         calc_tw_center(scene, &ml->x);
367                                         ml_sel = ml;
368                                         totsel++;
369                                 }
370                                 ml= ml->next;
371                         }
372                 }
373                 else if(obedit->type==OB_LATTICE) {
374                         BPoint *bp;
375                         Lattice *lt= obedit->data;
376
377                         bp= lt->editlatt->def;
378
379                         a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw;
380                         while(a--) {
381                                 if(bp->f1 & SELECT) {
382                                         calc_tw_center(scene, bp->vec);
383                                         totsel++;
384                                 }
385                                 bp++;
386                         }
387                 }
388
389                 /* selection center */
390                 if(totsel) {
391                         VecMulf(scene->twcent, 1.0f/(float)totsel);     // centroid!
392                         Mat4MulVecfl(obedit->obmat, scene->twcent);
393                         Mat4MulVecfl(obedit->obmat, scene->twmin);
394                         Mat4MulVecfl(obedit->obmat, scene->twmax);
395                 }
396         }
397         else if(ob && (ob->mode & OB_MODE_POSE)) {
398                 bPoseChannel *pchan;
399                 int mode = TFM_ROTATION; // mislead counting bones... bah. We don't know the manipulator mode, could be mixed
400
401                 if((ob->lay & v3d->lay)==0) return 0;
402
403                 totsel = count_set_pose_transflags(&mode, 0, ob);
404
405                 if(totsel) {
406                         /* use channels to get stats */
407                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
408                                 stats_pose(scene, v3d, pchan);
409                         }
410
411                         VecMulf(scene->twcent, 1.0f/(float)totsel);     // centroid!
412                         Mat4MulVecfl(ob->obmat, scene->twcent);
413                         Mat4MulVecfl(ob->obmat, scene->twmin);
414                         Mat4MulVecfl(ob->obmat, scene->twmax);
415                 }
416         }
417         else if(ob && (ob->mode & (OB_MODE_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT))) {
418                 ;
419         }
420         else if(ob && ob->mode & OB_MODE_PARTICLE_EDIT) {
421                 PTCacheEdit *edit= PE_get_current(scene, ob);
422                 PTCacheEditPoint *point;
423                 PTCacheEditKey *ek;
424                 int k;
425
426                 if(edit) {
427                         point = edit->points;
428                         for(a=0; a<edit->totpoint; a++,point++) {
429                                 if(point->flag & PEP_HIDE) continue;
430
431                                 for(k=0, ek=point->keys; k<point->totkey; k++, ek++) {
432                                         if(ek->flag & PEK_SELECT) {
433                                                 calc_tw_center(scene, ek->flag & PEK_USE_WCO ? ek->world_co : ek->co);
434                                                 totsel++;
435                                         }
436                                 }
437                         }
438
439                         /* selection center */
440                         if(totsel)
441                                 VecMulf(scene->twcent, 1.0f/(float)totsel);     // centroid!
442                 }
443         }
444         else {
445
446                 /* we need the one selected object, if its not active */
447                 ob= OBACT;
448                 if(ob && !(ob->flag & SELECT)) ob= NULL;
449
450                 for(base= scene->base.first; base; base= base->next) {
451                         if TESTBASELIB(scene, base) {
452                                 if(ob==NULL)
453                                         ob= base->object;
454                                 calc_tw_center(scene, base->object->obmat[3]);
455                                 protectflag_to_drawflags(base->object->protectflag, &v3d->twdrawflag);
456                                 totsel++;
457                         }
458                 }
459
460                 /* selection center */
461                 if(totsel) {
462                         VecMulf(scene->twcent, 1.0f/(float)totsel);     // centroid!
463                 }
464         }
465
466         /* global, local or normal orientation? */
467         if(ob && totsel) {
468
469                 switch(v3d->twmode) {
470                 
471                 case V3D_MANIP_GLOBAL:
472                         break; /* nothing to do */
473
474                 case V3D_MANIP_GIMBAL:
475                 {
476                         float mat[3][3];
477                         Mat3One(mat);
478                         gimbalAxis(ob, mat);
479                         Mat4CpyMat3(rv3d->twmat, mat);
480                         break;
481                 }
482                 case V3D_MANIP_NORMAL:
483                         if(obedit || ob->mode & OB_MODE_POSE) {
484                                 float mat[3][3];
485                                 int type;
486
487                                 type = getTransformOrientation(C, normal, plane, (v3d->around == V3D_ACTIVE));
488
489                                 switch (type)
490                                 {
491                                         case ORIENTATION_NORMAL:
492                                                 if (createSpaceNormalTangent(mat, normal, plane) == 0)
493                                                 {
494                                                         type = ORIENTATION_NONE;
495                                                 }
496                                                 break;
497                                         case ORIENTATION_VERT:
498                                                 if (createSpaceNormal(mat, normal) == 0)
499                                                 {
500                                                         type = ORIENTATION_NONE;
501                                                 }
502                                                 break;
503                                         case ORIENTATION_EDGE:
504                                                 if (createSpaceNormalTangent(mat, normal, plane) == 0)
505                                                 {
506                                                         type = ORIENTATION_NONE;
507                                                 }
508                                                 break;
509                                         case ORIENTATION_FACE:
510                                                 if (createSpaceNormalTangent(mat, normal, plane) == 0)
511                                                 {
512                                                         type = ORIENTATION_NONE;
513                                                 }
514                                                 break;
515                                 }
516
517                                 if (type == ORIENTATION_NONE)
518                                 {
519                                         Mat4One(rv3d->twmat);
520                                 }
521                                 else
522                                 {
523                                         Mat4CpyMat3(rv3d->twmat, mat);
524                                 }
525                                 break;
526                         }
527                         /* no break we define 'normal' as 'local' in Object mode */
528                 case V3D_MANIP_LOCAL:
529                         Mat4CpyMat4(rv3d->twmat, ob->obmat);
530                         Mat4Ortho(rv3d->twmat);
531                         break;
532
533                 case V3D_MANIP_VIEW:
534                         {
535                                 float mat[3][3];
536                                 Mat3CpyMat4(mat, rv3d->viewinv);
537                                 Mat3Ortho(mat);
538                                 Mat4CpyMat3(rv3d->twmat, mat);
539                         }
540                         break;
541                 default: /* V3D_MANIP_CUSTOM */
542                         {
543                                 float mat[3][3];
544                                 applyTransformOrientation(C, mat, NULL);
545                                 Mat4CpyMat3(rv3d->twmat, mat);
546                                 break;
547                         }
548                 }
549
550         }
551
552         return totsel;
553 }
554
555 /* ******************** DRAWING STUFFIES *********** */
556
557 static float screen_aligned(RegionView3D *rv3d, float mat[][4])
558 {
559         float vec[3], size;
560
561         VECCOPY(vec, mat[0]);
562         size= Normalize(vec);
563
564         glTranslatef(mat[3][0], mat[3][1], mat[3][2]);
565
566         /* sets view screen aligned */
567         glRotatef( -360.0f*saacos(rv3d->viewquat[0])/(float)M_PI, rv3d->viewquat[1], rv3d->viewquat[2], rv3d->viewquat[3]);
568
569         return size;
570 }
571
572
573 /* radring = radius of donut rings
574    radhole = radius hole
575    start = starting segment (based on nrings)
576    end   = end segment
577    nsides = amount of points in ring
578    nrigns = amount of rings
579 */
580 static void partial_donut(float radring, float radhole, int start, int end, int nsides, int nrings)
581 {
582         float theta, phi, theta1;
583         float cos_theta, sin_theta;
584         float cos_theta1, sin_theta1;
585         float ring_delta, side_delta;
586         int i, j, docaps= 1;
587
588         if(start==0 && end==nrings) docaps= 0;
589
590         ring_delta= 2.0f*(float)M_PI/(float)nrings;
591         side_delta= 2.0f*(float)M_PI/(float)nsides;
592
593         theta= (float)M_PI+0.5f*ring_delta;
594         cos_theta= (float)cos(theta);
595         sin_theta= (float)sin(theta);
596
597         for(i= nrings - 1; i >= 0; i--) {
598                 theta1= theta + ring_delta;
599                 cos_theta1= (float)cos(theta1);
600                 sin_theta1= (float)sin(theta1);
601
602                 if(docaps && i==start) {        // cap
603                         glBegin(GL_POLYGON);
604                         phi= 0.0;
605                         for(j= nsides; j >= 0; j--) {
606                                 float cos_phi, sin_phi, dist;
607
608                                 phi += side_delta;
609                                 cos_phi= (float)cos(phi);
610                                 sin_phi= (float)sin(phi);
611                                 dist= radhole + radring * cos_phi;
612
613                                 glVertex3f(cos_theta1 * dist, -sin_theta1 * dist,  radring * sin_phi);
614                         }
615                         glEnd();
616                 }
617                 if(i>=start && i<=end) {
618                         glBegin(GL_QUAD_STRIP);
619                         phi= 0.0;
620                         for(j= nsides; j >= 0; j--) {
621                                 float cos_phi, sin_phi, dist;
622
623                                 phi += side_delta;
624                                 cos_phi= (float)cos(phi);
625                                 sin_phi= (float)sin(phi);
626                                 dist= radhole + radring * cos_phi;
627
628                                 glVertex3f(cos_theta1 * dist, -sin_theta1 * dist, radring * sin_phi);
629                                 glVertex3f(cos_theta * dist, -sin_theta * dist,  radring * sin_phi);
630                         }
631                         glEnd();
632                 }
633
634                 if(docaps && i==end) {  // cap
635                         glBegin(GL_POLYGON);
636                         phi= 0.0;
637                         for(j= nsides; j >= 0; j--) {
638                                 float cos_phi, sin_phi, dist;
639
640                                 phi -= side_delta;
641                                 cos_phi= (float)cos(phi);
642                                 sin_phi= (float)sin(phi);
643                                 dist= radhole + radring * cos_phi;
644
645                                 glVertex3f(cos_theta * dist, -sin_theta * dist,  radring * sin_phi);
646                         }
647                         glEnd();
648                 }
649
650
651                 theta= theta1;
652                 cos_theta= cos_theta1;
653                 sin_theta= sin_theta1;
654         }
655 }
656
657 /* three colors can be set;
658    grey for ghosting
659    moving: in transform theme color
660    else the red/green/blue
661 */
662 static void manipulator_setcolor(View3D *v3d, char axis, int colcode)
663 {
664         float vec[4];
665         char col[4];
666
667         vec[3]= 0.7f; // alpha set on 0.5, can be glEnabled or not
668
669         if(colcode==MAN_GHOST) {
670                 glColor4ub(0, 0, 0, 70);
671         }
672         else if(colcode==MAN_MOVECOL) {
673                 UI_GetThemeColor3ubv(TH_TRANSFORM, col);
674                 glColor4ub(col[0], col[1], col[2], 128);
675         }
676         else {
677                 switch(axis) {
678                 case 'c':
679                         UI_GetThemeColor3ubv(TH_TRANSFORM, col);
680                         if(v3d->twmode == V3D_MANIP_LOCAL) {
681                                 col[0]= col[0]>200?255:col[0]+55;
682                                 col[1]= col[1]>200?255:col[1]+55;
683                                 col[2]= col[2]>200?255:col[2]+55;
684                         }
685                         else if(v3d->twmode == V3D_MANIP_NORMAL) {
686                                 col[0]= col[0]<55?0:col[0]-55;
687                                 col[1]= col[1]<55?0:col[1]-55;
688                                 col[2]= col[2]<55?0:col[2]-55;
689                         }
690                         glColor4ub(col[0], col[1], col[2], 128);
691                         break;
692                 case 'x':
693                         glColor4ub(220, 0, 0, 128);
694                         break;
695                 case 'y':
696                         glColor4ub(0, 220, 0, 128);
697                         break;
698                 case 'z':
699                         glColor4ub(30, 30, 220, 128);
700                         break;
701                 }
702         }
703 }
704
705 /* viewmatrix should have been set OK, also no shademode! */
706 static void draw_manipulator_axes(View3D *v3d, int colcode, int flagx, int flagy, int flagz)
707 {
708
709         /* axes */
710         if(flagx) {
711                 manipulator_setcolor(v3d, 'x', colcode);
712                 if(flagx & MAN_SCALE_X) glLoadName(MAN_SCALE_X);
713                 else if(flagx & MAN_TRANS_X) glLoadName(MAN_TRANS_X);
714                 glBegin(GL_LINES);
715                 glVertex3f(0.2f, 0.0f, 0.0f);
716                 glVertex3f(1.0f, 0.0f, 0.0f);
717                 glEnd();
718         }
719         if(flagy) {
720                 if(flagy & MAN_SCALE_Y) glLoadName(MAN_SCALE_Y);
721                 else if(flagy & MAN_TRANS_Y) glLoadName(MAN_TRANS_Y);
722                 manipulator_setcolor(v3d, 'y', colcode);
723                 glBegin(GL_LINES);
724                 glVertex3f(0.0f, 0.2f, 0.0f);
725                 glVertex3f(0.0f, 1.0f, 0.0f);
726                 glEnd();
727         }
728         if(flagz) {
729                 if(flagz & MAN_SCALE_Z) glLoadName(MAN_SCALE_Z);
730                 else if(flagz & MAN_TRANS_Z) glLoadName(MAN_TRANS_Z);
731                 manipulator_setcolor(v3d, 'z', colcode);
732                 glBegin(GL_LINES);
733                 glVertex3f(0.0f, 0.0f, 0.2f);
734                 glVertex3f(0.0f, 0.0f, 1.0f);
735                 glEnd();
736         }
737 }
738
739 static void preOrtho(int ortho, float twmat[][4], int axis)
740 {
741         if (ortho == 0) {
742                 float omat[4][4];
743                 Mat4CpyMat4(omat, twmat);
744                 Mat4Orthogonal(omat, axis);
745                 glPushMatrix();
746                 wmMultMatrix(omat);
747         }
748 }
749
750 static void preOrthoFront(int ortho, float twmat[][4], int axis)
751 {
752         if (ortho == 0) {
753                 float omat[4][4];
754                 Mat4CpyMat4(omat, twmat);
755                 Mat4Orthogonal(omat, axis);
756                 glPushMatrix();
757                 wmMultMatrix(omat);
758                 glFrontFace( is_mat4_flipped(omat)?GL_CW:GL_CCW);
759         }
760 }
761
762 static void postOrtho(int ortho)
763 {
764         if (ortho == 0) {
765                 glPopMatrix();
766         }
767 }
768
769 /* only called while G.moving */
770 static void draw_manipulator_rotate_ghost(View3D *v3d, RegionView3D *rv3d, int drawflags)
771 {
772         GLUquadricObj *qobj;
773         float size, phi, startphi, vec[3], svec[3], matt[4][4], cross[3], tmat[3][3];
774         int arcs= (G.rt!=2);
775         int ortho;
776
777         glDisable(GL_DEPTH_TEST);
778
779         qobj= gluNewQuadric();
780         gluQuadricDrawStyle(qobj, GLU_FILL);
781
782         glColor4ub(0,0,0,64);
783         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
784         glEnable(GL_BLEND);
785
786         /* we need both [4][4] transforms, t->mat seems to be premul, not post for mat[][4] */
787         Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
788 // XXX  Mat4MulMat34(matt, t->mat, rv3d->twmat);
789
790         /* Screen aligned view rot circle */
791         if(drawflags & MAN_ROT_V) {
792
793                 /* prepare for screen aligned draw */
794                 glPushMatrix();
795                 size= screen_aligned(rv3d, rv3d->twmat);
796
797                 vec[0]= 0; // XXX (float)(t->con.imval[0] - t->center2d[0]);
798                 vec[1]= 0; // XXX (float)(t->con.imval[1] - t->center2d[1]);
799                 vec[2]= 0.0f;
800                 Normalize(vec);
801
802                 startphi= saacos( vec[1] );
803                 if(vec[0]<0.0) startphi= -startphi;
804
805                 phi= 0; // XXX (float)fmod(180.0*t->val/M_PI, 360.0);
806                 if(phi > 180.0) phi-= 360.0;
807                 else if(phi<-180.0) phi+= 360.0;
808
809                 gluPartialDisk(qobj, 0.0, size, 32, 1, 180.0*startphi/M_PI, phi);
810
811                 glPopMatrix();
812         }
813         else if(arcs) {
814                 float imat[3][3], ivmat[3][3];
815                 /* try to get the start rotation */
816
817                 svec[0]= 0; // XXX (float)(t->con.imval[0] - t->center2d[0]);
818                 svec[1]= 0; // XXX (float)(t->con.imval[1] - t->center2d[1]);
819                 svec[2]= 0.0f;
820
821                 /* screen aligned vec transform back to manipulator space */
822                 Mat3CpyMat4(ivmat, rv3d->viewinv);
823                 Mat3CpyMat4(tmat, rv3d->twmat);
824                 Mat3Inv(imat, tmat);
825                 Mat3MulMat3(tmat, imat, ivmat);
826
827                 Mat3MulVecfl(tmat, svec);       // tmat is used further on
828                 Normalize(svec);
829         }
830         
831         ortho = IsMat4Orthogonal(rv3d->twmat);
832
833         if (ortho) {
834                 wmMultMatrix(rv3d->twmat);      // aligns with original widget
835         }
836         
837
838         /* Z disk */
839         if(drawflags & MAN_ROT_Z) {
840                 preOrtho(ortho, rv3d->twmat, 2);
841                 
842                 if(arcs) {
843                         /* correct for squeezed arc */
844                         svec[0]+= tmat[2][0];
845                         svec[1]+= tmat[2][1];
846                         Normalize(svec);
847
848                         startphi= (float)atan2(svec[0], svec[1]);
849                 }
850                 else startphi= 0.5f*(float)M_PI;
851
852                 VECCOPY(vec, rv3d->twmat[0]);   // use x axis to detect rotation
853                 Normalize(vec);
854                 Normalize(matt[0]);
855                 phi= saacos( Inpf(vec, matt[0]) );
856                 if(phi!=0.0) {
857                         Crossf(cross, vec, matt[0]);    // results in z vector
858                         if(Inpf(cross, rv3d->twmat[2]) > 0.0) phi= -phi;
859                         gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*(phi)/M_PI);
860                 }
861
862                 postOrtho(ortho);
863         }
864         /* X disk */
865         if(drawflags & MAN_ROT_X) {
866                 preOrtho(ortho, rv3d->twmat, 0);
867                 
868                 if(arcs) {
869                         /* correct for squeezed arc */
870                         svec[1]+= tmat[2][1];
871                         svec[2]+= tmat[2][2];
872                         Normalize(svec);
873
874                         startphi= (float)(M_PI + atan2(svec[2], -svec[1]));
875                 }
876                 else startphi= 0.0f;
877
878                 VECCOPY(vec, rv3d->twmat[1]);   // use y axis to detect rotation
879                 Normalize(vec);
880                 Normalize(matt[1]);
881                 phi= saacos( Inpf(vec, matt[1]) );
882                 if(phi!=0.0) {
883                         Crossf(cross, vec, matt[1]);    // results in x vector
884                         if(Inpf(cross, rv3d->twmat[0]) > 0.0) phi= -phi;
885                         glRotatef(90.0, 0.0, 1.0, 0.0);
886                         gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*phi/M_PI);
887                         glRotatef(-90.0, 0.0, 1.0, 0.0);
888                 }
889
890                 postOrtho(ortho);
891         }
892         /* Y circle */
893         if(drawflags & MAN_ROT_Y) {
894                 preOrtho(ortho, rv3d->twmat, 1);
895                 
896                 if(arcs) {
897                         /* correct for squeezed arc */
898                         svec[0]+= tmat[2][0];
899                         svec[2]+= tmat[2][2];
900                         Normalize(svec);
901
902                         startphi= (float)(M_PI + atan2(-svec[0], svec[2]));
903                 }
904                 else startphi= (float)M_PI;
905
906                 VECCOPY(vec, rv3d->twmat[2]);   // use z axis to detect rotation
907                 Normalize(vec);
908                 Normalize(matt[2]);
909                 phi= saacos( Inpf(vec, matt[2]) );
910                 if(phi!=0.0) {
911                         Crossf(cross, vec, matt[2]);    // results in y vector
912                         if(Inpf(cross, rv3d->twmat[1]) > 0.0) phi= -phi;
913                         glRotatef(-90.0, 1.0, 0.0, 0.0);
914                         gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*phi/M_PI);
915                         glRotatef(90.0, 1.0, 0.0, 0.0);
916                 }
917
918                 postOrtho(ortho);
919         }
920
921         glDisable(GL_BLEND);
922         wmLoadMatrix(rv3d->viewmat);
923 }
924
925 static void draw_manipulator_rotate(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo)
926 {
927         GLUquadricObj *qobj;
928         double plane[4];
929         float matt[4][4];
930         float size, vec[3], unitmat[4][4];
931         float cywid= 0.33f*0.01f*(float)U.tw_handlesize;
932         float cusize= cywid*0.65f;
933         int arcs= (G.rt!=2);
934         int colcode;
935         int ortho;
936
937         if(moving) colcode= MAN_MOVECOL;
938         else colcode= MAN_RGB;
939
940         /* when called while moving in mixed mode, do not draw when... */
941         if((drawflags & MAN_ROT_C)==0) return;
942
943         /* Init stuff */
944         glDisable(GL_DEPTH_TEST);
945         Mat4One(unitmat);
946
947         qobj= gluNewQuadric();
948         gluQuadricDrawStyle(qobj, GLU_FILL);
949
950         /* prepare for screen aligned draw */
951         VECCOPY(vec, rv3d->twmat[0]);
952         size= Normalize(vec);
953         glPushMatrix();
954         glTranslatef(rv3d->twmat[3][0], rv3d->twmat[3][1], rv3d->twmat[3][2]);
955
956         if(arcs) {
957                 /* clipplane makes nice handles, calc here because of multmatrix but with translate! */
958                 VECCOPY(plane, rv3d->viewinv[2]);
959                 plane[3]= -0.02*size; // clip just a bit more
960                 glClipPlane(GL_CLIP_PLANE0, plane);
961         }
962         /* sets view screen aligned */
963         glRotatef( -360.0f*saacos(rv3d->viewquat[0])/(float)M_PI, rv3d->viewquat[1], rv3d->viewquat[2], rv3d->viewquat[3]);
964
965         /* Screen aligned help circle */
966         if(arcs) {
967                 if((G.f & G_PICKSEL)==0) {
968                         UI_ThemeColorShade(TH_BACK, -30);
969                         drawcircball(GL_LINE_LOOP, unitmat[3], size, unitmat);
970                 }
971         }
972         /* Screen aligned view rot circle */
973         if(drawflags & MAN_ROT_V) {
974                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
975                 UI_ThemeColor(TH_TRANSFORM);
976                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
977
978                 if(moving) {
979                         float vec[3];
980                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
981                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
982                         vec[2]= 0.0f;
983                         Normalize(vec);
984                         VecMulf(vec, 1.2f*size);
985                         glBegin(GL_LINES);
986                         glVertex3f(0.0f, 0.0f, 0.0f);
987                         glVertex3fv(vec);
988                         glEnd();
989                 }
990         }
991         glPopMatrix();
992
993
994         ortho = IsMat4Orthogonal(rv3d->twmat);
995         
996         /* apply the transform delta */
997         if(moving) {
998                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
999                 // XXX Mat4MulMat34(matt, t->mat, rv3d->twmat);
1000                 if (ortho) {
1001                         wmMultMatrix(matt);
1002                         glFrontFace( is_mat4_flipped(matt)?GL_CW:GL_CCW);
1003                 }
1004         }
1005         else {
1006                 if (ortho) {
1007                         glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1008                         wmMultMatrix(rv3d->twmat);
1009                 }
1010         }
1011
1012         /* axes */
1013         if(arcs==0) {
1014                 if(!(G.f & G_PICKSEL)) {
1015                         if( (combo & V3D_MANIP_SCALE)==0) {
1016                                 /* axis */
1017                                 if( (drawflags & MAN_ROT_X) || (moving && (drawflags & MAN_ROT_Z)) ) {
1018                                         preOrthoFront(ortho, rv3d->twmat, 2);
1019                                         manipulator_setcolor(v3d, 'x', colcode);
1020                                         glBegin(GL_LINES);
1021                                         glVertex3f(0.2f, 0.0f, 0.0f);
1022                                         glVertex3f(1.0f, 0.0f, 0.0f);
1023                                         glEnd();
1024                                         postOrtho(ortho);
1025                                 }
1026                                 if( (drawflags & MAN_ROT_Y) || (moving && (drawflags & MAN_ROT_X)) ) {
1027                                         preOrthoFront(ortho, rv3d->twmat, 0);
1028                                         manipulator_setcolor(v3d, 'y', colcode);
1029                                         glBegin(GL_LINES);
1030                                         glVertex3f(0.0f, 0.2f, 0.0f);
1031                                         glVertex3f(0.0f, 1.0f, 0.0f);
1032                                         glEnd();
1033                                         postOrtho(ortho);
1034                                 }
1035                                 if( (drawflags & MAN_ROT_Z) || (moving && (drawflags & MAN_ROT_Y)) ) {
1036                                         preOrthoFront(ortho, rv3d->twmat, 1);
1037                                         manipulator_setcolor(v3d, 'z', colcode);
1038                                         glBegin(GL_LINES);
1039                                         glVertex3f(0.0f, 0.0f, 0.2f);
1040                                         glVertex3f(0.0f, 0.0f, 1.0f);
1041                                         glEnd();
1042                                         postOrtho(ortho);
1043                                 }
1044                         }
1045                 }
1046         }
1047
1048         if(arcs==0 && moving) {
1049
1050                 /* Z circle */
1051                 if(drawflags & MAN_ROT_Z) {
1052                         preOrthoFront(ortho, matt, 2);
1053                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1054                         manipulator_setcolor(v3d, 'z', colcode);
1055                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1056                         postOrtho(ortho);
1057                 }
1058                 /* X circle */
1059                 if(drawflags & MAN_ROT_X) {
1060                         preOrthoFront(ortho, matt, 0);
1061                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1062                         glRotatef(90.0, 0.0, 1.0, 0.0);
1063                         manipulator_setcolor(v3d, 'x', colcode);
1064                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1065                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1066                         postOrtho(ortho);
1067                 }
1068                 /* Y circle */
1069                 if(drawflags & MAN_ROT_Y) {
1070                         preOrthoFront(ortho, matt, 1);
1071                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1072                         glRotatef(-90.0, 1.0, 0.0, 0.0);
1073                         manipulator_setcolor(v3d, 'y', colcode);
1074                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1075                         glRotatef(90.0, 1.0, 0.0, 0.0);
1076                         postOrtho(ortho);
1077                 }
1078
1079                 if(arcs) glDisable(GL_CLIP_PLANE0);
1080         }
1081         // donut arcs
1082         if(arcs) {
1083                 glEnable(GL_CLIP_PLANE0);
1084
1085                 /* Z circle */
1086                 if(drawflags & MAN_ROT_Z) {
1087                         preOrthoFront(ortho, rv3d->twmat, 2);
1088                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1089                         manipulator_setcolor(v3d, 'z', colcode);
1090                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1091                         postOrtho(ortho);
1092                 }
1093                 /* X circle */
1094                 if(drawflags & MAN_ROT_X) {
1095                         preOrthoFront(ortho, rv3d->twmat, 0);
1096                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1097                         glRotatef(90.0, 0.0, 1.0, 0.0);
1098                         manipulator_setcolor(v3d, 'x', colcode);
1099                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1100                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1101                         postOrtho(ortho);
1102                 }
1103                 /* Y circle */
1104                 if(drawflags & MAN_ROT_Y) {
1105                         preOrthoFront(ortho, rv3d->twmat, 1);
1106                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1107                         glRotatef(-90.0, 1.0, 0.0, 0.0);
1108                         manipulator_setcolor(v3d, 'y', colcode);
1109                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1110                         glRotatef(90.0, 1.0, 0.0, 0.0);
1111                         postOrtho(ortho);
1112                 }
1113
1114                 glDisable(GL_CLIP_PLANE0);
1115         }
1116
1117         if(arcs==0) {
1118
1119                 /* Z handle on X axis */
1120                 if(drawflags & MAN_ROT_Z) {
1121                         preOrthoFront(ortho, rv3d->twmat, 2);
1122                         glPushMatrix();
1123                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1124                         manipulator_setcolor(v3d, 'z', colcode);
1125
1126                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1127
1128                         glPopMatrix();
1129                         postOrtho(ortho);
1130                 }
1131
1132                 /* Y handle on X axis */
1133                 if(drawflags & MAN_ROT_Y) {
1134                         preOrthoFront(ortho, rv3d->twmat, 1);
1135                         glPushMatrix();
1136                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1137                         manipulator_setcolor(v3d, 'y', colcode);
1138
1139                         glRotatef(90.0, 1.0, 0.0, 0.0);
1140                         glRotatef(90.0, 0.0, 0.0, 1.0);
1141                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1142
1143                         glPopMatrix();
1144                         postOrtho(ortho);
1145                 }
1146
1147                 /* X handle on Z axis */
1148                 if(drawflags & MAN_ROT_X) {
1149                         preOrthoFront(ortho, rv3d->twmat, 0);
1150                         glPushMatrix();
1151                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1152                         manipulator_setcolor(v3d, 'x', colcode);
1153
1154                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1155                         glRotatef(90.0, 0.0, 0.0, 1.0);
1156                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1157
1158                         glPopMatrix();
1159                         postOrtho(ortho);
1160                 }
1161
1162         }
1163
1164         /* restore */
1165         wmLoadMatrix(rv3d->viewmat);
1166         gluDeleteQuadric(qobj);
1167         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1168
1169 }
1170
1171 static void drawsolidcube(float size)
1172 {
1173         static float cube[8][3] = {
1174         {-1.0, -1.0, -1.0},
1175         {-1.0, -1.0,  1.0},
1176         {-1.0,  1.0,  1.0},
1177         {-1.0,  1.0, -1.0},
1178         { 1.0, -1.0, -1.0},
1179         { 1.0, -1.0,  1.0},
1180         { 1.0,  1.0,  1.0},
1181         { 1.0,  1.0, -1.0},     };
1182         float n[3];
1183
1184         glPushMatrix();
1185         glScalef(size, size, size);
1186
1187         n[0]=0; n[1]=0; n[2]=0;
1188         glBegin(GL_QUADS);
1189         n[0]= -1.0;
1190         glNormal3fv(n);
1191         glVertex3fv(cube[0]); glVertex3fv(cube[1]); glVertex3fv(cube[2]); glVertex3fv(cube[3]);
1192         n[0]=0;
1193         glEnd();
1194
1195         glBegin(GL_QUADS);
1196         n[1]= -1.0;
1197         glNormal3fv(n);
1198         glVertex3fv(cube[0]); glVertex3fv(cube[4]); glVertex3fv(cube[5]); glVertex3fv(cube[1]);
1199         n[1]=0;
1200         glEnd();
1201
1202         glBegin(GL_QUADS);
1203         n[0]= 1.0;
1204         glNormal3fv(n);
1205         glVertex3fv(cube[4]); glVertex3fv(cube[7]); glVertex3fv(cube[6]); glVertex3fv(cube[5]);
1206         n[0]=0;
1207         glEnd();
1208
1209         glBegin(GL_QUADS);
1210         n[1]= 1.0;
1211         glNormal3fv(n);
1212         glVertex3fv(cube[7]); glVertex3fv(cube[3]); glVertex3fv(cube[2]); glVertex3fv(cube[6]);
1213         n[1]=0;
1214         glEnd();
1215
1216         glBegin(GL_QUADS);
1217         n[2]= 1.0;
1218         glNormal3fv(n);
1219         glVertex3fv(cube[1]); glVertex3fv(cube[5]); glVertex3fv(cube[6]); glVertex3fv(cube[2]);
1220         n[2]=0;
1221         glEnd();
1222
1223         glBegin(GL_QUADS);
1224         n[2]= -1.0;
1225         glNormal3fv(n);
1226         glVertex3fv(cube[7]); glVertex3fv(cube[4]); glVertex3fv(cube[0]); glVertex3fv(cube[3]);
1227         glEnd();
1228
1229         glPopMatrix();
1230 }
1231
1232
1233 static void draw_manipulator_scale(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1234 {
1235         float cywid= 0.25f*0.01f*(float)U.tw_handlesize;
1236         float cusize= cywid*0.75f, dz;
1237
1238         /* when called while moving in mixed mode, do not draw when... */
1239         if((drawflags & MAN_SCALE_C)==0) return;
1240
1241         glDisable(GL_DEPTH_TEST);
1242
1243         /* not in combo mode */
1244         if( (combo & (V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE))==0) {
1245                 float size, unitmat[4][4];
1246                 int shift= 0; // XXX
1247
1248                 /* center circle, do not add to selection when shift is pressed (planar constraint)  */
1249                 if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_SCALE_C);
1250
1251                 manipulator_setcolor(v3d, 'c', colcode);
1252                 glPushMatrix();
1253                 size= screen_aligned(rv3d, rv3d->twmat);
1254                 Mat4One(unitmat);
1255                 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1256                 glPopMatrix();
1257
1258                 dz= 1.0;
1259         }
1260         else dz= 1.0f-4.0f*cusize;
1261
1262         if(moving) {
1263                 float matt[4][4];
1264
1265                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1266                 // XXX Mat4MulMat34(matt, t->mat, rv3d->twmat);
1267                 wmMultMatrix(matt);
1268                 glFrontFace( is_mat4_flipped(matt)?GL_CW:GL_CCW);
1269         }
1270         else {
1271                 wmMultMatrix(rv3d->twmat);
1272                 glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1273         }
1274
1275         /* axis */
1276
1277         /* in combo mode, this is always drawn as first type */
1278         draw_manipulator_axes(v3d, colcode, drawflags & MAN_SCALE_X, drawflags & MAN_SCALE_Y, drawflags & MAN_SCALE_Z);
1279
1280         /* Z cube */
1281         glTranslatef(0.0, 0.0, dz);
1282         if(drawflags & MAN_SCALE_Z) {
1283                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Z);
1284                 manipulator_setcolor(v3d, 'z', colcode);
1285                 drawsolidcube(cusize);
1286         }
1287         /* X cube */
1288         glTranslatef(dz, 0.0, -dz);
1289         if(drawflags & MAN_SCALE_X) {
1290                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_X);
1291                 manipulator_setcolor(v3d, 'x', colcode);
1292                 drawsolidcube(cusize);
1293         }
1294         /* Y cube */
1295         glTranslatef(-dz, dz, 0.0);
1296         if(drawflags & MAN_SCALE_Y) {
1297                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Y);
1298                 manipulator_setcolor(v3d, 'y', colcode);
1299                 drawsolidcube(cusize);
1300         }
1301
1302         /* if shiftkey, center point as last, for selectbuffer order */
1303         if(G.f & G_PICKSEL) {
1304                 int shift= 0; // XXX
1305
1306                 if(shift) {
1307                         glTranslatef(0.0, -dz, 0.0);
1308                         glLoadName(MAN_SCALE_C);
1309                         glBegin(GL_POINTS);
1310                         glVertex3f(0.0, 0.0, 0.0);
1311                         glEnd();
1312                 }
1313         }
1314
1315         /* restore */
1316         wmLoadMatrix(rv3d->viewmat);
1317
1318         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1319         glFrontFace(GL_CCW);
1320 }
1321
1322
1323 static void draw_cone(GLUquadricObj *qobj, float len, float width)
1324 {
1325         glTranslatef(0.0, 0.0, -0.5f*len);
1326         gluCylinder(qobj, width, 0.0, len, 8, 1);
1327         gluQuadricOrientation(qobj, GLU_INSIDE);
1328         gluDisk(qobj, 0.0, width, 8, 1);
1329         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1330         glTranslatef(0.0, 0.0, 0.5f*len);
1331 }
1332
1333 static void draw_cylinder(GLUquadricObj *qobj, float len, float width)
1334 {
1335
1336         width*= 0.8f;   // just for beauty
1337
1338         glTranslatef(0.0, 0.0, -0.5f*len);
1339         gluCylinder(qobj, width, width, len, 8, 1);
1340         gluQuadricOrientation(qobj, GLU_INSIDE);
1341         gluDisk(qobj, 0.0, width, 8, 1);
1342         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1343         glTranslatef(0.0, 0.0, len);
1344         gluDisk(qobj, 0.0, width, 8, 1);
1345         glTranslatef(0.0, 0.0, -0.5f*len);
1346 }
1347
1348
1349 static void draw_manipulator_translate(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1350 {
1351         GLUquadricObj *qobj;
1352         float cylen= 0.01f*(float)U.tw_handlesize;
1353         float cywid= 0.25f*cylen, dz, size;
1354         float unitmat[4][4];
1355         int shift= 0; // XXX
1356
1357         /* when called while moving in mixed mode, do not draw when... */
1358         if((drawflags & MAN_TRANS_C)==0) return;
1359
1360         // XXX if(moving) glTranslatef(t->vec[0], t->vec[1], t->vec[2]);
1361         glDisable(GL_DEPTH_TEST);
1362
1363         qobj= gluNewQuadric();
1364         gluQuadricDrawStyle(qobj, GLU_FILL);
1365
1366         /* center circle, do not add to selection when shift is pressed (planar constraint) */
1367         if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_TRANS_C);
1368
1369         manipulator_setcolor(v3d, 'c', colcode);
1370         glPushMatrix();
1371         size= screen_aligned(rv3d, rv3d->twmat);
1372         Mat4One(unitmat);
1373         drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1374         glPopMatrix();
1375
1376         /* and now apply matrix, we move to local matrix drawing */
1377         wmMultMatrix(rv3d->twmat);
1378
1379         /* axis */
1380         glLoadName(-1);
1381
1382         // translate drawn as last, only axis when no combo with scale, or for ghosting
1383         if((combo & V3D_MANIP_SCALE)==0 || colcode==MAN_GHOST)
1384                 draw_manipulator_axes(v3d, colcode, drawflags & MAN_TRANS_X, drawflags & MAN_TRANS_Y, drawflags & MAN_TRANS_Z);
1385
1386
1387         /* offset in combo mode, for rotate a bit more */
1388         if(combo & (V3D_MANIP_ROTATE)) dz= 1.0f+2.0f*cylen;
1389         else if(combo & (V3D_MANIP_SCALE)) dz= 1.0f+0.5f*cylen;
1390         else dz= 1.0f;
1391
1392         /* Z Cone */
1393         glTranslatef(0.0, 0.0, dz);
1394         if(drawflags & MAN_TRANS_Z) {
1395                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Z);
1396                 manipulator_setcolor(v3d, 'z', colcode);
1397                 draw_cone(qobj, cylen, cywid);
1398         }
1399         /* X Cone */
1400         glTranslatef(dz, 0.0, -dz);
1401         if(drawflags & MAN_TRANS_X) {
1402                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_X);
1403                 glRotatef(90.0, 0.0, 1.0, 0.0);
1404                 manipulator_setcolor(v3d, 'x', colcode);
1405                 draw_cone(qobj, cylen, cywid);
1406                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1407         }
1408         /* Y Cone */
1409         glTranslatef(-dz, dz, 0.0);
1410         if(drawflags & MAN_TRANS_Y) {
1411                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Y);
1412                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1413                 manipulator_setcolor(v3d, 'y', colcode);
1414                 draw_cone(qobj, cylen, cywid);
1415         }
1416
1417         gluDeleteQuadric(qobj);
1418         wmLoadMatrix(rv3d->viewmat);
1419
1420         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1421
1422 }
1423
1424 static void draw_manipulator_rotate_cyl(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1425 {
1426         GLUquadricObj *qobj;
1427         float size;
1428         float cylen= 0.01f*(float)U.tw_handlesize;
1429         float cywid= 0.25f*cylen;
1430
1431         /* when called while moving in mixed mode, do not draw when... */
1432         if((drawflags & MAN_ROT_C)==0) return;
1433
1434         /* prepare for screen aligned draw */
1435         glPushMatrix();
1436         size= screen_aligned(rv3d, rv3d->twmat);
1437
1438         glDisable(GL_DEPTH_TEST);
1439
1440         qobj= gluNewQuadric();
1441
1442         /* Screen aligned view rot circle */
1443         if(drawflags & MAN_ROT_V) {
1444                 float unitmat[4][4];
1445                 Mat4One(unitmat);
1446
1447                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
1448                 UI_ThemeColor(TH_TRANSFORM);
1449                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
1450
1451                 if(moving) {
1452                         float vec[3];
1453                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
1454                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
1455                         vec[2]= 0.0f;
1456                         Normalize(vec);
1457                         VecMulf(vec, 1.2f*size);
1458                         glBegin(GL_LINES);
1459                         glVertex3f(0.0, 0.0, 0.0);
1460                         glVertex3fv(vec);
1461                         glEnd();
1462                 }
1463         }
1464         glPopMatrix();
1465
1466         /* apply the transform delta */
1467         if(moving) {
1468                 float matt[4][4];
1469                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1470                 // XXX          if (t->flag & T_USES_MANIPULATOR) {
1471                 // XXX                  Mat4MulMat34(matt, t->mat, rv3d->twmat);
1472                 // XXX }
1473                 wmMultMatrix(matt);
1474         }
1475         else {
1476                 wmMultMatrix(rv3d->twmat);
1477         }
1478
1479         glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1480
1481         /* axis */
1482         if( (G.f & G_PICKSEL)==0 ) {
1483
1484                 // only draw axis when combo didn't draw scale axes
1485                 if((combo & V3D_MANIP_SCALE)==0)
1486                         draw_manipulator_axes(v3d, colcode, drawflags & MAN_ROT_X, drawflags & MAN_ROT_Y, drawflags & MAN_ROT_Z);
1487
1488                 /* only has to be set when not in picking */
1489                 gluQuadricDrawStyle(qobj, GLU_FILL);
1490         }
1491
1492         /* Z cyl */
1493         glTranslatef(0.0, 0.0, 1.0);
1494         if(drawflags & MAN_ROT_Z) {
1495                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1496                 manipulator_setcolor(v3d, 'z', colcode);
1497                 draw_cylinder(qobj, cylen, cywid);
1498         }
1499         /* X cyl */
1500         glTranslatef(1.0, 0.0, -1.0);
1501         if(drawflags & MAN_ROT_X) {
1502                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1503                 glRotatef(90.0, 0.0, 1.0, 0.0);
1504                 manipulator_setcolor(v3d, 'x', colcode);
1505                 draw_cylinder(qobj, cylen, cywid);
1506                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1507         }
1508         /* Y cylinder */
1509         glTranslatef(-1.0, 1.0, 0.0);
1510         if(drawflags & MAN_ROT_Y) {
1511                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1512                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1513                 manipulator_setcolor(v3d, 'y', colcode);
1514                 draw_cylinder(qobj, cylen, cywid);
1515         }
1516
1517         /* restore */
1518
1519         gluDeleteQuadric(qobj);
1520         wmLoadMatrix(rv3d->viewmat);
1521
1522         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1523
1524 }
1525
1526
1527 /* ********************************************* */
1528
1529 static float get_manipulator_drawsize(ARegion *ar)
1530 {
1531         RegionView3D *rv3d= ar->regiondata;
1532         float size = get_drawsize(ar, rv3d->twmat[3]);
1533
1534         size*= (float)U.tw_size;
1535
1536         return size;
1537 }
1538
1539
1540 /* main call, does calc centers & orientation too */
1541 /* uses global G.moving */
1542 static int drawflags= 0xFFFF;           // only for the calls below, belongs in scene...?
1543
1544 void BIF_draw_manipulator(const bContext *C)
1545 {
1546         ScrArea *sa= CTX_wm_area(C);
1547         ARegion *ar= CTX_wm_region(C);
1548         Scene *scene= CTX_data_scene(C);
1549         View3D *v3d= sa->spacedata.first;
1550         RegionView3D *rv3d= ar->regiondata;
1551         int totsel;
1552
1553         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return;
1554 //      if(G.moving && (G.moving & G_TRANSFORM_MANIP)==0) return;
1555
1556 //      if(G.moving==0) {
1557         {
1558                 v3d->twflag &= ~V3D_DRAW_MANIPULATOR;
1559
1560                 totsel= calc_manipulator_stats(C);
1561                 if(totsel==0) return;
1562                 drawflags= v3d->twdrawflag;     /* set in calc_manipulator_stats */
1563
1564                 v3d->twflag |= V3D_DRAW_MANIPULATOR;
1565
1566                 /* now we can define center */
1567                 switch(v3d->around) {
1568                 case V3D_CENTER:
1569                 case V3D_ACTIVE:
1570                         rv3d->twmat[3][0]= (scene->twmin[0] + scene->twmax[0])/2.0f;
1571                         rv3d->twmat[3][1]= (scene->twmin[1] + scene->twmax[1])/2.0f;
1572                         rv3d->twmat[3][2]= (scene->twmin[2] + scene->twmax[2])/2.0f;
1573                         if(v3d->around==V3D_ACTIVE && scene->obedit==NULL) {
1574                                 Object *ob= OBACT;
1575                                 if(ob && !(ob->mode & OB_MODE_POSE))
1576                                         VECCOPY(rv3d->twmat[3], ob->obmat[3]);
1577                         }
1578                         break;
1579                 case V3D_LOCAL:
1580                 case V3D_CENTROID:
1581                         VECCOPY(rv3d->twmat[3], scene->twcent);
1582                         break;
1583                 case V3D_CURSOR:
1584                         VECCOPY(rv3d->twmat[3], give_cursor(scene, v3d));
1585                         break;
1586                 }
1587
1588                 Mat4MulFloat3((float *)rv3d->twmat, get_manipulator_drawsize(ar));
1589         }
1590
1591         if(v3d->twflag & V3D_DRAW_MANIPULATOR) {
1592
1593                 if(v3d->twtype & V3D_MANIP_ROTATE) {
1594
1595                         /* rotate has special ghosting draw, for pie chart */
1596                         if(G.moving) draw_manipulator_rotate_ghost(v3d, rv3d, drawflags);
1597
1598                         if(G.moving) glEnable(GL_BLEND);
1599
1600                         if(G.rt==3) {
1601                                 if(G.moving) draw_manipulator_rotate_cyl(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1602                                 else draw_manipulator_rotate_cyl(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1603                         }
1604                         else
1605                                 draw_manipulator_rotate(v3d, rv3d, 0 /* G.moving*/, drawflags, v3d->twtype);
1606
1607                         glDisable(GL_BLEND);
1608                 }
1609                 if(v3d->twtype & V3D_MANIP_SCALE) {
1610                         if(G.moving) {
1611                                 glEnable(GL_BLEND);
1612                                 draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_GHOST);
1613                                 draw_manipulator_scale(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1614                                 glDisable(GL_BLEND);
1615                         }
1616                         else draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1617                 }
1618                 if(v3d->twtype & V3D_MANIP_TRANSLATE) {
1619                         if(G.moving) {
1620                                 glEnable(GL_BLEND);
1621                                 draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_GHOST);
1622                                 draw_manipulator_translate(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1623                                 glDisable(GL_BLEND);
1624                         }
1625                         else draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1626                 }
1627         }
1628 }
1629
1630 static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, short *mval, float hotspot)
1631 {
1632         View3D *v3d= sa->spacedata.first;
1633         RegionView3D *rv3d= ar->regiondata;
1634         rctf rect;
1635         GLuint buffer[64];              // max 4 items per select, so large enuf
1636         short hits;
1637         extern void setwinmatrixview3d(ARegion *ar, View3D *v3d, rctf *rect); // XXX check a bit later on this... (ton)
1638
1639         G.f |= G_PICKSEL;
1640
1641         rect.xmin= mval[0]-hotspot;
1642         rect.xmax= mval[0]+hotspot;
1643         rect.ymin= mval[1]-hotspot;
1644         rect.ymax= mval[1]+hotspot;
1645
1646         setwinmatrixview3d(ar, v3d, &rect);
1647         Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1648
1649         glSelectBuffer( 64, buffer);
1650         glRenderMode(GL_SELECT);
1651         glInitNames();  /* these two calls whatfor? It doesnt work otherwise */
1652         glPushName(-2);
1653
1654         /* do the drawing */
1655         if(v3d->twtype & V3D_MANIP_ROTATE) {
1656                 if(G.rt==3) draw_manipulator_rotate_cyl(v3d, rv3d, 0, MAN_ROT_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1657                 else draw_manipulator_rotate(v3d, rv3d, 0, MAN_ROT_C & v3d->twdrawflag, v3d->twtype);
1658         }
1659         if(v3d->twtype & V3D_MANIP_SCALE)
1660                 draw_manipulator_scale(v3d, rv3d, 0, MAN_SCALE_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1661         if(v3d->twtype & V3D_MANIP_TRANSLATE)
1662                 draw_manipulator_translate(v3d, rv3d, 0, MAN_TRANS_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1663
1664         glPopName();
1665         hits= glRenderMode(GL_RENDER);
1666
1667         G.f &= ~G_PICKSEL;
1668         setwinmatrixview3d(ar, v3d, NULL);
1669         Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1670
1671         if(hits==1) return buffer[3];
1672         else if(hits>1) {
1673                 GLuint val, dep, mindep=0, mindeprot=0, minval=0, minvalrot=0;
1674                 int a;
1675
1676                 /* we compare the hits in buffer, but value centers highest */
1677                 /* we also store the rotation hits separate (because of arcs) and return hits on other widgets if there are */
1678
1679                 for(a=0; a<hits; a++) {
1680                         dep= buffer[4*a + 1];
1681                         val= buffer[4*a + 3];
1682
1683                         if(val==MAN_TRANS_C) return MAN_TRANS_C;
1684                         else if(val==MAN_SCALE_C) return MAN_SCALE_C;
1685                         else {
1686                                 if(val & MAN_ROT_C) {
1687                                         if(minvalrot==0 || dep<mindeprot) {
1688                                                 mindeprot= dep;
1689                                                 minvalrot= val;
1690                                         }
1691                                 }
1692                                 else {
1693                                         if(minval==0 || dep<mindep) {
1694                                                 mindep= dep;
1695                                                 minval= val;
1696                                         }
1697                                 }
1698                         }
1699                 }
1700
1701                 if(minval)
1702                         return minval;
1703                 else
1704                         return minvalrot;
1705         }
1706         return 0;
1707 }
1708
1709 /* return 0; nothing happened */
1710 int BIF_do_manipulator(bContext *C, struct wmEvent *event, wmOperator *op)
1711 {
1712         ScrArea *sa= CTX_wm_area(C);
1713         View3D *v3d= sa->spacedata.first;
1714         ARegion *ar= CTX_wm_region(C);
1715         int constraint_axis[3] = {0, 0, 0};
1716         int val;
1717         int shift = event->shift;
1718
1719         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return 0;
1720         if(!(v3d->twflag & V3D_DRAW_MANIPULATOR)) return 0;
1721
1722         // find the hotspots first test narrow hotspot
1723         val= manipulator_selectbuf(sa, ar, event->mval, 0.5f*(float)U.tw_hotspot);
1724         if(val) {
1725
1726                 // drawflags still global, for drawing call above
1727                 drawflags= manipulator_selectbuf(sa, ar, event->mval, 0.2f*(float)U.tw_hotspot);
1728                 if(drawflags==0) drawflags= val;
1729
1730                 if (drawflags & MAN_TRANS_C) {
1731                         switch(drawflags) {
1732                         case MAN_TRANS_C:
1733                                 break;
1734                         case MAN_TRANS_X:
1735                                 if(shift) {
1736                                         constraint_axis[1] = 1;
1737                                         constraint_axis[2] = 1;
1738                                 }
1739                                 else
1740                                         constraint_axis[0] = 1;
1741                                 break;
1742                         case MAN_TRANS_Y:
1743                                 if(shift) {
1744                                         constraint_axis[0] = 1;
1745                                         constraint_axis[2] = 1;
1746                                 }
1747                                 else
1748                                         constraint_axis[1] = 1;
1749                                 break;
1750                         case MAN_TRANS_Z:
1751                                 if(shift) {
1752                                         constraint_axis[0] = 1;
1753                                         constraint_axis[1] = 1;
1754                                 }
1755                                 else
1756                                         constraint_axis[2] = 1;
1757                                 break;
1758                         }
1759                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1760                         WM_operator_name_call(C, "TFM_OT_translate", WM_OP_INVOKE_DEFAULT, op->ptr);
1761                 }
1762                 else if (drawflags & MAN_SCALE_C) {
1763                         switch(drawflags) {
1764                         case MAN_SCALE_X:
1765                                 if(shift) {
1766                                         constraint_axis[1] = 1;
1767                                         constraint_axis[2] = 1;
1768                                 }
1769                                 else
1770                                         constraint_axis[0] = 1;
1771                                 break;
1772                         case MAN_SCALE_Y:
1773                                 if(shift) {
1774                                         constraint_axis[0] = 1;
1775                                         constraint_axis[2] = 1;
1776                                 }
1777                                 else
1778                                         constraint_axis[1] = 1;
1779                                 break;
1780                         case MAN_SCALE_Z:
1781                                 if(shift) {
1782                                         constraint_axis[0] = 1;
1783                                         constraint_axis[1] = 1;
1784                                 }
1785                                 else
1786                                         constraint_axis[2] = 1;
1787                                 break;
1788                         }
1789                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1790                         WM_operator_name_call(C, "TFM_OT_resize", WM_OP_INVOKE_DEFAULT, op->ptr);
1791                 }
1792                 else if (drawflags == MAN_ROT_T) { /* trackball need special case, init is different */
1793                         WM_operator_name_call(C, "TFM_OT_trackball", WM_OP_INVOKE_DEFAULT, op->ptr);
1794                 }
1795                 else if (drawflags & MAN_ROT_C) {
1796                         switch(drawflags) {
1797                         case MAN_ROT_X:
1798                                 constraint_axis[0] = 1;
1799                                 break;
1800                         case MAN_ROT_Y:
1801                                 constraint_axis[1] = 1;
1802                                 break;
1803                         case MAN_ROT_Z:
1804                                 constraint_axis[2] = 1;
1805                                 break;
1806                         }
1807                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1808                         WM_operator_name_call(C, "TFM_OT_rotate", WM_OP_INVOKE_DEFAULT, op->ptr);
1809                 }
1810         }
1811         /* after transform, restore drawflags */
1812         drawflags= 0xFFFF;
1813
1814         return val;
1815 }
1816