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