merge with trunk at r31523
[blender.git] / source / blender / editors / space_view3d / view3d_header.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2004-2008 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 <stdlib.h>
32
33 #include "DNA_scene_types.h"
34 #include "DNA_object_types.h"
35
36 #include "RNA_access.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BKE_context.h"
41 #include "BKE_depsgraph.h"
42 #include "BKE_effect.h"
43 #include "BKE_main.h"
44 #include "BKE_mesh.h"
45 #include "BKE_modifier.h"
46 #include "BKE_paint.h"
47 #include "BKE_screen.h"
48 #include "BKE_tessmesh.h"
49
50 #include "ED_mesh.h"
51 #include "ED_util.h"
52 #include "ED_screen.h"
53 #include "ED_transform.h"
54 #include "ED_types.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "RNA_define.h"
60 #include "RNA_enum_types.h"
61
62
63 #include "BLI_math.h"
64 #include "BLI_blenlib.h"
65 #include "BLI_editVert.h"
66
67 #include "UI_interface.h"
68 #include "UI_resources.h"
69
70 #include "view3d_intern.h"
71
72
73 /* View3d->modeselect 
74  * This is a bit of a dodgy hack to enable a 'mode' menu with icons+labels
75  * rather than those buttons.
76  * I know the implementation's not good - it's an experiment to see if this
77  * approach would work well
78  *
79  * This can be cleaned when I make some new 'mode' icons.
80  */
81
82 #define TEST_EDITMESH   if(obedit==0) return; \
83                                                 if( (v3d->lay & obedit->lay)==0 ) return;
84
85 /* XXX port over */     
86 extern void borderselect();
87
88 /* view3d handler codes */
89 #define VIEW3D_HANDLER_BACKGROUND       1
90 #define VIEW3D_HANDLER_PROPERTIES       2
91 #define VIEW3D_HANDLER_OBJECT           3
92 #define VIEW3D_HANDLER_PREVIEW          4
93 #define VIEW3D_HANDLER_MULTIRES         5
94 #define VIEW3D_HANDLER_TRANSFORM        6
95 #define VIEW3D_HANDLER_GREASEPENCIL 7
96 #define VIEW3D_HANDLER_BONESKETCH       8
97
98 /* end XXX ************* */
99
100 static void do_view3d_header_buttons(bContext *C, void *arg, int event);
101
102 #define B_SCENELOCK 101
103 #define B_FULL          102
104 #define B_HOME          103
105 #define B_VIEWBUT       104
106 #define B_PERSP         105
107 #define B_MODESELECT 108
108 #define B_SEL_VERT      110
109 #define B_SEL_EDGE      111
110 #define B_SEL_FACE      112
111 #define B_MAN_TRANS     116
112 #define B_MAN_ROT       117
113 #define B_MAN_SCALE     118
114 #define B_NDOF          119     
115 #define B_MAN_MODE      120
116 #define B_REDR          122
117 #define B_NOP           123
118
119 // XXX quickly ported across
120 static void handle_view3d_lock(bContext *C) 
121 {
122         Main *bmain= CTX_data_main(C);
123         Scene *scene= CTX_data_scene(C);
124         ScrArea *sa= CTX_wm_area(C);
125         View3D *v3d= CTX_wm_view3d(C);
126         
127         if (v3d != NULL && sa != NULL) {
128                 if(v3d->localvd==NULL && v3d->scenelock && sa->spacetype==SPACE_VIEW3D) {
129                         /* copy to scene */
130                         scene->lay= v3d->lay;
131                         scene->layact= v3d->layact;
132                         scene->camera= v3d->camera;
133
134                         /* not through notifiery, listener don't have context
135                            and non-open screens or spaces need to be updated too */
136                         BKE_screen_view3d_main_sync(&bmain->screen, scene);
137                         
138                         /* notifiers for scene update */
139                         WM_event_add_notifier(C, NC_SCENE|ND_LAYER, scene);
140                 }
141         }
142 }
143
144 static int layers_exec(bContext *C, wmOperator *op)
145 {
146         Main *bmain= CTX_data_main(C);
147         Scene *scene= CTX_data_scene(C);
148         ScrArea *sa= CTX_wm_area(C);
149         View3D *v3d= sa->spacedata.first;
150         int nr= RNA_int_get(op->ptr, "nr");
151         int toggle= RNA_boolean_get(op->ptr, "toggle");
152         
153         if(nr < 0)
154                 return OPERATOR_CANCELLED;
155
156         if(nr == 0) {
157                 /* all layers */
158                 if(!v3d->layact)
159                         v3d->layact= 1;
160
161                 if (toggle && v3d->lay == ((1<<20)-1)) {
162                         /* return to active layer only */
163                         v3d->lay = v3d->layact;
164                 } else {
165                         v3d->lay |= (1<<20)-1;
166                 }               
167         }
168         else {
169                 int bit;
170                 nr--;
171
172                 if(RNA_boolean_get(op->ptr, "extend")) {
173                         if(toggle && v3d->lay & (1<<nr) && (v3d->lay & ~(1<<nr)))
174                                 v3d->lay &= ~(1<<nr);
175                         else
176                                 v3d->lay |= (1<<nr);
177                 } else {
178                         v3d->lay = (1<<nr);
179
180                         /* sanity check - when in editmode disallow switching the editmode layer off since its confusing
181                          * an alternative would be to always draw the editmode object. */
182                         if(scene->obedit && (scene->obedit->lay & v3d->lay)==0) {
183                                 for(bit=0; bit<32; bit++) {
184                                         if(scene->obedit->lay & (1<<bit)) {
185                                                 v3d->lay |= 1<<bit;
186                                                 break;
187                                         }
188                                 }
189                         }
190                 }
191                 
192                 /* set active layer, ensure to always have one */
193                 if(v3d->lay & (1<<nr))
194                    v3d->layact= 1<<nr;
195                 else if((v3d->lay & v3d->layact)==0) {
196                         for(bit=0; bit<32; bit++) {
197                                 if(v3d->lay & (1<<bit)) {
198                                         v3d->layact= 1<<bit;
199                                         break;
200                                 }
201                         }
202                 }
203         }
204         
205         if(v3d->scenelock) handle_view3d_lock(C);
206         
207         /* new layers might need unflushed events events */
208         DAG_scene_update_flags(bmain, scene, v3d->lay); /* tags all that moves and flushes */
209
210         ED_area_tag_redraw(sa);
211         
212         return OPERATOR_FINISHED;
213 }
214
215 /* applies shift and alt, lazy coding or ok? :) */
216 /* the local per-keymap-entry keymap will solve it */
217 static int layers_invoke(bContext *C, wmOperator *op, wmEvent *event)
218 {
219         if(event->ctrl || event->oskey)
220                 return OPERATOR_PASS_THROUGH;
221         
222         if(event->shift)
223                 RNA_boolean_set(op->ptr, "extend", 1);
224         
225         if(event->alt) {
226                 int nr= RNA_int_get(op->ptr, "nr") + 10;
227                 RNA_int_set(op->ptr, "nr", nr);
228         }
229         layers_exec(C, op);
230         
231         return OPERATOR_FINISHED;
232 }
233
234 int layers_poll(bContext *C)
235 {
236         return (ED_operator_view3d_active(C) && CTX_wm_view3d(C)->localvd==NULL);
237 }
238
239 void VIEW3D_OT_layers(wmOperatorType *ot)
240 {
241         /* identifiers */
242         ot->name= "Layers";
243         ot->description= "Toggle layer(s) visibility";
244         ot->idname= "VIEW3D_OT_layers";
245         
246         /* api callbacks */
247         ot->invoke= layers_invoke;
248         ot->exec= layers_exec;
249         ot->poll= layers_poll;
250         
251         /* flags */
252         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
253         
254         RNA_def_int(ot->srna, "nr", 1, 0, 20, "Number", "The layer number to set, zero for all layers", 0, 20);
255         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Add this layer to the current view layers");
256         RNA_def_boolean(ot->srna, "toggle", 1, "Toggle", "Toggle the layer");
257 }
258
259 static char *view3d_modeselect_pup(Scene *scene)
260 {
261         Object *ob= OBACT;
262         static char string[1024];
263         static char formatstr[] = "|%s %%x%d %%i%d";
264         char *str = string;
265
266         str += sprintf(str, "Mode: %%t");
267         
268         str += sprintf(str, formatstr, "Object Mode", OB_MODE_OBJECT, ICON_OBJECT_DATA);
269         
270         if(ob==NULL) return string;
271         
272         /* if active object is editable */
273         if ( ((ob->type == OB_MESH)
274                 || (ob->type == OB_CURVE) || (ob->type == OB_SURF) || (ob->type == OB_FONT)
275                 || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) {
276                 
277                 str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT, ICON_EDITMODE_HLT);
278         }
279         else if (ob->type == OB_ARMATURE) {
280                 if (ob->mode & OB_MODE_POSE)
281                         str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT|OB_MODE_POSE, ICON_EDITMODE_HLT);
282                 else
283                         str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT, ICON_EDITMODE_HLT);
284         }
285
286         if (ob->type == OB_MESH) {
287
288                 str += sprintf(str, formatstr, "Sculpt Mode", OB_MODE_SCULPT, ICON_SCULPTMODE_HLT);
289                 str += sprintf(str, formatstr, "Vertex Paint", OB_MODE_VERTEX_PAINT, ICON_VPAINT_HLT);
290                 str += sprintf(str, formatstr, "Texture Paint", OB_MODE_TEXTURE_PAINT, ICON_TPAINT_HLT);
291                 str += sprintf(str, formatstr, "Weight Paint", OB_MODE_WEIGHT_PAINT, ICON_WPAINT_HLT);
292         }
293
294         
295         /* if active object is an armature */
296         if (ob->type==OB_ARMATURE) {
297                 str += sprintf(str, formatstr, "Pose Mode", OB_MODE_POSE, ICON_POSE_HLT);
298         }
299
300         if (ob->particlesystem.first || modifiers_findByType(ob, eModifierType_Cloth) || modifiers_findByType(ob, eModifierType_Softbody)) {
301                 str += sprintf(str, formatstr, "Particle Mode", OB_MODE_PARTICLE_EDIT, ICON_PARTICLEMODE);
302         }
303
304         return (string);
305 }
306
307
308 static void do_view3d_header_buttons(bContext *C, void *arg, int event)
309 {
310         wmWindow *win= CTX_wm_window(C);
311         ToolSettings *ts= CTX_data_tool_settings(C);
312         ScrArea *sa= CTX_wm_area(C);
313         View3D *v3d= sa->spacedata.first;
314         Object *obedit = CTX_data_edit_object(C);
315         BMEditMesh *em= NULL;
316         int ctrl= win->eventstate->ctrl, shift= win->eventstate->shift;
317         PointerRNA props_ptr;
318         
319         if(obedit && obedit->type==OB_MESH) {
320                 em= ((Mesh *)obedit->data)->edit_btmesh;
321         }
322         /* watch it: if sa->win does not exist, check that when calling direct drawing routines */
323
324         switch(event) {
325         case B_REDR:
326                 ED_area_tag_redraw(sa);
327                 break;
328                 
329         case B_MODESELECT:
330                 WM_operator_properties_create(&props_ptr, "OBJECT_OT_mode_set");
331                 RNA_enum_set(&props_ptr, "mode", v3d->modeselect);
332                 WM_operator_name_call(C, "OBJECT_OT_mode_set", WM_OP_EXEC_REGION_WIN, &props_ptr);
333                 WM_operator_properties_free(&props_ptr);
334                 break;          
335                 
336         case B_SEL_VERT:
337                 if(em) {
338                         if(shift==0 || em->selectmode==0)
339                                 em->selectmode= SCE_SELECT_VERTEX;
340                         ts->selectmode= em->selectmode;
341                         EDBM_selectmode_set(em);
342                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
343                         ED_undo_push(C, "Selectmode Set: Vertex");
344                 }
345                 break;
346         case B_SEL_EDGE:
347                 if(em) {
348                         if(shift==0 || em->selectmode==0){
349                                 if( (em->selectmode ^ SCE_SELECT_EDGE) == SCE_SELECT_VERTEX){
350                                         if(ctrl) EDBM_convertsel(em, SCE_SELECT_VERTEX,SCE_SELECT_EDGE); 
351                                 }
352                                 em->selectmode = SCE_SELECT_EDGE;
353                         }
354                         ts->selectmode= em->selectmode;
355                         EDBM_selectmode_set(em);
356                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
357                         ED_undo_push(C, "Selectmode Set: Edge");
358                 }
359                 break;
360         case B_SEL_FACE:
361                 if(em) {
362                         if( shift==0 || em->selectmode==0){
363                                 if( ((ts->selectmode ^ SCE_SELECT_FACE) == SCE_SELECT_VERTEX) || ((ts->selectmode ^ SCE_SELECT_FACE) == SCE_SELECT_EDGE)){
364                                         if(ctrl) EDBM_convertsel(em, (ts->selectmode ^ SCE_SELECT_FACE),SCE_SELECT_FACE);
365                                 }
366                                 em->selectmode = SCE_SELECT_FACE;
367                         }
368                         ts->selectmode= em->selectmode;
369                         EDBM_selectmode_set(em);
370                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
371                         ED_undo_push(C, "Selectmode Set: Face");
372                 }
373                 break;  
374
375         case B_MAN_TRANS:
376                 if( shift==0 || v3d->twtype==0) {
377                         v3d->twtype= V3D_MANIP_TRANSLATE;
378                 }
379                 ED_area_tag_redraw(sa);
380                 break;
381         case B_MAN_ROT:
382                 if( shift==0 || v3d->twtype==0) {
383                         v3d->twtype= V3D_MANIP_ROTATE;
384                 }
385                 ED_area_tag_redraw(sa);
386                 break;
387         case B_MAN_SCALE:
388                 if( shift==0 || v3d->twtype==0) {
389                         v3d->twtype= V3D_MANIP_SCALE;
390                 }
391                 ED_area_tag_redraw(sa);
392                 break;
393         case B_NDOF:
394                 ED_area_tag_redraw(sa);
395                 break;
396         case B_MAN_MODE:
397                 ED_area_tag_redraw(sa);
398                 break;
399         default:
400                 break;
401         }
402 }
403
404 /* Returns the icon associated with an object mode */
405 static int object_mode_icon(int mode)
406 {
407         EnumPropertyItem *item = object_mode_items;
408         
409         while(item->name != NULL) {
410                 if(item->value == mode)
411                         return item->icon;
412                 ++item;
413         }
414
415         return ICON_OBJECT_DATAMODE;
416 }
417
418 void uiTemplateHeader3D(uiLayout *layout, struct bContext *C)
419 {
420         bScreen *screen= CTX_wm_screen(C);
421         ScrArea *sa= CTX_wm_area(C);
422         View3D *v3d= sa->spacedata.first;
423         Scene *scene= CTX_data_scene(C);
424         ToolSettings *ts= CTX_data_tool_settings(C);
425         PointerRNA v3dptr, toolsptr, sceneptr;
426         Object *ob= OBACT;
427         Object *obedit = CTX_data_edit_object(C);
428         uiBlock *block;
429         uiLayout *row;
430         
431         RNA_pointer_create(&screen->id, &RNA_SpaceView3D, v3d, &v3dptr);        
432         RNA_pointer_create(&scene->id, &RNA_ToolSettings, ts, &toolsptr);
433         RNA_pointer_create(&scene->id, &RNA_Scene, scene, &sceneptr);
434
435         block= uiLayoutGetBlock(layout);
436         uiBlockSetHandleFunc(block, do_view3d_header_buttons, NULL);
437
438         /* other buttons: */
439         uiBlockSetEmboss(block, UI_EMBOSS);
440         
441         /* mode */
442         if(ob) {
443                 /*sanity point checkpoint, put here to avoid seeding
444                   this same code in 10 different other places.*/
445                 if (!ob->mode)
446                         ob->mode = OB_MODE_OBJECT;
447
448                 v3d->modeselect = ob->mode;
449         } else {
450                 v3d->modeselect = OB_MODE_OBJECT;
451         }
452
453         v3d->flag &= ~V3D_MODE;
454         
455         /* not sure what the v3d->flag is useful for now... modeselect is confusing */
456         if(obedit) v3d->flag |= V3D_EDITMODE;
457         if(ob && (ob->mode & OB_MODE_POSE)) v3d->flag |= V3D_POSEMODE;
458         if(ob && (ob->mode & OB_MODE_VERTEX_PAINT)) v3d->flag |= V3D_VERTEXPAINT;
459         if(ob && (ob->mode & OB_MODE_WEIGHT_PAINT)) v3d->flag |= V3D_WEIGHTPAINT;
460         if(ob && (ob->mode & OB_MODE_TEXTURE_PAINT)) v3d->flag |= V3D_TEXTUREPAINT;
461         if(paint_facesel_test(ob)) v3d->flag |= V3D_FACESELECT;
462
463         uiBlockBeginAlign(block);
464         uiDefIconTextButS(block, MENU, B_MODESELECT, object_mode_icon(v3d->modeselect), view3d_modeselect_pup(scene) , 
465                           0,0,126,20, &(v3d->modeselect), 0, 0, 0, 0, "Mode");
466         uiBlockEndAlign(block);
467         
468         /* Draw type */
469         uiItemR(layout, &v3dptr, "viewport_shade", UI_ITEM_R_ICON_ONLY, "", 0);
470
471         if (obedit==NULL && ((ob && ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)))) {
472                 /* Manipulators aren't used in weight paint mode */
473                 
474                 PointerRNA meshptr;
475
476                 RNA_pointer_create(&ob->id, &RNA_Mesh, ob->data, &meshptr);
477                 uiItemR(layout, &meshptr, "use_paint_mask", UI_ITEM_R_ICON_ONLY, "", 0);
478         } else {
479                 char *str_menu;
480
481                 row= uiLayoutRow(layout, 1);
482                 uiItemR(row, &v3dptr, "pivot_point", UI_ITEM_R_ICON_ONLY, "", 0);
483                 uiItemR(row, &v3dptr, "use_pivot_point_align", UI_ITEM_R_ICON_ONLY, "", 0);
484
485                 /* NDOF */
486                 /* Not implemented yet
487                  if (G.ndofdevice ==0 ) {
488                         uiDefIconTextButC(block, ICONTEXTROW,B_NDOF, ICON_NDOF_TURN, ndof_pup(), 0,0,XIC+10,YIC, &(v3d->ndofmode), 0, 3.0, 0, 0, "Ndof mode");
489                 
490                         uiDefIconButC(block, TOG, B_NDOF,  ICON_NDOF_DOM,
491                                           0,0,XIC,YIC,
492                                           &v3d->ndoffilter, 0, 1, 0, 0, "dominant axis");       
493                 }
494                  */
495
496                 /* Transform widget / manipulators */
497                 row= uiLayoutRow(layout, 1);
498                 uiItemR(row, &v3dptr, "show_manipulator", UI_ITEM_R_ICON_ONLY, "", 0);
499                 block= uiLayoutGetBlock(row);
500                 
501                 if(v3d->twflag & V3D_USE_MANIPULATOR) {
502                         uiDefIconButBitS(block, TOG, V3D_MANIP_TRANSLATE, B_MAN_TRANS, ICON_MAN_TRANS, 0,0,XIC,YIC, &v3d->twtype, 1.0, 0.0, 0, 0, "Translate manipulator mode");
503                         uiDefIconButBitS(block, TOG, V3D_MANIP_ROTATE, B_MAN_ROT, ICON_MAN_ROT, 0,0,XIC,YIC, &v3d->twtype, 1.0, 0.0, 0, 0, "Rotate manipulator mode");
504                         uiDefIconButBitS(block, TOG, V3D_MANIP_SCALE, B_MAN_SCALE, ICON_MAN_SCALE, 0,0,XIC,YIC, &v3d->twtype, 1.0, 0.0, 0, 0, "Scale manipulator mode");
505                 }
506                         
507                 if (v3d->twmode > (BIF_countTransformOrientation(C) - 1) + V3D_MANIP_CUSTOM) {
508                         v3d->twmode = 0;
509                 }
510                         
511                 str_menu = BIF_menustringTransformOrientation(C, "Orientation");
512                 uiDefButS(block, MENU, B_MAN_MODE, str_menu,0,0,70,YIC, &v3d->twmode, 0, 0, 0, 0, "Transform Orientation");
513                 MEM_freeN(str_menu);
514         }
515                 
516         if(obedit==NULL && v3d->localvd==NULL) {
517                 int ob_lay = ob ? ob->lay : 0;
518                 
519                 /* Layers */
520                 if (v3d->scenelock)
521                         uiTemplateLayers(layout, &sceneptr, "layers", &v3dptr, "layers_used", ob_lay);
522                 else
523                         uiTemplateLayers(layout, &v3dptr, "layers", &v3dptr, "layers_used", ob_lay);
524
525                 /* Scene lock */
526                 uiItemR(layout, &v3dptr, "lock_camera_and_layers", UI_ITEM_R_ICON_ONLY, "", 0);
527         }
528         
529         /* selection modus, dont use python for this since it cant do the toggle buttons with shift+click as well as clicking to set one. */
530         if(obedit && (obedit->type == OB_MESH)) {
531                 BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
532
533                 ts->selectmode = em->selectmode;
534                 
535                 row= uiLayoutRow(layout, 1);
536                 block= uiLayoutGetBlock(row);
537                 uiDefIconButBitS(block, TOG, SCE_SELECT_VERTEX, B_SEL_VERT, ICON_VERTEXSEL, 0,0,XIC,YIC, &em->selectmode, 1.0, 0.0, 0, 0, "Vertex select mode");
538                 uiDefIconButBitS(block, TOG, SCE_SELECT_EDGE, B_SEL_EDGE, ICON_EDGESEL, 0,0,XIC,YIC, &em->selectmode, 1.0, 0.0, 0, 0, "Edge select mode");
539                 uiDefIconButBitS(block, TOG, SCE_SELECT_FACE, B_SEL_FACE, ICON_FACESEL, 0,0,XIC,YIC, &em->selectmode, 1.0, 0.0, 0, 0, "Face select mode");
540         }
541 }