4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2004-2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/editors/space_view3d/view3d_header.c
38 #include "DNA_scene_types.h"
39 #include "DNA_object_types.h"
41 #include "RNA_access.h"
43 #include "MEM_guardedalloc.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_editVert.h"
48 #include "BLI_utildefines.h"
50 #include "BKE_context.h"
51 #include "BKE_depsgraph.h"
52 #include "BKE_effect.h"
55 #include "BKE_modifier.h"
56 #include "BKE_paint.h"
57 #include "BKE_screen.h"
58 #include "BKE_tessmesh.h"
62 #include "ED_screen.h"
63 #include "ED_transform.h"
69 #include "RNA_define.h"
70 #include "RNA_enum_types.h"
72 #include "UI_interface.h"
73 #include "UI_resources.h"
75 #include "view3d_intern.h"
79 * This is a bit of a dodgy hack to enable a 'mode' menu with icons+labels
80 * rather than those buttons.
81 * I know the implementation's not good - it's an experiment to see if this
82 * approach would work well
84 * This can be cleaned when I make some new 'mode' icons.
87 #define TEST_EDITMESH if(obedit==0) return; \
88 if( (v3d->lay & obedit->lay)==0 ) return;
90 /* view3d handler codes */
91 #define VIEW3D_HANDLER_BACKGROUND 1
92 #define VIEW3D_HANDLER_PROPERTIES 2
93 #define VIEW3D_HANDLER_OBJECT 3
94 #define VIEW3D_HANDLER_PREVIEW 4
95 #define VIEW3D_HANDLER_MULTIRES 5
96 #define VIEW3D_HANDLER_TRANSFORM 6
97 #define VIEW3D_HANDLER_GREASEPENCIL 7
98 #define VIEW3D_HANDLER_BONESKETCH 8
100 /* end XXX ************* */
102 static void do_view3d_header_buttons(bContext *C, void *arg, int event);
104 #define B_SCENELOCK 101
107 #define B_VIEWBUT 104
109 #define B_MODESELECT 108
110 #define B_SEL_VERT 110
111 #define B_SEL_EDGE 111
112 #define B_SEL_FACE 112
113 #define B_MAN_TRANS 116
114 #define B_MAN_ROT 117
115 #define B_MAN_SCALE 118
117 #define B_MAN_MODE 120
121 // XXX quickly ported across
122 static void handle_view3d_lock(bContext *C)
124 Main *bmain= CTX_data_main(C);
125 Scene *scene= CTX_data_scene(C);
126 ScrArea *sa= CTX_wm_area(C);
127 View3D *v3d= CTX_wm_view3d(C);
129 if (v3d != NULL && sa != NULL) {
130 if(v3d->localvd==NULL && v3d->scenelock && sa->spacetype==SPACE_VIEW3D) {
132 scene->lay= v3d->lay;
133 scene->layact= v3d->layact;
134 scene->camera= v3d->camera;
136 /* not through notifiery, listener don't have context
137 and non-open screens or spaces need to be updated too */
138 BKE_screen_view3d_main_sync(&bmain->screen, scene);
140 /* notifiers for scene update */
141 WM_event_add_notifier(C, NC_SCENE|ND_LAYER, scene);
146 /* layer code is on three levels actually:
148 - uiTemplateLayers in interface/ code for buttons
149 - ED_view3d_scene_layer_set for RNA
151 static void view3d_layers_editmode_ensure(Scene *scene, View3D *v3d)
153 /* sanity check - when in editmode disallow switching the editmode layer off since its confusing
154 * an alternative would be to always draw the editmode object. */
155 if(scene->obedit && (scene->obedit->lay & v3d->lay)==0) {
157 for(bit=0; bit<32; bit++) {
158 if(scene->obedit->lay & (1<<bit)) {
166 static int view3d_layers_exec(bContext *C, wmOperator *op)
168 Scene *scene= CTX_data_scene(C);
169 ScrArea *sa= CTX_wm_area(C);
170 View3D *v3d= sa->spacedata.first;
171 int nr= RNA_int_get(op->ptr, "nr");
172 int toggle= RNA_boolean_get(op->ptr, "toggle");
175 return OPERATOR_CANCELLED;
182 if (toggle && v3d->lay == ((1<<20)-1)) {
183 /* return to active layer only */
184 v3d->lay = v3d->layact;
186 view3d_layers_editmode_ensure(scene, v3d);
189 v3d->lay |= (1<<20)-1;
196 if(RNA_boolean_get(op->ptr, "extend")) {
197 if(toggle && v3d->lay & (1<<nr) && (v3d->lay & ~(1<<nr)))
198 v3d->lay &= ~(1<<nr);
205 view3d_layers_editmode_ensure(scene, v3d);
207 /* set active layer, ensure to always have one */
208 if(v3d->lay & (1<<nr))
210 else if((v3d->lay & v3d->layact)==0) {
211 for(bit=0; bit<32; bit++) {
212 if(v3d->lay & (1<<bit)) {
220 if(v3d->scenelock) handle_view3d_lock(C);
222 DAG_on_visible_update(CTX_data_main(C), FALSE);
224 ED_area_tag_redraw(sa);
226 return OPERATOR_FINISHED;
229 /* applies shift and alt, lazy coding or ok? :) */
230 /* the local per-keymap-entry keymap will solve it */
231 static int view3d_layers_invoke(bContext *C, wmOperator *op, wmEvent *event)
233 if(event->ctrl || event->oskey)
234 return OPERATOR_PASS_THROUGH;
237 RNA_boolean_set(op->ptr, "extend", 1);
240 int nr= RNA_int_get(op->ptr, "nr") + 10;
241 RNA_int_set(op->ptr, "nr", nr);
243 view3d_layers_exec(C, op);
245 return OPERATOR_FINISHED;
248 static int view3d_layers_poll(bContext *C)
250 return (ED_operator_view3d_active(C) && CTX_wm_view3d(C)->localvd==NULL);
253 void VIEW3D_OT_layers(wmOperatorType *ot)
257 ot->description= "Toggle layer(s) visibility";
258 ot->idname= "VIEW3D_OT_layers";
261 ot->invoke= view3d_layers_invoke;
262 ot->exec= view3d_layers_exec;
263 ot->poll= view3d_layers_poll;
266 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
268 RNA_def_int(ot->srna, "nr", 1, 0, 20, "Number", "The layer number to set, zero for all layers", 0, 20);
269 RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Add this layer to the current view layers");
270 RNA_def_boolean(ot->srna, "toggle", 1, "Toggle", "Toggle the layer");
273 static char *view3d_modeselect_pup(Scene *scene)
276 static char string[256];
277 static char formatstr[] = "|%s %%x%d %%i%d";
280 str += sprintf(str, "Mode: %%t");
282 str += sprintf(str, formatstr, "Object Mode", OB_MODE_OBJECT, ICON_OBJECT_DATA);
284 if(ob==NULL || ob->data==NULL) return string;
285 if(ob->id.lib) return string;
287 if(!((ID *)ob->data)->lib) {
288 /* if active object is editable */
289 if ( ((ob->type == OB_MESH)
290 || (ob->type == OB_CURVE) || (ob->type == OB_SURF) || (ob->type == OB_FONT)
291 || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) {
293 str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT, ICON_EDITMODE_HLT);
295 else if (ob->type == OB_ARMATURE) {
296 if (ob->mode & OB_MODE_POSE)
297 str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT|OB_MODE_POSE, ICON_EDITMODE_HLT);
299 str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT, ICON_EDITMODE_HLT);
302 if (ob->type == OB_MESH) {
304 str += sprintf(str, formatstr, "Sculpt Mode", OB_MODE_SCULPT, ICON_SCULPTMODE_HLT);
305 str += sprintf(str, formatstr, "Vertex Paint", OB_MODE_VERTEX_PAINT, ICON_VPAINT_HLT);
306 str += sprintf(str, formatstr, "Texture Paint", OB_MODE_TEXTURE_PAINT, ICON_TPAINT_HLT);
307 str += sprintf(str, formatstr, "Weight Paint", OB_MODE_WEIGHT_PAINT, ICON_WPAINT_HLT);
311 /* if active object is an armature */
312 if (ob->type==OB_ARMATURE) {
313 str += sprintf(str, formatstr, "Pose Mode", OB_MODE_POSE, ICON_POSE_HLT);
316 if (ob->particlesystem.first || modifiers_findByType(ob, eModifierType_Cloth) || modifiers_findByType(ob, eModifierType_Softbody)) {
317 str += sprintf(str, formatstr, "Particle Mode", OB_MODE_PARTICLE_EDIT, ICON_PARTICLEMODE);
324 static void do_view3d_header_buttons(bContext *C, void *UNUSED(arg), int event)
326 wmWindow *win= CTX_wm_window(C);
327 ToolSettings *ts= CTX_data_tool_settings(C);
328 ScrArea *sa= CTX_wm_area(C);
329 View3D *v3d= sa->spacedata.first;
330 Object *obedit = CTX_data_edit_object(C);
331 BMEditMesh *em= NULL;
332 int ctrl= win->eventstate->ctrl, shift= win->eventstate->shift;
333 PointerRNA props_ptr;
335 if(obedit && obedit->type==OB_MESH) {
336 em= ((Mesh *)obedit->data)->edit_btmesh;
338 /* watch it: if sa->win does not exist, check that when calling direct drawing routines */
342 ED_area_tag_redraw(sa);
346 WM_operator_properties_create(&props_ptr, "OBJECT_OT_mode_set");
347 RNA_enum_set(&props_ptr, "mode", v3d->modeselect);
348 WM_operator_name_call(C, "OBJECT_OT_mode_set", WM_OP_EXEC_REGION_WIN, &props_ptr);
349 WM_operator_properties_free(&props_ptr);
354 if(shift==0 || em->selectmode==0)
355 em->selectmode= SCE_SELECT_VERTEX;
356 ts->selectmode= em->selectmode;
357 EDBM_selectmode_set(em);
358 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
359 ED_undo_push(C, "Selectmode Set: Vertex");
364 if(shift==0 || em->selectmode==0){
365 if( (em->selectmode ^ SCE_SELECT_EDGE) == SCE_SELECT_VERTEX){
366 if(ctrl) EDBM_convertsel(em, SCE_SELECT_VERTEX,SCE_SELECT_EDGE);
368 em->selectmode = SCE_SELECT_EDGE;
370 ts->selectmode= em->selectmode;
371 EDBM_selectmode_set(em);
372 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
373 ED_undo_push(C, "Selectmode Set: Edge");
378 if( shift==0 || em->selectmode==0){
379 if( ((ts->selectmode ^ SCE_SELECT_FACE) == SCE_SELECT_VERTEX) || ((ts->selectmode ^ SCE_SELECT_FACE) == SCE_SELECT_EDGE)){
380 if(ctrl) EDBM_convertsel(em, (ts->selectmode ^ SCE_SELECT_FACE),SCE_SELECT_FACE);
382 em->selectmode = SCE_SELECT_FACE;
384 ts->selectmode= em->selectmode;
385 EDBM_selectmode_set(em);
386 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
387 ED_undo_push(C, "Selectmode Set: Face");
392 if( shift==0 || v3d->twtype==0) {
393 v3d->twtype= V3D_MANIP_TRANSLATE;
395 ED_area_tag_redraw(sa);
398 if( shift==0 || v3d->twtype==0) {
399 v3d->twtype= V3D_MANIP_ROTATE;
401 ED_area_tag_redraw(sa);
404 if( shift==0 || v3d->twtype==0) {
405 v3d->twtype= V3D_MANIP_SCALE;
407 ED_area_tag_redraw(sa);
410 ED_area_tag_redraw(sa);
413 ED_area_tag_redraw(sa);
420 /* Returns the icon associated with an object mode */
421 static int object_mode_icon(int mode)
423 EnumPropertyItem *item = object_mode_items;
425 while(item->name != NULL) {
426 if(item->value == mode)
431 return ICON_OBJECT_DATAMODE;
434 void uiTemplateEditModeSelection(uiLayout *layout, struct bContext *C)
436 Object *obedit = CTX_data_edit_object(C);
437 uiBlock *block= uiLayoutGetBlock(layout);
439 uiBlockSetHandleFunc(block, do_view3d_header_buttons, NULL);
441 if(obedit && (obedit->type == OB_MESH)) {
442 BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
445 row= uiLayoutRow(layout, 1);
446 block= uiLayoutGetBlock(row);
447 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");
448 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");
449 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");
453 void uiTemplateHeader3D(uiLayout *layout, struct bContext *C)
455 bScreen *screen= CTX_wm_screen(C);
456 ScrArea *sa= CTX_wm_area(C);
457 View3D *v3d= sa->spacedata.first;
458 Scene *scene= CTX_data_scene(C);
459 ToolSettings *ts= CTX_data_tool_settings(C);
460 PointerRNA v3dptr, toolsptr, sceneptr;
462 Object *obedit = CTX_data_edit_object(C);
465 const float dpi_fac= UI_DPI_FAC;
467 RNA_pointer_create(&screen->id, &RNA_SpaceView3D, v3d, &v3dptr);
468 RNA_pointer_create(&scene->id, &RNA_ToolSettings, ts, &toolsptr);
469 RNA_pointer_create(&scene->id, &RNA_Scene, scene, &sceneptr);
471 block= uiLayoutGetBlock(layout);
472 uiBlockSetHandleFunc(block, do_view3d_header_buttons, NULL);
475 uiBlockSetEmboss(block, UI_EMBOSS);
479 /*sanity point checkpoint, put here to avoid seeding
480 this same code in 10 different other places.*/
482 ob->mode = OB_MODE_OBJECT;
484 v3d->modeselect = ob->mode;
486 v3d->modeselect = OB_MODE_OBJECT;
489 uiBlockBeginAlign(block);
490 uiDefIconTextButS(block, MENU, B_MODESELECT, object_mode_icon(v3d->modeselect), view3d_modeselect_pup(scene) ,
491 0,0,126 * dpi_fac, UI_UNIT_Y, &(v3d->modeselect), 0, 0, 0, 0, "Mode");
492 uiBlockEndAlign(block);
495 uiItemR(layout, &v3dptr, "viewport_shade", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
497 if (obedit==NULL && ((ob && ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)))) {
498 /* Manipulators aren't used in weight paint mode */
502 RNA_pointer_create(&ob->id, &RNA_Mesh, ob->data, &meshptr);
503 uiItemR(layout, &meshptr, "use_paint_mask", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
505 const char *str_menu;
507 row= uiLayoutRow(layout, 1);
508 uiItemR(row, &v3dptr, "pivot_point", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
509 uiItemR(row, &v3dptr, "use_pivot_point_align", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
511 /* Transform widget / manipulators */
512 row= uiLayoutRow(layout, 1);
513 uiItemR(row, &v3dptr, "show_manipulator", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
514 block= uiLayoutGetBlock(row);
516 if(v3d->twflag & V3D_USE_MANIPULATOR) {
517 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");
518 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");
519 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");
522 if (v3d->twmode > (BIF_countTransformOrientation(C) - 1) + V3D_MANIP_CUSTOM) {
526 str_menu = BIF_menustringTransformOrientation(C, "Orientation");
527 uiDefButC(block, MENU, B_MAN_MODE, str_menu,0,0,70 * dpi_fac, UI_UNIT_Y, &v3d->twmode, 0, 0, 0, 0, "Transform Orientation");
528 MEM_freeN((void *)str_menu);
531 if(obedit==NULL && v3d->localvd==NULL) {
532 unsigned int ob_lay = ob ? ob->lay : 0;
536 uiTemplateLayers(layout, &sceneptr, "layers", &v3dptr, "layers_used", ob_lay);
538 uiTemplateLayers(layout, &v3dptr, "layers", &v3dptr, "layers_used", ob_lay);
541 uiItemR(layout, &v3dptr, "lock_camera_and_layers", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
544 uiTemplateEditModeSelection(layout, C);