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