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