2.5
[blender.git] / source / blender / editors / space_view3d / space_view3d.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 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
32 #include "DNA_object_types.h"
33 #include "DNA_space_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_view3d_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_arithb.h"
42 #include "BLI_rand.h"
43
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_screen.h"
47 #include "BKE_utildefines.h"
48
49 #include "ED_space_api.h"
50 #include "ED_screen.h"
51
52 #include "BIF_gl.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "UI_interface.h"
58 #include "UI_resources.h"
59 #include "UI_view2d.h"
60
61 #include "view3d_intern.h"      // own include
62
63 /* ******************** default callbacks for view3d space ***************** */
64
65 static SpaceLink *view3d_new(const bContext *C)
66 {
67         Scene *scene= CTX_data_scene(C);
68         ARegion *ar;
69         View3D *vd;
70         
71         vd= MEM_callocN(sizeof(View3D), "initview3d");
72         vd->spacetype= SPACE_VIEW3D;
73         vd->blockscale= 0.7f;
74         vd->lay= vd->layact= 1;
75         if(scene) {
76                 vd->lay= vd->layact= scene->lay;
77                 vd->camera= scene->camera;
78         }
79         vd->scenelock= 1;
80         vd->grid= 1.0f;
81         vd->gridlines= 16;
82         vd->gridsubdiv = 10;
83         vd->drawtype= OB_WIRE;
84         
85         vd->gridflag |= V3D_SHOW_X;
86         vd->gridflag |= V3D_SHOW_Y;
87         vd->gridflag |= V3D_SHOW_FLOOR;
88         vd->gridflag &= ~V3D_SHOW_Z;
89         
90         vd->depths= NULL;
91         
92         /* XXX move view data to region? */
93         vd->viewquat[0]= 1.0f;
94         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
95         vd->persp= 1;
96         vd->view= 7;
97         vd->dist= 10.0;
98         vd->lens= 35.0f;
99         vd->near= 0.01f;
100         vd->far= 500.0f;
101         
102         /* header */
103         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
104         
105         BLI_addtail(&vd->regionbase, ar);
106         ar->regiontype= RGN_TYPE_HEADER;
107         ar->alignment= RGN_ALIGN_BOTTOM;
108         
109         /* main area */
110         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
111         
112         BLI_addtail(&vd->regionbase, ar);
113         ar->regiontype= RGN_TYPE_WINDOW;
114         
115         return (SpaceLink *)vd;
116 }
117
118 /* not spacelink itself */
119 static void view3d_free(SpaceLink *sl)
120 {
121         View3D *vd= (View3D *) sl;
122         
123         if(vd->bgpic) {
124                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
125                 MEM_freeN(vd->bgpic);
126         }
127         
128         if(vd->localvd) MEM_freeN(vd->localvd);
129         if(vd->clipbb) MEM_freeN(vd->clipbb);
130         if(vd->depths) {
131                 if(vd->depths->depths) MEM_freeN(vd->depths->depths);
132                 MEM_freeN(vd->depths);
133                 vd->depths= NULL;
134         }
135         
136 // XXX  retopo_free_view_data(vd);
137         
138         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
139         if(vd->ri) { 
140 // XXX          BIF_view3d_previewrender_free(vd);
141         }
142         
143 }
144
145
146 /* spacetype; init callback */
147 static void view3d_init(struct wmWindowManager *wm, ScrArea *sa)
148 {
149
150 }
151
152 static SpaceLink *view3d_duplicate(SpaceLink *sl)
153 {
154         View3D *v3do= (View3D *)sl;
155         View3D *v3dn= MEM_dupallocN(sl);
156         
157         /* clear or remove stuff from old */
158         
159 // XXX  BIF_view3d_previewrender_free(v3do);
160         v3do->depths= NULL;
161         v3do->retopo_view_data= NULL;
162         
163         if(v3do->localvd) {
164 // XXX          restore_localviewdata(v3do);
165                 v3do->localvd= NULL;
166                 v3do->properties_storage= NULL;
167                 v3do->localview= 0;
168                 v3do->lay &= 0xFFFFFF;
169         }
170         
171         /* copy or clear inside new stuff */
172
173         if(v3dn->bgpic) {
174                 v3dn->bgpic= MEM_dupallocN(v3dn->bgpic);
175                 if(v3dn->bgpic->ima) v3dn->bgpic->ima->id.us++;
176         }
177         v3dn->clipbb= MEM_dupallocN(v3dn->clipbb);
178         v3dn->ri= NULL;
179         v3dn->properties_storage= NULL;
180         
181         return (SpaceLink *)v3dn;
182 }
183
184 static void view3d_main_area_draw(const bContext *C, ARegion *ar)
185 {
186         /* draw entirely, view changes should be handled here */
187         ScrArea *sa= CTX_wm_area(C);
188         View3D *v3d= sa->spacedata.first;       /* XXX get from region */
189         
190         drawview3dspace(CTX_data_scene(C), ar, v3d);
191 }
192
193 /* add handlers, stuff you only do once or on area/region changes */
194 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
195 {
196         ListBase *keymap;
197         
198         /* own keymap */
199         keymap= WM_keymap_listbase(wm, "View3D", SPACE_VIEW3D, 0);
200         WM_event_add_keymap_handler(&ar->handlers, keymap);
201         
202         /* object ops. */
203         keymap= WM_keymap_listbase(wm, "Object Non-modal", 0, 0);
204         WM_event_add_keymap_handler(&ar->handlers, keymap);
205         
206         /* object modal ops default */
207         keymap= WM_keymap_listbase(wm, "Object Mode", 0, 0);
208         WM_event_add_keymap_handler(&ar->handlers, keymap);
209         
210 }
211
212 static void view3d_modal_keymaps(wmWindowManager *wm, ARegion *ar, int stype)
213 {
214         ListBase *keymap;
215         
216         keymap= WM_keymap_listbase(wm, "Object Mode", 0, 0);
217         if(stype==NS_MODE_OBJECT)
218                 WM_event_add_keymap_handler(&ar->handlers, keymap);
219         else
220                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
221         
222         
223         keymap= WM_keymap_listbase(wm, "EditMesh", 0, 0);
224         if(stype==NS_EDITMODE_MESH)
225                 WM_event_add_keymap_handler(&ar->handlers, keymap);
226         else
227                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
228         
229 }
230
231 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
232 {
233         /* context changes */
234         switch(wmn->category) {
235                 case NC_SCENE:
236                         switch(wmn->data) {
237                                 case ND_FRAME:
238                                 case ND_OB_ACTIVE:
239                                 case ND_OB_SELECT:
240                                         ED_region_tag_redraw(ar);
241                                         break;
242                                 case ND_MODE:
243                                         view3d_modal_keymaps(wmn->wm, ar, wmn->subtype);
244                                         ED_region_tag_redraw(ar);
245                                         break;
246                         }
247                         break;
248                 case NC_OBJECT:
249                         switch(wmn->data) {
250                                 case ND_BONE_ACTIVE:
251                                 case ND_BONE_SELECT:
252                                 case ND_TRANSFORM:
253                                 case ND_GEOM_SELECT:
254                                 case ND_DRAW:
255                                 case ND_MODIFIER:
256                                         ED_region_tag_redraw(ar);
257                                         break;
258                         }
259                 case NC_MATERIAL:
260                         switch(wmn->data) {
261                                 case ND_SHADING_DRAW:
262                                         ED_region_tag_redraw(ar);
263                                         break;
264                         }
265                 case NC_LAMP:
266                         switch(wmn->data) {
267                                 case ND_LIGHTING_DRAW:
268                                         ED_region_tag_redraw(ar);
269                                         break;
270                         }
271         }
272 }
273
274 /* concept is to retrieve cursor type context-less */
275 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
276 {
277         Scene *scene= win->screen->scene;
278
279         if(scene->obedit) {
280                 WM_cursor_set(win, CURSOR_EDIT);
281         }
282         else {
283                 WM_cursor_set(win, CURSOR_STD);
284         }
285 }
286
287 /* add handlers, stuff you only do once or on area/region changes */
288 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
289 {
290         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
291 }
292
293 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
294 {
295         float col[3];
296         
297         /* clear */
298         if(ED_screen_area_active(C))
299                 UI_GetThemeColor3fv(TH_HEADER, col);
300         else
301                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
302         
303         glClearColor(col[0], col[1], col[2], 0.0);
304         glClear(GL_COLOR_BUFFER_BIT);
305         
306         /* set view2d view matrix for scrolling (without scrollers) */
307         UI_view2d_view_ortho(C, &ar->v2d);
308         
309         view3d_header_buttons(C, ar);
310         
311         /* restore view matrix? */
312         UI_view2d_view_restore(C);
313 }
314
315 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
316 {
317         /* context changes */
318         switch(wmn->category) {
319                 case NC_SCENE:
320                         switch(wmn->data) {
321                                 case ND_FRAME:
322                                 case ND_OB_ACTIVE:
323                                 case ND_OB_SELECT:
324                                 case ND_MODE:
325                                         ED_region_tag_redraw(ar);
326                                         break;
327                         }
328                         break;
329         }
330 }
331
332
333 static int view3d_context(const bContext *C, const bContextDataMember *member, bContextDataResult *result)
334 {
335         View3D *v3d= (View3D*)CTX_wm_space_data(C);
336         Scene *scene= CTX_data_scene(C);
337         Base *base;
338
339         if(ELEM(member, CTX_data_selected_objects, CTX_data_selected_bases)) {
340                 for(base=scene->base.first; base; base=base->next) {
341                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
342                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
343                                         if(member == CTX_data_selected_objects)
344                                                 CTX_data_list_add(result, base->object);
345                                         else
346                                                 CTX_data_list_add(result, base);
347                                 }
348                         }
349                 }
350
351                 return 1;
352         }
353         else if(ELEM(member, CTX_data_visible_objects, CTX_data_visible_bases)) {
354                 for(base=scene->base.first; base; base=base->next) {
355                         if(base->lay & v3d->lay) {
356                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
357                                         if(member == CTX_data_selected_objects)
358                                                 CTX_data_list_add(result, base->object);
359                                         else
360                                                 CTX_data_list_add(result, base);
361                                 }
362                         }
363                 }
364                 
365                 return 1;
366         }
367         else if(member == CTX_data_active_base) {
368                 if(scene->basact && (scene->basact->lay & v3d->lay))
369                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
370                                 CTX_data_pointer_set(result, scene->basact);
371
372                 return 1;
373         }
374         else if(member == CTX_data_active_object) {
375                 if(scene->basact && (scene->basact->lay & v3d->lay))
376                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
377                                 CTX_data_pointer_set(result, scene->basact->object);
378
379                 return 1;
380         }
381
382         return 0;
383 }
384
385 /* only called once, from space/spacetypes.c */
386 void ED_spacetype_view3d(void)
387 {
388         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
389         ARegionType *art;
390         
391         st->spaceid= SPACE_VIEW3D;
392         
393         st->new= view3d_new;
394         st->free= view3d_free;
395         st->init= view3d_init;
396         st->duplicate= view3d_duplicate;
397         st->operatortypes= view3d_operatortypes;
398         st->keymap= view3d_keymap;
399         st->context= view3d_context;
400         
401         /* regions: main window */
402         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
403         art->regionid = RGN_TYPE_WINDOW;
404         art->draw= view3d_main_area_draw;
405         art->init= view3d_main_area_init;
406         art->listener= view3d_main_area_listener;
407         art->cursor= view3d_main_area_cursor;
408         BLI_addhead(&st->regiontypes, art);
409         
410         /* regions: header */
411         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
412         art->regionid = RGN_TYPE_HEADER;
413         art->minsizey= HEADERY;
414         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
415         art->listener= view3d_header_area_listener;
416         
417         art->init= view3d_header_area_init;
418         art->draw= view3d_header_area_draw;
419         
420         BLI_addhead(&st->regiontypes, art);
421         
422         BKE_spacetype_register(st);
423 }
424