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