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