fix [#24499] Consistency Issue with LassoSelect/ExtendOption
[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_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_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(scene, 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, char alpha)
699 {
700         char col[4];
701
702         if(colcode==MAN_GHOST) {
703                 glColor4ub(0, 0, 0, 70);
704         }
705         else if(colcode==MAN_MOVECOL) {
706                 UI_GetThemeColor3ubv(TH_TRANSFORM, col);
707                 glColor4ub(col[0], col[1], col[2], alpha);
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                         glColor4ub(col[0], col[1], col[2], alpha);
724                         break;
725                 case 'x':
726                         glColor4ub(220, 0, 0, alpha);
727                         break;
728                 case 'y':
729                         glColor4ub(0, 220, 0, alpha);
730                         break;
731                 case 'z':
732                         glColor4ub(30, 30, 220, alpha);
733                         break;
734                 }
735         }
736 }
737
738 /* viewmatrix should have been set OK, also no shademode! */
739 static void draw_manipulator_axes(View3D *v3d, RegionView3D *rv3d, int colcode, int flagx, int flagy, int flagz)
740 {
741
742         /* axes */
743         if(flagx) {
744                 manipulator_setcolor(v3d, 'x', colcode, axisBlendAngle(rv3d->twangle[0]));
745                 if(flagx & MAN_SCALE_X) glLoadName(MAN_SCALE_X);
746                 else if(flagx & MAN_TRANS_X) glLoadName(MAN_TRANS_X);
747                 glBegin(GL_LINES);
748                 glVertex3f(0.2f, 0.0f, 0.0f);
749                 glVertex3f(1.0f, 0.0f, 0.0f);
750                 glEnd();
751         }
752         if(flagy) {
753                 if(flagy & MAN_SCALE_Y) glLoadName(MAN_SCALE_Y);
754                 else if(flagy & MAN_TRANS_Y) glLoadName(MAN_TRANS_Y);
755                 manipulator_setcolor(v3d, 'y', colcode, axisBlendAngle(rv3d->twangle[1]));
756                 glBegin(GL_LINES);
757                 glVertex3f(0.0f, 0.2f, 0.0f);
758                 glVertex3f(0.0f, 1.0f, 0.0f);
759                 glEnd();
760         }
761         if(flagz) {
762                 if(flagz & MAN_SCALE_Z) glLoadName(MAN_SCALE_Z);
763                 else if(flagz & MAN_TRANS_Z) glLoadName(MAN_TRANS_Z);
764                 manipulator_setcolor(v3d, 'z', colcode, axisBlendAngle(rv3d->twangle[2]));
765                 glBegin(GL_LINES);
766                 glVertex3f(0.0f, 0.0f, 0.2f);
767                 glVertex3f(0.0f, 0.0f, 1.0f);
768                 glEnd();
769         }
770 }
771
772 static void preOrthoFront(int ortho, float twmat[][4], int axis)
773 {
774         if (ortho == 0) {
775                 float omat[4][4];
776                 copy_m4_m4(omat, twmat);
777                 orthogonalize_m4(omat, axis);
778                 glPushMatrix();
779                 glMultMatrixf(omat);
780                 glFrontFace(is_negative_m4(omat) ? GL_CW:GL_CCW);
781         }
782 }
783
784 static void postOrtho(int ortho)
785 {
786         if (ortho == 0) {
787                 glPopMatrix();
788         }
789 }
790
791 static void draw_manipulator_rotate(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo)
792 {
793         GLUquadricObj *qobj;
794         double plane[4];
795         float matt[4][4];
796         float size, vec[3], unitmat[4][4];
797         float cywid= 0.33f*0.01f*(float)U.tw_handlesize;
798         float cusize= cywid*0.65f;
799         int arcs= (G.rt!=2);
800         int colcode;
801         int ortho;
802
803         if(moving) colcode= MAN_MOVECOL;
804         else colcode= MAN_RGB;
805
806         /* when called while moving in mixed mode, do not draw when... */
807         if((drawflags & MAN_ROT_C)==0) return;
808
809         /* Init stuff */
810         glDisable(GL_DEPTH_TEST);
811         unit_m4(unitmat);
812
813         qobj= gluNewQuadric();
814         gluQuadricDrawStyle(qobj, GLU_FILL);
815
816         /* prepare for screen aligned draw */
817         VECCOPY(vec, rv3d->twmat[0]);
818         size= normalize_v3(vec);
819         glPushMatrix();
820         glTranslatef(rv3d->twmat[3][0], rv3d->twmat[3][1], rv3d->twmat[3][2]);
821
822         if(arcs) {
823                 /* clipplane makes nice handles, calc here because of multmatrix but with translate! */
824                 VECCOPY(plane, rv3d->viewinv[2]);
825                 plane[3]= -0.02*size; // clip just a bit more
826                 glClipPlane(GL_CLIP_PLANE0, plane);
827         }
828         /* sets view screen aligned */
829         glRotatef( -360.0f*saacos(rv3d->viewquat[0])/(float)M_PI, rv3d->viewquat[1], rv3d->viewquat[2], rv3d->viewquat[3]);
830
831         /* Screen aligned help circle */
832         if(arcs) {
833                 if((G.f & G_PICKSEL)==0) {
834                         UI_ThemeColorShade(TH_BACK, -30);
835                         drawcircball(GL_LINE_LOOP, unitmat[3], size, unitmat);
836                 }
837         }
838
839         /* Screen aligned trackball rot circle */
840         if(drawflags & MAN_ROT_T) {
841                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_T);
842
843                 UI_ThemeColor(TH_TRANSFORM);
844                 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
845         }
846
847         /* Screen aligned view rot circle */
848         if(drawflags & MAN_ROT_V) {
849                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
850                 UI_ThemeColor(TH_TRANSFORM);
851                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
852
853                 if(moving) {
854                         float vec[3];
855                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
856                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
857                         vec[2]= 0.0f;
858                         normalize_v3(vec);
859                         mul_v3_fl(vec, 1.2f*size);
860                         glBegin(GL_LINES);
861                         glVertex3f(0.0f, 0.0f, 0.0f);
862                         glVertex3fv(vec);
863                         glEnd();
864                 }
865         }
866         glPopMatrix();
867
868
869         ortho = is_orthogonal_m4(rv3d->twmat);
870         
871         /* apply the transform delta */
872         if(moving) {
873                 copy_m4_m4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
874                 // XXX mul_m4_m3m4(matt, t->mat, rv3d->twmat);
875                 if (ortho) {
876                         glMultMatrixf(matt);
877                         glFrontFace(is_negative_m4(matt) ? GL_CW:GL_CCW);
878                 }
879         }
880         else {
881                 if (ortho) {
882                         glFrontFace(is_negative_m4(rv3d->twmat) ? GL_CW:GL_CCW);
883                         glMultMatrixf(rv3d->twmat);
884                 }
885         }
886
887         /* axes */
888         if(arcs==0) {
889                 if(!(G.f & G_PICKSEL)) {
890                         if( (combo & V3D_MANIP_SCALE)==0) {
891                                 /* axis */
892                                 if( (drawflags & MAN_ROT_X) || (moving && (drawflags & MAN_ROT_Z)) ) {
893                                         preOrthoFront(ortho, rv3d->twmat, 2);
894                                         manipulator_setcolor(v3d, 'x', colcode, 255);
895                                         glBegin(GL_LINES);
896                                         glVertex3f(0.2f, 0.0f, 0.0f);
897                                         glVertex3f(1.0f, 0.0f, 0.0f);
898                                         glEnd();
899                                         postOrtho(ortho);
900                                 }
901                                 if( (drawflags & MAN_ROT_Y) || (moving && (drawflags & MAN_ROT_X)) ) {
902                                         preOrthoFront(ortho, rv3d->twmat, 0);
903                                         manipulator_setcolor(v3d, 'y', colcode, 255);
904                                         glBegin(GL_LINES);
905                                         glVertex3f(0.0f, 0.2f, 0.0f);
906                                         glVertex3f(0.0f, 1.0f, 0.0f);
907                                         glEnd();
908                                         postOrtho(ortho);
909                                 }
910                                 if( (drawflags & MAN_ROT_Z) || (moving && (drawflags & MAN_ROT_Y)) ) {
911                                         preOrthoFront(ortho, rv3d->twmat, 1);
912                                         manipulator_setcolor(v3d, 'z', colcode, 255);
913                                         glBegin(GL_LINES);
914                                         glVertex3f(0.0f, 0.0f, 0.2f);
915                                         glVertex3f(0.0f, 0.0f, 1.0f);
916                                         glEnd();
917                                         postOrtho(ortho);
918                                 }
919                         }
920                 }
921         }
922
923         if(arcs==0 && moving) {
924
925                 /* Z circle */
926                 if(drawflags & MAN_ROT_Z) {
927                         preOrthoFront(ortho, matt, 2);
928                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
929                         manipulator_setcolor(v3d, 'z', colcode, 255);
930                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
931                         postOrtho(ortho);
932                 }
933                 /* X circle */
934                 if(drawflags & MAN_ROT_X) {
935                         preOrthoFront(ortho, matt, 0);
936                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
937                         glRotatef(90.0, 0.0, 1.0, 0.0);
938                         manipulator_setcolor(v3d, 'x', colcode, 255);
939                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
940                         glRotatef(-90.0, 0.0, 1.0, 0.0);
941                         postOrtho(ortho);
942                 }
943                 /* Y circle */
944                 if(drawflags & MAN_ROT_Y) {
945                         preOrthoFront(ortho, matt, 1);
946                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
947                         glRotatef(-90.0, 1.0, 0.0, 0.0);
948                         manipulator_setcolor(v3d, 'y', colcode, 255);
949                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
950                         glRotatef(90.0, 1.0, 0.0, 0.0);
951                         postOrtho(ortho);
952                 }
953
954                 if(arcs) glDisable(GL_CLIP_PLANE0);
955         }
956         // donut arcs
957         if(arcs) {
958                 glEnable(GL_CLIP_PLANE0);
959
960                 /* Z circle */
961                 if(drawflags & MAN_ROT_Z) {
962                         preOrthoFront(ortho, rv3d->twmat, 2);
963                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
964                         manipulator_setcolor(v3d, 'z', colcode, 255);
965                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
966                         postOrtho(ortho);
967                 }
968                 /* X circle */
969                 if(drawflags & MAN_ROT_X) {
970                         preOrthoFront(ortho, rv3d->twmat, 0);
971                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
972                         glRotatef(90.0, 0.0, 1.0, 0.0);
973                         manipulator_setcolor(v3d, 'x', colcode, 255);
974                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
975                         glRotatef(-90.0, 0.0, 1.0, 0.0);
976                         postOrtho(ortho);
977                 }
978                 /* Y circle */
979                 if(drawflags & MAN_ROT_Y) {
980                         preOrthoFront(ortho, rv3d->twmat, 1);
981                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
982                         glRotatef(-90.0, 1.0, 0.0, 0.0);
983                         manipulator_setcolor(v3d, 'y', colcode, 255);
984                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
985                         glRotatef(90.0, 1.0, 0.0, 0.0);
986                         postOrtho(ortho);
987                 }
988
989                 glDisable(GL_CLIP_PLANE0);
990         }
991
992         if(arcs==0) {
993
994                 /* Z handle on X axis */
995                 if(drawflags & MAN_ROT_Z) {
996                         preOrthoFront(ortho, rv3d->twmat, 2);
997                         glPushMatrix();
998                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
999                         manipulator_setcolor(v3d, 'z', colcode, 255);
1000
1001                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1002
1003                         glPopMatrix();
1004                         postOrtho(ortho);
1005                 }
1006
1007                 /* Y handle on X axis */
1008                 if(drawflags & MAN_ROT_Y) {
1009                         preOrthoFront(ortho, rv3d->twmat, 1);
1010                         glPushMatrix();
1011                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1012                         manipulator_setcolor(v3d, 'y', colcode, 255);
1013
1014                         glRotatef(90.0, 1.0, 0.0, 0.0);
1015                         glRotatef(90.0, 0.0, 0.0, 1.0);
1016                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1017
1018                         glPopMatrix();
1019                         postOrtho(ortho);
1020                 }
1021
1022                 /* X handle on Z axis */
1023                 if(drawflags & MAN_ROT_X) {
1024                         preOrthoFront(ortho, rv3d->twmat, 0);
1025                         glPushMatrix();
1026                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1027                         manipulator_setcolor(v3d, 'x', colcode, 255);
1028
1029                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1030                         glRotatef(90.0, 0.0, 0.0, 1.0);
1031                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1032
1033                         glPopMatrix();
1034                         postOrtho(ortho);
1035                 }
1036
1037         }
1038
1039         /* restore */
1040         glLoadMatrixf(rv3d->viewmat);
1041         gluDeleteQuadric(qobj);
1042         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1043
1044 }
1045
1046 static void drawsolidcube(float size)
1047 {
1048         static float cube[8][3] = {
1049         {-1.0, -1.0, -1.0},
1050         {-1.0, -1.0,  1.0},
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         float n[3];
1058
1059         glPushMatrix();
1060         glScalef(size, size, size);
1061
1062         n[0]=0; n[1]=0; n[2]=0;
1063         glBegin(GL_QUADS);
1064         n[0]= -1.0;
1065         glNormal3fv(n);
1066         glVertex3fv(cube[0]); glVertex3fv(cube[1]); glVertex3fv(cube[2]); glVertex3fv(cube[3]);
1067         n[0]=0;
1068         glEnd();
1069
1070         glBegin(GL_QUADS);
1071         n[1]= -1.0;
1072         glNormal3fv(n);
1073         glVertex3fv(cube[0]); glVertex3fv(cube[4]); glVertex3fv(cube[5]); glVertex3fv(cube[1]);
1074         n[1]=0;
1075         glEnd();
1076
1077         glBegin(GL_QUADS);
1078         n[0]= 1.0;
1079         glNormal3fv(n);
1080         glVertex3fv(cube[4]); glVertex3fv(cube[7]); glVertex3fv(cube[6]); glVertex3fv(cube[5]);
1081         n[0]=0;
1082         glEnd();
1083
1084         glBegin(GL_QUADS);
1085         n[1]= 1.0;
1086         glNormal3fv(n);
1087         glVertex3fv(cube[7]); glVertex3fv(cube[3]); glVertex3fv(cube[2]); glVertex3fv(cube[6]);
1088         n[1]=0;
1089         glEnd();
1090
1091         glBegin(GL_QUADS);
1092         n[2]= 1.0;
1093         glNormal3fv(n);
1094         glVertex3fv(cube[1]); glVertex3fv(cube[5]); glVertex3fv(cube[6]); glVertex3fv(cube[2]);
1095         n[2]=0;
1096         glEnd();
1097
1098         glBegin(GL_QUADS);
1099         n[2]= -1.0;
1100         glNormal3fv(n);
1101         glVertex3fv(cube[7]); glVertex3fv(cube[4]); glVertex3fv(cube[0]); glVertex3fv(cube[3]);
1102         glEnd();
1103
1104         glPopMatrix();
1105 }
1106
1107
1108 static void draw_manipulator_scale(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1109 {
1110         float cywid= 0.25f*0.01f*(float)U.tw_handlesize;
1111         float cusize= cywid*0.75f, dz;
1112
1113         /* when called while moving in mixed mode, do not draw when... */
1114         if((drawflags & MAN_SCALE_C)==0) return;
1115
1116         glDisable(GL_DEPTH_TEST);
1117
1118         /* not in combo mode */
1119         if( (combo & (V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE))==0) {
1120                 float size, unitmat[4][4];
1121                 int shift= 0; // XXX
1122
1123                 /* center circle, do not add to selection when shift is pressed (planar constraint)  */
1124                 if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_SCALE_C);
1125
1126                 manipulator_setcolor(v3d, 'c', colcode, 255);
1127                 glPushMatrix();
1128                 size= screen_aligned(rv3d, rv3d->twmat);
1129                 unit_m4(unitmat);
1130                 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1131                 glPopMatrix();
1132
1133                 dz= 1.0;
1134         }
1135         else dz= 1.0f-4.0f*cusize;
1136
1137         if(moving) {
1138                 float matt[4][4];
1139
1140                 copy_m4_m4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1141                 // XXX mul_m4_m3m4(matt, t->mat, rv3d->twmat);
1142                 glMultMatrixf(matt);
1143                 glFrontFace(is_negative_m4(matt) ? GL_CW:GL_CCW);
1144         }
1145         else {
1146                 glMultMatrixf(rv3d->twmat);
1147                 glFrontFace(is_negative_m4(rv3d->twmat) ? GL_CW:GL_CCW);
1148         }
1149
1150         /* axis */
1151
1152         /* in combo mode, this is always drawn as first type */
1153         draw_manipulator_axes(v3d, rv3d, colcode, drawflags & MAN_SCALE_X, drawflags & MAN_SCALE_Y, drawflags & MAN_SCALE_Z);
1154
1155         /* Z cube */
1156         glTranslatef(0.0, 0.0, dz);
1157         if(drawflags & MAN_SCALE_Z) {
1158                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Z);
1159                 manipulator_setcolor(v3d, 'z', colcode, axisBlendAngle(rv3d->twangle[2]));
1160                 drawsolidcube(cusize);
1161         }
1162         /* X cube */
1163         glTranslatef(dz, 0.0, -dz);
1164         if(drawflags & MAN_SCALE_X) {
1165                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_X);
1166                 manipulator_setcolor(v3d, 'x', colcode, axisBlendAngle(rv3d->twangle[0]));
1167                 drawsolidcube(cusize);
1168         }
1169         /* Y cube */
1170         glTranslatef(-dz, dz, 0.0);
1171         if(drawflags & MAN_SCALE_Y) {
1172                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Y);
1173                 manipulator_setcolor(v3d, 'y', colcode, axisBlendAngle(rv3d->twangle[1]));
1174                 drawsolidcube(cusize);
1175         }
1176
1177         /* if shiftkey, center point as last, for selectbuffer order */
1178         if(G.f & G_PICKSEL) {
1179                 int shift= 0; // XXX
1180
1181                 if(shift) {
1182                         glTranslatef(0.0, -dz, 0.0);
1183                         glLoadName(MAN_SCALE_C);
1184                         glBegin(GL_POINTS);
1185                         glVertex3f(0.0, 0.0, 0.0);
1186                         glEnd();
1187                 }
1188         }
1189
1190         /* restore */
1191         glLoadMatrixf(rv3d->viewmat);
1192
1193         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1194         glFrontFace(GL_CCW);
1195 }
1196
1197
1198 static void draw_cone(GLUquadricObj *qobj, float len, float width)
1199 {
1200         glTranslatef(0.0, 0.0, -0.5f*len);
1201         gluCylinder(qobj, width, 0.0, len, 8, 1);
1202         gluQuadricOrientation(qobj, GLU_INSIDE);
1203         gluDisk(qobj, 0.0, width, 8, 1);
1204         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1205         glTranslatef(0.0, 0.0, 0.5f*len);
1206 }
1207
1208 static void draw_cylinder(GLUquadricObj *qobj, float len, float width)
1209 {
1210
1211         width*= 0.8f;   // just for beauty
1212
1213         glTranslatef(0.0, 0.0, -0.5f*len);
1214         gluCylinder(qobj, width, width, len, 8, 1);
1215         gluQuadricOrientation(qobj, GLU_INSIDE);
1216         gluDisk(qobj, 0.0, width, 8, 1);
1217         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1218         glTranslatef(0.0, 0.0, len);
1219         gluDisk(qobj, 0.0, width, 8, 1);
1220         glTranslatef(0.0, 0.0, -0.5f*len);
1221 }
1222
1223
1224 static void draw_manipulator_translate(View3D *v3d, RegionView3D *rv3d, int UNUSED(moving), int drawflags, int combo, int colcode)
1225 {
1226         GLUquadricObj *qobj;
1227         float cylen= 0.01f*(float)U.tw_handlesize;
1228         float cywid= 0.25f*cylen, dz, size;
1229         float unitmat[4][4];
1230         int shift= 0; // XXX
1231
1232         /* when called while moving in mixed mode, do not draw when... */
1233         if((drawflags & MAN_TRANS_C)==0) return;
1234
1235         // XXX if(moving) glTranslatef(t->vec[0], t->vec[1], t->vec[2]);
1236         glDisable(GL_DEPTH_TEST);
1237
1238         qobj= gluNewQuadric();
1239         gluQuadricDrawStyle(qobj, GLU_FILL);
1240
1241         /* center circle, do not add to selection when shift is pressed (planar constraint) */
1242         if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_TRANS_C);
1243
1244         manipulator_setcolor(v3d, 'c', colcode, 255);
1245         glPushMatrix();
1246         size= screen_aligned(rv3d, rv3d->twmat);
1247         unit_m4(unitmat);
1248         drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1249         glPopMatrix();
1250
1251         /* and now apply matrix, we move to local matrix drawing */
1252         glMultMatrixf(rv3d->twmat);
1253
1254         /* axis */
1255         glLoadName(-1);
1256
1257         // translate drawn as last, only axis when no combo with scale, or for ghosting
1258         if((combo & V3D_MANIP_SCALE)==0 || colcode==MAN_GHOST)
1259                 draw_manipulator_axes(v3d, rv3d, colcode, drawflags & MAN_TRANS_X, drawflags & MAN_TRANS_Y, drawflags & MAN_TRANS_Z);
1260
1261
1262         /* offset in combo mode, for rotate a bit more */
1263         if(combo & (V3D_MANIP_ROTATE)) dz= 1.0f+2.0f*cylen;
1264         else if(combo & (V3D_MANIP_SCALE)) dz= 1.0f+0.5f*cylen;
1265         else dz= 1.0f;
1266
1267         /* Z Cone */
1268         glTranslatef(0.0, 0.0, dz);
1269         if(drawflags & MAN_TRANS_Z) {
1270                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Z);
1271                 manipulator_setcolor(v3d, 'z', colcode, axisBlendAngle(rv3d->twangle[2]));
1272                 draw_cone(qobj, cylen, cywid);
1273         }
1274         /* X Cone */
1275         glTranslatef(dz, 0.0, -dz);
1276         if(drawflags & MAN_TRANS_X) {
1277                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_X);
1278                 glRotatef(90.0, 0.0, 1.0, 0.0);
1279                 manipulator_setcolor(v3d, 'x', colcode, axisBlendAngle(rv3d->twangle[0]));
1280                 draw_cone(qobj, cylen, cywid);
1281                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1282         }
1283         /* Y Cone */
1284         glTranslatef(-dz, dz, 0.0);
1285         if(drawflags & MAN_TRANS_Y) {
1286                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Y);
1287                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1288                 manipulator_setcolor(v3d, 'y', colcode, axisBlendAngle(rv3d->twangle[1]));
1289                 draw_cone(qobj, cylen, cywid);
1290         }
1291
1292         gluDeleteQuadric(qobj);
1293         glLoadMatrixf(rv3d->viewmat);
1294
1295         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1296
1297 }
1298
1299 static void draw_manipulator_rotate_cyl(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1300 {
1301         GLUquadricObj *qobj;
1302         float size;
1303         float cylen= 0.01f*(float)U.tw_handlesize;
1304         float cywid= 0.25f*cylen;
1305
1306         /* when called while moving in mixed mode, do not draw when... */
1307         if((drawflags & MAN_ROT_C)==0) return;
1308
1309         /* prepare for screen aligned draw */
1310         glPushMatrix();
1311         size= screen_aligned(rv3d, rv3d->twmat);
1312
1313         glDisable(GL_DEPTH_TEST);
1314
1315         qobj= gluNewQuadric();
1316
1317         /* Screen aligned view rot circle */
1318         if(drawflags & MAN_ROT_V) {
1319                 float unitmat[4][4];
1320                 unit_m4(unitmat);
1321
1322                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
1323                 UI_ThemeColor(TH_TRANSFORM);
1324                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
1325
1326                 if(moving) {
1327                         float vec[3];
1328                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
1329                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
1330                         vec[2]= 0.0f;
1331                         normalize_v3(vec);
1332                         mul_v3_fl(vec, 1.2f*size);
1333                         glBegin(GL_LINES);
1334                         glVertex3f(0.0, 0.0, 0.0);
1335                         glVertex3fv(vec);
1336                         glEnd();
1337                 }
1338         }
1339         glPopMatrix();
1340
1341         /* apply the transform delta */
1342         if(moving) {
1343                 float matt[4][4];
1344                 copy_m4_m4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1345                 // XXX          if (t->flag & T_USES_MANIPULATOR) {
1346                 // XXX                  mul_m4_m3m4(matt, t->mat, rv3d->twmat);
1347                 // XXX }
1348                 glMultMatrixf(matt);
1349         }
1350         else {
1351                 glMultMatrixf(rv3d->twmat);
1352         }
1353
1354         glFrontFace(is_negative_m4(rv3d->twmat) ? GL_CW:GL_CCW);
1355
1356         /* axis */
1357         if( (G.f & G_PICKSEL)==0 ) {
1358
1359                 // only draw axis when combo didn't draw scale axes
1360                 if((combo & V3D_MANIP_SCALE)==0)
1361                         draw_manipulator_axes(v3d, rv3d, colcode, drawflags & MAN_ROT_X, drawflags & MAN_ROT_Y, drawflags & MAN_ROT_Z);
1362
1363                 /* only has to be set when not in picking */
1364                 gluQuadricDrawStyle(qobj, GLU_FILL);
1365         }
1366
1367         /* Z cyl */
1368         glTranslatef(0.0, 0.0, 1.0);
1369         if(drawflags & MAN_ROT_Z) {
1370                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1371                 manipulator_setcolor(v3d, 'z', colcode, 255);
1372                 draw_cylinder(qobj, cylen, cywid);
1373         }
1374         /* X cyl */
1375         glTranslatef(1.0, 0.0, -1.0);
1376         if(drawflags & MAN_ROT_X) {
1377                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1378                 glRotatef(90.0, 0.0, 1.0, 0.0);
1379                 manipulator_setcolor(v3d, 'x', colcode, 255);
1380                 draw_cylinder(qobj, cylen, cywid);
1381                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1382         }
1383         /* Y cylinder */
1384         glTranslatef(-1.0, 1.0, 0.0);
1385         if(drawflags & MAN_ROT_Y) {
1386                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1387                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1388                 manipulator_setcolor(v3d, 'y', colcode, 255);
1389                 draw_cylinder(qobj, cylen, cywid);
1390         }
1391
1392         /* restore */
1393
1394         gluDeleteQuadric(qobj);
1395         glLoadMatrixf(rv3d->viewmat);
1396
1397         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1398
1399 }
1400
1401
1402 /* ********************************************* */
1403
1404 static float get_manipulator_drawsize(ARegion *ar)
1405 {
1406         RegionView3D *rv3d= ar->regiondata;
1407         float size = get_drawsize(ar, rv3d->twmat[3]);
1408
1409         size*= (float)U.tw_size;
1410
1411         return size;
1412 }
1413
1414
1415 /* main call, does calc centers & orientation too */
1416 /* uses global G.moving */
1417 static int drawflags= 0xFFFF;           // only for the calls below, belongs in scene...?
1418
1419 void BIF_draw_manipulator(const bContext *C)
1420 {
1421         ScrArea *sa= CTX_wm_area(C);
1422         ARegion *ar= CTX_wm_region(C);
1423         Scene *scene= CTX_data_scene(C);
1424         View3D *v3d= sa->spacedata.first;
1425         RegionView3D *rv3d= ar->regiondata;
1426         int totsel;
1427
1428         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return;
1429 //      if(G.moving && (G.moving & G_TRANSFORM_MANIP)==0) return;
1430
1431 //      if(G.moving==0) {
1432         {
1433                 v3d->twflag &= ~V3D_DRAW_MANIPULATOR;
1434
1435                 totsel= calc_manipulator_stats(C);
1436                 if(totsel==0) return;
1437
1438                 v3d->twflag |= V3D_DRAW_MANIPULATOR;
1439
1440                 /* now we can define center */
1441                 switch(v3d->around) {
1442                 case V3D_CENTER:
1443                 case V3D_ACTIVE:
1444                         rv3d->twmat[3][0]= (scene->twmin[0] + scene->twmax[0])/2.0f;
1445                         rv3d->twmat[3][1]= (scene->twmin[1] + scene->twmax[1])/2.0f;
1446                         rv3d->twmat[3][2]= (scene->twmin[2] + scene->twmax[2])/2.0f;
1447                         if(v3d->around==V3D_ACTIVE && scene->obedit==NULL) {
1448                                 Object *ob= OBACT;
1449                                 if(ob && !(ob->mode & OB_MODE_POSE))
1450                                         VECCOPY(rv3d->twmat[3], ob->obmat[3]);
1451                         }
1452                         break;
1453                 case V3D_LOCAL:
1454                 case V3D_CENTROID:
1455                         VECCOPY(rv3d->twmat[3], scene->twcent);
1456                         break;
1457                 case V3D_CURSOR:
1458                         VECCOPY(rv3d->twmat[3], give_cursor(scene, v3d));
1459                         break;
1460                 }
1461
1462                 mul_mat3_m4_fl(rv3d->twmat, get_manipulator_drawsize(ar));
1463         }
1464
1465         test_manipulator_axis(C);
1466         drawflags= rv3d->twdrawflag;    /* set in calc_manipulator_stats */
1467
1468         if(v3d->twflag & V3D_DRAW_MANIPULATOR) {
1469
1470                 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1471                 glEnable(GL_BLEND);
1472                 if(v3d->twtype & V3D_MANIP_ROTATE) {
1473
1474                         if(G.rt==3) {
1475                                 if(G.moving) draw_manipulator_rotate_cyl(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1476                                 else draw_manipulator_rotate_cyl(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1477                         }
1478                         else
1479                                 draw_manipulator_rotate(v3d, rv3d, 0 /* G.moving*/, drawflags, v3d->twtype);
1480                 }
1481                 if(v3d->twtype & V3D_MANIP_SCALE) {
1482                         draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1483                 }
1484                 if(v3d->twtype & V3D_MANIP_TRANSLATE) {
1485                         draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1486                 }
1487
1488                 glDisable(GL_BLEND);
1489         }
1490 }
1491
1492 static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, short *mval, float hotspot)
1493 {
1494         View3D *v3d= sa->spacedata.first;
1495         RegionView3D *rv3d= ar->regiondata;
1496         rctf rect;
1497         GLuint buffer[64];              // max 4 items per select, so large enuf
1498         short hits;
1499         extern void setwinmatrixview3d(ARegion *ar, View3D *v3d, rctf *rect); // XXX check a bit later on this... (ton)
1500
1501         G.f |= G_PICKSEL;
1502
1503         rect.xmin= mval[0]-hotspot;
1504         rect.xmax= mval[0]+hotspot;
1505         rect.ymin= mval[1]-hotspot;
1506         rect.ymax= mval[1]+hotspot;
1507
1508         setwinmatrixview3d(ar, v3d, &rect);
1509         mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1510
1511         glSelectBuffer( 64, buffer);
1512         glRenderMode(GL_SELECT);
1513         glInitNames();  /* these two calls whatfor? It doesnt work otherwise */
1514         glPushName(-2);
1515
1516         /* do the drawing */
1517         if(v3d->twtype & V3D_MANIP_ROTATE) {
1518                 if(G.rt==3) draw_manipulator_rotate_cyl(v3d, rv3d, 0, MAN_ROT_C & rv3d->twdrawflag, v3d->twtype, MAN_RGB);
1519                 else draw_manipulator_rotate(v3d, rv3d, 0, MAN_ROT_C & rv3d->twdrawflag, v3d->twtype);
1520         }
1521         if(v3d->twtype & V3D_MANIP_SCALE)
1522                 draw_manipulator_scale(v3d, rv3d, 0, MAN_SCALE_C & rv3d->twdrawflag, v3d->twtype, MAN_RGB);
1523         if(v3d->twtype & V3D_MANIP_TRANSLATE)
1524                 draw_manipulator_translate(v3d, rv3d, 0, MAN_TRANS_C & rv3d->twdrawflag, v3d->twtype, MAN_RGB);
1525
1526         glPopName();
1527         hits= glRenderMode(GL_RENDER);
1528
1529         G.f &= ~G_PICKSEL;
1530         setwinmatrixview3d(ar, v3d, NULL);
1531         mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1532
1533         if(hits==1) return buffer[3];
1534         else if(hits>1) {
1535                 GLuint val, dep, mindep=0, mindeprot=0, minval=0, minvalrot=0;
1536                 int a;
1537
1538                 /* we compare the hits in buffer, but value centers highest */
1539                 /* we also store the rotation hits separate (because of arcs) and return hits on other widgets if there are */
1540
1541                 for(a=0; a<hits; a++) {
1542                         dep= buffer[4*a + 1];
1543                         val= buffer[4*a + 3];
1544
1545                         if(val==MAN_TRANS_C) return MAN_TRANS_C;
1546                         else if(val==MAN_SCALE_C) return MAN_SCALE_C;
1547                         else {
1548                                 if(val & MAN_ROT_C) {
1549                                         if(minvalrot==0 || dep<mindeprot) {
1550                                                 mindeprot= dep;
1551                                                 minvalrot= val;
1552                                         }
1553                                 }
1554                                 else {
1555                                         if(minval==0 || dep<mindep) {
1556                                                 mindep= dep;
1557                                                 minval= val;
1558                                         }
1559                                 }
1560                         }
1561                 }
1562
1563                 if(minval)
1564                         return minval;
1565                 else
1566                         return minvalrot;
1567         }
1568         return 0;
1569 }
1570
1571 int wm_operator_invoke(bContext *C, wmOperatorType *ot, wmEvent *event, PointerRNA *properties, ReportList *reports);
1572
1573 /* return 0; nothing happened */
1574 int BIF_do_manipulator(bContext *C, struct wmEvent *event, wmOperator *op)
1575 {
1576         ScrArea *sa= CTX_wm_area(C);
1577         View3D *v3d= sa->spacedata.first;
1578         ARegion *ar= CTX_wm_region(C);
1579         int constraint_axis[3] = {0, 0, 0};
1580         int val;
1581         int shift = event->shift;
1582
1583         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return 0;
1584         if(!(v3d->twflag & V3D_DRAW_MANIPULATOR)) return 0;
1585
1586         /* Force orientation */
1587         RNA_enum_set(op->ptr, "constraint_orientation", v3d->twmode);
1588
1589         // find the hotspots first test narrow hotspot
1590         val= manipulator_selectbuf(sa, ar, event->mval, 0.5f*(float)U.tw_hotspot);
1591         if(val) {
1592
1593                 // drawflags still global, for drawing call above
1594                 drawflags= manipulator_selectbuf(sa, ar, event->mval, 0.2f*(float)U.tw_hotspot);
1595                 if(drawflags==0) drawflags= val;
1596
1597                 if (drawflags & MAN_TRANS_C) {
1598                         switch(drawflags) {
1599                         case MAN_TRANS_C:
1600                                 break;
1601                         case MAN_TRANS_X:
1602                                 if(shift) {
1603                                         constraint_axis[1] = 1;
1604                                         constraint_axis[2] = 1;
1605                                 }
1606                                 else
1607                                         constraint_axis[0] = 1;
1608                                 break;
1609                         case MAN_TRANS_Y:
1610                                 if(shift) {
1611                                         constraint_axis[0] = 1;
1612                                         constraint_axis[2] = 1;
1613                                 }
1614                                 else
1615                                         constraint_axis[1] = 1;
1616                                 break;
1617                         case MAN_TRANS_Z:
1618                                 if(shift) {
1619                                         constraint_axis[0] = 1;
1620                                         constraint_axis[1] = 1;
1621                                 }
1622                                 else
1623                                         constraint_axis[2] = 1;
1624                                 break;
1625                         }
1626                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1627                         WM_operator_name_call(C, "TRANSFORM_OT_translate", WM_OP_INVOKE_DEFAULT, op->ptr);
1628                         //wm_operator_invoke(C, WM_operatortype_find("TRANSFORM_OT_translate", 0), event, op->ptr, NULL);
1629                 }
1630                 else if (drawflags & MAN_SCALE_C) {
1631                         switch(drawflags) {
1632                         case MAN_SCALE_X:
1633                                 if(shift) {
1634                                         constraint_axis[1] = 1;
1635                                         constraint_axis[2] = 1;
1636                                 }
1637                                 else
1638                                         constraint_axis[0] = 1;
1639                                 break;
1640                         case MAN_SCALE_Y:
1641                                 if(shift) {
1642                                         constraint_axis[0] = 1;
1643                                         constraint_axis[2] = 1;
1644                                 }
1645                                 else
1646                                         constraint_axis[1] = 1;
1647                                 break;
1648                         case MAN_SCALE_Z:
1649                                 if(shift) {
1650                                         constraint_axis[0] = 1;
1651                                         constraint_axis[1] = 1;
1652                                 }
1653                                 else
1654                                         constraint_axis[2] = 1;
1655                                 break;
1656                         }
1657                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1658                         WM_operator_name_call(C, "TRANSFORM_OT_resize", WM_OP_INVOKE_DEFAULT, op->ptr);
1659                         //wm_operator_invoke(C, WM_operatortype_find("TRANSFORM_OT_resize", 0), event, op->ptr, NULL);
1660                 }
1661                 else if (drawflags == MAN_ROT_T) { /* trackball need special case, init is different */
1662                         //wm_operator_invoke(C, WM_operatortype_find("TRANSFORM_OT_trackball", 0), event, op->ptr, NULL);
1663                         WM_operator_name_call(C, "TRANSFORM_OT_trackball", WM_OP_INVOKE_DEFAULT, op->ptr);
1664                 }
1665                 else if (drawflags & MAN_ROT_C) {
1666                         switch(drawflags) {
1667                         case MAN_ROT_X:
1668                                 constraint_axis[0] = 1;
1669                                 break;
1670                         case MAN_ROT_Y:
1671                                 constraint_axis[1] = 1;
1672                                 break;
1673                         case MAN_ROT_Z:
1674                                 constraint_axis[2] = 1;
1675                                 break;
1676                         }
1677                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1678                         WM_operator_name_call(C, "TRANSFORM_OT_rotate", WM_OP_INVOKE_DEFAULT, op->ptr);
1679                         //wm_operator_invoke(C, WM_operatortype_find("TRANSFORM_OT_rotate", 0), event, op->ptr, NULL);
1680                 }
1681         }
1682         /* after transform, restore drawflags */
1683         drawflags= 0xFFFF;
1684
1685         return val;
1686 }
1687