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