== Auto-Keyframing ==
[blender-staging.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 #include <string.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #ifndef WIN32
41 #include <unistd.h>
42 #else
43 #include <io.h>
44 #endif   
45
46 #include "MEM_guardedalloc.h"
47
48 #include "BMF_Api.h"
49
50 #include "PIL_time.h"
51
52 #include "DNA_action_types.h"
53 #include "DNA_armature_types.h"
54 #include "DNA_curve_types.h"
55 #include "DNA_ipo_types.h"
56 #include "DNA_lattice_types.h"
57 #include "DNA_meta_types.h"
58 #include "DNA_mesh_types.h"
59 #include "DNA_modifier_types.h"
60 #include "DNA_object_types.h"
61 #include "DNA_screen_types.h"
62 #include "DNA_scene_types.h"
63 #include "DNA_space_types.h"
64 #include "DNA_view3d_types.h"
65 #include "DNA_userdef_types.h"  /* for U.dupflag */
66 #include "BLI_blenlib.h"
67 #include "BLI_arithb.h"
68 #include "BLI_editVert.h"
69 #include "BLI_linklist.h"
70
71 #include "BKE_action.h"
72 #include "BKE_armature.h"
73 #include "BKE_anim.h"
74 #include "BKE_curve.h"
75 #include "BKE_depsgraph.h"
76 #include "BKE_DerivedMesh.h"
77 #include "BKE_displist.h"
78 #include "BKE_global.h"
79 #include "BKE_ipo.h"
80 #include "BKE_lattice.h"
81 #include "BKE_mesh.h"
82 #include "BKE_modifier.h"
83 #include "BKE_object.h"
84 #include "BKE_utildefines.h"
85
86 #ifdef WITH_VERSE
87 #include "BKE_verse.h"
88 #endif
89
90 #include "BIF_editmesh.h"
91 #include "BIF_editview.h"
92 #include "BIF_editarmature.h"
93 #include "BIF_gl.h"
94 #include "BIF_glutil.h"
95 #include "BIF_interface.h"
96 #include "BIF_mywindow.h"
97 #include "BIF_resources.h"
98 #include "BIF_space.h"
99 #include "BIF_screen.h"
100 #include "BIF_toolbox.h"
101
102 #ifdef WITH_VERSE
103 #include "BIF_verse.h"
104 #endif
105
106 #include "BSE_edit.h"
107 #include "BSE_drawipo.h"
108 #include "BSE_drawview.h"
109 #include "BSE_trans_types.h"
110 #include "BSE_view.h"
111
112 #include "BDR_editobject.h"
113 #include "BDR_editmball.h"
114 #include "BDR_editcurve.h"
115
116 /* old stuff */
117 #include "blendef.h"
118 #include "mydevice.h"
119
120 /*#include "armature.h"*/
121 /*  #include "edit.h" */
122 #include "nla.h"
123 #include "transform.h"
124
125 #ifdef __NLA
126 #include "BIF_editarmature.h"
127 #endif
128
129
130 /* circle selection callback */
131 typedef void (*select_CBfunc)(short selecting, Object *editobj, short *mval, float rad);
132
133 extern void obedit_selectionCB(short selecting, Object *editobj, 
134                                short *mval, float rad);
135 extern void uvedit_selectionCB(short selecting, Object *editobj, 
136                                short *mval, float rad);
137
138 void circle_selectCB(select_CBfunc func);
139
140 /* local protos ---------------*/
141 void snap_curs_to_firstsel(void);
142
143 /* flag==2 only border, flag==3 cross+border
144    flag==5 cross + border + start&end frame
145  */
146 int get_border(rcti *rect, short flag)
147 {
148         float dvec[4], fac1, fac2;
149         int retval=1;
150         unsigned short event= 0;
151         short mval[2], mvalo[4], val, x1, y1;
152         char str[64];
153
154         mywinset(G.curscreen->mainwin);
155         
156         /* slightly larger, 1 pixel at the edge */
157         glReadBuffer(GL_FRONT);
158         glDrawBuffer(GL_FRONT);
159
160         /* removed my_get_frontbuffer, this crashes when it gets a part outside the screen */
161         /* solved it with just a redraw! */
162
163         mywinset(curarea->win);
164         
165         glDrawBuffer(GL_FRONT);
166         persp(PERSP_WIN);
167         initgrabz(0.0, 0.0, 0.0);
168         
169         if(flag & 1) {
170                 getmouseco_areawin(mvalo);
171
172                 /* draws the selection initial cross */
173                 sdrawXORline4(0, 0,  mvalo[1],  curarea->winx,  mvalo[1]);
174                 sdrawXORline4(1, mvalo[0],  0,  mvalo[0],  curarea->winy); 
175                 bglFlush();
176                 
177                 while(TRUE) {
178                 
179                         /* selection loop while mouse pressed */
180                         getmouseco_areawin(mval);
181                         
182                         if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
183
184                                 /* aiming cross */
185                                 sdrawXORline4(0, 0,  mval[1],  curarea->winx,  mval[1]);
186                                 sdrawXORline4(1, mval[0],  0,  mval[0],  curarea->winy);
187                                 bglFlush();
188
189                                 mvalo[0]= mval[0];
190                                 mvalo[1]= mval[1];
191                         }
192                         event= extern_qread(&val);
193
194                         if(event && val) {
195
196                                 /* for when a renderwindow is open, and a mouse cursor activates it */
197                                 persp(PERSP_VIEW);
198                                 mywinset(curarea->win);
199                                 persp(PERSP_WIN);
200                                 
201                                 if(event==ESCKEY) {
202                                         retval= 0;
203                                         break;
204                                 }
205                                 else if(event==BKEY) {
206                                         /* b has been pressed twice: proceed with circle select */
207                                         retval= 0;
208                                         break;
209                                 }
210                                 else if(event==LEFTMOUSE) break;
211                                 else if(event==MIDDLEMOUSE) break;
212                                 else if(event==RIGHTMOUSE) break;
213                         }
214                         else PIL_sleep_ms(10);
215                         
216                 } /* end while (TRUE) */
217
218                 /* erase XORed lines */
219                 sdrawXORline4(-1, 0, 0, 0, 0);
220         }
221         else getmouseco_areawin(mval);
222         
223         if(retval) {
224                 /* box select */
225                 x1= mval[0];
226                 y1= mval[1];
227                 
228                 getmouseco_areawin(mvalo);
229
230                 sdrawXORline4(0, x1, y1, x1, mvalo[1]); 
231                 sdrawXORline4(1, x1, mvalo[1], mvalo[0], mvalo[1]); 
232                 sdrawXORline4(2, mvalo[0], mvalo[1], mvalo[0], y1); 
233                 sdrawXORline4(3,  mvalo[0], y1, x1, y1); 
234                 bglFlush();
235                         
236                 while(TRUE) {
237                         getmouseco_areawin(mval);
238                         if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
239
240                                 sdrawXORline4(0, x1, y1, x1, mval[1]); 
241                                 sdrawXORline4(1, x1, mval[1], mval[0], mval[1]); 
242                                 sdrawXORline4(2, mval[0], mval[1], mval[0], y1); 
243                                 sdrawXORline4(3,  mval[0], y1, x1, y1); 
244                                 
245                                 /* draw size information in corner */
246                                 if(curarea->spacetype==SPACE_VIEW3D) {
247                                         BIF_ThemeColor(TH_BACK);
248                                         glRecti(10, 25, 250, 40);
249         
250                                         if(G.vd->persp==0) {
251                                                 window_to_3d(dvec, mvalo[0]-x1, mvalo[1]-y1);
252         
253                                                 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]));
254                                                 glColor3f(0.0, 0.0, 0.0); 
255                                                 glRasterPos2i(15,  27);
256                                                 BMF_DrawString(G.fonts, str);
257                                                 glColor3f(0.7, 0.7, 0.7); 
258                                                 glRasterPos2i(16,  28);
259                                                 BMF_DrawString(G.fonts, str);
260                                         }
261                                         else if(G.vd->persp==2) {
262                                                 rctf vb;
263         
264                                                 calc_viewborder(G.vd, &vb);
265         
266                                                 fac1= (mvalo[0]-x1)/( (float) (vb.xmax-vb.xmin) );
267                                                 fac1*= 0.01*G.scene->r.size*G.scene->r.xsch;
268                                                 
269                                                 fac2= (mvalo[1]-y1)/( (float) (vb.ymax-vb.ymin) );
270                                                 fac2*= 0.01*G.scene->r.size*G.scene->r.ysch;
271                                                 
272                                                 sprintf(str, "X %.1f  Y %.1f  Dia %.1f", fabs(fac1), fabs(fac2), sqrt(fac1*fac1 + fac2*fac2) );
273                                                 glColor3f(0.0, 0.0, 0.0); 
274                                                 glRasterPos2i(15,  27);
275                                                 BMF_DrawString(G.fonts, str);
276                                                 glColor3f(0.7, 0.7, 0.7); 
277                                                 glRasterPos2i(16,  28);
278                                                 BMF_DrawString(G.fonts, str);
279                                         }
280                                 }
281                                 else if(curarea->spacetype==SPACE_IPO) {
282                                         SpaceIpo *sipo= curarea->spacedata.first;
283         
284                                         BIF_ThemeColor(TH_BACK);
285                                         glRecti(20, 30, 170, 40);
286                                                                 
287                                         mvalo[2]= x1;
288                                         mvalo[3]= y1;
289                                         areamouseco_to_ipoco(&sipo->v2d, mval, dvec, dvec+1);
290                                         areamouseco_to_ipoco(&sipo->v2d, mvalo+2, dvec+2, dvec+3);
291
292                                         if (flag == 5) {
293                                                 /* setting preview range */
294                                                 if (dvec[0] < dvec[2])
295                                                         sprintf(str, "Preview Range: %d to %d", (int)dvec[0], (int)dvec[2]);
296                                                 else
297                                                         sprintf(str, "Preview Range: %d to %d", (int)dvec[2], (int)dvec[0]);
298                                         }
299                                         else
300                                                 sprintf(str, "Time: %.4f  Y %.4f", dvec[0]-dvec[2], dvec[1]-dvec[3]);
301                                         
302                                         BIF_ThemeColor(TH_BACK);
303                                         glRecti(14, 24, 165, 38);
304                                         
305                                         glColor3f(0.0, 0.0, 0.0); 
306                                         glRasterPos2i(15,  27);
307                                         BMF_DrawString(G.fonts, str);
308                                         glColor3f(0.8, 0.8, 0.8); 
309                                         glRasterPos2i(16,  28);
310                                         BMF_DrawString(G.fonts, str);
311                                 }
312                                 else if ((ELEM3(curarea->spacetype, SPACE_ACTION, SPACE_NLA, SPACE_TIME)) && flag==5) {
313                                         /* only while setting preview range */
314                                         View2D *v2d;
315                                         
316                                         switch (curarea->spacetype) 
317                                         {
318                                                 case SPACE_ACTION:
319                                                 {
320                                                         SpaceAction *saaction= curarea->spacedata.first;
321                                                         v2d= &saaction->v2d;
322                                                 }
323                                                         break;
324                                                 case SPACE_NLA:
325                                                 {
326                                                         SpaceNla *snla= curarea->spacedata.first;
327                                                         v2d= &snla->v2d;
328                                                 }
329                                                         break;
330                                                 default:
331                                                         v2d= G.v2d;
332                                                         break;
333                                         }
334                                         
335                                         mvalo[2]= x1;
336                                         mvalo[3]= y1;
337                                         areamouseco_to_ipoco(v2d, mval, dvec, dvec+1);
338                                         areamouseco_to_ipoco(v2d, mvalo+2, dvec+2, dvec+3);
339                                         
340                                         if (dvec[0] < dvec[2])
341                                                 sprintf(str, "Preview Range: %d to %d", (int)dvec[0], (int)dvec[2]);
342                                         else
343                                                 sprintf(str, "Preview Range: %d to %d", (int)dvec[2], (int)dvec[0]);
344                                         
345                                         BIF_ThemeColor(TH_BACK);
346                                         glRecti(14, 24, 165, 38);
347                                         
348                                         glColor3f(0.0, 0.0, 0.0); 
349                                         glRasterPos2i(15,  27);
350                                         BMF_DrawString(G.fonts, str);
351                                         glColor3f(0.8, 0.8, 0.8); 
352                                         glRasterPos2i(16,  28);
353                                         BMF_DrawString(G.fonts, str);
354                                 }
355
356                                 bglFlush();
357
358                                 mvalo[0]= mval[0];
359                                 mvalo[1]= mval[1];
360                         }
361                         
362                         event= extern_qread(&val);
363                         
364                         if(event && val==0) {
365                                 /* still because of the renderwindow... */
366                                 persp(PERSP_VIEW);
367                                 mywinset(curarea->win);
368                                 persp(PERSP_WIN);
369                                 
370                                 if(event==ESCKEY) {
371                                         retval= 0;
372                                         break;
373                                 }
374                                 else if(event==LEFTMOUSE) break;
375                                 else if(event==MIDDLEMOUSE) break;
376                                 else if(event==RIGHTMOUSE) break;
377                         }
378                         
379                 } /* end while (TRUE) */
380                 sdrawXORline4(-1, 0, 0, 0, 0);
381                 
382                 if(retval) {
383                         rect->xmin= x1;
384                         rect->ymin= y1;
385                         rect->xmax= mval[0];
386                         rect->ymax= mval[1];
387                         retval= event;
388
389                         /* normalize */
390                         if(rect->xmin>rect->xmax) SWAP(int, rect->xmin, rect->xmax);
391                         if(rect->ymin>rect->ymax) SWAP(int, rect->ymin, rect->ymax);
392                         
393                         if(rect->xmin==rect->xmax) retval= 0;
394                         if(rect->ymin==rect->ymax) retval= 0;
395                 }
396         }
397
398
399         /* clear */
400         if(event!=BKEY) {
401                 if ELEM(curarea->spacetype, SPACE_VIEW3D, SPACE_IPO) {
402                         scrarea_queue_winredraw(curarea);
403                 }
404                 else if ELEM3(curarea->spacetype, SPACE_ACTION, SPACE_NLA, SPACE_TIME) {
405                         scrarea_queue_winredraw(curarea); // only really needed for 
406                 }
407         }
408         
409         bglFlush();
410         glReadBuffer(GL_BACK);
411         glDrawBuffer(GL_BACK);
412
413         persp(PERSP_VIEW);
414         
415         /* pressed B again ? -> brush select */
416         if(event==BKEY) {
417                 setlinestyle(0);
418                 switch (curarea->spacetype) {
419                 case SPACE_VIEW3D:
420                         if (G.obedit) {
421                                 if ELEM4(G.obedit->type, OB_MESH, OB_CURVE, OB_SURF, OB_LATTICE) {
422                                         circle_selectCB(&obedit_selectionCB);
423                                 }
424                         }
425                         else if (G.f&G_FACESELECT) {
426                                 circle_selectCB(&obedit_selectionCB);
427                         }
428                         return 0;
429                         
430                 case SPACE_IMAGE: // brush select in UV editor
431                         circle_selectCB(&uvedit_selectionCB);
432                         // this is a hack; we return 0 that the caller from get_border
433                         // doesn't execute the selection code for border select..
434                         return 0;
435                 }
436         }
437         return retval;
438 }
439
440 void draw_sel_circle(short *mval, short *mvalo, float rad, float rado, int selecting)
441 {
442         static short no_mvalo=0;
443
444         if(mval==NULL && mvalo==NULL) { /* signal */
445                 no_mvalo= 1;
446                 return;
447         }
448
449         persp(PERSP_WIN);
450         glReadBuffer(GL_FRONT);
451         glDrawBuffer(GL_FRONT);
452         //setlinestyle(2);
453
454         /* draw circle */
455         if(mvalo && no_mvalo==0) {
456                 fdrawXORcirc(mvalo[0], mvalo[1], rado);
457         }
458         
459         if(mval) {
460                 fdrawXORcirc(mval[0], mval[1], rad);
461         }
462         //setlinestyle(0);
463
464         bglFlush();
465         persp(PERSP_VIEW);
466         glDrawBuffer(GL_BACK);
467         glReadBuffer(GL_BACK);
468
469         no_mvalo= 0;
470 }
471
472 /** This function does the same as editview.c:circle_select(),
473   * but the selection actions are defined by a callback, making
474   * it (hopefully) reusable for other windows than the 3D view.
475   */
476
477 void circle_selectCB(select_CBfunc callback)
478 {
479         static float rad= 40.0;
480         float rado;
481         int firsttime=1;
482         int escape= 0;
483         unsigned short event;
484         short mvalo[2], mval[2], val;
485         short selecting=0;
486         Object *obj;
487         
488         if(G.obedit) obj = G.obedit;
489         else obj = OBACT;
490
491         mywinset(curarea->win);
492         
493         getmouseco_areawin(mvalo);
494         mval[0]= mvalo[0]; mval[1]= mvalo[1];
495
496         draw_sel_circle(mval, NULL, rad, 0.0, selecting); // draws frontbuffer, but sets backbuf again
497         
498         rado= rad;
499         
500         while(TRUE) {
501                 
502                 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || rado!=rad || firsttime) {
503                         firsttime= 0;
504                         
505                         if(selecting) {
506                                 callback(selecting, obj, mval, rad);
507                         }
508
509                         draw_sel_circle(mval, mvalo, rad, rado, selecting);
510                 
511                         mvalo[0]= mval[0];
512                         mvalo[1]= mval[1];
513                         rado= rad;
514
515                 }
516                 
517                 while(qtest()) {
518                         event= extern_qread(&val);
519                         if (event) {
520
521                                 /* for when another window is open and a mouse cursor activates it */
522                                 if(event!=MOUSEY && event!=MOUSEX) mywinset(curarea->win);
523                                 
524                                 getmouseco_areawin(mval);       // important to do here, trust events!
525                                 
526                                 switch(event) {
527                         
528                                 case LEFTMOUSE:
529                                 case MIDDLEMOUSE:
530                                         if(val) selecting= event;
531                                         else selecting= 0;
532                                         firsttime= 1;
533                                         
534                                         break;
535                                 case PAGEUPKEY:
536                                 case WHEELDOWNMOUSE:
537                                 case PADPLUSKEY:
538                                 case EQUALKEY:
539                                         if(val) if(rad<200.0) rad*= 1.2;
540                                         break;
541                                 case PAGEDOWNKEY:
542                                 case WHEELUPMOUSE:
543                                 case PADMINUS:
544                                 case MINUSKEY:
545                                         if(val) if(rad>5.0) rad/= 1.2;
546                                         break;
547                                 
548                                 case ESCKEY: case SPACEKEY: case RIGHTMOUSE: case INPUTCHANGE: 
549                                 case GKEY: case SKEY: case RKEY: case XKEY: case EKEY: case TABKEY:
550                                         escape= 1;
551                                         break;
552
553                                 }
554                                 
555                                 if(escape) break;
556                         }
557                 }
558                 PIL_sleep_ms(10);
559                 
560                 if(escape) break;
561         }
562         
563         /* clear circle */
564         draw_sel_circle(NULL, mvalo, 0, rad, 1);
565         BIF_undo_push("Circle Select");
566         countall();
567         allqueue(REDRAWINFO, 0);
568 }
569
570 static void count_object(Object *ob, int sel, int totob)
571 {
572         Mesh *me;
573         Curve *cu;
574         DerivedMesh *dm;
575         int tot=0, totf=0;
576
577         switch(ob->type) {
578         case OB_MESH:
579                         G.totmesh+=totob;
580                         me= get_mesh(ob);
581                         if(me) {
582                                         int totvert, totedge, totface;
583                                         dm = mesh_get_derived_final(ob, get_viewedit_datamask());
584                                         totvert = dm->getNumVerts(dm);
585                                         totedge = dm->getNumEdges(dm);
586                                         totface = dm->getNumFaces(dm);
587
588                                         G.totvert+= totvert*totob;
589                                         G.totedge+= totedge*totob;
590                                         G.totface+= totface*totob;
591                                         if(sel) {
592                                                         G.totvertsel+= totvert;
593                                                         G.totfacesel+= totface;
594                                         }
595                         }
596                         break;
597
598         case OB_LAMP:
599                 G.totlamp+=totob;
600                 break;
601         case OB_SURF:
602         case OB_CURVE:
603         case OB_FONT:
604                 G.totcurve+=totob;
605                 tot=totf= 0;
606                 cu= ob->data;
607                 if(cu->disp.first)
608                         count_displist( &cu->disp, &tot, &totf);
609                 tot*= totob;
610                 totf*= totob;
611                 G.totvert+= tot;
612                 G.totface+= totf;
613                 if(sel) {
614                         G.totvertsel+= tot;
615                         G.totfacesel+= totf;
616                 }
617                 break;
618         case OB_MBALL:
619                 count_displist( &ob->disp, &tot, &totf);
620                 tot*= totob;
621                 totf*= totob;
622                 G.totvert+= tot;
623                 G.totface+= totf;
624                 if(sel) {
625                         G.totvertsel+= tot;
626                         G.totfacesel+= totf;
627                 }
628                 break;
629         }
630         
631 }
632
633 /* countall does statistics */
634 /* is called on most actions, like select/add/delete/layermove */
635 void countall()
636 {
637         extern ListBase editNurb;
638         Base *base;
639         Object *ob= OBACT;
640         Mesh *me;
641         Nurb *nu;
642         BezTriple *bezt;
643         BPoint *bp;
644         MetaElem *ml;
645         struct EditBone *ebo;
646         int a;
647
648         G.totvert= G.totvertsel= G.totedge= G.totedgesel= G.totfacesel= G.totface= G.totobj= 
649             G.totmesh= G.totlamp= G.totcurve= G.totobjsel= G.totbone= G.totbonesel=  0;
650
651         if(G.obedit) {
652                 
653                 if(G.obedit->type==OB_MESH) {
654                         EditMesh *em = G.editMesh;
655                         EditVert *eve;
656                         EditEdge *eed;
657                         EditFace *efa;
658                         
659                         for(eve= em->verts.first; eve; eve= eve->next) {
660                                 G.totvert++;
661                                 if(eve->f & SELECT) G.totvertsel++;
662                         }
663                         for(eed= em->edges.first; eed; eed= eed->next) {
664                                 G.totedge++;
665                                 if(eed->f & SELECT) G.totedgesel++;
666                         }
667                         for(efa= em->faces.first; efa; efa= efa->next) {
668                                 G.totface++;
669                                 if(efa->f & SELECT) G.totfacesel++;
670                         }
671                         
672                         EM_validate_selections();
673                 }
674                 else if (G.obedit->type==OB_ARMATURE){
675                         for (ebo=G.edbo.first;ebo;ebo=ebo->next){
676                                 G.totbone++;
677                                 
678                                 /* Sync selection to parent for connected children */
679                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
680                                         G.totvert--;
681                                         if (ebo->parent->flag & BONE_TIPSEL)
682                                                 ebo->flag |= BONE_ROOTSEL;
683                                         else
684                                                 ebo->flag &= ~BONE_ROOTSEL;
685                                 }
686                                 
687                                 if (ebo->flag & BONE_TIPSEL)
688                                         G.totvertsel++;
689                                 if (ebo->flag & BONE_ROOTSEL)
690                                         G.totvertsel++;
691                                 
692                                 if ((ebo->flag & BONE_TIPSEL) && (ebo->flag & BONE_ROOTSEL))
693                                         ebo->flag |= BONE_SELECTED;
694                                 else
695                                         ebo->flag &= ~BONE_SELECTED;
696                                 
697                                 if(ebo->flag & BONE_SELECTED) G.totbonesel++;
698
699                                 //      If this is a connected child and it's parent is being moved, remove our root
700                                 if ((ebo->flag & BONE_CONNECTED)&& (ebo->flag & BONE_ROOTSEL) && ebo->parent && (ebo->parent->flag & BONE_TIPSEL)){
701                                         G.totvertsel--;
702                                 }
703
704                                 G.totvert+=2;
705                         }
706                 }
707                 else if ELEM3(G.obedit->type, OB_CURVE, OB_SURF, OB_FONT) {
708                         nu= editNurb.first;
709                         while(nu) {
710                                 if((nu->type & 7)==CU_BEZIER) {
711                                         bezt= nu->bezt;
712                                         a= nu->pntsu;
713                                         while(a--) {
714                                                 G.totvert+=3;
715                                                 if(bezt->f1) G.totvertsel++;
716                                                 if(bezt->f2) G.totvertsel++;
717                                                 if(bezt->f3) G.totvertsel++;
718                                                 bezt++;
719                                         }
720                                 }
721                                 else {
722                                         bp= nu->bp;
723                                         a= nu->pntsu*nu->pntsv;
724                                         while(a--) {
725                                                 G.totvert++;
726                                                 if(bp->f1 & 1) G.totvertsel++;
727                                                 bp++;
728                                         }
729                                 }
730                                 nu= nu->next;
731                         }
732                 }
733                 else if(G.obedit->type==OB_MBALL) {
734                         /* editmball.c */
735                         extern ListBase editelems;  /* go away ! */
736                         
737                         ml= editelems.first;
738                         while(ml) {
739                                 G.totvert++;
740                                 if(ml->flag & SELECT) G.totvertsel++;
741                                 ml= ml->next;
742                         }
743                 }
744                 else if(G.obedit->type==OB_LATTICE) {
745                         bp= editLatt->def;
746                         
747                         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
748                         while(a--) {
749                                 G.totvert++;
750                                 if(bp->f1 & 1) G.totvertsel++;
751                                 bp++;
752                         }
753                 }
754                 
755                 allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
756                 return;
757         }
758         else if(ob && (ob->flag & OB_POSEMODE)) {
759                 if(ob->pose) {
760                         bArmature *arm= ob->data;
761                         bPoseChannel *pchan;
762                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
763                                 G.totbone++;
764                                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED))
765                                         if(pchan->bone->layer & arm->layer)
766                                                 G.totbonesel++;
767                         }
768                 }
769                 allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
770                 return;
771         }
772         else if(G.f & (G_FACESELECT + G_VERTEXPAINT + G_TEXTUREPAINT +G_WEIGHTPAINT)) {
773                 me= get_mesh((G.scene->basact) ? (G.scene->basact->object) : 0);
774                 if(me) {
775                         G.totface= me->totface;
776                         G.totvert= me->totvert;
777                 }
778                 allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
779                 return;
780         }
781
782         if(G.scene==NULL) return;
783
784         base= (G.scene->base.first);
785         while(base) {
786                 if(G.scene->lay & base->lay) {
787                         ob= base->object;       /* warning, ob not is obact anymore */
788                         
789                         if(base->flag & SELECT) G.totobjsel++;
790                         
791                         if(ob->parent && (ob->parent->transflag & (OB_DUPLIVERTS|OB_DUPLIFACES))) {
792                                 int tot= count_duplilist(ob->parent);
793                                 G.totobj+=tot;
794                                 count_object(ob, base->flag & SELECT, tot);
795                         }
796                         else if(ob->transflag & OB_DUPLIFRAMES) {
797                                 int tot= count_duplilist(ob);
798                                 G.totobj+=tot;
799                                 count_object(ob, base->flag & SELECT, tot);
800                         }
801                         else {
802                                 count_object(ob, base->flag & SELECT, 1);
803                                 G.totobj++;
804                         }
805                 }
806                 base= base->next;
807         }
808         allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
809 }
810
811 /* ************************************************** */
812 /* ********************* old transform stuff ******** */
813 /* ************************************************** */
814
815 static TransVert *transvmain=NULL;
816 static int tottrans= 0;
817
818 /* copied from editobject.c, now uses (almost) proper depgraph */
819 static void special_transvert_update(void)
820 {
821         
822         if(G.obedit) {
823                 
824                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
825                 
826                 if(G.obedit->type==OB_MESH) {
827 #ifdef WITH_VERSE
828                         if(G.editMesh->vnode)
829                                 sync_all_verseverts_with_editverts((VNode*)G.editMesh->vnode);
830 #endif
831                         recalc_editnormals();   // does face centers too
832                 }
833                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
834                         extern ListBase editNurb;
835                         Nurb *nu= editNurb.first;
836                         while(nu) {
837                                 test2DNurb(nu);
838                                 testhandlesNurb(nu); /* test for bezier too */
839                                 nu= nu->next;
840                         }
841                 }
842                 else if(G.obedit->type==OB_ARMATURE){
843                         bArmature *arm= G.obedit->data;
844                         EditBone *ebo;
845                         TransVert *tv= transvmain;
846                         int a=0;
847                         
848                         /* Ensure all bone tails are correctly adjusted */
849                         for (ebo=G.edbo.first; ebo; ebo=ebo->next) {
850                                 /* adjust tip if both ends selected */
851                                 if ((ebo->flag & BONE_ROOTSEL) && (ebo->flag & BONE_TIPSEL)) {
852                                         if (tv) {
853                                                 float diffvec[3];
854                                                 
855                                                 VecSubf(diffvec, tv->loc, tv->oldloc);
856                                                 VecAddf(ebo->tail, ebo->tail, diffvec);
857                                                 
858                                                 a++;
859                                                 if (a<tottrans) tv++;
860                                         }
861                                 }
862                         }
863                         
864                         /* Ensure all bones are correctly adjusted */
865                         for (ebo=G.edbo.first; ebo; ebo=ebo->next) {
866                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
867                                         /* If this bone has a parent tip that has been moved */
868                                         if (ebo->parent->flag & BONE_TIPSEL){
869                                                 VECCOPY (ebo->head, ebo->parent->tail);
870                                         }
871                                         /* If this bone has a parent tip that has NOT been moved */
872                                         else{
873                                                 VECCOPY (ebo->parent->tail, ebo->head);
874                                         }
875                                 }
876                         }
877                         if(arm->flag & ARM_MIRROR_EDIT) 
878                                 transform_armature_mirror_update();
879                 }
880                 else if(G.obedit->type==OB_LATTICE) {
881                         if(editLatt->flag & LT_OUTSIDE) outside_lattice(editLatt);
882                 }
883         }
884 }
885
886 /* copied from editobject.c, needs to be replaced with new transform code still */
887 /* mode: 1 = proportional */
888 static void make_trans_verts(float *min, float *max, int mode)  
889 {
890         extern ListBase editNurb;
891         EditMesh *em = G.editMesh;
892         Nurb *nu;
893         BezTriple *bezt;
894         BPoint *bp;
895         TransVert *tv=NULL;
896         MetaElem *ml;
897         EditVert *eve;
898         EditBone        *ebo;
899         float total, center[3], centroid[3];
900         int a;
901
902         tottrans= 0; // global!
903         
904         INIT_MINMAX(min, max);
905         centroid[0]=centroid[1]=centroid[2]= 0.0;
906         
907         /* note for transform refactor: dont rely on countall anymore... its ancient */
908         /* I skip it for editmesh now (ton) */
909         if(G.obedit->type!=OB_MESH) {
910                 countall();
911                 if(mode) tottrans= G.totvert;
912                 else tottrans= G.totvertsel;
913
914                 if(G.totvertsel==0) {
915                         tottrans= 0;
916                         return;
917                 }
918                 tv=transvmain= MEM_callocN(tottrans*sizeof(TransVert), "maketransverts");
919         }
920         
921         /* we count again because of hide (old, not for mesh!) */
922         tottrans= 0;
923         
924         if(G.obedit->type==OB_MESH) {
925                 int proptrans= 0;
926                 
927                 // transform now requires awareness for select mode, so we tag the f1 flags in verts
928                 tottrans= 0;
929                 if(G.scene->selectmode & SCE_SELECT_VERTEX) {
930                         for(eve= em->verts.first; eve; eve= eve->next) {
931                                 if(eve->h==0 && (eve->f & SELECT)) {
932                                         eve->f1= SELECT;
933                                         tottrans++;
934                                 }
935                                 else eve->f1= 0;
936                         }
937                 }
938                 else if(G.scene->selectmode & SCE_SELECT_EDGE) {
939                         EditEdge *eed;
940                         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
941                         for(eed= em->edges.first; eed; eed= eed->next) {
942                                 if(eed->h==0 && (eed->f & SELECT)) eed->v1->f1= eed->v2->f1= SELECT;
943                         }
944                         for(eve= em->verts.first; eve; eve= eve->next) if(eve->f1) tottrans++;
945                 }
946                 else {
947                         EditFace *efa;
948                         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
949                         for(efa= em->faces.first; efa; efa= efa->next) {
950                                 if(efa->h==0 && (efa->f & SELECT)) {
951                                         efa->v1->f1= efa->v2->f1= efa->v3->f1= SELECT;
952                                         if(efa->v4) efa->v4->f1= SELECT;
953                                 }
954                         }
955                         for(eve= em->verts.first; eve; eve= eve->next) if(eve->f1) tottrans++;
956                 }
957                 
958                 /* proportional edit exception... */
959                 if(mode==1 && tottrans) {
960                         for(eve= em->verts.first; eve; eve= eve->next) {
961                                 if(eve->h==0) {
962                                         eve->f1 |= 2;
963                                         proptrans++;
964                                 }
965                         }
966                         if(proptrans>tottrans) tottrans= proptrans;
967                 }
968                 
969                 /* and now make transverts */
970                 if(tottrans) {
971                         tv=transvmain= MEM_callocN(tottrans*sizeof(TransVert), "maketransverts");
972
973                         for(eve= em->verts.first; eve; eve= eve->next) {
974                                 if(eve->f1) {
975                                         VECCOPY(tv->oldloc, eve->co);
976                                         tv->loc= eve->co;
977                                         if(eve->no[0]!=0.0 || eve->no[1]!=0.0 ||eve->no[2]!=0.0)
978                                                 tv->nor= eve->no; // note this is a hackish signal (ton)
979                                         tv->flag= eve->f1 & SELECT;
980                                         tv++;
981                                 }
982                         }
983                 }
984         }
985         else if (G.obedit->type==OB_ARMATURE){
986                 bArmature *arm= G.obedit->data;
987                 
988                 for (ebo=G.edbo.first;ebo;ebo=ebo->next){
989                         if(ebo->layer & arm->layer) {
990                                 short tipsel= (ebo->flag & BONE_TIPSEL);
991                                 short rootsel= (ebo->flag & BONE_ROOTSEL);
992                                 short rootok= (!(ebo->parent && (ebo->flag & BONE_CONNECTED) && ebo->parent->flag & BONE_TIPSEL));
993                                 
994                                 if ((tipsel && rootsel) || (rootsel)) {
995                                         /* Only add the root if there is no connection.
996                                          * Don't add the tip, otherwise we get zero-length bones.
997                                          */
998                                         if (rootok) {
999                                                 VECCOPY (tv->oldloc, ebo->head);
1000                                                 tv->loc= ebo->head;
1001                                                 tv->nor= NULL;
1002                                                 tv->flag= 1;
1003                                                 tv++;
1004                                                 tottrans++;
1005                                         }       
1006                                 }
1007                                 else if (tipsel) {
1008                                         VECCOPY (tv->oldloc, ebo->tail);
1009                                         tv->loc= ebo->tail;
1010                                         tv->nor= NULL;
1011                                         tv->flag= 1;
1012                                         tv++;
1013                                         tottrans++;
1014                                 }
1015                         }                       
1016                 }
1017         }
1018         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1019                 nu= editNurb.first;
1020                 while(nu) {
1021                         if((nu->type & 7)==CU_BEZIER) {
1022                                 a= nu->pntsu;
1023                                 bezt= nu->bezt;
1024                                 while(a--) {
1025                                         if(bezt->hide==0) {
1026                                                 if(mode==1 || (bezt->f1 & 1)) {
1027                                                         VECCOPY(tv->oldloc, bezt->vec[0]);
1028                                                         tv->loc= bezt->vec[0];
1029                                                         tv->flag= bezt->f1 & 1;
1030                                                         tv++;
1031                                                         tottrans++;
1032                                                 }
1033                                                 if(mode==1 || (bezt->f2 & 1)) {
1034                                                         VECCOPY(tv->oldloc, bezt->vec[1]);
1035                                                         tv->loc= bezt->vec[1];
1036                                                         tv->val= &(bezt->alfa);
1037                                                         tv->oldval= bezt->alfa;
1038                                                         tv->flag= bezt->f2 & 1;
1039                                                         tv++;
1040                                                         tottrans++;
1041                                                 }
1042                                                 if(mode==1 || (bezt->f3 & 1)) {
1043                                                         VECCOPY(tv->oldloc, bezt->vec[2]);
1044                                                         tv->loc= bezt->vec[2];
1045                                                         tv->flag= bezt->f3 & 1;
1046                                                         tv++;
1047                                                         tottrans++;
1048                                                 }
1049                                         }
1050                                         bezt++;
1051                                 }
1052                         }
1053                         else {
1054                                 a= nu->pntsu*nu->pntsv;
1055                                 bp= nu->bp;
1056                                 while(a--) {
1057                                         if(bp->hide==0) {
1058                                                 if(mode==1 || (bp->f1 & 1)) {
1059                                                         VECCOPY(tv->oldloc, bp->vec);
1060                                                         tv->loc= bp->vec;
1061                                                         tv->val= &(bp->alfa);
1062                                                         tv->oldval= bp->alfa;
1063                                                         tv->flag= bp->f1 & 1;
1064                                                         tv++;
1065                                                         tottrans++;
1066                                                 }
1067                                         }
1068                                         bp++;
1069                                 }
1070                         }
1071                         nu= nu->next;
1072                 }
1073         }
1074         else if(G.obedit->type==OB_MBALL) {
1075                 extern ListBase editelems;  /* go away ! */
1076                 ml= editelems.first;
1077                 while(ml) {
1078                         if(ml->flag & SELECT) {
1079                                 tv->loc= &ml->x;
1080                                 VECCOPY(tv->oldloc, tv->loc);
1081                                 tv->val= &(ml->rad);
1082                                 tv->oldval= ml->rad;
1083                                 tv->flag= 1;
1084                                 tv++;
1085                                 tottrans++;
1086                         }
1087                         ml= ml->next;
1088                 }
1089         }
1090         else if(G.obedit->type==OB_LATTICE) {
1091                 bp= editLatt->def;
1092                 
1093                 a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1094                 
1095                 while(a--) {
1096                         if(mode==1 || (bp->f1 & 1)) {
1097                                 if(bp->hide==0) {
1098                                         VECCOPY(tv->oldloc, bp->vec);
1099                                         tv->loc= bp->vec;
1100                                         tv->flag= bp->f1 & 1;
1101                                         tv++;
1102                                         tottrans++;
1103                                 }
1104                         }
1105                         bp++;
1106                 }
1107         }
1108         
1109         /* cent etc */
1110         tv= transvmain;
1111         total= 0.0;
1112         for(a=0; a<tottrans; a++, tv++) {
1113                 if(tv->flag & SELECT) {
1114                         centroid[0]+= tv->oldloc[0];
1115                         centroid[1]+= tv->oldloc[1];
1116                         centroid[2]+= tv->oldloc[2];
1117                         total+= 1.0;
1118                         DO_MINMAX(tv->oldloc, min, max);
1119                 }
1120         }
1121         if(total!=0.0) {
1122                 centroid[0]/= total;
1123                 centroid[1]/= total;
1124                 centroid[2]/= total;
1125         }
1126
1127         center[0]= (min[0]+max[0])/2.0;
1128         center[1]= (min[1]+max[1])/2.0;
1129         center[2]= (min[2]+max[2])/2.0;
1130         
1131 }
1132
1133 void snap_sel_to_grid()
1134 {
1135         extern float originmat[3][3];   /* object.c */
1136         TransVert *tv;
1137         Base *base;
1138         Object *ob;
1139         float gridf, imat[3][3], bmat[3][3], vec[3];
1140         int a;
1141
1142         gridf= G.vd->gridview;
1143
1144
1145         if(G.obedit) {
1146                 tottrans= 0;
1147                 
1148                 if ELEM6(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE, OB_MBALL) 
1149                         make_trans_verts(bmat[0], bmat[1], 0);
1150                 if(tottrans==0) return;
1151
1152                 Mat3CpyMat4(bmat, G.obedit->obmat);
1153                 Mat3Inv(imat, bmat);
1154
1155                 tv= transvmain;
1156                 for(a=0; a<tottrans; a++, tv++) {
1157
1158                         VECCOPY(vec, tv->loc);
1159                         Mat3MulVecfl(bmat, vec);
1160                         VecAddf(vec, vec, G.obedit->obmat[3]);
1161                         vec[0]= G.vd->gridview*floor(.5+ vec[0]/gridf);
1162                         vec[1]= G.vd->gridview*floor(.5+ vec[1]/gridf);
1163                         vec[2]= G.vd->gridview*floor(.5+ vec[2]/gridf);
1164                         VecSubf(vec, vec, G.obedit->obmat[3]);
1165
1166                         Mat3MulVecfl(imat, vec);
1167                         VECCOPY(tv->loc, vec);
1168
1169                 }
1170
1171                 special_transvert_update();
1172                 
1173                 MEM_freeN(transvmain);
1174                 transvmain= 0;
1175
1176                 allqueue(REDRAWVIEW3D, 0);
1177                 return;
1178         }
1179
1180         base= (G.scene->base.first);
1181         while(base) {
1182                 if( ( ((base)->flag & SELECT) && ((base)->lay & G.vd->lay) && ((base)->object->id.lib==0))) {
1183                         ob= base->object;
1184                         if(ob->flag & OB_POSEMODE) {
1185                                 bPoseChannel *pchan;
1186                                 bArmature *arm= ob->data;
1187                                 
1188                                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1189                                         if(pchan->bone->flag & BONE_SELECTED) {
1190                                                 if(pchan->bone->layer & arm->layer) {
1191                                                         if((pchan->bone->flag & BONE_CONNECTED)==0) { 
1192                                                                 float vecN[3], nLoc[3]; 
1193                                                                 
1194                                                                 /* get nearest grid point to snap to */
1195                                                                 VECCOPY(nLoc, pchan->pose_mat[3]);
1196                                                                 vec[0]= gridf * (float)(floor(.5+ nLoc[0]/gridf));
1197                                                                 vec[1]= gridf * (float)(floor(.5+ nLoc[1]/gridf));
1198                                                                 vec[2]= gridf * (float)(floor(.5+ nLoc[2]/gridf));
1199                                                                 
1200                                                                 /* get bone-space location of grid point */
1201                                                                 armature_loc_pose_to_bone(pchan, vec, vecN);
1202                                                                 
1203                                                                 /* adjust location */
1204                                                                 VECCOPY(pchan->loc, vecN);
1205                                                         }
1206                                                         /* if the bone has a parent and is connected to the parent, 
1207                                                          * don't do anything - will break chain unless we do auto-ik. 
1208                                                          */
1209                                                 }
1210                                         }
1211                                 }
1212                                 ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1213                                 
1214                                 /* autokeyframing */
1215                                 autokeyframe_pose_cb_func(ob, TFM_TRANSLATION, 0);
1216                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1217                         }
1218                         else {
1219                                 ob->recalc |= OB_RECALC_OB;
1220                                 
1221                                 vec[0]= -ob->obmat[3][0]+G.vd->gridview*floor(.5+ ob->obmat[3][0]/gridf);
1222                                 vec[1]= -ob->obmat[3][1]+G.vd->gridview*floor(.5+ ob->obmat[3][1]/gridf);
1223                                 vec[2]= -ob->obmat[3][2]+G.vd->gridview*floor(.5+ ob->obmat[3][2]/gridf);
1224
1225                                 if(ob->parent) {
1226                                         where_is_object(ob);
1227
1228                                         Mat3Inv(imat, originmat);
1229                                         Mat3MulVecfl(imat, vec);
1230                                         ob->loc[0]+= vec[0];
1231                                         ob->loc[1]+= vec[1];
1232                                         ob->loc[2]+= vec[2];
1233                                 }
1234                                 else {
1235                                         ob->loc[0]+= vec[0];
1236                                         ob->loc[1]+= vec[1];
1237                                         ob->loc[2]+= vec[2];
1238                                 }
1239 #ifdef WITH_VERSE
1240                                 if(ob->vnode) b_verse_send_transformation(ob);
1241 #endif
1242                         
1243                                 /* auto-keyframing */
1244                                 autokeyframe_ob_cb_func(ob, TFM_TRANSLATION);
1245                         }
1246                 }
1247
1248                 base= base->next;
1249         }
1250         DAG_scene_flush_update(G.scene, screen_view3d_layers());
1251         allqueue(REDRAWVIEW3D, 0);
1252 }
1253
1254 void snap_sel_to_curs()
1255 {
1256         extern float originmat[3][3];   /* object.c */
1257         TransVert *tv;
1258         Base *base;
1259         Object *ob;
1260         float *curs, imat[3][3], bmat[3][3], vec[3];
1261         int a;
1262
1263         curs= give_cursor();
1264
1265         if(G.obedit) {
1266                 tottrans= 0;
1267
1268                 if ELEM6(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE, OB_MBALL) 
1269                         make_trans_verts(bmat[0], bmat[1], 0);
1270                 if(tottrans==0) return;
1271
1272                 Mat3CpyMat4(bmat, G.obedit->obmat);
1273                 Mat3Inv(imat, bmat);
1274
1275                 tv= transvmain;
1276                 for(a=0; a<tottrans; a++, tv++) {
1277
1278                         vec[0]= curs[0]-G.obedit->obmat[3][0];
1279                         vec[1]= curs[1]-G.obedit->obmat[3][1];
1280                         vec[2]= curs[2]-G.obedit->obmat[3][2];
1281
1282                         Mat3MulVecfl(imat, vec);
1283                         VECCOPY(tv->loc, vec);
1284
1285                 }
1286                 
1287                 special_transvert_update();
1288                 
1289                 MEM_freeN(transvmain);
1290                 transvmain= 0;
1291
1292                 allqueue(REDRAWVIEW3D, 0);
1293                 return;
1294         }
1295
1296         base= (G.scene->base.first);
1297         while(base) {
1298                 if( ( ((base)->flag & SELECT) && ((base)->lay & G.vd->lay) && ((base)->object->id.lib==0))) {
1299                         ob= base->object;
1300                         if(ob->flag & OB_POSEMODE) {
1301                                 bPoseChannel *pchan;
1302                                 bArmature *arm= ob->data;
1303                                 float cursp[3];
1304                                 
1305                                 Mat4Invert(ob->imat, ob->obmat);
1306                                 VECCOPY(cursp, curs);
1307                                 Mat4MulVecfl(ob->imat, cursp);
1308                                 
1309                                 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1310                                         if(pchan->bone->flag & BONE_SELECTED) {
1311                                                 if(pchan->bone->layer & arm->layer) {
1312                                                         if((pchan->bone->flag & BONE_CONNECTED)==0) { 
1313                                                                 float curspn[3];
1314                                                                 
1315                                                                 /* get location of cursor in bone-space */
1316                                                                 armature_loc_pose_to_bone(pchan, cursp, curspn);
1317                                                                 
1318                                                                 /* calculate new position */
1319                                                                 VECCOPY(pchan->loc, curspn);
1320                                                         }
1321                                                         /* if the bone has a parent and is connected to the parent, 
1322                                                          * don't do anything - will break chain unless we do auto-ik. 
1323                                                          */
1324                                                 }
1325                                         }
1326                                 }
1327                                 ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1328                                 
1329                                 /* autokeyframing */
1330                                 autokeyframe_pose_cb_func(ob, TFM_TRANSLATION, 0);
1331                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1332                         }
1333                         else {
1334                                 ob->recalc |= OB_RECALC_OB;
1335                                 
1336                                 vec[0]= -ob->obmat[3][0] + curs[0];
1337                                 vec[1]= -ob->obmat[3][1] + curs[1];
1338                                 vec[2]= -ob->obmat[3][2] + curs[2];
1339
1340                                 if(ob->parent) {
1341                                         where_is_object(ob);
1342
1343                                         Mat3Inv(imat, originmat);
1344                                         Mat3MulVecfl(imat, vec);
1345                                         ob->loc[0]+= vec[0];
1346                                         ob->loc[1]+= vec[1];
1347                                         ob->loc[2]+= vec[2];
1348                                 }
1349                                 else {
1350                                         ob->loc[0]+= vec[0];
1351                                         ob->loc[1]+= vec[1];
1352                                         ob->loc[2]+= vec[2];
1353                                 }
1354 #ifdef WITH_VERSE
1355                                 if(ob->vnode) b_verse_send_transformation(ob);
1356 #endif
1357                                 
1358                                 /* auto-keyframing */
1359                                 autokeyframe_ob_cb_func(ob, TFM_TRANSLATION);
1360                         }
1361                 }
1362
1363                 base= base->next;
1364         }
1365         DAG_scene_flush_update(G.scene, screen_view3d_layers());
1366         allqueue(REDRAWVIEW3D, 0);
1367 }
1368
1369 void snap_curs_to_grid()
1370 {
1371         float gridf, *curs;
1372
1373         gridf= G.vd->gridview;
1374         curs= give_cursor();
1375
1376         curs[0]= G.vd->gridview*floor(.5+curs[0]/gridf);
1377         curs[1]= G.vd->gridview*floor(.5+curs[1]/gridf);
1378         curs[2]= G.vd->gridview*floor(.5+curs[2]/gridf);
1379
1380         allqueue(REDRAWVIEW3D, 0);
1381
1382 }
1383
1384 void snap_curs_to_sel()
1385 {
1386         TransVert *tv;
1387         Base *base;
1388         float *curs, bmat[3][3], vec[3], min[3], max[3], centroid[3];
1389         int count, a;
1390
1391         curs= give_cursor();
1392
1393         count= 0;
1394         INIT_MINMAX(min, max);
1395         centroid[0]= centroid[1]= centroid[2]= 0.0;
1396
1397         if(G.obedit) {
1398                 tottrans=0;
1399
1400                 if ELEM6(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE, OB_MBALL) 
1401                         make_trans_verts(bmat[0], bmat[1], 0);
1402                 if(tottrans==0) return;
1403
1404                 Mat3CpyMat4(bmat, G.obedit->obmat);
1405
1406                 tv= transvmain;
1407                 for(a=0; a<tottrans; a++, tv++) {
1408                         VECCOPY(vec, tv->loc);
1409                         Mat3MulVecfl(bmat, vec);
1410                         VecAddf(vec, vec, G.obedit->obmat[3]);
1411                         VecAddf(centroid, centroid, vec);
1412                         DO_MINMAX(vec, min, max);
1413                 }
1414
1415                 if(G.vd->around==V3D_CENTROID) {
1416                         VecMulf(centroid, 1.0/(float)tottrans);
1417                         VECCOPY(curs, centroid);
1418                 }
1419                 else {
1420                         curs[0]= (min[0]+max[0])/2;
1421                         curs[1]= (min[1]+max[1])/2;
1422                         curs[2]= (min[2]+max[2])/2;
1423                 }
1424                 MEM_freeN(transvmain);
1425                 transvmain= 0;
1426         }
1427         else {
1428                 Object *ob= OBACT;
1429                 
1430                 if(ob && (ob->flag & OB_POSEMODE)) {
1431                         bArmature *arm= ob->data;
1432                         bPoseChannel *pchan;
1433                         for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1434                                 if(arm->layer & pchan->bone->layer) {
1435                                         if(pchan->bone->flag & BONE_SELECTED) {
1436                                                 VECCOPY(vec, pchan->pose_head);
1437                                                 Mat4MulVecfl(ob->obmat, vec);
1438                                                 VecAddf(centroid, centroid, vec);
1439                                                 DO_MINMAX(vec, min, max);
1440                                                 count++;
1441                                         }
1442                                 }
1443                         }
1444                 }
1445                 else {
1446                         for(base= G.scene->base.first; base; base= base->next) {
1447                                 if(((base)->flag & SELECT) && ((base)->lay & G.vd->lay) ) {
1448                                         VECCOPY(vec, base->object->obmat[3]);
1449                                         VecAddf(centroid, centroid, vec);
1450                                         DO_MINMAX(vec, min, max);
1451                                         count++;
1452                                 }
1453                         }
1454                 }
1455                 if(count) {
1456                         if(G.vd->around==V3D_CENTROID) {
1457                                 VecMulf(centroid, 1.0/(float)count);
1458                                 VECCOPY(curs, centroid);
1459                         }
1460                         else {
1461                                 curs[0]= (min[0]+max[0])/2;
1462                                 curs[1]= (min[1]+max[1])/2;
1463                                 curs[2]= (min[2]+max[2])/2;
1464                         }
1465                 }
1466         }
1467         allqueue(REDRAWVIEW3D, 0);
1468 }
1469
1470 void snap_curs_to_firstsel()
1471 {
1472         TransVert *tv;
1473         Base *base;
1474         float *curs, bmat[3][3], vec[3], min[3], max[3], centroid[3];
1475         int count;
1476
1477         curs= give_cursor();
1478
1479         count= 0;
1480         INIT_MINMAX(min, max);
1481         centroid[0]= centroid[1]= centroid[2]= 0.0;
1482
1483         if(G.obedit) {
1484                 tottrans=0;
1485
1486                 if ELEM6(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE, OB_MBALL) 
1487                         make_trans_verts(bmat[0], bmat[1], 0);
1488                 if(tottrans==0) return;
1489
1490                 Mat3CpyMat4(bmat, G.obedit->obmat);
1491
1492                 tv= transvmain;
1493                 VECCOPY(vec, tv->loc);
1494                         /*Mat3MulVecfl(bmat, vec);
1495                         VecAddf(vec, vec, G.obedit->obmat[3]);
1496                         VecAddf(centroid, centroid, vec);
1497                         DO_MINMAX(vec, min, max);*/
1498
1499                 if(G.vd->around==V3D_CENTROID) {
1500                         VecMulf(vec, 1.0/(float)tottrans);
1501                         VECCOPY(curs, vec);
1502                 }
1503                 else {
1504                         curs[0]= vec[0];
1505                         curs[1]= vec[1];
1506                         curs[2]= vec[2];
1507                 }
1508                 MEM_freeN(transvmain);
1509                 transvmain= 0;
1510         }
1511         else {
1512                 base= (G.scene->base.first);
1513                 while(base) {
1514                         if(((base)->flag & SELECT) && ((base)->lay & G.vd->lay) ) {
1515                                 VECCOPY(vec, base->object->obmat[3]);
1516                                 VecAddf(centroid, centroid, vec);
1517                                 DO_MINMAX(vec, min, max);
1518                                 count++;
1519                         }
1520                         base= base->next;
1521                 }
1522                 if(count) {
1523                         if(G.vd->around==V3D_CENTROID) {
1524                                 VecMulf(centroid, 1.0/(float)count);
1525                                 VECCOPY(curs, centroid);
1526                         }
1527                         else {
1528                                 curs[0]= (min[0]+max[0])/2;
1529                                 curs[1]= (min[1]+max[1])/2;
1530                                 curs[2]= (min[2]+max[2])/2;
1531                         }
1532                 }
1533         }
1534         allqueue(REDRAWVIEW3D, 0);
1535 }
1536
1537 void snap_to_center()
1538 {
1539         extern float originmat[3][3];
1540         TransVert *tv;
1541         Base *base;
1542         Object *ob;
1543         float snaploc[3], imat[3][3], bmat[3][3], vec[3], min[3], max[3], centroid[3];
1544         int count, a;
1545
1546         /*calculate the snaplocation (centerpoint) */
1547         count= 0;
1548         INIT_MINMAX(min, max);
1549         centroid[0]= centroid[1]= centroid[2]= 0.0f;
1550         snaploc[0]= snaploc[1]= snaploc[2]= 0.0f;
1551
1552         if(G.obedit) {
1553                 tottrans= 0;
1554
1555                 if ELEM6(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE, OB_MBALL) 
1556                         make_trans_verts(bmat[0], bmat[1], 0);
1557                 if(tottrans==0) return;
1558
1559                 Mat3CpyMat4(bmat, G.obedit->obmat);
1560                 Mat3Inv(imat, bmat);
1561
1562                 tv= transvmain;
1563                 for(a=0; a<tottrans; a++, tv++) {
1564                         VECCOPY(vec, tv->loc);
1565                         Mat3MulVecfl(bmat, vec);
1566                         VecAddf(vec, vec, G.obedit->obmat[3]);
1567                         VecAddf(centroid, centroid, vec);
1568                         DO_MINMAX(vec, min, max);
1569                 }
1570
1571                 if(G.vd->around==V3D_CENTROID) {
1572                         VecMulf(centroid, 1.0/(float)tottrans);
1573                         VECCOPY(snaploc, centroid);
1574                 }
1575                 else {
1576                         snaploc[0]= (min[0]+max[0])/2;
1577                         snaploc[1]= (min[1]+max[1])/2;
1578                         snaploc[2]= (min[2]+max[2])/2;
1579                 }
1580                 
1581                 MEM_freeN(transvmain);
1582                 transvmain= 0;
1583
1584         }
1585         else {
1586                 base= (G.scene->base.first);
1587                 while(base) {
1588                         if(((base)->flag & SELECT) && ((base)->lay & G.vd->lay) ) {
1589                                 ob= base->object;
1590                                 if(ob->flag & OB_POSEMODE) {
1591                                         bPoseChannel *pchan;
1592                                         bArmature *arm= ob->data;
1593                                         
1594                                         for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1595                                                 if(pchan->bone->flag & BONE_SELECTED) {
1596                                                         if(pchan->bone->layer & arm->layer) {
1597                                                                 VECCOPY(vec, pchan->pose_mat[3]);
1598                                                                 VecAddf(centroid, centroid, vec);
1599                                                                 DO_MINMAX(vec, min, max);
1600                                                                 count++;
1601                                                         }
1602                                                 }
1603                                         }
1604                                 }
1605                                 else {
1606                                         /* not armature bones (i.e. objects) */
1607                                         VECCOPY(vec, base->object->obmat[3]);
1608                                         VecAddf(centroid, centroid, vec);
1609                                         DO_MINMAX(vec, min, max);
1610                                         count++;
1611                                 }
1612                         }
1613                         base= base->next;
1614                 }
1615                 if(count) {
1616                         if(G.vd->around==V3D_CENTROID) {
1617                                 VecMulf(centroid, 1.0/(float)count);
1618                                 VECCOPY(snaploc, centroid);
1619                         }
1620                         else {
1621                                 snaploc[0]= (min[0]+max[0])/2;
1622                                 snaploc[1]= (min[1]+max[1])/2;
1623                                 snaploc[2]= (min[2]+max[2])/2;
1624                         }
1625                 }
1626         }
1627
1628         /* Snap the selection to the snaplocation (duh!) */
1629         if(G.obedit) {
1630                 tottrans= 0;
1631
1632                 if ELEM6(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE, OB_MBALL) 
1633                         make_trans_verts(bmat[0], bmat[1], 0);
1634                 if(tottrans==0) return;
1635
1636                 Mat3CpyMat4(bmat, G.obedit->obmat);
1637                 Mat3Inv(imat, bmat);
1638
1639                 tv= transvmain;
1640                 for(a=0; a<tottrans; a++, tv++) {
1641
1642                         vec[0]= snaploc[0]-G.obedit->obmat[3][0];
1643                         vec[1]= snaploc[1]-G.obedit->obmat[3][1];
1644                         vec[2]= snaploc[2]-G.obedit->obmat[3][2];
1645
1646                         Mat3MulVecfl(imat, vec);
1647                         VECCOPY(tv->loc, vec);
1648                 }
1649                 
1650                 special_transvert_update();
1651                 
1652                 MEM_freeN(transvmain);
1653                 transvmain= 0;
1654
1655                 allqueue(REDRAWVIEW3D, 0);
1656                 return;
1657         }
1658
1659         base= (G.scene->base.first);
1660         while(base) {
1661                 if( ( ((base)->flag & SELECT) && ((base)->lay & G.vd->lay) && ((base)->object->id.lib==0))) {
1662                         ob= base->object;
1663                         if(ob->flag & OB_POSEMODE) {
1664                                 bPoseChannel *pchan;
1665                                 bArmature *arm= ob->data;
1666                                 
1667                                 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1668                                         if(pchan->bone->flag & BONE_SELECTED) {
1669                                                 if(pchan->bone->layer & arm->layer) {
1670                                                         if((pchan->bone->flag & BONE_CONNECTED)==0) { 
1671                                                                 /* get location of cursor in bone-space */
1672                                                                 armature_loc_pose_to_bone(pchan, snaploc, vec);
1673                                                                 
1674                                                                 /* calculate new position */
1675                                                                 VECCOPY(pchan->loc, vec);
1676                                                         }
1677                                                         /* if the bone has a parent and is connected to the parent, 
1678                                                          * don't do anything - will break chain unless we do auto-ik. 
1679                                                          */
1680                                                 }
1681                                         }
1682                                 }
1683                                 
1684                                 /* autokeyframing */
1685                                 ob->pose->flag |= POSE_DO_UNLOCK;
1686                                 autokeyframe_pose_cb_func(ob, TFM_TRANSLATION, 0);
1687                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1688                         }
1689                         else {
1690                                 ob->recalc |= OB_RECALC_OB;
1691                                 
1692                                 vec[0]= -ob->obmat[3][0] + snaploc[0];
1693                                 vec[1]= -ob->obmat[3][1] + snaploc[1];
1694                                 vec[2]= -ob->obmat[3][2] + snaploc[2];
1695
1696                                 if(ob->parent) {
1697                                         where_is_object(ob);
1698
1699                                         Mat3Inv(imat, originmat);
1700                                         Mat3MulVecfl(imat, vec);
1701                                         ob->loc[0]+= vec[0];
1702                                         ob->loc[1]+= vec[1];
1703                                         ob->loc[2]+= vec[2];
1704                                 }
1705                                 else {
1706                                         ob->loc[0]+= vec[0];
1707                                         ob->loc[1]+= vec[1];
1708                                         ob->loc[2]+= vec[2];
1709                                 }
1710 #ifdef WITH_VERSE
1711                                 if(ob->vnode) b_verse_send_transformation(ob);
1712 #endif
1713                                 
1714                                 /* auto-keyframing */
1715                                 autokeyframe_ob_cb_func(ob, TFM_TRANSLATION);
1716                         }
1717                 }
1718
1719                 base= base->next;
1720         }
1721         DAG_scene_flush_update(G.scene, screen_view3d_layers());
1722         allqueue(REDRAWVIEW3D, 0);
1723 }
1724
1725
1726 void snapmenu()
1727 {
1728         short event;
1729
1730         event = pupmenu("Snap %t|Selection -> Grid%x1|Selection -> Cursor%x2|Cursor-> Grid%x3|Cursor-> Selection%x4|Selection-> Center%x5");
1731
1732         switch (event) {
1733                 case 1: /*Selection to grid*/
1734                     snap_sel_to_grid();
1735                         BIF_undo_push("Snap selection to grid");
1736                     break;
1737                 case 2: /*Selection to cursor*/
1738                     snap_sel_to_curs();
1739                         BIF_undo_push("Snap selection to cursor");
1740                     break;          
1741                 case 3: /*Cursor to grid*/
1742                     snap_curs_to_grid();
1743                     break;
1744                 case 4: /*Cursor to selection*/
1745                     snap_curs_to_sel();
1746                     break;
1747                 case 5: /*Selection to center of selection*/
1748                     snap_to_center();
1749                         BIF_undo_push("Snap selection to center");
1750                     break;
1751         }
1752 }
1753
1754
1755 #define MERGELIMIT 0.001
1756 void mergemenu(void)
1757 {       
1758
1759         short event;
1760         int remCount = 0;
1761         
1762         if(G.scene->selectmode & SCE_SELECT_VERTEX)
1763                 if(G.editMesh->selected.first && G.editMesh->selected.last && 
1764                         ((EditSelection*)G.editMesh->selected.first)->type == EDITVERT && ((EditSelection*)G.editMesh->selected.last)->type == EDITVERT) 
1765                                 event = pupmenu("Merge %t|At First %x6|At Last%x1|At Center%x3|At Cursor%x4|Collapse%x2");
1766                 else if (G.editMesh->selected.first && ((EditSelection*)G.editMesh->selected.first)->type == EDITVERT) 
1767                         event = pupmenu("Merge %t|At First %x6|At Center%x3|At Cursor%x4|Collapse%x2");
1768                 else if (G.editMesh->selected.last && ((EditSelection*)G.editMesh->selected.last)->type == EDITVERT) 
1769                         event = pupmenu("Merge %t|At Last %x1|At Center%x3|At Cursor%x4|Collapse%x2");
1770                 else event = pupmenu("Merge %t|At Center%x3|At Cursor%x4|Collapse%x2");
1771         else event = pupmenu("Merge %t|At Center%x3|At Cursor%x4|Collapse%x2");
1772         switch (event)
1773         {
1774                 case -1:
1775                         return;
1776                 case 3:
1777                         if(G.qual & LR_CTRLKEY) remCount = merge_target(0,1);
1778                         else remCount = merge_target(0,0);
1779                         BIF_undo_push("Merge at center");
1780                         break;
1781                 case 4:
1782                         if(G.qual & LR_CTRLKEY) remCount = merge_target(1,1);
1783                         else remCount = merge_target(1,0);
1784                         BIF_undo_push("Merge at cursor");
1785                         break;
1786                 case 1:
1787                         if(G.qual & LR_CTRLKEY) remCount = merge_firstlast(0,1);
1788                         else remCount = merge_firstlast(0,0);
1789                         BIF_undo_push("Merge at last selected");
1790                         break;
1791                 case 6:
1792                         if(G.qual & LR_CTRLKEY) remCount = merge_firstlast(1,1);
1793                         else remCount = merge_firstlast(1,0);
1794                         BIF_undo_push("Merge at first selected");
1795                         break;
1796                 case 2:
1797                         remCount = collapseEdges();
1798                         BIF_undo_push("Collapse");
1799                         break;
1800         }
1801         notice("Removed %d Vertices", remCount);
1802         allqueue(REDRAWVIEW3D, 0);
1803         countall();
1804 }
1805 #undef MERGELIMIT
1806
1807
1808 void delete_context_selected(void) 
1809 {
1810         if(G.obedit) {
1811                 if(G.obedit->type==OB_MESH) delete_mesh();
1812                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) delNurb();
1813                 else if(G.obedit->type==OB_MBALL) delete_mball();
1814                 else if (G.obedit->type==OB_ARMATURE) delete_armature();
1815         }
1816         else delete_obj(0);
1817 }
1818
1819 void duplicate_context_selected(void) 
1820 {
1821         if(G.obedit) {
1822                 if(G.obedit->type==OB_MESH) adduplicate_mesh();
1823                 else if(G.obedit->type==OB_ARMATURE) adduplicate_armature();
1824                 else if(G.obedit->type==OB_MBALL) adduplicate_mball();
1825                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) adduplicate_nurb();
1826         }
1827         else {
1828                 adduplicate(0, U.dupflag);
1829         }
1830 }
1831
1832 void toggle_shading(void) 
1833 {
1834         if(G.qual & LR_SHIFTKEY) {
1835                 if(G.qual & LR_ALTKEY) {
1836                         reshadeall_displist();
1837                         G.vd->drawtype= OB_SHADED;
1838                 }
1839                 else {
1840                         if(G.vd->drawtype== OB_SHADED) G.vd->drawtype= OB_WIRE;
1841                         else G.vd->drawtype= OB_SHADED;
1842                 }
1843         }
1844         else if(G.qual & LR_ALTKEY) {
1845                 if(G.vd->drawtype== OB_TEXTURE) G.vd->drawtype= OB_SOLID;
1846                 else G.vd->drawtype= OB_TEXTURE;
1847         }
1848         else {
1849                 if(G.vd->drawtype==OB_SOLID || G.vd->drawtype==OB_SHADED) G.vd->drawtype= OB_WIRE;
1850                 else G.vd->drawtype= OB_SOLID;
1851         }
1852         
1853         if(G.vd->drawtype != OB_WIRE && G.vd->depths) {
1854                 G.vd->depths->damaged= 1;
1855         }
1856 }
1857
1858 int minmax_verts(float *min, float *max)
1859 {
1860         TransVert *tv;
1861         float centroid[3], vec[3], bmat[3][3];
1862         int a;
1863
1864         tottrans=0;
1865         if ELEM5(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) 
1866                 make_trans_verts(bmat[0], bmat[1], 0);
1867         if(tottrans==0) return 0;
1868
1869         Mat3CpyMat4(bmat, G.obedit->obmat);
1870         
1871         tv= transvmain;
1872         for(a=0; a<tottrans; a++, tv++) {               
1873                 VECCOPY(vec, tv->loc);
1874                 Mat3MulVecfl(bmat, vec);
1875                 VecAddf(vec, vec, G.obedit->obmat[3]);
1876                 VecAddf(centroid, centroid, vec);
1877                 DO_MINMAX(vec, min, max);               
1878         }
1879         
1880         MEM_freeN(transvmain);
1881         transvmain= 0;
1882         return 1;
1883 }
1884