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