Patch [#19799] Add trackball loop to rotate manipulator
[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
990         /* Screen aligned trackball rot circle */
991         if(drawflags & MAN_ROT_T) {
992                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_T);
993
994                 UI_ThemeColor(TH_TRANSFORM);
995                 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
996         }
997
998         /* Screen aligned view rot circle */
999         if(drawflags & MAN_ROT_V) {
1000                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
1001                 UI_ThemeColor(TH_TRANSFORM);
1002                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
1003
1004                 if(moving) {
1005                         float vec[3];
1006                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
1007                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
1008                         vec[2]= 0.0f;
1009                         Normalize(vec);
1010                         VecMulf(vec, 1.2f*size);
1011                         glBegin(GL_LINES);
1012                         glVertex3f(0.0f, 0.0f, 0.0f);
1013                         glVertex3fv(vec);
1014                         glEnd();
1015                 }
1016         }
1017         glPopMatrix();
1018
1019
1020         ortho = IsMat4Orthogonal(rv3d->twmat);
1021         
1022         /* apply the transform delta */
1023         if(moving) {
1024                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1025                 // XXX Mat4MulMat34(matt, t->mat, rv3d->twmat);
1026                 if (ortho) {
1027                         wmMultMatrix(matt);
1028                         glFrontFace( is_mat4_flipped(matt)?GL_CW:GL_CCW);
1029                 }
1030         }
1031         else {
1032                 if (ortho) {
1033                         glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1034                         wmMultMatrix(rv3d->twmat);
1035                 }
1036         }
1037
1038         /* axes */
1039         if(arcs==0) {
1040                 if(!(G.f & G_PICKSEL)) {
1041                         if( (combo & V3D_MANIP_SCALE)==0) {
1042                                 /* axis */
1043                                 if( (drawflags & MAN_ROT_X) || (moving && (drawflags & MAN_ROT_Z)) ) {
1044                                         preOrthoFront(ortho, rv3d->twmat, 2);
1045                                         manipulator_setcolor(v3d, 'x', colcode);
1046                                         glBegin(GL_LINES);
1047                                         glVertex3f(0.2f, 0.0f, 0.0f);
1048                                         glVertex3f(1.0f, 0.0f, 0.0f);
1049                                         glEnd();
1050                                         postOrtho(ortho);
1051                                 }
1052                                 if( (drawflags & MAN_ROT_Y) || (moving && (drawflags & MAN_ROT_X)) ) {
1053                                         preOrthoFront(ortho, rv3d->twmat, 0);
1054                                         manipulator_setcolor(v3d, 'y', colcode);
1055                                         glBegin(GL_LINES);
1056                                         glVertex3f(0.0f, 0.2f, 0.0f);
1057                                         glVertex3f(0.0f, 1.0f, 0.0f);
1058                                         glEnd();
1059                                         postOrtho(ortho);
1060                                 }
1061                                 if( (drawflags & MAN_ROT_Z) || (moving && (drawflags & MAN_ROT_Y)) ) {
1062                                         preOrthoFront(ortho, rv3d->twmat, 1);
1063                                         manipulator_setcolor(v3d, 'z', colcode);
1064                                         glBegin(GL_LINES);
1065                                         glVertex3f(0.0f, 0.0f, 0.2f);
1066                                         glVertex3f(0.0f, 0.0f, 1.0f);
1067                                         glEnd();
1068                                         postOrtho(ortho);
1069                                 }
1070                         }
1071                 }
1072         }
1073
1074         if(arcs==0 && moving) {
1075
1076                 /* Z circle */
1077                 if(drawflags & MAN_ROT_Z) {
1078                         preOrthoFront(ortho, matt, 2);
1079                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1080                         manipulator_setcolor(v3d, 'z', colcode);
1081                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1082                         postOrtho(ortho);
1083                 }
1084                 /* X circle */
1085                 if(drawflags & MAN_ROT_X) {
1086                         preOrthoFront(ortho, matt, 0);
1087                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1088                         glRotatef(90.0, 0.0, 1.0, 0.0);
1089                         manipulator_setcolor(v3d, 'x', colcode);
1090                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1091                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1092                         postOrtho(ortho);
1093                 }
1094                 /* Y circle */
1095                 if(drawflags & MAN_ROT_Y) {
1096                         preOrthoFront(ortho, matt, 1);
1097                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1098                         glRotatef(-90.0, 1.0, 0.0, 0.0);
1099                         manipulator_setcolor(v3d, 'y', colcode);
1100                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1101                         glRotatef(90.0, 1.0, 0.0, 0.0);
1102                         postOrtho(ortho);
1103                 }
1104
1105                 if(arcs) glDisable(GL_CLIP_PLANE0);
1106         }
1107         // donut arcs
1108         if(arcs) {
1109                 glEnable(GL_CLIP_PLANE0);
1110
1111                 /* Z circle */
1112                 if(drawflags & MAN_ROT_Z) {
1113                         preOrthoFront(ortho, rv3d->twmat, 2);
1114                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1115                         manipulator_setcolor(v3d, 'z', colcode);
1116                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1117                         postOrtho(ortho);
1118                 }
1119                 /* X circle */
1120                 if(drawflags & MAN_ROT_X) {
1121                         preOrthoFront(ortho, rv3d->twmat, 0);
1122                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1123                         glRotatef(90.0, 0.0, 1.0, 0.0);
1124                         manipulator_setcolor(v3d, 'x', colcode);
1125                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1126                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1127                         postOrtho(ortho);
1128                 }
1129                 /* Y circle */
1130                 if(drawflags & MAN_ROT_Y) {
1131                         preOrthoFront(ortho, rv3d->twmat, 1);
1132                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1133                         glRotatef(-90.0, 1.0, 0.0, 0.0);
1134                         manipulator_setcolor(v3d, 'y', colcode);
1135                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1136                         glRotatef(90.0, 1.0, 0.0, 0.0);
1137                         postOrtho(ortho);
1138                 }
1139
1140                 glDisable(GL_CLIP_PLANE0);
1141         }
1142
1143         if(arcs==0) {
1144
1145                 /* Z handle on X axis */
1146                 if(drawflags & MAN_ROT_Z) {
1147                         preOrthoFront(ortho, rv3d->twmat, 2);
1148                         glPushMatrix();
1149                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1150                         manipulator_setcolor(v3d, 'z', colcode);
1151
1152                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1153
1154                         glPopMatrix();
1155                         postOrtho(ortho);
1156                 }
1157
1158                 /* Y handle on X axis */
1159                 if(drawflags & MAN_ROT_Y) {
1160                         preOrthoFront(ortho, rv3d->twmat, 1);
1161                         glPushMatrix();
1162                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1163                         manipulator_setcolor(v3d, 'y', colcode);
1164
1165                         glRotatef(90.0, 1.0, 0.0, 0.0);
1166                         glRotatef(90.0, 0.0, 0.0, 1.0);
1167                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1168
1169                         glPopMatrix();
1170                         postOrtho(ortho);
1171                 }
1172
1173                 /* X handle on Z axis */
1174                 if(drawflags & MAN_ROT_X) {
1175                         preOrthoFront(ortho, rv3d->twmat, 0);
1176                         glPushMatrix();
1177                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1178                         manipulator_setcolor(v3d, 'x', colcode);
1179
1180                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1181                         glRotatef(90.0, 0.0, 0.0, 1.0);
1182                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1183
1184                         glPopMatrix();
1185                         postOrtho(ortho);
1186                 }
1187
1188         }
1189
1190         /* restore */
1191         wmLoadMatrix(rv3d->viewmat);
1192         gluDeleteQuadric(qobj);
1193         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1194
1195 }
1196
1197 static void drawsolidcube(float size)
1198 {
1199         static float cube[8][3] = {
1200         {-1.0, -1.0, -1.0},
1201         {-1.0, -1.0,  1.0},
1202         {-1.0,  1.0,  1.0},
1203         {-1.0,  1.0, -1.0},
1204         { 1.0, -1.0, -1.0},
1205         { 1.0, -1.0,  1.0},
1206         { 1.0,  1.0,  1.0},
1207         { 1.0,  1.0, -1.0},     };
1208         float n[3];
1209
1210         glPushMatrix();
1211         glScalef(size, size, size);
1212
1213         n[0]=0; n[1]=0; n[2]=0;
1214         glBegin(GL_QUADS);
1215         n[0]= -1.0;
1216         glNormal3fv(n);
1217         glVertex3fv(cube[0]); glVertex3fv(cube[1]); glVertex3fv(cube[2]); glVertex3fv(cube[3]);
1218         n[0]=0;
1219         glEnd();
1220
1221         glBegin(GL_QUADS);
1222         n[1]= -1.0;
1223         glNormal3fv(n);
1224         glVertex3fv(cube[0]); glVertex3fv(cube[4]); glVertex3fv(cube[5]); glVertex3fv(cube[1]);
1225         n[1]=0;
1226         glEnd();
1227
1228         glBegin(GL_QUADS);
1229         n[0]= 1.0;
1230         glNormal3fv(n);
1231         glVertex3fv(cube[4]); glVertex3fv(cube[7]); glVertex3fv(cube[6]); glVertex3fv(cube[5]);
1232         n[0]=0;
1233         glEnd();
1234
1235         glBegin(GL_QUADS);
1236         n[1]= 1.0;
1237         glNormal3fv(n);
1238         glVertex3fv(cube[7]); glVertex3fv(cube[3]); glVertex3fv(cube[2]); glVertex3fv(cube[6]);
1239         n[1]=0;
1240         glEnd();
1241
1242         glBegin(GL_QUADS);
1243         n[2]= 1.0;
1244         glNormal3fv(n);
1245         glVertex3fv(cube[1]); glVertex3fv(cube[5]); glVertex3fv(cube[6]); glVertex3fv(cube[2]);
1246         n[2]=0;
1247         glEnd();
1248
1249         glBegin(GL_QUADS);
1250         n[2]= -1.0;
1251         glNormal3fv(n);
1252         glVertex3fv(cube[7]); glVertex3fv(cube[4]); glVertex3fv(cube[0]); glVertex3fv(cube[3]);
1253         glEnd();
1254
1255         glPopMatrix();
1256 }
1257
1258
1259 static void draw_manipulator_scale(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1260 {
1261         float cywid= 0.25f*0.01f*(float)U.tw_handlesize;
1262         float cusize= cywid*0.75f, dz;
1263
1264         /* when called while moving in mixed mode, do not draw when... */
1265         if((drawflags & MAN_SCALE_C)==0) return;
1266
1267         glDisable(GL_DEPTH_TEST);
1268
1269         /* not in combo mode */
1270         if( (combo & (V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE))==0) {
1271                 float size, unitmat[4][4];
1272                 int shift= 0; // XXX
1273
1274                 /* center circle, do not add to selection when shift is pressed (planar constraint)  */
1275                 if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_SCALE_C);
1276
1277                 manipulator_setcolor(v3d, 'c', colcode);
1278                 glPushMatrix();
1279                 size= screen_aligned(rv3d, rv3d->twmat);
1280                 Mat4One(unitmat);
1281                 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1282                 glPopMatrix();
1283
1284                 dz= 1.0;
1285         }
1286         else dz= 1.0f-4.0f*cusize;
1287
1288         if(moving) {
1289                 float matt[4][4];
1290
1291                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1292                 // XXX Mat4MulMat34(matt, t->mat, rv3d->twmat);
1293                 wmMultMatrix(matt);
1294                 glFrontFace( is_mat4_flipped(matt)?GL_CW:GL_CCW);
1295         }
1296         else {
1297                 wmMultMatrix(rv3d->twmat);
1298                 glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1299         }
1300
1301         /* axis */
1302
1303         /* in combo mode, this is always drawn as first type */
1304         draw_manipulator_axes(v3d, colcode, drawflags & MAN_SCALE_X, drawflags & MAN_SCALE_Y, drawflags & MAN_SCALE_Z);
1305
1306         /* Z cube */
1307         glTranslatef(0.0, 0.0, dz);
1308         if(drawflags & MAN_SCALE_Z) {
1309                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Z);
1310                 manipulator_setcolor(v3d, 'z', colcode);
1311                 drawsolidcube(cusize);
1312         }
1313         /* X cube */
1314         glTranslatef(dz, 0.0, -dz);
1315         if(drawflags & MAN_SCALE_X) {
1316                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_X);
1317                 manipulator_setcolor(v3d, 'x', colcode);
1318                 drawsolidcube(cusize);
1319         }
1320         /* Y cube */
1321         glTranslatef(-dz, dz, 0.0);
1322         if(drawflags & MAN_SCALE_Y) {
1323                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Y);
1324                 manipulator_setcolor(v3d, 'y', colcode);
1325                 drawsolidcube(cusize);
1326         }
1327
1328         /* if shiftkey, center point as last, for selectbuffer order */
1329         if(G.f & G_PICKSEL) {
1330                 int shift= 0; // XXX
1331
1332                 if(shift) {
1333                         glTranslatef(0.0, -dz, 0.0);
1334                         glLoadName(MAN_SCALE_C);
1335                         glBegin(GL_POINTS);
1336                         glVertex3f(0.0, 0.0, 0.0);
1337                         glEnd();
1338                 }
1339         }
1340
1341         /* restore */
1342         wmLoadMatrix(rv3d->viewmat);
1343
1344         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1345         glFrontFace(GL_CCW);
1346 }
1347
1348
1349 static void draw_cone(GLUquadricObj *qobj, float len, float width)
1350 {
1351         glTranslatef(0.0, 0.0, -0.5f*len);
1352         gluCylinder(qobj, width, 0.0, len, 8, 1);
1353         gluQuadricOrientation(qobj, GLU_INSIDE);
1354         gluDisk(qobj, 0.0, width, 8, 1);
1355         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1356         glTranslatef(0.0, 0.0, 0.5f*len);
1357 }
1358
1359 static void draw_cylinder(GLUquadricObj *qobj, float len, float width)
1360 {
1361
1362         width*= 0.8f;   // just for beauty
1363
1364         glTranslatef(0.0, 0.0, -0.5f*len);
1365         gluCylinder(qobj, width, width, len, 8, 1);
1366         gluQuadricOrientation(qobj, GLU_INSIDE);
1367         gluDisk(qobj, 0.0, width, 8, 1);
1368         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1369         glTranslatef(0.0, 0.0, len);
1370         gluDisk(qobj, 0.0, width, 8, 1);
1371         glTranslatef(0.0, 0.0, -0.5f*len);
1372 }
1373
1374
1375 static void draw_manipulator_translate(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1376 {
1377         GLUquadricObj *qobj;
1378         float cylen= 0.01f*(float)U.tw_handlesize;
1379         float cywid= 0.25f*cylen, dz, size;
1380         float unitmat[4][4];
1381         int shift= 0; // XXX
1382
1383         /* when called while moving in mixed mode, do not draw when... */
1384         if((drawflags & MAN_TRANS_C)==0) return;
1385
1386         // XXX if(moving) glTranslatef(t->vec[0], t->vec[1], t->vec[2]);
1387         glDisable(GL_DEPTH_TEST);
1388
1389         qobj= gluNewQuadric();
1390         gluQuadricDrawStyle(qobj, GLU_FILL);
1391
1392         /* center circle, do not add to selection when shift is pressed (planar constraint) */
1393         if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_TRANS_C);
1394
1395         manipulator_setcolor(v3d, 'c', colcode);
1396         glPushMatrix();
1397         size= screen_aligned(rv3d, rv3d->twmat);
1398         Mat4One(unitmat);
1399         drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1400         glPopMatrix();
1401
1402         /* and now apply matrix, we move to local matrix drawing */
1403         wmMultMatrix(rv3d->twmat);
1404
1405         /* axis */
1406         glLoadName(-1);
1407
1408         // translate drawn as last, only axis when no combo with scale, or for ghosting
1409         if((combo & V3D_MANIP_SCALE)==0 || colcode==MAN_GHOST)
1410                 draw_manipulator_axes(v3d, colcode, drawflags & MAN_TRANS_X, drawflags & MAN_TRANS_Y, drawflags & MAN_TRANS_Z);
1411
1412
1413         /* offset in combo mode, for rotate a bit more */
1414         if(combo & (V3D_MANIP_ROTATE)) dz= 1.0f+2.0f*cylen;
1415         else if(combo & (V3D_MANIP_SCALE)) dz= 1.0f+0.5f*cylen;
1416         else dz= 1.0f;
1417
1418         /* Z Cone */
1419         glTranslatef(0.0, 0.0, dz);
1420         if(drawflags & MAN_TRANS_Z) {
1421                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Z);
1422                 manipulator_setcolor(v3d, 'z', colcode);
1423                 draw_cone(qobj, cylen, cywid);
1424         }
1425         /* X Cone */
1426         glTranslatef(dz, 0.0, -dz);
1427         if(drawflags & MAN_TRANS_X) {
1428                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_X);
1429                 glRotatef(90.0, 0.0, 1.0, 0.0);
1430                 manipulator_setcolor(v3d, 'x', colcode);
1431                 draw_cone(qobj, cylen, cywid);
1432                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1433         }
1434         /* Y Cone */
1435         glTranslatef(-dz, dz, 0.0);
1436         if(drawflags & MAN_TRANS_Y) {
1437                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Y);
1438                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1439                 manipulator_setcolor(v3d, 'y', colcode);
1440                 draw_cone(qobj, cylen, cywid);
1441         }
1442
1443         gluDeleteQuadric(qobj);
1444         wmLoadMatrix(rv3d->viewmat);
1445
1446         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1447
1448 }
1449
1450 static void draw_manipulator_rotate_cyl(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1451 {
1452         GLUquadricObj *qobj;
1453         float size;
1454         float cylen= 0.01f*(float)U.tw_handlesize;
1455         float cywid= 0.25f*cylen;
1456
1457         /* when called while moving in mixed mode, do not draw when... */
1458         if((drawflags & MAN_ROT_C)==0) return;
1459
1460         /* prepare for screen aligned draw */
1461         glPushMatrix();
1462         size= screen_aligned(rv3d, rv3d->twmat);
1463
1464         glDisable(GL_DEPTH_TEST);
1465
1466         qobj= gluNewQuadric();
1467
1468         /* Screen aligned view rot circle */
1469         if(drawflags & MAN_ROT_V) {
1470                 float unitmat[4][4];
1471                 Mat4One(unitmat);
1472
1473                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
1474                 UI_ThemeColor(TH_TRANSFORM);
1475                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
1476
1477                 if(moving) {
1478                         float vec[3];
1479                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
1480                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
1481                         vec[2]= 0.0f;
1482                         Normalize(vec);
1483                         VecMulf(vec, 1.2f*size);
1484                         glBegin(GL_LINES);
1485                         glVertex3f(0.0, 0.0, 0.0);
1486                         glVertex3fv(vec);
1487                         glEnd();
1488                 }
1489         }
1490         glPopMatrix();
1491
1492         /* apply the transform delta */
1493         if(moving) {
1494                 float matt[4][4];
1495                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1496                 // XXX          if (t->flag & T_USES_MANIPULATOR) {
1497                 // XXX                  Mat4MulMat34(matt, t->mat, rv3d->twmat);
1498                 // XXX }
1499                 wmMultMatrix(matt);
1500         }
1501         else {
1502                 wmMultMatrix(rv3d->twmat);
1503         }
1504
1505         glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1506
1507         /* axis */
1508         if( (G.f & G_PICKSEL)==0 ) {
1509
1510                 // only draw axis when combo didn't draw scale axes
1511                 if((combo & V3D_MANIP_SCALE)==0)
1512                         draw_manipulator_axes(v3d, colcode, drawflags & MAN_ROT_X, drawflags & MAN_ROT_Y, drawflags & MAN_ROT_Z);
1513
1514                 /* only has to be set when not in picking */
1515                 gluQuadricDrawStyle(qobj, GLU_FILL);
1516         }
1517
1518         /* Z cyl */
1519         glTranslatef(0.0, 0.0, 1.0);
1520         if(drawflags & MAN_ROT_Z) {
1521                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1522                 manipulator_setcolor(v3d, 'z', colcode);
1523                 draw_cylinder(qobj, cylen, cywid);
1524         }
1525         /* X cyl */
1526         glTranslatef(1.0, 0.0, -1.0);
1527         if(drawflags & MAN_ROT_X) {
1528                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1529                 glRotatef(90.0, 0.0, 1.0, 0.0);
1530                 manipulator_setcolor(v3d, 'x', colcode);
1531                 draw_cylinder(qobj, cylen, cywid);
1532                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1533         }
1534         /* Y cylinder */
1535         glTranslatef(-1.0, 1.0, 0.0);
1536         if(drawflags & MAN_ROT_Y) {
1537                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1538                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1539                 manipulator_setcolor(v3d, 'y', colcode);
1540                 draw_cylinder(qobj, cylen, cywid);
1541         }
1542
1543         /* restore */
1544
1545         gluDeleteQuadric(qobj);
1546         wmLoadMatrix(rv3d->viewmat);
1547
1548         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1549
1550 }
1551
1552
1553 /* ********************************************* */
1554
1555 static float get_manipulator_drawsize(ARegion *ar)
1556 {
1557         RegionView3D *rv3d= ar->regiondata;
1558         float size = get_drawsize(ar, rv3d->twmat[3]);
1559
1560         size*= (float)U.tw_size;
1561
1562         return size;
1563 }
1564
1565
1566 /* main call, does calc centers & orientation too */
1567 /* uses global G.moving */
1568 static int drawflags= 0xFFFF;           // only for the calls below, belongs in scene...?
1569
1570 void BIF_draw_manipulator(const bContext *C)
1571 {
1572         ScrArea *sa= CTX_wm_area(C);
1573         ARegion *ar= CTX_wm_region(C);
1574         Scene *scene= CTX_data_scene(C);
1575         View3D *v3d= sa->spacedata.first;
1576         RegionView3D *rv3d= ar->regiondata;
1577         int totsel;
1578
1579         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return;
1580 //      if(G.moving && (G.moving & G_TRANSFORM_MANIP)==0) return;
1581
1582 //      if(G.moving==0) {
1583         {
1584                 v3d->twflag &= ~V3D_DRAW_MANIPULATOR;
1585
1586                 totsel= calc_manipulator_stats(C);
1587                 if(totsel==0) return;
1588                 drawflags= v3d->twdrawflag;     /* set in calc_manipulator_stats */
1589
1590                 v3d->twflag |= V3D_DRAW_MANIPULATOR;
1591
1592                 /* now we can define center */
1593                 switch(v3d->around) {
1594                 case V3D_CENTER:
1595                 case V3D_ACTIVE:
1596                         rv3d->twmat[3][0]= (scene->twmin[0] + scene->twmax[0])/2.0f;
1597                         rv3d->twmat[3][1]= (scene->twmin[1] + scene->twmax[1])/2.0f;
1598                         rv3d->twmat[3][2]= (scene->twmin[2] + scene->twmax[2])/2.0f;
1599                         if(v3d->around==V3D_ACTIVE && scene->obedit==NULL) {
1600                                 Object *ob= OBACT;
1601                                 if(ob && !(ob->mode & OB_MODE_POSE))
1602                                         VECCOPY(rv3d->twmat[3], ob->obmat[3]);
1603                         }
1604                         break;
1605                 case V3D_LOCAL:
1606                 case V3D_CENTROID:
1607                         VECCOPY(rv3d->twmat[3], scene->twcent);
1608                         break;
1609                 case V3D_CURSOR:
1610                         VECCOPY(rv3d->twmat[3], give_cursor(scene, v3d));
1611                         break;
1612                 }
1613
1614                 Mat4MulFloat3((float *)rv3d->twmat, get_manipulator_drawsize(ar));
1615         }
1616
1617         if(v3d->twflag & V3D_DRAW_MANIPULATOR) {
1618
1619                 if(v3d->twtype & V3D_MANIP_ROTATE) {
1620
1621                         /* rotate has special ghosting draw, for pie chart */
1622                         if(G.moving) draw_manipulator_rotate_ghost(v3d, rv3d, drawflags);
1623
1624                         if(G.moving) glEnable(GL_BLEND);
1625
1626                         if(G.rt==3) {
1627                                 if(G.moving) draw_manipulator_rotate_cyl(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1628                                 else draw_manipulator_rotate_cyl(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1629                         }
1630                         else
1631                                 draw_manipulator_rotate(v3d, rv3d, 0 /* G.moving*/, drawflags, v3d->twtype);
1632
1633                         glDisable(GL_BLEND);
1634                 }
1635                 if(v3d->twtype & V3D_MANIP_SCALE) {
1636                         if(G.moving) {
1637                                 glEnable(GL_BLEND);
1638                                 draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_GHOST);
1639                                 draw_manipulator_scale(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1640                                 glDisable(GL_BLEND);
1641                         }
1642                         else draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1643                 }
1644                 if(v3d->twtype & V3D_MANIP_TRANSLATE) {
1645                         if(G.moving) {
1646                                 glEnable(GL_BLEND);
1647                                 draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_GHOST);
1648                                 draw_manipulator_translate(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1649                                 glDisable(GL_BLEND);
1650                         }
1651                         else draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1652                 }
1653         }
1654 }
1655
1656 static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, short *mval, float hotspot)
1657 {
1658         View3D *v3d= sa->spacedata.first;
1659         RegionView3D *rv3d= ar->regiondata;
1660         rctf rect;
1661         GLuint buffer[64];              // max 4 items per select, so large enuf
1662         short hits;
1663         extern void setwinmatrixview3d(ARegion *ar, View3D *v3d, rctf *rect); // XXX check a bit later on this... (ton)
1664
1665         G.f |= G_PICKSEL;
1666
1667         rect.xmin= mval[0]-hotspot;
1668         rect.xmax= mval[0]+hotspot;
1669         rect.ymin= mval[1]-hotspot;
1670         rect.ymax= mval[1]+hotspot;
1671
1672         setwinmatrixview3d(ar, v3d, &rect);
1673         Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1674
1675         glSelectBuffer( 64, buffer);
1676         glRenderMode(GL_SELECT);
1677         glInitNames();  /* these two calls whatfor? It doesnt work otherwise */
1678         glPushName(-2);
1679
1680         /* do the drawing */
1681         if(v3d->twtype & V3D_MANIP_ROTATE) {
1682                 if(G.rt==3) draw_manipulator_rotate_cyl(v3d, rv3d, 0, MAN_ROT_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1683                 else draw_manipulator_rotate(v3d, rv3d, 0, MAN_ROT_C & v3d->twdrawflag, v3d->twtype);
1684         }
1685         if(v3d->twtype & V3D_MANIP_SCALE)
1686                 draw_manipulator_scale(v3d, rv3d, 0, MAN_SCALE_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1687         if(v3d->twtype & V3D_MANIP_TRANSLATE)
1688                 draw_manipulator_translate(v3d, rv3d, 0, MAN_TRANS_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1689
1690         glPopName();
1691         hits= glRenderMode(GL_RENDER);
1692
1693         G.f &= ~G_PICKSEL;
1694         setwinmatrixview3d(ar, v3d, NULL);
1695         Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1696
1697         if(hits==1) return buffer[3];
1698         else if(hits>1) {
1699                 GLuint val, dep, mindep=0, mindeprot=0, minval=0, minvalrot=0;
1700                 int a;
1701
1702                 /* we compare the hits in buffer, but value centers highest */
1703                 /* we also store the rotation hits separate (because of arcs) and return hits on other widgets if there are */
1704
1705                 for(a=0; a<hits; a++) {
1706                         dep= buffer[4*a + 1];
1707                         val= buffer[4*a + 3];
1708
1709                         if(val==MAN_TRANS_C) return MAN_TRANS_C;
1710                         else if(val==MAN_SCALE_C) return MAN_SCALE_C;
1711                         else {
1712                                 if(val & MAN_ROT_C) {
1713                                         if(minvalrot==0 || dep<mindeprot) {
1714                                                 mindeprot= dep;
1715                                                 minvalrot= val;
1716                                         }
1717                                 }
1718                                 else {
1719                                         if(minval==0 || dep<mindep) {
1720                                                 mindep= dep;
1721                                                 minval= val;
1722                                         }
1723                                 }
1724                         }
1725                 }
1726
1727                 if(minval)
1728                         return minval;
1729                 else
1730                         return minvalrot;
1731         }
1732         return 0;
1733 }
1734
1735 /* return 0; nothing happened */
1736 int BIF_do_manipulator(bContext *C, struct wmEvent *event, wmOperator *op)
1737 {
1738         ScrArea *sa= CTX_wm_area(C);
1739         View3D *v3d= sa->spacedata.first;
1740         ARegion *ar= CTX_wm_region(C);
1741         int constraint_axis[3] = {0, 0, 0};
1742         int val;
1743         int shift = event->shift;
1744
1745         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return 0;
1746         if(!(v3d->twflag & V3D_DRAW_MANIPULATOR)) return 0;
1747
1748         // find the hotspots first test narrow hotspot
1749         val= manipulator_selectbuf(sa, ar, event->mval, 0.5f*(float)U.tw_hotspot);
1750         if(val) {
1751
1752                 // drawflags still global, for drawing call above
1753                 drawflags= manipulator_selectbuf(sa, ar, event->mval, 0.2f*(float)U.tw_hotspot);
1754                 if(drawflags==0) drawflags= val;
1755
1756                 if (drawflags & MAN_TRANS_C) {
1757                         switch(drawflags) {
1758                         case MAN_TRANS_C:
1759                                 break;
1760                         case MAN_TRANS_X:
1761                                 if(shift) {
1762                                         constraint_axis[1] = 1;
1763                                         constraint_axis[2] = 1;
1764                                 }
1765                                 else
1766                                         constraint_axis[0] = 1;
1767                                 break;
1768                         case MAN_TRANS_Y:
1769                                 if(shift) {
1770                                         constraint_axis[0] = 1;
1771                                         constraint_axis[2] = 1;
1772                                 }
1773                                 else
1774                                         constraint_axis[1] = 1;
1775                                 break;
1776                         case MAN_TRANS_Z:
1777                                 if(shift) {
1778                                         constraint_axis[0] = 1;
1779                                         constraint_axis[1] = 1;
1780                                 }
1781                                 else
1782                                         constraint_axis[2] = 1;
1783                                 break;
1784                         }
1785                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1786                         WM_operator_name_call(C, "TFM_OT_translate", WM_OP_INVOKE_DEFAULT, op->ptr);
1787                 }
1788                 else if (drawflags & MAN_SCALE_C) {
1789                         switch(drawflags) {
1790                         case MAN_SCALE_X:
1791                                 if(shift) {
1792                                         constraint_axis[1] = 1;
1793                                         constraint_axis[2] = 1;
1794                                 }
1795                                 else
1796                                         constraint_axis[0] = 1;
1797                                 break;
1798                         case MAN_SCALE_Y:
1799                                 if(shift) {
1800                                         constraint_axis[0] = 1;
1801                                         constraint_axis[2] = 1;
1802                                 }
1803                                 else
1804                                         constraint_axis[1] = 1;
1805                                 break;
1806                         case MAN_SCALE_Z:
1807                                 if(shift) {
1808                                         constraint_axis[0] = 1;
1809                                         constraint_axis[1] = 1;
1810                                 }
1811                                 else
1812                                         constraint_axis[2] = 1;
1813                                 break;
1814                         }
1815                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1816                         WM_operator_name_call(C, "TFM_OT_resize", WM_OP_INVOKE_DEFAULT, op->ptr);
1817                 }
1818                 else if (drawflags == MAN_ROT_T) { /* trackball need special case, init is different */
1819                         WM_operator_name_call(C, "TFM_OT_trackball", WM_OP_INVOKE_DEFAULT, op->ptr);
1820                 }
1821                 else if (drawflags & MAN_ROT_C) {
1822                         switch(drawflags) {
1823                         case MAN_ROT_X:
1824                                 constraint_axis[0] = 1;
1825                                 break;
1826                         case MAN_ROT_Y:
1827                                 constraint_axis[1] = 1;
1828                                 break;
1829                         case MAN_ROT_Z:
1830                                 constraint_axis[2] = 1;
1831                                 break;
1832                         }
1833                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1834                         WM_operator_name_call(C, "TFM_OT_rotate", WM_OP_INVOKE_DEFAULT, op->ptr);
1835                 }
1836         }
1837         /* after transform, restore drawflags */
1838         drawflags= 0xFFFF;
1839
1840         return val;
1841 }
1842