svn merge -r40197:40311 ^/trunk/blender
[blender.git] / source / blender / editors / space_view3d / view3d_buttons.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL 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. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_view3d/view3d_buttons.c
30  *  \ingroup spview3d
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <math.h>
37 #include <float.h>
38
39 #include "DNA_armature_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_lattice_types.h"
42 #include "DNA_meta_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46
47 #include "MEM_guardedalloc.h"
48
49 #include "BLI_math.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_editVert.h"
52 #include "BLI_rand.h"
53 #include "BLI_utildefines.h"
54
55 #include "BKE_action.h"
56 #include "BKE_context.h"
57 #include "BKE_curve.h"
58 #include "BKE_customdata.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_main.h"
61 #include "BKE_mesh.h"
62 #include "BKE_screen.h"
63 #include "BKE_deform.h"
64
65 #include "WM_api.h"
66 #include "WM_types.h"
67
68 #include "RNA_access.h"
69
70 #include "ED_armature.h"
71 #include "ED_gpencil.h"
72 #include "ED_mesh.h"
73 #include "ED_screen.h"
74 #include "ED_transform.h"
75 #include "ED_curve.h"
76
77 #include "UI_interface.h"
78 #include "UI_resources.h"
79
80 #include "view3d_intern.h"      // own include
81
82
83 /* ******************* view3d space & buttons ************** */
84 #define B_NOP           1
85 #define B_REDR          2
86 #define B_OBJECTPANELROT        1007
87 #define B_OBJECTPANELMEDIAN 1008
88 #define B_ARMATUREPANEL1        1009
89 #define B_ARMATUREPANEL2        1010
90 #define B_OBJECTPANELPARENT 1011
91 #define B_OBJECTPANEL           1012
92 #define B_ARMATUREPANEL3        1013
93 #define B_OBJECTPANELSCALE      1014
94 #define B_OBJECTPANELDIMS       1015
95 #define B_TRANSFORMSPACEADD     1016
96 #define B_TRANSFORMSPACECLEAR   1017
97 #define B_SETPT_AUTO    2125
98 #define B_SETPT_VECTOR  2126
99 #define B_SETPT_ALIGN   2127
100 #define B_SETPT_FREE    2128
101 #define B_RECALCMBALL   2501
102
103 #define B_WEIGHT0_0             2840
104 #define B_WEIGHT1_4             2841
105 #define B_WEIGHT1_2             2842
106 #define B_WEIGHT3_4             2843
107 #define B_WEIGHT1_0             2844
108
109 #define B_OPA1_8                2845
110 #define B_OPA1_4                2846
111 #define B_OPA1_2                2847
112 #define B_OPA3_4                2848
113 #define B_OPA1_0                2849
114
115 #define B_CLR_WPAINT    2850
116
117 #define B_RV3D_LOCKED   2900
118 #define B_RV3D_BOXVIEW  2901
119 #define B_RV3D_BOXCLIP  2902
120
121 #define B_IDNAME                3000
122
123 /* temporary struct for storing transform properties */
124 typedef struct {
125         float ob_eul[4];        // used for quat too....
126         float ob_scale[3]; // need temp space due to linked values
127         float ob_dims[3];
128         short link_scale;
129         float ve_median[6];
130         int curdef;
131         float *defweightp;
132 } TransformProperties;
133
134
135 /* is used for both read and write... */
136 static void v3d_editvertex_buts(uiLayout *layout, View3D *v3d, Object *ob, float lim)
137 {
138         uiBlock *block= (layout)? uiLayoutAbsoluteBlock(layout): NULL;
139         MDeformVert *dvert=NULL;
140         TransformProperties *tfp;
141         float median[6], ve_median[6];
142         int tot, totw, totweight, totedge, totradius;
143         char defstr[320];
144         
145         median[0]= median[1]= median[2]= median[3]= median[4]= median[5]= 0.0;
146         tot= totw= totweight= totedge= totradius= 0;
147         defstr[0]= 0;
148
149         /* make sure we got storage */
150         if(v3d->properties_storage==NULL)
151                 v3d->properties_storage= MEM_callocN(sizeof(TransformProperties), "TransformProperties");
152         tfp= v3d->properties_storage;
153         
154         if(ob->type==OB_MESH) {
155                 Mesh *me= ob->data;
156                 EditMesh *em = BKE_mesh_get_editmesh(me);
157                 EditVert *eve, *evedef=NULL;
158                 EditEdge *eed;
159                 
160                 eve= em->verts.first;
161                 while(eve) {
162                         if(eve->f & SELECT) {
163                                 evedef= eve;
164                                 tot++;
165                                 add_v3_v3(median, eve->co);
166                         }
167                         eve= eve->next;
168                 }
169                 eed= em->edges.first;
170                 while(eed) {
171                         if((eed->f & SELECT)) {
172                                 totedge++;
173                                 median[3]+= eed->crease;
174                         }
175                         eed= eed->next;
176                 }
177
178                 /* check for defgroups */
179                 if(evedef)
180                         dvert= CustomData_em_get(&em->vdata, evedef->data, CD_MDEFORMVERT);
181                 if(tot==1 && dvert && dvert->totweight) {
182                         bDeformGroup *dg;
183                         int i, max=1, init=1;
184                         char str[320];
185                         
186                         for (i=0; i<dvert->totweight; i++){
187                                 dg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
188                                 if(dg) {
189                                         max+= BLI_snprintf(str, sizeof(str), "%s %%x%d|", dg->name, dvert->dw[i].def_nr); 
190                                         if(max<320) strcat(defstr, str);
191                                 }
192
193                                 if(tfp->curdef==dvert->dw[i].def_nr) {
194                                         init= 0;
195                                         tfp->defweightp= &dvert->dw[i].weight;
196                                 }
197                         }
198                         
199                         if(init) {      // needs new initialized 
200                                 tfp->curdef= dvert->dw[0].def_nr;
201                                 tfp->defweightp= &dvert->dw[0].weight;
202                         }
203                 }
204
205                 BKE_mesh_end_editmesh(me, em);
206         }
207         else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
208                 Curve *cu= ob->data;
209                 Nurb *nu;
210                 BPoint *bp;
211                 BezTriple *bezt;
212                 int a;
213                 ListBase *nurbs= curve_editnurbs(cu);
214
215                 nu= nurbs->first;
216                 while(nu) {
217                         if(nu->type == CU_BEZIER) {
218                                 bezt= nu->bezt;
219                                 a= nu->pntsu;
220                                 while(a--) {
221                                         if(bezt->f2 & SELECT) {
222                                                 add_v3_v3(median, bezt->vec[1]);
223                                                 tot++;
224                                                 median[4]+= bezt->weight;
225                                                 totweight++;
226                                                 median[5]+= bezt->radius;
227                                                 totradius++;
228                                         }
229                                         else {
230                                                 if(bezt->f1 & SELECT) {
231                                                         add_v3_v3(median, bezt->vec[0]);
232                                                         tot++;
233                                                 }
234                                                 if(bezt->f3 & SELECT) {
235                                                         add_v3_v3(median, bezt->vec[2]);
236                                                         tot++;
237                                                 }
238                                         }
239                                         bezt++;
240                                 }
241                         }
242                         else {
243                                 bp= nu->bp;
244                                 a= nu->pntsu*nu->pntsv;
245                                 while(a--) {
246                                         if(bp->f1 & SELECT) {
247                                                 add_v3_v3(median, bp->vec);
248                                                 median[3]+= bp->vec[3];
249                                                 totw++;
250                                                 tot++;
251                                                 median[4]+= bp->weight;
252                                                 totweight++;
253                                                 median[5]+= bp->radius;
254                                                 totradius++;
255                                         }
256                                         bp++;
257                                 }
258                         }
259                         nu= nu->next;
260                 }
261         }
262         else if(ob->type==OB_LATTICE) {
263                 Lattice *lt= ob->data;
264                 BPoint *bp;
265                 int a;
266                 
267                 a= lt->editlatt->latt->pntsu*lt->editlatt->latt->pntsv*lt->editlatt->latt->pntsw;
268                 bp= lt->editlatt->latt->def;
269                 while(a--) {
270                         if(bp->f1 & SELECT) {
271                                 add_v3_v3(median, bp->vec);
272                                 tot++;
273                                 median[4]+= bp->weight;
274                                 totweight++;
275                         }
276                         bp++;
277                 }
278         }
279         
280         if(tot==0) {
281                 uiDefBut(block, LABEL, 0, "Nothing selected",0, 130, 200, 20, NULL, 0, 0, 0, 0, "");
282                 return;
283         }
284         median[0] /= (float)tot;
285         median[1] /= (float)tot;
286         median[2] /= (float)tot;
287         if(totedge) median[3] /= (float)totedge;
288         else if(totw) median[3] /= (float)totw;
289         if(totweight) median[4] /= (float)totweight;
290         if(totradius) median[5] /= (float)totradius;
291         
292         if(v3d->flag & V3D_GLOBAL_STATS)
293                 mul_m4_v3(ob->obmat, median);
294         
295         if(block) {     // buttons
296                 uiBut *but;
297
298                 memcpy(tfp->ve_median, median, sizeof(tfp->ve_median));
299                 
300                 uiBlockBeginAlign(block);
301                 if(tot==1) {
302                         uiDefBut(block, LABEL, 0, "Vertex:",                                    0, 130, 200, 20, NULL, 0, 0, 0, 0, "");
303                         uiBlockBeginAlign(block);
304
305                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "X:",           0, 110, 200, 20, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
306                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
307                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Y:",           0, 90, 200, 20, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
308                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
309                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Z:",           0, 70, 200, 20, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
310                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
311
312                         if(totw==1) {
313                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "W:",        0, 50, 200, 20, &(tfp->ve_median[3]), 0.01, 100.0, 1, 3, "");
314                                 uiBlockBeginAlign(block);
315                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
316                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
317                                 uiBlockEndAlign(block);
318                                 if(totweight)
319                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 0, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 1, 3, "");
320                                 if(totradius)
321                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 0, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 1, 3, "Radius of curve CPs");
322                                 }
323                         else {
324                                 uiBlockBeginAlign(block);
325                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
326                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
327                                 uiBlockEndAlign(block);
328                                 if(totweight)
329                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 20, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "");
330                                 if(totradius)
331                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 20, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 10, 3, "Radius of curve CPs");
332                         }
333                 }
334                 else {
335                         uiDefBut(block, LABEL, 0, "Median:",                                    0, 130, 200, 20, NULL, 0, 0, 0, 0, "");
336                         uiBlockBeginAlign(block);
337                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "X:",           0, 110, 200, 20, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
338                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
339                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Y:",           0, 90, 200, 20, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
340                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
341                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Z:",           0, 70, 200, 20, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
342                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
343                         if(totw==tot) {
344                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "W:",        0, 50, 200, 20, &(tfp->ve_median[3]), 0.01, 100.0, 1, 3, "");
345                                 uiBlockEndAlign(block);
346                                 uiBlockBeginAlign(block);
347                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
348                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
349                                 uiBlockEndAlign(block);
350                                 if(totweight)
351                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 0, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "Weight is used for SoftBody Goal");
352                                 if(totradius)
353                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 0, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 10, 3, "Radius of curve CPs");
354                                 uiBlockEndAlign(block);
355                         }
356                         else {
357                                 uiBlockBeginAlign(block);
358                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
359                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
360                                 uiBlockEndAlign(block);
361                                 if(totweight)
362                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 20, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 1, 3, "Weight is used for SoftBody Goal");
363                                 if(totradius)
364                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 0, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 1, 3, "Radius of curve CPs");
365                                 uiBlockEndAlign(block);
366                         }
367                 }
368                                 
369                 if(totedge==1)
370                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Crease:",   0, 20, 200, 20, &(tfp->ve_median[3]), 0.0, 1.0, 1, 3, "");
371                 else if(totedge>1)
372                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Mean Crease:",      0, 20, 200, 20, &(tfp->ve_median[3]), 0.0, 1.0, 1, 3, "");
373                 
374         }
375         else {  // apply
376                 memcpy(ve_median, tfp->ve_median, sizeof(tfp->ve_median));
377                 
378                 if(v3d->flag & V3D_GLOBAL_STATS) {
379                         invert_m4_m4(ob->imat, ob->obmat);
380                         mul_m4_v3(ob->imat, median);
381                         mul_m4_v3(ob->imat, ve_median);
382                 }
383                 sub_v3_v3v3(median, ve_median, median);
384                 median[3]= ve_median[3]-median[3];
385                 median[4]= ve_median[4]-median[4];
386                 median[5]= ve_median[5]-median[5];
387                 
388                 if(ob->type==OB_MESH) {
389                         Mesh *me= ob->data;
390                         EditMesh *em = BKE_mesh_get_editmesh(me);
391
392                         /* allow for some rounding error becasue of matrix transform */
393                         if(len_v3(median) > 0.000001f) {
394                                 EditVert *eve;
395
396                                 for(eve= em->verts.first; eve; eve= eve->next) {
397                                         if(eve->f & SELECT) {
398                                                 add_v3_v3(eve->co, median);
399                                         }
400                                 }
401
402                                 recalc_editnormals(em);
403                         }
404
405                         if(median[3] != 0.0f) {
406                                 EditEdge *eed;
407                                 const float fixed_crease= (ve_median[3] <= 0.0f ? 0.0f : (ve_median[3] >= 1.0f ? 1.0f : FLT_MAX));
408                                 
409                                 if(fixed_crease != FLT_MAX) {
410                                         /* simple case */
411
412                                         for(eed= em->edges.first; eed; eed= eed->next) {
413                                                 if(eed->f & SELECT) {
414                                                         eed->crease= fixed_crease;
415                                                 }
416                                         }
417                                 }
418                                 else {
419                                         /* scale crease to target median */
420                                         float median_new= ve_median[3];
421                                         float median_orig= ve_median[3] - median[3]; /* previous median value */
422
423                                         /* incase of floating point error */
424                                         CLAMP(median_orig, 0.0f, 1.0f);
425                                         CLAMP(median_new, 0.0f, 1.0f);
426
427                                         if(median_new < median_orig) {
428                                                 /* scale down */
429                                                 const float sca= median_new / median_orig;
430                                                 
431                                                 for(eed= em->edges.first; eed; eed= eed->next) {
432                                                         if(eed->f & SELECT) {
433                                                                 eed->crease *= sca;
434                                                                 CLAMP(eed->crease, 0.0f, 1.0f);
435                                                         }
436                                                 }
437                                         }
438                                         else {
439                                                 /* scale up */
440                                                 const float sca= (1.0f - median_new) / (1.0f - median_orig);
441
442                                                 for(eed= em->edges.first; eed; eed= eed->next) {
443                                                         if(eed->f & SELECT) {
444                                                                 eed->crease = 1.0f - ((1.0f - eed->crease) * sca);
445                                                                 CLAMP(eed->crease, 0.0f, 1.0f);
446                                                         }
447                                                 }
448                                         }
449                                 }
450                         }
451
452                         BKE_mesh_end_editmesh(me, em);
453                 }
454                 else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
455                         Curve *cu= ob->data;
456                         Nurb *nu;
457                         BPoint *bp;
458                         BezTriple *bezt;
459                         int a;
460                         ListBase *nurbs= curve_editnurbs(cu);
461
462                         nu= nurbs->first;
463                         while(nu) {
464                                 if(nu->type == CU_BEZIER) {
465                                         bezt= nu->bezt;
466                                         a= nu->pntsu;
467                                         while(a--) {
468                                                 if(bezt->f2 & SELECT) {
469                                                         add_v3_v3(bezt->vec[0], median);
470                                                         add_v3_v3(bezt->vec[1], median);
471                                                         add_v3_v3(bezt->vec[2], median);
472                                                         bezt->weight+= median[4];
473                                                         bezt->radius+= median[5];
474                                                 }
475                                                 else {
476                                                         if(bezt->f1 & SELECT) {
477                                                                 add_v3_v3(bezt->vec[0], median);
478                                                         }
479                                                         if(bezt->f3 & SELECT) {
480                                                                 add_v3_v3(bezt->vec[2], median);
481                                                         }
482                                                 }
483                                                 bezt++;
484                                         }
485                                 }
486                                 else {
487                                         bp= nu->bp;
488                                         a= nu->pntsu*nu->pntsv;
489                                         while(a--) {
490                                                 if(bp->f1 & SELECT) {
491                                                         add_v3_v3(bp->vec, median);
492                                                         bp->vec[3]+= median[3];
493                                                         bp->weight+= median[4];
494                                                         bp->radius+= median[5];
495                                                 }
496                                                 bp++;
497                                         }
498                                 }
499                                 test2DNurb(nu);
500                                 testhandlesNurb(nu); /* test for bezier too */
501
502                                 nu= nu->next;
503                         }
504                 }
505                 else if(ob->type==OB_LATTICE) {
506                         Lattice *lt= ob->data;
507                         BPoint *bp;
508                         int a;
509                         
510                         a= lt->editlatt->latt->pntsu*lt->editlatt->latt->pntsv*lt->editlatt->latt->pntsw;
511                         bp= lt->editlatt->latt->def;
512                         while(a--) {
513                                 if(bp->f1 & SELECT) {
514                                         add_v3_v3(bp->vec, median);
515                                         bp->weight+= median[4];
516                                 }
517                                 bp++;
518                         }
519                 }
520                 
521 //              ED_undo_push(C, "Transform properties");
522         }
523 }
524 #define B_VGRP_PNL_COPY 1
525 #define B_VGRP_PNL_NORMALIZE 2
526 #define B_VGRP_PNL_EDIT_SINGLE 8 /* or greater */
527 #define B_VGRP_PNL_COPY_SINGLE 16384 /* or greater */
528
529 static void act_vert_def(Object *ob, EditVert **eve, MDeformVert **dvert)
530 {
531         if(ob && ob->mode & OB_MODE_EDIT && ob->type==OB_MESH && ob->defbase.first) {
532                 Mesh *me= ob->data;
533                 EditMesh *em = BKE_mesh_get_editmesh(me);
534                 EditSelection *ese = ((EditSelection*)em->selected.last);
535
536                 if(ese && ese->type == EDITVERT) {
537                         *eve= (EditVert*)ese->data;
538                         *dvert= CustomData_em_get(&em->vdata, (*eve)->data, CD_MDEFORMVERT);
539                         return;
540                 }
541
542                 BKE_mesh_end_editmesh(me, em);
543         }
544
545         *eve= NULL;
546         *dvert= NULL;
547 }
548
549 static void editvert_mirror_update(Object *ob, EditVert *eve, int def_nr, int index)
550 {
551         Mesh *me= ob->data;
552         EditMesh *em = BKE_mesh_get_editmesh(me);
553         EditVert *eve_mirr;
554
555         eve_mirr= editmesh_get_x_mirror_vert(ob, em, eve, eve->co, index);
556
557         if(eve_mirr && eve_mirr != eve) {
558                 MDeformVert *dvert_src= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
559                 MDeformVert *dvert_dst= CustomData_em_get(&em->vdata, eve_mirr->data, CD_MDEFORMVERT);
560                 if(dvert_dst) {
561                         if(def_nr == -1) {
562                                 /* all vgroups, add groups where neded  */
563
564                                 int *flip_map= defgroup_flip_map(ob, 1);
565                                 defvert_sync_mapped(dvert_dst, dvert_src, flip_map, 1);
566                                 MEM_freeN(flip_map);
567                         }
568                         else {
569                                 /* single vgroup */
570                                 MDeformWeight *dw= defvert_verify_index(dvert_dst, defgroup_flip_index(ob, def_nr, 1));
571                                 if(dw) {
572                                         dw->weight= defvert_find_weight(dvert_src, def_nr);
573                                 }
574                         }
575                 }
576         }
577 }
578
579 static void vgroup_adjust_active(Object *ob, int def_nr)
580 {
581         EditVert *eve_act;
582         MDeformVert *dvert_act;
583
584         act_vert_def(ob, &eve_act, &dvert_act);
585
586         if(dvert_act) {
587                 if(((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X)
588                         editvert_mirror_update(ob, eve_act, def_nr, -1);
589         }
590 }
591
592 static void vgroup_copy_active_to_sel(Object *ob)
593 {
594         EditVert *eve_act;
595         MDeformVert *dvert_act;
596
597         act_vert_def(ob, &eve_act, &dvert_act);
598
599         if(dvert_act==NULL) {
600                 return;
601         }
602         else {
603                 Mesh *me= ob->data;
604                 EditMesh *em = BKE_mesh_get_editmesh(me);
605                 EditVert *eve;
606                 MDeformVert *dvert;
607                 int index= 0;
608
609                 for(eve= em->verts.first; eve; eve= eve->next, index++) {
610                         if(eve->f & SELECT && eve != eve_act) {
611                                 dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
612                                 if(dvert) {
613                                         defvert_copy(dvert, dvert_act);
614
615                                         if(me->editflag & ME_EDIT_MIRROR_X)
616                                                 editvert_mirror_update(ob, eve, -1, index);
617
618                                 }
619                         }
620                 }
621         }
622 }
623
624 static void vgroup_copy_active_to_sel_single(Object *ob, int def_nr)
625 {
626         EditVert *eve_act;
627         MDeformVert *dvert_act;
628
629         act_vert_def(ob, &eve_act, &dvert_act);
630
631         if(dvert_act==NULL) {
632                 return;
633         }
634         else {
635                 Mesh *me= ob->data;
636                 EditMesh *em = BKE_mesh_get_editmesh(me);
637                 EditVert *eve;
638                 MDeformVert *dvert;
639                 MDeformWeight *dw;
640                 float act_weight = -1.0f;
641                 int i;
642                 int index= 0;
643
644                 for(i=0, dw=dvert_act->dw; i < dvert_act->totweight; i++, dw++) {
645                         if(def_nr == dw->def_nr) {
646                                 act_weight= dw->weight;
647                                 break;
648                         }
649                 }
650
651                 if(act_weight < -0.5f)
652                         return;
653
654                 for(eve= em->verts.first; eve; eve= eve->next, index++) {
655                         if(eve->f & SELECT && eve != eve_act) {
656                                 dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
657                                 if(dvert) {
658                                         for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++) {
659                                                 if(def_nr == dw->def_nr) {
660                                                         dw->weight= act_weight;
661
662                                                         if(me->editflag & ME_EDIT_MIRROR_X)
663                                                                 editvert_mirror_update(ob, eve, -1, index);
664
665                                                         break;
666                                                 }
667                                         }
668                                 }
669                         }
670                 }
671
672                 if(me->editflag & ME_EDIT_MIRROR_X)
673                         editvert_mirror_update(ob, eve_act, -1, -1);
674
675         }
676 }
677
678 static void vgroup_normalize_active(Object *ob)
679 {
680         EditVert *eve_act;
681         MDeformVert *dvert_act;
682
683         act_vert_def(ob, &eve_act, &dvert_act);
684
685         if(dvert_act==NULL)
686                 return;
687
688         defvert_normalize(dvert_act);
689
690         if(((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X)
691                 editvert_mirror_update(ob, eve_act, -1, -1);
692
693
694
695 }
696
697 static void do_view3d_vgroup_buttons(bContext *C, void *UNUSED(arg), int event)
698 {
699         Scene *scene= CTX_data_scene(C);
700         Object *ob= OBACT;
701
702         if(event==B_VGRP_PNL_NORMALIZE) {
703                 vgroup_normalize_active(ob);
704         }
705         else if(event == B_VGRP_PNL_COPY) {
706                 vgroup_copy_active_to_sel(ob);
707         }
708         else if(event >= B_VGRP_PNL_COPY_SINGLE) {
709                 vgroup_copy_active_to_sel_single(ob, event - B_VGRP_PNL_COPY_SINGLE);
710         }
711         else if(event >= B_VGRP_PNL_EDIT_SINGLE) {
712                 vgroup_adjust_active(ob, event - B_VGRP_PNL_EDIT_SINGLE);
713         }
714
715 //  todo
716 //      if(((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X)
717 //              ED_vgroup_mirror(ob, 1, 1, 0);
718
719         /* default for now */
720         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
721         WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
722 }
723
724 static int view3d_panel_vgroup_poll(const bContext *C, PanelType *UNUSED(pt))
725 {
726         Scene *scene= CTX_data_scene(C);
727         Object *ob= OBACT;
728         EditVert *eve_act;
729         MDeformVert *dvert_act;
730
731         act_vert_def(ob, &eve_act, &dvert_act);
732
733         return dvert_act ? dvert_act->totweight : 0;
734 }
735
736
737 static void view3d_panel_vgroup(const bContext *C, Panel *pa)
738 {
739         uiBlock *block= uiLayoutAbsoluteBlock(pa->layout);
740         Scene *scene= CTX_data_scene(C);
741         Object *ob= OBACT;
742
743         EditVert *eve;
744         MDeformVert *dvert;
745
746         act_vert_def(ob, &eve, &dvert);
747
748         if(dvert && dvert->totweight) {
749                 uiLayout *col;
750                 bDeformGroup *dg;
751                 int i;
752                 int yco = 0;
753
754                 uiBlockSetHandleFunc(block, do_view3d_vgroup_buttons, NULL);
755
756                 col= uiLayoutColumn(pa->layout, 0);
757                 block= uiLayoutAbsoluteBlock(col);
758
759                 uiBlockBeginAlign(block);
760
761                 for (i=0; i<dvert->totweight; i++){
762                         dg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
763                         if(dg) {
764                                 uiDefButF(block, NUM, B_VGRP_PNL_EDIT_SINGLE + dvert->dw[i].def_nr, dg->name,   0, yco, 180, 20, &dvert->dw[i].weight, 0.0, 1.0, 1, 3, "");
765                                 uiDefBut(block, BUT, B_VGRP_PNL_COPY_SINGLE + dvert->dw[i].def_nr, "C", 180,yco,20,20, NULL, 0, 0, 0, 0, "Copy this groups weight to other selected verts");
766                                 yco -= 20;
767                         }
768                 }
769                 yco-=2;
770
771                 uiBlockEndAlign(block);
772                 uiBlockBeginAlign(block);
773                 uiDefBut(block, BUT, B_VGRP_PNL_NORMALIZE, "Normalize", 0, yco,100,20, NULL, 0, 0, 0, 0, "Normalize active vertex weights");
774                 uiDefBut(block, BUT, B_VGRP_PNL_COPY, "Copy", 100,yco,100,20, NULL, 0, 0, 0, 0, "Copy active vertex to other seleted verts");
775                 uiBlockEndAlign(block);
776         }
777 }
778
779 static void v3d_transform_butsR(uiLayout *layout, PointerRNA *ptr)
780 {
781         uiLayout *split, *colsub;
782         
783         split = uiLayoutSplit(layout, 0.8, 0);
784         
785         if (ptr->type == &RNA_PoseBone) {
786                 PointerRNA boneptr;
787                 Bone *bone;
788                 
789                 boneptr = RNA_pointer_get(ptr, "bone");
790                 bone = boneptr.data;
791                 uiLayoutSetActive(split, !(bone->parent && bone->flag & BONE_CONNECTED));
792         }
793         colsub = uiLayoutColumn(split, 1);
794         uiItemR(colsub, ptr, "location", 0, "Location", ICON_NONE);
795         colsub = uiLayoutColumn(split, 1);
796         uiItemL(colsub, "", ICON_NONE);
797         uiItemR(colsub, ptr, "lock_location", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
798         
799         split = uiLayoutSplit(layout, 0.8, 0);
800         
801         switch(RNA_enum_get(ptr, "rotation_mode")) {
802                 case ROT_MODE_QUAT: /* quaternion */
803                         colsub = uiLayoutColumn(split, 1);
804                         uiItemR(colsub, ptr, "rotation_quaternion", 0, "Rotation", ICON_NONE);
805                         colsub = uiLayoutColumn(split, 1);
806                         uiItemR(colsub, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE, "4L", ICON_NONE);
807                         if (RNA_boolean_get(ptr, "lock_rotations_4d"))
808                                 uiItemR(colsub, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
809                         else
810                                 uiItemL(colsub, "", ICON_NONE);
811                         uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
812                         break;
813                 case ROT_MODE_AXISANGLE: /* axis angle */
814                         colsub = uiLayoutColumn(split, 1);
815                         uiItemR(colsub, ptr, "rotation_axis_angle", 0, "Rotation", ICON_NONE);
816                         colsub = uiLayoutColumn(split, 1);
817                         uiItemR(colsub, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE, "4L", ICON_NONE);
818                         if (RNA_boolean_get(ptr, "lock_rotations_4d"))
819                                 uiItemR(colsub, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
820                         else
821                                 uiItemL(colsub, "", ICON_NONE);
822                         uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
823                         break;
824                 default: /* euler rotations */
825                         colsub = uiLayoutColumn(split, 1);
826                         uiItemR(colsub, ptr, "rotation_euler", 0, "Rotation", ICON_NONE);
827                         colsub = uiLayoutColumn(split, 1);
828                         uiItemL(colsub, "", ICON_NONE);
829                         uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
830                         break;
831         }
832         uiItemR(layout, ptr, "rotation_mode", 0, "", ICON_NONE);
833         
834         split = uiLayoutSplit(layout, 0.8, 0);
835         colsub = uiLayoutColumn(split, 1);
836         uiItemR(colsub, ptr, "scale", 0, "Scale", ICON_NONE);
837         colsub = uiLayoutColumn(split, 1);
838         uiItemL(colsub, "", ICON_NONE);
839         uiItemR(colsub, ptr, "lock_scale", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
840         
841         if (ptr->type == &RNA_Object) {
842                 Object *ob = ptr->data;
843                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL))
844                         uiItemR(layout, ptr, "dimensions", 0, "Dimensions", ICON_NONE);
845         }
846 }
847
848 static void v3d_posearmature_buts(uiLayout *layout, Object *ob)
849 {
850 //      uiBlock *block= uiLayoutGetBlock(layout);
851 //      bArmature *arm;
852         bPoseChannel *pchan;
853 //      TransformProperties *tfp= v3d->properties_storage;
854         PointerRNA pchanptr;
855         uiLayout *col;
856 //      uiLayout *row;
857 //      uiBut *but;
858
859         pchan= get_active_posechannel(ob);
860
861 //      row= uiLayoutRow(layout, 0);
862         
863         if (!pchan)     {
864                 uiItemL(layout, "No Bone Active", ICON_NONE);
865                 return; 
866         }
867
868         RNA_pointer_create(&ob->id, &RNA_PoseBone, pchan, &pchanptr);
869
870         col= uiLayoutColumn(layout, 0);
871         
872         /* XXX: RNA buts show data in native types (i.e. quats, 4-component axis/angle, etc.)
873          * but oldskool UI shows in eulers always. Do we want to be able to still display in Eulers?
874          * Maybe needs RNA/ui options to display rotations as different types... */
875         v3d_transform_butsR(col, &pchanptr);
876
877 #if 0
878         uiLayoutAbsoluteBlock(layout);
879
880         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
881                 float quat[4];
882                 /* convert to euler, passing through quats... */
883                 axis_angle_to_quat(quat, pchan->rotAxis, pchan->rotAngle);
884                 quat_to_eul( tfp->ob_eul,quat);
885         }
886         else if (pchan->rotmode == ROT_MODE_QUAT)
887                 quat_to_eul( tfp->ob_eul,pchan->quat);
888         else
889                 copy_v3_v3(tfp->ob_eul, pchan->eul);
890         tfp->ob_eul[0]*= RAD2DEGF(1.0f);
891         tfp->ob_eul[1]*= RAD2DEGF(1.0f);
892         tfp->ob_eul[2]*= RAD2DEGF(1.0f);
893         
894         uiDefBut(block, LABEL, 0, "Location:",                  0, 240, 100, 20, 0, 0, 0, 0, 0, "");
895         uiBlockBeginAlign(block);
896         
897         but= uiDefButF(block, NUM, B_ARMATUREPANEL2, "X:",      0, 220, 120, 19, pchan->loc, -lim, lim, 100, 3, "");
898         uiButSetUnitType(but, PROP_UNIT_LENGTH);
899         but= uiDefButF(block, NUM, B_ARMATUREPANEL2, "Y:",      0, 200, 120, 19, pchan->loc+1, -lim, lim, 100, 3, "");
900         uiButSetUnitType(but, PROP_UNIT_LENGTH);
901         but= uiDefButF(block, NUM, B_ARMATUREPANEL2, "Z:",      0, 180, 120, 19, pchan->loc+2, -lim, lim, 100, 3, "");
902         uiButSetUnitType(but, PROP_UNIT_LENGTH);
903         uiBlockEndAlign(block);
904         
905         uiBlockBeginAlign(block);
906         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCX, B_REDR, ICON_UNLOCKED,   125, 220, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects X Location value from being Transformed");
907         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCY, B_REDR, ICON_UNLOCKED,   125, 200, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Y Location value from being Transformed");
908         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCZ, B_REDR, ICON_UNLOCKED,   125, 180, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Z Location value from being Transformed");
909         uiBlockEndAlign(block);
910         
911         uiDefBut(block, LABEL, 0, "Rotation:",                  0, 160, 100, 20, 0, 0, 0, 0, 0, "");
912         uiBlockBeginAlign(block);
913         uiDefButF(block, NUM, B_ARMATUREPANEL3, "X:",   0, 140, 120, 19, tfp->ob_eul, -1000.0, 1000.0, 100, 3, "");
914         uiDefButF(block, NUM, B_ARMATUREPANEL3, "Y:",   0, 120, 120, 19, tfp->ob_eul+1, -1000.0, 1000.0, 100, 3, "");
915         uiDefButF(block, NUM, B_ARMATUREPANEL3, "Z:",   0, 100, 120, 19, tfp->ob_eul+2, -1000.0, 1000.0, 100, 3, "");
916         uiBlockEndAlign(block);
917         
918         uiBlockBeginAlign(block);
919         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, B_REDR, ICON_UNLOCKED,   125, 140, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects X Rotation value from being Transformed");
920         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, B_REDR, ICON_UNLOCKED,   125, 120, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Y Rotation value from being Transformed");
921         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, B_REDR, ICON_UNLOCKED,   125, 100, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Z Rotation value from being Transformed");
922         uiBlockEndAlign(block);
923         
924         uiDefBut(block, LABEL, 0, "Scale:",                             0, 80, 100, 20, 0, 0, 0, 0, 0, "");
925         uiBlockBeginAlign(block);
926         uiDefButF(block, NUM, B_ARMATUREPANEL2, "X:",   0, 60, 120, 19, pchan->size, -lim, lim, 10, 3, "");
927         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Y:",   0, 40, 120, 19, pchan->size+1, -lim, lim, 10, 3, "");
928         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Z:",   0, 20, 120, 19, pchan->size+2, -lim, lim, 10, 3, "");
929         uiBlockEndAlign(block);
930         
931         uiBlockBeginAlign(block);
932         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEX, B_REDR, ICON_UNLOCKED, 125, 60, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects X Scale value from being Transformed");
933         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEY, B_REDR, ICON_UNLOCKED, 125, 40, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Y Scale value from being Transformed");
934         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEZ, B_REDR, ICON_UNLOCKED, 125, 20, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects z Scale value from being Transformed");
935         uiBlockEndAlign(block);
936 #endif
937 }
938
939 /* assumes armature editmode */
940 #if 0
941 static void validate_editbonebutton_cb(bContext *C, void *bonev, void *namev)
942 {
943         EditBone *eBone= bonev;
944         char oldname[sizeof(eBone->name)], newname[sizeof(eBone->name)];
945
946         /* need to be on the stack */
947         BLI_strncpy(newname, eBone->name, sizeof(eBone->name));
948         BLI_strncpy(oldname, (char *)namev, sizeof(eBone->name));
949         /* restore */
950         BLI_strncpy(eBone->name, oldname, sizeof(eBone->name));
951
952         ED_armature_bone_rename(CTX_data_edit_object(C)->data, oldname, newname); // editarmature.c
953         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, CTX_data_edit_object(C)); // XXX fix
954 }
955 #endif
956
957 static void v3d_editarmature_buts(uiLayout *layout, Object *ob)
958 {
959 //      uiBlock *block= uiLayoutGetBlock(layout);
960         bArmature *arm= ob->data;
961         EditBone *ebone;
962 //      TransformProperties *tfp= v3d->properties_storage;
963 //      uiLayout *row;
964         uiLayout *col;
965         PointerRNA eboneptr;
966         
967         ebone= arm->act_edbone;
968
969         if (!ebone || (ebone->layer & arm->layer)==0) {
970                 uiItemL(layout, "Nothing selected", ICON_NONE);
971                 return;
972         }
973 //      row= uiLayoutRow(layout, 0);
974         RNA_pointer_create(&arm->id, &RNA_EditBone, ebone, &eboneptr);
975
976         col= uiLayoutColumn(layout, 0);
977         uiItemR(col, &eboneptr, "head", 0, "Head", ICON_NONE);
978         if (ebone->parent && ebone->flag & BONE_CONNECTED ) {
979                 PointerRNA parptr = RNA_pointer_get(&eboneptr, "parent");
980                 uiItemR(col, &parptr, "tail_radius", 0, "Radius (Parent)", ICON_NONE);
981         } else {
982                 uiItemR(col, &eboneptr, "head_radius", 0, "Radius", ICON_NONE);
983         }
984         
985         uiItemR(col, &eboneptr, "tail", 0, "Tail", ICON_NONE);
986         uiItemR(col, &eboneptr, "tail_radius", 0, "Radius", ICON_NONE);
987         
988         uiItemR(col, &eboneptr, "roll", 0, "Roll", ICON_NONE);
989 }
990
991 static void v3d_editmetaball_buts(uiLayout *layout, Object *ob)
992 {
993         PointerRNA mbptr, ptr;
994         MetaBall *mball= ob->data;
995 //      uiLayout *row;
996         uiLayout *col;
997         
998         if (!mball || !(mball->lastelem)) return;
999         
1000         RNA_pointer_create(&mball->id, &RNA_MetaBall, mball, &mbptr);
1001         
1002 //      row= uiLayoutRow(layout, 0);
1003
1004         RNA_pointer_create(&mball->id, &RNA_MetaElement, mball->lastelem, &ptr);
1005         
1006         col= uiLayoutColumn(layout, 0);
1007         uiItemR(col, &ptr, "co", 0, "Location", ICON_NONE);
1008         
1009         uiItemR(col, &ptr, "radius", 0, "Radius", ICON_NONE);
1010         uiItemR(col, &ptr, "stiffness", 0, "Stiffness", ICON_NONE);
1011         
1012         uiItemR(col, &ptr, "type", 0, "Type", ICON_NONE);
1013         
1014         col= uiLayoutColumn(layout, 1);
1015         switch (RNA_enum_get(&ptr, "type")) {
1016                 case MB_BALL:
1017                         break;
1018                 case MB_CUBE:
1019                         uiItemL(col, "Size:", ICON_NONE);
1020                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1021                         uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NONE);
1022                         uiItemR(col, &ptr, "size_z", 0, "Z", ICON_NONE);
1023                         break;
1024                 case MB_TUBE:
1025                         uiItemL(col, "Size:", ICON_NONE);
1026                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1027                         break;
1028                 case MB_PLANE:
1029                         uiItemL(col, "Size:", ICON_NONE);
1030                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1031                         uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NONE);
1032                         break;
1033                 case MB_ELIPSOID:
1034                         uiItemL(col, "Size:", ICON_NONE);
1035                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1036                         uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NONE);
1037                         uiItemR(col, &ptr, "size_z", 0, "Z", ICON_NONE);
1038                         break;             
1039         }       
1040 }
1041
1042 /* test if 'ob' is a parent somewhere in par's parents */
1043 static int test_parent_loop(Object *par, Object *ob)
1044 {
1045         if(par == NULL) return 0;
1046         if(ob == par) return 1;
1047         return test_parent_loop(par->parent, ob);
1048 }
1049
1050 static void do_view3d_region_buttons(bContext *C, void *UNUSED(index), int event)
1051 {
1052         Main *bmain= CTX_data_main(C);
1053         Scene *scene= CTX_data_scene(C);
1054 //      Object *obedit= CTX_data_edit_object(C);
1055         View3D *v3d= CTX_wm_view3d(C);
1056 //      BoundBox *bb;
1057         Object *ob= OBACT;
1058         TransformProperties *tfp= v3d->properties_storage;
1059         
1060         switch(event) {
1061         
1062         case B_REDR:
1063                 ED_area_tag_redraw(CTX_wm_area(C));
1064                 return; /* no notifier! */
1065                 
1066         case B_OBJECTPANEL:
1067                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1068                 break;
1069
1070         
1071         case B_OBJECTPANELMEDIAN:
1072                 if(ob) {
1073                         v3d_editvertex_buts(NULL, v3d, ob, 1.0);
1074                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1075                 }
1076                 break;
1077                 
1078                 /* note; this case also used for parbone */
1079         case B_OBJECTPANELPARENT:
1080                 if(ob) {
1081                         if(ob->id.lib || test_parent_loop(ob->parent, ob) ) 
1082                                 ob->parent= NULL;
1083                         else {
1084                                 DAG_scene_sort(bmain, scene);
1085                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1086                         }
1087                 }
1088                 break;
1089                 
1090
1091         case B_ARMATUREPANEL3:  // rotate button on channel
1092                 {
1093                         bPoseChannel *pchan;
1094                         float eul[3];
1095                         
1096                         pchan= get_active_posechannel(ob);
1097                         if (!pchan) return;
1098                         
1099                         /* make a copy to eul[3], to allow TAB on buttons to work */
1100                         eul[0]= DEG2RADF(tfp->ob_eul[0]);
1101                         eul[1]= DEG2RADF(tfp->ob_eul[1]);
1102                         eul[2]= DEG2RADF(tfp->ob_eul[2]);
1103                         
1104                         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1105                                 float quat[4];
1106                                 /* convert to axis-angle, passing through quats  */
1107                                 eul_to_quat( quat,eul);
1108                                 quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,quat);
1109                         }
1110                         else if (pchan->rotmode == ROT_MODE_QUAT)
1111                                 eul_to_quat( pchan->quat,eul);
1112                         else
1113                                 copy_v3_v3(pchan->eul, eul);
1114                 }
1115                 /* no break, pass on */
1116         case B_ARMATUREPANEL2:
1117                 {
1118                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1119                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1120                 }
1121                 break;
1122         case B_TRANSFORMSPACEADD:
1123         {
1124                 char names[sizeof(((TransformOrientation *)NULL)->name)]= "";
1125                 BIF_createTransformOrientation(C, NULL, names, 1, 0);
1126                 break;
1127         }
1128         case B_TRANSFORMSPACECLEAR:
1129                 BIF_clearTransformOrientation(C);
1130                 break;
1131                 
1132 #if 0 // XXX
1133         case B_WEIGHT0_0:
1134                 wpaint->weight = 0.0f;
1135                 break;
1136                 
1137         case B_WEIGHT1_4:
1138                 wpaint->weight = 0.25f;
1139                 break;
1140         case B_WEIGHT1_2:
1141                 wpaint->weight = 0.5f;
1142                 break;
1143         case B_WEIGHT3_4:
1144                 wpaint->weight = 0.75f;
1145                 break;
1146         case B_WEIGHT1_0:
1147                 wpaint->weight = 1.0f;
1148                 break;
1149                 
1150         case B_OPA1_8:
1151                 wpaint->a = 0.125f;
1152                 break;
1153         case B_OPA1_4:
1154                 wpaint->a = 0.25f;
1155                 break;
1156         case B_OPA1_2:
1157                 wpaint->a = 0.5f;
1158                 break;
1159         case B_OPA3_4:
1160                 wpaint->a = 0.75f;
1161                 break;
1162         case B_OPA1_0:
1163                 wpaint->a = 1.0f;
1164                 break;
1165 #endif
1166         case B_CLR_WPAINT:
1167 //              if(!multires_level1_test()) {
1168                 {
1169                         bDeformGroup *defGroup = BLI_findlink(&ob->defbase, ob->actdef-1);
1170                         if(defGroup) {
1171                                 Mesh *me= ob->data;
1172                                 int a;
1173                                 for(a=0; a<me->totvert; a++)
1174                                         ED_vgroup_vert_remove (ob, defGroup, a);
1175                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1176                         }
1177                 }
1178                 break;
1179         case B_RV3D_LOCKED:
1180         case B_RV3D_BOXVIEW:
1181         case B_RV3D_BOXCLIP:
1182                 {
1183                         ScrArea *sa= CTX_wm_area(C);
1184                         ARegion *ar= sa->regionbase.last;
1185                         RegionView3D *rv3d;
1186                         short viewlock;
1187                         
1188                         ar= ar->prev;
1189                         rv3d= ar->regiondata;
1190                         viewlock= rv3d->viewlock;
1191                         
1192                         if((viewlock & RV3D_LOCKED)==0)
1193                                 viewlock= 0;
1194                         else if((viewlock & RV3D_BOXVIEW)==0)
1195                                 viewlock &= ~RV3D_BOXCLIP;
1196                         
1197                         for(; ar; ar= ar->prev) {
1198                                 if(ar->alignment==RGN_ALIGN_QSPLIT) {
1199                                         rv3d= ar->regiondata;
1200                                         rv3d->viewlock= viewlock;
1201                                 }
1202                         }
1203                         
1204                         if(rv3d->viewlock & RV3D_BOXVIEW)
1205                                 view3d_boxview_copy(sa, sa->regionbase.last);
1206                         
1207                         ED_area_tag_redraw(sa);
1208                 }
1209                 break;
1210         }
1211
1212         /* default for now */
1213         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, ob);
1214 }
1215
1216 static void view3d_panel_object(const bContext *C, Panel *pa)
1217 {
1218         uiBlock *block;
1219         Scene *scene= CTX_data_scene(C);
1220         Object *obedit= CTX_data_edit_object(C);
1221         View3D *v3d= CTX_wm_view3d(C);
1222         //uiBut *bt;
1223         Object *ob= OBACT;
1224         // TransformProperties *tfp; // UNUSED
1225         PointerRNA obptr;
1226         uiLayout *col, *row;
1227         float lim;
1228         
1229         if(ob==NULL) return;
1230
1231         /* make sure we got storage */
1232         /*
1233         if(v3d->properties_storage==NULL)
1234                 v3d->properties_storage= MEM_callocN(sizeof(TransformProperties), "TransformProperties");
1235         tfp= v3d->properties_storage;
1236         
1237 // XXX  uiSetButLock(object_is_libdata(ob), ERROR_LIBDATA_MESSAGE);
1238
1239         if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)) {
1240         }
1241         else {
1242                 if((ob->mode & OB_MODE_PARTICLE_EDIT)==0) {
1243                         uiBlockEndAlign(block);
1244                 }
1245         }
1246         */
1247
1248         lim= 10000.0f * MAX2(1.0f, v3d->grid);
1249
1250         block= uiLayoutGetBlock(pa->layout);
1251         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
1252
1253         col= uiLayoutColumn(pa->layout, 0);
1254         row= uiLayoutRow(col, 0);
1255         RNA_id_pointer_create(&ob->id, &obptr);
1256
1257         if(ob==obedit) {
1258                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(col, ob);
1259                 else if(ob->type==OB_MBALL) v3d_editmetaball_buts(col, ob);
1260                 else v3d_editvertex_buts(col, v3d, ob, lim);
1261         }
1262         else if(ob->mode & OB_MODE_POSE) {
1263                 v3d_posearmature_buts(col, ob);
1264         }
1265         else {
1266
1267                 v3d_transform_butsR(col, &obptr);
1268         }
1269 }
1270
1271 #if 0
1272 static void view3d_panel_preview(bContext *C, ARegion *ar, short cntrl) // VIEW3D_HANDLER_PREVIEW
1273 {
1274         uiBlock *block;
1275         View3D *v3d= sa->spacedata.first;
1276         int ofsx, ofsy;
1277         
1278         block= uiBeginBlock(C, ar, "view3d_panel_preview", UI_EMBOSS);
1279         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
1280         uiSetPanelHandler(VIEW3D_HANDLER_PREVIEW);  // for close and esc
1281         
1282         ofsx= -150+(sa->winx/2)/v3d->blockscale;
1283         ofsy= -100+(sa->winy/2)/v3d->blockscale;
1284         if(uiNewPanel(C, ar, block, "Preview", "View3d", ofsx, ofsy, 300, 200)==0) return;
1285
1286         uiBlockSetDrawExtraFunc(block, BIF_view3d_previewdraw);
1287         
1288         if(scene->recalc & SCE_PRV_CHANGED) {
1289                 scene->recalc &= ~SCE_PRV_CHANGED;
1290                 //printf("found recalc\n");
1291                 BIF_view3d_previewrender_free(sa->spacedata.first);
1292                 BIF_preview_changed(0);
1293         }
1294 }
1295 #endif
1296
1297 #if 0 // XXX not used
1298 static void delete_sketch_armature(bContext *C, void *arg1, void *arg2)
1299 {
1300         BIF_deleteSketch(C);
1301 }
1302
1303 static void convert_sketch_armature(bContext *C, void *arg1, void *arg2)
1304 {
1305         BIF_convertSketch(C);
1306 }
1307
1308 static void assign_template_sketch_armature(bContext *C, void *arg1, void *arg2)
1309 {
1310         int index = *(int*)arg1;
1311         BIF_setTemplate(C, index);
1312 }
1313
1314
1315 static int view3d_panel_bonesketch_spaces_poll(const bContext *C, PanelType *pt)
1316 {
1317         Object *obedit = CTX_data_edit_object(C);
1318
1319         /* replace with check call to sketching lib */
1320         return (obedit && obedit->type == OB_ARMATURE);
1321 }
1322 static void view3d_panel_bonesketch_spaces(const bContext *C, Panel *pa)
1323 {
1324         Scene *scene = CTX_data_scene(C);
1325         static int template_index;
1326         static char joint_label[128];
1327         uiBlock *block;
1328         uiBut *but;
1329         char *bone_name;
1330         int yco = 130;
1331         int nb_joints;
1332         static char subdiv_tooltip[4][64] = {
1333                 "Subdivide arcs based on a fixed number of bones",
1334                 "Subdivide arcs in bones of equal length",
1335                 "Subdivide arcs based on correlation",
1336                 "Retarget template to stroke"
1337                 };
1338
1339         
1340         block= uiLayoutAbsoluteBlock(pa->layout);
1341         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
1342
1343         uiBlockBeginAlign(block);
1344         
1345         /* use real flag instead of 1 */
1346         uiDefButBitC(block, TOG, BONE_SKETCHING, B_REDR, "Use Bone Sketching", 10, yco, 160, 20, &scene->toolsettings->bone_sketching, 0, 0, 0, 0, "Use sketching to create and edit bones, (Ctrl snaps to mesh volume)");
1347         uiDefButBitC(block, TOG, BONE_SKETCHING_ADJUST, B_REDR, "A", 170, yco, 20, 20, &scene->toolsettings->bone_sketching, 0, 0, 0, 0, "Adjust strokes by drawing near them");
1348         uiDefButBitC(block, TOG, BONE_SKETCHING_QUICK, B_REDR, "Q", 190, yco, 20, 20, &scene->toolsettings->bone_sketching, 0, 0, 0, 0, "Automatically convert and delete on stroke end");
1349         yco -= 20;
1350         
1351         but = uiDefBut(block, BUT, B_REDR, "Convert", 10,yco,100,20, 0, 0, 0, 0, 0, "Convert sketch to armature");
1352         uiButSetFunc(but, convert_sketch_armature, NULL, NULL);
1353
1354         but = uiDefBut(block, BUT, B_REDR, "Delete", 110,yco,100,20, 0, 0, 0, 0, 0, "Delete sketch");
1355         uiButSetFunc(but, delete_sketch_armature, NULL, NULL);
1356         yco -= 20;
1357         
1358         uiBlockEndAlign(block);
1359
1360         uiBlockBeginAlign(block);
1361         
1362         uiDefButC(block, MENU, B_REDR, "Subdivision Method%t|Length%x1|Adaptative%x2|Fixed%x0|Template%x3", 10,yco,60,19, &scene->toolsettings->bone_sketching_convert, 0, 0, 0, 0, subdiv_tooltip[(unsigned char)scene->toolsettings->bone_sketching_convert]);
1363
1364         switch(scene->toolsettings->bone_sketching_convert)
1365         {
1366         case SK_CONVERT_CUT_LENGTH:
1367                 uiDefButF(block, NUM, B_REDR,                                   "Lim:",         70, yco, 140, 19, &scene->toolsettings->skgen_length_limit,0.1,50.0, 10, 0,             "Maximum length of the subdivided bones");
1368                 yco -= 20;
1369                 break;
1370         case SK_CONVERT_CUT_ADAPTATIVE:
1371                 uiDefButF(block, NUM, B_REDR,                                   "Thres:",                       70, yco, 140, 19, &scene->toolsettings->skgen_correlation_limit,0.0, 1.0, 0.01, 0,      "Correlation threshold for subdivision");
1372                 yco -= 20;
1373                 break;
1374         default:
1375         case SK_CONVERT_CUT_FIXED:
1376                 uiDefButC(block, NUM, B_REDR,                                   "Num:",         70, yco, 140, 19, &scene->toolsettings->skgen_subdivision_number,1, 100, 1, 5,  "Number of subdivided bones");
1377                 yco -= 20;
1378                 break;
1379         case SK_CONVERT_RETARGET:
1380                 uiDefButC(block, ROW, B_NOP, "No",                      70,  yco, 40,19, &scene->toolsettings->skgen_retarget_roll, 0, 0, 0, 0,                                                                 "No special roll treatment");
1381                 uiDefButC(block, ROW, B_NOP, "View",            110,  yco, 50,19, &scene->toolsettings->skgen_retarget_roll, 0, SK_RETARGET_ROLL_VIEW, 0, 0,                            "Roll bones perpendicular to view");
1382                 uiDefButC(block, ROW, B_NOP, "Joint",           160, yco, 50,19, &scene->toolsettings->skgen_retarget_roll, 0, SK_RETARGET_ROLL_JOINT, 0, 0,                            "Roll bones relative to joint bend");
1383                 yco -= 30;
1384
1385                 uiBlockEndAlign(block);
1386
1387                 uiBlockBeginAlign(block);
1388                 /* button here to select what to do (copy or not), template, ...*/
1389
1390                 BIF_makeListTemplates(C);
1391                 template_index = BIF_currentTemplate(C);
1392                 
1393                 but = uiDefButI(block, MENU, B_REDR, BIF_listTemplates(C), 10,yco,200,19, &template_index, 0, 0, 0, 0, "Template");
1394                 uiButSetFunc(but, assign_template_sketch_armature, &template_index, NULL);
1395                 
1396                 yco -= 20;
1397                 
1398                 uiDefButF(block, NUM, B_NOP,                                                    "A:",                   10, yco, 66,19, &scene->toolsettings->skgen_retarget_angle_weight, 0, 10, 1, 0,         "Angle Weight");
1399                 uiDefButF(block, NUM, B_NOP,                                                    "L:",                   76, yco, 67,19, &scene->toolsettings->skgen_retarget_length_weight, 0, 10, 1, 0,                "Length Weight");
1400                 uiDefButF(block, NUM, B_NOP,                                                    "D:",           143,yco, 67,19, &scene->toolsettings->skgen_retarget_distance_weight, 0, 10, 1, 0,              "Distance Weight");
1401                 yco -= 20;
1402                 
1403                 uiDefBut(block, TEX,B_REDR,"S:",                                                        10,  yco, 90, 20, scene->toolsettings->skgen_side_string, 0.0, 8.0, 0, 0, "Text to replace &S with");
1404                 uiDefBut(block, TEX,B_REDR,"N:",                                                        100, yco, 90, 20, scene->toolsettings->skgen_num_string, 0.0, 8.0, 0, 0, "Text to replace &N with");
1405                 uiDefIconButBitC(block, TOG, SK_RETARGET_AUTONAME, B_NOP, ICON_AUTO,190,yco,20,20, &scene->toolsettings->skgen_retarget_options, 0, 0, 0, 0, "Use Auto Naming");        
1406                 yco -= 20;
1407
1408                 /* auto renaming magic */
1409                 uiBlockEndAlign(block);
1410                 
1411                 nb_joints = BIF_nbJointsTemplate(C);
1412
1413                 if (nb_joints == -1)
1414                 {
1415                         //XXX
1416                         //nb_joints = G.totvertsel;
1417                 }
1418                 
1419                 bone_name = BIF_nameBoneTemplate(C);
1420                 
1421                 BLI_snprintf(joint_label, 32, "%i joints: %s", nb_joints, bone_name);
1422                 
1423                 uiDefBut(block, LABEL, 1, joint_label,                                  10, yco, 200, 20, NULL, 0.0, 0.0, 0, 0, "");
1424                 yco -= 20;
1425                 break;
1426         }
1427
1428         uiBlockEndAlign(block);
1429 }
1430
1431 #endif // XXX not used
1432
1433 void view3d_buttons_register(ARegionType *art)
1434 {
1435         PanelType *pt;
1436
1437         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel object");
1438         strcpy(pt->idname, "VIEW3D_PT_object");
1439         strcpy(pt->label, "Transform");
1440         pt->draw= view3d_panel_object;
1441         BLI_addtail(&art->paneltypes, pt);
1442         
1443         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel gpencil");
1444         strcpy(pt->idname, "VIEW3D_PT_gpencil");
1445         strcpy(pt->label, "Grease Pencil");
1446         pt->draw= gpencil_panel_standard;
1447         BLI_addtail(&art->paneltypes, pt);
1448
1449         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel vgroup");
1450         strcpy(pt->idname, "VIEW3D_PT_vgroup");
1451         strcpy(pt->label, "Vertex Groups");
1452         pt->draw= view3d_panel_vgroup;
1453         pt->poll= view3d_panel_vgroup_poll;
1454         BLI_addtail(&art->paneltypes, pt);
1455
1456         // XXX view3d_panel_preview(C, ar, 0);
1457 }
1458
1459 static int view3d_properties(bContext *C, wmOperator *UNUSED(op))
1460 {
1461         ScrArea *sa= CTX_wm_area(C);
1462         ARegion *ar= view3d_has_buttons_region(sa);
1463         
1464         if(ar)
1465                 ED_region_toggle_hidden(C, ar);
1466
1467         return OPERATOR_FINISHED;
1468 }
1469
1470 void VIEW3D_OT_properties(wmOperatorType *ot)
1471 {
1472         ot->name= "Properties";
1473         ot->description= "Toggles the properties panel display";
1474         ot->idname= "VIEW3D_OT_properties";
1475         
1476         ot->exec= view3d_properties;
1477         ot->poll= ED_operator_view3d_active;
1478         
1479         /* flags */
1480         ot->flag= 0;
1481 }