merged 30707:31141 from trunk
[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->layact= v3d->layact;
141                         scene->camera= v3d->camera;
142
143                         /* not through notifiery, listener don't have context
144                            and non-open screens or spaces need to be updated too */
145                         BKE_screen_view3d_main_sync(&bmain->screen, scene);
146                         
147                         /* notifiers for scene update */
148                         WM_event_add_notifier(C, NC_SCENE|ND_LAYER, scene);
149                 }
150         }
151 }
152
153 static int layers_exec(bContext *C, wmOperator *op)
154 {
155         Main *bmain= CTX_data_main(C);
156         Scene *scene= CTX_data_scene(C);
157         ScrArea *sa= CTX_wm_area(C);
158         View3D *v3d= sa->spacedata.first;
159         int nr= RNA_int_get(op->ptr, "nr");
160         int toggle= RNA_boolean_get(op->ptr, "toggle");
161         
162         if(nr < 0)
163                 return OPERATOR_CANCELLED;
164
165         if(nr == 0) {
166                 /* all layers */
167                 if(!v3d->layact)
168                         v3d->layact= 1;
169
170                 if (toggle && v3d->lay == ((1<<20)-1)) {
171                         /* return to active layer only */
172                         v3d->lay = v3d->layact;
173                 } else {
174                         v3d->lay |= (1<<20)-1;
175                 }               
176         }
177         else {
178                 int bit;
179                 nr--;
180
181                 if(RNA_boolean_get(op->ptr, "extend")) {
182                         if(toggle && v3d->lay & (1<<nr) && (v3d->lay & ~(1<<nr)))
183                                 v3d->lay &= ~(1<<nr);
184                         else
185                                 v3d->lay |= (1<<nr);
186                 } else {
187                         v3d->lay = (1<<nr);
188
189                         /* sanity check - when in editmode disallow switching the editmode layer off since its confusing
190                          * an alternative would be to always draw the editmode object. */
191                         if(scene->obedit && (scene->obedit->lay & v3d->lay)==0) {
192                                 for(bit=0; bit<32; bit++) {
193                                         if(scene->obedit->lay & (1<<bit)) {
194                                                 v3d->lay |= 1<<bit;
195                                                 break;
196                                         }
197                                 }
198                         }
199                 }
200                 
201                 /* set active layer, ensure to always have one */
202                 if(v3d->lay & (1<<nr))
203                    v3d->layact= 1<<nr;
204                 else if((v3d->lay & v3d->layact)==0) {
205                         for(bit=0; bit<32; bit++) {
206                                 if(v3d->lay & (1<<bit)) {
207                                         v3d->layact= 1<<bit;
208                                         break;
209                                 }
210                         }
211                 }
212         }
213         
214         if(v3d->scenelock) handle_view3d_lock(C);
215         
216         /* new layers might need unflushed events events */
217         DAG_scene_update_flags(bmain, scene, v3d->lay); /* tags all that moves and flushes */
218
219         ED_area_tag_redraw(sa);
220         
221         return OPERATOR_FINISHED;
222 }
223
224 /* applies shift and alt, lazy coding or ok? :) */
225 /* the local per-keymap-entry keymap will solve it */
226 static int layers_invoke(bContext *C, wmOperator *op, wmEvent *event)
227 {
228         if(event->ctrl || event->oskey)
229                 return OPERATOR_PASS_THROUGH;
230         
231         if(event->shift)
232                 RNA_boolean_set(op->ptr, "extend", 1);
233         
234         if(event->alt) {
235                 int nr= RNA_int_get(op->ptr, "nr") + 10;
236                 RNA_int_set(op->ptr, "nr", nr);
237         }
238         layers_exec(C, op);
239         
240         return OPERATOR_FINISHED;
241 }
242
243 int layers_poll(bContext *C)
244 {
245         return (ED_operator_view3d_active(C) && CTX_wm_view3d(C)->localvd==NULL);
246 }
247
248 void VIEW3D_OT_layers(wmOperatorType *ot)
249 {
250         /* identifiers */
251         ot->name= "Layers";
252         ot->description= "Toggle layer(s) visibility";
253         ot->idname= "VIEW3D_OT_layers";
254         
255         /* api callbacks */
256         ot->invoke= layers_invoke;
257         ot->exec= layers_exec;
258         ot->poll= layers_poll;
259         
260         /* flags */
261         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
262         
263         RNA_def_int(ot->srna, "nr", 1, 0, 20, "Number", "The layer number to set, zero for all layers", 0, 20);
264         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Add this layer to the current view layers");
265         RNA_def_boolean(ot->srna, "toggle", 1, "Toggle", "Toggle the layer");
266 }
267
268 static char *view3d_modeselect_pup(Scene *scene)
269 {
270         Object *ob= OBACT;
271         static char string[1024];
272         static char formatstr[] = "|%s %%x%d %%i%d";
273         char *str = string;
274
275         str += sprintf(str, "Mode: %%t");
276         
277         str += sprintf(str, formatstr, "Object Mode", OB_MODE_OBJECT, ICON_OBJECT_DATA);
278         
279         if(ob==NULL) return string;
280         
281         /* if active object is editable */
282         if ( ((ob->type == OB_MESH)
283                 || (ob->type == OB_CURVE) || (ob->type == OB_SURF) || (ob->type == OB_FONT)
284                 || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) {
285                 
286                 str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT, ICON_EDITMODE_HLT);
287         }
288         else if (ob->type == OB_ARMATURE) {
289                 if (ob->mode & OB_MODE_POSE)
290                         str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT|OB_MODE_POSE, ICON_EDITMODE_HLT);
291                 else
292                         str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT, ICON_EDITMODE_HLT);
293         }
294
295         if (ob->type == OB_MESH) {
296
297                 str += sprintf(str, formatstr, "Sculpt Mode", OB_MODE_SCULPT, ICON_SCULPTMODE_HLT);
298                 str += sprintf(str, formatstr, "Vertex Paint", OB_MODE_VERTEX_PAINT, ICON_VPAINT_HLT);
299                 str += sprintf(str, formatstr, "Texture Paint", OB_MODE_TEXTURE_PAINT, ICON_TPAINT_HLT);
300                 str += sprintf(str, formatstr, "Weight Paint", OB_MODE_WEIGHT_PAINT, ICON_WPAINT_HLT);
301         }
302
303         
304         /* if active object is an armature */
305         if (ob->type==OB_ARMATURE) {
306                 str += sprintf(str, formatstr, "Pose Mode", OB_MODE_POSE, ICON_POSE_HLT);
307         }
308
309         if (ob->particlesystem.first || modifiers_findByType(ob, eModifierType_Cloth) || modifiers_findByType(ob, eModifierType_Softbody)) {
310                 str += sprintf(str, formatstr, "Particle Mode", OB_MODE_PARTICLE_EDIT, ICON_PARTICLEMODE);
311         }
312
313         return (string);
314 }
315
316
317 static void do_view3d_header_buttons(bContext *C, void *arg, int event)
318 {
319         wmWindow *win= CTX_wm_window(C);
320         ToolSettings *ts= CTX_data_tool_settings(C);
321         ScrArea *sa= CTX_wm_area(C);
322         View3D *v3d= sa->spacedata.first;
323         Object *obedit = CTX_data_edit_object(C);
324         EditMesh *em= NULL;
325         int ctrl= win->eventstate->ctrl, shift= win->eventstate->shift;
326         PointerRNA props_ptr;
327         
328         if(obedit && obedit->type==OB_MESH) {
329                 em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
330         }
331         /* watch it: if sa->win does not exist, check that when calling direct drawing routines */
332
333         switch(event) {
334         case B_REDR:
335                 ED_area_tag_redraw(sa);
336                 break;
337                 
338         case B_MODESELECT:
339                 WM_operator_properties_create(&props_ptr, "OBJECT_OT_mode_set");
340                 RNA_enum_set(&props_ptr, "mode", v3d->modeselect);
341                 WM_operator_name_call(C, "OBJECT_OT_mode_set", WM_OP_EXEC_REGION_WIN, &props_ptr);
342                 WM_operator_properties_free(&props_ptr);
343                 break;          
344                 
345         case B_SEL_VERT:
346                 if(em) {
347                         if(shift==0 || em->selectmode==0)
348                                 em->selectmode= SCE_SELECT_VERTEX;
349                         ts->selectmode= em->selectmode;
350                         EM_selectmode_set(em);
351                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
352                         ED_undo_push(C, "Selectmode Set: Vertex");
353                 }
354                 break;
355         case B_SEL_EDGE:
356                 if(em) {
357                         if(shift==0 || em->selectmode==0){
358                                 if( (em->selectmode ^ SCE_SELECT_EDGE) == SCE_SELECT_VERTEX){
359                                         if(ctrl) EM_convertsel(em, SCE_SELECT_VERTEX,SCE_SELECT_EDGE); 
360                                 }
361                                 em->selectmode = SCE_SELECT_EDGE;
362                         }
363                         ts->selectmode= em->selectmode;
364                         EM_selectmode_set(em);
365                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
366                         ED_undo_push(C, "Selectmode Set: Edge");
367                 }
368                 break;
369         case B_SEL_FACE:
370                 if(em) {
371                         if( shift==0 || em->selectmode==0){
372                                 if( ((em->selectmode ^ SCE_SELECT_FACE) == SCE_SELECT_VERTEX) || ((em->selectmode ^ SCE_SELECT_FACE) == SCE_SELECT_EDGE)){
373                                         if(ctrl)
374                                                 EM_convertsel(em, (em->selectmode ^ SCE_SELECT_FACE),SCE_SELECT_FACE);
375                                 }
376                                 em->selectmode = SCE_SELECT_FACE;
377                         }
378                         ts->selectmode= em->selectmode;
379                         EM_selectmode_set(em);
380                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
381                         ED_undo_push(C, "Selectmode Set: Face");
382                 }
383                 break;  
384
385         case B_MAN_TRANS:
386                 if( shift==0 || v3d->twtype==0) {
387                         v3d->twtype= V3D_MANIP_TRANSLATE;
388                 }
389                 ED_area_tag_redraw(sa);
390                 break;
391         case B_MAN_ROT:
392                 if( shift==0 || v3d->twtype==0) {
393                         v3d->twtype= V3D_MANIP_ROTATE;
394                 }
395                 ED_area_tag_redraw(sa);
396                 break;
397         case B_MAN_SCALE:
398                 if( shift==0 || v3d->twtype==0) {
399                         v3d->twtype= V3D_MANIP_SCALE;
400                 }
401                 ED_area_tag_redraw(sa);
402                 break;
403         case B_NDOF:
404                 ED_area_tag_redraw(sa);
405                 break;
406         case B_MAN_MODE:
407                 ED_area_tag_redraw(sa);
408                 break;
409         default:
410                 break;
411         }
412
413         if(obedit && obedit->type==OB_MESH)
414                 BKE_mesh_end_editmesh(obedit->data, em);
415 }
416
417 /* Returns the icon associated with an object mode */
418 static int object_mode_icon(int mode)
419 {
420         EnumPropertyItem *item = object_mode_items;
421         
422         while(item->name != NULL) {
423                 if(item->value == mode)
424                         return item->icon;
425                 ++item;
426         }
427
428         return ICON_OBJECT_DATAMODE;
429 }
430
431 void uiTemplateHeader3D(uiLayout *layout, struct bContext *C)
432 {
433         bScreen *screen= CTX_wm_screen(C);
434         ScrArea *sa= CTX_wm_area(C);
435         View3D *v3d= sa->spacedata.first;
436         Scene *scene= CTX_data_scene(C);
437         ToolSettings *ts= CTX_data_tool_settings(C);
438         PointerRNA v3dptr, toolsptr, sceneptr;
439         Object *ob= OBACT;
440         Object *obedit = CTX_data_edit_object(C);
441         uiBlock *block;
442         uiLayout *row;
443         
444         RNA_pointer_create(&screen->id, &RNA_SpaceView3D, v3d, &v3dptr);        
445         RNA_pointer_create(&scene->id, &RNA_ToolSettings, ts, &toolsptr);
446         RNA_pointer_create(&scene->id, &RNA_Scene, scene, &sceneptr);
447
448         block= uiLayoutGetBlock(layout);
449         uiBlockSetHandleFunc(block, do_view3d_header_buttons, NULL);
450
451         /* other buttons: */
452         uiBlockSetEmboss(block, UI_EMBOSS);
453         
454         /* mode */
455         if(ob)
456                 v3d->modeselect = ob->mode;
457         else
458                 v3d->modeselect = OB_MODE_OBJECT;
459                 
460         v3d->flag &= ~V3D_MODE;
461         
462         /* not sure what the v3d->flag is useful for now... modeselect is confusing */
463         if(obedit) v3d->flag |= V3D_EDITMODE;
464         if(ob && (ob->mode & OB_MODE_POSE)) v3d->flag |= V3D_POSEMODE;
465         if(ob && (ob->mode & OB_MODE_VERTEX_PAINT)) v3d->flag |= V3D_VERTEXPAINT;
466         if(ob && (ob->mode & OB_MODE_WEIGHT_PAINT)) v3d->flag |= V3D_WEIGHTPAINT;
467         if(ob && (ob->mode & OB_MODE_TEXTURE_PAINT)) v3d->flag |= V3D_TEXTUREPAINT;
468         if(paint_facesel_test(ob)) v3d->flag |= V3D_FACESELECT;
469
470         uiBlockBeginAlign(block);
471         uiDefIconTextButS(block, MENU, B_MODESELECT, object_mode_icon(v3d->modeselect), view3d_modeselect_pup(scene) , 
472                           0,0,126,20, &(v3d->modeselect), 0, 0, 0, 0, "Mode");
473         uiBlockEndAlign(block);
474         
475         /* Draw type */
476         uiItemR(layout, &v3dptr, "viewport_shading", UI_ITEM_R_ICON_ONLY, "", 0);
477
478         if (obedit==NULL && ((ob && ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)))) {
479                 /* Manipulators aren't used in weight paint mode */
480                 
481                 PointerRNA meshptr;
482
483                 RNA_pointer_create(&ob->id, &RNA_Mesh, ob->data, &meshptr);
484                 uiItemR(layout, &meshptr, "use_paint_mask", UI_ITEM_R_ICON_ONLY, "", 0);
485         } else {
486                 char *str_menu;
487
488                 row= uiLayoutRow(layout, 1);
489                 uiItemR(row, &v3dptr, "pivot_point", UI_ITEM_R_ICON_ONLY, "", 0);
490                 uiItemR(row, &v3dptr, "pivot_point_align", UI_ITEM_R_ICON_ONLY, "", 0);
491
492                 /* NDOF */
493                 /* Not implemented yet
494                  if (G.ndofdevice ==0 ) {
495                         uiDefIconTextButC(block, ICONTEXTROW,B_NDOF, ICON_NDOF_TURN, ndof_pup(), 0,0,XIC+10,YIC, &(v3d->ndofmode), 0, 3.0, 0, 0, "Ndof mode");
496                 
497                         uiDefIconButC(block, TOG, B_NDOF,  ICON_NDOF_DOM,
498                                           0,0,XIC,YIC,
499                                           &v3d->ndoffilter, 0, 1, 0, 0, "dominant axis");       
500                 }
501                  */
502
503                 /* Transform widget / manipulators */
504                 row= uiLayoutRow(layout, 1);
505                 uiItemR(row, &v3dptr, "manipulator", UI_ITEM_R_ICON_ONLY, "", 0);
506                 block= uiLayoutGetBlock(row);
507                 
508                 if(v3d->twflag & V3D_USE_MANIPULATOR) {
509                         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");
510                         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");
511                         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");
512                 }
513                         
514                 if (v3d->twmode > (BIF_countTransformOrientation(C) - 1) + V3D_MANIP_CUSTOM) {
515                         v3d->twmode = 0;
516                 }
517                         
518                 str_menu = BIF_menustringTransformOrientation(C, "Orientation");
519                 uiDefButS(block, MENU, B_MAN_MODE, str_menu,0,0,70,YIC, &v3d->twmode, 0, 0, 0, 0, "Transform Orientation");
520                 MEM_freeN(str_menu);
521         }
522                 
523         if(obedit==NULL && v3d->localvd==NULL) {
524                 int ob_lay = ob ? ob->lay : 0;
525                 
526                 /* Layers */
527                 if (v3d->scenelock)
528                         uiTemplateLayers(layout, &sceneptr, "layers", &v3dptr, "used_layers", ob_lay);
529                 else
530                         uiTemplateLayers(layout, &v3dptr, "layers", &v3dptr, "used_layers", ob_lay);
531
532                 /* Scene lock */
533                 uiItemR(layout, &v3dptr, "lock_camera_and_layers", UI_ITEM_R_ICON_ONLY, "", 0);
534         }
535         
536         /* selection modus, dont use python for this since it cant do the toggle buttons with shift+click as well as clicking to set one. */
537         if(obedit && (obedit->type == OB_MESH)) {
538                 EditMesh *em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
539
540                 row= uiLayoutRow(layout, 1);
541                 block= uiLayoutGetBlock(row);
542                 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");
543                 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");
544                 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");
545
546                 BKE_mesh_end_editmesh(obedit->data, em);
547         }
548 }