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