doxygen: prevent GPL license block from being parsed as doxygen comment.
[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 "BLI_math.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_editVert.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_context.h"
46 #include "BKE_depsgraph.h"
47 #include "BKE_effect.h"
48 #include "BKE_main.h"
49 #include "BKE_mesh.h"
50 #include "BKE_modifier.h"
51 #include "BKE_paint.h"
52 #include "BKE_screen.h"
53
54 #include "ED_mesh.h"
55 #include "ED_util.h"
56 #include "ED_screen.h"
57 #include "ED_transform.h"
58 #include "ED_types.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "RNA_define.h"
64 #include "RNA_enum_types.h"
65
66 #include "UI_interface.h"
67 #include "UI_resources.h"
68
69 #include "view3d_intern.h"
70
71
72 /* View3d->modeselect 
73  * This is a bit of a dodgy hack to enable a 'mode' menu with icons+labels
74  * rather than those buttons.
75  * I know the implementation's not good - it's an experiment to see if this
76  * approach would work well
77  *
78  * This can be cleaned when I make some new 'mode' icons.
79  */
80
81 #define TEST_EDITMESH   if(obedit==0) return; \
82                                                 if( (v3d->lay & obedit->lay)==0 ) return;
83
84 /* view3d handler codes */
85 #define VIEW3D_HANDLER_BACKGROUND       1
86 #define VIEW3D_HANDLER_PROPERTIES       2
87 #define VIEW3D_HANDLER_OBJECT           3
88 #define VIEW3D_HANDLER_PREVIEW          4
89 #define VIEW3D_HANDLER_MULTIRES         5
90 #define VIEW3D_HANDLER_TRANSFORM        6
91 #define VIEW3D_HANDLER_GREASEPENCIL 7
92 #define VIEW3D_HANDLER_BONESKETCH       8
93
94 /* end XXX ************* */
95
96 static void do_view3d_header_buttons(bContext *C, void *arg, int event);
97
98 #define B_SCENELOCK 101
99 #define B_FULL          102
100 #define B_HOME          103
101 #define B_VIEWBUT       104
102 #define B_PERSP         105
103 #define B_MODESELECT 108
104 #define B_SEL_VERT      110
105 #define B_SEL_EDGE      111
106 #define B_SEL_FACE      112
107 #define B_MAN_TRANS     116
108 #define B_MAN_ROT       117
109 #define B_MAN_SCALE     118
110 #define B_NDOF          119     
111 #define B_MAN_MODE      120
112 #define B_REDR          122
113 #define B_NOP           123
114
115 // XXX quickly ported across
116 static void handle_view3d_lock(bContext *C) 
117 {
118         Main *bmain= CTX_data_main(C);
119         Scene *scene= CTX_data_scene(C);
120         ScrArea *sa= CTX_wm_area(C);
121         View3D *v3d= CTX_wm_view3d(C);
122         
123         if (v3d != NULL && sa != NULL) {
124                 if(v3d->localvd==NULL && v3d->scenelock && sa->spacetype==SPACE_VIEW3D) {
125                         /* copy to scene */
126                         scene->lay= v3d->lay;
127                         scene->layact= v3d->layact;
128                         scene->camera= v3d->camera;
129
130                         /* not through notifiery, listener don't have context
131                            and non-open screens or spaces need to be updated too */
132                         BKE_screen_view3d_main_sync(&bmain->screen, scene);
133                         
134                         /* notifiers for scene update */
135                         WM_event_add_notifier(C, NC_SCENE|ND_LAYER, scene);
136                 }
137         }
138 }
139
140 static int view3d_layers_exec(bContext *C, wmOperator *op)
141 {
142         Scene *scene= CTX_data_scene(C);
143         ScrArea *sa= CTX_wm_area(C);
144         View3D *v3d= sa->spacedata.first;
145         Base *base;
146         int oldlay= v3d->lay;
147         int nr= RNA_int_get(op->ptr, "nr");
148         int toggle= RNA_boolean_get(op->ptr, "toggle");
149         
150         if(nr < 0)
151                 return OPERATOR_CANCELLED;
152
153         if(nr == 0) {
154                 /* all layers */
155                 if(!v3d->layact)
156                         v3d->layact= 1;
157
158                 if (toggle && v3d->lay == ((1<<20)-1)) {
159                         /* return to active layer only */
160                         v3d->lay = v3d->layact;
161                 } else {
162                         v3d->lay |= (1<<20)-1;
163                 }               
164         }
165         else {
166                 int bit;
167                 nr--;
168
169                 if(RNA_boolean_get(op->ptr, "extend")) {
170                         if(toggle && v3d->lay & (1<<nr) && (v3d->lay & ~(1<<nr)))
171                                 v3d->lay &= ~(1<<nr);
172                         else
173                                 v3d->lay |= (1<<nr);
174                 } else {
175                         v3d->lay = (1<<nr);
176
177                         /* sanity check - when in editmode disallow switching the editmode layer off since its confusing
178                          * an alternative would be to always draw the editmode object. */
179                         if(scene->obedit && (scene->obedit->lay & v3d->lay)==0) {
180                                 for(bit=0; bit<32; bit++) {
181                                         if(scene->obedit->lay & (1<<bit)) {
182                                                 v3d->lay |= 1<<bit;
183                                                 break;
184                                         }
185                                 }
186                         }
187                 }
188                 
189                 /* set active layer, ensure to always have one */
190                 if(v3d->lay & (1<<nr))
191                    v3d->layact= 1<<nr;
192                 else if((v3d->lay & v3d->layact)==0) {
193                         for(bit=0; bit<32; bit++) {
194                                 if(v3d->lay & (1<<bit)) {
195                                         v3d->layact= 1<<bit;
196                                         break;
197                                 }
198                         }
199                 }
200         }
201         
202         if(v3d->scenelock) handle_view3d_lock(C);
203         
204         /* XXX new layers might need updates, there is no provision yet to detect if that's needed */
205         oldlay= ~oldlay & v3d->lay;
206         for (base= scene->base.first; base; base= base->next) {
207                 if(base->lay & oldlay)
208                         base->object->recalc= OB_RECALC_OB|OB_RECALC_DATA;
209         }
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 view3d_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         view3d_layers_exec(C, op);
231         
232         return OPERATOR_FINISHED;
233 }
234
235 static int view3d_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= view3d_layers_invoke;
249         ot->exec= view3d_layers_exec;
250         ot->poll= view3d_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 *UNUSED(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         uiBlockBeginAlign(block);
453         uiDefIconTextButS(block, MENU, B_MODESELECT, object_mode_icon(v3d->modeselect), view3d_modeselect_pup(scene) , 
454                           0,0,126,20, &(v3d->modeselect), 0, 0, 0, 0, "Mode");
455         uiBlockEndAlign(block);
456         
457         /* Draw type */
458         uiItemR(layout, &v3dptr, "viewport_shade", UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
459
460         if (obedit==NULL && ((ob && ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)))) {
461                 /* Manipulators aren't used in weight paint mode */
462                 
463                 PointerRNA meshptr;
464
465                 RNA_pointer_create(&ob->id, &RNA_Mesh, ob->data, &meshptr);
466                 uiItemR(layout, &meshptr, "use_paint_mask", UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
467         } else {
468                 const char *str_menu;
469
470                 row= uiLayoutRow(layout, 1);
471                 uiItemR(row, &v3dptr, "pivot_point", UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
472                 uiItemR(row, &v3dptr, "use_pivot_point_align", UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
473
474                 /* NDOF */
475                 /* Not implemented yet
476                  if (G.ndofdevice ==0 ) {
477                         uiDefIconTextButC(block, ICONTEXTROW,B_NDOF, ICON_NDOF_TURN, ndof_pup(), 0,0,XIC+10,YIC, &(v3d->ndofmode), 0, 3.0, 0, 0, "Ndof mode");
478                 
479                         uiDefIconButC(block, TOG, B_NDOF,  ICON_NDOF_DOM,
480                                           0,0,XIC,YIC,
481                                           &v3d->ndoffilter, 0, 1, 0, 0, "dominant axis");       
482                 }
483                  */
484
485                 /* Transform widget / manipulators */
486                 row= uiLayoutRow(layout, 1);
487                 uiItemR(row, &v3dptr, "show_manipulator", UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
488                 block= uiLayoutGetBlock(row);
489                 
490                 if(v3d->twflag & V3D_USE_MANIPULATOR) {
491                         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");
492                         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");
493                         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");
494                 }
495                         
496                 if (v3d->twmode > (BIF_countTransformOrientation(C) - 1) + V3D_MANIP_CUSTOM) {
497                         v3d->twmode = 0;
498                 }
499                         
500                 str_menu = BIF_menustringTransformOrientation(C, "Orientation");
501                 uiDefButS(block, MENU, B_MAN_MODE, str_menu,0,0,70,YIC, &v3d->twmode, 0, 0, 0, 0, "Transform Orientation");
502                 MEM_freeN((void *)str_menu);
503         }
504                 
505         if(obedit==NULL && v3d->localvd==NULL) {
506                 unsigned int ob_lay = ob ? ob->lay : 0;
507                 
508                 /* Layers */
509                 if (v3d->scenelock)
510                         uiTemplateLayers(layout, &sceneptr, "layers", &v3dptr, "layers_used", ob_lay);
511                 else
512                         uiTemplateLayers(layout, &v3dptr, "layers", &v3dptr, "layers_used", ob_lay);
513
514                 /* Scene lock */
515                 uiItemR(layout, &v3dptr, "lock_camera_and_layers", UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
516         }
517         
518         /* selection modus, dont use python for this since it cant do the toggle buttons with shift+click as well as clicking to set one. */
519         if(obedit && (obedit->type == OB_MESH)) {
520                 EditMesh *em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
521
522                 row= uiLayoutRow(layout, 1);
523                 block= uiLayoutGetBlock(row);
524                 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");
525                 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");
526                 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");
527
528                 BKE_mesh_end_editmesh(obedit->data, em);
529         }
530 }