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