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