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