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