Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_view3d / view3d_header.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2004-2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_header.c
28  *  \ingroup spview3d
29  */
30
31 #include <string.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "DNA_scene_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_gpencil_types.h"
38
39 #include "BLI_utildefines.h"
40
41 #include "BLT_translation.h"
42
43 #include "BKE_context.h"
44 #include "BKE_main.h"
45 #include "BKE_screen.h"
46 #include "BKE_editmesh.h"
47
48 #include "DEG_depsgraph.h"
49
50 #include "RNA_access.h"
51 #include "RNA_define.h"
52 #include "RNA_enum_types.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "ED_mesh.h"
58 #include "ED_undo.h"
59 #include "ED_screen.h"
60
61 #include "UI_interface.h"
62 #include "UI_resources.h"
63
64 #include "view3d_intern.h"
65
66 static void do_view3d_header_buttons(bContext *C, void *arg, int event);
67
68 #define B_SEL_VERT  110
69 #define B_SEL_EDGE  111
70 #define B_SEL_FACE  112
71
72 /* -------------------------------------------------------------------- */
73 /** \name Toggle Matcap Flip Operator
74  * \{ */
75
76 static int toggle_matcap_flip(bContext *C, wmOperator *UNUSED(op))
77 {
78         View3D *v3d = CTX_wm_view3d(C);
79         v3d->shading.flag ^= V3D_SHADING_MATCAP_FLIP_X;
80         ED_view3d_shade_update(CTX_data_main(C), v3d, CTX_wm_area(C));
81         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
82         return OPERATOR_FINISHED;
83 }
84
85 void VIEW3D_OT_toggle_matcap_flip(wmOperatorType *ot)
86 {
87         /* identifiers */
88         ot->name = "Flip MatCap";
89         ot->description = "Flip MatCap";
90         ot->idname = "VIEW3D_OT_toggle_matcap_flip";
91
92         /* api callbacks */
93         ot->exec = toggle_matcap_flip;
94         ot->poll = ED_operator_view3d_active;
95 }
96
97 /** \} */
98
99
100 static void do_view3d_header_buttons(bContext *C, void *UNUSED(arg), int event)
101 {
102         wmWindow *win = CTX_wm_window(C);
103         const int ctrl = win->eventstate->ctrl, shift = win->eventstate->shift;
104
105         /* watch it: if sa->win does not exist, check that when calling direct drawing routines */
106
107         switch (event) {
108                 case B_SEL_VERT:
109                         if (EDBM_selectmode_toggle(C, SCE_SELECT_VERTEX, -1, shift, ctrl)) {
110                                 ED_undo_push(C, "Selectmode Set: Vertex");
111                         }
112                         break;
113                 case B_SEL_EDGE:
114                         if (EDBM_selectmode_toggle(C, SCE_SELECT_EDGE, -1, shift, ctrl)) {
115                                 ED_undo_push(C, "Selectmode Set: Edge");
116                         }
117                         break;
118                 case B_SEL_FACE:
119                         if (EDBM_selectmode_toggle(C, SCE_SELECT_FACE, -1, shift, ctrl)) {
120                                 ED_undo_push(C, "Selectmode Set: Face");
121                         }
122                         break;
123                 default:
124                         break;
125         }
126 }
127
128 void uiTemplateEditModeSelection(uiLayout *layout, struct bContext *C)
129 {
130         Object *obedit = CTX_data_edit_object(C);
131         uiBlock *block = uiLayoutGetBlock(layout);
132
133         UI_block_func_handle_set(block, do_view3d_header_buttons, NULL);
134
135         if (obedit && (obedit->type == OB_MESH)) {
136                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
137                 uiLayout *row;
138
139                 row = uiLayoutRow(layout, true);
140                 block = uiLayoutGetBlock(row);
141                 uiDefIconButBitS(block, UI_BTYPE_TOGGLE, SCE_SELECT_VERTEX, B_SEL_VERT, ICON_VERTEXSEL,
142                                  0, 0, UI_UNIT_X, UI_UNIT_Y, &em->selectmode, 1.0, 0.0, 0, 0,
143                                  TIP_("Vertex select - Shift-Click for multiple modes, Ctrl-Click contracts selection"));
144                 uiDefIconButBitS(block, UI_BTYPE_TOGGLE, SCE_SELECT_EDGE, B_SEL_EDGE, ICON_EDGESEL,
145                                  0, 0, UI_UNIT_X, UI_UNIT_Y, &em->selectmode, 1.0, 0.0, 0, 0,
146                                  TIP_("Edge select - Shift-Click for multiple modes, Ctrl-Click expands/contracts selection"));
147                 uiDefIconButBitS(block, UI_BTYPE_TOGGLE, SCE_SELECT_FACE, B_SEL_FACE, ICON_FACESEL,
148                                  0, 0, UI_UNIT_X, UI_UNIT_Y, &em->selectmode, 1.0, 0.0, 0, 0,
149                                  TIP_("Face select - Shift-Click for multiple modes, Ctrl-Click expands selection"));
150         }
151 }
152
153 static void uiTemplatePaintModeSelection(uiLayout *layout, struct bContext *C)
154 {
155         ViewLayer *view_layer = CTX_data_view_layer(C);
156         Object *ob = OBACT(view_layer);
157
158         /* Gizmos aren't used in paint modes */
159         if (!ELEM(ob->mode, OB_MODE_SCULPT, OB_MODE_PARTICLE_EDIT)) {
160                 /* masks aren't used for sculpt and particle painting */
161                 PointerRNA meshptr;
162
163                 RNA_pointer_create(ob->data, &RNA_Mesh, ob->data, &meshptr);
164                 if (ob->mode & (OB_MODE_TEXTURE_PAINT)) {
165                         uiItemR(layout, &meshptr, "use_paint_mask", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
166                 }
167                 else {
168                         uiLayout *row = uiLayoutRow(layout, true);
169                         uiItemR(row, &meshptr, "use_paint_mask", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
170                         if (ob->mode & OB_MODE_WEIGHT_PAINT) {
171                                 uiItemR(row, &meshptr, "use_paint_mask_vertex", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
172                         }
173                 }
174         }
175 }
176
177 void uiTemplateHeader3D_mode(uiLayout *layout, struct bContext *C)
178 {
179         /* Extracted from: uiTemplateHeader3D */
180         ViewLayer *view_layer = CTX_data_view_layer(C);
181         Object *ob = OBACT(view_layer);
182         Object *obedit = CTX_data_edit_object(C);
183         bGPdata *gpd = CTX_data_gpencil_data(C);
184
185         bool is_paint = (
186                 ob && !(gpd && (gpd->flag & GP_DATA_STROKE_EDITMODE)) &&
187                 ELEM(ob->mode,
188                      OB_MODE_SCULPT, OB_MODE_VERTEX_PAINT, OB_MODE_WEIGHT_PAINT, OB_MODE_TEXTURE_PAINT));
189
190         uiTemplateEditModeSelection(layout, C);
191         if ((obedit == NULL) && is_paint) {
192                 uiTemplatePaintModeSelection(layout, C);
193         }
194 }
195
196 void uiTemplateHeader3D(uiLayout *layout, struct bContext *C)
197 {
198         bScreen *screen = CTX_wm_screen(C);
199         ScrArea *sa = CTX_wm_area(C);
200         View3D *v3d = sa->spacedata.first;
201         Scene *scene = CTX_data_scene(C);
202         ViewLayer *view_layer = CTX_data_view_layer(C);
203         ToolSettings *ts = CTX_data_tool_settings(C);
204         PointerRNA v3dptr, toolsptr, sceneptr;
205         Object *ob = OBACT(view_layer);
206         Object *obedit = CTX_data_edit_object(C);
207         bGPdata *gpd = CTX_data_gpencil_data(C);
208         uiBlock *block;
209         bool is_paint = (
210                 ob && !(gpd && (gpd->flag & GP_DATA_STROKE_EDITMODE)) &&
211                 ELEM(ob->mode,
212                      OB_MODE_SCULPT, OB_MODE_VERTEX_PAINT, OB_MODE_WEIGHT_PAINT, OB_MODE_TEXTURE_PAINT));
213
214         RNA_pointer_create(&screen->id, &RNA_SpaceView3D, v3d, &v3dptr);
215         RNA_pointer_create(&scene->id, &RNA_ToolSettings, ts, &toolsptr);
216         RNA_pointer_create(&scene->id, &RNA_Scene, scene, &sceneptr);
217
218         block = uiLayoutGetBlock(layout);
219         UI_block_func_handle_set(block, do_view3d_header_buttons, NULL);
220
221         /* other buttons: */
222         UI_block_emboss_set(block, UI_EMBOSS);
223
224         /* moved to topbar */
225 #if 0
226         uiLayout *row = uiLayoutRow(layout, true);
227         uiItemR(row, &v3dptr, "pivot_point", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
228         if (!ob || ELEM(ob->mode, OB_MODE_OBJECT, OB_MODE_POSE, OB_MODE_WEIGHT_PAINT)) {
229                 uiItemR(row, &v3dptr, "use_pivot_point_align", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
230         }
231 #endif
232
233         if (obedit == NULL && is_paint) {
234                 /* Currently Python calls this directly. */
235 #if 0
236                 uiTemplatePaintModeSelection(layout, C);
237 #endif
238
239         }
240         else {
241                 /* Moved to popover and topbar. */
242 #if 0
243                 /* Transform widget / gizmos */
244                 row = uiLayoutRow(layout, true);
245                 uiItemR(row, &v3dptr, "show_gizmo", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
246                 uiItemR(row, &sceneptr, "transform_orientation", 0, "", ICON_NONE);
247 #endif
248         }
249
250         if (obedit == NULL && v3d->localvd == NULL) {
251                 /* Scene lock */
252                 uiItemR(layout, &v3dptr, "lock_camera_and_layers", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
253         }
254
255         /* Currently Python calls this directly. */
256 #if 0
257         uiTemplateEditModeSelection(layout, C);
258 #endif
259 }