4 * ***** BEGIN GPL/BL DUAL 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. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
36 #include "MEM_guardedalloc.h"
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_lattice_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_view3d_types.h"
44 #include "DNA_userdef_types.h"
45 #include "DNA_constraint_types.h"
47 #include "BIF_screen.h"
48 #include "BIF_resources.h"
49 #include "BIF_mywindow.h"
51 #include "BIF_editarmature.h"
52 #include "BIF_editmesh.h"
54 #include "BKE_action.h"
56 #include "BKE_armature.h"
57 #include "BKE_curve.h"
58 #include "BKE_depsgraph.h"
59 #include "BKE_displist.h"
60 #include "BKE_depsgraph.h"
61 #include "BKE_global.h"
63 #include "BKE_lattice.h"
64 #include "BKE_object.h"
65 #include "BKE_utildefines.h"
69 #include "BLI_arithb.h"
70 #include "BLI_blenlib.h"
76 #include "transform.h"
78 extern ListBase editNurb;
79 extern ListBase editelems;
81 extern TransInfo Trans; /* From transform.c */
83 /* ************************** Functions *************************** */
86 void getViewVector(float coord[3], float vec[3]) {
95 Mat4MulVec4fl(G.vd->viewmat, p2);
101 Mat4MulVec4fl(G.vd->viewinv, p2);
103 VecSubf(vec, p1, p2);
106 VECCOPY(vec, G.vd->viewinv[2]);
111 /* ************************** GENERICS **************************** */
113 /* if editbone (partial) selected, copy data */
114 /* context; editmode armature, with mirror editing enabled */
115 static void transform_armature_mirror_update(void)
117 EditBone *ebo, *eboflip;
119 for (ebo=G.edbo.first; ebo; ebo=ebo->next) {
120 if(ebo->flag & (BONE_TIPSEL|BONE_ROOTSEL)) {
122 eboflip= armature_bone_get_mirrored(ebo);
125 /* we assume X-axis flipping for now */
126 if(ebo->flag & BONE_TIPSEL) {
127 eboflip->tail[0]= -ebo->tail[0];
128 eboflip->tail[1]= ebo->tail[1];
129 eboflip->tail[2]= ebo->tail[2];
130 eboflip->rad_tail= ebo->rad_tail;
132 if(ebo->flag & BONE_ROOTSEL) {
133 eboflip->head[0]= -ebo->head[0];
134 eboflip->head[1]= ebo->head[1];
135 eboflip->head[2]= ebo->head[2];
136 eboflip->rad_head= ebo->rad_head;
138 if(ebo->flag & BONE_SELECTED)
139 eboflip->dist= ebo->dist;
146 /* called for objects updating while transform acts, once per redraw */
147 void recalcData(TransInfo *t)
151 if (G.obedit->type == OB_MESH) {
152 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA); /* sets recalc flags */
154 recalc_editnormals();
156 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
157 Nurb *nu= editNurb.first;
158 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA); /* sets recalc flags */
162 testhandlesNurb(nu); /* test for bezier too */
166 else if(G.obedit->type==OB_ARMATURE){ /* no recalc flag, does pose */
167 bArmature *arm= G.obedit->data;
170 /* Ensure all bones are correctly adjusted */
171 for (ebo=G.edbo.first; ebo; ebo=ebo->next){
173 if ((ebo->flag & BONE_IK_TOPARENT) && ebo->parent){
174 /* If this bone has a parent tip that has been moved */
175 if (ebo->parent->flag & BONE_TIPSEL){
176 VECCOPY (ebo->head, ebo->parent->tail);
177 if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
179 /* If this bone has a parent tip that has NOT been moved */
181 VECCOPY (ebo->parent->tail, ebo->head);
182 if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
186 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
187 if(ebo->oldlength==0.0f) {
188 ebo->rad_head= 0.25f*ebo->length;
189 ebo->rad_tail= 0.10f*ebo->length;
190 ebo->dist= 0.25f*ebo->length;
192 if(ebo->rad_head > ebo->parent->rad_tail)
193 ebo->rad_head= ebo->parent->rad_tail;
197 if(arm->flag & ARM_MIRROR_EDIT)
198 transform_armature_mirror_update();
201 else if(G.obedit->type==OB_LATTICE) {
202 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA); /* sets recalc flags */
204 if(editLatt->flag & LT_OUTSIDE) outside_lattice(editLatt);
207 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA); /* sets recalc flags */
210 else if( (t->flag & T_POSE) && t->poseobj) {
211 Object *ob= t->poseobj;
212 bArmature *arm= ob->data;
214 /* old optimize trick... this enforces to bypass the depgraph */
215 if (!(arm->flag & ARM_DELAYDEFORM))
216 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA); /* sets recalc flags */
225 /* this flag is from depgraph, was stored in nitialize phase, handled in drawview.c */
226 if(base->flag & BA_HAS_RECALC_OB)
227 base->object->recalc |= OB_RECALC_OB;
228 if(base->flag & BA_HAS_RECALC_DATA)
229 base->object->recalc |= OB_RECALC_DATA;
231 /* thanks to ob->ctime usage, ipos are not called in where_is_object,
232 unless we edit ipokeys */
233 if(base->flag & BA_DO_IPO) {
234 if(base->object->ipo) {
237 base->object->ctime= -1234567.0;
239 icu= base->object->ipo->curve.first;
241 calchandles_ipocurve(icu);
251 /* update shaded drawmode while transform */
252 if(G.vd->drawtype == OB_SHADED) reshadeall_displist();
256 void initTransModeFlags(TransInfo *t, int mode)
261 /* REMOVING RESTRICTIONS FLAGS */
262 t->flag &= ~T_ALL_RESTRICTIONS;
266 t->flag |= T_NULL_ONE;
267 t->num.flag |= NUM_NULL_ONE;
268 t->num.flag |= NUM_AFFECT_ALL;
270 t->flag |= T_NO_ZERO;
271 t->num.flag |= NUM_NO_ZERO;
275 t->num.flag |= NUM_NULL_ONE;
276 t->num.flag |= NUM_NO_NEGATIVE;
277 t->flag |= T_NO_CONSTRAINT;
281 case TFM_BONE_ENVELOPE:
282 t->flag |= T_NO_CONSTRAINT;
287 void drawLine(float *center, float *dir, char axis, short options)
289 extern void make_axis_color(char *col, char *col2, char axis); // drawview.c
290 float v1[3], v2[3], v3[3];
291 char col[3], col2[3];
293 //if(G.obedit) mymultmatrix(G.obedit->obmat); // sets opengl viewing
296 VecMulf(v3, G.vd->far);
298 VecSubf(v2, center, v3);
299 VecAddf(v1, center, v3);
301 if (options & DRAWLIGHT) {
302 col[0] = col[1] = col[2] = 220;
305 BIF_GetThemeColor3ubv(TH_GRID, col);
307 make_axis_color(col, col2, axis);
311 glBegin(GL_LINE_STRIP);
316 myloadmatrix(G.vd->viewmat);
319 void initTrans (TransInfo *t)
322 /* moving: is shown in drawobject() (transform color) */
323 if(G.obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
324 else G.moving= G_TRANSFORM_OBJ;
331 /* setting PET flag */
332 if ((t->context & CTX_NO_PET) == 0 && (G.scene->proportional)) {
333 t->flag |= T_PROP_EDIT;
334 if(G.scene->proportional==2) t->flag |= T_PROP_CONNECTED; // yes i know, has to become define
337 getmouseco_areawin(t->imval);
338 t->con.imval[0] = t->imval[0];
339 t->con.imval[1] = t->imval[1];
354 t->num.val[2] = 0.0f;
362 Mat4CpyMat4(t->viewmat, G.vd->viewmat);
363 Mat4CpyMat4(t->viewinv, G.vd->viewinv);
364 Mat4CpyMat4(t->persinv, G.vd->persinv);
367 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
368 void postTrans (TransInfo *t)
370 G.moving = 0; // Set moving flag off (display as usual)
374 /* postTrans can be called when nothing is selected, so data is NULL already */
379 /* since ipokeys are optional on objects, we mallocced them per trans-data */
380 for(a=0, td= t->data; a<t->total; a++, td++) {
381 if(td->tdi) MEM_freeN(td->tdi);
386 if (t->ext) MEM_freeN(t->ext);
390 static void apply_grid3(float *val, int max_index, float fac1, float fac2, float fac3)
392 /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
393 int invert = U.flag & USER_AUTOGRABGRID;
397 for (i=0; i<=max_index; i++) {
400 if(G.qual & LR_CTRLKEY) ctrl= 0;
403 else ctrl= (G.qual & LR_CTRLKEY);
405 if(ctrl && (G.qual & LR_SHIFTKEY)) {
407 for (i=0; i<=max_index; i++) {
408 val[i]= fac3*(float)floor(val[i]/fac3 +.5);
414 for (i=0; i<=max_index; i++) {
415 val[i]= fac2*(float)floor(val[i]/fac2 +.5);
421 for (i=0; i<=max_index; i++) {
422 val[i]= fac1*(float)floor(val[i]/fac1 +.5);
429 void snapGrid(TransInfo *t, float *val) {
430 apply_grid3(val, t->idx_max, t->snap[0], t->snap[1], t->snap[2]);
433 void applyTransObjects(TransInfo *t)
437 for (td = t->data; td < t->data + t->total; td++) {
438 VECCOPY(td->iloc, td->loc);
440 VECCOPY(td->ext->irot, td->ext->rot);
443 VECCOPY(td->ext->isize, td->ext->size);
449 /* helper for below */
450 static void restore_ipokey(float *poin, float *old)
459 static void restoreElement(TransData *td) {
460 /* TransData for crease has no loc */
462 VECCOPY(td->loc, td->iloc);
469 VECCOPY(td->ext->rot, td->ext->irot);
472 VECCOPY(td->ext->size, td->ext->isize);
474 if(td->flag & TD_USEQUAT) {
476 QUATCOPY(td->ext->quat, td->ext->iquat);
481 TransDataIpokey *tdi= td->tdi;
483 restore_ipokey(tdi->locx, tdi->oldloc);
484 restore_ipokey(tdi->locy, tdi->oldloc+1);
485 restore_ipokey(tdi->locz, tdi->oldloc+2);
487 restore_ipokey(tdi->rotx, tdi->oldrot);
488 restore_ipokey(tdi->roty, tdi->oldrot+1);
489 restore_ipokey(tdi->rotz, tdi->oldrot+2);
491 restore_ipokey(tdi->sizex, tdi->oldsize);
492 restore_ipokey(tdi->sizey, tdi->oldsize+1);
493 restore_ipokey(tdi->sizez, tdi->oldsize+2);
497 void restoreTransObjects(TransInfo *t)
501 for (td = t->data; td < t->data + t->total; td++) {
507 void calculateCenterCursor(TransInfo *t)
511 cursor = give_cursor();
512 VECCOPY(t->center, cursor);
514 if(t->flag & (T_EDIT|T_POSE)) {
515 Object *ob= G.obedit?G.obedit:t->poseobj;
516 float mat[3][3], imat[3][3];
519 VecSubf(t->center, t->center, ob->obmat[3]);
520 Mat3CpyMat4(mat, ob->obmat);
522 Mat3MulVecfl(imat, t->center);
524 VECCOPY(vec, t->center);
525 Mat4MulVecfl(ob->obmat, vec);
526 project_int(vec, t->center2d);
529 project_int(t->center, t->center2d);
533 void calculateCenterMedian(TransInfo *t)
535 float partial[3] = {0.0f, 0.0f, 0.0f};
537 for(i = 0; i < t->total; i++) {
538 if (t->data[i].flag & TD_SELECTED) {
539 VecAddf(partial, partial, t->data[i].center);
543 All the selected elements are at the head of the array
544 which means we can stop when it finds unselected data
549 VecMulf(partial, 1.0f / i);
550 VECCOPY(t->center, partial);
552 if (t->flag & (T_EDIT|T_POSE)) {
553 Object *ob= G.obedit?G.obedit:t->poseobj;
556 VECCOPY(vec, t->center);
557 Mat4MulVecfl(ob->obmat, vec);
558 project_int(vec, t->center2d);
561 project_int(t->center, t->center2d);
565 void calculateCenterBound(TransInfo *t)
570 for(i = 0; i < t->total; i++) {
572 if (t->data[i].flag & TD_SELECTED) {
573 MinMax3(min, max, t->data[i].center);
577 All the selected elements are at the head of the array
578 which means we can stop when it finds unselected data
584 VECCOPY(max, t->data[i].center);
585 VECCOPY(min, t->data[i].center);
588 VecAddf(t->center, min, max);
589 VecMulf(t->center, 0.5);
591 if (t->flag & (T_EDIT|T_POSE)) {
592 Object *ob= G.obedit?G.obedit:t->poseobj;
595 VECCOPY(vec, t->center);
596 Mat4MulVecfl(ob->obmat, vec);
597 project_int(vec, t->center2d);
600 project_int(t->center, t->center2d);
604 void calculateCenter(TransInfo *t)
606 switch(G.vd->around) {
608 calculateCenterBound(t);
611 calculateCenterMedian(t);
614 calculateCenterCursor(t);
617 /* Individual element center uses median center for helpline and such */
618 calculateCenterMedian(t);
621 /* set median, and if if if... do object center */
622 calculateCenterMedian(t);
623 if((t->flag & (T_EDIT|T_POSE))==0) {
626 VECCOPY(t->center, ob->obmat[3]);
627 project_int(t->center, t->center2d);
633 /* setting constraint center */
634 VECCOPY(t->con.center, t->center);
635 if(t->flag & (T_EDIT|T_POSE)) {
636 Object *ob= G.obedit?G.obedit:t->poseobj;
637 Mat4MulVecfl(ob->obmat, t->con.center);
640 /* voor panning from cameraview */
641 if(t->flag & T_OBJECT) {
642 if( G.vd->camera==OBACT && G.vd->persp>1) {
644 /* persinv is nasty, use viewinv instead, always right */
645 VECCOPY(axis, G.vd->viewinv[2]);
648 /* 6.0 = 6 grid units */
649 axis[0]= t->center[0]- 6.0f*axis[0];
650 axis[1]= t->center[1]- 6.0f*axis[1];
651 axis[2]= t->center[2]- 6.0f*axis[2];
653 project_int(axis, t->center2d);
655 /* rotate only needs correct 2d center, grab needs initgrabz() value */
656 if(t->mode==TFM_TRANSLATION) VECCOPY(t->center, axis);
659 initgrabz(t->center[0], t->center[1], t->center[2]);
662 void calculatePropRatio(TransInfo *t)
664 TransData *td = t->data;
667 short connected = t->flag & T_PROP_CONNECTED;
669 if (t->flag & T_PROP_EDIT) {
670 for(i = 0 ; i < t->total; i++, td++) {
671 if (td->flag & TD_SELECTED) {
674 else if ((connected &&
675 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
678 td->rdist > t->propsize)) {
680 The elements are sorted according to their dist member in the array,
681 that means we can stop when it finds one element outside of the propsize.
683 td->flag |= TD_NOACTION;
688 /* Use rdist for falloff calculations, it is the real distance */
689 td->flag &= ~TD_NOACTION;
690 dist= (t->propsize-td->rdist)/t->propsize;
691 switch(G.scene->prop_mode) {
693 td->factor= dist*dist;
696 td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
699 td->factor = (float)sqrt(dist);
708 td->factor = (float)sqrt(2*dist - dist * dist);
715 switch(G.scene->prop_mode) {
717 strcpy(t->proptext, "(Sharp)");
720 strcpy(t->proptext, "(Smooth)");
723 strcpy(t->proptext, "(Root)");
726 strcpy(t->proptext, "(Linear)");
729 strcpy(t->proptext, "(Constant)");
732 strcpy(t->proptext, "(Sphere)");
735 strcpy(t->proptext, "");
739 for(i = 0 ; i < t->total; i++, td++) {
742 strcpy(t->proptext, "");
746 TransInfo * BIF_GetTransInfo() {