4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2005 Blender Foundation
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
40 #include "MEM_guardedalloc.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_action_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meta_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_userdef_types.h"
54 #include "DNA_view3d_types.h"
56 #include "RNA_access.h"
58 #include "BKE_action.h"
59 #include "BKE_armature.h"
60 #include "BKE_context.h"
61 #include "BKE_global.h"
62 #include "BKE_lattice.h"
64 #include "BKE_object.h"
65 #include "BKE_particle.h"
66 #include "BKE_pointcache.h"
67 #include "BKE_utildefines.h"
70 #include "BLI_editVert.h"
77 #include "ED_armature.h"
79 #include "ED_particle.h"
80 #include "ED_space_api.h"
81 #include "ED_transform.h"
82 #include "ED_view3d.h"
84 #include "UI_resources.h"
86 /* local module include */
87 #include "transform.h"
89 /* return codes for select, and drawing flags */
100 #define MAN_ROT_T 128
101 #define MAN_ROT_C 248
103 #define MAN_SCALE_X 256
104 #define MAN_SCALE_Y 512
105 #define MAN_SCALE_Z 1024
106 #define MAN_SCALE_C 1792
112 #define MAN_MOVECOL 2
115 static int is_mat4_flipped(float mat[][4])
119 cross_v3_v3v3(vec, mat[0], mat[1]);
120 if( dot_v3v3(vec, mat[2]) < 0.0 ) return 1;
124 /* transform widget center calc helper for below */
125 static void calc_tw_center(Scene *scene, float *co)
127 float *twcent= scene->twcent;
128 float *min= scene->twmin;
129 float *max= scene->twmax;
131 DO_MINMAX(co, min, max);
132 add_v3_v3v3(twcent, twcent, co);
135 static void protectflag_to_drawflags(short protectflag, short *drawflags)
137 if(protectflag & OB_LOCK_LOCX)
138 *drawflags &= ~MAN_TRANS_X;
139 if(protectflag & OB_LOCK_LOCY)
140 *drawflags &= ~MAN_TRANS_Y;
141 if(protectflag & OB_LOCK_LOCZ)
142 *drawflags &= ~MAN_TRANS_Z;
144 if(protectflag & OB_LOCK_ROTX)
145 *drawflags &= ~MAN_ROT_X;
146 if(protectflag & OB_LOCK_ROTY)
147 *drawflags &= ~MAN_ROT_Y;
148 if(protectflag & OB_LOCK_ROTZ)
149 *drawflags &= ~MAN_ROT_Z;
151 if(protectflag & OB_LOCK_SCALEX)
152 *drawflags &= ~MAN_SCALE_X;
153 if(protectflag & OB_LOCK_SCALEY)
154 *drawflags &= ~MAN_SCALE_Y;
155 if(protectflag & OB_LOCK_SCALEZ)
156 *drawflags &= ~MAN_SCALE_Z;
160 static void stats_pose(Scene *scene, View3D *v3d, bPoseChannel *pchan)
162 Bone *bone= pchan->bone;
165 if (bone->flag & BONE_TRANSFORM) {
166 calc_tw_center(scene, pchan->pose_head);
167 protectflag_to_drawflags(pchan->protectflag, &v3d->twdrawflag);
173 static void stats_editbone(View3D *v3d, EditBone *ebo)
175 if (ebo->flag & BONE_EDITMODE_LOCKED)
176 protectflag_to_drawflags(OB_LOCK_LOC|OB_LOCK_ROT|OB_LOCK_SCALE, &v3d->twdrawflag);
180 static int test_rotmode_euler(short rotmode)
182 return (ELEM(rotmode, ROT_MODE_AXISANGLE, ROT_MODE_QUAT)) ? 0:1;
185 int gimbal_axis(Object *ob, float gmat[][3])
188 if(ob->mode & OB_MODE_POSE)
190 bPoseChannel *pchan= get_active_posechannel(ob);
192 if(pchan && test_rotmode_euler(pchan->rotmode)) {
193 float mat[3][3], tmat[3][3], obmat[3][3];
195 eulO_to_gimbal_axis(mat, pchan->eul, pchan->rotmode);
197 /* apply bone transformation */
198 mul_m3_m3m3(tmat, pchan->bone->bone_mat, mat);
202 float parent_mat[3][3];
204 copy_m3_m4(parent_mat, pchan->parent->pose_mat);
205 mul_m3_m3m3(mat, parent_mat, tmat);
207 /* needed if object transformation isn't identity */
208 copy_m3_m4(obmat, ob->obmat);
209 mul_m3_m3m3(gmat, obmat, mat);
213 /* needed if object transformation isn't identity */
214 copy_m3_m4(obmat, ob->obmat);
215 mul_m3_m3m3(gmat, obmat, tmat);
223 if(test_rotmode_euler(ob->rotmode)) {
228 float parent_mat[3][3], amat[3][3];
230 eulO_to_gimbal_axis(amat, ob->rot, ob->rotmode);
231 copy_m3_m4(parent_mat, ob->parent->obmat);
232 normalize_m3(parent_mat);
233 mul_m3_m3m3(gmat, parent_mat, amat);
238 eulO_to_gimbal_axis(gmat, ob->rot, ob->rotmode);
249 /* centroid, boundbox, of selection */
250 /* returns total items selected */
251 int calc_manipulator_stats(const bContext *C)
253 ScrArea *sa= CTX_wm_area(C);
254 ARegion *ar= CTX_wm_region(C);
255 Scene *scene= CTX_data_scene(C);
256 Object *obedit= CTX_data_edit_object(C);
257 View3D *v3d= sa->spacedata.first;
258 RegionView3D *rv3d= ar->regiondata;
263 /* transform widget matrix */
264 unit_m4(rv3d->twmat);
266 v3d->twdrawflag= 0xFFFF;
268 /* transform widget centroid/center */
269 scene->twcent[0]= scene->twcent[1]= scene->twcent[2]= 0.0f;
270 INIT_MINMAX(scene->twmin, scene->twmax);
274 if((ob->lay & v3d->lay)==0) return 0;
276 if(obedit->type==OB_MESH) {
277 EditMesh *em = BKE_mesh_get_editmesh(obedit->data);
280 float vec[3]= {0,0,0};
282 /* USE LAST SELECTE WITH ACTIVE */
283 if (v3d->around==V3D_ACTIVE && EM_get_actSelection(em, &ese)) {
284 EM_editselection_center(vec, &ese);
285 calc_tw_center(scene, vec);
288 /* do vertices for center, and if still no normal found, use vertex normals */
289 for(eve= em->verts.first; eve; eve= eve->next) {
290 if(eve->f & SELECT) {
292 calc_tw_center(scene, eve->co);
297 else if (obedit->type==OB_ARMATURE){
298 bArmature *arm= obedit->data;
300 for (ebo= arm->edbo->first; ebo; ebo=ebo->next){
301 if(ebo->layer & arm->layer) {
302 if (ebo->flag & BONE_TIPSEL) {
303 calc_tw_center(scene, ebo->tail);
306 if (ebo->flag & BONE_ROOTSEL) {
307 calc_tw_center(scene, ebo->head);
310 if (ebo->flag & BONE_SELECTED) {
311 stats_editbone(v3d, ebo);
316 else if ELEM(obedit->type, OB_CURVE, OB_SURF) {
317 Curve *cu= obedit->data;
322 nu= cu->editnurb->first;
324 if(nu->type == CU_BEZIER) {
329 * if handles are hidden then only check the center points.
330 * If 2 or more are selected then only use the center point too.
332 if (cu->drawflag & CU_HIDE_HANDLES) {
333 if (bezt->f2 & SELECT) {
334 calc_tw_center(scene, bezt->vec[1]);
338 else if ( (bezt->f1 & SELECT) + (bezt->f2 & SELECT) + (bezt->f3 & SELECT) > SELECT ) {
339 calc_tw_center(scene, bezt->vec[1]);
344 calc_tw_center(scene, bezt->vec[0]);
348 calc_tw_center(scene, bezt->vec[1]);
352 calc_tw_center(scene, bezt->vec[2]);
361 a= nu->pntsu*nu->pntsv;
363 if(bp->f1 & SELECT) {
364 calc_tw_center(scene, bp->vec);
373 else if(obedit->type==OB_MBALL) {
374 MetaBall *mb = (MetaBall*)obedit->data;
375 MetaElem *ml, *ml_sel=NULL;
377 ml= mb->editelems->first;
379 if(ml->flag & SELECT) {
380 calc_tw_center(scene, &ml->x);
387 else if(obedit->type==OB_LATTICE) {
389 Lattice *lt= obedit->data;
391 bp= lt->editlatt->def;
393 a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw;
395 if(bp->f1 & SELECT) {
396 calc_tw_center(scene, bp->vec);
403 /* selection center */
405 mul_v3_fl(scene->twcent, 1.0f/(float)totsel); // centroid!
406 mul_m4_v3(obedit->obmat, scene->twcent);
407 mul_m4_v3(obedit->obmat, scene->twmin);
408 mul_m4_v3(obedit->obmat, scene->twmax);
411 else if(ob && (ob->mode & OB_MODE_POSE)) {
413 int mode = TFM_ROTATION; // mislead counting bones... bah. We don't know the manipulator mode, could be mixed
415 if((ob->lay & v3d->lay)==0) return 0;
417 totsel = count_set_pose_transflags(&mode, 0, ob);
420 /* use channels to get stats */
421 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
422 stats_pose(scene, v3d, pchan);
425 mul_v3_fl(scene->twcent, 1.0f/(float)totsel); // centroid!
426 mul_m4_v3(ob->obmat, scene->twcent);
427 mul_m4_v3(ob->obmat, scene->twmin);
428 mul_m4_v3(ob->obmat, scene->twmax);
431 else if(ob && (ob->mode & (OB_MODE_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT))) {
434 else if(ob && ob->mode & OB_MODE_PARTICLE_EDIT) {
435 PTCacheEdit *edit= PE_get_current(scene, ob);
436 PTCacheEditPoint *point;
441 point = edit->points;
442 for(a=0; a<edit->totpoint; a++,point++) {
443 if(point->flag & PEP_HIDE) continue;
445 for(k=0, ek=point->keys; k<point->totkey; k++, ek++) {
446 if(ek->flag & PEK_SELECT) {
447 calc_tw_center(scene, ek->flag & PEK_USE_WCO ? ek->world_co : ek->co);
453 /* selection center */
455 mul_v3_fl(scene->twcent, 1.0f/(float)totsel); // centroid!
460 /* we need the one selected object, if its not active */
462 if(ob && !(ob->flag & SELECT)) ob= NULL;
464 for(base= scene->base.first; base; base= base->next) {
465 if TESTBASELIB(scene, base) {
468 calc_tw_center(scene, base->object->obmat[3]);
469 protectflag_to_drawflags(base->object->protectflag, &v3d->twdrawflag);
474 /* selection center */
476 mul_v3_fl(scene->twcent, 1.0f/(float)totsel); // centroid!
480 /* global, local or normal orientation? */
483 switch(v3d->twmode) {
485 case V3D_MANIP_GLOBAL:
486 break; /* nothing to do */
488 case V3D_MANIP_GIMBAL:
491 if (gimbal_axis(ob, mat)) {
492 copy_m4_m3(rv3d->twmat, mat);
495 /* if not gimbal, fall through to normal */
497 case V3D_MANIP_NORMAL:
498 if(obedit || ob->mode & OB_MODE_POSE) {
500 ED_getTransformOrientationMatrix(C, mat, (v3d->around == V3D_ACTIVE));
501 copy_m4_m3(rv3d->twmat, mat);
504 /* no break we define 'normal' as 'local' in Object mode */
505 case V3D_MANIP_LOCAL:
506 copy_m4_m4(rv3d->twmat, ob->obmat);
507 normalize_m4(rv3d->twmat);
513 copy_m3_m4(mat, rv3d->viewinv);
515 copy_m4_m3(rv3d->twmat, mat);
518 default: /* V3D_MANIP_CUSTOM */
521 applyTransformOrientation(C, mat, NULL);
522 copy_m4_m3(rv3d->twmat, mat);
532 /* ******************** DRAWING STUFFIES *********** */
534 static float screen_aligned(RegionView3D *rv3d, float mat[][4])
538 VECCOPY(vec, mat[0]);
539 size= normalize_v3(vec);
541 glTranslatef(mat[3][0], mat[3][1], mat[3][2]);
543 /* sets view screen aligned */
544 glRotatef( -360.0f*saacos(rv3d->viewquat[0])/(float)M_PI, rv3d->viewquat[1], rv3d->viewquat[2], rv3d->viewquat[3]);
550 /* radring = radius of donut rings
551 radhole = radius hole
552 start = starting segment (based on nrings)
554 nsides = amount of points in ring
555 nrigns = amount of rings
557 static void partial_donut(float radring, float radhole, int start, int end, int nsides, int nrings)
559 float theta, phi, theta1;
560 float cos_theta, sin_theta;
561 float cos_theta1, sin_theta1;
562 float ring_delta, side_delta;
565 if(start==0 && end==nrings) docaps= 0;
567 ring_delta= 2.0f*(float)M_PI/(float)nrings;
568 side_delta= 2.0f*(float)M_PI/(float)nsides;
570 theta= (float)M_PI+0.5f*ring_delta;
571 cos_theta= (float)cos(theta);
572 sin_theta= (float)sin(theta);
574 for(i= nrings - 1; i >= 0; i--) {
575 theta1= theta + ring_delta;
576 cos_theta1= (float)cos(theta1);
577 sin_theta1= (float)sin(theta1);
579 if(docaps && i==start) { // cap
582 for(j= nsides; j >= 0; j--) {
583 float cos_phi, sin_phi, dist;
586 cos_phi= (float)cos(phi);
587 sin_phi= (float)sin(phi);
588 dist= radhole + radring * cos_phi;
590 glVertex3f(cos_theta1 * dist, -sin_theta1 * dist, radring * sin_phi);
594 if(i>=start && i<=end) {
595 glBegin(GL_QUAD_STRIP);
597 for(j= nsides; j >= 0; j--) {
598 float cos_phi, sin_phi, dist;
601 cos_phi= (float)cos(phi);
602 sin_phi= (float)sin(phi);
603 dist= radhole + radring * cos_phi;
605 glVertex3f(cos_theta1 * dist, -sin_theta1 * dist, radring * sin_phi);
606 glVertex3f(cos_theta * dist, -sin_theta * dist, radring * sin_phi);
611 if(docaps && i==end) { // cap
614 for(j= nsides; j >= 0; j--) {
615 float cos_phi, sin_phi, dist;
618 cos_phi= (float)cos(phi);
619 sin_phi= (float)sin(phi);
620 dist= radhole + radring * cos_phi;
622 glVertex3f(cos_theta * dist, -sin_theta * dist, radring * sin_phi);
629 cos_theta= cos_theta1;
630 sin_theta= sin_theta1;
634 /* three colors can be set;
636 moving: in transform theme color
637 else the red/green/blue
639 static void manipulator_setcolor(View3D *v3d, char axis, int colcode)
644 vec[3]= 0.7f; // alpha set on 0.5, can be glEnabled or not
646 if(colcode==MAN_GHOST) {
647 glColor4ub(0, 0, 0, 70);
649 else if(colcode==MAN_MOVECOL) {
650 UI_GetThemeColor3ubv(TH_TRANSFORM, col);
651 glColor4ub(col[0], col[1], col[2], 128);
656 UI_GetThemeColor3ubv(TH_TRANSFORM, col);
657 if(v3d->twmode == V3D_MANIP_LOCAL) {
658 col[0]= col[0]>200?255:col[0]+55;
659 col[1]= col[1]>200?255:col[1]+55;
660 col[2]= col[2]>200?255:col[2]+55;
662 else if(v3d->twmode == V3D_MANIP_NORMAL) {
663 col[0]= col[0]<55?0:col[0]-55;
664 col[1]= col[1]<55?0:col[1]-55;
665 col[2]= col[2]<55?0:col[2]-55;
667 glColor4ub(col[0], col[1], col[2], 128);
670 glColor4ub(220, 0, 0, 128);
673 glColor4ub(0, 220, 0, 128);
676 glColor4ub(30, 30, 220, 128);
682 /* viewmatrix should have been set OK, also no shademode! */
683 static void draw_manipulator_axes(View3D *v3d, int colcode, int flagx, int flagy, int flagz)
688 manipulator_setcolor(v3d, 'x', colcode);
689 if(flagx & MAN_SCALE_X) glLoadName(MAN_SCALE_X);
690 else if(flagx & MAN_TRANS_X) glLoadName(MAN_TRANS_X);
692 glVertex3f(0.2f, 0.0f, 0.0f);
693 glVertex3f(1.0f, 0.0f, 0.0f);
697 if(flagy & MAN_SCALE_Y) glLoadName(MAN_SCALE_Y);
698 else if(flagy & MAN_TRANS_Y) glLoadName(MAN_TRANS_Y);
699 manipulator_setcolor(v3d, 'y', colcode);
701 glVertex3f(0.0f, 0.2f, 0.0f);
702 glVertex3f(0.0f, 1.0f, 0.0f);
706 if(flagz & MAN_SCALE_Z) glLoadName(MAN_SCALE_Z);
707 else if(flagz & MAN_TRANS_Z) glLoadName(MAN_TRANS_Z);
708 manipulator_setcolor(v3d, 'z', colcode);
710 glVertex3f(0.0f, 0.0f, 0.2f);
711 glVertex3f(0.0f, 0.0f, 1.0f);
716 static void preOrtho(int ortho, float twmat[][4], int axis)
720 copy_m4_m4(omat, twmat);
721 orthogonalize_m4(omat, axis);
727 static void preOrthoFront(int ortho, float twmat[][4], int axis)
731 copy_m4_m4(omat, twmat);
732 orthogonalize_m4(omat, axis);
735 glFrontFace( is_mat4_flipped(omat)?GL_CW:GL_CCW);
739 static void postOrtho(int ortho)
746 /* only called while G.moving */
747 static void draw_manipulator_rotate_ghost(View3D *v3d, RegionView3D *rv3d, int drawflags)
750 float size, phi, startphi, vec[3], svec[3], matt[4][4], cross[3], tmat[3][3];
754 glDisable(GL_DEPTH_TEST);
756 qobj= gluNewQuadric();
757 gluQuadricDrawStyle(qobj, GLU_FILL);
759 glColor4ub(0,0,0,64);
760 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
763 /* we need both [4][4] transforms, t->mat seems to be premul, not post for mat[][4] */
764 copy_m4_m4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
765 // XXX mul_m4_m3m4(matt, t->mat, rv3d->twmat);
767 /* Screen aligned view rot circle */
768 if(drawflags & MAN_ROT_V) {
770 /* prepare for screen aligned draw */
772 size= screen_aligned(rv3d, rv3d->twmat);
774 vec[0]= 0; // XXX (float)(t->con.imval[0] - t->center2d[0]);
775 vec[1]= 0; // XXX (float)(t->con.imval[1] - t->center2d[1]);
779 startphi= saacos( vec[1] );
780 if(vec[0]<0.0) startphi= -startphi;
782 phi= 0; // XXX (float)fmod(180.0*t->val/M_PI, 360.0);
783 if(phi > 180.0) phi-= 360.0;
784 else if(phi<-180.0) phi+= 360.0;
786 gluPartialDisk(qobj, 0.0, size, 32, 1, 180.0*startphi/M_PI, phi);
791 float imat[3][3], ivmat[3][3];
792 /* try to get the start rotation */
794 svec[0]= 0; // XXX (float)(t->con.imval[0] - t->center2d[0]);
795 svec[1]= 0; // XXX (float)(t->con.imval[1] - t->center2d[1]);
798 /* screen aligned vec transform back to manipulator space */
799 copy_m3_m4(ivmat, rv3d->viewinv);
800 copy_m3_m4(tmat, rv3d->twmat);
801 invert_m3_m3(imat, tmat);
802 mul_m3_m3m3(tmat, imat, ivmat);
804 mul_m3_v3(tmat, svec); // tmat is used further on
808 ortho = is_orthogonal_m4(rv3d->twmat);
811 wmMultMatrix(rv3d->twmat); // aligns with original widget
816 if(drawflags & MAN_ROT_Z) {
817 preOrtho(ortho, rv3d->twmat, 2);
820 /* correct for squeezed arc */
821 svec[0]+= tmat[2][0];
822 svec[1]+= tmat[2][1];
825 startphi= (float)atan2(svec[0], svec[1]);
827 else startphi= 0.5f*(float)M_PI;
829 VECCOPY(vec, rv3d->twmat[0]); // use x axis to detect rotation
831 normalize_v3(matt[0]);
832 phi= saacos( dot_v3v3(vec, matt[0]) );
834 cross_v3_v3v3(cross, vec, matt[0]); // results in z vector
835 if(dot_v3v3(cross, rv3d->twmat[2]) > 0.0) phi= -phi;
836 gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*(phi)/M_PI);
842 if(drawflags & MAN_ROT_X) {
843 preOrtho(ortho, rv3d->twmat, 0);
846 /* correct for squeezed arc */
847 svec[1]+= tmat[2][1];
848 svec[2]+= tmat[2][2];
851 startphi= (float)(M_PI + atan2(svec[2], -svec[1]));
855 VECCOPY(vec, rv3d->twmat[1]); // use y axis to detect rotation
857 normalize_v3(matt[1]);
858 phi= saacos( dot_v3v3(vec, matt[1]) );
860 cross_v3_v3v3(cross, vec, matt[1]); // results in x vector
861 if(dot_v3v3(cross, rv3d->twmat[0]) > 0.0) phi= -phi;
862 glRotatef(90.0, 0.0, 1.0, 0.0);
863 gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*phi/M_PI);
864 glRotatef(-90.0, 0.0, 1.0, 0.0);
870 if(drawflags & MAN_ROT_Y) {
871 preOrtho(ortho, rv3d->twmat, 1);
874 /* correct for squeezed arc */
875 svec[0]+= tmat[2][0];
876 svec[2]+= tmat[2][2];
879 startphi= (float)(M_PI + atan2(-svec[0], svec[2]));
881 else startphi= (float)M_PI;
883 VECCOPY(vec, rv3d->twmat[2]); // use z axis to detect rotation
885 normalize_v3(matt[2]);
886 phi= saacos( dot_v3v3(vec, matt[2]) );
888 cross_v3_v3v3(cross, vec, matt[2]); // results in y vector
889 if(dot_v3v3(cross, rv3d->twmat[1]) > 0.0) phi= -phi;
890 glRotatef(-90.0, 1.0, 0.0, 0.0);
891 gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*phi/M_PI);
892 glRotatef(90.0, 1.0, 0.0, 0.0);
899 wmLoadMatrix(rv3d->viewmat);
902 static void draw_manipulator_rotate(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo)
907 float size, vec[3], unitmat[4][4];
908 float cywid= 0.33f*0.01f*(float)U.tw_handlesize;
909 float cusize= cywid*0.65f;
914 if(moving) colcode= MAN_MOVECOL;
915 else colcode= MAN_RGB;
917 /* when called while moving in mixed mode, do not draw when... */
918 if((drawflags & MAN_ROT_C)==0) return;
921 glDisable(GL_DEPTH_TEST);
924 qobj= gluNewQuadric();
925 gluQuadricDrawStyle(qobj, GLU_FILL);
927 /* prepare for screen aligned draw */
928 VECCOPY(vec, rv3d->twmat[0]);
929 size= normalize_v3(vec);
931 glTranslatef(rv3d->twmat[3][0], rv3d->twmat[3][1], rv3d->twmat[3][2]);
934 /* clipplane makes nice handles, calc here because of multmatrix but with translate! */
935 VECCOPY(plane, rv3d->viewinv[2]);
936 plane[3]= -0.02*size; // clip just a bit more
937 glClipPlane(GL_CLIP_PLANE0, plane);
939 /* sets view screen aligned */
940 glRotatef( -360.0f*saacos(rv3d->viewquat[0])/(float)M_PI, rv3d->viewquat[1], rv3d->viewquat[2], rv3d->viewquat[3]);
942 /* Screen aligned help circle */
944 if((G.f & G_PICKSEL)==0) {
945 UI_ThemeColorShade(TH_BACK, -30);
946 drawcircball(GL_LINE_LOOP, unitmat[3], size, unitmat);
950 /* Screen aligned trackball rot circle */
951 if(drawflags & MAN_ROT_T) {
952 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_T);
954 UI_ThemeColor(TH_TRANSFORM);
955 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
958 /* Screen aligned view rot circle */
959 if(drawflags & MAN_ROT_V) {
960 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
961 UI_ThemeColor(TH_TRANSFORM);
962 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
966 vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
967 vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
970 mul_v3_fl(vec, 1.2f*size);
972 glVertex3f(0.0f, 0.0f, 0.0f);
980 ortho = is_orthogonal_m4(rv3d->twmat);
982 /* apply the transform delta */
984 copy_m4_m4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
985 // XXX mul_m4_m3m4(matt, t->mat, rv3d->twmat);
988 glFrontFace( is_mat4_flipped(matt)?GL_CW:GL_CCW);
993 glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
994 wmMultMatrix(rv3d->twmat);
1000 if(!(G.f & G_PICKSEL)) {
1001 if( (combo & V3D_MANIP_SCALE)==0) {
1003 if( (drawflags & MAN_ROT_X) || (moving && (drawflags & MAN_ROT_Z)) ) {
1004 preOrthoFront(ortho, rv3d->twmat, 2);
1005 manipulator_setcolor(v3d, 'x', colcode);
1007 glVertex3f(0.2f, 0.0f, 0.0f);
1008 glVertex3f(1.0f, 0.0f, 0.0f);
1012 if( (drawflags & MAN_ROT_Y) || (moving && (drawflags & MAN_ROT_X)) ) {
1013 preOrthoFront(ortho, rv3d->twmat, 0);
1014 manipulator_setcolor(v3d, 'y', colcode);
1016 glVertex3f(0.0f, 0.2f, 0.0f);
1017 glVertex3f(0.0f, 1.0f, 0.0f);
1021 if( (drawflags & MAN_ROT_Z) || (moving && (drawflags & MAN_ROT_Y)) ) {
1022 preOrthoFront(ortho, rv3d->twmat, 1);
1023 manipulator_setcolor(v3d, 'z', colcode);
1025 glVertex3f(0.0f, 0.0f, 0.2f);
1026 glVertex3f(0.0f, 0.0f, 1.0f);
1034 if(arcs==0 && moving) {
1037 if(drawflags & MAN_ROT_Z) {
1038 preOrthoFront(ortho, matt, 2);
1039 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1040 manipulator_setcolor(v3d, 'z', colcode);
1041 drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1045 if(drawflags & MAN_ROT_X) {
1046 preOrthoFront(ortho, matt, 0);
1047 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1048 glRotatef(90.0, 0.0, 1.0, 0.0);
1049 manipulator_setcolor(v3d, 'x', colcode);
1050 drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1051 glRotatef(-90.0, 0.0, 1.0, 0.0);
1055 if(drawflags & MAN_ROT_Y) {
1056 preOrthoFront(ortho, matt, 1);
1057 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1058 glRotatef(-90.0, 1.0, 0.0, 0.0);
1059 manipulator_setcolor(v3d, 'y', colcode);
1060 drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1061 glRotatef(90.0, 1.0, 0.0, 0.0);
1065 if(arcs) glDisable(GL_CLIP_PLANE0);
1069 glEnable(GL_CLIP_PLANE0);
1072 if(drawflags & MAN_ROT_Z) {
1073 preOrthoFront(ortho, rv3d->twmat, 2);
1074 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1075 manipulator_setcolor(v3d, 'z', colcode);
1076 partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1080 if(drawflags & MAN_ROT_X) {
1081 preOrthoFront(ortho, rv3d->twmat, 0);
1082 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1083 glRotatef(90.0, 0.0, 1.0, 0.0);
1084 manipulator_setcolor(v3d, 'x', colcode);
1085 partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1086 glRotatef(-90.0, 0.0, 1.0, 0.0);
1090 if(drawflags & MAN_ROT_Y) {
1091 preOrthoFront(ortho, rv3d->twmat, 1);
1092 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1093 glRotatef(-90.0, 1.0, 0.0, 0.0);
1094 manipulator_setcolor(v3d, 'y', colcode);
1095 partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1096 glRotatef(90.0, 1.0, 0.0, 0.0);
1100 glDisable(GL_CLIP_PLANE0);
1105 /* Z handle on X axis */
1106 if(drawflags & MAN_ROT_Z) {
1107 preOrthoFront(ortho, rv3d->twmat, 2);
1109 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1110 manipulator_setcolor(v3d, 'z', colcode);
1112 partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1118 /* Y handle on X axis */
1119 if(drawflags & MAN_ROT_Y) {
1120 preOrthoFront(ortho, rv3d->twmat, 1);
1122 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1123 manipulator_setcolor(v3d, 'y', colcode);
1125 glRotatef(90.0, 1.0, 0.0, 0.0);
1126 glRotatef(90.0, 0.0, 0.0, 1.0);
1127 partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1133 /* X handle on Z axis */
1134 if(drawflags & MAN_ROT_X) {
1135 preOrthoFront(ortho, rv3d->twmat, 0);
1137 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1138 manipulator_setcolor(v3d, 'x', colcode);
1140 glRotatef(-90.0, 0.0, 1.0, 0.0);
1141 glRotatef(90.0, 0.0, 0.0, 1.0);
1142 partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1151 wmLoadMatrix(rv3d->viewmat);
1152 gluDeleteQuadric(qobj);
1153 if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1157 static void drawsolidcube(float size)
1159 static float cube[8][3] = {
1167 { 1.0, 1.0, -1.0}, };
1171 glScalef(size, size, size);
1173 n[0]=0; n[1]=0; n[2]=0;
1177 glVertex3fv(cube[0]); glVertex3fv(cube[1]); glVertex3fv(cube[2]); glVertex3fv(cube[3]);
1184 glVertex3fv(cube[0]); glVertex3fv(cube[4]); glVertex3fv(cube[5]); glVertex3fv(cube[1]);
1191 glVertex3fv(cube[4]); glVertex3fv(cube[7]); glVertex3fv(cube[6]); glVertex3fv(cube[5]);
1198 glVertex3fv(cube[7]); glVertex3fv(cube[3]); glVertex3fv(cube[2]); glVertex3fv(cube[6]);
1205 glVertex3fv(cube[1]); glVertex3fv(cube[5]); glVertex3fv(cube[6]); glVertex3fv(cube[2]);
1212 glVertex3fv(cube[7]); glVertex3fv(cube[4]); glVertex3fv(cube[0]); glVertex3fv(cube[3]);
1219 static void draw_manipulator_scale(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1221 float cywid= 0.25f*0.01f*(float)U.tw_handlesize;
1222 float cusize= cywid*0.75f, dz;
1224 /* when called while moving in mixed mode, do not draw when... */
1225 if((drawflags & MAN_SCALE_C)==0) return;
1227 glDisable(GL_DEPTH_TEST);
1229 /* not in combo mode */
1230 if( (combo & (V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE))==0) {
1231 float size, unitmat[4][4];
1232 int shift= 0; // XXX
1234 /* center circle, do not add to selection when shift is pressed (planar constraint) */
1235 if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_SCALE_C);
1237 manipulator_setcolor(v3d, 'c', colcode);
1239 size= screen_aligned(rv3d, rv3d->twmat);
1241 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1246 else dz= 1.0f-4.0f*cusize;
1251 copy_m4_m4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1252 // XXX mul_m4_m3m4(matt, t->mat, rv3d->twmat);
1254 glFrontFace( is_mat4_flipped(matt)?GL_CW:GL_CCW);
1257 wmMultMatrix(rv3d->twmat);
1258 glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1263 /* in combo mode, this is always drawn as first type */
1264 draw_manipulator_axes(v3d, colcode, drawflags & MAN_SCALE_X, drawflags & MAN_SCALE_Y, drawflags & MAN_SCALE_Z);
1267 glTranslatef(0.0, 0.0, dz);
1268 if(drawflags & MAN_SCALE_Z) {
1269 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Z);
1270 manipulator_setcolor(v3d, 'z', colcode);
1271 drawsolidcube(cusize);
1274 glTranslatef(dz, 0.0, -dz);
1275 if(drawflags & MAN_SCALE_X) {
1276 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_X);
1277 manipulator_setcolor(v3d, 'x', colcode);
1278 drawsolidcube(cusize);
1281 glTranslatef(-dz, dz, 0.0);
1282 if(drawflags & MAN_SCALE_Y) {
1283 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Y);
1284 manipulator_setcolor(v3d, 'y', colcode);
1285 drawsolidcube(cusize);
1288 /* if shiftkey, center point as last, for selectbuffer order */
1289 if(G.f & G_PICKSEL) {
1290 int shift= 0; // XXX
1293 glTranslatef(0.0, -dz, 0.0);
1294 glLoadName(MAN_SCALE_C);
1296 glVertex3f(0.0, 0.0, 0.0);
1302 wmLoadMatrix(rv3d->viewmat);
1304 if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1305 glFrontFace(GL_CCW);
1309 static void draw_cone(GLUquadricObj *qobj, float len, float width)
1311 glTranslatef(0.0, 0.0, -0.5f*len);
1312 gluCylinder(qobj, width, 0.0, len, 8, 1);
1313 gluQuadricOrientation(qobj, GLU_INSIDE);
1314 gluDisk(qobj, 0.0, width, 8, 1);
1315 gluQuadricOrientation(qobj, GLU_OUTSIDE);
1316 glTranslatef(0.0, 0.0, 0.5f*len);
1319 static void draw_cylinder(GLUquadricObj *qobj, float len, float width)
1322 width*= 0.8f; // just for beauty
1324 glTranslatef(0.0, 0.0, -0.5f*len);
1325 gluCylinder(qobj, width, width, len, 8, 1);
1326 gluQuadricOrientation(qobj, GLU_INSIDE);
1327 gluDisk(qobj, 0.0, width, 8, 1);
1328 gluQuadricOrientation(qobj, GLU_OUTSIDE);
1329 glTranslatef(0.0, 0.0, len);
1330 gluDisk(qobj, 0.0, width, 8, 1);
1331 glTranslatef(0.0, 0.0, -0.5f*len);
1335 static void draw_manipulator_translate(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1337 GLUquadricObj *qobj;
1338 float cylen= 0.01f*(float)U.tw_handlesize;
1339 float cywid= 0.25f*cylen, dz, size;
1340 float unitmat[4][4];
1341 int shift= 0; // XXX
1343 /* when called while moving in mixed mode, do not draw when... */
1344 if((drawflags & MAN_TRANS_C)==0) return;
1346 // XXX if(moving) glTranslatef(t->vec[0], t->vec[1], t->vec[2]);
1347 glDisable(GL_DEPTH_TEST);
1349 qobj= gluNewQuadric();
1350 gluQuadricDrawStyle(qobj, GLU_FILL);
1352 /* center circle, do not add to selection when shift is pressed (planar constraint) */
1353 if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_TRANS_C);
1355 manipulator_setcolor(v3d, 'c', colcode);
1357 size= screen_aligned(rv3d, rv3d->twmat);
1359 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1362 /* and now apply matrix, we move to local matrix drawing */
1363 wmMultMatrix(rv3d->twmat);
1368 // translate drawn as last, only axis when no combo with scale, or for ghosting
1369 if((combo & V3D_MANIP_SCALE)==0 || colcode==MAN_GHOST)
1370 draw_manipulator_axes(v3d, colcode, drawflags & MAN_TRANS_X, drawflags & MAN_TRANS_Y, drawflags & MAN_TRANS_Z);
1373 /* offset in combo mode, for rotate a bit more */
1374 if(combo & (V3D_MANIP_ROTATE)) dz= 1.0f+2.0f*cylen;
1375 else if(combo & (V3D_MANIP_SCALE)) dz= 1.0f+0.5f*cylen;
1379 glTranslatef(0.0, 0.0, dz);
1380 if(drawflags & MAN_TRANS_Z) {
1381 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Z);
1382 manipulator_setcolor(v3d, 'z', colcode);
1383 draw_cone(qobj, cylen, cywid);
1386 glTranslatef(dz, 0.0, -dz);
1387 if(drawflags & MAN_TRANS_X) {
1388 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_X);
1389 glRotatef(90.0, 0.0, 1.0, 0.0);
1390 manipulator_setcolor(v3d, 'x', colcode);
1391 draw_cone(qobj, cylen, cywid);
1392 glRotatef(-90.0, 0.0, 1.0, 0.0);
1395 glTranslatef(-dz, dz, 0.0);
1396 if(drawflags & MAN_TRANS_Y) {
1397 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Y);
1398 glRotatef(-90.0, 1.0, 0.0, 0.0);
1399 manipulator_setcolor(v3d, 'y', colcode);
1400 draw_cone(qobj, cylen, cywid);
1403 gluDeleteQuadric(qobj);
1404 wmLoadMatrix(rv3d->viewmat);
1406 if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1410 static void draw_manipulator_rotate_cyl(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1412 GLUquadricObj *qobj;
1414 float cylen= 0.01f*(float)U.tw_handlesize;
1415 float cywid= 0.25f*cylen;
1417 /* when called while moving in mixed mode, do not draw when... */
1418 if((drawflags & MAN_ROT_C)==0) return;
1420 /* prepare for screen aligned draw */
1422 size= screen_aligned(rv3d, rv3d->twmat);
1424 glDisable(GL_DEPTH_TEST);
1426 qobj= gluNewQuadric();
1428 /* Screen aligned view rot circle */
1429 if(drawflags & MAN_ROT_V) {
1430 float unitmat[4][4];
1433 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
1434 UI_ThemeColor(TH_TRANSFORM);
1435 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
1439 vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
1440 vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
1443 mul_v3_fl(vec, 1.2f*size);
1445 glVertex3f(0.0, 0.0, 0.0);
1452 /* apply the transform delta */
1455 copy_m4_m4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1456 // XXX if (t->flag & T_USES_MANIPULATOR) {
1457 // XXX mul_m4_m3m4(matt, t->mat, rv3d->twmat);
1462 wmMultMatrix(rv3d->twmat);
1465 glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1468 if( (G.f & G_PICKSEL)==0 ) {
1470 // only draw axis when combo didn't draw scale axes
1471 if((combo & V3D_MANIP_SCALE)==0)
1472 draw_manipulator_axes(v3d, colcode, drawflags & MAN_ROT_X, drawflags & MAN_ROT_Y, drawflags & MAN_ROT_Z);
1474 /* only has to be set when not in picking */
1475 gluQuadricDrawStyle(qobj, GLU_FILL);
1479 glTranslatef(0.0, 0.0, 1.0);
1480 if(drawflags & MAN_ROT_Z) {
1481 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1482 manipulator_setcolor(v3d, 'z', colcode);
1483 draw_cylinder(qobj, cylen, cywid);
1486 glTranslatef(1.0, 0.0, -1.0);
1487 if(drawflags & MAN_ROT_X) {
1488 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1489 glRotatef(90.0, 0.0, 1.0, 0.0);
1490 manipulator_setcolor(v3d, 'x', colcode);
1491 draw_cylinder(qobj, cylen, cywid);
1492 glRotatef(-90.0, 0.0, 1.0, 0.0);
1495 glTranslatef(-1.0, 1.0, 0.0);
1496 if(drawflags & MAN_ROT_Y) {
1497 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1498 glRotatef(-90.0, 1.0, 0.0, 0.0);
1499 manipulator_setcolor(v3d, 'y', colcode);
1500 draw_cylinder(qobj, cylen, cywid);
1505 gluDeleteQuadric(qobj);
1506 wmLoadMatrix(rv3d->viewmat);
1508 if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1513 /* ********************************************* */
1515 static float get_manipulator_drawsize(ARegion *ar)
1517 RegionView3D *rv3d= ar->regiondata;
1518 float size = get_drawsize(ar, rv3d->twmat[3]);
1520 size*= (float)U.tw_size;
1526 /* main call, does calc centers & orientation too */
1527 /* uses global G.moving */
1528 static int drawflags= 0xFFFF; // only for the calls below, belongs in scene...?
1530 void BIF_draw_manipulator(const bContext *C)
1532 ScrArea *sa= CTX_wm_area(C);
1533 ARegion *ar= CTX_wm_region(C);
1534 Scene *scene= CTX_data_scene(C);
1535 View3D *v3d= sa->spacedata.first;
1536 RegionView3D *rv3d= ar->regiondata;
1539 if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return;
1540 // if(G.moving && (G.moving & G_TRANSFORM_MANIP)==0) return;
1542 // if(G.moving==0) {
1544 v3d->twflag &= ~V3D_DRAW_MANIPULATOR;
1546 totsel= calc_manipulator_stats(C);
1547 if(totsel==0) return;
1548 drawflags= v3d->twdrawflag; /* set in calc_manipulator_stats */
1550 v3d->twflag |= V3D_DRAW_MANIPULATOR;
1552 /* now we can define center */
1553 switch(v3d->around) {
1556 rv3d->twmat[3][0]= (scene->twmin[0] + scene->twmax[0])/2.0f;
1557 rv3d->twmat[3][1]= (scene->twmin[1] + scene->twmax[1])/2.0f;
1558 rv3d->twmat[3][2]= (scene->twmin[2] + scene->twmax[2])/2.0f;
1559 if(v3d->around==V3D_ACTIVE && scene->obedit==NULL) {
1561 if(ob && !(ob->mode & OB_MODE_POSE))
1562 VECCOPY(rv3d->twmat[3], ob->obmat[3]);
1567 VECCOPY(rv3d->twmat[3], scene->twcent);
1570 VECCOPY(rv3d->twmat[3], give_cursor(scene, v3d));
1574 mul_mat3_m4_fl(rv3d->twmat, get_manipulator_drawsize(ar));
1577 if(v3d->twflag & V3D_DRAW_MANIPULATOR) {
1579 if(v3d->twtype & V3D_MANIP_ROTATE) {
1581 /* rotate has special ghosting draw, for pie chart */
1582 if(G.moving) draw_manipulator_rotate_ghost(v3d, rv3d, drawflags);
1584 if(G.moving) glEnable(GL_BLEND);
1587 if(G.moving) draw_manipulator_rotate_cyl(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1588 else draw_manipulator_rotate_cyl(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1591 draw_manipulator_rotate(v3d, rv3d, 0 /* G.moving*/, drawflags, v3d->twtype);
1593 glDisable(GL_BLEND);
1595 if(v3d->twtype & V3D_MANIP_SCALE) {
1598 draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_GHOST);
1599 draw_manipulator_scale(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1600 glDisable(GL_BLEND);
1602 else draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1604 if(v3d->twtype & V3D_MANIP_TRANSLATE) {
1607 draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_GHOST);
1608 draw_manipulator_translate(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1609 glDisable(GL_BLEND);
1611 else draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1616 static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, short *mval, float hotspot)
1618 View3D *v3d= sa->spacedata.first;
1619 RegionView3D *rv3d= ar->regiondata;
1621 GLuint buffer[64]; // max 4 items per select, so large enuf
1623 extern void setwinmatrixview3d(ARegion *ar, View3D *v3d, rctf *rect); // XXX check a bit later on this... (ton)
1627 rect.xmin= mval[0]-hotspot;
1628 rect.xmax= mval[0]+hotspot;
1629 rect.ymin= mval[1]-hotspot;
1630 rect.ymax= mval[1]+hotspot;
1632 setwinmatrixview3d(ar, v3d, &rect);
1633 mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1635 glSelectBuffer( 64, buffer);
1636 glRenderMode(GL_SELECT);
1637 glInitNames(); /* these two calls whatfor? It doesnt work otherwise */
1640 /* do the drawing */
1641 if(v3d->twtype & V3D_MANIP_ROTATE) {
1642 if(G.rt==3) draw_manipulator_rotate_cyl(v3d, rv3d, 0, MAN_ROT_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1643 else draw_manipulator_rotate(v3d, rv3d, 0, MAN_ROT_C & v3d->twdrawflag, v3d->twtype);
1645 if(v3d->twtype & V3D_MANIP_SCALE)
1646 draw_manipulator_scale(v3d, rv3d, 0, MAN_SCALE_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1647 if(v3d->twtype & V3D_MANIP_TRANSLATE)
1648 draw_manipulator_translate(v3d, rv3d, 0, MAN_TRANS_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1651 hits= glRenderMode(GL_RENDER);
1654 setwinmatrixview3d(ar, v3d, NULL);
1655 mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1657 if(hits==1) return buffer[3];
1659 GLuint val, dep, mindep=0, mindeprot=0, minval=0, minvalrot=0;
1662 /* we compare the hits in buffer, but value centers highest */
1663 /* we also store the rotation hits separate (because of arcs) and return hits on other widgets if there are */
1665 for(a=0; a<hits; a++) {
1666 dep= buffer[4*a + 1];
1667 val= buffer[4*a + 3];
1669 if(val==MAN_TRANS_C) return MAN_TRANS_C;
1670 else if(val==MAN_SCALE_C) return MAN_SCALE_C;
1672 if(val & MAN_ROT_C) {
1673 if(minvalrot==0 || dep<mindeprot) {
1679 if(minval==0 || dep<mindep) {
1695 /* return 0; nothing happened */
1696 int BIF_do_manipulator(bContext *C, struct wmEvent *event, wmOperator *op)
1698 ScrArea *sa= CTX_wm_area(C);
1699 View3D *v3d= sa->spacedata.first;
1700 ARegion *ar= CTX_wm_region(C);
1701 int constraint_axis[3] = {0, 0, 0};
1703 int shift = event->shift;
1705 if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return 0;
1706 if(!(v3d->twflag & V3D_DRAW_MANIPULATOR)) return 0;
1708 // find the hotspots first test narrow hotspot
1709 val= manipulator_selectbuf(sa, ar, event->mval, 0.5f*(float)U.tw_hotspot);
1712 // drawflags still global, for drawing call above
1713 drawflags= manipulator_selectbuf(sa, ar, event->mval, 0.2f*(float)U.tw_hotspot);
1714 if(drawflags==0) drawflags= val;
1716 if (drawflags & MAN_TRANS_C) {
1722 constraint_axis[1] = 1;
1723 constraint_axis[2] = 1;
1726 constraint_axis[0] = 1;
1730 constraint_axis[0] = 1;
1731 constraint_axis[2] = 1;
1734 constraint_axis[1] = 1;
1738 constraint_axis[0] = 1;
1739 constraint_axis[1] = 1;
1742 constraint_axis[2] = 1;
1745 RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1746 WM_operator_name_call(C, "TRANSFORM_OT_translate", WM_OP_INVOKE_DEFAULT, op->ptr);
1748 else if (drawflags & MAN_SCALE_C) {
1752 constraint_axis[1] = 1;
1753 constraint_axis[2] = 1;
1756 constraint_axis[0] = 1;
1760 constraint_axis[0] = 1;
1761 constraint_axis[2] = 1;
1764 constraint_axis[1] = 1;
1768 constraint_axis[0] = 1;
1769 constraint_axis[1] = 1;
1772 constraint_axis[2] = 1;
1775 RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1776 WM_operator_name_call(C, "TRANSFORM_OT_resize", WM_OP_INVOKE_DEFAULT, op->ptr);
1778 else if (drawflags == MAN_ROT_T) { /* trackball need special case, init is different */
1779 WM_operator_name_call(C, "TRANSFORM_OT_trackball", WM_OP_INVOKE_DEFAULT, op->ptr);
1781 else if (drawflags & MAN_ROT_C) {
1784 constraint_axis[0] = 1;
1787 constraint_axis[1] = 1;
1790 constraint_axis[2] = 1;
1793 RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1794 WM_operator_name_call(C, "TRANSFORM_OT_rotate", WM_OP_INVOKE_DEFAULT, op->ptr);
1797 /* after transform, restore drawflags */