1bc770ea8a6b5b4f3d9090eb99061686921c2b16
[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_action_types.h"
33 #include "DNA_armature_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_view3d_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_arithb.h"
44 #include "BLI_rand.h"
45
46 #include "BKE_context.h"
47 #include "BKE_global.h"
48 #include "BKE_screen.h"
49 #include "BKE_utildefines.h"
50
51 #include "ED_armature.h"
52 #include "ED_space_api.h"
53 #include "ED_screen.h"
54
55 #include "BIF_gl.h"
56
57 #include "WM_api.h"
58 #include "WM_types.h"
59
60 #include "UI_interface.h"
61 #include "UI_resources.h"
62 #include "UI_view2d.h"
63
64 #include "view3d_intern.h"      // own include
65
66 /* ******************** default callbacks for view3d space ***************** */
67
68 static SpaceLink *view3d_new(const bContext *C)
69 {
70         Scene *scene= CTX_data_scene(C);
71         ARegion *ar;
72         View3D *v3d;
73         RegionView3D *rv3d;
74         
75         v3d= MEM_callocN(sizeof(View3D), "initview3d");
76         v3d->spacetype= SPACE_VIEW3D;
77         v3d->blockscale= 0.7f;
78         v3d->lay= v3d->layact= 1;
79         if(scene) {
80                 v3d->lay= v3d->layact= scene->lay;
81                 v3d->camera= scene->camera;
82         }
83         v3d->scenelock= 1;
84         v3d->grid= 1.0f;
85         v3d->gridlines= 16;
86         v3d->gridsubdiv = 10;
87         v3d->drawtype= OB_WIRE;
88         
89         v3d->gridflag |= V3D_SHOW_X;
90         v3d->gridflag |= V3D_SHOW_Y;
91         v3d->gridflag |= V3D_SHOW_FLOOR;
92         v3d->gridflag &= ~V3D_SHOW_Z;
93         
94         v3d->lens= 35.0f;
95         v3d->near= 0.01f;
96         v3d->far= 500.0f;
97         
98         /* header */
99         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
100         
101         BLI_addtail(&v3d->regionbase, ar);
102         ar->regiontype= RGN_TYPE_HEADER;
103         ar->alignment= RGN_ALIGN_BOTTOM;
104         
105         /* main area */
106         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
107         
108         BLI_addtail(&v3d->regionbase, ar);
109         ar->regiontype= RGN_TYPE_WINDOW;
110         
111         ar->regiondata= MEM_callocN(sizeof(RegionView3D), "region view3d");
112         rv3d= ar->regiondata;
113         rv3d->viewquat[0]= 1.0f;
114         rv3d->persp= 1;
115         rv3d->view= 7;
116         rv3d->dist= 10.0;
117         Mat4One(rv3d->twmat);
118         
119         return (SpaceLink *)v3d;
120 }
121
122 /* not spacelink itself */
123 static void view3d_free(SpaceLink *sl)
124 {
125         View3D *vd= (View3D *) sl;
126         
127         if(vd->bgpic) {
128                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
129                 MEM_freeN(vd->bgpic);
130         }
131         
132         if(vd->localvd) MEM_freeN(vd->localvd);
133         
134         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
135         
136 }
137
138
139 /* spacetype; init callback */
140 static void view3d_init(struct wmWindowManager *wm, ScrArea *sa)
141 {
142
143 }
144
145 static SpaceLink *view3d_duplicate(SpaceLink *sl)
146 {
147         View3D *v3do= (View3D *)sl;
148         View3D *v3dn= MEM_dupallocN(sl);
149         
150         /* clear or remove stuff from old */
151         
152 // XXX  BIF_view3d_previewrender_free(v3do);
153         
154         if(v3do->localvd) {
155 // XXX          restore_localviewdata(v3do);
156                 v3do->localvd= NULL;
157                 v3do->properties_storage= NULL;
158                 v3do->localview= 0;
159                 v3do->lay &= 0xFFFFFF;
160         }
161         
162         /* copy or clear inside new stuff */
163
164         if(v3dn->bgpic) {
165                 v3dn->bgpic= MEM_dupallocN(v3dn->bgpic);
166                 if(v3dn->bgpic->ima) v3dn->bgpic->ima->id.us++;
167         }
168         v3dn->properties_storage= NULL;
169         
170         return (SpaceLink *)v3dn;
171 }
172
173 /* add handlers, stuff you only do once or on area/region changes */
174 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
175 {
176         ListBase *keymap;
177         
178         /* own keymap */
179         keymap= WM_keymap_listbase(wm, "View3D", SPACE_VIEW3D, 0);
180         WM_event_add_keymap_handler(&ar->handlers, keymap);
181         
182         /* object ops. */
183         keymap= WM_keymap_listbase(wm, "Object Non-modal", 0, 0);
184         WM_event_add_keymap_handler(&ar->handlers, keymap);
185         
186         /* pose is not modal, operator poll checks for this */
187         keymap= WM_keymap_listbase(wm, "Pose", 0, 0);
188         WM_event_add_keymap_handler(&ar->handlers, keymap);
189         
190         /* object modal ops default */
191         keymap= WM_keymap_listbase(wm, "Object Mode", 0, 0);
192         WM_event_add_keymap_handler(&ar->handlers, keymap);
193         
194 }
195
196 /* type callback, not region itself */
197 static void view3d_main_area_free(ARegion *ar)
198 {
199         RegionView3D *rv3d= ar->regiondata;
200         
201         if(rv3d) {
202                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
203                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
204
205                 // XXX  retopo_free_view_data(rv3d);
206                 if(rv3d->ri) { 
207                         // XXX          BIF_view3d_previewrender_free(rv3d);
208                 }
209                 
210                 if(rv3d->depths) {
211                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
212                         MEM_freeN(rv3d->depths);
213                 }
214                 MEM_freeN(rv3d);
215                 ar->regiondata= NULL;
216         }
217 }
218
219 /* copy regiondata */
220 static void *view3d_main_area_duplicate(void *poin)
221 {
222         if(poin) {
223                 RegionView3D *rv3d= poin, *new;
224         
225                 new= MEM_dupallocN(rv3d);
226                 if(rv3d->localvd) 
227                         new->localvd= MEM_dupallocN(rv3d->localvd);
228                 if(rv3d->clipbb) 
229                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
230                 
231                 new->depths= NULL;
232                 new->retopo_view_data= NULL;
233                 new->ri= NULL;
234                 new->gpd= NULL;
235                 new->sms= NULL;
236                 new->smooth_timer= NULL;
237                 
238                 return new;
239         }
240         return NULL;
241 }
242
243
244 static void view3d_modal_keymaps(wmWindowManager *wm, ARegion *ar, int stype)
245 {
246         ListBase *keymap;
247         
248         keymap= WM_keymap_listbase(wm, "Object Mode", 0, 0);
249         if(stype==NS_MODE_OBJECT)
250                 WM_event_add_keymap_handler(&ar->handlers, keymap);
251         else
252                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
253         
254         keymap= WM_keymap_listbase(wm, "EditMesh", 0, 0);
255         if(stype==NS_EDITMODE_MESH)
256                 WM_event_add_keymap_handler(&ar->handlers, keymap);
257         else
258                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
259
260         keymap= WM_keymap_listbase(wm, "Curve", 0, 0);
261         if(stype==NS_EDITMODE_CURVE)
262                 WM_event_add_keymap_handler(&ar->handlers, keymap);
263         else
264                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
265         
266         keymap= WM_keymap_listbase(wm, "Armature", 0, 0);
267         if(stype==NS_EDITMODE_ARMATURE)
268                 WM_event_add_keymap_handler(&ar->handlers, keymap);
269         else
270                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
271         
272         /* editfont keymap swallows all... */
273         keymap= WM_keymap_listbase(wm, "Font", 0, 0);
274         if(stype==NS_EDITMODE_TEXT)
275                 WM_event_add_keymap_handler_priority(&ar->handlers, keymap, 10);
276         else
277                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
278         
279 }
280
281 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
282 {
283         /* context changes */
284         switch(wmn->category) {
285                 case NC_SCENE:
286                         switch(wmn->data) {
287                                 case ND_FRAME:
288                                 case ND_OB_ACTIVE:
289                                 case ND_OB_SELECT:
290                                         ED_region_tag_redraw(ar);
291                                         break;
292                                 case ND_MODE:
293                                         view3d_modal_keymaps(wmn->wm, ar, wmn->subtype);
294                                         ED_region_tag_redraw(ar);
295                                         break;
296                         }
297                         break;
298                 case NC_OBJECT:
299                         switch(wmn->data) {
300                                 case ND_BONE_ACTIVE:
301                                 case ND_BONE_SELECT:
302                                 case ND_TRANSFORM:
303                                 case ND_GEOM_SELECT:
304                                 case ND_DRAW:
305                                 case ND_MODIFIER:
306                                 case ND_KEYS:
307                                         ED_region_tag_redraw(ar);
308                                         break;
309                         }
310                 case NC_GROUP:
311                         /* all group ops for now */
312                         ED_region_tag_redraw(ar);
313                         break;
314                 case NC_MATERIAL:
315                         switch(wmn->data) {
316                                 case ND_SHADING_DRAW:
317                                         ED_region_tag_redraw(ar);
318                                         break;
319                         }
320                 case NC_LAMP:
321                         switch(wmn->data) {
322                                 case ND_LIGHTING_DRAW:
323                                         ED_region_tag_redraw(ar);
324                                         break;
325                         }
326         }
327 }
328
329 /* concept is to retrieve cursor type context-less */
330 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
331 {
332         Scene *scene= win->screen->scene;
333
334         if(scene->obedit) {
335                 WM_cursor_set(win, CURSOR_EDIT);
336         }
337         else {
338                 WM_cursor_set(win, CURSOR_STD);
339         }
340 }
341
342 /* add handlers, stuff you only do once or on area/region changes */
343 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
344 {
345         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
346 }
347
348 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
349 {
350         float col[3];
351         
352         /* clear */
353         if(ED_screen_area_active(C))
354                 UI_GetThemeColor3fv(TH_HEADER, col);
355         else
356                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
357         
358         glClearColor(col[0], col[1], col[2], 0.0);
359         glClear(GL_COLOR_BUFFER_BIT);
360         
361         /* set view2d view matrix for scrolling (without scrollers) */
362         UI_view2d_view_ortho(C, &ar->v2d);
363         
364         view3d_header_buttons(C, ar);
365         
366         /* restore view matrix? */
367         UI_view2d_view_restore(C);
368 }
369
370 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
371 {
372         /* context changes */
373         switch(wmn->category) {
374                 case NC_SCENE:
375                         switch(wmn->data) {
376                                 case ND_FRAME:
377                                 case ND_OB_ACTIVE:
378                                 case ND_OB_SELECT:
379                                 case ND_MODE:
380                                         ED_region_tag_redraw(ar);
381                                         break;
382                         }
383                         break;
384         }
385 }
386
387 /*
388  * Returns true if the Object is a from an external blend file (libdata)
389  */
390 static int object_is_libdata(Object *ob)
391 {
392         if (!ob) return 0;
393         if (ob->proxy) return 0;
394         if (ob->id.lib) return 1;
395         return 0;
396 }
397
398 static int view3d_context(const bContext *C, bContextDataMember member, bContextDataResult *result)
399 {
400         View3D *v3d= CTX_wm_view3d(C);
401         Scene *scene= CTX_data_scene(C);
402         Base *base;
403
404         if(v3d==NULL) return 0;
405
406         if(ELEM(member, CTX_DATA_SELECTED_OBJECTS, CTX_DATA_SELECTED_BASES)) {
407                 for(base=scene->base.first; base; base=base->next) {
408                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
409                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
410                                         if(member == CTX_DATA_SELECTED_OBJECTS)
411                                                 CTX_data_list_add(result, base->object);
412                                         else
413                                                 CTX_data_list_add(result, base);
414                                 }
415                         }
416                 }
417
418                 return 1;
419         }
420         else if(ELEM(member, CTX_DATA_SELECTED_EDITABLE_OBJECTS, CTX_DATA_SELECTED_EDITABLE_BASES)) {
421                 for(base=scene->base.first; base; base=base->next) {
422                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
423                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
424                                         if(0==object_is_libdata(base->object)) {
425                                                 if(member == CTX_DATA_SELECTED_EDITABLE_OBJECTS)
426                                                         CTX_data_list_add(result, base->object);
427                                                 else
428                                                         CTX_data_list_add(result, base);
429                                         }
430                                 }
431                         }
432                 }
433                 
434                 return 1;
435         }
436         else if(ELEM(member, CTX_DATA_VISIBLE_OBJECTS, CTX_DATA_VISIBLE_BASES)) {
437                 for(base=scene->base.first; base; base=base->next) {
438                         if(base->lay & v3d->lay) {
439                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
440                                         if(member == CTX_DATA_VISIBLE_OBJECTS)
441                                                 CTX_data_list_add(result, base->object);
442                                         else
443                                                 CTX_data_list_add(result, base);
444                                 }
445                         }
446                 }
447                 
448                 return 1;
449         }
450         else if(member == CTX_DATA_ACTIVE_BASE) {
451                 if(scene->basact && (scene->basact->lay & v3d->lay))
452                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
453                                 CTX_data_pointer_set(result, scene->basact);
454
455                 return 1;
456         }
457         else if(member == CTX_DATA_ACTIVE_OBJECT) {
458                 if(scene->basact && (scene->basact->lay & v3d->lay))
459                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
460                                 CTX_data_pointer_set(result, scene->basact->object);
461
462                 return 1;
463         }
464         else if(ELEM(member, CTX_DATA_SELECTED_BONES, CTX_DATA_SELECTED_EDITABLE_BONES)) {
465                 Object *obedit= scene->obedit; // XXX get from context?
466                 bArmature *arm= (obedit) ? obedit->data : NULL;
467                 EditBone *ebone, *flipbone=NULL;
468                 
469                 if (arm && arm->edbo) {
470                         /* Attention: X-Axis Mirroring is also handled here... */
471                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
472                                 /* first and foremost, bone must be visible and selected */
473                                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
474                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
475                                          * so that most users of this data don't need to explicitly check for it themselves.
476                                          * 
477                                          * We need to make sure that these mirrored copies are not selected, otherwise some
478                                          * bones will be operated on twice.
479                                          */
480                                         if (arm->flag & ARM_MIRROR_EDIT)
481                                                 flipbone = armature_bone_get_mirrored(arm->edbo, ebone);
482                                         
483                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
484                                         if (member == CTX_DATA_SELECTED_EDITABLE_BONES) {
485                                                 /* only selected + editable */
486                                                 if (EBONE_EDITABLE(ebone)) {
487                                                         CTX_data_list_add(result, ebone);
488                                                 
489                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
490                                                                 CTX_data_list_add(result, flipbone);
491                                                 }
492                                         }
493                                         else {
494                                                 /* only include bones if selected */
495                                                 CTX_data_list_add(result, ebone);
496                                                 
497                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
498                                                         CTX_data_list_add(result, flipbone);
499                                         }
500                                 }
501                         }       
502                         
503                         return 1;
504                 }
505         }
506         else if(member == CTX_DATA_SELECTED_PCHANS) {
507                 Object *obact= OBACT;
508                 bArmature *arm= (obact) ? obact->data : NULL;
509                 bPoseChannel *pchan;
510                 
511                 if (obact && arm) {
512                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
513                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
514                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
515                                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) 
516                                                 CTX_data_list_add(result, pchan);
517                                 }
518                         }
519                         
520                         return 1;
521                 }
522         }
523         else if(member == CTX_DATA_ACTIVE_BONE) {
524                 Object *obedit= scene->obedit; // XXX get from context?
525                 bArmature *arm= (obedit) ? obedit->data : NULL;
526                 EditBone *ebone;
527                 
528                 if (arm && arm->edbo) {
529                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
530                                 if (EBONE_VISIBLE(arm, ebone)) {
531                                         if (ebone->flag & BONE_ACTIVE) {
532                                                 CTX_data_pointer_set(result, ebone);
533                                                 
534                                                 return 1;
535                                         }
536                                 }
537                         }
538                 }
539                 
540         }
541         else if(member == CTX_DATA_ACTIVE_PCHAN) {
542                 Object *obact= OBACT;
543                 bArmature *arm= (obact) ? obact->data : NULL;
544                 bPoseChannel *pchan;
545                 
546                 pchan= get_active_posechannel(obact);
547                 if (pchan) {
548                         CTX_data_pointer_set(result, pchan);
549                         return 1;
550                 }
551         }
552
553         return 0;
554 }
555
556 /* only called once, from space/spacetypes.c */
557 void ED_spacetype_view3d(void)
558 {
559         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
560         ARegionType *art;
561         
562         st->spaceid= SPACE_VIEW3D;
563         
564         st->new= view3d_new;
565         st->free= view3d_free;
566         st->init= view3d_init;
567         st->duplicate= view3d_duplicate;
568         st->operatortypes= view3d_operatortypes;
569         st->keymap= view3d_keymap;
570         st->context= view3d_context;
571         
572         /* regions: main window */
573         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
574         art->regionid = RGN_TYPE_WINDOW;
575         art->keymapflag= ED_KEYMAP_FRAMES;
576         art->draw= view3d_main_area_draw;
577         art->init= view3d_main_area_init;
578         art->free= view3d_main_area_free;
579         art->duplicate= view3d_main_area_duplicate;
580         art->listener= view3d_main_area_listener;
581         art->cursor= view3d_main_area_cursor;
582         BLI_addhead(&st->regiontypes, art);
583         
584         /* regions: header */
585         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
586         art->regionid = RGN_TYPE_HEADER;
587         art->minsizey= HEADERY;
588         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
589         art->listener= view3d_header_area_listener;
590         
591         art->init= view3d_header_area_init;
592         art->draw= view3d_header_area_draw;
593         
594         BLI_addhead(&st->regiontypes, art);
595         
596         BKE_spacetype_register(st);
597 }
598