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