Lotsa undo stuff added;
[blender.git] / source / blender / src / edit.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. 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
12  * about this.
13  *
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.
18  *
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.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <math.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #ifndef WIN32
40 #include <unistd.h>
41 #else
42 #include <io.h>
43 #include "BLI_winstuff.h"
44 #endif   
45 #include "MEM_guardedalloc.h"
46
47 #include "BMF_Api.h"
48
49 #include "PIL_time.h"
50
51 #include "DNA_armature_types.h"
52 #include "DNA_curve_types.h"
53 #include "DNA_lattice_types.h"
54 #include "DNA_meta_types.h"
55 #include "DNA_mesh_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_screen_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_space_types.h"
60 #include "DNA_view3d_types.h"
61
62 #include "BLI_blenlib.h"
63 #include "BLI_arithb.h"
64 #include "BLI_editVert.h"
65
66 #include "BKE_utildefines.h"
67 #include "BKE_anim.h"
68 #include "BKE_object.h"
69 #include "BKE_displist.h"
70 #include "BKE_global.h"
71 #include "BKE_lattice.h"
72 #include "BKE_mesh.h"
73
74 #include "BIF_editmesh.h"
75 #include "BIF_editview.h"
76 #include "BIF_gl.h"
77 #include "BIF_glutil.h"
78 #include "BIF_interface.h"
79 #include "BIF_mywindow.h"
80 #include "BIF_resources.h"
81 #include "BIF_space.h"
82 #include "BIF_screen.h"
83 #include "BIF_toolbox.h"
84
85 #include "BSE_edit.h"
86 #include "BSE_drawipo.h"
87 #include "BSE_drawview.h"
88 #include "BSE_trans_types.h"
89 #include "BSE_view.h"
90
91 #include "BDR_editobject.h"
92 #include "BDR_editmball.h"
93 #include "BDR_editcurve.h"
94
95 /* old stuff */
96 #include "blendef.h"
97 #include "mydevice.h"
98
99 /*#include "armature.h"*/
100 /*  #include "edit.h" */
101 #include "nla.h"
102
103 #ifdef __NLA
104 #include "BIF_editarmature.h"
105 #endif
106
107 /* editmball.c */
108 extern ListBase editelems;  /* go away ! */
109
110
111 /* from editobject */
112 extern void make_trans_verts(float *min, float *max, int mode);     
113
114 /* circle selection callback */
115 typedef void (*select_CBfunc)(short selecting, Object *editobj, short *mval, float rad);
116
117 extern void obedit_selectionCB(short selecting, Object *editobj, 
118                                short *mval, float rad);
119 extern void uvedit_selectionCB(short selecting, Object *editobj, 
120                                short *mval, float rad);
121
122 void circle_selectCB(select_CBfunc func);
123
124 int get_border(rcti *rect, short col)
125 {
126         float dvec[4], fac1, fac2;
127         int retval=1;
128         unsigned short event;
129         short mval[2], mvalo[4], val, x1, y1;
130         char str[64];
131
132         mywinset(G.curscreen->mainwin);
133         
134         /* slightly larger, 1 pixel at the edge */
135         glReadBuffer(GL_FRONT);
136         glDrawBuffer(GL_FRONT);
137
138         /* removed my_get_frontbuffer, this crashes when it gets a part outside the screen */
139         /* solved it with just a redraw! */
140
141         mywinset(curarea->win);
142         
143         glDrawBuffer(GL_FRONT);
144         persp(PERSP_WIN);
145         initgrabz(0.0, 0.0, 0.0);
146         
147         getmouseco_areawin(mvalo);
148
149         /* draws the selection initial cross */
150         sdrawXORline4(0, 0,  mvalo[1],  curarea->winx,  mvalo[1]);
151         sdrawXORline4(1, mvalo[0],  0,  mvalo[0],  curarea->winy); 
152         glFlush();
153         
154         while(TRUE) {
155         
156                 /* selection loop while mouse pressed */
157                 getmouseco_areawin(mval);
158
159                 if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
160
161                         /* aiming cross */
162                         sdrawXORline4(0, 0,  mval[1],  curarea->winx,  mval[1]);
163                         sdrawXORline4(1, mval[0],  0,  mval[0],  curarea->winy);
164                         glFlush();
165
166                         mvalo[0]= mval[0];
167                         mvalo[1]= mval[1];
168                 }
169                 event= extern_qread(&val);
170
171                 if(event && val) {
172
173                         /* for when a renderwindow is open, and a mouse cursor activates it */
174                         persp(PERSP_VIEW);
175                         mywinset(curarea->win);
176                         persp(PERSP_WIN);
177                         
178                         if(event==ESCKEY) {
179                                 retval= 0;
180                                 break;
181                         }
182                         else if(event==BKEY) {
183                                 /* b has been pressed twice: proceed with circle select */
184                                 retval= 0;
185                                 break;
186                         }
187                         else if(event==LEFTMOUSE) break;
188                         else if(event==MIDDLEMOUSE) break;
189                         else if(event==RIGHTMOUSE) break;
190                 }
191                 else PIL_sleep_ms(10);
192                 
193         } /* end while (TRUE) */
194
195         /* erase XORed lines */
196         sdrawXORline4(-1, 0, 0, 0, 0);
197         
198         if(retval) {
199                 /* box select */
200                 x1= mval[0];
201                 y1= mval[1];
202                 
203                 getmouseco_areawin(mvalo);
204
205                 sdrawXORline4(0, x1, y1, x1, mvalo[1]); 
206                 sdrawXORline4(1, x1, mvalo[1], mvalo[0], mvalo[1]); 
207                 sdrawXORline4(2, mvalo[0], mvalo[1], mvalo[0], y1); 
208                 sdrawXORline4(3,  mvalo[0], y1, x1, y1); 
209                 glFlush();
210                         
211                 while(TRUE) {
212                         getmouseco_areawin(mval);
213                         if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
214
215                                 sdrawXORline4(0, x1, y1, x1, mval[1]); 
216                                 sdrawXORline4(1, x1, mval[1], mval[0], mval[1]); 
217                                 sdrawXORline4(2, mval[0], mval[1], mval[0], y1); 
218                                 sdrawXORline4(3,  mval[0], y1, x1, y1); 
219                                 
220                                 /* draw size information in corner */
221                                 if(curarea->spacetype==SPACE_VIEW3D) {
222                                         BIF_ThemeColor(TH_BACK);
223                                         glRecti(10, 25, 250, 40);
224         
225                                         if(G.vd->persp==0) {
226                                                 window_to_3d(dvec, mvalo[0]-x1, mvalo[1]-y1);
227         
228                                                 sprintf(str, "X %.4f  Y %.4f  Z %.4f  Dia %.4f", dvec[0], dvec[1], dvec[2], sqrt(dvec[0]*dvec[0]+dvec[1]*dvec[1]+dvec[2]*dvec[2]));
229                                                 glColor3f(0.0, 0.0, 0.0); 
230                                                 glRasterPos2i(15,  27);
231                                                 BMF_DrawString(G.fonts, str);
232                                                 glColor3f(0.7, 0.7, 0.7); 
233                                                 glRasterPos2i(16,  28);
234                                                 BMF_DrawString(G.fonts, str);
235                                         }
236                                         else if(G.vd->persp==2) {
237                                                 rcti vb;
238         
239                                                 calc_viewborder(G.vd, &vb);
240         
241                                                 fac1= (mvalo[0]-x1)/( (float) (vb.xmax-vb.xmin) );
242                                                 fac1*= 0.01*G.scene->r.size*G.scene->r.xsch;
243                                                 
244                                                 fac2= (mvalo[1]-y1)/( (float) (vb.ymax-vb.ymin) );
245                                                 fac2*= 0.01*G.scene->r.size*G.scene->r.ysch;
246                                                 
247                                                 sprintf(str, "X %.1f  Y %.1f  Dia %.1f", fabs(fac1), fabs(fac2), sqrt(fac1*fac1 + fac2*fac2) );
248                                                 glColor3f(0.0, 0.0, 0.0); 
249                                                 glRasterPos2i(15,  27);
250                                                 BMF_DrawString(G.fonts, str);
251                                                 glColor3f(0.7, 0.7, 0.7); 
252                                                 glRasterPos2i(16,  28);
253                                                 BMF_DrawString(G.fonts, str);
254                                         }
255                                 }
256                                 else if(curarea->spacetype==SPACE_IPO) {
257                                         SpaceIpo *sipo= curarea->spacedata.first;
258         
259                                         BIF_ThemeColor(TH_BACK);
260                                         glRecti(20, 30, 170, 40);
261                                                                 
262                                         mvalo[2]= x1;
263                                         mvalo[3]= y1;
264                                         areamouseco_to_ipoco(&sipo->v2d, mval, dvec, dvec+1);
265                                         areamouseco_to_ipoco(&sipo->v2d, mvalo+2, dvec+2, dvec+3);
266
267                                         sprintf(str, "Time: %.4f  Y %.4f", dvec[0]-dvec[2], dvec[1]-dvec[3]);
268                                         glRasterPos2i(30,  30);
269                                         glColor3f(0.0, 0.0, 0.0); 
270                                         BMF_DrawString(G.fonts, str);
271                                         glRasterPos2i(31,  31);
272                                         glColor3f(0.9, 0.9, 0.9); 
273                                         BMF_DrawString(G.fonts, str);
274                                 }
275
276                                 glFlush();
277
278                                 mvalo[0]= mval[0];
279                                 mvalo[1]= mval[1];
280                         }
281                         
282                         event= extern_qread(&val);
283                         
284                         if(event && val==0) {
285                                 /* still because of the renderwindow... */
286                                 persp(PERSP_VIEW);
287                                 mywinset(curarea->win);
288                                 persp(PERSP_WIN);
289                                 
290                                 if(event==ESCKEY) {
291                                         retval= 0;
292                                         break;
293                                 }
294                                 else if(event==LEFTMOUSE) break;
295                                 else if(event==MIDDLEMOUSE) break;
296                                 else if(event==RIGHTMOUSE) break;
297                         }
298                         
299                 } /* end while (TRUE) */
300                 sdrawXORline4(-1, 0, 0, 0, 0);
301                 
302                 if(retval) {
303                         rect->xmin= x1;
304                         rect->ymin= y1;
305                         rect->xmax= mval[0];
306                         rect->ymax= mval[1];
307                         retval= event;
308
309                         /* normalise */
310                         if(rect->xmin>rect->xmax) SWAP(int, rect->xmin, rect->xmax);
311                         if(rect->ymin>rect->ymax) SWAP(int, rect->ymin, rect->ymax);
312                         
313                         if(rect->xmin==rect->xmax) retval= 0;
314                         if(rect->ymin==rect->ymax) retval= 0;
315                 }
316         }
317
318
319         /* clear */
320         if(event!=BKEY) {
321                 if ELEM(curarea->spacetype, SPACE_VIEW3D, SPACE_IPO) {
322                         scrarea_queue_winredraw(curarea);
323                 }
324         }
325         
326         glFlush();
327         glReadBuffer(GL_BACK);
328         glDrawBuffer(GL_BACK);
329
330         persp(PERSP_VIEW);
331         
332         /* pressed B again ? -> brush select */
333         if(event==BKEY) {
334                 setlinestyle(0);
335                 switch (curarea->spacetype) {
336                 case SPACE_VIEW3D:
337                         if (G.obedit)
338                                 circle_selectCB(&obedit_selectionCB);
339                         return 0;
340                 case SPACE_IMAGE: // brush select in UV editor
341                         circle_selectCB(&uvedit_selectionCB);
342                         // this is a hack; we return 0 that the caller from get_border
343                         // doesn't execute the selection code for border select..
344                         return 0;
345                 }
346         }
347         return retval;
348 }
349
350 void draw_sel_circle(short *mval, short *mvalo, float rad, float rado, int selecting)
351 {
352         static short no_mvalo=0;
353
354         if(mval==NULL && mvalo==NULL) { /* signal */
355                 no_mvalo= 1;
356                 return;
357         }
358
359         persp(PERSP_WIN);
360         glReadBuffer(GL_FRONT);
361         glDrawBuffer(GL_FRONT);
362         //setlinestyle(2);
363
364         /* draw circle */
365         if(mvalo && no_mvalo==0) {
366                 fdrawXORcirc(mvalo[0], mvalo[1], rado);
367         }
368         
369         if(mval) {
370                 fdrawXORcirc(mval[0], mval[1], rad);
371         }
372         //setlinestyle(0);
373
374         glFlush();
375         persp(PERSP_VIEW);
376         glDrawBuffer(GL_BACK);
377         glReadBuffer(GL_BACK);
378
379         no_mvalo= 0;
380 }
381
382 /** This function does the same as editview.c:circle_select(),
383   * but the selection actions are defined by a callback, making
384   * it (hopefully) reusable for other windows than the 3D view.
385   */
386
387 void circle_selectCB(select_CBfunc callback)
388 {
389         static float rad= 40.0;
390         float rado;
391         int firsttime=1;
392         unsigned short event;
393         short mvalo[2], mval[2], val;
394         short selecting=0;
395         Object *obj;
396         
397         if(G.obedit) obj = G.obedit;
398         else obj = OBACT;
399
400         mywinset(curarea->win);
401         
402         getmouseco_areawin(mvalo);
403         mval[0]= mvalo[0]; mval[1]= mvalo[1];
404
405         draw_sel_circle(mval, NULL, rad, 0.0, selecting); // draws frontbuffer, but sets backbuf again
406         
407         rado= rad;
408         
409         while(TRUE) {
410                 
411                 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || rado!=rad || firsttime) {
412                         firsttime= 0;
413                         
414                         if(selecting) {
415                                 callback(selecting, obj, mval, rad);
416                         }
417
418                         draw_sel_circle(mval, mvalo, rad, rado, selecting);
419                 
420                         mvalo[0]= mval[0];
421                         mvalo[1]= mval[1];
422                         rado= rad;
423
424                 }
425                 
426                 event= extern_qread(&val);
427                 if (event) {
428                         int afbreek= 0;
429
430                         /* for when another window is open and a mouse cursor activates it */
431                         if(event!=MOUSEY && event!=MOUSEX) mywinset(curarea->win);
432                         
433                         getmouseco_areawin(mval);       // important to do here, trust events!
434                         
435                         switch(event) {
436                 
437                         case LEFTMOUSE:
438                         case MIDDLEMOUSE:
439                                 if(val) selecting= event;
440                                 else selecting= 0;
441                                 firsttime= 1;
442                                 
443                                 break;
444                         case WHEELDOWNMOUSE:
445                         case PADPLUSKEY:
446                                 if(val) if(rad<200.0) rad*= 1.2;
447                                 break;
448                         case WHEELUPMOUSE:
449                         case PADMINUS:
450                                 if(val) if(rad>5.0) rad/= 1.2;
451                                 break;
452                         
453                         case ESCKEY: case SPACEKEY: case RIGHTMOUSE: case INPUTCHANGE: 
454                         case GKEY: case SKEY: case RKEY: case XKEY: case EKEY: case TABKEY:
455                                 afbreek= 1;
456                                 break;
457
458                         }
459                         
460                         if(afbreek) break;
461                 }
462                 else PIL_sleep_ms(10);
463         }
464         
465         /* clear circle */
466         draw_sel_circle(NULL, mvalo, 0, rad, 1);
467         BIF_undo_push("Circle Select");
468         countall();
469         allqueue(REDRAWINFO, 0);
470 }
471
472 void count_object(Object *ob, int sel)
473 {
474         Mesh *me;
475         Curve *cu;
476         int tot=0, totf=0;
477         
478         switch(ob->type) {
479         case OB_MESH:
480                 G.totmesh++;
481                 me= get_mesh(ob);
482                 if(me) {
483                         int totvert, totface;
484                                 /* hack, should be getting displistmesh from a central function */
485                         if (mesh_uses_displist(me) && me->disp.first && ((DispList*)me->disp.first)->type==DL_MESH) {
486                                 DispListMesh *dlm= ((DispList*)me->disp.first)->mesh;
487                                 totvert= dlm->totvert;
488                                 totface= dlm->totface;
489                         } else {
490                                 totvert= me->totvert;
491                                 totface= me->totface;
492                         }
493                         
494                         G.totvert+= totvert;
495                         G.totface+= totface;
496                         if(sel) {
497                                 G.totvertsel+= totvert;
498                                 G.totfacesel+= totface;
499                         }
500                 }
501                 break;
502
503         case OB_LAMP:
504                 G.totlamp++;
505                 break;
506         case OB_SURF:
507         case OB_CURVE:
508         case OB_FONT:
509                 G.totcurve++;
510                 tot=totf= 0;
511                 cu= ob->data;
512                 if(cu->disp.first==0) makeDispList(ob);
513                 count_displist( &cu->disp, &tot, &totf);
514                 G.totvert+= tot;
515                 G.totface+= totf;
516                 if(sel) {
517                         G.totvertsel+= tot;
518                         G.totfacesel+= totf;
519                 }
520                 break;
521         case OB_MBALL:
522                 count_displist( &ob->disp, &tot, &totf);
523                 G.totvert+= tot;
524                 G.totface+= totf;
525                 if(sel) {
526                         G.totvertsel+= tot;
527                         G.totfacesel+= totf;
528                 }
529                 
530                 break;
531         }
532         
533 }
534
535 void countall()
536 {
537 /*      extern Lattice *editLatt; in BKE_lattice.h*/
538         extern ListBase editNurb;
539         /* extern ListBase bpbase; */
540         Base *base;
541         Object *ob;
542         Mesh *me;
543         Nurb *nu;
544         BezTriple *bezt;
545         BPoint *bp;
546         MetaElem *ml;
547         /* struct BodyPoint *bop; */
548         struct EditVert *eve;
549         struct EditFace *efa;
550 #ifdef __NLA
551         struct EditBone *ebo;
552 #endif
553         int a;
554
555         G.totvert= G.totvertsel= G.totfacesel= G.totface= G.totobj= 
556             G.totmesh= G.totlamp= G.totcurve= G.totobjsel=  0;
557
558         if(G.obedit) {
559                 
560                 if(G.obedit->type==OB_MESH) {
561                         EditMesh *em = G.editMesh;
562                         eve= em->verts.first;
563                         while(eve) {
564                                 G.totvert++;
565                                 if(eve->f & SELECT) G.totvertsel++;
566                                 eve= eve->next;
567                         }
568                         
569                         efa= em->faces.first;
570                         while(efa) {
571                                 G.totface++;
572                                 if(efa->f & SELECT) G.totfacesel++;
573                                 efa= efa->next;
574                         }
575                 }
576 #ifdef __NLA
577                 else if (G.obedit->type==OB_ARMATURE){
578                         for (ebo=G.edbo.first;ebo;ebo=ebo->next){
579                                 
580                                 /* Sync selection to parent for ik children */
581                                 if ((ebo->flag & BONE_IK_TOPARENT) && ebo->parent){
582                                         G.totvert--;
583                                         if (ebo->parent->flag & BONE_TIPSEL)
584                                                 ebo->flag |= BONE_ROOTSEL;
585                                         else
586                                                 ebo->flag &= ~BONE_ROOTSEL;
587                                 }
588                                 
589                                 if (ebo->flag & BONE_TIPSEL)
590                                         G.totvertsel++;
591                                 if (ebo->flag & BONE_ROOTSEL)
592                                         G.totvertsel++;
593                                 
594                                 if ((ebo->flag & BONE_TIPSEL) && (ebo->flag & BONE_ROOTSEL))
595                                         ebo->flag |= BONE_SELECTED;
596                                 else
597                                         ebo->flag &= ~BONE_SELECTED;
598                                 
599                                 //      If this is an IK child and it's parent is being moved, remove our root
600                                 if ((ebo->flag & BONE_IK_TOPARENT)&& (ebo->flag & BONE_ROOTSEL) && ebo->parent && (ebo->parent->flag & BONE_TIPSEL)){
601                                         G.totvertsel--;
602                                 }
603
604                                 G.totvert+=2;
605                                 G.totface++;
606                                 
607
608                         }
609                 }
610 #endif
611                 else if ELEM3(G.obedit->type, OB_CURVE, OB_SURF, OB_FONT) {
612                         nu= editNurb.first;
613                         while(nu) {
614                                 if((nu->type & 7)==CU_BEZIER) {
615                                         bezt= nu->bezt;
616                                         a= nu->pntsu;
617                                         while(a--) {
618                                                 G.totvert+=3;
619                                                 if(bezt->f1) G.totvertsel++;
620                                                 if(bezt->f2) G.totvertsel++;
621                                                 if(bezt->f3) G.totvertsel++;
622                                                 bezt++;
623                                         }
624                                 }
625                                 else {
626                                         bp= nu->bp;
627                                         a= nu->pntsu*nu->pntsv;
628                                         while(a--) {
629                                                 G.totvert++;
630                                                 if(bp->f1 & 1) G.totvertsel++;
631                                                 bp++;
632                                         }
633                                 }
634                                 nu= nu->next;
635                         }
636                 }
637                 else if(G.obedit->type==OB_MBALL) {
638                         ml= editelems.first;
639                         while(ml) {
640                                 G.totvert++;
641                                 if(ml->flag & SELECT) G.totvertsel++;
642                                 ml= ml->next;
643                         }
644                 }
645                 else if(G.obedit->type==OB_LATTICE) {
646                         bp= editLatt->def;
647                         
648                         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
649                         while(a--) {
650                                 G.totvert++;
651                                 if(bp->f1 & 1) G.totvertsel++;
652                                 bp++;
653                         }
654                 }
655                 
656                 allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
657                 return;
658         }
659         else if(G.f & (G_FACESELECT + G_VERTEXPAINT + G_TEXTUREPAINT +G_WEIGHTPAINT)) {
660                 me= get_mesh((G.scene->basact) ? (G.scene->basact->object) : 0);
661                 if(me) {
662                         G.totface= me->totface;
663                         G.totvert= me->totvert;
664                 }
665                 allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
666                 return;
667         }
668
669         if(G.scene==NULL) return;
670
671         base= (G.scene->base.first);
672         while(base) {
673                 if(G.scene->lay & base->lay) {
674                         
675                         G.totobj++;
676                         if(base->flag & SELECT) G.totobjsel++;
677                         
678                         count_object(base->object, base->flag & SELECT);
679                         
680                         if(base->object->transflag & OB_DUPLI) {
681                                 extern ListBase duplilist;
682
683                                 make_duplilist(G.scene, base->object);
684                                 ob= duplilist.first;
685                                 while(ob) {
686                                         G.totobj++;
687                                         count_object(ob, base->flag & SELECT);
688                                         ob= ob->id.next;
689                                 }
690                                 free_duplilist();
691                         }
692                 }
693                 base= base->next;
694         }
695         allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
696 }
697
698
699 void snap_sel_to_grid()
700 {
701         extern TransVert *transvmain;
702         extern int tottrans;
703         extern float originmat[3][3];   /* object.c */
704         TransVert *tv;
705         Base *base;
706         Object *ob;
707         float gridf, imat[3][3], bmat[3][3], vec[3];
708         int a;
709
710         gridf= G.vd->grid;
711
712
713                 if(G.obedit) {
714 #ifdef __NLA
715                         if ELEM5(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
716 #else
717                         if ELEM4(G.obedit->type, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
718 #endif
719                         if(tottrans==0) return;
720
721                         Mat3CpyMat4(bmat, G.obedit->obmat);
722                         Mat3Inv(imat, bmat);
723
724                         tv= transvmain;
725                         for(a=0; a<tottrans; a++, tv++) {
726
727                                 VECCOPY(vec, tv->loc);
728                                 Mat3MulVecfl(bmat, vec);
729                                 VecAddf(vec, vec, G.obedit->obmat[3]);
730                                 vec[0]= G.vd->grid*floor(.5+ vec[0]/gridf);
731                                 vec[1]= G.vd->grid*floor(.5+ vec[1]/gridf);
732                                 vec[2]= G.vd->grid*floor(.5+ vec[2]/gridf);
733                                 VecSubf(vec, vec, G.obedit->obmat[3]);
734
735                                 Mat3MulVecfl(imat, vec);
736                                 VECCOPY(tv->loc, vec);
737
738                         }
739
740                         MEM_freeN(transvmain);
741                         transvmain= 0;
742                         
743                         calc_trans_verts(); // does test2d, makedisplist too */
744
745                         if (G.obedit->type == OB_ARMATURE)
746                                 special_trans_update(0);
747
748                         allqueue(REDRAWVIEW3D, 0);
749                         return;
750                 }
751 #ifdef __NLA
752                 if (G.obpose){
753                         allqueue(REDRAWVIEW3D, 0);
754                         return;
755                 }
756 #endif
757                 base= (G.scene->base.first);
758                 while(base) {
759                         if( ( ((base)->flag & SELECT) && ((base)->lay & G.vd->lay) && ((base)->object->id.lib==0))) {
760                                 ob= base->object;
761
762                                 vec[0]= -ob->obmat[3][0]+G.vd->grid*floor(.5+ ob->obmat[3][0]/gridf);
763                                 vec[1]= -ob->obmat[3][1]+G.vd->grid*floor(.5+ ob->obmat[3][1]/gridf);
764                                 vec[2]= -ob->obmat[3][2]+G.vd->grid*floor(.5+ ob->obmat[3][2]/gridf);
765
766                                 if(ob->parent) {
767                                         where_is_object(ob);
768
769                                         Mat3Inv(imat, originmat);
770                                         Mat3MulVecfl(imat, vec);
771                                         ob->loc[0]+= vec[0];
772                                         ob->loc[1]+= vec[1];
773                                         ob->loc[2]+= vec[2];
774                                 }
775                                 else {
776                                         ob->loc[0]+= vec[0];
777                                         ob->loc[1]+= vec[1];
778                                         ob->loc[2]+= vec[2];
779                                 }
780                         }
781
782                         base= base->next;
783                 }
784                 allqueue(REDRAWVIEW3D, 0);
785 }
786
787 void snap_sel_to_curs()
788 {
789         extern TransVert *transvmain;
790         extern int tottrans;
791         extern float originmat[3][3];   /* object.c */
792         TransVert *tv;
793         Base *base;
794         Object *ob;
795         float *curs, imat[3][3], bmat[3][3], vec[3];
796         int a;
797
798         curs= give_cursor();
799
800                 if(G.obedit) {
801 #ifdef __NLA
802                         if ELEM5(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
803 #else
804                         if ELEM4(G.obedit->type, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
805 #endif
806                         if(tottrans==0) return;
807
808                         Mat3CpyMat4(bmat, G.obedit->obmat);
809                         Mat3Inv(imat, bmat);
810
811                         tv= transvmain;
812                         for(a=0; a<tottrans; a++, tv++) {
813
814
815                                 vec[0]= curs[0]-G.obedit->obmat[3][0];
816                                 vec[1]= curs[1]-G.obedit->obmat[3][1];
817                                 vec[2]= curs[2]-G.obedit->obmat[3][2];
818
819
820                                 Mat3MulVecfl(imat, vec);
821                                 VECCOPY(tv->loc, vec);
822
823                         }
824                         MEM_freeN(transvmain);
825                         transvmain= 0;
826
827                         calc_trans_verts(); // does test2d, makedisplist too */
828
829                         if (G.obedit->type == OB_ARMATURE)
830                                 special_trans_update(0);
831
832                         allqueue(REDRAWVIEW3D, 0);
833                         return;
834                 }
835 #ifdef __NLA
836                 if (G.obpose){
837                         allqueue(REDRAWVIEW3D, 0);
838                         return;
839                 }
840 #endif
841                 base= (G.scene->base.first);
842                 while(base) {
843                         if( ( ((base)->flag & SELECT) && ((base)->lay & G.vd->lay) && ((base)->object->id.lib==0))) {
844                                 ob= base->object;
845
846                                 vec[0]= -ob->obmat[3][0] + curs[0];
847                                 vec[1]= -ob->obmat[3][1] + curs[1];
848                                 vec[2]= -ob->obmat[3][2] + curs[2];
849
850
851                                 if(ob->parent) {
852                                         where_is_object(ob);
853
854                                         Mat3Inv(imat, originmat);
855                                         Mat3MulVecfl(imat, vec);
856                                         ob->loc[0]+= vec[0];
857                                         ob->loc[1]+= vec[1];
858                                         ob->loc[2]+= vec[2];
859                                 }
860                                 else {
861                                         ob->loc[0]+= vec[0];
862                                         ob->loc[1]+= vec[1];
863                                         ob->loc[2]+= vec[2];
864                                 }
865                         }
866
867                         base= base->next;
868                 }
869                 allqueue(REDRAWVIEW3D, 0);
870 }
871
872 void snap_curs_to_grid()
873 {
874         float gridf, *curs;
875
876         gridf= G.vd->grid;
877         curs= give_cursor();
878
879         curs[0]= G.vd->grid*floor(.5+curs[0]/gridf);
880         curs[1]= G.vd->grid*floor(.5+curs[1]/gridf);
881         curs[2]= G.vd->grid*floor(.5+curs[2]/gridf);
882
883         allqueue(REDRAWVIEW3D, 0);
884
885 }
886
887 void snap_curs_to_sel()
888 {
889         extern TransVert *transvmain;
890         extern int tottrans;
891         TransVert *tv;
892         Base *base;
893         float *curs, bmat[3][3], vec[3], min[3], max[3], centroid[3];
894         int count, a;
895
896         curs= give_cursor();
897
898         count= 0;
899         INIT_MINMAX(min, max);
900         centroid[0]= centroid[1]= centroid[2]= 0.0;
901
902                 if(G.obedit) {
903                         tottrans=0;
904 #ifdef __NLA
905                         if ELEM5(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
906 #else
907                         if ELEM4(G.obedit->type, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
908 #endif
909                         if(tottrans==0) return;
910
911                         Mat3CpyMat4(bmat, G.obedit->obmat);
912
913                         tv= transvmain;
914                         for(a=0; a<tottrans; a++, tv++) {
915                                 VECCOPY(vec, tv->loc);
916                                 Mat3MulVecfl(bmat, vec);
917                                 VecAddf(vec, vec, G.obedit->obmat[3]);
918                                 VecAddf(centroid, centroid, vec);
919                                 DO_MINMAX(vec, min, max);
920                         }
921
922                         if(G.vd->around==V3D_CENTROID) {
923                                 VecMulf(centroid, 1.0/(float)tottrans);
924                                 VECCOPY(curs, centroid);
925                         }
926                         else {
927                                 curs[0]= (min[0]+max[0])/2;
928                                 curs[1]= (min[1]+max[1])/2;
929                                 curs[2]= (min[2]+max[2])/2;
930                         }
931                         MEM_freeN(transvmain);
932                         transvmain= 0;
933                 }
934                 else {
935                         base= (G.scene->base.first);
936                         while(base) {
937                                 if(((base)->flag & SELECT) && ((base)->lay & G.vd->lay) ) {
938                                         VECCOPY(vec, base->object->obmat[3]);
939                                         VecAddf(centroid, centroid, vec);
940                                         DO_MINMAX(vec, min, max);
941                                         count++;
942                                 }
943                                 base= base->next;
944                         }
945                         if(count) {
946                                 if(G.vd->around==V3D_CENTROID) {
947                                         VecMulf(centroid, 1.0/(float)count);
948                                         VECCOPY(curs, centroid);
949                                 }
950                                 else {
951                                         curs[0]= (min[0]+max[0])/2;
952                                         curs[1]= (min[1]+max[1])/2;
953                                         curs[2]= (min[2]+max[2])/2;
954                                 }
955                         }
956                 }
957                 allqueue(REDRAWVIEW3D, 0);
958 }
959
960 void snap_curs_to_firstsel()
961 {
962         extern TransVert *transvmain;
963         extern int tottrans;
964         TransVert *tv;
965         Base *base;
966         float *curs, bmat[3][3], vec[3], min[3], max[3], centroid[3];
967         int count;
968
969         curs= give_cursor();
970
971         count= 0;
972         INIT_MINMAX(min, max);
973         centroid[0]= centroid[1]= centroid[2]= 0.0;
974
975                 if(G.obedit) {
976                         tottrans=0;
977 #ifdef __NLA
978                         if ELEM5(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
979 #else
980                         if ELEM4(G.obedit->type, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
981 #endif
982                         if(tottrans==0) return;
983
984                         Mat3CpyMat4(bmat, G.obedit->obmat);
985
986                         tv= transvmain;
987                         VECCOPY(vec, tv->loc);
988                                 /*Mat3MulVecfl(bmat, vec);
989                                 VecAddf(vec, vec, G.obedit->obmat[3]);
990                                 VecAddf(centroid, centroid, vec);
991                                 DO_MINMAX(vec, min, max);*/
992
993                         if(G.vd->around==V3D_CENTROID) {
994                                 VecMulf(vec, 1.0/(float)tottrans);
995                                 VECCOPY(curs, vec);
996                         }
997                         else {
998                                 curs[0]= vec[0];
999                                 curs[1]= vec[1];
1000                                 curs[2]= vec[2];
1001                         }
1002                         MEM_freeN(transvmain);
1003                         transvmain= 0;
1004                 }
1005                 else {
1006                         base= (G.scene->base.first);
1007                         while(base) {
1008                                 if(((base)->flag & SELECT) && ((base)->lay & G.vd->lay) ) {
1009                                         VECCOPY(vec, base->object->obmat[3]);
1010                                         VecAddf(centroid, centroid, vec);
1011                                         DO_MINMAX(vec, min, max);
1012                                         count++;
1013                                 }
1014                                 base= base->next;
1015                         }
1016                         if(count) {
1017                                 if(G.vd->around==V3D_CENTROID) {
1018                                         VecMulf(centroid, 1.0/(float)count);
1019                                         VECCOPY(curs, centroid);
1020                                 }
1021                                 else {
1022                                         curs[0]= (min[0]+max[0])/2;
1023                                         curs[1]= (min[1]+max[1])/2;
1024                                         curs[2]= (min[2]+max[2])/2;
1025                                 }
1026                         }
1027                 }
1028                 allqueue(REDRAWVIEW3D, 0);
1029 }
1030
1031 void snap_to_center()
1032 {
1033         extern TransVert *transvmain;
1034         extern int tottrans;
1035         extern float originmat[3][3];
1036         TransVert *tv;
1037         Base *base;
1038         Object *ob;
1039         float snaploc[3], imat[3][3], bmat[3][3], vec[3], min[3], max[3], centroid[3];
1040         int count, a;
1041
1042
1043 /*calculate the snaplocation (centerpoint) */
1044         count= 0;
1045         INIT_MINMAX(min, max);
1046         centroid[0]= centroid[1]= centroid[2]= 0.0;
1047
1048                 if(G.obedit) {
1049                         /*tottrans=0;*/
1050 #ifdef __NLA
1051                         if ELEM5(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
1052 #else
1053                         if ELEM4(G.obedit->type, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
1054 #endif
1055                         if(tottrans==0) return;
1056
1057                         Mat3CpyMat4(bmat, G.obedit->obmat);
1058                         Mat3Inv(imat, bmat);
1059
1060                         tv= transvmain;
1061                         for(a=0; a<tottrans; a++, tv++) {
1062                                 VECCOPY(vec, tv->loc);
1063                                 Mat3MulVecfl(bmat, vec);
1064                                 VecAddf(vec, vec, G.obedit->obmat[3]);
1065                                 VecAddf(centroid, centroid, vec);
1066                                 DO_MINMAX(vec, min, max);
1067                         }
1068
1069                         if(G.vd->around==V3D_CENTROID) {
1070                                 VecMulf(centroid, 1.0/(float)tottrans);
1071                                 VECCOPY(snaploc, centroid);
1072                         }
1073                         else {
1074                                 snaploc[0]= (min[0]+max[0])/2;
1075                                 snaploc[1]= (min[1]+max[1])/2;
1076                                 snaploc[2]= (min[2]+max[2])/2;
1077                         }
1078                         
1079                         MEM_freeN(transvmain);
1080                         transvmain= 0;
1081
1082                 }
1083                 else {
1084                         base= (G.scene->base.first);
1085                         while(base) {
1086                                 if(((base)->flag & SELECT) && ((base)->lay & G.vd->lay) ) {
1087                                         VECCOPY(vec, base->object->obmat[3]);
1088                                         VecAddf(centroid, centroid, vec);
1089                                         DO_MINMAX(vec, min, max);
1090                                         count++;
1091                                 }
1092                                 base= base->next;
1093                         }
1094                         if(count) {
1095                                 if(G.vd->around==V3D_CENTROID) {
1096                                         VecMulf(centroid, 1.0/(float)count);
1097                                         VECCOPY(snaploc, centroid);
1098                                 }
1099                                 else {
1100                                         snaploc[0]= (min[0]+max[0])/2;
1101                                         snaploc[1]= (min[1]+max[1])/2;
1102                                         snaploc[2]= (min[2]+max[2])/2;
1103                                 }
1104                         }
1105                 }
1106
1107
1108 /* Snap the selection to the snaplocation (duh!) */
1109
1110                 if(G.obedit) {
1111 #ifdef __NLA
1112                         if ELEM5(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
1113 #else
1114                         if ELEM4(G.obedit->type, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
1115 #endif
1116                         if(tottrans==0) return;
1117
1118                         Mat3CpyMat4(bmat, G.obedit->obmat);
1119                         Mat3Inv(imat, bmat);
1120
1121                         tv= transvmain;
1122                         for(a=0; a<tottrans; a++, tv++) {
1123
1124
1125                                 vec[0]= snaploc[0]-G.obedit->obmat[3][0];
1126                                 vec[1]= snaploc[1]-G.obedit->obmat[3][1];
1127                                 vec[2]= snaploc[2]-G.obedit->obmat[3][2];
1128
1129
1130                                 Mat3MulVecfl(imat, vec);
1131                                 VECCOPY(tv->loc, vec);
1132
1133                         }
1134                         MEM_freeN(transvmain);
1135                         transvmain= 0;
1136
1137                         if ELEM(G.obedit->type, OB_SURF, OB_CURVE) makeDispList(G.obedit);
1138
1139                         if (G.obedit->type == OB_ARMATURE)
1140                                 special_trans_update(0);
1141
1142                         allqueue(REDRAWVIEW3D, 0);
1143                         return;
1144                 }
1145 #ifdef __NLA
1146                 if (G.obpose){
1147                         allqueue(REDRAWVIEW3D, 0);
1148                         return;
1149                 }
1150 #endif
1151                 base= (G.scene->base.first);
1152                 while(base) {
1153                         if( ( ((base)->flag & SELECT) && ((base)->lay & G.vd->lay) && ((base)->object->id.lib==0))) {
1154                                 ob= base->object;
1155
1156                                 vec[0]= -ob->obmat[3][0] + snaploc[0];
1157                                 vec[1]= -ob->obmat[3][1] + snaploc[1];
1158                                 vec[2]= -ob->obmat[3][2] + snaploc[2];
1159
1160
1161                                 if(ob->parent) {
1162                                         where_is_object(ob);
1163
1164                                         Mat3Inv(imat, originmat);
1165                                         Mat3MulVecfl(imat, vec);
1166                                         ob->loc[0]+= vec[0];
1167                                         ob->loc[1]+= vec[1];
1168                                         ob->loc[2]+= vec[2];
1169                                 }
1170                                 else {
1171                                         ob->loc[0]+= vec[0];
1172                                         ob->loc[1]+= vec[1];
1173                                         ob->loc[2]+= vec[2];
1174                                 }
1175                         }
1176
1177                         base= base->next;
1178                 }
1179
1180                 allqueue(REDRAWVIEW3D, 0);
1181 }
1182
1183
1184 void snapmenu()
1185 {
1186         short event;
1187
1188         event = pupmenu("Snap %t|Selection -> Grid%x1|Selection -> Cursor%x2|Cursor-> Grid%x3|Cursor-> Selection%x4|Selection-> Center%x5");
1189
1190         switch (event) {
1191                 case 1: /*Selection to grid*/
1192                     snap_sel_to_grid();
1193                         BIF_undo_push("Snap selection to grid");
1194                     break;
1195                 case 2: /*Selection to cursor*/
1196                     snap_sel_to_curs();
1197                         BIF_undo_push("Snap selection to cursor");
1198                     break;          
1199                 case 3: /*Cursor to grid*/
1200                     snap_curs_to_grid();
1201                     break;
1202                 case 4: /*Cursor to selection*/
1203                     snap_curs_to_sel();
1204                     break;
1205                 case 5: /*Selection to center of selection*/
1206                     snap_to_center();
1207                         BIF_undo_push("Snap selection to center");
1208                     break;
1209         }
1210 }
1211
1212
1213 void mergemenu(void)
1214 {
1215         extern float doublimit;
1216         short event;
1217
1218         event = pupmenu("Merge %t|At Center%x1|At Cursor%x2");
1219
1220         if (event==-1) return; /* Return if the menu is closed without any choices */
1221
1222         if (event==1) 
1223                 snap_to_center(); /*Merge at Center*/
1224         else
1225                 snap_sel_to_curs(); /*Merge at Cursor*/
1226
1227         notice("Removed %d Vertices", removedoublesflag(1, doublimit));
1228         allqueue(REDRAWVIEW3D, 0);
1229         countall();
1230         BIF_undo_push("Merge"); /* push the mesh down the undo pipe */
1231
1232 }
1233
1234 void delete_context_selected(void) {
1235         if(G.obedit) {
1236                 if(G.obedit->type==OB_MESH) delete_mesh();
1237                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) delNurb();
1238                 else if(G.obedit->type==OB_MBALL) delete_mball();
1239                 else if (G.obedit->type==OB_ARMATURE) delete_armature();
1240         }
1241         else delete_obj(0);
1242 }
1243
1244 void duplicate_context_selected(void) {
1245         if(G.obedit) {
1246                 if(G.obedit->type==OB_MESH) adduplicate_mesh();
1247                 else if(G.obedit->type==OB_ARMATURE) adduplicate_armature();
1248                 else if(G.obedit->type==OB_MBALL) adduplicate_mball();
1249                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) adduplicate_nurb();
1250         }
1251         else if(!(G.obpose)){
1252                 adduplicate(0);
1253         }
1254 }
1255
1256 void toggle_shading(void) 
1257 {
1258         if(G.qual & LR_SHIFTKEY) {
1259                 if(G.qual & LR_ALTKEY) {
1260                         reshadeall_displist();
1261                         G.vd->drawtype= OB_SHADED;
1262                 }
1263                 else {
1264                         if(G.vd->drawtype== OB_SHADED) G.vd->drawtype= OB_WIRE;
1265                         else G.vd->drawtype= OB_SHADED;
1266                 }
1267         }
1268         else if(G.qual & LR_ALTKEY) {
1269                 if(G.vd->drawtype== OB_TEXTURE) G.vd->drawtype= OB_SOLID;
1270                 else G.vd->drawtype= OB_TEXTURE;
1271         }
1272         else {
1273                 if(G.vd->drawtype==OB_SOLID || G.vd->drawtype==OB_SHADED) G.vd->drawtype= OB_WIRE;
1274                 else G.vd->drawtype= OB_SOLID;
1275         }
1276 }
1277
1278 void minmax_verts(float *min, float *max)
1279 {
1280         extern TransVert *transvmain;
1281         extern int tottrans;
1282         TransVert *tv;
1283         float centroid[3], vec[3], bmat[3][3];
1284         int a;
1285
1286         tottrans=0;
1287 #ifdef __NLA
1288         if ELEM5(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
1289 #else
1290         if ELEM4(G.obedit->type, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) make_trans_verts(bmat[0], bmat[1], 0);
1291 #endif
1292         if(tottrans==0) return;
1293
1294         Mat3CpyMat4(bmat, G.obedit->obmat);
1295         
1296         tv= transvmain;
1297         for(a=0; a<tottrans; a++, tv++) {               
1298                 VECCOPY(vec, tv->loc);
1299                 Mat3MulVecfl(bmat, vec);
1300                 VecAddf(vec, vec, G.obedit->obmat[3]);
1301                 VecAddf(centroid, centroid, vec);
1302                 DO_MINMAX(vec, min, max);               
1303         }
1304         
1305         MEM_freeN(transvmain);
1306         transvmain= 0;
1307 }
1308