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