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