added bone envelope to the editbone UI, also use more BLI_math functions for armature...
[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         uiItemR(col, &eboneptr, "envelope_distance", 0, "Envelope", ICON_NONE);
990 }
991
992 static void v3d_editmetaball_buts(uiLayout *layout, Object *ob)
993 {
994         PointerRNA mbptr, ptr;
995         MetaBall *mball= ob->data;
996 //      uiLayout *row;
997         uiLayout *col;
998         
999         if (!mball || !(mball->lastelem)) return;
1000         
1001         RNA_pointer_create(&mball->id, &RNA_MetaBall, mball, &mbptr);
1002         
1003 //      row= uiLayoutRow(layout, 0);
1004
1005         RNA_pointer_create(&mball->id, &RNA_MetaElement, mball->lastelem, &ptr);
1006         
1007         col= uiLayoutColumn(layout, 0);
1008         uiItemR(col, &ptr, "co", 0, "Location", ICON_NONE);
1009         
1010         uiItemR(col, &ptr, "radius", 0, "Radius", ICON_NONE);
1011         uiItemR(col, &ptr, "stiffness", 0, "Stiffness", ICON_NONE);
1012         
1013         uiItemR(col, &ptr, "type", 0, "Type", ICON_NONE);
1014         
1015         col= uiLayoutColumn(layout, 1);
1016         switch (RNA_enum_get(&ptr, "type")) {
1017                 case MB_BALL:
1018                         break;
1019                 case MB_CUBE:
1020                         uiItemL(col, "Size:", ICON_NONE);
1021                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1022                         uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NONE);
1023                         uiItemR(col, &ptr, "size_z", 0, "Z", ICON_NONE);
1024                         break;
1025                 case MB_TUBE:
1026                         uiItemL(col, "Size:", ICON_NONE);
1027                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1028                         break;
1029                 case MB_PLANE:
1030                         uiItemL(col, "Size:", ICON_NONE);
1031                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1032                         uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NONE);
1033                         break;
1034                 case MB_ELIPSOID:
1035                         uiItemL(col, "Size:", ICON_NONE);
1036                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1037                         uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NONE);
1038                         uiItemR(col, &ptr, "size_z", 0, "Z", ICON_NONE);
1039                         break;             
1040         }       
1041 }
1042
1043 /* test if 'ob' is a parent somewhere in par's parents */
1044 static int test_parent_loop(Object *par, Object *ob)
1045 {
1046         if(par == NULL) return 0;
1047         if(ob == par) return 1;
1048         return test_parent_loop(par->parent, ob);
1049 }
1050
1051 static void do_view3d_region_buttons(bContext *C, void *UNUSED(index), int event)
1052 {
1053         Main *bmain= CTX_data_main(C);
1054         Scene *scene= CTX_data_scene(C);
1055 //      Object *obedit= CTX_data_edit_object(C);
1056         View3D *v3d= CTX_wm_view3d(C);
1057 //      BoundBox *bb;
1058         Object *ob= OBACT;
1059         TransformProperties *tfp= v3d->properties_storage;
1060         
1061         switch(event) {
1062         
1063         case B_REDR:
1064                 ED_area_tag_redraw(CTX_wm_area(C));
1065                 return; /* no notifier! */
1066                 
1067         case B_OBJECTPANEL:
1068                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1069                 break;
1070
1071         
1072         case B_OBJECTPANELMEDIAN:
1073                 if(ob) {
1074                         v3d_editvertex_buts(NULL, v3d, ob, 1.0);
1075                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1076                 }
1077                 break;
1078                 
1079                 /* note; this case also used for parbone */
1080         case B_OBJECTPANELPARENT:
1081                 if(ob) {
1082                         if(ob->id.lib || test_parent_loop(ob->parent, ob) ) 
1083                                 ob->parent= NULL;
1084                         else {
1085                                 DAG_scene_sort(bmain, scene);
1086                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1087                         }
1088                 }
1089                 break;
1090                 
1091
1092         case B_ARMATUREPANEL3:  // rotate button on channel
1093                 {
1094                         bPoseChannel *pchan;
1095                         float eul[3];
1096                         
1097                         pchan= get_active_posechannel(ob);
1098                         if (!pchan) return;
1099                         
1100                         /* make a copy to eul[3], to allow TAB on buttons to work */
1101                         eul[0]= DEG2RADF(tfp->ob_eul[0]);
1102                         eul[1]= DEG2RADF(tfp->ob_eul[1]);
1103                         eul[2]= DEG2RADF(tfp->ob_eul[2]);
1104                         
1105                         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1106                                 float quat[4];
1107                                 /* convert to axis-angle, passing through quats  */
1108                                 eul_to_quat( quat,eul);
1109                                 quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,quat);
1110                         }
1111                         else if (pchan->rotmode == ROT_MODE_QUAT)
1112                                 eul_to_quat( pchan->quat,eul);
1113                         else
1114                                 copy_v3_v3(pchan->eul, eul);
1115                 }
1116                 /* no break, pass on */
1117         case B_ARMATUREPANEL2:
1118                 {
1119                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1120                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1121                 }
1122                 break;
1123         case B_TRANSFORMSPACEADD:
1124         {
1125                 char names[sizeof(((TransformOrientation *)NULL)->name)]= "";
1126                 BIF_createTransformOrientation(C, NULL, names, 1, 0);
1127                 break;
1128         }
1129         case B_TRANSFORMSPACECLEAR:
1130                 BIF_clearTransformOrientation(C);
1131                 break;
1132                 
1133 #if 0 // XXX
1134         case B_WEIGHT0_0:
1135                 wpaint->weight = 0.0f;
1136                 break;
1137                 
1138         case B_WEIGHT1_4:
1139                 wpaint->weight = 0.25f;
1140                 break;
1141         case B_WEIGHT1_2:
1142                 wpaint->weight = 0.5f;
1143                 break;
1144         case B_WEIGHT3_4:
1145                 wpaint->weight = 0.75f;
1146                 break;
1147         case B_WEIGHT1_0:
1148                 wpaint->weight = 1.0f;
1149                 break;
1150                 
1151         case B_OPA1_8:
1152                 wpaint->a = 0.125f;
1153                 break;
1154         case B_OPA1_4:
1155                 wpaint->a = 0.25f;
1156                 break;
1157         case B_OPA1_2:
1158                 wpaint->a = 0.5f;
1159                 break;
1160         case B_OPA3_4:
1161                 wpaint->a = 0.75f;
1162                 break;
1163         case B_OPA1_0:
1164                 wpaint->a = 1.0f;
1165                 break;
1166 #endif
1167         case B_CLR_WPAINT:
1168 //              if(!multires_level1_test()) {
1169                 {
1170                         bDeformGroup *defGroup = BLI_findlink(&ob->defbase, ob->actdef-1);
1171                         if(defGroup) {
1172                                 Mesh *me= ob->data;
1173                                 int a;
1174                                 for(a=0; a<me->totvert; a++)
1175                                         ED_vgroup_vert_remove (ob, defGroup, a);
1176                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1177                         }
1178                 }
1179                 break;
1180         case B_RV3D_LOCKED:
1181         case B_RV3D_BOXVIEW:
1182         case B_RV3D_BOXCLIP:
1183                 {
1184                         ScrArea *sa= CTX_wm_area(C);
1185                         ARegion *ar= sa->regionbase.last;
1186                         RegionView3D *rv3d;
1187                         short viewlock;
1188                         
1189                         ar= ar->prev;
1190                         rv3d= ar->regiondata;
1191                         viewlock= rv3d->viewlock;
1192                         
1193                         if((viewlock & RV3D_LOCKED)==0)
1194                                 viewlock= 0;
1195                         else if((viewlock & RV3D_BOXVIEW)==0)
1196                                 viewlock &= ~RV3D_BOXCLIP;
1197                         
1198                         for(; ar; ar= ar->prev) {
1199                                 if(ar->alignment==RGN_ALIGN_QSPLIT) {
1200                                         rv3d= ar->regiondata;
1201                                         rv3d->viewlock= viewlock;
1202                                 }
1203                         }
1204                         
1205                         if(rv3d->viewlock & RV3D_BOXVIEW)
1206                                 view3d_boxview_copy(sa, sa->regionbase.last);
1207                         
1208                         ED_area_tag_redraw(sa);
1209                 }
1210                 break;
1211         }
1212
1213         /* default for now */
1214         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, ob);
1215 }
1216
1217 static void view3d_panel_object(const bContext *C, Panel *pa)
1218 {
1219         uiBlock *block;
1220         Scene *scene= CTX_data_scene(C);
1221         Object *obedit= CTX_data_edit_object(C);
1222         View3D *v3d= CTX_wm_view3d(C);
1223         //uiBut *bt;
1224         Object *ob= OBACT;
1225         // TransformProperties *tfp; // UNUSED
1226         PointerRNA obptr;
1227         uiLayout *col, *row;
1228         float lim;
1229         
1230         if(ob==NULL) return;
1231
1232         /* make sure we got storage */
1233         /*
1234         if(v3d->properties_storage==NULL)
1235                 v3d->properties_storage= MEM_callocN(sizeof(TransformProperties), "TransformProperties");
1236         tfp= v3d->properties_storage;
1237         
1238 // XXX  uiSetButLock(object_is_libdata(ob), ERROR_LIBDATA_MESSAGE);
1239
1240         if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)) {
1241         }
1242         else {
1243                 if((ob->mode & OB_MODE_PARTICLE_EDIT)==0) {
1244                         uiBlockEndAlign(block);
1245                 }
1246         }
1247         */
1248
1249         lim= 10000.0f * MAX2(1.0f, v3d->grid);
1250
1251         block= uiLayoutGetBlock(pa->layout);
1252         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
1253
1254         col= uiLayoutColumn(pa->layout, 0);
1255         row= uiLayoutRow(col, 0);
1256         RNA_id_pointer_create(&ob->id, &obptr);
1257
1258         if(ob==obedit) {
1259                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(col, ob);
1260                 else if(ob->type==OB_MBALL) v3d_editmetaball_buts(col, ob);
1261                 else v3d_editvertex_buts(col, v3d, ob, lim);
1262         }
1263         else if(ob->mode & OB_MODE_POSE) {
1264                 v3d_posearmature_buts(col, ob);
1265         }
1266         else {
1267
1268                 v3d_transform_butsR(col, &obptr);
1269         }
1270 }
1271
1272 #if 0
1273 static void view3d_panel_preview(bContext *C, ARegion *ar, short cntrl) // VIEW3D_HANDLER_PREVIEW
1274 {
1275         uiBlock *block;
1276         View3D *v3d= sa->spacedata.first;
1277         int ofsx, ofsy;
1278         
1279         block= uiBeginBlock(C, ar, "view3d_panel_preview", UI_EMBOSS);
1280         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
1281         uiSetPanelHandler(VIEW3D_HANDLER_PREVIEW);  // for close and esc
1282         
1283         ofsx= -150+(sa->winx/2)/v3d->blockscale;
1284         ofsy= -100+(sa->winy/2)/v3d->blockscale;
1285         if(uiNewPanel(C, ar, block, "Preview", "View3d", ofsx, ofsy, 300, 200)==0) return;
1286
1287         uiBlockSetDrawExtraFunc(block, BIF_view3d_previewdraw);
1288         
1289         if(scene->recalc & SCE_PRV_CHANGED) {
1290                 scene->recalc &= ~SCE_PRV_CHANGED;
1291                 //printf("found recalc\n");
1292                 BIF_view3d_previewrender_free(sa->spacedata.first);
1293                 BIF_preview_changed(0);
1294         }
1295 }
1296 #endif
1297
1298 #if 0 // XXX not used
1299 static void delete_sketch_armature(bContext *C, void *arg1, void *arg2)
1300 {
1301         BIF_deleteSketch(C);
1302 }
1303
1304 static void convert_sketch_armature(bContext *C, void *arg1, void *arg2)
1305 {
1306         BIF_convertSketch(C);
1307 }
1308
1309 static void assign_template_sketch_armature(bContext *C, void *arg1, void *arg2)
1310 {
1311         int index = *(int*)arg1;
1312         BIF_setTemplate(C, index);
1313 }
1314
1315
1316 static int view3d_panel_bonesketch_spaces_poll(const bContext *C, PanelType *pt)
1317 {
1318         Object *obedit = CTX_data_edit_object(C);
1319
1320         /* replace with check call to sketching lib */
1321         return (obedit && obedit->type == OB_ARMATURE);
1322 }
1323 static void view3d_panel_bonesketch_spaces(const bContext *C, Panel *pa)
1324 {
1325         Scene *scene = CTX_data_scene(C);
1326         static int template_index;
1327         static char joint_label[128];
1328         uiBlock *block;
1329         uiBut *but;
1330         char *bone_name;
1331         int yco = 130;
1332         int nb_joints;
1333         static char subdiv_tooltip[4][64] = {
1334                 "Subdivide arcs based on a fixed number of bones",
1335                 "Subdivide arcs in bones of equal length",
1336                 "Subdivide arcs based on correlation",
1337                 "Retarget template to stroke"
1338                 };
1339
1340         
1341         block= uiLayoutAbsoluteBlock(pa->layout);
1342         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
1343
1344         uiBlockBeginAlign(block);
1345         
1346         /* use real flag instead of 1 */
1347         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)");
1348         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");
1349         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");
1350         yco -= 20;
1351         
1352         but = uiDefBut(block, BUT, B_REDR, "Convert", 10,yco,100,20, 0, 0, 0, 0, 0, "Convert sketch to armature");
1353         uiButSetFunc(but, convert_sketch_armature, NULL, NULL);
1354
1355         but = uiDefBut(block, BUT, B_REDR, "Delete", 110,yco,100,20, 0, 0, 0, 0, 0, "Delete sketch");
1356         uiButSetFunc(but, delete_sketch_armature, NULL, NULL);
1357         yco -= 20;
1358         
1359         uiBlockEndAlign(block);
1360
1361         uiBlockBeginAlign(block);
1362         
1363         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]);
1364
1365         switch(scene->toolsettings->bone_sketching_convert)
1366         {
1367         case SK_CONVERT_CUT_LENGTH:
1368                 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");
1369                 yco -= 20;
1370                 break;
1371         case SK_CONVERT_CUT_ADAPTATIVE:
1372                 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");
1373                 yco -= 20;
1374                 break;
1375         default:
1376         case SK_CONVERT_CUT_FIXED:
1377                 uiDefButC(block, NUM, B_REDR,                                   "Num:",         70, yco, 140, 19, &scene->toolsettings->skgen_subdivision_number,1, 100, 1, 5,  "Number of subdivided bones");
1378                 yco -= 20;
1379                 break;
1380         case SK_CONVERT_RETARGET:
1381                 uiDefButC(block, ROW, B_NOP, "No",                      70,  yco, 40,19, &scene->toolsettings->skgen_retarget_roll, 0, 0, 0, 0,                                                                 "No special roll treatment");
1382                 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");
1383                 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");
1384                 yco -= 30;
1385
1386                 uiBlockEndAlign(block);
1387
1388                 uiBlockBeginAlign(block);
1389                 /* button here to select what to do (copy or not), template, ...*/
1390
1391                 BIF_makeListTemplates(C);
1392                 template_index = BIF_currentTemplate(C);
1393                 
1394                 but = uiDefButI(block, MENU, B_REDR, BIF_listTemplates(C), 10,yco,200,19, &template_index, 0, 0, 0, 0, "Template");
1395                 uiButSetFunc(but, assign_template_sketch_armature, &template_index, NULL);
1396                 
1397                 yco -= 20;
1398                 
1399                 uiDefButF(block, NUM, B_NOP,                                                    "A:",                   10, yco, 66,19, &scene->toolsettings->skgen_retarget_angle_weight, 0, 10, 1, 0,         "Angle Weight");
1400                 uiDefButF(block, NUM, B_NOP,                                                    "L:",                   76, yco, 67,19, &scene->toolsettings->skgen_retarget_length_weight, 0, 10, 1, 0,                "Length Weight");
1401                 uiDefButF(block, NUM, B_NOP,                                                    "D:",           143,yco, 67,19, &scene->toolsettings->skgen_retarget_distance_weight, 0, 10, 1, 0,              "Distance Weight");
1402                 yco -= 20;
1403                 
1404                 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");
1405                 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");
1406                 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");        
1407                 yco -= 20;
1408
1409                 /* auto renaming magic */
1410                 uiBlockEndAlign(block);
1411                 
1412                 nb_joints = BIF_nbJointsTemplate(C);
1413
1414                 if (nb_joints == -1)
1415                 {
1416                         //XXX
1417                         //nb_joints = G.totvertsel;
1418                 }
1419                 
1420                 bone_name = BIF_nameBoneTemplate(C);
1421                 
1422                 BLI_snprintf(joint_label, 32, "%i joints: %s", nb_joints, bone_name);
1423                 
1424                 uiDefBut(block, LABEL, 1, joint_label,                                  10, yco, 200, 20, NULL, 0.0, 0.0, 0, 0, "");
1425                 yco -= 20;
1426                 break;
1427         }
1428
1429         uiBlockEndAlign(block);
1430 }
1431
1432 #endif // XXX not used
1433
1434 void view3d_buttons_register(ARegionType *art)
1435 {
1436         PanelType *pt;
1437
1438         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel object");
1439         strcpy(pt->idname, "VIEW3D_PT_object");
1440         strcpy(pt->label, "Transform");
1441         pt->draw= view3d_panel_object;
1442         BLI_addtail(&art->paneltypes, pt);
1443         
1444         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel gpencil");
1445         strcpy(pt->idname, "VIEW3D_PT_gpencil");
1446         strcpy(pt->label, "Grease Pencil");
1447         pt->draw= gpencil_panel_standard;
1448         BLI_addtail(&art->paneltypes, pt);
1449
1450         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel vgroup");
1451         strcpy(pt->idname, "VIEW3D_PT_vgroup");
1452         strcpy(pt->label, "Vertex Groups");
1453         pt->draw= view3d_panel_vgroup;
1454         pt->poll= view3d_panel_vgroup_poll;
1455         BLI_addtail(&art->paneltypes, pt);
1456
1457         // XXX view3d_panel_preview(C, ar, 0);
1458 }
1459
1460 static int view3d_properties(bContext *C, wmOperator *UNUSED(op))
1461 {
1462         ScrArea *sa= CTX_wm_area(C);
1463         ARegion *ar= view3d_has_buttons_region(sa);
1464         
1465         if(ar)
1466                 ED_region_toggle_hidden(C, ar);
1467
1468         return OPERATOR_FINISHED;
1469 }
1470
1471 void VIEW3D_OT_properties(wmOperatorType *ot)
1472 {
1473         ot->name= "Properties";
1474         ot->description= "Toggles the properties panel display";
1475         ot->idname= "VIEW3D_OT_properties";
1476         
1477         ot->exec= view3d_properties;
1478         ot->poll= ED_operator_view3d_active;
1479         
1480         /* flags */
1481         ot->flag= 0;
1482 }