2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2004-2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_view3d/view3d_header.c
36 #include "DNA_scene_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_mesh_types.h"
40 #include "RNA_access.h"
42 #include "MEM_guardedalloc.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_utildefines.h"
48 #include "BLF_translation.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 /* end XXX ************* */
89 static void do_view3d_header_buttons(bContext *C, void *arg, int event);
91 #define B_MODESELECT 108
92 #define B_SEL_VERT 110
93 #define B_SEL_EDGE 111
94 #define B_SEL_FACE 112
95 #define B_MAN_TRANS 116
97 #define B_MAN_SCALE 118
98 #define B_MAN_MODE 120
100 /* XXX quickly ported across */
101 static void handle_view3d_lock(bContext *C)
103 Main *bmain = CTX_data_main(C);
104 Scene *scene = CTX_data_scene(C);
105 ScrArea *sa = CTX_wm_area(C);
106 View3D *v3d = CTX_wm_view3d(C);
108 if (v3d != NULL && sa != NULL) {
109 if (v3d->localvd == NULL && v3d->scenelock && sa->spacetype == SPACE_VIEW3D) {
111 scene->lay = v3d->lay;
112 scene->layact = v3d->layact;
113 scene->camera = v3d->camera;
115 /* not through notifier, listener don't have context
116 * and non-open screens or spaces need to be updated too */
117 BKE_screen_view3d_main_sync(&bmain->screen, scene);
119 /* notifiers for scene update */
120 WM_event_add_notifier(C, NC_SCENE | ND_LAYER, scene);
126 * layer code is on three levels actually:
127 * - here for operator
128 * - uiTemplateLayers in interface/ code for buttons
129 * - ED_view3d_scene_layer_set for RNA
131 static void view3d_layers_editmode_ensure(Scene *scene, View3D *v3d)
133 /* sanity check - when in editmode disallow switching the editmode layer off since its confusing
134 * an alternative would be to always draw the editmode object. */
135 if (scene->obedit && (scene->obedit->lay & v3d->lay) == 0) {
137 for (bit = 0; bit < 32; bit++) {
138 if (scene->obedit->lay & (1 << bit)) {
139 v3d->lay |= 1 << bit;
146 static int view3d_layers_exec(bContext *C, wmOperator *op)
148 Scene *scene = CTX_data_scene(C);
149 ScrArea *sa = CTX_wm_area(C);
150 View3D *v3d = sa->spacedata.first;
151 int nr = RNA_int_get(op->ptr, "nr");
152 int toggle = RNA_boolean_get(op->ptr, "toggle");
155 return OPERATOR_CANCELLED;
162 if (toggle && v3d->lay == ((1 << 20) - 1)) {
163 /* return to active layer only */
164 v3d->lay = v3d->layact;
166 view3d_layers_editmode_ensure(scene, v3d);
169 v3d->lay |= (1 << 20) - 1;
176 if (RNA_boolean_get(op->ptr, "extend")) {
177 if (toggle && v3d->lay & (1 << nr) && (v3d->lay & ~(1 << nr)))
178 v3d->lay &= ~(1 << nr);
180 v3d->lay |= (1 << nr);
183 v3d->lay = (1 << nr);
186 view3d_layers_editmode_ensure(scene, v3d);
188 /* set active layer, ensure to always have one */
189 if (v3d->lay & (1 << nr))
190 v3d->layact = 1 << nr;
191 else if ((v3d->lay & v3d->layact) == 0) {
192 for (bit = 0; bit < 32; bit++) {
193 if (v3d->lay & (1 << bit)) {
194 v3d->layact = 1 << bit;
201 if (v3d->scenelock) handle_view3d_lock(C);
203 DAG_on_visible_update(CTX_data_main(C), FALSE);
205 ED_area_tag_redraw(sa);
207 return OPERATOR_FINISHED;
210 /* applies shift and alt, lazy coding or ok? :) */
211 /* the local per-keymap-entry keymap will solve it */
212 static int view3d_layers_invoke(bContext *C, wmOperator *op, wmEvent *event)
214 if (event->ctrl || event->oskey)
215 return OPERATOR_PASS_THROUGH;
218 RNA_boolean_set(op->ptr, "extend", TRUE);
220 RNA_boolean_set(op->ptr, "extend", FALSE);
223 int nr = RNA_int_get(op->ptr, "nr") + 10;
224 RNA_int_set(op->ptr, "nr", nr);
226 view3d_layers_exec(C, op);
228 return OPERATOR_FINISHED;
231 static int view3d_layers_poll(bContext *C)
233 return (ED_operator_view3d_active(C) && CTX_wm_view3d(C)->localvd == NULL);
236 void VIEW3D_OT_layers(wmOperatorType *ot)
240 ot->description = "Toggle layer(s) visibility";
241 ot->idname = "VIEW3D_OT_layers";
244 ot->invoke = view3d_layers_invoke;
245 ot->exec = view3d_layers_exec;
246 ot->poll = view3d_layers_poll;
249 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
251 RNA_def_int(ot->srna, "nr", 1, 0, 20, "Number", "The layer number to set, zero for all layers", 0, 20);
252 RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Add this layer to the current view layers");
253 RNA_def_boolean(ot->srna, "toggle", 1, "Toggle", "Toggle the layer");
256 static int modeselect_addmode(char *str, const char *title, int id, int icon)
258 static char formatstr[] = "|%s %%x%d %%i%d";
260 return sprintf(str, formatstr, IFACE_(title), id, icon);
263 static char *view3d_modeselect_pup(Scene *scene)
266 static char string[512];
267 const char *title = IFACE_("Mode: %t");
270 BLI_strncpy(str, title, sizeof(string));
272 str += modeselect_addmode(str, N_("Object Mode"), OB_MODE_OBJECT, ICON_OBJECT_DATA);
274 if (ob == NULL || ob->data == NULL) return string;
275 if (ob->id.lib) return string;
277 if (!((ID *)ob->data)->lib) {
278 /* if active object is editable */
279 if (ob->type == OB_ARMATURE) {
280 if (ob->mode & OB_MODE_POSE)
281 str += modeselect_addmode(str, N_("Edit Mode"), OB_MODE_EDIT | OB_MODE_POSE, ICON_EDITMODE_HLT);
283 str += modeselect_addmode(str, N_("Edit Mode"), OB_MODE_EDIT, ICON_EDITMODE_HLT);
285 else if (OB_TYPE_SUPPORT_EDITMODE(ob->type)) {
286 str += modeselect_addmode(str, N_("Edit Mode"), OB_MODE_EDIT, ICON_EDITMODE_HLT);
289 if (ob->type == OB_MESH) {
291 str += modeselect_addmode(str, N_("Sculpt Mode"), OB_MODE_SCULPT, ICON_SCULPTMODE_HLT);
292 str += modeselect_addmode(str, N_("Vertex Paint"), OB_MODE_VERTEX_PAINT, ICON_VPAINT_HLT);
293 str += modeselect_addmode(str, N_("Texture Paint"), OB_MODE_TEXTURE_PAINT, ICON_TPAINT_HLT);
294 str += modeselect_addmode(str, N_("Weight Paint"), OB_MODE_WEIGHT_PAINT, ICON_WPAINT_HLT);
298 /* if active object is an armature */
299 if (ob->type == OB_ARMATURE) {
300 str += modeselect_addmode(str, N_("Pose Mode"), OB_MODE_POSE, ICON_POSE_HLT);
303 if (ob->particlesystem.first ||
304 modifiers_findByType(ob, eModifierType_Cloth) ||
305 modifiers_findByType(ob, eModifierType_Softbody))
307 str += modeselect_addmode(str, N_("Particle Mode"), OB_MODE_PARTICLE_EDIT, ICON_PARTICLEMODE);
314 static void do_view3d_header_buttons(bContext *C, void *UNUSED(arg), int event)
316 wmWindow *win = CTX_wm_window(C);
317 ToolSettings *ts = CTX_data_tool_settings(C);
318 ScrArea *sa = CTX_wm_area(C);
319 View3D *v3d = sa->spacedata.first;
320 Object *obedit = CTX_data_edit_object(C);
321 BMEditMesh *em = NULL;
322 const int ctrl = win->eventstate->ctrl, shift = win->eventstate->shift;
323 PointerRNA props_ptr;
325 if (obedit && obedit->type == OB_MESH) {
326 em = BMEdit_FromObject(obedit);
328 /* watch it: if sa->win does not exist, check that when calling direct drawing routines */
332 WM_operator_properties_create(&props_ptr, "OBJECT_OT_mode_set");
333 RNA_enum_set(&props_ptr, "mode", v3d->modeselect);
334 WM_operator_name_call(C, "OBJECT_OT_mode_set", WM_OP_EXEC_REGION_WIN, &props_ptr);
335 WM_operator_properties_free(&props_ptr);
340 if (shift == 0 || em->selectmode == 0)
341 em->selectmode = SCE_SELECT_VERTEX;
342 ts->selectmode = em->selectmode;
343 EDBM_selectmode_set(em);
344 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
345 ED_undo_push(C, "Selectmode Set: Vertex");
350 if (shift == 0 || em->selectmode == 0) {
352 const short selmode_max = highest_order_bit_s(ts->selectmode);
353 if (selmode_max == SCE_SELECT_VERTEX) {
354 EDBM_selectmode_convert(em, selmode_max, SCE_SELECT_EDGE);
357 em->selectmode = SCE_SELECT_EDGE;
359 ts->selectmode = em->selectmode;
360 EDBM_selectmode_set(em);
361 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
362 ED_undo_push(C, "Selectmode Set: Edge");
367 if (shift == 0 || em->selectmode == 0) {
369 const short selmode_max = highest_order_bit_s(ts->selectmode);
370 if (ELEM(selmode_max, SCE_SELECT_VERTEX, SCE_SELECT_EDGE)) {
371 EDBM_selectmode_convert(em, selmode_max, SCE_SELECT_FACE);
375 em->selectmode = SCE_SELECT_FACE;
377 ts->selectmode = em->selectmode;
378 EDBM_selectmode_set(em);
379 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
380 ED_undo_push(C, "Selectmode Set: Face");
385 if (shift == 0 || v3d->twtype == 0) {
386 v3d->twtype = V3D_MANIP_TRANSLATE;
388 ED_area_tag_redraw(sa);
391 if (shift == 0 || v3d->twtype == 0) {
392 v3d->twtype = V3D_MANIP_ROTATE;
394 ED_area_tag_redraw(sa);
397 if (shift == 0 || v3d->twtype == 0) {
398 v3d->twtype = V3D_MANIP_SCALE;
400 ED_area_tag_redraw(sa);
403 ED_area_tag_redraw(sa);
410 /* Returns the icon associated with an object mode */
411 static int object_mode_icon(int mode)
413 EnumPropertyItem *item = object_mode_items;
415 while (item->name != NULL) {
416 if (item->value == mode) {
422 return ICON_OBJECT_DATAMODE;
425 void uiTemplateEditModeSelection(uiLayout *layout, struct bContext *C)
427 Object *obedit = CTX_data_edit_object(C);
428 uiBlock *block = uiLayoutGetBlock(layout);
430 uiBlockSetHandleFunc(block, do_view3d_header_buttons, NULL);
432 if (obedit && (obedit->type == OB_MESH)) {
433 BMEditMesh *em = BMEdit_FromObject(obedit);
436 row = uiLayoutRow(layout, TRUE);
437 block = uiLayoutGetBlock(row);
438 uiDefIconButBitS(block, TOG, SCE_SELECT_VERTEX, B_SEL_VERT, ICON_VERTEXSEL,
439 0, 0, UI_UNIT_X, UI_UNIT_Y, &em->selectmode, 1.0, 0.0, 0, 0,
440 "Vertex select - Shift-Click for multiple modes");
441 uiDefIconButBitS(block, TOG, SCE_SELECT_EDGE, B_SEL_EDGE, ICON_EDGESEL,
442 0, 0, UI_UNIT_X, UI_UNIT_Y, &em->selectmode, 1.0, 0.0, 0, 0,
443 "Edge select - Shift-Click for multiple modes, Ctrl-Click expands selection");
444 uiDefIconButBitS(block, TOG, SCE_SELECT_FACE, B_SEL_FACE, ICON_FACESEL,
445 0, 0, UI_UNIT_X, UI_UNIT_Y, &em->selectmode, 1.0, 0.0, 0, 0,
446 "Face select - Shift-Click for multiple modes, Ctrl-Click expands selection");
450 void uiTemplateHeader3D(uiLayout *layout, struct bContext *C)
452 bScreen *screen = CTX_wm_screen(C);
453 ScrArea *sa = CTX_wm_area(C);
454 View3D *v3d = sa->spacedata.first;
455 Scene *scene = CTX_data_scene(C);
456 ToolSettings *ts = CTX_data_tool_settings(C);
457 PointerRNA v3dptr, toolsptr, sceneptr;
459 Object *obedit = CTX_data_edit_object(C);
463 const float dpi_fac = UI_DPI_FAC;
466 RNA_pointer_create(&screen->id, &RNA_SpaceView3D, v3d, &v3dptr);
467 RNA_pointer_create(&scene->id, &RNA_ToolSettings, ts, &toolsptr);
468 RNA_pointer_create(&scene->id, &RNA_Scene, scene, &sceneptr);
470 block = uiLayoutGetBlock(layout);
471 uiBlockSetHandleFunc(block, do_view3d_header_buttons, NULL);
474 uiBlockSetEmboss(block, UI_EMBOSS);
478 v3d->modeselect = ob->mode;
479 is_paint = ELEM4(ob->mode, OB_MODE_SCULPT, OB_MODE_VERTEX_PAINT, OB_MODE_WEIGHT_PAINT, OB_MODE_TEXTURE_PAINT);
482 v3d->modeselect = OB_MODE_OBJECT;
485 row = uiLayoutRow(layout, TRUE);
486 uiDefIconTextButS(block, MENU, B_MODESELECT, object_mode_icon(v3d->modeselect), view3d_modeselect_pup(scene),
487 0, 0, 126 * dpi_fac, UI_UNIT_Y, &(v3d->modeselect), 0, 0, 0, 0, TIP_("Mode"));
490 uiItemR(layout, &v3dptr, "viewport_shade", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
492 if (obedit == NULL && is_paint) {
493 /* Manipulators aren't used in paint modes */
494 if (!ELEM(ob->mode, OB_MODE_SCULPT, OB_MODE_PARTICLE_EDIT)) {
495 /* masks aren't used for sculpt and particle painting */
498 RNA_pointer_create(&ob->id, &RNA_Mesh, ob->data, &meshptr);
499 if (ob->mode & (OB_MODE_TEXTURE_PAINT | OB_MODE_VERTEX_PAINT)) {
500 uiItemR(layout, &meshptr, "use_paint_mask", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
503 row = uiLayoutRow(layout, TRUE);
504 uiItemR(row, &meshptr, "use_paint_mask", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
505 uiItemR(row, &meshptr, "use_paint_mask_vertex", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
510 const char *str_menu;
512 row = uiLayoutRow(layout, TRUE);
513 uiItemR(row, &v3dptr, "pivot_point", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
515 /* pose/object only however we want to allow in weight paint mode too
516 * so don't be totally strict and just check not-editmode for now */
517 if (obedit == NULL) {
518 uiItemR(row, &v3dptr, "use_pivot_point_align", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
521 /* Transform widget / manipulators */
522 row = uiLayoutRow(layout, TRUE);
523 uiItemR(row, &v3dptr, "show_manipulator", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
524 block = uiLayoutGetBlock(row);
526 if (v3d->twflag & V3D_USE_MANIPULATOR) {
527 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, TIP_("Translate manipulator - Shift-Click for multiple modes"));
528 uiButClearFlag(but, UI_BUT_UNDO); /* skip undo on screen buttons */
529 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, TIP_("Rotate manipulator - Shift-Click for multiple modes"));
530 uiButClearFlag(but, UI_BUT_UNDO); /* skip undo on screen buttons */
531 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, TIP_("Scale manipulator - Shift-Click for multiple modes"));
532 uiButClearFlag(but, UI_BUT_UNDO); /* skip undo on screen buttons */
535 if (v3d->twmode > (BIF_countTransformOrientation(C) - 1) + V3D_MANIP_CUSTOM) {
539 str_menu = BIF_menustringTransformOrientation(C, "Orientation");
540 but = uiDefButC(block, MENU, B_MAN_MODE, str_menu, 0, 0, 70 * dpi_fac, UI_UNIT_Y, &v3d->twmode, 0, 0, 0, 0, TIP_("Transform Orientation"));
541 uiButClearFlag(but, UI_BUT_UNDO); /* skip undo on screen buttons */
542 MEM_freeN((void *)str_menu);
545 if (obedit == NULL && v3d->localvd == NULL) {
546 unsigned int ob_lay = ob ? ob->lay : 0;
549 uiTemplateLayers(layout, v3d->scenelock ? &sceneptr : &v3dptr, "layers", &v3dptr, "layers_used", ob_lay);
552 uiItemR(layout, &v3dptr, "lock_camera_and_layers", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
555 uiTemplateEditModeSelection(layout, C);