Camera tracking: libmv distortion API now also uses camera intrinsics
[blender.git] / source / blender / editors / space_view3d / space_view3d.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/space_view3d.c
28  *  \ingroup spview3d
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_material_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_rand.h"
44 #include "BLI_utildefines.h"
45
46 #include "BKE_context.h"
47 #include "BKE_icons.h"
48 #include "BKE_object.h"
49 #include "BKE_screen.h"
50
51 #include "ED_space_api.h"
52 #include "ED_screen.h"
53 #include "ED_object.h"
54
55 #include "GPU_material.h"
56
57 #include "BIF_gl.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "RE_engine.h"
63
64 #include "RNA_access.h"
65
66 #include "UI_resources.h"
67
68 #include "view3d_intern.h"  /* own include */
69
70 /* ******************** manage regions ********************* */
71
72 ARegion *view3d_has_buttons_region(ScrArea *sa)
73 {
74         ARegion *ar, *arnew;
75
76         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
77         if (ar) return ar;
78         
79         /* add subdiv level; after header */
80         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
81
82         /* is error! */
83         if (ar == NULL) return NULL;
84         
85         arnew = MEM_callocN(sizeof(ARegion), "buttons for view3d");
86         
87         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
88         arnew->regiontype = RGN_TYPE_UI;
89         arnew->alignment = RGN_ALIGN_RIGHT;
90         
91         arnew->flag = RGN_FLAG_HIDDEN;
92         
93         return arnew;
94 }
95
96 ARegion *view3d_has_tools_region(ScrArea *sa)
97 {
98         ARegion *ar, *artool = NULL, *arprops = NULL, *arhead;
99         
100         for (ar = sa->regionbase.first; ar; ar = ar->next) {
101                 if (ar->regiontype == RGN_TYPE_TOOLS)
102                         artool = ar;
103                 if (ar->regiontype == RGN_TYPE_TOOL_PROPS)
104                         arprops = ar;
105         }
106         
107         /* tool region hide/unhide also hides props */
108         if (arprops && artool) return artool;
109         
110         if (artool == NULL) {
111                 /* add subdiv level; after header */
112                 for (arhead = sa->regionbase.first; arhead; arhead = arhead->next)
113                         if (arhead->regiontype == RGN_TYPE_HEADER)
114                                 break;
115                 
116                 /* is error! */
117                 if (arhead == NULL) return NULL;
118                 
119                 artool = MEM_callocN(sizeof(ARegion), "tools for view3d");
120                 
121                 BLI_insertlinkafter(&sa->regionbase, arhead, artool);
122                 artool->regiontype = RGN_TYPE_TOOLS;
123                 artool->alignment = RGN_ALIGN_LEFT;
124                 artool->flag = RGN_FLAG_HIDDEN;
125         }
126
127         if (arprops == NULL) {
128                 /* add extra subdivided region for tool properties */
129                 arprops = MEM_callocN(sizeof(ARegion), "tool props for view3d");
130                 
131                 BLI_insertlinkafter(&sa->regionbase, artool, arprops);
132                 arprops->regiontype = RGN_TYPE_TOOL_PROPS;
133                 arprops->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
134         }
135         
136         return artool;
137 }
138
139 /* ****************************************************** */
140
141 /* function to always find a regionview3d context inside 3D window */
142 RegionView3D *ED_view3d_context_rv3d(bContext *C)
143 {
144         RegionView3D *rv3d = CTX_wm_region_view3d(C);
145         
146         if (rv3d == NULL) {
147                 ScrArea *sa = CTX_wm_area(C);
148                 if (sa && sa->spacetype == SPACE_VIEW3D) {
149                         ARegion *ar = BKE_area_find_region_active_win(sa);
150                         if (ar) {
151                                 rv3d = ar->regiondata;
152                         }
153                 }
154         }
155         return rv3d;
156 }
157
158 /* ideally would return an rv3d but in some cases the region is needed too
159  * so return that, the caller can then access the ar->regiondata */
160 int ED_view3d_context_user_region(bContext *C, View3D **v3d_r, ARegion **ar_r)
161 {
162         ScrArea *sa = CTX_wm_area(C);
163
164         *v3d_r = NULL;
165         *ar_r = NULL;
166
167         if (sa && sa->spacetype == SPACE_VIEW3D) {
168                 ARegion *ar = CTX_wm_region(C);
169                 View3D *v3d = (View3D *)sa->spacedata.first;
170
171                 if (ar) {
172                         RegionView3D *rv3d = ar->regiondata;
173                         if (rv3d && rv3d->viewlock == 0) {
174                                 *v3d_r = v3d;
175                                 *ar_r = ar;
176                                 return 1;
177                         }
178                         else {
179                                 ARegion *ar_unlock_user = NULL;
180                                 ARegion *ar_unlock = NULL;
181                                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
182                                         /* find the first unlocked rv3d */
183                                         if (ar->regiondata && ar->regiontype == RGN_TYPE_WINDOW) {
184                                                 rv3d = ar->regiondata;
185                                                 if (rv3d->viewlock == 0) {
186                                                         ar_unlock = ar;
187                                                         if (rv3d->persp == RV3D_PERSP || rv3d->persp == RV3D_CAMOB) {
188                                                                 ar_unlock_user = ar;
189                                                                 break;
190                                                         }
191                                                 }
192                                         }
193                                 }
194
195                                 /* camera/perspective view get priority when the active region is locked */
196                                 if (ar_unlock_user) {
197                                         *v3d_r = v3d;
198                                         *ar_r = ar_unlock_user;
199                                         return 1;
200                                 }
201
202                                 if (ar_unlock) {
203                                         *v3d_r = v3d;
204                                         *ar_r = ar_unlock;
205                                         return 1;
206                                 }
207                         }
208                 }
209         }
210
211         return 0;
212 }
213
214 /* Most of the time this isn't needed since you could assume the view matrix was
215  * set while drawing, however when functions like mesh_foreachScreenVert are
216  * called by selection tools, we can't be sure this object was the last.
217  *
218  * for example, transparent objects are drawn after editmode and will cause
219  * the rv3d mat's to change and break selection.
220  *
221  * 'ED_view3d_init_mats_rv3d' should be called before
222  * view3d_project_short_clip and view3d_project_short_noclip in cases where
223  * these functions are not used during draw_object
224  */
225 void ED_view3d_init_mats_rv3d(struct Object *ob, struct RegionView3D *rv3d)
226 {
227         /* local viewmat and persmat, to calculate projections */
228         mult_m4_m4m4(rv3d->viewmatob, rv3d->viewmat, ob->obmat);
229         mult_m4_m4m4(rv3d->persmatob, rv3d->persmat, ob->obmat);
230
231         /* initializes object space clipping, speeds up clip tests */
232         ED_view3d_clipping_local(rv3d, ob->obmat);
233 }
234
235 void ED_view3d_init_mats_rv3d_gl(struct Object *ob, struct RegionView3D *rv3d)
236 {
237         ED_view3d_init_mats_rv3d(ob, rv3d);
238
239         /* we have to multiply instead of loading viewmatob to make
240          * it work with duplis using displists, otherwise it will
241          * override the dupli-matrix */
242         glMultMatrixf(ob->obmat);
243 }
244
245 /* ******************** default callbacks for view3d space ***************** */
246
247 static SpaceLink *view3d_new(const bContext *C)
248 {
249         Scene *scene = CTX_data_scene(C);
250         ARegion *ar;
251         View3D *v3d;
252         RegionView3D *rv3d;
253         
254         v3d = MEM_callocN(sizeof(View3D), "initview3d");
255         v3d->spacetype = SPACE_VIEW3D;
256         v3d->blockscale = 0.7f;
257         v3d->lay = v3d->layact = 1;
258         if (scene) {
259                 v3d->lay = v3d->layact = scene->lay;
260                 v3d->camera = scene->camera;
261         }
262         v3d->scenelock = TRUE;
263         v3d->grid = 1.0f;
264         v3d->gridlines = 16;
265         v3d->gridsubdiv = 10;
266         v3d->drawtype = OB_SOLID;
267
268         v3d->gridflag = V3D_SHOW_X | V3D_SHOW_Y | V3D_SHOW_FLOOR;
269         
270         v3d->flag = V3D_SELECT_OUTLINE;
271         v3d->flag2 = V3D_SHOW_RECONSTRUCTION | V3D_SHOW_GPENCIL;
272         
273         v3d->lens = 35.0f;
274         v3d->near = 0.01f;
275         v3d->far = 1000.0f;
276
277         v3d->twflag |= U.tw_flag & V3D_USE_MANIPULATOR;
278         v3d->twtype = V3D_MANIP_TRANSLATE;
279         v3d->around = V3D_CENTROID;
280         
281         v3d->bundle_size = 0.2f;
282         v3d->bundle_drawtype = OB_PLAINAXES;
283         
284         /* header */
285         ar = MEM_callocN(sizeof(ARegion), "header for view3d");
286         
287         BLI_addtail(&v3d->regionbase, ar);
288         ar->regiontype = RGN_TYPE_HEADER;
289         ar->alignment = RGN_ALIGN_BOTTOM;
290         
291         /* tool shelf */
292         ar = MEM_callocN(sizeof(ARegion), "toolshelf for view3d");
293         
294         BLI_addtail(&v3d->regionbase, ar);
295         ar->regiontype = RGN_TYPE_TOOLS;
296         ar->alignment = RGN_ALIGN_LEFT;
297         ar->flag = RGN_FLAG_HIDDEN;
298         
299         /* tool properties */
300         ar = MEM_callocN(sizeof(ARegion), "tool properties for view3d");
301         
302         BLI_addtail(&v3d->regionbase, ar);
303         ar->regiontype = RGN_TYPE_TOOL_PROPS;
304         ar->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
305         ar->flag = RGN_FLAG_HIDDEN;
306         
307         /* buttons/list view */
308         ar = MEM_callocN(sizeof(ARegion), "buttons for view3d");
309         
310         BLI_addtail(&v3d->regionbase, ar);
311         ar->regiontype = RGN_TYPE_UI;
312         ar->alignment = RGN_ALIGN_RIGHT;
313         ar->flag = RGN_FLAG_HIDDEN;
314         
315         /* main area */
316         ar = MEM_callocN(sizeof(ARegion), "main area for view3d");
317         
318         BLI_addtail(&v3d->regionbase, ar);
319         ar->regiontype = RGN_TYPE_WINDOW;
320         
321         ar->regiondata = MEM_callocN(sizeof(RegionView3D), "region view3d");
322         rv3d = ar->regiondata;
323         rv3d->viewquat[0] = 1.0f;
324         rv3d->persp = RV3D_PERSP;
325         rv3d->view = RV3D_VIEW_PERSPORTHO;
326         rv3d->dist = 10.0;
327         
328         return (SpaceLink *)v3d;
329 }
330
331 /* not spacelink itself */
332 static void view3d_free(SpaceLink *sl)
333 {
334         View3D *vd = (View3D *) sl;
335
336         BLI_freelistN(&vd->bgpicbase);
337
338         if (vd->localvd) MEM_freeN(vd->localvd);
339         
340         if (vd->properties_storage) MEM_freeN(vd->properties_storage);
341         
342         /* matcap material, its preview rect gets freed via icons */
343         if (vd->defmaterial) {
344                 if (vd->defmaterial->gpumaterial.first)
345                         GPU_material_free(vd->defmaterial);
346                 BKE_previewimg_free(&vd->defmaterial->preview);
347                 MEM_freeN(vd->defmaterial);
348         }
349 }
350
351
352 /* spacetype; init callback */
353 static void view3d_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
354 {
355
356 }
357
358 static SpaceLink *view3d_duplicate(SpaceLink *sl)
359 {
360         View3D *v3do = (View3D *)sl;
361         View3D *v3dn = MEM_dupallocN(sl);
362         
363         /* clear or remove stuff from old */
364         
365 // XXX  BIF_view3d_previewrender_free(v3do);
366         
367         if (v3do->localvd) {
368                 v3do->localvd = NULL;
369                 v3do->properties_storage = NULL;
370                 v3do->lay = v3dn->localvd->lay;
371                 v3do->lay &= 0xFFFFFF;
372         }
373
374         if (v3dn->drawtype == OB_RENDER)
375                 v3dn->drawtype = OB_SOLID;
376         
377         /* copy or clear inside new stuff */
378
379         v3dn->defmaterial = NULL;
380         
381         BLI_duplicatelist(&v3dn->bgpicbase, &v3do->bgpicbase);
382
383         v3dn->properties_storage = NULL;
384         
385         return (SpaceLink *)v3dn;
386 }
387
388 /* add handlers, stuff you only do once or on area/region changes */
389 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
390 {
391         ListBase *lb;
392         wmKeyMap *keymap;
393
394         /* object ops. */
395         
396         /* important to be before Pose keymap since they can both be enabled at once */
397         keymap = WM_keymap_find(wm->defaultconf, "Face Mask", 0, 0);
398         WM_event_add_keymap_handler(&ar->handlers, keymap);
399         
400         
401         keymap = WM_keymap_find(wm->defaultconf, "Weight Paint Vertex Selection", 0, 0);
402         WM_event_add_keymap_handler(&ar->handlers, keymap);
403
404         /* pose is not modal, operator poll checks for this */
405         keymap = WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
406         WM_event_add_keymap_handler(&ar->handlers, keymap);
407         
408         keymap = WM_keymap_find(wm->defaultconf, "Object Mode", 0, 0);
409         WM_event_add_keymap_handler(&ar->handlers, keymap);
410
411         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
412         WM_event_add_keymap_handler(&ar->handlers, keymap);
413
414         keymap = WM_keymap_find(wm->defaultconf, "Vertex Paint", 0, 0);
415         WM_event_add_keymap_handler(&ar->handlers, keymap);
416
417         keymap = WM_keymap_find(wm->defaultconf, "Weight Paint", 0, 0);
418         WM_event_add_keymap_handler(&ar->handlers, keymap);
419
420         keymap = WM_keymap_find(wm->defaultconf, "Sculpt", 0, 0);
421         WM_event_add_keymap_handler(&ar->handlers, keymap);
422         
423         keymap = WM_keymap_find(wm->defaultconf, "Mesh", 0, 0);
424         WM_event_add_keymap_handler(&ar->handlers, keymap);
425         
426         keymap = WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
427         WM_event_add_keymap_handler(&ar->handlers, keymap);
428         
429         keymap = WM_keymap_find(wm->defaultconf, "Armature", 0, 0);
430         WM_event_add_keymap_handler(&ar->handlers, keymap);
431
432         keymap = WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
433         WM_event_add_keymap_handler(&ar->handlers, keymap);
434
435         keymap = WM_keymap_find(wm->defaultconf, "Metaball", 0, 0);
436         WM_event_add_keymap_handler(&ar->handlers, keymap);
437         
438         keymap = WM_keymap_find(wm->defaultconf, "Lattice", 0, 0);
439         WM_event_add_keymap_handler(&ar->handlers, keymap);
440
441         /* armature sketching needs to take over mouse */
442         keymap = WM_keymap_find(wm->defaultconf, "Armature Sketch", 0, 0);
443         WM_event_add_keymap_handler(&ar->handlers, keymap);
444
445         keymap = WM_keymap_find(wm->defaultconf, "Particle", 0, 0);
446         WM_event_add_keymap_handler(&ar->handlers, keymap);
447
448         /* editfont keymap swallows all... */
449         keymap = WM_keymap_find(wm->defaultconf, "Font", 0, 0);
450         WM_event_add_keymap_handler(&ar->handlers, keymap);
451
452         keymap = WM_keymap_find(wm->defaultconf, "Object Non-modal", 0, 0);
453         WM_event_add_keymap_handler(&ar->handlers, keymap);
454
455         keymap = WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
456         WM_event_add_keymap_handler(&ar->handlers, keymap);
457
458         /* own keymap, last so modes can override it */
459         keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
460         WM_event_add_keymap_handler(&ar->handlers, keymap);
461
462         keymap = WM_keymap_find(wm->defaultconf, "3D View", SPACE_VIEW3D, 0);
463         WM_event_add_keymap_handler(&ar->handlers, keymap);
464         
465         /* add drop boxes */
466         lb = WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
467         
468         WM_event_add_dropbox_handler(&ar->handlers, lb);
469         
470 }
471
472 static int view3d_ob_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
473 {
474         if (drag->type == WM_DRAG_ID) {
475                 ID *id = (ID *)drag->poin;
476                 if (GS(id->name) == ID_OB)
477                         return 1;
478         }
479         return 0;
480 }
481
482 static int view3d_group_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
483 {
484         if (drag->type == WM_DRAG_ID) {
485                 ID *id = (ID *)drag->poin;
486                 if (GS(id->name) == ID_GR)
487                         return 1;
488         }
489         return 0;
490 }
491
492 static int view3d_mat_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
493 {
494         if (drag->type == WM_DRAG_ID) {
495                 ID *id = (ID *)drag->poin;
496                 if (GS(id->name) == ID_MA)
497                         return 1;
498         }
499         return 0;
500 }
501
502 static int view3d_ima_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
503 {
504         if (drag->type == WM_DRAG_ID) {
505                 ID *id = (ID *)drag->poin;
506                 if (GS(id->name) == ID_IM)
507                         return 1;
508         }
509         else if (drag->type == WM_DRAG_PATH) {
510                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE))   /* rule might not work? */
511                         return 1;
512         }
513         return 0;
514 }
515
516
517 static int view3d_ima_empty_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
518 {
519         Base *base = ED_view3d_give_base_under_cursor(C, event->mval);
520
521         if (!base || (base && base->object->type == OB_EMPTY))
522                 return view3d_ima_drop_poll(C, drag, event);
523         return 0;
524 }
525
526 static int view3d_ima_mesh_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
527 {
528         Base *base = ED_view3d_give_base_under_cursor(C, event->mval);
529
530         if (base && base->object->type == OB_MESH)
531                 return view3d_ima_drop_poll(C, drag, event);
532         return 0;
533 }
534
535 static void view3d_ob_drop_copy(wmDrag *drag, wmDropBox *drop)
536 {
537         ID *id = (ID *)drag->poin;
538
539         RNA_string_set(drop->ptr, "name", id->name + 2);
540 }
541
542 static void view3d_group_drop_copy(wmDrag *drag, wmDropBox *drop)
543 {
544         ID *id = (ID *)drag->poin;
545         
546         drop->opcontext = WM_OP_EXEC_DEFAULT;
547         RNA_string_set(drop->ptr, "name", id->name + 2);
548 }
549
550 static void view3d_id_drop_copy(wmDrag *drag, wmDropBox *drop)
551 {
552         ID *id = (ID *)drag->poin;
553         
554         RNA_string_set(drop->ptr, "name", id->name + 2);
555 }
556
557 static void view3d_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
558 {
559         ID *id = (ID *)drag->poin;
560         
561         if (id)
562                 RNA_string_set(drop->ptr, "name", id->name + 2);
563         if (drag->path[0])
564                 RNA_string_set(drop->ptr, "filepath", drag->path);
565 }
566
567
568 /* region dropbox definition */
569 static void view3d_dropboxes(void)
570 {
571         ListBase *lb = WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
572         
573         WM_dropbox_add(lb, "OBJECT_OT_add_named", view3d_ob_drop_poll, view3d_ob_drop_copy);
574         WM_dropbox_add(lb, "OBJECT_OT_drop_named_material", view3d_mat_drop_poll, view3d_id_drop_copy);
575         WM_dropbox_add(lb, "MESH_OT_drop_named_image", view3d_ima_mesh_drop_poll, view3d_id_path_drop_copy);
576         WM_dropbox_add(lb, "OBJECT_OT_drop_named_image", view3d_ima_empty_drop_poll, view3d_id_path_drop_copy);
577         WM_dropbox_add(lb, "OBJECT_OT_group_instance_add", view3d_group_drop_poll, view3d_group_drop_copy);
578 }
579
580
581
582 /* type callback, not region itself */
583 static void view3d_main_area_free(ARegion *ar)
584 {
585         RegionView3D *rv3d = ar->regiondata;
586         
587         if (rv3d) {
588                 if (rv3d->localvd) MEM_freeN(rv3d->localvd);
589                 if (rv3d->clipbb) MEM_freeN(rv3d->clipbb);
590
591                 if (rv3d->ri) {
592                         // XXX          BIF_view3d_previewrender_free(rv3d);
593                 }
594
595                 if (rv3d->render_engine)
596                         RE_engine_free(rv3d->render_engine);
597                 
598                 if (rv3d->depths) {
599                         if (rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
600                         MEM_freeN(rv3d->depths);
601                 }
602                 if (rv3d->sms) {
603                         MEM_freeN(rv3d->sms);
604                 }
605                 MEM_freeN(rv3d);
606                 ar->regiondata = NULL;
607         }
608 }
609
610 /* copy regiondata */
611 static void *view3d_main_area_duplicate(void *poin)
612 {
613         if (poin) {
614                 RegionView3D *rv3d = poin, *new;
615         
616                 new = MEM_dupallocN(rv3d);
617                 if (rv3d->localvd)
618                         new->localvd = MEM_dupallocN(rv3d->localvd);
619                 if (rv3d->clipbb)
620                         new->clipbb = MEM_dupallocN(rv3d->clipbb);
621                 
622                 new->depths = NULL;
623                 new->ri = NULL;
624                 new->render_engine = NULL;
625                 new->gpd = NULL;
626                 new->sms = NULL;
627                 new->smooth_timer = NULL;
628                 
629                 return new;
630         }
631         return NULL;
632 }
633
634 static void view3d_recalc_used_layers(ARegion *ar, wmNotifier *wmn, Scene *scene)
635 {
636         wmWindow *win = wmn->wm->winactive;
637         ScrArea *sa;
638         unsigned int lay_used = 0;
639         Base *base;
640
641         if (!win) return;
642
643         base = scene->base.first;
644         while (base) {
645                 lay_used |= base->lay & ((1 << 20) - 1); /* ignore localview */
646
647                 if (lay_used == (1 << 20) - 1)
648                         break;
649
650                 base = base->next;
651         }
652
653         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
654                 if (sa->spacetype == SPACE_VIEW3D) {
655                         if (BLI_findindex(&sa->regionbase, ar) != -1) {
656                                 View3D *v3d = sa->spacedata.first;
657                                 v3d->lay_used = lay_used;
658                                 break;
659                         }
660                 }
661         }
662 }
663
664 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
665 {
666         
667         /* context changes */
668         switch (wmn->category) {
669                 case NC_ANIMATION:
670                         switch (wmn->data) {
671                                 case ND_KEYFRAME_PROP:
672                                 case ND_NLA_ACTCHANGE:
673                                         ED_region_tag_redraw(ar);
674                                         break;
675                                 case ND_NLA:
676                                 case ND_KEYFRAME:
677                                         if (wmn->action == NA_EDITED)
678                                                 ED_region_tag_redraw(ar);
679                                         break;
680                                 case ND_ANIMCHAN:
681                                         if (wmn->action == NA_SELECTED)
682                                                 ED_region_tag_redraw(ar);
683                                         break;
684                         }
685                         break;
686                 case NC_SCENE:
687                         switch (wmn->data) {
688                                 case ND_LAYER_CONTENT:
689                                         if (wmn->reference)
690                                                 view3d_recalc_used_layers(ar, wmn, wmn->reference);
691                                         ED_region_tag_redraw(ar);
692                                         break;
693                                 case ND_FRAME:
694                                 case ND_TRANSFORM:
695                                 case ND_OB_ACTIVE:
696                                 case ND_OB_SELECT:
697                                 case ND_OB_VISIBLE:
698                                 case ND_LAYER:
699                                 case ND_RENDER_OPTIONS:
700                                 case ND_MODE:
701                                         ED_region_tag_redraw(ar);
702                                         break;
703                                 case ND_WORLD:
704                                         /* handled by space_view3d_listener() for v3d access */
705                                         break;
706                         }
707                         if (wmn->action == NA_EDITED)
708                                 ED_region_tag_redraw(ar);
709                         break;
710                 case NC_OBJECT:
711                         switch (wmn->data) {
712                                 case ND_BONE_ACTIVE:
713                                 case ND_BONE_SELECT:
714                                 case ND_TRANSFORM:
715                                 case ND_POSE:
716                                 case ND_DRAW:
717                                 case ND_MODIFIER:
718                                 case ND_CONSTRAINT:
719                                 case ND_KEYS:
720                                 case ND_PARTICLE:
721                                         ED_region_tag_redraw(ar);
722                                         break;
723                         }
724                         switch (wmn->action) {
725                                 case NA_ADDED:
726                                         ED_region_tag_redraw(ar);
727                                         break;
728                         }
729                         break;
730                 case NC_GEOM:
731                         switch (wmn->data) {
732                                 case ND_DATA:
733                                 case ND_VERTEX_GROUP:
734                                 case ND_SELECT:
735                                         ED_region_tag_redraw(ar);
736                                         break;
737                         }
738                         switch (wmn->action) {
739                                 case NA_EDITED:
740                                         ED_region_tag_redraw(ar);
741                                         break;
742                         }
743                         break;
744                 case NC_GROUP:
745                         /* all group ops for now */
746                         ED_region_tag_redraw(ar);
747                         break;
748                 case NC_BRUSH:
749                         if (wmn->action == NA_EDITED)
750                                 ED_region_tag_redraw_overlay(ar);
751                         break;
752                 case NC_MATERIAL:
753                         switch (wmn->data) {
754                                 case ND_SHADING_DRAW:
755                                 case ND_SHADING_LINKS:
756                                         ED_region_tag_redraw(ar);
757                                         break;
758                         }
759                         break;
760                 case NC_WORLD:
761                         switch (wmn->data) {
762                                 case ND_WORLD_DRAW:
763                                         /* handled by space_view3d_listener() for v3d access */
764                                         break;
765                                 case ND_WORLD_STARS:
766                                 {
767                                         RegionView3D *rv3d = ar->regiondata;
768                                         if (rv3d->persp == RV3D_CAMOB) {
769                                                 ED_region_tag_redraw(ar);
770                                         }
771                                 }
772                         }
773                         break;
774                 case NC_LAMP:
775                         switch (wmn->data) {
776                                 case ND_LIGHTING_DRAW:
777                                         ED_region_tag_redraw(ar);
778                                         break;
779                         }
780                         break;
781                 case NC_IMAGE:
782                         /* this could be more fine grained checks if we had
783                          * more context than just the region */
784                         ED_region_tag_redraw(ar);
785                         break;
786                 case NC_TEXTURE:
787                         /* same as above */
788                         ED_region_tag_redraw(ar);
789                         break;
790                 case NC_MOVIECLIP:
791                         if (wmn->data == ND_DISPLAY)
792                                 ED_region_tag_redraw(ar);
793                         break;
794                 case NC_SPACE:
795                         if (wmn->data == ND_SPACE_VIEW3D) {
796                                 if (wmn->subtype == NS_VIEW3D_GPU) {
797                                         RegionView3D *rv3d = ar->regiondata;
798                                         rv3d->rflag |= RV3D_GPULIGHT_UPDATE;
799                                 }
800                                 ED_region_tag_redraw(ar);
801                         }
802                         break;
803                 case NC_ID:
804                         if (wmn->action == NA_RENAME)
805                                 ED_region_tag_redraw(ar);
806                         break;
807                 case NC_SCREEN:
808                         switch (wmn->data) {
809                                 case ND_ANIMPLAY:
810                                 case ND_SKETCH:
811                                         ED_region_tag_redraw(ar);
812                                         break;
813                                 case ND_SCREENBROWSE:
814                                 case ND_SCREENDELETE:
815                                 case ND_SCREENSET:
816                                         /* screen was changed, need to update used layers due to NC_SCENE|ND_LAYER_CONTENT */
817                                         /* updates used layers only for View3D in active screen */
818                                         if (wmn->reference) {
819                                                 bScreen *sc = wmn->reference;
820                                                 view3d_recalc_used_layers(ar, wmn, sc->scene);
821                                         }
822                                         ED_region_tag_redraw(ar);
823                                         break;
824                         }
825
826                         break;
827                 case NC_GPENCIL:
828                         if (wmn->action == NA_EDITED)
829                                 ED_region_tag_redraw(ar);
830                         break;
831         }
832 }
833
834 /* concept is to retrieve cursor type context-less */
835 static void view3d_main_area_cursor(wmWindow *win, ScrArea *UNUSED(sa), ARegion *UNUSED(ar))
836 {
837         Scene *scene = win->screen->scene;
838
839         if (scene->obedit) {
840                 WM_cursor_set(win, CURSOR_EDIT);
841         }
842         else {
843                 WM_cursor_set(win, CURSOR_STD);
844         }
845 }
846
847 /* add handlers, stuff you only do once or on area/region changes */
848 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
849 {
850         wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
851         
852         WM_event_add_keymap_handler(&ar->handlers, keymap);
853
854         ED_region_header_init(ar);
855 }
856
857 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
858 {
859         ED_region_header(C, ar);
860 }
861
862 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
863 {
864         /* context changes */
865         switch (wmn->category) {
866                 case NC_SCENE:
867                         switch (wmn->data) {
868                                 case ND_FRAME:
869                                 case ND_OB_ACTIVE:
870                                 case ND_OB_SELECT:
871                                 case ND_OB_VISIBLE:
872                                 case ND_MODE:
873                                 case ND_LAYER:
874                                 case ND_TOOLSETTINGS:
875                                 case ND_LAYER_CONTENT:
876                                         ED_region_tag_redraw(ar);
877                                         break;
878                         }
879                         break;
880                 case NC_SPACE:
881                         if (wmn->data == ND_SPACE_VIEW3D)
882                                 ED_region_tag_redraw(ar);
883                         break;
884         }
885 }
886
887 /* add handlers, stuff you only do once or on area/region changes */
888 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
889 {
890         wmKeyMap *keymap;
891
892         ED_region_panels_init(wm, ar);
893         
894         keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
895         WM_event_add_keymap_handler(&ar->handlers, keymap);
896 }
897
898 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
899 {
900         ED_region_panels(C, ar, 1, NULL, -1);
901 }
902
903 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
904 {
905         /* context changes */
906         switch (wmn->category) {
907                 case NC_ANIMATION:
908                         switch (wmn->data) {
909                                 case ND_KEYFRAME_PROP:
910                                 case ND_NLA_ACTCHANGE:
911                                         ED_region_tag_redraw(ar);
912                                         break;
913                                 case ND_NLA:
914                                 case ND_KEYFRAME:
915                                         if (wmn->action == NA_EDITED)
916                                                 ED_region_tag_redraw(ar);
917                                         break;
918                         }
919                         break;
920                 case NC_SCENE:
921                         switch (wmn->data) {
922                                 case ND_FRAME:
923                                 case ND_OB_ACTIVE:
924                                 case ND_OB_SELECT:
925                                 case ND_OB_VISIBLE:
926                                 case ND_MODE:
927                                 case ND_LAYER:
928                                 case ND_LAYER_CONTENT:
929                                 case ND_TOOLSETTINGS:
930                                         ED_region_tag_redraw(ar);
931                                         break;
932                         }
933                         switch (wmn->action) {
934                                 case NA_EDITED:
935                                         ED_region_tag_redraw(ar);
936                                         break;
937                         }
938                         break;
939                 case NC_OBJECT:
940                         switch (wmn->data) {
941                                 case ND_BONE_ACTIVE:
942                                 case ND_BONE_SELECT:
943                                 case ND_TRANSFORM:
944                                 case ND_POSE:
945                                 case ND_DRAW:
946                                 case ND_KEYS:
947                                 case ND_MODIFIER:
948                                         ED_region_tag_redraw(ar);
949                                         break;
950                         }
951                         break;
952                 case NC_GEOM:
953                         switch (wmn->data) {
954                                 case ND_DATA:
955                                 case ND_VERTEX_GROUP:
956                                 case ND_SELECT:
957                                         ED_region_tag_redraw(ar);
958                                         break;
959                         }
960                         if (wmn->action == NA_EDITED)
961                                 ED_region_tag_redraw(ar);
962                         break;
963                 case NC_TEXTURE:
964                 case NC_MATERIAL:
965                         /* for brush textures */
966                         ED_region_tag_redraw(ar);
967                         break;
968                 case NC_BRUSH:
969                         if (wmn->action == NA_EDITED)
970                                 ED_region_tag_redraw(ar);
971                         break;
972                 case NC_SPACE:
973                         if (wmn->data == ND_SPACE_VIEW3D)
974                                 ED_region_tag_redraw(ar);
975                         break;
976                 case NC_ID:
977                         if (wmn->action == NA_RENAME)
978                                 ED_region_tag_redraw(ar);
979                         break;
980                 case NC_GPENCIL:
981                         if (wmn->data == ND_DATA || wmn->action == NA_EDITED)
982                                 ED_region_tag_redraw(ar);
983                         break;
984         }
985 }
986
987 /* add handlers, stuff you only do once or on area/region changes */
988 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
989 {
990         wmKeyMap *keymap;
991         
992         ED_region_panels_init(wm, ar);
993
994         keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
995         WM_event_add_keymap_handler(&ar->handlers, keymap);
996 }
997
998 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
999 {
1000         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
1001 }
1002
1003 static void view3d_props_area_listener(ARegion *ar, wmNotifier *wmn)
1004 {
1005         /* context changes */
1006         switch (wmn->category) {
1007                 case NC_WM:
1008                         if (wmn->data == ND_HISTORY)
1009                                 ED_region_tag_redraw(ar);
1010                         break;
1011                 case NC_SCENE:
1012                         if (wmn->data == ND_MODE)
1013                                 ED_region_tag_redraw(ar);
1014                         break;
1015                 case NC_SPACE:
1016                         if (wmn->data == ND_SPACE_VIEW3D)
1017                                 ED_region_tag_redraw(ar);
1018                         break;
1019         }
1020 }
1021
1022 /*area (not region) level listener*/
1023 static void space_view3d_listener(ScrArea *sa, struct wmNotifier *wmn)
1024 {
1025         View3D *v3d = sa->spacedata.first;
1026
1027         /* context changes */
1028         switch (wmn->category) {
1029                 case NC_SCENE:
1030                         switch (wmn->data) {
1031                                 case ND_WORLD:
1032                                         if (v3d->flag2 & V3D_RENDER_OVERRIDE)
1033                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1034                                         break;
1035                         }
1036                         break;
1037                 case NC_WORLD:
1038                         switch (wmn->data) {
1039                                 case ND_WORLD_DRAW:
1040                                         if (v3d->flag2 & V3D_RENDER_OVERRIDE)
1041                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1042                                         break;
1043                         }
1044                         break;
1045                 case NC_MATERIAL:
1046                         switch (wmn->data) {
1047                                 case ND_NODES:
1048                                         if (v3d->drawtype == OB_TEXTURE)
1049                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1050                                         break;
1051                         }
1052                         break;
1053         }
1054
1055         /* removed since BKE_image_user_frame_calc is now called in view3d_draw_bgpic because screen_ops doesnt call the notifier. */
1056 #if 0
1057         if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) {
1058                 View3D *v3d = area->spacedata.first;
1059                 BGpic *bgpic = v3d->bgpicbase.first;
1060
1061                 for (; bgpic; bgpic = bgpic->next) {
1062                         if (bgpic->ima) {
1063                                 Scene *scene = wmn->reference;
1064                                 BKE_image_user_frame_calc(&bgpic->iuser, scene->r.cfra, 0);
1065                         }
1066                 }
1067         }
1068 #endif
1069 }
1070
1071 const char *view3d_context_dir[] = {
1072         "selected_objects", "selected_bases", "selected_editable_objects",
1073         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
1074         "active_base", "active_object", NULL
1075 };
1076
1077 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
1078 {
1079         View3D *v3d = CTX_wm_view3d(C);
1080         Scene *scene = CTX_data_scene(C);
1081         Base *base;
1082         /* fallback to the scene layer, allows duplicate and other object operators to run outside the 3d view */
1083         unsigned int lay = v3d ? v3d->lay : scene->lay;
1084
1085         if (CTX_data_dir(member)) {
1086                 CTX_data_dir_set(result, view3d_context_dir);
1087         }
1088         else if (CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
1089                 int selected_objects = CTX_data_equals(member, "selected_objects");
1090
1091                 for (base = scene->base.first; base; base = base->next) {
1092                         if ((base->flag & SELECT) && (base->lay & lay)) {
1093                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW) == 0) {
1094                                         if (selected_objects)
1095                                                 CTX_data_id_list_add(result, &base->object->id);
1096                                         else
1097                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1098                                 }
1099                         }
1100                 }
1101                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1102                 return 1;
1103         }
1104         else if (CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
1105                 int selected_editable_objects = CTX_data_equals(member, "selected_editable_objects");
1106
1107                 for (base = scene->base.first; base; base = base->next) {
1108                         if ((base->flag & SELECT) && (base->lay & lay)) {
1109                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW) == 0) {
1110                                         if (0 == BKE_object_is_libdata(base->object)) {
1111                                                 if (selected_editable_objects)
1112                                                         CTX_data_id_list_add(result, &base->object->id);
1113                                                 else
1114                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1115                                         }
1116                                 }
1117                         }
1118                 }
1119                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1120                 return 1;
1121         }
1122         else if (CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
1123                 int visible_objects = CTX_data_equals(member, "visible_objects");
1124
1125                 for (base = scene->base.first; base; base = base->next) {
1126                         if (base->lay & lay) {
1127                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW) == 0) {
1128                                         if (visible_objects)
1129                                                 CTX_data_id_list_add(result, &base->object->id);
1130                                         else
1131                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1132                                 }
1133                         }
1134                 }
1135                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1136                 return 1;
1137         }
1138         else if (CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
1139                 int selectable_objects = CTX_data_equals(member, "selectable_objects");
1140
1141                 for (base = scene->base.first; base; base = base->next) {
1142                         if (base->lay & lay) {
1143                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW) == 0 && (base->object->restrictflag & OB_RESTRICT_SELECT) == 0) {
1144                                         if (selectable_objects)
1145                                                 CTX_data_id_list_add(result, &base->object->id);
1146                                         else
1147                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1148                                 }
1149                         }
1150                 }
1151                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1152                 return 1;
1153         }
1154         else if (CTX_data_equals(member, "active_base")) {
1155                 if (scene->basact && (scene->basact->lay & lay)) {
1156                         Object *ob = scene->basact->object;
1157                         /* if hidden but in edit mode, we still display, can happen with animation */
1158                         if ((ob->restrictflag & OB_RESTRICT_VIEW) == 0 || (ob->mode & OB_MODE_EDIT))
1159                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
1160                 }
1161                 
1162                 return 1;
1163         }
1164         else if (CTX_data_equals(member, "active_object")) {
1165                 if (scene->basact && (scene->basact->lay & lay)) {
1166                         Object *ob = scene->basact->object;
1167                         if ((ob->restrictflag & OB_RESTRICT_VIEW) == 0 || (ob->mode & OB_MODE_EDIT))
1168                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
1169                 }
1170                 
1171                 return 1;
1172         }
1173         else {
1174                 return 0; /* not found */
1175         }
1176
1177         return -1; /* found but not available */
1178 }
1179
1180
1181 /* only called once, from space/spacetypes.c */
1182 void ED_spacetype_view3d(void)
1183 {
1184         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype view3d");
1185         ARegionType *art;
1186         
1187         st->spaceid = SPACE_VIEW3D;
1188         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
1189         
1190         st->new = view3d_new;
1191         st->free = view3d_free;
1192         st->init = view3d_init;
1193         st->listener = space_view3d_listener;
1194         st->duplicate = view3d_duplicate;
1195         st->operatortypes = view3d_operatortypes;
1196         st->keymap = view3d_keymap;
1197         st->dropboxes = view3d_dropboxes;
1198         st->context = view3d_context;
1199         
1200         /* regions: main window */
1201         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d main region");
1202         art->regionid = RGN_TYPE_WINDOW;
1203         art->keymapflag = ED_KEYMAP_GPENCIL;
1204         art->draw = view3d_main_area_draw;
1205         art->init = view3d_main_area_init;
1206         art->free = view3d_main_area_free;
1207         art->duplicate = view3d_main_area_duplicate;
1208         art->listener = view3d_main_area_listener;
1209         art->cursor = view3d_main_area_cursor;
1210         art->lock = 1;   /* can become flag, see BKE_spacedata_draw_locks */
1211         BLI_addhead(&st->regiontypes, art);
1212         
1213         /* regions: listview/buttons */
1214         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d buttons region");
1215         art->regionid = RGN_TYPE_UI;
1216         art->prefsizex = 180; /* XXX */
1217         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1218         art->listener = view3d_buttons_area_listener;
1219         art->init = view3d_buttons_area_init;
1220         art->draw = view3d_buttons_area_draw;
1221         BLI_addhead(&st->regiontypes, art);
1222
1223         view3d_buttons_register(art);
1224
1225         /* regions: tool(bar) */
1226         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d tools region");
1227         art->regionid = RGN_TYPE_TOOLS;
1228         art->prefsizex = 160; /* XXX */
1229         art->prefsizey = 50; /* XXX */
1230         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1231         art->listener = view3d_buttons_area_listener;
1232         art->init = view3d_tools_area_init;
1233         art->draw = view3d_tools_area_draw;
1234         BLI_addhead(&st->regiontypes, art);
1235         
1236 #if 0
1237         /* unfinished still */
1238         view3d_toolshelf_register(art);
1239 #endif
1240
1241         /* regions: tool properties */
1242         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d tool properties region");
1243         art->regionid = RGN_TYPE_TOOL_PROPS;
1244         art->prefsizex = 0;
1245         art->prefsizey = 120;
1246         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1247         art->listener = view3d_props_area_listener;
1248         art->init = view3d_tools_area_init;
1249         art->draw = view3d_tools_area_draw;
1250         BLI_addhead(&st->regiontypes, art);
1251         
1252         view3d_tool_props_register(art);
1253         
1254         
1255         /* regions: header */
1256         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d header region");
1257         art->regionid = RGN_TYPE_HEADER;
1258         art->prefsizey = HEADERY;
1259         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
1260         art->listener = view3d_header_area_listener;
1261         art->init = view3d_header_area_init;
1262         art->draw = view3d_header_area_draw;
1263         BLI_addhead(&st->regiontypes, art);
1264         
1265         BKE_spacetype_register(st);
1266 }
1267