New UV Calculation panel and code. The uv mapping function was split up into
[blender.git] / source / blender / src / buttons_editing.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 <time.h>
34 #include <math.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #ifdef WIN32
43 #include "BLI_winstuff.h"
44 #ifndef snprintf
45 #define snprintf _snprintf
46 #endif
47 #endif
48
49 #include "MEM_guardedalloc.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
52 #include "DNA_scene_types.h"
53
54 #include "DNA_action_types.h"
55 #include "DNA_armature_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_constraint_types.h"
58 #include "DNA_curve_types.h"
59 #include "DNA_effect_types.h"
60 #include "DNA_group_types.h"
61 #include "DNA_ika_types.h"
62 #include "DNA_image_types.h"
63 #include "DNA_key_types.h"
64 #include "DNA_lamp_types.h"
65 #include "DNA_lattice_types.h"
66 #include "DNA_material_types.h"
67 #include "DNA_meta_types.h"
68 #include "DNA_mesh_types.h"
69 #include "DNA_meshdata_types.h"
70 #include "DNA_object_types.h"
71 #include "DNA_radio_types.h"
72 #include "DNA_screen_types.h"
73 #include "DNA_sound_types.h"
74 #include "DNA_texture_types.h"
75 #include "DNA_userdef_types.h"
76 #include "DNA_vfont_types.h"
77 #include "DNA_view3d_types.h"
78 #include "DNA_world_types.h"
79 #include "DNA_packedFile_types.h"
80
81 #include "BKE_global.h"
82 #include "BKE_main.h"
83 #include "BKE_library.h"
84 #include "BKE_packedFile.h"
85
86 #include "BLI_blenlib.h"
87 #include "BLI_arithb.h"
88 #include "BLI_vfontdata.h"
89
90 #include "BSE_filesel.h"
91
92 #include "BIF_gl.h"
93 #include "BIF_editarmature.h"   
94 #include "BIF_editconstraint.h" 
95 #include "BIF_editdeform.h"
96 #include "BIF_editfont.h"
97 #include "BIF_editmesh.h"
98 #include "BIF_editsound.h"
99 #include "BIF_interface.h"
100 #include "BIF_mywindow.h"
101 #include "BIF_renderwin.h"
102 #include "BIF_resources.h"
103 #include "BIF_screen.h"
104 #include "BIF_scrarea.h"
105 #include "BIF_space.h"
106 #include "BIF_toets.h"
107 #include "BIF_toolbox.h"
108 #include "BIF_previewrender.h"
109 #include "BIF_butspace.h"
110
111 #include "mydevice.h"
112 #include "blendef.h"
113
114 #include "BKE_action.h"
115 #include "BKE_anim.h"
116 #include "BKE_armature.h"
117 #include "BKE_constraint.h"
118 #include "BKE_curve.h"
119 #include "BKE_displist.h"
120 #include "BKE_effect.h"
121 #include "BKE_font.h"
122 #include "BKE_ika.h"
123 #include "BKE_image.h"
124 #include "BKE_ipo.h"
125 #include "BKE_lattice.h"
126 #include "BKE_material.h"
127 #include "BKE_mball.h"
128 #include "BKE_mesh.h"
129 #include "BKE_object.h"
130 #include "BKE_sound.h"
131 #include "BKE_texture.h"
132 #include "BKE_utildefines.h"
133
134 #include "BDR_drawobject.h"
135 #include "BDR_editcurve.h"
136 #include "BDR_editface.h"
137 #include "BDR_editobject.h"
138 #include "BDR_vpaint.h"
139
140 #include "BSE_drawview.h"
141 #include "BSE_editipo.h"
142 #include "BSE_edit.h"
143 #include "BSE_filesel.h"
144 #include "BSE_headerbuttons.h"
145 #include "BSE_trans_types.h"
146 #include "BSE_view.h"
147 #include "BSE_buttons.h"
148 #include "BSE_seqaudio.h"
149
150 #include "LOD_DependKludge.h"
151 #include "LOD_decimation.h"
152
153 #include "butspace.h" // own module
154
155 static int decim_faces=0;
156 static short degr= 90, step= 9, turn= 1;
157 static float extr_offs= 1.0;
158 static float editbutweight=1.0;
159 short editbutflag= 1;
160 float doublimit= 0.001, editbutvweight=1, editbutsize=0.1;
161 float uv_calc_radius= 1.0, uv_calc_cubesize= 1.0;
162 short uv_calc_mapdir= 1, uv_calc_mapalign= 1, facesel_draw_edges= 0;
163
164
165 /* *************************** MESH DECIMATE ******************************** */
166
167 /* should be removed from this c file (ton) */
168
169 static int decimate_count_tria(Object *ob)
170 {
171         int tottria;
172         MFace *mface;
173         Mesh *me;
174         int a;
175         
176         me= ob->data;
177         
178         /* count number of trias, since decimator doesnt allow quads */
179         tottria= 0;
180         mface= me->mface;
181         for(a=0; a<me->totface; a++, mface++) {
182                 if(mface->v4) tottria++;
183                 if(mface->v3) tottria++;                
184         }
185         
186         return tottria;
187 }
188
189 static void decimate_faces(void)
190 {
191         Object *ob;
192         Mesh *me;
193         MVert *mvert;
194         MFace *mface;
195         LOD_Decimation_Info lod; 
196         float *vb=NULL;
197         float *vnb=NULL;
198         int *tib=NULL;
199         int a, tottria;
200         
201         /* we assume the active object being decimated */
202         ob= OBACT;
203         if(ob==NULL || ob->type!=OB_MESH) return;
204         me= ob->data;
205
206         /* add warning for vertex col and tfaces */
207         if(me->tface || me->mcol || me->dvert) {
208                 if(okee("This will remove UV coordinates, vertexcolors, and deform weights")==0) return;
209                 if(me->tface) MEM_freeN(me->tface);
210                 if(me->mcol) MEM_freeN(me->mcol);
211                 if(me->dvert) free_dverts(me->dvert, me->totvert);
212                 me->tface= NULL;
213                 me->mcol= NULL;
214                 me->dvert= NULL;
215         }
216         
217         /* count number of trias, since decimator doesnt allow quads */
218         tottria= decimate_count_tria(ob);
219
220         if(tottria<3) {
221                 error("You must have more than 3 input faces selected.");
222                 return;
223         }
224         /* allocate and init */
225         lod.vertex_buffer= MEM_mallocN(3*sizeof(float)*me->totvert, "vertices");
226         lod.vertex_normal_buffer= MEM_mallocN(3*sizeof(float)*me->totvert, "normals");
227         lod.triangle_index_buffer= MEM_mallocN(3*sizeof(int)*tottria, "trias");
228         lod.vertex_num= me->totvert;
229         lod.face_num= tottria;
230         
231         /* fill vertex buffer */
232         vb= lod.vertex_buffer;
233         vnb= lod.vertex_normal_buffer;
234         mvert= me->mvert;
235         for(a=0; a<me->totvert; a++, mvert++, vb+=3, vnb+=3) {
236                 VECCOPY(vb, mvert->co);
237                 VECCOPY(vnb, mvert->no);
238                 Normalise(vnb);
239         }
240         
241         /* fill index buffer */
242         mface= me->mface;
243         tib= lod.triangle_index_buffer;
244         for(a=0; a<me->totface; a++, mface++) {
245                 if(mface->v4) {
246                         tib[0]= mface->v1;
247                         tib[1]= mface->v3;
248                         tib[2]= mface->v4;
249                         tib+= 3;
250                 }
251                 if(mface->v3) {
252                         tib[0]= mface->v1;
253                         tib[1]= mface->v2;
254                         tib[2]= mface->v3;
255                         tib+= 3;
256                 }
257         }
258
259         if(LOD_LoadMesh(&lod) ) {
260                 if( LOD_PreprocessMesh(&lod) ) {
261                         DispList *dl;
262                         DispListMesh *dlm;
263                         MFace *mfaceint;
264                         
265                         /* we assume the decim_faces tells how much to reduce */
266                         
267                         while(lod.face_num > decim_faces) {
268                                 if( LOD_CollapseEdge(&lod)==0) break;
269                         }
270
271                         /* ok, put back the stuff in a displist */
272                         freedisplist(&(ob->disp));
273                         dl= MEM_callocN(sizeof(DispList), "disp");
274                         BLI_addtail(&ob->disp, dl);
275                         dl->type= DL_MESH;
276                         dlm=dl->mesh= MEM_callocN(sizeof(DispListMesh), "dispmesh");
277                         dlm->mvert= MEM_callocN(lod.vertex_num*sizeof(MVert), "mvert");
278                         dlm->mface= MEM_callocN(lod.face_num*sizeof(MFace), "mface");
279                         dlm->totvert= lod.vertex_num;
280                         dlm->totface= lod.face_num;
281                         
282                         mvert= dlm->mvert;
283                         vb= lod.vertex_buffer;
284                         for(a=0; a<lod.vertex_num; a++, vb+=3, mvert++) {
285                                 VECCOPY(mvert->co, vb);
286                         }
287                         
288                         mfaceint= dlm->mface;
289                         tib= lod.triangle_index_buffer;
290                         for(a=0; a<lod.face_num; a++, mfaceint++, tib+=3) {
291                                 mfaceint->v1= tib[0];
292                                 mfaceint->v2= tib[1];
293                                 mfaceint->v3= tib[2];
294                         }
295                 }
296                 else error("No memory");
297
298                 LOD_FreeDecimationData(&lod);
299         }
300         else error("No manifold Mesh");
301         
302         MEM_freeN(lod.vertex_buffer);
303         MEM_freeN(lod.vertex_normal_buffer);
304         MEM_freeN(lod.triangle_index_buffer);
305         
306         allqueue(REDRAWVIEW3D, 0);
307 }
308
309
310
311 static void decimate_cancel(void)
312 {
313         Object *ob;
314         
315         ob= OBACT;
316         if(ob) {
317                 freedisplist(&ob->disp);
318                 makeDispList(ob);
319         }
320         allqueue(REDRAWVIEW3D, 0);
321 }
322
323 static void decimate_apply(void)
324 {
325         Object *ob;
326         DispList *dl;
327         DispListMesh *dlm;
328         Mesh *me;
329         MFace *mface;
330         MFace *mfaceint;
331         int a;
332         
333         if(G.obedit) return;
334         
335         ob= OBACT;
336         if(ob) {
337                 dl= ob->disp.first;
338                 if(dl && dl->mesh) {
339                         dlm= dl->mesh;
340                         me= ob->data;
341                         
342                         // vertices 
343                         if(me->mvert) MEM_freeN(me->mvert);
344                         me->mvert= dlm->mvert;
345                         dlm->mvert= NULL;
346                         me->totvert= dlm->totvert;
347                         
348                         // faces
349                         if(me->mface) MEM_freeN(me->mface);
350                         me->mface= MEM_callocN(dlm->totface*sizeof(MFace), "mface");
351                         me->totface= dlm->totface;
352                         mface= me->mface;
353                         mfaceint= dlm->mface;
354                         for(a=0; a<me->totface; a++, mface++, mfaceint++) {
355                                 mface->v1= mfaceint->v1;
356                                 mface->v2= mfaceint->v2;
357                                 mface->v3= mfaceint->v3;
358                                 test_index_mface(mface, 3);
359                         }
360                         
361                         freedisplist(&ob->disp);
362                         
363                         G.obedit= ob;
364                         make_editMesh();
365                         load_editMesh();
366                         free_editMesh();
367                         G.obedit= NULL;
368                         tex_space_mesh(me);
369
370                         if (mesh_uses_displist(me)) {
371                                 makeDispList(ob);
372                         }
373                 }
374                 else error("Not a decimated Mesh");
375         }
376 }
377
378 /* *************************** MESH  ******************************** */
379
380
381 static void editing_panel_mesh_type(Object *ob, Mesh *me)
382 {
383         uiBlock *block;
384         float val;
385         /* Hope to support more than two subsurf algorithms */
386         char subsurfmenu[]="Subsurf Type%t|Catmull-Clark%x0|Simple Subdiv.%x1";
387         
388         block= uiNewBlock(&curarea->uiblocks, "editing_panel_mesh_type", UI_EMBOSS, UI_HELV, curarea->win);
389         if( uiNewPanel(curarea, block, "Mesh", "Editing", 320, 0, 318, 204)==0) return;
390
391         uiBlockBeginAlign(block);
392         uiDefButS(block, TOG|BIT|5, REDRAWVIEW3D, "Auto Smooth",10,180,154,19, &me->flag, 0, 0, 0, 0, "Treats all faces with angles less than Degr: as 'smooth' during render");
393         uiDefButS(block, NUM, B_DIFF, "Degr:",                                  10,160,154,19, &me->smoothresh, 1, 80, 0, 0, "Defines maximum angle between face normals that 'Auto Smooth' will operate on");
394         
395         uiBlockBeginAlign(block);
396         uiBlockSetCol(block, TH_BUT_SETTING1);
397         uiDefButS(block, TOG|BIT|7, B_MAKEDISP, "SubSurf",              10,124,70,19, &me->flag, 0, 0, 0, 0, "Treats the active object as a Subdivision Surface");
398         uiDefButS(block, MENU, B_MAKEDISP, subsurfmenu,         80,124,84,19, &(me->subsurftype), 0, 0, 0, 0, "Selects type of Subsurf algorithm.");
399         uiBlockSetCol(block, TH_AUTO);
400         uiDefButS(block, NUM, B_MAKEDISP, "Subdiv:",                    10,104,110,19, &me->subdiv, 0, 6, 0, 0, "Defines the level of subdivision to display in real time interactively");
401         uiDefButS(block, NUM, B_MAKEDISP, "",                                   120, 104, 44, 19, &me->subdivr, 0, 6, 0, 0, "Defines the level of subdivision to apply during rendering");
402         uiDefButS(block, TOG|BIT|8, B_MAKEDISP, "Optimal",              10,84,154,19, &me->flag, 0, 0, 0, 0, "Only draws optimal wireframe");
403         
404         
405         uiBlockBeginAlign(block);
406         if(me->msticky) val= 1.0; else val= 0.0;
407         uiDefBut(block, LABEL, 0, "Sticky", 10,50,70,20, 0, val, 0, 0, 0, "");
408         if(me->msticky==0) {
409                 uiDefBut(block, BUT, B_MAKESTICKY, "Make",      80,50,84,19, 0, 0, 0, 0, 0, "Creates Sticky coordinates for the active object from the current camera view background picture");
410         }
411         else uiDefBut(block, BUT, B_DELSTICKY, "Delete", 80,50,84,19, 0, 0, 0, 0, 0, "Deletes Sticky texture coordinates");
412
413         if(me->mcol) val= 1.0; else val= 0.0;
414         uiDefBut(block, LABEL, 0, "VertCol", 10,30,70,20, 0, val, 0, 0, 0, "");
415         if(me->mcol==0) {
416                 uiDefBut(block, BUT, B_MAKEVERTCOL, "Make",     80,30,84,19, 0, 0, 0, 0, 0, "Enables vertex colour painting on active object");
417         }
418         else uiDefBut(block, BUT, B_DELVERTCOL, "Delete", 80,30,84,19, 0, 0, 0, 0, 0, "Deletes vertex colours on active object");
419
420         if(me->tface) val= 1.0; else val= 0.0;
421         uiDefBut(block, LABEL, 0, "TexFace", 10,10,70,20, 0, val, 0, 0, 0, "");
422         if(me->tface==0) {
423                 uiDefBut(block, BUT, B_MAKE_TFACES, "Make",     80,10,84,19, 0, 0, 0, 0, 0, "Enables the active object's faces for UV coordinate mapping");
424         }
425         else uiDefBut(block, BUT, B_DEL_TFACES, "Delete", 80,10,84,19, 0, 0, 0, 0, 0, "Deletes UV coordinates for active object's faces");
426         uiBlockEndAlign(block);
427         
428
429         /* decimator */
430         if(G.obedit==NULL) {
431                 int tottria= decimate_count_tria(ob);
432                 DispList *dl;
433         
434                 // wacko, wait for new displist system (ton)
435                 if( (dl=ob->disp.first) && dl->mesh);
436                 else decim_faces= tottria;
437         
438                 uiBlockBeginAlign(block);
439                 uiBlockSetCol(block, TH_BUT_SETTING1);
440                 uiDefButI(block, NUM,B_DECIM_FACES, "Decimator:",       175,180,230,19, &decim_faces, 4.0, tottria, 10, 10, "Defines the number of triangular faces to decimate the active Mesh object to");
441                 uiBlockSetCol(block, TH_AUTO);
442                 uiDefBut(block, BUT,B_DECIM_APPLY, "Apply",             175,160,110,19, 0, 0, 0, 0, 0, "Applies the decimation to the active Mesh object");
443                 uiDefBut(block, BUT,B_DECIM_CANCEL, "Cancel",   285,160,120,19, 0, 0, 0, 0, 0, "Restores the Mesh to its original number of faces");
444                 uiBlockEndAlign(block);
445         }
446
447         
448         uiDefIDPoinBut(block, test_meshpoin_but, 0, "TexMesh: ",        175,124,230,19, &me->texcomesh, "Enter the name of a Meshblock");
449
450         if(me->key) {
451                 uiBlockBeginAlign(block);
452                 uiDefButS(block, NUM, B_DIFF, "Slurph:",                                175,95,95,19, &(me->key->slurph), -500.0, 500.0, 0, 0, "");
453                 uiDefButS(block, TOG, B_RELKEY, "Relative Keys",                175,75,95,19, &me->key->type, 0, 0, 0, 0, "");
454         }
455
456         uiBlockBeginAlign(block);
457         uiDefBut(block, BUT, B_SLOWERDRAW,"SlowerDraw",                 175,30,95,19, 0, 0, 0, 0, 0, "Displays the active object with all possible edges shown");
458         uiDefBut(block, BUT, B_FASTERDRAW,"FasterDraw",                 175,10,95,19, 0, 0, 0, 0, 0, "Displays the active object faster by omitting some edges when drawing");
459
460         uiBlockBeginAlign(block);
461         uiDefBut(block, BUT,B_DOCENTRE, "Centre",                                       275, 95, 130, 19, 0, 0, 0, 0, 0, "Shifts object data to be centered about object's origin");
462         uiDefBut(block, BUT,B_DOCENTRENEW, "Centre New",                        275, 75, 130, 19, 0, 0, 0, 0, 0, "Shifts object's origin to center of object data");
463         uiDefBut(block, BUT,B_DOCENTRECURSOR, "Centre Cursor",          275, 55, 130, 19, 0, 0, 0, 0, 0, "Shifts object's origin to cursor location");
464
465         uiBlockBeginAlign(block);
466         uiDefButS(block, TOG|BIT|2, REDRAWVIEW3D, "Double Sided",       275,30,130,19, &me->flag, 0, 0, 0, 0, "Toggles selected faces as doublesided or single-sided");
467         uiDefButS(block, TOG|BIT|1, REDRAWVIEW3D, "No V.Normal Flip",275,10,130,19, &me->flag, 0, 0, 0, 0, "Disables flipping of vertexnormals during render");
468         uiBlockEndAlign(block);
469
470 }
471
472
473
474 /* *************************** FONT ******************************** */
475
476 static short give_vfontnr(VFont *vfont)
477 {
478         VFont *vf;
479         short nr= 1;
480
481         vf= G.main->vfont.first;
482         while(vf) {
483                 if(vf==vfont) return nr;
484                 nr++;
485                 vf= vf->id.next;
486         }
487         return -1;
488 }
489
490 static VFont *give_vfontpointer(int nr) /* nr= button */
491 {
492         VFont *vf;
493         short tel= 1;
494
495         vf= G.main->vfont.first;
496         while(vf) {
497                 if(tel==nr) return vf;
498                 tel++;
499                 vf= vf->id.next;
500         }
501         return G.main->vfont.first;
502 }
503
504 static VFont *exist_vfont(char *str)
505 {
506         VFont *vf;
507
508         vf= G.main->vfont.first;
509         while(vf) {
510                 if(strcmp(vf->name, str)==0) return vf;
511                 vf= vf->id.next;
512         }
513         return 0;
514 }
515
516 static char *give_vfontbutstr(void)
517 {
518         VFont *vf;
519         int len= 0;
520         char *str, di[FILE_MAXDIR], fi[FILE_MAXFILE];
521
522         vf= G.main->vfont.first;
523         while(vf) {
524                 strcpy(di, vf->name);
525                 BLI_splitdirstring(di, fi);
526                 len+= strlen(fi)+4;
527                 vf= vf->id.next;
528         }
529         
530         str= MEM_callocN(len+21, "vfontbutstr");
531         strcpy(str, "FONTS %t");
532         vf= G.main->vfont.first;
533         while(vf) {
534                 
535                 if(vf->id.us==0) strcat(str, "|0 ");
536                 else strcat(str, "|   ");
537                 
538                 strcpy(di, vf->name);
539                 BLI_splitdirstring(di, fi);
540                 
541                 strcat(str, fi);
542                 vf= vf->id.next;
543         }
544         return str;
545 }
546
547 static void load_buts_vfont(char *name)
548 {
549         VFont *vf;
550         Curve *cu;
551         
552         if(OBACT && OBACT->type==OB_FONT) cu= OBACT->data;
553         else return;
554         
555         vf= exist_vfont(name);
556         if(vf==0) {
557                 vf= load_vfont(name);
558                 if(vf==0) return;
559         }
560         else id_us_plus((ID *)vf);
561         
562         if(cu->vfont) cu->vfont->id.us--;
563         cu->vfont= vf;
564         
565         text_to_curve(OBACT, 0);
566         makeDispList(OBACT);
567         allqueue(REDRAWVIEW3D, 0);
568         allqueue(REDRAWBUTSEDIT, 0);
569 }
570
571 void do_fontbuts(unsigned short event)
572 {
573         Curve *cu;
574         VFont *vf;
575         Object *ob;
576         ScrArea *sa;
577         char str[80];
578         
579         ob= OBACT;
580         
581         switch(event) {
582         case B_MAKEFONT:
583                 text_to_curve(ob, 0);
584                 makeDispList(ob);
585                 allqueue(REDRAWVIEW3D, 0);
586                 break;
587         case B_TOUPPER:
588                 to_upper();
589                 break;
590         case B_LOADFONT:
591                 vf= give_vfontpointer(G.buts->texnr);
592                 if(vf && vf->id.prev!=vf->id.next) strcpy(str, vf->name);
593                 else strcpy(str, U.fontdir);
594                 
595                 sa= closest_bigger_area();
596                 areawinset(sa->win);
597
598                 activate_fileselect(FILE_SPECIAL, "SELECT FONT", str, load_buts_vfont);
599
600                 break;
601         case B_PACKFONT:
602                 if (ob) {
603                         cu= ob->data;
604                         if(cu && cu->vfont) {
605                                 if (cu->vfont->packedfile) {
606                                         if (G.fileflags & G_AUTOPACK) {
607                                                 if (okee("Disable AutoPack ?")) {
608                                                         G.fileflags &= ~G_AUTOPACK;
609                                                 }
610                                         }
611                                         
612                                         if ((G.fileflags & G_AUTOPACK) == 0) {
613                                                 if (unpackVFont(cu->vfont, PF_ASK) == RET_OK) {
614                                                         text_to_curve(ob, 0);
615                                                         makeDispList(ob);
616                                                         allqueue(REDRAWVIEW3D, 0);
617                                                 }
618                                         }
619                                 } else {
620                                         cu->vfont->packedfile = newPackedFile(cu->vfont->name);
621                                 }
622                         }
623                 }
624                 allqueue(REDRAWHEADERS, 0);
625                 allqueue(REDRAWBUTSEDIT, 0);
626                 break;
627
628         case B_SETFONT:
629                 if(ob) {
630                         cu= ob->data;
631
632                         vf= give_vfontpointer(G.buts->texnr);
633                         if(vf) {
634                                 id_us_plus((ID *)vf);
635                                 cu->vfont->id.us--;
636                                 cu->vfont= vf;
637                                 text_to_curve(ob, 0);
638                                 makeDispList(ob);
639                                 allqueue(REDRAWVIEW3D, 0);
640                                 allqueue(REDRAWBUTSEDIT, 0);
641                         }
642                 }       
643                 break;
644         case B_TEXTONCURVE:
645                 if(ob) {
646                         cu= ob->data;
647                         if(cu->textoncurve && cu->textoncurve->type!=OB_CURVE) {
648                                 error("Only Curve Objects");
649                                 cu->textoncurve= 0;
650                                 allqueue(REDRAWBUTSEDIT, 0);
651                         }
652                         text_to_curve(ob, 0);
653                         makeDispList(ob);
654                 }
655         }
656 }
657
658 static void editing_panel_font_type(Object *ob, Curve *cu)
659 {
660         uiBlock *block;
661         char *strp;
662         static int packdummy = 0;
663         VFontData *vfd;
664
665         block= uiNewBlock(&curarea->uiblocks, "editing_panel_font_type", UI_EMBOSS, UI_HELV, curarea->win);
666         if(uiNewPanel(curarea, block, "Font", "Editing", 640, 0, 318, 204)==0) return;
667
668         G.buts->texnr= give_vfontnr(cu->vfont);
669         strp= give_vfontbutstr();
670         vfd= cu->vfont->data;
671
672         uiDefBut(block, BUT,B_LOADFONT, "Load", 480,188,68,20, 0, 0, 0, 0, 0, "Load a new font");
673         uiDefButS(block, MENU, B_SETFONT, strp, 550,188,220,20, &G.buts->texnr, 0, 0, 0, 0, "Change font for object");
674
675         if (cu->vfont->packedfile) {
676                 packdummy = 1;
677         } else {
678                 packdummy = 0;
679         }
680         uiDefIconButI(block, TOG|BIT|0, B_PACKFONT, ICON_PACKAGE,       772,188,20,20, &packdummy, 0, 0, 0, 0, "Pack/Unpack this font");
681         uiDefBut(block, LABEL, 0, vfd->name,  480, 165,314,20, 0, 0, 0, 0, 0, "Postscript name of the font");
682         
683         MEM_freeN(strp);
684
685         uiBlockBeginAlign(block);
686         uiDefButS(block, ROW,B_MAKEFONT, "Left",                480,135,53,20, &cu->spacemode, 0.0,0.0, 0, 0, "Left align the text from the object centre");
687         uiDefButS(block, ROW,B_MAKEFONT, "Middle",              535,135,55,20, &cu->spacemode, 0.0,1.0, 0, 0, "Middle align the text from the object centre");
688         uiDefButS(block, ROW,B_MAKEFONT, "Right",               592,135,53,20, &cu->spacemode, 0.0,2.0, 0, 0, "Right align the text from the object centre");
689         uiDefButS(block, ROW,B_MAKEFONT, "Flush",               647,135,53,20, &cu->spacemode, 0.0,3.0, 0, 0, "Fill characters to maximum linewidth. (Multiple lines required)");
690         uiDefBut(block, BUT, B_TOUPPER, "ToUpper",              715,135,78,20, 0, 0, 0, 0, 0, "Toggle between upper and lower case in editmode");
691         uiBlockEndAlign(block);
692         
693         uiDefIDPoinBut(block, test_obpoin_but, B_TEXTONCURVE, "TextOnCurve:",   480,105,220,19, &cu->textoncurve, "Apply a deforming curve to the text");
694         uiDefBut(block, TEX,REDRAWVIEW3D, "Ob Family:", 480,84,220,19, cu->family, 0.0, 20.0, 0, 0, "Blender uses font from selfmade objects");
695
696         uiBlockBeginAlign(block);
697         uiDefButF(block, NUM,B_MAKEFONT, "Size:",               480,56,155,20, &cu->fsize, 0.1,10.0, 10, 0, "Size of the text");
698         uiDefButF(block, NUM,B_MAKEFONT, "Linedist:",   640,56,155,20, &cu->linedist, 0.0,10.0, 10, 0, "Distance between text lines");
699         uiDefButF(block, NUM,B_MAKEFONT, "Spacing:",    480,34,155,20, &cu->spacing, 0.0,10.0, 10, 0, "Spacing of individual characters");
700         uiDefButF(block, NUM,B_MAKEFONT, "X offset:",   640,34,155,20, &cu->xof, -50.0,50.0, 10, 0, "Horizontal position from object centre");
701         uiDefButF(block, NUM,B_MAKEFONT, "Shear:",              480,12,155,20, &cu->shear, -1.0,1.0, 10, 0, "Italic angle of the characters");
702         uiDefButF(block, NUM,B_MAKEFONT, "Y offset:",   640,12,155,20, &cu->yof, -50.0,50.0, 10, 0, "Vertical position from object centre");
703         uiBlockEndAlign(block);
704 }
705
706
707 /* *************************** CURVE ******************************** */
708
709
710 void do_curvebuts(unsigned short event)
711 {
712         extern Nurb *lastnu;
713         extern ListBase editNurb;  /* from editcurve */
714         Object *ob;
715         Curve *cu;
716         Nurb *nu;
717         
718         ob= OBACT;
719         if(ob==0) return;
720         
721         switch(event) { 
722
723         case B_CONVERTPOLY:
724         case B_CONVERTBEZ:
725         case B_CONVERTBSPL:
726         case B_CONVERTCARD:
727         case B_CONVERTNURB:
728                 if(G.obedit) {
729                         setsplinetype(event-B_CONVERTPOLY);
730                         makeDispList(G.obedit);
731                         allqueue(REDRAWVIEW3D, 0);
732                 }
733                 break;
734         case B_UNIFU:
735         case B_ENDPU:
736         case B_BEZU:
737         case B_UNIFV:
738         case B_ENDPV:
739         case B_BEZV:
740                 if(G.obedit) {
741                         nu= editNurb.first;
742                         while(nu) {
743                                 if(isNurbsel(nu)) {
744                                         if((nu->type & 7)==CU_NURBS) {
745                                                 if(event<B_UNIFV) {
746                                                         nu->flagu &= 1;
747                                                         nu->flagu += ((event-B_UNIFU)<<1);
748                                                         makeknots(nu, 1, nu->flagu>>1);
749                                                 }
750                                                 else if(nu->pntsv>1) {
751                                                         nu->flagv &= 1;
752                                                         nu->flagv += ((event-B_UNIFV)<<1);
753                                                         makeknots(nu, 2, nu->flagv>>1);
754                                                 }
755                                         }
756                                 }
757                                 nu= nu->next;
758                         }
759                         makeDispList(G.obedit);
760                         allqueue(REDRAWVIEW3D, 0);
761                 }
762                 break;
763         case B_SETWEIGHT:
764                 if(G.obedit) {
765                         weightflagNurb(1, editbutweight, 0);
766                         makeDispList(G.obedit);
767                         allqueue(REDRAWVIEW3D, 0);
768                 }
769                 break;
770         case B_SETW1:
771                 editbutweight= 1.0;
772                 scrarea_queue_winredraw(curarea);
773                 break;
774         case B_SETW2:
775                 editbutweight= sqrt(2.0)/4.0;
776                 scrarea_queue_winredraw(curarea);
777                 break;
778         case B_SETW3:
779                 editbutweight= 0.25;
780                 scrarea_queue_winredraw(curarea);
781                 break;
782         case B_SETW4:
783                 editbutweight= sqrt(0.5);
784                 scrarea_queue_winredraw(curarea);
785                 break;
786         case B_SETORDER:
787                 if(G.obedit) {
788                         nu= lastnu;
789                         if(nu && (nu->type & 7)==CU_NURBS ) {
790                                 if(nu->orderu>nu->pntsu) {
791                                         nu->orderu= nu->pntsu;
792                                         scrarea_queue_winredraw(curarea);
793                                 }
794                                 makeknots(nu, 1, nu->flagu>>1);
795                                 if(nu->orderv>nu->pntsv) {
796                                         nu->orderv= nu->pntsv;
797                                         scrarea_queue_winredraw(curarea);
798                                 }
799                                 makeknots(nu, 2, nu->flagv>>1);
800                         }
801                         makeDispList(G.obedit);
802                         allqueue(REDRAWVIEW3D, 0);
803                 }
804                 break;
805         case B_MAKEDISP:
806                 if(ob->type==OB_FONT) text_to_curve(ob, 0);
807                 makeDispList(ob);
808                 allqueue(REDRAWVIEW3D, 0);
809                 allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
810                 break;
811         
812         case B_SUBDIVCURVE:
813                 subdivideNurb();
814                 break;
815         case B_SPINNURB:
816                 /* bad bad bad!!! use brackets!!! In case you wondered:
817                   {==,!=} goes before & goes before || */
818                 if( (G.obedit==0) || 
819                     (G.obedit->type!=OB_SURF) || 
820                         ((G.obedit->lay & G.vd->lay) == 0) ) return;
821                 spinNurb(0, 0);
822                 countall();
823                 makeDispList(G.obedit);
824                 allqueue(REDRAWVIEW3D, 0);
825                 break;
826         case B_CU3D:        /* allow 3D curve */
827                 if(G.obedit) {
828                         cu= G.obedit->data;
829                         nu= editNurb.first;
830                         while(nu) {
831                                 nu->type &= ~CU_2D;
832                                 if((cu->flag & CU_3D)==0) nu->type |= CU_2D;
833                                 test2DNurb(nu);
834                                 nu= nu->next;
835                         }
836                 }
837                 if(ob->type==OB_CURVE) {
838                         cu= ob->data;
839                         nu= cu->nurb.first;
840                         while(nu) {
841                                 nu->type &= ~CU_2D;
842                                 if((cu->flag & CU_3D)==0) nu->type |= CU_2D;
843                                 test2DNurb(nu);
844                                 nu= nu->next;
845                         }
846                 }
847                 makeDispList(G.obedit);
848                 allqueue(REDRAWVIEW3D, 0);
849                 break;
850         case B_SETRESOLU:
851                 if(ob->type==OB_CURVE) {
852                         cu= ob->data;
853                         if(ob==G.obedit) nu= editNurb.first;
854                         else nu= cu->nurb.first;
855                         
856                         while(nu) {
857                                 nu->resolu= cu->resolu;
858                                 nu= nu->next;
859                         }
860                 }
861                 else if(ob->type==OB_FONT) text_to_curve(ob, 0);
862                 
863                 makeDispList(ob);
864                 allqueue(REDRAWVIEW3D, 0);
865
866                 break;
867         }
868 }
869
870 static void editing_panel_curve_tools(Object *ob, Curve *cu)
871 {
872         Nurb *nu;
873         extern ListBase editNurb;  /* from editcurve */
874         extern Nurb *lastnu;
875         uiBlock *block;
876         short *sp;
877         
878         block= uiNewBlock(&curarea->uiblocks, "editing_panel_curve_tools", UI_EMBOSS, UI_HELV, curarea->win);
879         if(uiNewPanel(curarea, block, "Curve Tools", "Editing", 640, 0, 318, 204)==0) return;
880         
881         uiDefBut(block, LABEL, 0, "Make Knots",562,173,102, 18, 0, 0, 0, 0, 0, "");
882
883         if(ob->type==OB_CURVE) {
884                 uiDefBut(block, LABEL, 0, "Convert",    463,173,72, 18, 0, 0, 0, 0, 0, "");
885                 uiBlockBeginAlign(block);
886                 uiDefBut(block, BUT,B_CONVERTPOLY,"Poly",               467,152,72, 18, 0, 0, 0, 0, 0, "");
887                 uiDefBut(block, BUT,B_CONVERTBEZ,"Bezier",      467,132,72, 18, 0, 0, 0, 0, 0, "");
888                 uiDefBut(block, BUT,B_CONVERTBSPL,"Bspline",    467,112,72, 18, 0, 0, 0, 0, 0, "");
889                 uiDefBut(block, BUT,B_CONVERTCARD,"Cardinal",   467,92,72, 18, 0, 0, 0, 0, 0, "");
890                 uiDefBut(block, BUT,B_CONVERTNURB,"Nurb",               467,72,72, 18, 0, 0, 0, 0, 0, "");
891         }
892         uiBlockBeginAlign(block);
893         uiDefBut(block, BUT,B_UNIFU,"Uniform U",        565,152,102, 18, 0, 0, 0, 0, 0, "");
894         uiDefBut(block, BUT,B_UNIFV,"V",                        670,152,50, 18, 0, 0, 0, 0, 0, "");
895         uiDefBut(block, BUT,B_ENDPU,"Endpoint U",       565,132,102, 18, 0, 0, 0, 0, 0, "");
896         uiDefBut(block, BUT,B_ENDPV,"V",                        670,132,50, 18, 0, 0, 0, 0, 0, "");
897         uiDefBut(block, BUT,B_BEZU,"Bezier U",          565,112,102, 18, 0, 0, 0, 0, 0, "");
898         uiDefBut(block, BUT,B_BEZV,"V",                         670,112,50, 18, 0, 0, 0, 0, 0, "");
899         uiBlockEndAlign(block);
900
901         uiDefBut(block, BUT,B_SETWEIGHT,"Set Weight",   465,11,95,49, 0, 0, 0, 0, 0, "");
902
903         uiBlockBeginAlign(block);
904         uiDefButF(block, NUM,0,"Weight:",               565,36,102,22, &editbutweight, 0.01, 100.0, 10, 0, "");
905         uiDefBut(block, BUT,B_SETW1,"1.0",              670,36,50,22, 0, 0, 0, 0, 0, "");
906         uiDefBut(block, BUT,B_SETW2,"sqrt(2)/4",565,11,55,20, 0, 0, 0, 0, 0, "");
907         uiDefBut(block, BUT,B_SETW3,"0.25",             620,11,45,20, 0, 0, 0, 0, 0, "");
908         uiDefBut(block, BUT,B_SETW4,"sqrt(0.5)",665,11,55,20, 0, 0, 0, 0, 0, "");
909         uiBlockEndAlign(block);
910         
911         if(ob==G.obedit) {
912                 nu= lastnu;
913                 if(nu==NULL) nu= editNurb.first;
914                 if(nu) {
915                         uiBlockBeginAlign(block);
916                         sp= &(nu->orderu); 
917                         uiDefButS(block, NUM, B_SETORDER, "Order U:", 565,90,102, 19, sp, 2.0, 6.0, 0, 0, "");
918                         sp= &(nu->orderv); 
919                         uiDefButS(block, NUM, B_SETORDER, "V:",         670,90,50, 19, sp, 2.0, 6.0, 0, 0, "");
920                         sp= &(nu->resolu); 
921                         uiDefButS(block, NUM, B_MAKEDISP, "Resol U:", 565,70,102, 19, sp, 1.0, 128.0, 0, 0, "");
922                         sp= &(nu->resolv); 
923                         uiDefButS(block, NUM, B_MAKEDISP, "V:",         670,70,50, 19, sp, 1.0, 128.0, 0, 0, "");
924                 }
925         }
926         
927
928 }
929
930 static void editing_panel_curve_tools1(Object *ob, Curve *cu)
931 {
932         uiBlock *block;
933         
934         block= uiNewBlock(&curarea->uiblocks, "editing_panel_curve_tools1", UI_EMBOSS, UI_HELV, curarea->win);
935         if(uiNewPanel(curarea, block, "Curve Tools1", "Editing", 960, 0, 318, 204)==0) return;
936         
937         uiDefBut(block, BUT, B_SUBDIVCURVE, "Subdivide", 400,180,150,20, 0, 0, 0, 0, 0, "");
938         if(ob->type==OB_SURF) {
939                 uiDefBut(block, BUT, B_SPINNURB, "Spin",         400,160,150,20, 0, 0, 0, 0, 0, "");
940         }
941         uiBlockBeginAlign(block);
942         uiDefBut(block, BUT,B_HIDE,             "Hide",                 400,120,150,18, 0, 0, 0, 0, 0, "Hides selected faces");
943         uiDefBut(block, BUT,B_REVEAL,   "Reveal",               400,100,150,18, 0, 0, 0, 0, 0, "Reveals selected faces");
944         uiDefBut(block, BUT,B_SELSWAP,  "Select Swap",  400,80,150,18, 0, 0, 0, 0, 0, "Selects unselected faces, and deselects selected faces");
945         uiBlockEndAlign(block);
946         
947         uiDefButF(block, NUM,   REDRAWVIEW3D, "NSize:", 400, 40, 150, 19, &editbutsize, 0.001, 1.0, 10, 0, "");
948 }
949
950 /* for curve, surf and font! */
951 static void editing_panel_curve_type(Object *ob, Curve *cu)
952 {
953         uiBlock *block;
954         
955         block= uiNewBlock(&curarea->uiblocks, "editing_panel_curve_type", UI_EMBOSS, UI_HELV, curarea->win);
956         if(uiNewPanel(curarea, block, "Curve and Surface", "Editing", 320, 0, 318, 204)==0) return;
957         
958         uiDefButS(block, TOG|BIT|5, 0, "UV Orco",                                       600,160,150,19, &cu->flag, 0, 0, 0, 0, "");
959         if(ob->type==OB_SURF) 
960                 uiDefButS(block, TOG|BIT|6, REDRAWVIEW3D, "No Puno Flip",       600,140,150,19, &cu->flag, 0, 0, 0, 0, "");
961         
962         uiBlockBeginAlign(block);
963         uiDefBut(block, BUT,B_DOCENTRE, "Centre",                                       600, 115, 150, 19, 0, 0, 0, 0, 0, "Shifts object data to be centered about object's origin");
964         uiDefBut(block, BUT,B_DOCENTRENEW, "Centre New",                        600, 95, 150, 19, 0, 0, 0, 0, 0, "Shifts object's origin to center of object data");
965         uiDefBut(block, BUT,B_DOCENTRECURSOR, "Centre Cursor",          600, 75, 150, 19, 0, 0, 0, 0, 0, "Shifts object's origin to cursor location");
966         uiBlockEndAlign(block);
967         
968         if(ob->type==OB_SURF) {
969                 if(cu->key) {
970                         /* uiDefButS(block, NUM, B_DIFF, "Slurph:",                     600,25,140,19, &(cu->key->slurph), -500.0, 500.0,0,0); ,""*/
971                         uiDefButS(block, TOG, B_RELKEY, "Relative Keys",        600,45,140,19, &cu->key->type, 0, 0, 0, 0, "");
972                 }
973         }
974
975         if(ob->type!=OB_SURF) {
976                 
977                 if(ob->type==OB_CURVE) {
978                         extern float prlen;             // buttons_object.c, should be moved....
979                         char str[32];
980                         uiBlockBeginAlign(block);
981                         uiDefButS(block, NUM, B_RECALCPATH, "PathLen:",                 600,50,150,19, &cu->pathlen, 1.0, 9000.0, 0, 0, "");
982                         uiDefButS(block, TOG|BIT|3, B_RECALCPATH, "CurvePath",  600,30,75,19 , &cu->flag, 0, 0, 0, 0, "");
983                         uiDefButS(block, TOG|BIT|4, REDRAWVIEW3D, "CurveFollow",675,30,75,19, &cu->flag, 0, 0, 0, 0, "");
984                         sprintf(str, "%.4f", prlen);
985                         uiDefBut(block, BUT, B_PRINTLEN,                "PrintLen",     600,10,75,19, 0, 0, 0, 0, 0, "");
986                         uiDefBut(block, LABEL, 0, str,                                          675,10,75,19, 0, 1.0, 0, 0, 0, "");
987                 }
988
989                 uiBlockBeginAlign(block);
990                 uiDefButS(block, NUM, B_MAKEDISP, "DefResolU:", 760,160,120,19, &cu->resolu, 1.0, 128.0, 0, 0, "");
991                 uiDefBut(block, BUT, B_SETRESOLU, "Set",                880,160,30,19, 0, 0, 0, 0, 0, "");
992                 
993                 uiBlockBeginAlign(block);
994                 uiDefButF(block, NUM, B_MAKEDISP, "Width:",             760,90,150,19, &cu->width, 0.0, 2.0, 1, 0, "");
995                 uiDefButF(block, NUM, B_MAKEDISP, "Ext1:",              760,70,150,19, &cu->ext1, 0.0, 5.0, 10, 0, "");
996                 uiDefButF(block, NUM, B_MAKEDISP, "Ext2:",              760,50,150,19, &cu->ext2, 0.0, 2.0, 1, 0, "");
997                 uiDefButS(block, NUM, B_MAKEDISP, "BevResol:",  760,30,150,19, &cu->bevresol, 0.0, 10.0, 0, 0, "");
998                 uiDefIDPoinBut(block, test_obcurpoin_but, B_MAKEDISP, "BevOb:",         760,10,150,19, &cu->bevobj, "");
999
1000                 uiBlockBeginAlign(block);
1001                 uiBlockSetCol(block, TH_BUT_SETTING1);
1002                 uiDefButS(block, TOG|BIT|2, B_MAKEDISP, "Back", 760,130,50,19, &cu->flag, 0, 0, 0, 0, "");
1003                 uiDefButS(block, TOG|BIT|1, B_MAKEDISP, "Front",810,130,50,19, &cu->flag, 0, 0, 0, 0, "");
1004                 uiDefButS(block, TOG|BIT|0, B_CU3D, "3D",               860,130,50,19, &cu->flag, 0, 0, 0, 0, "");
1005
1006                 
1007         }
1008
1009 }
1010
1011
1012 /* *************************** CAMERA ******************************** */
1013
1014
1015 static void editing_panel_camera_type(Object *ob, Camera *cam)
1016 {
1017         uiBlock *block;
1018         float grid=0.0;
1019         
1020         if(G.vd) grid= G.vd->grid; 
1021         if(grid<1.0) grid= 1.0;
1022         
1023         block= uiNewBlock(&curarea->uiblocks, "editing_panel_camera_type", UI_EMBOSS, UI_HELV, curarea->win);
1024         if(uiNewPanel(curarea, block, "Camera", "Editing", 320, 0, 318, 204)==0) return;
1025
1026         uiDefButF(block, NUM,REDRAWVIEW3D, "Lens:", 470,178,160,20, &cam->lens, 1.0, 250.0, 100, 0, "Specify the lens of the camera");
1027
1028         uiBlockBeginAlign(block);
1029         uiDefButF(block, NUM,REDRAWVIEW3D, "ClipSta:", 470,147,160,20, &cam->clipsta, 0.001*grid, 100.0*grid, 10, 0, "Specify the startvalue of the the field of view");
1030         uiDefButF(block, NUM,REDRAWVIEW3D, "ClipEnd:", 470,125,160,20, &cam->clipend, 1.0, 5000.0*grid, 100, 0, "Specify the endvalue of the the field of view");
1031         uiBlockEndAlign(block);
1032         
1033         uiDefButF(block, NUM,REDRAWVIEW3D, "DrawSize:", 470,90,160,20, &cam->drawsize, 0.1*grid, 10.0, 10, 0, "Specify the drawsize of the camera");
1034
1035         uiDefButS(block, TOG, REDRAWVIEW3D, "Ortho", 470,49,61,40, &cam->type, 0, 0, 0, 0, "Render orthogonally");
1036         uiBlockBeginAlign(block);
1037         uiDefButS(block, TOG|BIT|0,REDRAWVIEW3D, "ShowLimits", 533,69,97,20, &cam->flag, 0, 0, 0, 0, "Draw the field of view");
1038         uiDefButS(block, TOG|BIT|1,REDRAWVIEW3D, "Show Mist", 533,49,97,20, &cam->flag, 0, 0, 0, 0, "Draw a line that indicates the mist area");
1039         uiBlockEndAlign(block);
1040 }
1041
1042 /* *************************** MBALL ******************************** */
1043
1044 void do_mballbuts(unsigned short event)
1045 {
1046         switch(event) {
1047         case B_RECALCMBALL:
1048                 makeDispList(OBACT);
1049                 allqueue(REDRAWVIEW3D, 0);
1050                 break;
1051         }
1052 }
1053
1054 static void editing_panel_mball_type(Object *ob, MetaBall *mb)
1055 {
1056         uiBlock *block;
1057         
1058         block= uiNewBlock(&curarea->uiblocks, "editing_panel_mball_type", UI_EMBOSS, UI_HELV, curarea->win);
1059         if(uiNewPanel(curarea, block, "MetaBall", "Editing", 320, 0, 318, 204)==0) return;
1060         
1061         if (ob==find_basis_mball(ob)) {
1062                 uiBlockBeginAlign(block);
1063                 uiDefButF(block, NUMSLI, B_RECALCMBALL, "Wiresize:",    470,178,250,19, &mb->wiresize, 0.05, 1.0, 0, 0, "");
1064                 uiDefButF(block, NUMSLI, 0, "Rendersize:",                      470,158,250,19, &mb->rendersize, 0.05, 1.0, 0, 0, "");
1065                 uiDefButF(block, NUMSLI, B_RECALCMBALL, "Threshold:", 470,138,250,19, &mb->thresh, 0.0001, 5.0, 0, 0, "");
1066
1067                 uiBlockBeginAlign(block);
1068                 uiBlockSetCol(block, TH_BUT_SETTING1);
1069                 uiDefBut(block, LABEL, 0, "Update:",            471,108,120,19, 0, 0, 0, 0, 0, "");
1070                 uiDefButS(block, ROW, B_DIFF, "Always", 471, 85, 120, 19, &mb->flag, 0.0, 0.0, 0, 0, "");
1071                 uiDefButS(block, ROW, B_DIFF, "Half Res",       471, 65, 120, 19, &mb->flag, 0.0, 1.0, 0, 0, "");
1072                 uiDefButS(block, ROW, B_DIFF, "Fast",           471, 45, 120, 19, &mb->flag, 0.0, 2.0, 0, 0, "");
1073         }
1074         
1075 }
1076
1077 static void editing_panel_mball_tools(Object *ob, MetaBall *mb)
1078 {
1079         extern MetaElem *lastelem;
1080         uiBlock *block;
1081         
1082         block= uiNewBlock(&curarea->uiblocks, "editing_panel_mball_tools", UI_EMBOSS, UI_HELV, curarea->win);
1083         if( uiNewPanel(curarea, block, "MetaBall tools", "Editing", 640, 0, 318, 204)==0) return;
1084         
1085         if(ob==G.obedit && lastelem) {
1086                 uiBlockBeginAlign(block);
1087                 uiDefButF(block, NUMSLI, B_RECALCMBALL, "Stiffness:", 750,178,250,19, &lastelem->s, 0.0, 10.0, 0, 0, "");
1088                 if(lastelem->type!=MB_BALL)
1089                 uiDefButF(block, NUMSLI, B_RECALCMBALL, "dx:",          750,158,250,19, &lastelem->expx, 0.0, 20.0, 0, 0, "");
1090                 if((lastelem->type!=MB_BALL)&&(lastelem->type!=MB_TUBE))
1091                 uiDefButF(block, NUMSLI, B_RECALCMBALL, "dy:",          750,138,250,19, &lastelem->expy, 0.0, 20.0, 0, 0, "");
1092
1093                 if((lastelem->type==MB_CUBE)||(lastelem->type==MB_ELIPSOID))
1094                 uiDefButF(block, NUMSLI, B_RECALCMBALL, "dz:",          750,118,250,19, &lastelem->expz, 0.0, 20.0, 0, 0, "");
1095                 uiBlockEndAlign(block);
1096                 
1097                 uiDefButS(block, ROW, B_RECALCMBALL, "Ball",                    753,83,60,19, &lastelem->type, 1.0, 0.0, 0, 0, "");
1098                 uiBlockBeginAlign(block);
1099                 uiDefButS(block, ROW, B_RECALCMBALL, "Tube",                    753,62,60,19, &lastelem->type, 1.0, 4.0, 0, 0, "");
1100                 uiDefButS(block, ROW, B_RECALCMBALL, "Plane",                   814,62,60,19, &lastelem->type, 1.0, 5.0, 0, 0, "");
1101                 uiDefButS(block, ROW, B_RECALCMBALL, "Elipsoid",                876,62,60,19, &lastelem->type, 1.0, 6.0, 0, 0, "");
1102                 uiDefButS(block, ROW, B_RECALCMBALL, "Cube",                    938,62,60,19, &lastelem->type, 1.0, 7.0, 0, 0, "");
1103                 uiBlockEndAlign(block);
1104
1105                 uiDefButS(block, TOG|BIT|1, B_RECALCMBALL, "Negative",753,16,60,19, &lastelem->flag, 0, 0, 0, 0, "");
1106
1107         }
1108
1109 }
1110
1111
1112 /* *************************** LATTICE ******************************** */
1113
1114 void do_latticebuts(unsigned short event)
1115 {
1116         Object *ob;
1117         Lattice *lt;
1118         
1119         ob= OBACT;
1120         if(ob->type!=OB_LATTICE) return;
1121         
1122         switch(event) {
1123         case B_RESIZELAT:
1124                 if(ob) {
1125                         if(ob==G.obedit) resizelattice(editLatt);
1126                         else resizelattice(ob->data);
1127                 }
1128                 allqueue(REDRAWVIEW3D, 0);
1129                 break;
1130         case B_DRAWLAT:
1131                 if(ob==G.obedit) calc_lattverts_ext();
1132                 allqueue(REDRAWVIEW3D, 0);
1133                 break;
1134         case B_LATTCHANGED:
1135                 
1136                 lt= ob->data;
1137                 if(lt->flag & LT_OUTSIDE) outside_lattice(lt);
1138                 
1139                 make_displists_by_parent(ob);
1140
1141                 allqueue(REDRAWVIEW3D, 0);
1142                 
1143                 break;
1144         }
1145 }
1146
1147 static void editing_panel_lattice_type(Object *ob, Lattice *lt)
1148 {
1149         uiBlock *block;
1150         
1151         block= uiNewBlock(&curarea->uiblocks, "editing_panel_lattice_type", UI_EMBOSS, UI_HELV, curarea->win);
1152         if(uiNewPanel(curarea, block, "Latice", "Editing", 320, 0, 318, 204)==0) return;
1153         
1154
1155         uiSetButLock(lt->key!=0, "Not with VertexKeys");
1156         uiSetButLock(ob==G.obedit, "Unable to perform function in EditMode");
1157
1158         uiBlockBeginAlign(block);
1159         uiDefButS(block, NUM, B_RESIZELAT,      "U:",                           470, 178,100,19, &lt->pntsu, 1.0, 64.0, 0, 0, "");
1160         uiDefButC(block, ROW, B_LATTCHANGED,            "Lin",          572, 178, 40, 19, &lt->typeu, 1.0, (float)KEY_LINEAR, 0, 0, "");
1161         uiDefButC(block, ROW, B_LATTCHANGED,            "Card",         612, 178, 40, 19, &lt->typeu, 1.0, (float)KEY_CARDINAL, 0, 0, "");
1162         uiDefButC(block, ROW, B_LATTCHANGED,            "B",            652, 178, 40, 19, &lt->typeu, 1.0, (float)KEY_BSPLINE, 0, 0, "");
1163
1164         uiBlockBeginAlign(block);
1165         uiDefButS(block, NUM, B_RESIZELAT,      "V:",                           470, 158,100,19, &lt->pntsv, 1.0, 64.0, 0, 0, "");
1166         uiDefButC(block, ROW, B_LATTCHANGED,            "Lin",          572, 158, 40, 19, &lt->typev, 2.0, (float)KEY_LINEAR, 0, 0, "");
1167         uiDefButC(block, ROW, B_LATTCHANGED,            "Card",         612, 158, 40, 19, &lt->typev, 2.0, (float)KEY_CARDINAL, 0, 0, "");
1168         uiDefButC(block, ROW, B_LATTCHANGED,            "B",            652, 158, 40, 19, &lt->typev, 2.0, (float)KEY_BSPLINE, 0, 0, "");
1169
1170         uiBlockBeginAlign(block);
1171         uiDefButS(block, NUM, B_RESIZELAT,      "W:",                           470, 138,100,19, &lt->pntsw, 1.0, 64.0, 0, 0, "");
1172         uiDefButC(block, ROW, B_LATTCHANGED,            "Lin",          572, 138, 40, 19, &lt->typew, 3.0, (float)KEY_LINEAR, 0, 0, "");
1173         uiDefButC(block, ROW, B_LATTCHANGED,            "Card",         612, 138, 40, 19, &lt->typew, 3.0, (float)KEY_CARDINAL, 0, 0, "");
1174         uiDefButC(block, ROW, B_LATTCHANGED,            "B",            652, 138, 40, 19, &lt->typew, 3.0, (float)KEY_BSPLINE, 0, 0, "");
1175         uiBlockEndAlign(block);
1176         
1177         uiDefBut(block, BUT, B_RESIZELAT,       "Make Regular",         470,101,99,32, 0, 0, 0, 0, 0, "");
1178
1179         uiDefButS(block, TOG|BIT|1, B_LATTCHANGED, "Outside",   571,101,120,31, &lt->flag, 0, 0, 0, 0, "");
1180
1181         if(lt->key) {
1182                 uiDefButS(block, NUM, B_DIFF, "Slurph:",                        470,60,120,19, &(lt->key->slurph), -500.0, 500.0, 0, 0, "");
1183                 uiDefButS(block, TOG, B_RELKEY, "Relative Keys",        470,40,120,19, &lt->key->type, 0, 0, 0, 0, "");
1184         }
1185
1186 }
1187
1188 /* *************************** ARMATURE ******************************** */
1189
1190
1191
1192 static int editbone_to_parnr (EditBone *bone)
1193 {
1194         EditBone *ebone;
1195         int     index;
1196
1197         for (ebone=G.edbo.first, index=0; ebone; ebone=ebone->next, index++){
1198                 if (ebone==bone)
1199                         return index;
1200         }
1201
1202         return -1;
1203 }
1204
1205 static void parnr_to_editbone(EditBone *bone)
1206 {
1207         if (bone->parNr == -1){
1208                 bone->parent = NULL;
1209                 bone->flag &= ~BONE_IK_TOPARENT;
1210         }
1211         else{
1212                 bone->parent = BLI_findlink(&G.edbo, bone->parNr);
1213                 attach_bone_to_parent(bone);
1214         }
1215 }
1216
1217 static void parnr_to_editbone_cb(void *bonev, void *arg2_unused)
1218 {
1219         EditBone *curBone= bonev;
1220         parnr_to_editbone(curBone);
1221 }
1222
1223 static void build_bonestring (char *string, EditBone *bone){
1224         EditBone *curBone;
1225         EditBone *pBone;
1226         int             skip=0;
1227         int             index, numbones, i;
1228         char (*qsort_ptr)[32] = NULL;
1229
1230         sprintf (string, "Parent%%t| %%x%d", -1);       /* That space is there 
1231                                                                                                  * for a reason 
1232                                                                                                  */
1233         
1234         numbones = BLI_countlist(&G.edbo);
1235
1236         /* 
1237          * This will hold the bone names temporarily so we can sort them
1238          */
1239         if (numbones > 0)
1240                 qsort_ptr = MEM_callocN (numbones * sizeof (qsort_ptr[0]), 
1241                                                                  "qsort_ptr");
1242
1243         numbones = 0;
1244         for (curBone = G.edbo.first, index=0; curBone; 
1245                  curBone=curBone->next, index++){
1246                 /* Make sure this is a valid child */
1247                 if (curBone != bone){
1248                         skip=0;
1249                         for (pBone=curBone->parent; pBone; pBone=pBone->parent){
1250                                 if (pBone==bone){
1251                                         skip=1;
1252                                         break;
1253                                 }
1254                         }
1255                         
1256                         if (skip)
1257                                 continue;
1258
1259                         sprintf (qsort_ptr[numbones], "|%s%%x%d", curBone->name, index);
1260                         numbones++;
1261                 }
1262         }
1263         qsort (qsort_ptr, numbones, sizeof (qsort_ptr[0]), 
1264                    ( int (*)(const void *, const void *) ) strcmp);
1265
1266         for (i=0; i < numbones; ++i) {
1267                 sprintf (string, "%s%s", string, qsort_ptr[i]);
1268         }
1269
1270         if (qsort_ptr)
1271                 MEM_freeN(qsort_ptr);
1272 }
1273
1274 static void constraint_ebone_name_fix(ListBase *conlist, EditBone *eBone)
1275 {
1276
1277         bConstraint *curcon;
1278         char *subtarget;
1279
1280         for (curcon = conlist->first; curcon; curcon=curcon->next){
1281                 subtarget = get_con_subtarget_name(curcon, G.obedit);
1282                 if (subtarget)
1283                         if (!strcmp(subtarget,eBone->oldname) )
1284                                 strcpy(subtarget, eBone->name);
1285         }
1286 }
1287
1288 static void validate_editbonebutton(EditBone *eBone){
1289         EditBone        *prev;
1290         bAction         *act=NULL;
1291         bActionChannel *chan;
1292         Base *base;
1293         bPose        *pose;
1294         bPoseChannel *pchan;
1295
1296         /* Separate the bone from the G.edbo */
1297         prev=eBone->prev;
1298         BLI_remlink (&G.edbo, eBone);
1299
1300         /*      Validate the name */
1301         unique_editbone_name (eBone->name);
1302
1303         /* Re-insert the bone */
1304         if (prev)
1305                 BLI_insertlink(&G.edbo, prev, eBone);
1306         else
1307                 BLI_addhead (&G.edbo, eBone);
1308
1309         /* Rename *action* channel if necessary */
1310         if (G.obedit)
1311                 act = G.obedit->action;
1312
1313         if (act && !act->id.lib){
1314                 /*      Find the appropriate channel
1315                  */
1316                 for (chan = act->chanbase.first; chan; chan=chan->next){
1317                         if (!strcmp (chan->name, eBone->oldname)){
1318                                 strcpy (chan->name, eBone->name);
1319                         }
1320                 }
1321                 allqueue(REDRAWACTION, 0);
1322         }
1323
1324         /* Rename the *pose* channel, if it exists (thus making sure
1325          * that the constraints are still valid) 
1326          */
1327         pose = G.obedit->pose;
1328         if (pose) {
1329                 pchan = get_pose_channel(pose, eBone->oldname);
1330                 if (pchan) {
1331                         strcpy (pchan->name, eBone->name);
1332                 }
1333         }
1334
1335         /* Update the parenting info of any users */
1336         /*      Yes, I know this is the worst thing you have ever seen. */
1337
1338         for (base = G.scene->base.first; base; base=base->next){
1339                 Object *ob = base->object;
1340                 ListBase *conlist;
1341
1342                 /* See if an object is parented to this armature */
1343                 if (ob->parent && ob->partype==PARBONE && 
1344                         (ob->parent->type==OB_ARMATURE) && 
1345                         (ob->parent->data == G.obedit->data)){
1346                         if (!strcmp(ob->parsubstr, eBone->oldname))
1347                                 strcpy(ob->parsubstr, eBone->name);
1348                 }
1349
1350                 /* Update any constraints to use the new bone name */
1351                 conlist = &ob->constraints;
1352                 constraint_ebone_name_fix(conlist, eBone);
1353
1354                 switch (ob->type){
1355                         case OB_ARMATURE:
1356                                 if (ob->pose){
1357                                         for (pchan = ob->pose->chanbase.first; pchan; 
1358                                                  pchan=pchan->next){
1359                                                 conlist = &pchan->constraints;
1360                                                 constraint_ebone_name_fix(conlist, eBone);
1361                                         }
1362                                 }
1363                                 break;
1364                         default:
1365                                 break;
1366         }
1367                 
1368         }
1369
1370         exit_editmode(0);       /* To ensure new names make it to the edit armature */
1371
1372 }
1373
1374 static void validate_editbonebutton_cb(void *bonev, void *arg2_unused)
1375 {
1376         EditBone *curBone= bonev;
1377         validate_editbonebutton(curBone);
1378 }
1379
1380 static void armature_rest_pos_func(void *notused1, void *notused2) {
1381         clear_object_constraint_status(OBACT);
1382         make_displists_by_armature(OBACT);
1383 }
1384
1385 static void editing_panel_armature_type(Object *ob, bArmature *arm)
1386 {
1387         uiBlock         *block;
1388         uiBut       *but;
1389         int                     bx=148, by=100;
1390
1391         block= uiNewBlock(&curarea->uiblocks, "editing_panel_armature_type", UI_EMBOSS, UI_HELV, curarea->win);
1392         if(uiNewPanel(curarea, block, "Armature", "Editing", 320, 0, 318, 204)==0) return;
1393         
1394         but = uiDefButI(block, TOG|BIT|ARM_RESTPOSBIT,REDRAWVIEW3D, 
1395                                         "Rest Pos", bx,by,97,20, &arm->flag, 0, 0, 0, 0, 
1396                                         "Disable all animation for this object");
1397         uiButSetFunc(but, armature_rest_pos_func, NULL, NULL);
1398
1399         uiBlockBeginAlign(block);
1400         uiDefButI(block, TOG|BIT|ARM_DRAWAXESBIT,REDRAWVIEW3D, "Draw Axes", bx,by-46,97,20, &arm->flag, 0, 0, 0, 0, "Draw bone axes");
1401         uiDefButI(block, TOG|BIT|ARM_DRAWNAMESBIT,REDRAWVIEW3D, "Draw Names", bx,by-69,97,20, &arm->flag, 0, 0, 0, 0, "Draw bone names");
1402         uiDefButI(block, TOG|BIT|ARM_DRAWXRAYBIT,REDRAWVIEW3D, "X-Ray", bx,by-92,97,20, &arm->flag, 0, 0, 0, 0, "Draw armature in front of shaded objects");
1403         uiDefButI(block, TOG|BIT|ARM_DELAYBIT,REDRAWVIEW3D, 
1404                           "Delay Deform", bx,by-115,97,20, &arm->flag, 0, 0, 0, 0, 
1405                           "Don't deform children when manipulating bones in pose mode");
1406 }
1407
1408 static void editing_panel_armature_bones(Object *ob, bArmature *arm)
1409 {
1410         uiBlock         *block;
1411         EditBone        *curBone;
1412         uiBut           *but;
1413         char            *boneString=NULL;
1414         int                     bx=148, by=180;
1415         int                     index;
1416
1417         /* Draw the bone name block */
1418
1419         block= uiNewBlock(&curarea->uiblocks, "editing_panel_armature_bones", UI_EMBOSS, UI_HELV, curarea->win);
1420         if(uiNewPanel(curarea, block, "Armature Bones", "Editing", 640, 0, 318, 204)==0) return;
1421
1422         /* this is a variable height panel, newpanel doesnt force new size on existing panels */
1423         /* so first we make it default height */
1424         uiNewPanelHeight(block, 204);
1425
1426
1427         uiDefBut(block, LABEL, 0, "Selected Bones",                                             bx,by,158,18, 0, 0, 0, 0, 0, "");
1428         by-=20;
1429         for (curBone=G.edbo.first, index=0; curBone; curBone=curBone->next, index++){
1430                 if (curBone->flag & (BONE_SELECTED)) {
1431
1432                         /* Hide in posemode flag */
1433                         uiDefButI(block, TOG|BIT|BONE_HIDDENBIT, REDRAWVIEW3D, "Hide", bx-55,by,45,18, &curBone->flag, 0, 0, 0, 0, "Toggles display of this bone in posemode");
1434                         
1435                         /*      Bone naming button */
1436                         strcpy (curBone->oldname, curBone->name);
1437                         but=uiDefBut(block, TEX, REDRAWVIEW3D, "BO:", bx-10,by,117,18, &curBone->name, 0, 24, 0, 0, "Change the bone name");
1438                         uiButSetFunc(but, validate_editbonebutton_cb, curBone, NULL);
1439                         
1440                         uiDefBut(block, LABEL, 0, "child of", bx+107,by,73,18, NULL, 0.0, 0.0, 0.0, 0.0, "");
1441
1442                         boneString = malloc((BLI_countlist(&G.edbo) * 64)+64);
1443                         build_bonestring (boneString, curBone);
1444                         
1445                         curBone->parNr = editbone_to_parnr(curBone->parent);
1446                         but = uiDefButI(block, MENU,REDRAWVIEW3D, boneString, bx+180,by,120,18, &curBone->parNr, 0.0, 0.0, 0.0, 0.0, "Parent");
1447                         uiButSetFunc(but, parnr_to_editbone_cb, curBone, NULL);
1448
1449                         free(boneString);
1450
1451                         /* IK to parent flag */
1452                         if (curBone->parent){
1453                                 but=uiDefButI(block, TOG|BIT|BONE_IK_TOPARENTBIT, REDRAWVIEW3D, "IK", bx+300,by,32,18, &curBone->flag, 0.0, 0.0, 0.0, 0.0, "IK link to parent");
1454                                 uiButSetFunc(but, attach_bone_to_parent_cb, curBone, NULL);
1455                         }
1456                         
1457                         /* Dist and weight buttons */
1458                         uiBlockBeginAlign(block);
1459                         but=uiDefButS(block, MENU, REDRAWVIEW3D,
1460                                                         "Skinnable %x0|"
1461                                                         "Unskinnable %x1|"
1462                                                         "Head %x2|"
1463                                                         "Neck %x3|"
1464                                                         "Back %x4|"
1465                                                         "Shoulder %x5|"
1466                                                         "Arm %x6|"
1467                                                         "Hand %x7|"
1468                                                         "Finger %x8|"
1469                                                         "Thumb %x9|"
1470                                                         "Pelvis %x10|"
1471                                                         "Leg %x11|"
1472                                                         "Foot %x12|"
1473                                                         "Toe %x13|"
1474                                                         "Tentacle %x14",
1475                                                         bx-10,by-19,117,18,
1476                                                         &curBone->boneclass,
1477                                                         0.0, 0.0, 0.0, 0.0, 
1478                                                         "Classification of armature element");
1479                         
1480                         /* Dist and weight buttons */
1481                         uiDefButF(block, NUM,REDRAWVIEW3D, "Dist:", bx+110, by-19, 
1482                                                 105, 18, &curBone->dist, 0.0, 1000.0, 10.0, 0.0, 
1483                                                 "Bone deformation distance");
1484                         uiDefButF(block, NUM,REDRAWVIEW3D, "Weight:", bx+223, by-19, 
1485                                                 110, 18, &curBone->weight, 0.0F, 1000.0F, 
1486                                                 10.0F, 0.0F, "Bone deformation weight");
1487                         
1488                         uiBlockEndAlign(block);
1489                         by-=42; 
1490                 }
1491         }
1492         
1493         if(by<0) {
1494                 uiNewPanelHeight(block, 204 - by);
1495         }
1496         
1497 }
1498
1499
1500 /* *************************** MESH ******************************** */
1501
1502
1503
1504 void do_meshbuts(unsigned short event)
1505 {
1506         void decimate_faces(void);
1507         void decimate_cancel(void);
1508         void decimate_apply(void);
1509         Object *ob;
1510         Mesh *me;
1511         float fac;
1512         short randfac;
1513
1514         ob= OBACT;
1515         if(ob && ob->type==OB_MESH) {
1516                 
1517                 me= get_mesh(ob);
1518                 if(me==0) return;
1519                 
1520                 switch(event) {
1521                 case B_AUTOVGROUP:
1522                         if (!get_armature(ob->parent)){
1523                                 error ("Mesh must be the child of an armature");
1524                                 break;
1525                         }
1526                                 /* Verify that there are vertex groups for bones in armature */
1527                                 /* Remove selected vertices from all defgroups */
1528                                 /* Perform assignment for selected vertices */
1529
1530                         allqueue (REDRAWVIEW3D, 1);
1531                         break;
1532                 case B_NEWVGROUP:
1533                         add_defgroup (G.obedit);
1534                         scrarea_queue_winredraw(curarea);
1535                         break;
1536                 case B_DELVGROUP:
1537                         del_defgroup (G.obedit);
1538                         allqueue (REDRAWVIEW3D, 1);
1539                         break;
1540                 case B_ASSIGNVGROUP:
1541                         undo_push_mesh("Assign to vertex group");
1542                         assign_verts_defgroup ();
1543                         allqueue (REDRAWVIEW3D, 1);
1544                         break;
1545                 case B_REMOVEVGROUP:
1546                         undo_push_mesh("Remove from vertex group");
1547                         remove_verts_defgroup (0);
1548                         allqueue (REDRAWVIEW3D, 1);
1549                         break;
1550                 case B_SELVGROUP:
1551                         sel_verts_defgroup(1);
1552                         allqueue (REDRAWVIEW3D, 1);
1553                         break;
1554                 case B_DESELVGROUP:
1555                         sel_verts_defgroup(0);
1556                         allqueue (REDRAWVIEW3D, 1);
1557                         break;
1558                 case B_DELSTICKY:
1559                 
1560                         if(me->msticky) MEM_freeN(me->msticky);
1561                         me->msticky= 0;
1562                         allqueue(REDRAWBUTSEDIT, 0);
1563                         break;
1564                 case B_MAKESTICKY:
1565                         make_sticky();
1566                         break;
1567                 case B_MAKEVERTCOL:
1568                         make_vertexcol();
1569                         break;
1570                 case B_DELVERTCOL:
1571                         if(me->mcol) MEM_freeN(me->mcol);
1572                         me->mcol= 0;
1573                         G.f &= ~G_VERTEXPAINT;
1574                         freedisplist(&(ob->disp));
1575                         allqueue(REDRAWBUTSEDIT, 0);
1576                         allqueue(REDRAWVIEW3D, 0);
1577                         break;
1578
1579                 case B_MAKE_TFACES:
1580                         make_tfaces(me);
1581                         allqueue(REDRAWBUTSEDIT, 0);
1582                         break;
1583
1584                 case B_DEL_TFACES:
1585                         if(me->tface) MEM_freeN(me->tface);
1586                         me->tface= 0;
1587                         G.f &= ~G_FACESELECT;
1588                         allqueue(REDRAWBUTSEDIT, 0);
1589                         allqueue(REDRAWVIEW3D, 0);
1590                         allqueue(REDRAWIMAGE, 0);
1591                         break;
1592                         
1593                 case B_FLIPNORM:
1594                         if(G.obedit) {
1595                                 flip_editnormals();
1596                         }
1597                         else flipnorm_mesh( get_mesh(ob) );
1598                         
1599                         allqueue(REDRAWVIEW3D, 0);
1600                         break;
1601
1602                 case B_DECIM_FACES:
1603                         decimate_faces();
1604                         break;
1605                 case B_DECIM_CANCEL:
1606                         decimate_cancel();
1607                         break;
1608                 case B_DECIM_APPLY:
1609                         decimate_apply();
1610                         break;
1611
1612                 case B_SLOWERDRAW:
1613                         slowerdraw();
1614                         break;
1615                 case B_FASTERDRAW:
1616                         fasterdraw();
1617                         break;
1618                 }
1619         }
1620         
1621         if(G.obedit==0 || (G.obedit->type!=OB_MESH)) return;
1622         
1623         switch(event) {
1624         case B_SPIN:
1625                 if( select_area(SPACE_VIEW3D)) spin_mesh(step, degr, 0, 0);
1626                 break;
1627         case B_SPINDUP:
1628                 if( select_area(SPACE_VIEW3D)) spin_mesh(step, degr, 0, 1);
1629                 break;
1630         case B_EXTR:
1631                 G.f |= G_DISABLE_OK;
1632                 if( select_area(SPACE_VIEW3D)) extrude_mesh();
1633                 G.f -= G_DISABLE_OK;
1634                 break;
1635         case B_SCREW:
1636                 if( select_area(SPACE_VIEW3D)) screw_mesh(step, turn);
1637                 break;
1638         case B_EXTREP:
1639                 if( select_area(SPACE_VIEW3D)) extrude_repeat_mesh(step, extr_offs);
1640                 break;
1641         case B_SPLIT:
1642                 G.f |= G_DISABLE_OK;
1643                 split_mesh();
1644                 G.f -= G_DISABLE_OK;
1645                 break;
1646         case B_REMDOUB:
1647                 undo_push_mesh("Rem Doubles");
1648                 notice("Removed: %d", removedoublesflag(1, doublimit));
1649                 allqueue(REDRAWVIEW3D, 0);
1650                 break;
1651         case B_SUBDIV:
1652                 waitcursor(1);
1653                 undo_push_mesh("Subdivide");
1654                 subdivideflag(1, 0.0, editbutflag & B_BEAUTY);
1655                 countall();
1656                 waitcursor(0);
1657                 allqueue(REDRAWVIEW3D, 0);
1658                 break;
1659         case B_FRACSUBDIV:
1660                 randfac= 10;
1661                 if(button(&randfac, 1, 100, "Rand fac:")==0) return;
1662                 waitcursor(1);
1663                 undo_push_mesh("Fractal Subdivide");
1664                 fac= -( (float)randfac )/100;
1665                 subdivideflag(1, fac, editbutflag & B_BEAUTY);
1666                 countall();
1667                 waitcursor(0);
1668                 allqueue(REDRAWVIEW3D, 0);
1669                 break;
1670         case B_XSORT:
1671                 if( select_area(SPACE_VIEW3D)) xsortvert_flag(1);
1672                 break;
1673         case B_HASH:
1674                 hashvert_flag(1);
1675                 break;
1676         case B_TOSPHERE:
1677                 vertices_to_sphere();
1678                 break;
1679         case B_VERTEXNOISE:
1680                 vertexnoise();
1681                 break;
1682         case B_VERTEXSMOOTH:
1683                 vertexsmooth();
1684                 break;
1685         }
1686         /* WATCH IT: previous events only in editmode! */
1687 }
1688
1689 static void editing_panel_mesh_tools(Object *ob, Mesh *me)
1690 {
1691         uiBlock *block;
1692
1693         block= uiNewBlock(&curarea->uiblocks, "editing_panel_mesh_tools", UI_EMBOSS, UI_HELV, curarea->win);
1694         if(uiNewPanel(curarea, block, "Mesh Tools", "Editing", 640, 0, 318, 204)==0) return;
1695         
1696         uiBlockBeginAlign(block);
1697         uiDefButS(block, TOG|BIT|2, 0, "Beauty",                10,195,80,19, &editbutflag, 0, 0, 0, 0, "Causes 'Subdivide' to split faces in halves instead of quarters");
1698         uiDefBut(block, BUT,B_SUBDIV,"Subdivide",               90,195,80,19, 0, 0, 0, 0, 0, "Splits selected faces into halves or quarters");
1699         uiDefBut(block, BUT,B_FRACSUBDIV, "Fract Subd", 170,195,85,19, 0, 0, 0, 0, 0, "Subdivides selected faces with a random factor");
1700
1701         uiDefBut(block, BUT,B_VERTEXNOISE,"Noise",              10,175,80,19, 0, 0, 0, 0, 0, "Use vertex coordinate as texture coordinate");
1702         uiDefBut(block, BUT,B_HASH,"Hash",                              90,175,80,19, 0, 0, 0, 0, 0, "Randomizes selected vertice sequence data");
1703         uiDefBut(block, BUT,B_XSORT,"Xsort",                    170,175,85,19, 0, 0, 0, 0, 0, "Sorts selected vertice data in the X direction");
1704
1705         uiDefBut(block, BUT,B_TOSPHERE,"To Sphere",             10,155,80,19, 0, 0, 0, 0, 0, "Moves selected vertices outwards into a spherical shape");
1706         uiDefBut(block, BUT,B_VERTEXSMOOTH,"Smooth",    90,155,80,19, 0, 0, 0, 0, 0, "Flattens angles of selected faces");
1707         uiDefBut(block, BUT,B_SPLIT,"Split",                    170,155,85,19, 0, 0, 0, 0, 0, "Splits selected verts to separate sub-mesh.");
1708
1709         uiDefBut(block, BUT,B_FLIPNORM,"Flip Normals",  10,135,80,19, 0, 0, 0, 0, 0, "Toggles the direction of the selected face's normals");
1710         uiDefBut(block, BUT,B_REMDOUB,"Rem Doubles",    90,135,80,19, 0, 0, 0, 0, 0, "Removes duplicates from selected vertices");
1711         uiDefButF(block, NUM, B_DIFF, "Limit:",                 170,135,85,19, &doublimit, 0.0001, 1.0, 10, 0, "Specifies the max distance 'Rem Doubles' will consider vertices as 'doubled'");
1712         uiBlockEndAlign(block);
1713
1714         uiDefBut(block, BUT,B_EXTR,"Extrude",                   10,105,245,24, 0, 0, 0, 0, 0, "Converts selected edges to faces and selects the new vertices");
1715
1716         uiBlockBeginAlign(block);
1717         uiDefBut(block, BUT,B_SCREW,"Screw",                    10,75,80,24, 0, 0, 0, 0, 0, "Activates the screw tool");  // Bish - This could use some more definition
1718         uiDefBut(block, BUT,B_SPIN, "Spin",                             90,75,80,24, 0, 0, 0, 0, 0, "Extrudes the selected vertices in a circle around the cursor in the indicated viewport");
1719         uiDefBut(block, BUT,B_SPINDUP,"Spin Dup",               170,75,85,24, 0, 0, 0, 0, 0, "Creates copies of the selected vertices in a circle around the cursor in the indicated viewport");
1720
1721         uiDefButS(block, NUM, B_DIFF, "Degr:",                  10,55,80,19, &degr,10.0,360.0, 0, 0, "Specifies the number of degrees 'Spin' revolves");
1722         uiDefButS(block, NUM, B_DIFF, "Steps:",                 90,55,80,19, &step,1.0,180.0, 0, 0, "Specifies the total number of 'Spin' slices");
1723         uiDefButS(block, NUM, B_DIFF, "Turns:",                 170,55,85,19, &turn,1.0,360.0, 0, 0, "Specifies the number of revolutions the screw turns");
1724         uiDefButS(block, TOG|BIT|1, B_DIFF, "Keep Original",10,35,160,19, &editbutflag, 0, 0, 0, 0, "Keeps a copy of the original vertices and faces after executing tools");
1725         uiDefButS(block, TOG|BIT|0, B_DIFF, "Clockwise",        170,35,85,19, &editbutflag, 0, 0, 0, 0, "Specifies the direction for 'Screw' and 'Spin'");
1726
1727         uiBlockBeginAlign(block);
1728         uiDefBut(block, BUT,B_EXTREP, "Extrude Dup",    10,10,120,19, 0, 0, 0, 0, 0, "Creates copies of the selected vertices in a straight line away from the current viewport");
1729         uiDefButF(block, NUM, B_DIFF, "Offset:",                130,10,125,19, &extr_offs, 0.01, 10.0, 100, 0, "Sets the distance between each copy for 'Extrude Dup'");
1730         uiBlockEndAlign(block);
1731 }
1732
1733 static void verify_vertexgroup_name_func(void *datav, void *data2_unused)
1734 {
1735         unique_vertexgroup_name((bDeformGroup*)datav, OBACT);
1736 }
1737
1738
1739
1740 static void editing_panel_mesh_tools1(Object *ob, Mesh *me)
1741 {
1742         uiBlock *block;
1743
1744
1745         block= uiNewBlock(&curarea->uiblocks, "editing_panel_mesh_tools1", UI_EMBOSS, UI_HELV, curarea->win);
1746         if(uiNewPanel(curarea, block, "Mesh Tools 1", "Editing", 960, 0, 318, 204)==0) return;
1747
1748         uiDefBut(block, BUT,B_DOCENTRE, "Centre",       1091, 200, 166, 19, 0, 0, 0, 0, 0, "Shifts object data to be centered about object's origin");
1749         uiBlockBeginAlign(block);
1750         uiDefBut(block, BUT,B_HIDE,             "Hide",         1091,155,77,24, 0, 0, 0, 0, 0, "Hides selected faces");
1751         uiDefBut(block, BUT,B_REVEAL,   "Reveal",       1171,155,86,24, 0, 0, 0, 0, 0, "Reveals selected faces");
1752         uiBlockEndAlign(block);
1753
1754         uiDefBut(block, BUT,B_SELSWAP,  "Select Swap",  1091,124,166,24, 0, 0, 0, 0, 0, "Selects unselected faces, and deselects selected faces");
1755
1756         uiBlockBeginAlign(block);
1757         uiDefButF(block, NUM,             REDRAWVIEW3D, "NSize:",               1090, 90, 164, 19, &editbutsize, 0.001, 2.0, 10, 0, "Sets the length to use when displaying face normals");
1758         uiDefButI(block, TOG|BIT|6, REDRAWVIEW3D, "Draw Normals",       1090,70,164,19, &G.f, 0, 0, 0, 0, "Displays face normals as lines");
1759         uiDefButI(block, TOG|BIT|7, REDRAWVIEW3D, "Draw Faces", 1090,50,164,19, &G.f, 0, 0, 0, 0, "Displays all faces as shades");
1760         uiDefButI(block, TOG|BIT|18, REDRAWVIEW3D, "Draw Edges", 1090,30,164,19, &G.f, 0, 0, 0, 0, "Displays selected edges using hilights");
1761         uiDefButI(block, TOG|BIT|11, 0, "All edges",                            1090,10,164,19, &G.f, 0, 0, 0, 0, "Displays all edges in object mode without optimization");
1762         uiBlockEndAlign(block);
1763
1764 }
1765
1766
1767 static void editing_panel_links(Object *ob)
1768 {
1769         uiBlock *block;
1770         ID *id, *idfrom;
1771         int *poin, xco=143;
1772         float min;
1773         Material *ma;
1774         char str[64];
1775         uiBut *but;
1776         
1777         block= uiNewBlock(&curarea->uiblocks, "editing_panel_links", UI_EMBOSS, UI_HELV, curarea->win);
1778         if(uiNewPanel(curarea, block, "Link and Materials", "Editing", 0, 0, 318, 204)==0) return;
1779
1780         buttons_active_id(&id, &idfrom);
1781         
1782         if(id) {
1783                 int alone= 0;
1784                 int local= 0;
1785                 int browse= B_EDITBROWSE;
1786
1787                 if(ob->type==OB_MESH) {
1788                         browse= B_MESHBROWSE;
1789                         alone= B_MESHALONE;
1790                         local= B_MESHLOCAL;
1791                         uiSetButLock(G.obedit!=0, "Unable to perform function in EditMode");
1792                 }
1793                 else if(ob->type==OB_MBALL) {
1794                         alone= B_MBALLALONE;
1795                         local= B_MBALLLOCAL;
1796                 }
1797                 else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
1798                         alone= B_CURVEALONE;
1799                         local= B_CURVELOCAL;
1800                 }
1801                 else if(ob->type==OB_CAMERA) {
1802                         alone= B_CAMERAALONE;
1803                         local= B_CAMERALOCAL;
1804                 }
1805                 else if(ob->type==OB_LAMP) {
1806                         alone= B_LAMPALONE;
1807                         local= B_LAMPLOCAL;
1808                 }
1809                 else if (ob->type==OB_ARMATURE){
1810                         alone = B_ARMALONE;
1811                         local = B_ARMLOCAL;
1812                 }
1813                 else if(ob->type==OB_LATTICE) {
1814                         alone= B_LATTALONE;
1815                         local= B_LATTLOCAL;
1816                 }
1817                 uiBlockSetCol(block, TH_BUT_SETTING2);
1818                 xco= std_libbuttons(block, 143, 180, 0, NULL, browse, id, idfrom, &(G.buts->menunr), alone, local, 0, 0, B_KEEPDATA);
1819                 uiBlockSetCol(block, TH_AUTO);  
1820         }
1821         if(ob) {
1822                 but = uiDefBut(block, TEX, B_IDNAME, "OB:",     xco, 180, 454-xco, YIC, ob->id.name+2, 0.0, 19.0, 0, 0, "Displays Active Object name. Click to change.");
1823                 uiButSetFunc(but, test_idbutton_cb, ob->id.name, NULL);
1824         }
1825
1826
1827
1828         /* to be sure */
1829         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL);
1830         else return;
1831         
1832         
1833         if(ob->type==OB_MESH) poin= &( ((Mesh *)ob->data)->texflag );
1834         else if(ob->type==OB_MBALL) poin= &( ((MetaBall *)ob->data)->texflag );
1835         else poin= &( ((Curve *)ob->data)->texflag );
1836         uiDefButI(block, TOG|BIT|0, B_AUTOTEX, "AutoTexSpace",  143,15,140,19, poin, 0, 0, 0, 0, "Adjusts active object's texture space automatically when transforming object");
1837
1838         sprintf(str,"%d Mat:", ob->totcol);
1839         if(ob->totcol) min= 1.0; else min= 0.0;
1840         ma= give_current_material(ob, ob->actcol);
1841         
1842         if(ma) uiDefBut(block, LABEL, 0, ma->id.name+2, 318,153, 103, 20, 0, 0, 0, 0, 0, "");
1843         
1844         uiBlockBeginAlign(block);
1845         if(ma) uiDefButF(block, COL, 0, "",                     291,123,24,30, &(ma->r), 0, 0, 0, 0, "");
1846         uiDefButC(block, NUM, B_REDR,   str,            318,123,103,30, &ob->actcol, min, (float)(ob->totcol), 0, 0, "Displays total number of material indices and the current index");
1847         uiDefBut(block, BUT,B_MATWICH,  "?",            423,123,31,30, 0, 0, 0, 0, 0, "In EditMode, sets the active material index from selected faces");
1848         
1849         uiBlockBeginAlign(block);
1850         uiDefBut(block, BUT,B_MATNEW,   "New",          292,98,80,20, 0, 0, 0, 0, 0, "Adds a new Material index");
1851         uiDefBut(block, BUT,B_MATDEL,   "Delete",       374,98,80,20, 0, 0, 0, 0, 0, "Deletes this Material index");
1852         uiDefBut(block, BUT,B_MATSEL,   "Select",       292,76,80,20, 0, 0, 0, 0, 0, "In EditMode, selects faces that have the active index");
1853         uiDefBut(block, BUT,B_MATDESEL, "Deselect",     374,76,80,20, 0, 0, 0, 0, 0, "Deselects everything with current indexnumber");
1854         uiDefBut(block, BUT,B_MATASS,   "Assign",       292,47,162,26, 0, 0, 0, 0, 0, "In EditMode, assigns the active index to selected faces");
1855
1856         uiBlockBeginAlign(block);
1857         uiDefBut(block, BUT,B_SETSMOOTH,"Set Smooth",   291,15,80,20, 0, 0, 0, 0, 0, "In EditMode, sets 'smooth' rendering of selected faces");
1858         uiDefBut(block, BUT,B_SETSOLID, "Set Solid",    373,15,80,20, 0, 0, 0, 0, 0, "In EditMode, sets 'solid' rendering of selected faces");
1859         uiBlockEndAlign(block);
1860
1861         /* vertex group... partially editmode... */
1862         if(ob->type==OB_MESH) {
1863                 uiBut *but;
1864                 int     defCount;
1865                 bDeformGroup    *defGroup;
1866                 char *s, *menustr;
1867                 bDeformGroup *dg;
1868                 int min, index;
1869                 char (*qsort_ptr)[32] = NULL;
1870                 
1871                 uiDefBut(block, LABEL,0,"Vertex Groups", 
1872                                  143,153,130,20, 0, 0, 0, 0, 0, "");
1873
1874                 defCount=BLI_countlist(&ob->defbase);
1875
1876                 if (!defCount) min=0;
1877                 else min=1;
1878                 
1879                 if (defCount > 0) {
1880                         /* 
1881                          * This will hold the group names temporarily 
1882                          * so we can sort them 
1883                          */
1884                         qsort_ptr = MEM_callocN (defCount * sizeof (qsort_ptr[0]), 
1885                                                                          "qsort_ptr");
1886                         for (index = 1, dg = ob->defbase.first; dg; index++, dg=dg->next) {
1887                                 snprintf (qsort_ptr[index - 1], sizeof (qsort_ptr[0]), 
1888                                                   "%s%%x%d|", dg->name, index);
1889                         }
1890
1891                         qsort (qsort_ptr, defCount, sizeof (qsort_ptr[0]), 
1892                                    ( int (*)(const void *, const void *) ) strcmp);
1893                 }
1894
1895                 s= menustr = MEM_callocN((32 * defCount)+20, "menustr");
1896                 for (index = 0; index < defCount; index++) {
1897                         int cnt= sprintf (s, "%s", qsort_ptr[index]);
1898                         if (cnt>0) s+= cnt;
1899                 }
1900
1901                 if (qsort_ptr)
1902                   MEM_freeN (qsort_ptr);
1903
1904                 uiBlockBeginAlign(block);
1905                 if (defCount) 
1906                         uiDefButS(block, MENU, REDRAWBUTSEDIT, menustr,
1907                                           143, 132,18,21, &ob->actdef, min, defCount, 0, 0, 
1908                                           "Browses available vertex groups");
1909
1910                 MEM_freeN (menustr);
1911
1912                 if (ob->actdef){
1913                         defGroup = BLI_findlink(&ob->defbase, ob->actdef-1);
1914                         but= uiDefBut(block, TEX,REDRAWBUTSEDIT,"",             161,132,140-18,21, defGroup->name, 0, 32, 0, 0, "Displays current vertex group name. Click to change. (Match bone name for deformation.)");
1915                         uiButSetFunc(but, verify_vertexgroup_name_func, defGroup, NULL);
1916
1917                         uiDefButF(block, NUM, REDRAWVIEW3D, "Weight:",          143, 111, 140, 21, &editbutvweight, 0, 1, 10, 0, "Sets the current vertex group's bone deformation strength");
1918                 }
1919                 uiBlockEndAlign(block);
1920
1921                 if (G.obedit && G.obedit==ob){
1922                         uiBlockBeginAlign(block);
1923         /*              uiDefBut(block, BUT,B_AUTOVGROUP,"Auto Weight",                 740,by-=22,93,18, 0, 0, 0, 0, 0, "Automatically assigns deformation groups"); */
1924                         uiDefBut(block, BUT,B_NEWVGROUP,"New",                  143,90,70,21, 0, 0, 0, 0, 0, "Creates a new vertex group");
1925                         uiDefBut(block, BUT,B_DELVGROUP,"Delete",               213,90,70,21, 0, 0, 0, 0, 0, "Removes the current vertex group");
1926         
1927                         uiDefBut(block, BUT,B_ASSIGNVGROUP,"Assign",    143,69,70,21, 0, 0, 0, 0, 0, "Assigns selected vertices to the current vertex group");
1928                         uiDefBut(block, BUT,B_REMOVEVGROUP,"Remove",    213,69,70,21, 0, 0, 0, 0, 0, "Removes selected vertices from the current vertex group");
1929         
1930                         uiDefBut(block, BUT,B_SELVGROUP,"Select",               143,48,70,21, 0, 0, 0, 0, 0, "Selects vertices belonging to the current vertex group");
1931                         uiDefBut(block, BUT,B_DESELVGROUP,"Desel.",             213,48,70,21, 0, 0, 0, 0, 0, "Deselects vertices belonging to the current vertex group");
1932                         uiBlockEndAlign(block);
1933                 }
1934         }
1935
1936         
1937 }
1938
1939 /* *************************** FACE/PAINT *************************** */
1940
1941 void do_fpaintbuts(unsigned short event)
1942 {
1943         Mesh *me;
1944         Object *ob;
1945         extern TFace *lasttface; /* caches info on tface bookkeeping ?*/
1946         
1947         ob= OBACT;
1948         if(ob==0) return;
1949
1950         switch(event) { 
1951                 
1952         case B_VPGAMMA:
1953                 vpaint_dogamma();
1954                 break;
1955         case B_COPY_TF_MODE:
1956         case B_COPY_TF_UV:
1957         case B_COPY_TF_COL:
1958         case B_COPY_TF_TEX:
1959                 me= get_mesh(ob);
1960                 if(me && me->tface) {
1961 /*                      extern TFace *lasttface; */
1962                         TFace *tface= me->tface;
1963                         int a= me->totface;
1964                         
1965                         set_lasttface();
1966                         if(lasttface) {
1967                         
1968                                 while(a--) {
1969                                         if(tface!=lasttface && (tface->flag & TF_SELECT)) {
1970                                                 if(event==B_COPY_TF_MODE) {
1971                                                         tface->mode= lasttface->mode;
1972                                                         tface->transp= lasttface->transp;
1973                                                 }
1974                                                 else if(event==B_COPY_TF_UV) {
1975                                                         memcpy(tface->uv, lasttface->uv, sizeof(tface->uv));
1976                                                         tface->tpage= lasttface->tpage;
1977                                                         tface->tile= lasttface->tile;
1978                                                         
1979                                                         if(lasttface->mode & TF_TILES) tface->mode |= TF_TILES;
1980                                                         else tface->mode &= ~TF_TILES;
1981                                                         
1982                                                 }
1983                                                 else if(event==B_COPY_TF_TEX) {
1984                                                         tface->tpage= lasttface->tpage;
1985                                                         tface->tile= lasttface->tile;
1986
1987                                                         if(lasttface->mode & TF_TILES) tface->mode |= TF_TILES;
1988                                                         else tface->mode &= ~TF_TILES;
1989                                                 }
1990                                                 else if(event==B_COPY_TF_COL) memcpy(tface->col, lasttface->col, sizeof(tface->col));
1991                                         }
1992                                         tface++;
1993                                 }
1994                         }
1995                         do_shared_vertexcol(me);
1996                         allqueue(REDRAWVIEW3D, 0);
1997                         allqueue(REDRAWIMAGE, 0);
1998                 }
1999                 break;
2000         case B_SET_VCOL:
2001                 clear_vpaint_selectedfaces();
2002                 break;
2003         case B_REDR_3D_IMA:
2004                 allqueue(REDRAWVIEW3D, 0);
2005                 allqueue(REDRAWIMAGE, 0);
2006                 break;
2007         case B_ASSIGNMESH:
2008                 
2009                 test_object_materials(ob->data);
2010                 allqueue(REDRAWVIEW3D, 0);
2011                 allqueue(REDRAWBUTSEDIT, 0);
2012                 break;
2013                 
2014         case B_TFACE_HALO:
2015                 set_lasttface();
2016                 if(lasttface) {
2017                         lasttface->mode &= ~TF_BILLBOARD2;
2018                         allqueue(REDRAWBUTSEDIT, 0);
2019                 }
2020                 break;
2021
2022         case B_TFACE_BILLB:
2023                 set_lasttface();
2024                 if(lasttface) {
2025                         lasttface->mode &= ~TF_BILLBOARD;
2026                         allqueue(REDRAWBUTSEDIT, 0);
2027                 }
2028                 break;
2029         }       
2030 }
2031
2032
2033 /* -------------------- MODE: vpaint faceselect ------------------- */
2034
2035 static void editing_panel_mesh_paint(void)
2036 {
2037         extern VPaint Gvp;         /* from vpaint */
2038         uiBlock *block;
2039
2040         block= uiNewBlock(&curarea->uiblocks, "editing_panel_mesh_paint", UI_EMBOSS, UI_HELV, curarea->win);
2041         if(uiNewPanel(curarea, block, "Paint", "Editing", 640, 0, 318, 204)==0) return;
2042         
2043         uiBlockBeginAlign(block);
2044         uiDefButF(block, NUMSLI, 0, "R ",                       979,160,194,19, &Gvp.r, 0.0, 1.0, B_VPCOLSLI, 0, "The amount of red used for painting");
2045         uiDefButF(block, NUMSLI, 0, "G ",                       979,140,194,19, &Gvp.g, 0.0, 1.0, B_VPCOLSLI, 0, "The amount of green used for painting");
2046         uiDefButF(block, NUMSLI, 0, "B ",                       979,120,194,19, &Gvp.b, 0.0, 1.0, B_VPCOLSLI, 0, "The amount of blue used for painting");
2047         uiBlockEndAlign(block);
2048
2049         uiDefButF(block, NUMSLI, 0, "Opacity ",         979,100,194,19, &Gvp.a, 0.0, 1.0, 0, 0, "The amount of pressure on the brush");
2050         uiDefButF(block, NUMSLI, 0, "Size ",            979,80,194,19, &Gvp.size, 2.0, 64.0, 0, 0, "The size of the brush");
2051
2052         uiDefButF(block, COL, B_VPCOLSLI, "",           1176,100,28,80, &(Gvp.r), 0, 0, 0, 0, "");
2053         uiBlockBeginAlign(block);
2054         uiDefButS(block, ROW, B_DIFF, "Mix",                    1212,160,63,19, &Gvp.mode, 1.0, 0.0, 0, 0, "Mix the vertex colours");
2055         uiDefButS(block, ROW, B_DIFF, "Add",                    1212,140,63,19, &Gvp.mode, 1.0, 1.0, 0, 0, "Add the vertex colour");
2056         uiDefButS(block, ROW, B_DIFF, "Sub",                    1212, 120,63,19, &Gvp.mode, 1.0, 2.0, 0, 0, "Subtract from the vertex colour");
2057         uiDefButS(block, ROW, B_DIFF, "Mul",                    1212, 100,63,19, &Gvp.mode, 1.0, 3.0, 0, 0, "Multiply the vertex colour");
2058         uiDefButS(block, ROW, B_DIFF, "Filter",         1212, 80,63,19, &Gvp.mode, 1.0, 4.0, 0, 0, "Mix the colours with an alpha factor");
2059
2060         uiBlockBeginAlign(block);
2061         uiDefButS(block, TOG|BIT|1, 0, "Area",          980,50,80,19, &Gvp.flag, 0, 0, 0, 0, "Set the area the brush covers");
2062         uiDefButS(block, TOG|BIT|2, 0, "Soft",          1061,50,112,19, &Gvp.flag, 0, 0, 0, 0, "Use a soft brush");
2063         uiDefButS(block, TOG|BIT|3, 0, "Normals",       1174,50,102,19, &Gvp.flag, 0, 0, 0, 0, "Use vertex normal for painting");
2064         
2065         uiBlockBeginAlign(block);
2066         uiDefBut(block, BUT, B_VPGAMMA, "Set",  980,30,80,19, 0, 0, 0, 0, 0, "Apply Mul and Gamma to vertex colours");
2067         uiDefButF(block, NUM, B_DIFF, "Mul:",           1061,30,112,19, &Gvp.mul, 0.1, 50.0, 10, 0, "Set the number to multiply vertex colours with");
2068         uiDefButF(block, NUM, B_DIFF, "Gamma:",         1174,30,102,19, &Gvp.gamma, 0.1, 5.0, 10, 0, "Change the clarity of the vertex colours");
2069         uiBlockEndAlign(block);
2070         
2071         uiDefBut(block, BUT, B_SET_VCOL, "Set VertCol", 980,5,80,20, 0, 0, 0, 0, 0, "Set Vertex colour of selection to current (Shift+K)");
2072
2073 }
2074
2075
2076 static void editing_panel_mesh_texface(void)
2077 {
2078         uiBlock *block;
2079         extern TFace *lasttface;
2080         
2081         block= uiNewBlock(&curarea->uiblocks, "editing_panel_mesh_texface", UI_EMBOSS, UI_HELV, curarea->win);
2082         if(uiNewPanel(curarea, block, "Texture face", "Editing", 960, 0, 318, 204)==0) return;
2083
2084         set_lasttface();        // checks for ob type
2085         if(lasttface) {
2086         
2087                 uiBlockBeginAlign(block);
2088                 uiDefButS(block, TOG|BIT|2, B_REDR_3D_IMA, "Tex",       600,160,60,19, &lasttface->mode, 0, 0, 0, 0, "Render face with texture");
2089                 uiDefButS(block, TOG|BIT|7, B_REDR_3D_IMA, "Tiles",     660,160,60,19, &lasttface->mode, 0, 0, 0, 0, "Use tilemode for face");
2090                 uiDefButS(block, TOG|BIT|4, REDRAWVIEW3D, "Light",      720,160,60,19, &lasttface->mode, 0, 0, 0, 0, "Use light for face");
2091                 uiDefButS(block, TOG|BIT|10, REDRAWVIEW3D, "Invisible",780,160,60,19, &lasttface->mode, 0, 0, 0, 0, "Make face invisible");
2092                 uiDefButS(block, TOG|BIT|0, REDRAWVIEW3D, "Collision", 840,160,60,19, &lasttface->mode, 0, 0, 0, 0, "Use face for collision detection");
2093                 
2094                 uiBlockBeginAlign(block);
2095                 uiDefButS(block, TOG|BIT|6, REDRAWVIEW3D, "Shared",     600,135,60,19, &lasttface->mode, 0, 0, 0, 0, "Blend vertex colours across face when vertices are shared");
2096                 uiDefButS(block, TOG|BIT|9, REDRAWVIEW3D, "Twoside",660,135,60,19, &lasttface->mode, 0, 0, 0, 0, "Render face twosided");
2097                 uiDefButS(block, TOG|BIT|11, REDRAWVIEW3D, "ObColor",720,135,60,19, &lasttface->mode, 0, 0, 0, 0, "Use ObColor instead of vertex colours");
2098
2099                 uiBlockBeginAlign(block);
2100                 uiDefButS(block, TOG|BIT|8, B_TFACE_HALO, "Halo",       600,110,60,19, &lasttface->mode, 0, 0, 0, 0, "Screen aligned billboard");
2101                 uiDefButS(block, TOG|BIT|12, B_TFACE_BILLB, "Billboard",660,110,60,19, &lasttface->mode, 0, 0, 0, 0, "Billboard with Z-axis constraint");
2102                 uiDefButS(block, TOG|BIT|13, REDRAWVIEW3D, "Shadow", 720,110,60,19, &lasttface->mode, 0, 0, 0, 0, "Face is used for shadow");
2103                 uiDefButS(block, TOG|BIT|14, REDRAWVIEW3D, "Text", 780,110,60,19, &lasttface->mode, 0, 0, 0, 0, "Enable bitmap text on face");
2104
2105                 uiBlockBeginAlign(block);
2106                 uiBlockSetCol(block, TH_BUT_SETTING1);
2107                 uiDefButC(block, ROW, REDRAWVIEW3D, "Opaque",   600,80,60,19, &lasttface->transp, 2.0, 0.0, 0, 0, "Render colour of textured face as colour");
2108                 uiDefButC(block, ROW, REDRAWVIEW3D, "Add",              660,80,60,19, &lasttface->transp, 2.0, 1.0, 0, 0, "Render face transparent and add colour of face");
2109                 uiDefButC(block, ROW, REDRAWVIEW3D, "Alpha",    720,80,60,19, &lasttface->transp, 2.0, 2.0, 0, 0, "Render polygon transparent, depending on alpha channel of the texture");
2110
2111                 uiBlockSetCol(block, TH_AUTO);
2112                 uiBlockBeginAlign(block);
2113                 uiDefBut(block, BUT, B_COPY_TF_MODE, "Copy DrawMode", 600,7,117,28, 0, 0, 0, 0, 0, "Copy the drawmode");
2114                 uiDefBut(block, BUT, B_COPY_TF_UV, "Copy UV+tex",         721,7,85,28, 0, 0, 0, 0, 0, "Copy UV information and textures");
2115                 uiDefBut(block, BUT, B_COPY_TF_COL, "Copy VertCol",       809,7,103,28, 0, 0, 0, 0, 0, "Copy vertex colours");
2116         }
2117
2118 }
2119
2120 void do_uvautocalculationbuts(unsigned short event)
2121 {   
2122         switch(event) { 
2123         case B_UVAUTO_STD1:
2124         case B_UVAUTO_STD2:
2125         case B_UVAUTO_STD4:
2126         case B_UVAUTO_STD8:
2127         case B_UVAUTO_CUBE:
2128                 calculate_uv_map(event);
2129                 break;
2130         case B_UVAUTO_BOUNDS1:
2131         case B_UVAUTO_BOUNDS2:
2132         case B_UVAUTO_BOUNDS4:
2133         case B_UVAUTO_BOUNDS8:
2134         case B_UVAUTO_SPHERE:
2135         case B_UVAUTO_CYLINDER:
2136         case B_UVAUTO_WINDOW:
2137                 if(select_area(SPACE_VIEW3D)) calculate_uv_map(event);
2138                 break;
2139         }
2140 }
2141
2142 static void editing_panel_mesh_uvautocalculation(void)
2143 {
2144         uiBlock *block;
2145         int butH= 19, butHB= 20, row= 180, butS= 10;
2146         
2147         block= uiNewBlock(&curarea->uiblocks, "editing_panel_mesh_uvautocalculation", UI_EMBOSS, UI_HELV, curarea->win);
2148         /* make this a tab of "Texture face" to save screen space*/
2149         uiNewPanelTabbed("Texture face", "Editing"); 
2150         if(uiNewPanel(curarea, block, "UV Calculation", "Editing", 960, 0, 318, 204)==0)
2151                 return;
2152         
2153         uiBlockBeginAlign(block);
2154         uiDefBut(block, BUT, B_UVAUTO_STD1,"Standard 1/1",100,row,200,butH, 0, 0, 0, 0, 0, "Applies standard UV mapping");
2155         uiDefBut(block, BUT, B_UVAUTO_STD2,"1/2",100,row-butHB,66,butH, 0, 0, 0, 0, 0, "Applies standard UV mapping 1/2");
2156         uiDefBut(block, BUT, B_UVAUTO_STD4,"1/4",166,row-butHB,67,butH, 0, 0, 0, 0, 0, "Applies standard UV mapping 1/4");
2157         uiDefBut(block, BUT, B_UVAUTO_STD8,"1/8",234,row-butHB,66,butH, 0, 0, 0, 0, 0, "Applies standard UV mapping 1/8");
2158         uiBlockEndAlign(block);
2159         row-= 2*butHB+butS;
2160
2161         uiBlockBeginAlign(block); 
2162         uiDefBut(block, BUT, B_UVAUTO_BOUNDS1,"Bounds 1/1",100,row,200,butH, 0, 0, 0, 0, 0, "Applies planar UV mapping with bounds 1/1");
2163         uiDefBut(block, BUT, B_UVAUTO_BOUNDS2,"1/2",100,row-butHB,66,butH, 0, 0, 0, 0, 0, "Applies planar UV mapping with bounds 1/2");
2164         uiDefBut(block, BUT, B_UVAUTO_BOUNDS4,"1/4",166,row-butHB,67,butH, 0, 0, 0, 0, 0, "Applies planar UV mapping with bounds 1/4");
2165         uiDefBut(block, BUT, B_UVAUTO_BOUNDS8,"1/8",234,row-butHB,66,butH, 0, 0, 0, 0, 0, "Applies planar UV mapping with bounds 1/8");
2166         uiDefBut(block, BUT, B_UVAUTO_WINDOW,"From Window",100,row-2*butH,200,butH, 0, 0, 0, 0, 0, "Applies planar UV mapping from window");
2167         uiBlockEndAlign(block);
2168         row-= 3*butHB+butS;
2169         
2170         uiBlockBeginAlign(block);
2171         uiDefButS(block,ROW,REDRAWVIEW3D,"No Edges",100,row,200,butH,&facesel_draw_edges, 2.0, 0, 0, 0,  "Draw edges in 3D view");
2172         uiDefButS(block,ROW,REDRAWVIEW3D,"Draw Edges",100,row-butH,200,butH,&facesel_draw_edges, 2.0, 1.0, 0, 0,  "Draw edges in 3D view");
2173         uiDefButS(block,ROW,REDRAWVIEW3D,"All Edges",100,row-2*butH,200,butH,&facesel_draw_edges, 2.0, 2.0, 0, 0,  "Draw edges in 3D view");
2174         uiBlockEndAlign(block);
2175         row-= 3*butHB+butS;
2176
2177         row= 180;
2178
2179         uiBlockBeginAlign(block);
2180         uiDefBut(block, BUT, B_UVAUTO_CUBE,"Cube",315,row,200,butH, 0, 0, 0, 0, 0, "Applies cube UV mapping");
2181         uiDefButF(block, NUM,B_UVAUTO_CUBESIZE ,"Size:",315,row-butHB,200,butH, &uv_calc_cubesize, 0.0001, 100.0, 10, 0, "Defines the cubemap size");
2182         uiBlockEndAlign(block);
2183         row-= 2*butHB+butS;
2184
2185         uiBlockBeginAlign(block);
2186         uiDefBut(block, BUT, B_UVAUTO_SPHERE,"Sphere",315,row,200,butH, 0, 0, 0, 0, 0, "Applies spherical UV mapping");
2187         uiBlockEndAlign(block);
2188         row-= butHB+butS;
2189
2190         uiBlockBeginAlign(block);
2191         uiDefBut(block, BUT, B_UVAUTO_CYLINDER,"Cylinder",315,row,200,butH, 0, 0, 0, 0, 0, "Applies cylindrical UV mapping");
2192         uiDefButF(block, NUM,B_UVAUTO_CYLRADIUS ,"Radius:",315,row-butHB,200,butH, &uv_calc_radius, 0.1, 100.0, 10, 0, "Defines the radius of the UV mapping cylinder");                
2193         uiBlockEndAlign(block);
2194         row-= 2*butHB+butS;
2195
2196         
2197         uiBlockBeginAlign(block);
2198         uiDefButS(block, ROW,B_UVAUTO_FACE,"View Aligns Face",315,row,200,butH, &uv_calc_mapdir,2.0, 1.0, 0.0,0.0, "View is on aequator for cylindrical and spherical UV mapping");
2199         uiDefButS(block, ROW,B_UVAUTO_TOP,"VA Top",315,row-butHB,100,butH, &uv_calc_mapdir,2.0, 0.0, 0.0,0.0, "View is on poles for cylindrical and spherical UV mapping");
2200         uiDefButS(block, ROW,B_UVAUTO_TOP,"Al Obj",415,row-butHB,100,butH, &uv_calc_mapdir,2.0, 2.0, 0.0,0.0, "Align to object for cylindrical and spherical UV mapping");
2201         uiBlockEndAlign(block);
2202         row-= 2*butHB+butS;
2203
2204         uiBlockBeginAlign(block);
2205         uiDefButS(block, ROW,B_UVAUTO_ALIGNX,"Polar ZX",315,row,100,butH, &uv_calc_mapalign,2.0, 0.0, 0.0,0.0, "Polar 0 is X for cylindrical and spherical UV mapping");
2206         uiDefButS(block, ROW,B_UVAUTO_ALIGNY,"Polar ZY",415,row,100,butH, &uv_calc_mapalign,2.0, 1.0, 0.0,0.0, "Polar 0 is Y for cylindrical and spherical UV mapping");
2207         uiBlockEndAlign(block);
2208 }
2209
2210 /* this is a mode context sensitive system */
2211
2212 void editing_panels()
2213 {
2214         Object *ob;
2215         Curve *cu;
2216         MetaBall *mb;
2217         Lattice *lt;
2218         bArmature *arm;
2219         Camera *cam;
2220         
2221         ob= OBACT;
2222         if(ob==NULL) return;
2223         
2224         switch(ob->type) {
2225         case OB_MESH:
2226                 editing_panel_links(ob); // no editmode!
2227                 editing_panel_mesh_type(ob, ob->data);  // no editmode!
2228                 /* modes */
2229                 if(G.obedit) {
2230                         editing_panel_mesh_tools(ob, ob->data); // no editmode!
2231                         editing_panel_mesh_tools1(ob, ob->data); // no editmode!
2232                 }
2233                 else {
2234                         if(G.f & G_FACESELECT) {
2235                                 editing_panel_mesh_texface();
2236                                 editing_panel_mesh_uvautocalculation();
2237                         }
2238                         
2239                         if(G.f & (G_VERTEXPAINT | G_TEXTUREPAINT | G_WEIGHTPAINT) )
2240                                 editing_panel_mesh_paint();
2241                 }
2242                 break;
2243                 
2244         case OB_CURVE:
2245         case OB_SURF:
2246                 cu= ob->data;
2247                 editing_panel_links(ob); // no editmode!
2248                 editing_panel_curve_type(ob, cu);
2249                 if(G.obedit) {
2250                         editing_panel_curve_tools(ob, cu);
2251                         editing_panel_curve_tools1(ob, cu);
2252                 }
2253                 break;
2254
2255         case OB_MBALL:
2256                 mb= ob->data;
2257                 editing_panel_links(ob); // no editmode!
2258                 editing_panel_mball_type(ob, mb);
2259                 if(G.obedit) {
2260                         editing_panel_mball_tools(ob, mb);
2261                 }
2262                 break;
2263
2264         case OB_FONT:
2265                 cu= ob->data;
2266                 editing_panel_links(ob); // no editmode!
2267                 editing_panel_curve_type(ob, cu);
2268                 editing_panel_font_type(ob, cu);
2269                 break;
2270
2271         case OB_LATTICE:
2272                 lt= ob->data;
2273                 editing_panel_links(ob); // no editmode!
2274                 editing_panel_lattice_type(ob, lt);
2275                 break;
2276
2277         case OB_LAMP:
2278                 editing_panel_links(ob); // no editmode!
2279                 break;
2280                 
2281         case OB_EMPTY:
2282                 editing_panel_links(ob); // no editmode!
2283                 break;
2284
2285         case OB_CAMERA:
2286                 cam= ob->data;
2287                 editing_panel_links(ob); // no editmode!
2288                 editing_panel_camera_type(ob, cam);
2289                 break;
2290                 
2291         case OB_ARMATURE:
2292                 arm= ob->data;
2293                 editing_panel_links(ob); // no editmode!
2294                 editing_panel_armature_type(ob, arm);
2295                 if(G.obedit) {
2296                         editing_panel_armature_bones(ob, arm);
2297                 }
2298                 break;
2299         }
2300
2301 }