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