Merge branch 'master' into soc-2018-npr
[blender.git] / source / blender / editors / space_view3d / space_view3d.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup spview3d
22  */
23
24
25 #include <string.h>
26 #include <stdio.h>
27
28 #include "DNA_lightprobe_types.h"
29 #include "DNA_material_types.h"
30 #include "DNA_object_types.h"
31 #include "DNA_scene_types.h"
32 #include "DNA_gpencil_types.h"
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_blenlib.h"
37 #include "BLI_math.h"
38 #include "BLI_utildefines.h"
39
40 #include "BKE_context.h"
41 #include "BKE_curve.h"
42 #include "BKE_icons.h"
43 #include "BKE_lattice.h"
44 #include "BKE_main.h"
45 #include "BKE_mball.h"
46 #include "BKE_mesh.h"
47 #include "BKE_object.h"
48 #include "BKE_scene.h"
49 #include "BKE_screen.h"
50 #include "BKE_workspace.h"
51
52 #include "ED_space_api.h"
53 #include "ED_screen.h"
54 #include "ED_transform.h"
55
56 #include "GPU_framebuffer.h"
57 #include "GPU_material.h"
58 #include "GPU_viewport.h"
59 #include "GPU_matrix.h"
60
61 #include "DRW_engine.h"
62
63 #include "WM_api.h"
64 #include "WM_types.h"
65 #include "WM_message.h"
66 #include "WM_toolsystem.h"
67
68 #include "RE_engine.h"
69 #include "RE_pipeline.h"
70
71 #include "RNA_access.h"
72
73 #include "UI_interface.h"
74 #include "UI_resources.h"
75
76 #ifdef WITH_PYTHON
77 #  include "BPY_extern.h"
78 #endif
79
80 #include "DEG_depsgraph.h"
81
82 #include "view3d_intern.h"  /* own include */
83
84 /* ******************** manage regions ********************* */
85
86 ARegion *view3d_has_buttons_region(ScrArea *sa)
87 {
88         ARegion *ar, *arnew;
89
90         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
91         if (ar) {
92                 return ar;
93         }
94
95         /* add subdiv level; after header */
96         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
97
98         /* is error! */
99         if (ar == NULL) {
100                 return NULL;
101         }
102
103         arnew = MEM_callocN(sizeof(ARegion), "buttons for view3d");
104
105         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
106         arnew->regiontype = RGN_TYPE_UI;
107         arnew->alignment = RGN_ALIGN_RIGHT;
108
109         arnew->flag = RGN_FLAG_HIDDEN;
110
111         return arnew;
112 }
113
114 ARegion *view3d_has_tools_region(ScrArea *sa)
115 {
116         ARegion *ar, *artool = NULL, *arhead;
117
118         for (ar = sa->regionbase.first; ar; ar = ar->next) {
119                 if (ar->regiontype == RGN_TYPE_TOOLS) {
120                         artool = ar;
121                 }
122         }
123
124         /* tool region hide/unhide also hides props */
125         if (artool) {
126                 return artool;
127         }
128
129         if (artool == NULL) {
130                 /* add subdiv level; after header */
131                 for (arhead = sa->regionbase.first; arhead; arhead = arhead->next) {
132                         if (arhead->regiontype == RGN_TYPE_HEADER) {
133                                 break;
134                         }
135                 }
136
137                 /* is error! */
138                 if (arhead == NULL) {
139                         return NULL;
140                 }
141
142                 artool = MEM_callocN(sizeof(ARegion), "tools for view3d");
143
144                 BLI_insertlinkafter(&sa->regionbase, arhead, artool);
145                 artool->regiontype = RGN_TYPE_TOOLS;
146                 artool->alignment = RGN_ALIGN_LEFT;
147                 artool->flag = RGN_FLAG_HIDDEN;
148         }
149
150         return artool;
151 }
152
153 /* ****************************************************** */
154
155 /* function to always find a regionview3d context inside 3D window */
156 RegionView3D *ED_view3d_context_rv3d(bContext *C)
157 {
158         RegionView3D *rv3d = CTX_wm_region_view3d(C);
159
160         if (rv3d == NULL) {
161                 ScrArea *sa = CTX_wm_area(C);
162                 if (sa && sa->spacetype == SPACE_VIEW3D) {
163                         ARegion *ar = BKE_area_find_region_active_win(sa);
164                         if (ar) {
165                                 rv3d = ar->regiondata;
166                         }
167                 }
168         }
169         return rv3d;
170 }
171
172 /* ideally would return an rv3d but in some cases the region is needed too
173  * so return that, the caller can then access the ar->regiondata */
174 bool ED_view3d_context_user_region(bContext *C, View3D **r_v3d, ARegion **r_ar)
175 {
176         ScrArea *sa = CTX_wm_area(C);
177
178         *r_v3d = NULL;
179         *r_ar = NULL;
180
181         if (sa && sa->spacetype == SPACE_VIEW3D) {
182                 ARegion *ar = CTX_wm_region(C);
183                 View3D *v3d = (View3D *)sa->spacedata.first;
184
185                 if (ar) {
186                         RegionView3D *rv3d;
187                         if ((ar->regiontype == RGN_TYPE_WINDOW) && (rv3d = ar->regiondata) && (rv3d->viewlock & RV3D_LOCKED) == 0) {
188                                 *r_v3d = v3d;
189                                 *r_ar = ar;
190                                 return true;
191                         }
192                         else {
193                                 ARegion *ar_unlock_user = NULL;
194                                 ARegion *ar_unlock = NULL;
195                                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
196                                         /* find the first unlocked rv3d */
197                                         if (ar->regiondata && ar->regiontype == RGN_TYPE_WINDOW) {
198                                                 rv3d = ar->regiondata;
199                                                 if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
200                                                         ar_unlock = ar;
201                                                         if (rv3d->persp == RV3D_PERSP || rv3d->persp == RV3D_CAMOB) {
202                                                                 ar_unlock_user = ar;
203                                                                 break;
204                                                         }
205                                                 }
206                                         }
207                                 }
208
209                                 /* camera/perspective view get priority when the active region is locked */
210                                 if (ar_unlock_user) {
211                                         *r_v3d = v3d;
212                                         *r_ar = ar_unlock_user;
213                                         return true;
214                                 }
215
216                                 if (ar_unlock) {
217                                         *r_v3d = v3d;
218                                         *r_ar = ar_unlock;
219                                         return true;
220                                 }
221                         }
222                 }
223         }
224
225         return false;
226 }
227
228 /* Most of the time this isn't needed since you could assume the view matrix was
229  * set while drawing, however when functions like mesh_foreachScreenVert are
230  * called by selection tools, we can't be sure this object was the last.
231  *
232  * for example, transparent objects are drawn after editmode and will cause
233  * the rv3d mat's to change and break selection.
234  *
235  * 'ED_view3d_init_mats_rv3d' should be called before
236  * view3d_project_short_clip and view3d_project_short_noclip in cases where
237  * these functions are not used during draw_object
238  */
239 void ED_view3d_init_mats_rv3d(struct Object *ob, struct RegionView3D *rv3d)
240 {
241         /* local viewmat and persmat, to calculate projections */
242         mul_m4_m4m4(rv3d->viewmatob, rv3d->viewmat, ob->obmat);
243         mul_m4_m4m4(rv3d->persmatob, rv3d->persmat, ob->obmat);
244
245         /* initializes object space clipping, speeds up clip tests */
246         ED_view3d_clipping_local(rv3d, ob->obmat);
247 }
248
249 void ED_view3d_init_mats_rv3d_gl(struct Object *ob, struct RegionView3D *rv3d)
250 {
251         ED_view3d_init_mats_rv3d(ob, rv3d);
252
253         /* we have to multiply instead of loading viewmatob to make
254          * it work with duplis using displists, otherwise it will
255          * override the dupli-matrix */
256         GPU_matrix_mul(ob->obmat);
257 }
258
259 #ifdef DEBUG
260 /* ensure we correctly initialize */
261 void ED_view3d_clear_mats_rv3d(struct RegionView3D *rv3d)
262 {
263         zero_m4(rv3d->viewmatob);
264         zero_m4(rv3d->persmatob);
265 }
266
267 void ED_view3d_check_mats_rv3d(struct RegionView3D *rv3d)
268 {
269         BLI_ASSERT_ZERO_M4(rv3d->viewmatob);
270         BLI_ASSERT_ZERO_M4(rv3d->persmatob);
271 }
272 #endif
273
274 void ED_view3d_stop_render_preview(wmWindowManager *wm, ARegion *ar)
275 {
276         RegionView3D *rv3d = ar->regiondata;
277
278         if (rv3d->render_engine) {
279 #ifdef WITH_PYTHON
280                 BPy_BEGIN_ALLOW_THREADS;
281 #endif
282
283                 WM_jobs_kill_type(wm, ar, WM_JOB_TYPE_RENDER_PREVIEW);
284
285 #ifdef WITH_PYTHON
286                 BPy_END_ALLOW_THREADS;
287 #endif
288
289                 RE_engine_free(rv3d->render_engine);
290                 rv3d->render_engine = NULL;
291         }
292 }
293
294 void ED_view3d_shade_update(Main *bmain, View3D *v3d, ScrArea *sa)
295 {
296         wmWindowManager *wm = bmain->wm.first;
297
298         if (v3d->shading.type != OB_RENDER) {
299                 ARegion *ar;
300
301                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
302                         if ((ar->regiontype == RGN_TYPE_WINDOW) && ar->regiondata) {
303                                 ED_view3d_stop_render_preview(wm, ar);
304                                 break;
305                         }
306                 }
307         }
308 }
309
310 /* ******************** default callbacks for view3d space ***************** */
311
312 static SpaceLink *view3d_new(const ScrArea *UNUSED(sa), const Scene *scene)
313 {
314         ARegion *ar;
315         View3D *v3d;
316         RegionView3D *rv3d;
317
318         v3d = MEM_callocN(sizeof(View3D), "initview3d");
319         v3d->spacetype = SPACE_VIEW3D;
320         if (scene) {
321                 v3d->camera = scene->camera;
322         }
323         v3d->scenelock = true;
324         v3d->grid = 1.0f;
325         v3d->gridlines = 16;
326         v3d->gridsubdiv = 10;
327         BKE_screen_view3d_shading_init(&v3d->shading);
328
329         v3d->overlay.wireframe_threshold = 1.0f;
330         v3d->overlay.xray_alpha_bone = 0.5f;
331         v3d->overlay.texture_paint_mode_opacity = 1.0f;
332         v3d->overlay.weight_paint_mode_opacity = 1.0f;
333         v3d->overlay.vertex_paint_mode_opacity = 1.0f;
334         v3d->overlay.edit_flag = V3D_OVERLAY_EDIT_FACES |
335                                  V3D_OVERLAY_EDIT_SEAMS |
336                                  V3D_OVERLAY_EDIT_SHARP |
337                                  V3D_OVERLAY_EDIT_FREESTYLE_EDGE |
338                                  V3D_OVERLAY_EDIT_FREESTYLE_FACE |
339                                  V3D_OVERLAY_EDIT_EDGES |
340                                  V3D_OVERLAY_EDIT_CREASES |
341                                  V3D_OVERLAY_EDIT_BWEIGHTS |
342                                  V3D_OVERLAY_EDIT_CU_HANDLES |
343                                  V3D_OVERLAY_EDIT_CU_NORMALS;
344
345         v3d->gridflag = V3D_SHOW_X | V3D_SHOW_Y | V3D_SHOW_FLOOR;
346
347         v3d->flag = V3D_SELECT_OUTLINE;
348         v3d->flag2 = V3D_SHOW_RECONSTRUCTION | V3D_SHOW_ANNOTATION;
349
350         v3d->lens = 50.0f;
351         v3d->clip_start = 0.01f;
352         v3d->clip_end = 1000.0f;
353
354         v3d->overlay.gpencil_paper_opacity = 0.5f;
355         v3d->overlay.gpencil_grid_opacity = 0.9f;
356
357         v3d->bundle_size = 0.2f;
358         v3d->bundle_drawtype = OB_PLAINAXES;
359
360         /* stereo */
361         v3d->stereo3d_camera = STEREO_3D_ID;
362         v3d->stereo3d_flag |= V3D_S3D_DISPPLANE;
363         v3d->stereo3d_convergence_alpha = 0.15f;
364         v3d->stereo3d_volume_alpha = 0.05f;
365
366         /* grease pencil settings */
367         v3d->vertex_opacity = 1.0f;
368         v3d->gp_flag |= V3D_GP_SHOW_EDIT_LINES;
369
370         /* header */
371         ar = MEM_callocN(sizeof(ARegion), "header for view3d");
372
373         BLI_addtail(&v3d->regionbase, ar);
374         ar->regiontype = RGN_TYPE_HEADER;
375         ar->alignment = (U.uiflag & USER_HEADER_BOTTOM) ? RGN_ALIGN_BOTTOM : RGN_ALIGN_TOP;
376
377         /* tool shelf */
378         ar = MEM_callocN(sizeof(ARegion), "toolshelf for view3d");
379
380         BLI_addtail(&v3d->regionbase, ar);
381         ar->regiontype = RGN_TYPE_TOOLS;
382         ar->alignment = RGN_ALIGN_LEFT;
383         ar->flag = RGN_FLAG_HIDDEN;
384
385         /* buttons/list view */
386         ar = MEM_callocN(sizeof(ARegion), "buttons for view3d");
387
388         BLI_addtail(&v3d->regionbase, ar);
389         ar->regiontype = RGN_TYPE_UI;
390         ar->alignment = RGN_ALIGN_RIGHT;
391         ar->flag = RGN_FLAG_HIDDEN;
392
393         /* main region */
394         ar = MEM_callocN(sizeof(ARegion), "main region for view3d");
395
396         BLI_addtail(&v3d->regionbase, ar);
397         ar->regiontype = RGN_TYPE_WINDOW;
398
399         ar->regiondata = MEM_callocN(sizeof(RegionView3D), "region view3d");
400         rv3d = ar->regiondata;
401         rv3d->viewquat[0] = 1.0f;
402         rv3d->persp = RV3D_PERSP;
403         rv3d->view = RV3D_VIEW_USER;
404         rv3d->dist = 10.0;
405
406         return (SpaceLink *)v3d;
407 }
408
409 /* not spacelink itself */
410 static void view3d_free(SpaceLink *sl)
411 {
412         View3D *vd = (View3D *) sl;
413
414         if (vd->localvd) {
415                 MEM_freeN(vd->localvd);
416         }
417
418         if (vd->properties_storage) {
419                 MEM_freeN(vd->properties_storage);
420         }
421
422         if (vd->fx_settings.ssao) {
423                 MEM_freeN(vd->fx_settings.ssao);
424         }
425         if (vd->fx_settings.dof) {
426                 MEM_freeN(vd->fx_settings.dof);
427         }
428 }
429
430
431 /* spacetype; init callback */
432 static void view3d_init(wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
433 {
434
435 }
436
437 static SpaceLink *view3d_duplicate(SpaceLink *sl)
438 {
439         View3D *v3do = (View3D *)sl;
440         View3D *v3dn = MEM_dupallocN(sl);
441
442         /* clear or remove stuff from old */
443
444         if (v3dn->localvd) {
445                 v3dn->localvd = NULL;
446                 v3dn->properties_storage = NULL;
447         }
448
449         if (v3dn->shading.type == OB_RENDER) {
450                 v3dn->shading.type = OB_SOLID;
451         }
452
453         /* copy or clear inside new stuff */
454
455         v3dn->properties_storage = NULL;
456         if (v3dn->fx_settings.dof) {
457                 v3dn->fx_settings.dof = MEM_dupallocN(v3do->fx_settings.dof);
458         }
459         if (v3dn->fx_settings.ssao) {
460                 v3dn->fx_settings.ssao = MEM_dupallocN(v3do->fx_settings.ssao);
461         }
462
463         return (SpaceLink *)v3dn;
464 }
465
466 /* add handlers, stuff you only do once or on area/region changes */
467 static void view3d_main_region_init(wmWindowManager *wm, ARegion *ar)
468 {
469         ListBase *lb;
470         wmKeyMap *keymap;
471
472         /* object ops. */
473
474         /* important to be before Pose keymap since they can both be enabled at once */
475         keymap = WM_keymap_ensure(wm->defaultconf, "Face Mask", 0, 0);
476         WM_event_add_keymap_handler(&ar->handlers, keymap);
477
478
479         keymap = WM_keymap_ensure(wm->defaultconf, "Weight Paint Vertex Selection", 0, 0);
480         WM_event_add_keymap_handler(&ar->handlers, keymap);
481
482         /* pose is not modal, operator poll checks for this */
483         keymap = WM_keymap_ensure(wm->defaultconf, "Pose", 0, 0);
484         WM_event_add_keymap_handler(&ar->handlers, keymap);
485
486         keymap = WM_keymap_ensure(wm->defaultconf, "Object Mode", 0, 0);
487         WM_event_add_keymap_handler(&ar->handlers, keymap);
488
489         keymap = WM_keymap_ensure(wm->defaultconf, "Paint Curve", 0, 0);
490         WM_event_add_keymap_handler(&ar->handlers, keymap);
491
492         keymap = WM_keymap_ensure(wm->defaultconf, "Curve", 0, 0);
493         WM_event_add_keymap_handler(&ar->handlers, keymap);
494
495         keymap = WM_keymap_ensure(wm->defaultconf, "Image Paint", 0, 0);
496         WM_event_add_keymap_handler(&ar->handlers, keymap);
497
498         keymap = WM_keymap_ensure(wm->defaultconf, "Vertex Paint", 0, 0);
499         WM_event_add_keymap_handler(&ar->handlers, keymap);
500
501         keymap = WM_keymap_ensure(wm->defaultconf, "Weight Paint", 0, 0);
502         WM_event_add_keymap_handler(&ar->handlers, keymap);
503
504         keymap = WM_keymap_ensure(wm->defaultconf, "Sculpt", 0, 0);
505         WM_event_add_keymap_handler(&ar->handlers, keymap);
506
507         keymap = WM_keymap_ensure(wm->defaultconf, "Mesh", 0, 0);
508         WM_event_add_keymap_handler(&ar->handlers, keymap);
509
510         keymap = WM_keymap_ensure(wm->defaultconf, "Curve", 0, 0);
511         WM_event_add_keymap_handler(&ar->handlers, keymap);
512
513         keymap = WM_keymap_ensure(wm->defaultconf, "Armature", 0, 0);
514         WM_event_add_keymap_handler(&ar->handlers, keymap);
515
516         keymap = WM_keymap_ensure(wm->defaultconf, "Pose", 0, 0);
517         WM_event_add_keymap_handler(&ar->handlers, keymap);
518
519         keymap = WM_keymap_ensure(wm->defaultconf, "Metaball", 0, 0);
520         WM_event_add_keymap_handler(&ar->handlers, keymap);
521
522         keymap = WM_keymap_ensure(wm->defaultconf, "Lattice", 0, 0);
523         WM_event_add_keymap_handler(&ar->handlers, keymap);
524
525         keymap = WM_keymap_ensure(wm->defaultconf, "Particle", 0, 0);
526         WM_event_add_keymap_handler(&ar->handlers, keymap);
527
528         /* editfont keymap swallows all... */
529         keymap = WM_keymap_ensure(wm->defaultconf, "Font", 0, 0);
530         WM_event_add_keymap_handler(&ar->handlers, keymap);
531
532         keymap = WM_keymap_ensure(wm->defaultconf, "Object Non-modal", 0, 0);
533         WM_event_add_keymap_handler(&ar->handlers, keymap);
534
535         keymap = WM_keymap_ensure(wm->defaultconf, "Frames", 0, 0);
536         WM_event_add_keymap_handler(&ar->handlers, keymap);
537
538         /* own keymap, last so modes can override it */
539         keymap = WM_keymap_ensure(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
540         WM_event_add_keymap_handler(&ar->handlers, keymap);
541
542         keymap = WM_keymap_ensure(wm->defaultconf, "3D View", SPACE_VIEW3D, 0);
543         WM_event_add_keymap_handler(&ar->handlers, keymap);
544
545         /* add drop boxes */
546         lb = WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
547
548         WM_event_add_dropbox_handler(&ar->handlers, lb);
549
550 }
551
552 static void view3d_main_region_exit(wmWindowManager *wm, ARegion *ar)
553 {
554         ED_view3d_stop_render_preview(wm, ar);
555 }
556
557 static bool view3d_ob_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
558 {
559         return WM_drag_ID(drag, ID_OB) != NULL;
560 }
561
562 static bool view3d_collection_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
563 {
564         return WM_drag_ID(drag, ID_GR) != NULL;
565 }
566
567 static bool view3d_mat_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
568 {
569         return WM_drag_ID(drag, ID_MA) != NULL;
570 }
571
572 static bool view3d_ima_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
573 {
574         if (drag->type == WM_DRAG_PATH) {
575                 /* rule might not work? */
576                 return (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE));
577         }
578         else {
579                 return WM_drag_ID(drag, ID_IM) != NULL;
580         }
581 }
582
583 static bool view3d_ima_bg_is_camera_view(bContext *C)
584 {
585         RegionView3D *rv3d = CTX_wm_region_view3d(C);
586         if ((rv3d && (rv3d->persp == RV3D_CAMOB))) {
587                 View3D *v3d = CTX_wm_view3d(C);
588                 if (v3d && v3d->camera && v3d->camera->type == OB_CAMERA) {
589                         return true;
590                 }
591         }
592         return false;
593 }
594
595 static bool view3d_ima_bg_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event, const char **tooltip)
596 {
597         if (!view3d_ima_drop_poll(C, drag, event, tooltip)) {
598                 return false;
599         }
600
601         if (ED_view3d_is_object_under_cursor(C, event->mval)) {
602                 return false;
603         }
604
605         return view3d_ima_bg_is_camera_view(C);
606 }
607
608 static bool view3d_ima_empty_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event, const char **tooltip)
609 {
610         if (!view3d_ima_drop_poll(C, drag, event, tooltip)) {
611                 return false;
612         }
613
614         Object *ob = ED_view3d_give_object_under_cursor(C, event->mval);
615
616         if (ob == NULL) {
617                 return true;
618         }
619
620         if (ob->type == OB_EMPTY && ob->empty_drawtype == OB_EMPTY_IMAGE) {
621                 return true;
622         }
623
624         return false;
625 }
626
627 static void view3d_ob_drop_copy(wmDrag *drag, wmDropBox *drop)
628 {
629         ID *id = WM_drag_ID(drag, ID_OB);
630
631         RNA_string_set(drop->ptr, "name", id->name + 2);
632 }
633
634 static void view3d_collection_drop_copy(wmDrag *drag, wmDropBox *drop)
635 {
636         ID *id = WM_drag_ID(drag, ID_GR);
637
638         drop->opcontext = WM_OP_EXEC_DEFAULT;
639         RNA_string_set(drop->ptr, "name", id->name + 2);
640 }
641
642 static void view3d_id_drop_copy(wmDrag *drag, wmDropBox *drop)
643 {
644         ID *id = WM_drag_ID(drag, 0);
645
646         RNA_string_set(drop->ptr, "name", id->name + 2);
647 }
648
649 static void view3d_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
650 {
651         ID *id = WM_drag_ID(drag, 0);
652
653         if (id) {
654                 RNA_string_set(drop->ptr, "name", id->name + 2);
655                 RNA_struct_property_unset(drop->ptr, "filepath");
656         }
657         else if (drag->path[0]) {
658                 RNA_string_set(drop->ptr, "filepath", drag->path);
659                 RNA_struct_property_unset(drop->ptr, "image");
660         }
661 }
662
663 static void view3d_lightcache_update(bContext *C)
664 {
665         PointerRNA op_ptr;
666
667         Scene *scene = CTX_data_scene(C);
668
669         if (strcmp(scene->r.engine, RE_engine_id_BLENDER_EEVEE) != 0) {
670                 /* Only do auto bake if eevee is the active engine */
671                 return;
672         }
673
674         WM_operator_properties_create(&op_ptr, "SCENE_OT_light_cache_bake");
675         RNA_int_set(&op_ptr, "delay", 200);
676         RNA_enum_set_identifier(C, &op_ptr, "subset", "DIRTY");
677
678         WM_operator_name_call(C, "SCENE_OT_light_cache_bake", WM_OP_INVOKE_DEFAULT, &op_ptr);
679
680         WM_operator_properties_free(&op_ptr);
681 }
682
683 /* region dropbox definition */
684 static void view3d_dropboxes(void)
685 {
686         ListBase *lb = WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
687
688         WM_dropbox_add(lb, "OBJECT_OT_add_named", view3d_ob_drop_poll, view3d_ob_drop_copy);
689         WM_dropbox_add(lb, "OBJECT_OT_drop_named_material", view3d_mat_drop_poll, view3d_id_drop_copy);
690         WM_dropbox_add(lb, "VIEW3D_OT_background_image_add", view3d_ima_bg_drop_poll, view3d_id_path_drop_copy);
691         WM_dropbox_add(lb, "OBJECT_OT_drop_named_image", view3d_ima_empty_drop_poll, view3d_id_path_drop_copy);
692         WM_dropbox_add(lb, "OBJECT_OT_collection_instance_add", view3d_collection_drop_poll, view3d_collection_drop_copy);
693 }
694
695 static void view3d_widgets(void)
696 {
697         wmGizmoMapType *gzmap_type = WM_gizmomaptype_ensure(
698                 &(const struct wmGizmoMapType_Params){SPACE_VIEW3D, RGN_TYPE_WINDOW});
699
700         WM_gizmogrouptype_append_and_link(gzmap_type, VIEW3D_GGT_light_spot);
701         WM_gizmogrouptype_append_and_link(gzmap_type, VIEW3D_GGT_light_area);
702         WM_gizmogrouptype_append_and_link(gzmap_type, VIEW3D_GGT_light_target);
703         WM_gizmogrouptype_append_and_link(gzmap_type, VIEW3D_GGT_force_field);
704         WM_gizmogrouptype_append_and_link(gzmap_type, VIEW3D_GGT_camera);
705         WM_gizmogrouptype_append_and_link(gzmap_type, VIEW3D_GGT_camera_view);
706         WM_gizmogrouptype_append_and_link(gzmap_type, VIEW3D_GGT_empty_image);
707         WM_gizmogrouptype_append_and_link(gzmap_type, VIEW3D_GGT_armature_spline);
708
709         WM_gizmogrouptype_append(TRANSFORM_GGT_gizmo);
710         WM_gizmogrouptype_append(VIEW3D_GGT_xform_cage);
711         WM_gizmogrouptype_append(VIEW3D_GGT_xform_shear);
712         WM_gizmogrouptype_append(VIEW3D_GGT_xform_extrude);
713         WM_gizmogrouptype_append(VIEW3D_GGT_mesh_preselect_elem);
714         WM_gizmogrouptype_append(VIEW3D_GGT_mesh_preselect_edgering);
715
716         WM_gizmogrouptype_append(VIEW3D_GGT_ruler);
717         WM_gizmotype_append(VIEW3D_GT_ruler_item);
718
719         WM_gizmogrouptype_append_and_link(gzmap_type, VIEW3D_GGT_navigate);
720         WM_gizmotype_append(VIEW3D_GT_navigate_rotate);
721 }
722
723
724 /* type callback, not region itself */
725 static void view3d_main_region_free(ARegion *ar)
726 {
727         RegionView3D *rv3d = ar->regiondata;
728
729         if (rv3d) {
730                 if (rv3d->localvd) {
731                         MEM_freeN(rv3d->localvd);
732                 }
733                 if (rv3d->clipbb) {
734                         MEM_freeN(rv3d->clipbb);
735                 }
736
737                 if (rv3d->render_engine) {
738                         RE_engine_free(rv3d->render_engine);
739                 }
740
741                 if (rv3d->depths) {
742                         if (rv3d->depths->depths) {
743                                 MEM_freeN(rv3d->depths->depths);
744                         }
745                         MEM_freeN(rv3d->depths);
746                 }
747                 if (rv3d->sms) {
748                         MEM_freeN(rv3d->sms);
749                 }
750
751                 MEM_freeN(rv3d);
752                 ar->regiondata = NULL;
753         }
754 }
755
756 /* copy regiondata */
757 static void *view3d_main_region_duplicate(void *poin)
758 {
759         if (poin) {
760                 RegionView3D *rv3d = poin, *new;
761
762                 new = MEM_dupallocN(rv3d);
763                 if (rv3d->localvd) {
764                         new->localvd = MEM_dupallocN(rv3d->localvd);
765                 }
766                 if (rv3d->clipbb) {
767                         new->clipbb = MEM_dupallocN(rv3d->clipbb);
768                 }
769
770                 new->depths = NULL;
771                 new->render_engine = NULL;
772                 new->sms = NULL;
773                 new->smooth_timer = NULL;
774
775                 return new;
776         }
777         return NULL;
778 }
779
780 static void view3d_main_region_listener(
781         wmWindow *UNUSED(win), ScrArea *sa, ARegion *ar,
782         wmNotifier *wmn, const Scene *scene)
783 {
784         View3D *v3d = sa->spacedata.first;
785         RegionView3D *rv3d = ar->regiondata;
786         wmGizmoMap *gzmap = ar->gizmo_map;
787
788         /* context changes */
789         switch (wmn->category) {
790                 case NC_WM:
791                         if (ELEM(wmn->data, ND_UNDO)) {
792                                 WM_gizmomap_tag_refresh(gzmap);
793                         }
794                         break;
795                 case NC_ANIMATION:
796                         switch (wmn->data) {
797                                 case ND_KEYFRAME_PROP:
798                                 case ND_NLA_ACTCHANGE:
799                                         ED_region_tag_redraw(ar);
800                                         break;
801                                 case ND_NLA:
802                                 case ND_KEYFRAME:
803                                         if (ELEM(wmn->action, NA_EDITED, NA_ADDED, NA_REMOVED)) {
804                                                 ED_region_tag_redraw(ar);
805                                         }
806                                         break;
807                                 case ND_ANIMCHAN:
808                                         if (ELEM(wmn->action, NA_EDITED, NA_ADDED, NA_REMOVED, NA_SELECTED)) {
809                                                 ED_region_tag_redraw(ar);
810                                         }
811                                         break;
812                         }
813                         break;
814                 case NC_SCENE:
815                         switch (wmn->data) {
816                                 case ND_SCENEBROWSE:
817                                 case ND_LAYER_CONTENT:
818                                         ED_region_tag_redraw(ar);
819                                         WM_gizmomap_tag_refresh(gzmap);
820                                         break;
821                                 case ND_LAYER:
822                                         if (wmn->reference) {
823                                                 BKE_screen_view3d_sync(v3d, wmn->reference);
824                                         }
825                                         ED_region_tag_redraw(ar);
826                                         WM_gizmomap_tag_refresh(gzmap);
827                                         break;
828                                 case ND_OB_ACTIVE:
829                                 case ND_OB_SELECT:
830                                         ATTR_FALLTHROUGH;
831                                 case ND_FRAME:
832                                 case ND_TRANSFORM:
833                                 case ND_OB_VISIBLE:
834                                 case ND_RENDER_OPTIONS:
835                                 case ND_MARKERS:
836                                 case ND_MODE:
837                                         ED_region_tag_redraw(ar);
838                                         WM_gizmomap_tag_refresh(gzmap);
839                                         break;
840                                 case ND_WORLD:
841                                         /* handled by space_view3d_listener() for v3d access */
842                                         break;
843                                 case ND_DRAW_RENDER_VIEWPORT:
844                                 {
845                                         if (v3d->camera && (scene == wmn->reference)) {
846                                                 if (rv3d->persp == RV3D_CAMOB) {
847                                                         ED_region_tag_redraw(ar);
848                                                 }
849                                         }
850                                         break;
851                                 }
852                         }
853                         if (wmn->action == NA_EDITED) {
854                                 ED_region_tag_redraw(ar);
855                         }
856                         break;
857                 case NC_OBJECT:
858                         switch (wmn->data) {
859                                 case ND_BONE_ACTIVE:
860                                 case ND_BONE_SELECT:
861                                 case ND_TRANSFORM:
862                                 case ND_POSE:
863                                 case ND_DRAW:
864                                 case ND_MODIFIER:
865                                 case ND_CONSTRAINT:
866                                 case ND_KEYS:
867                                 case ND_PARTICLE:
868                                 case ND_POINTCACHE:
869                                 case ND_LOD:
870                                         ED_region_tag_redraw(ar);
871                                         WM_gizmomap_tag_refresh(gzmap);
872                                         break;
873                         }
874                         switch (wmn->action) {
875                                 case NA_ADDED:
876                                         ED_region_tag_redraw(ar);
877                                         break;
878                         }
879                         break;
880                 case NC_GEOM:
881                         switch (wmn->data) {
882                                 case ND_SELECT:
883                                 {
884                                         WM_gizmomap_tag_refresh(gzmap);
885                                         ATTR_FALLTHROUGH;
886                                 }
887                                 case ND_DATA:
888                                 case ND_VERTEX_GROUP:
889                                         ED_region_tag_redraw(ar);
890                                         break;
891                         }
892                         switch (wmn->action) {
893                                 case NA_EDITED:
894                                         ED_region_tag_redraw(ar);
895                                         break;
896                         }
897                         break;
898                 case NC_CAMERA:
899                         switch (wmn->data) {
900                                 case ND_DRAW_RENDER_VIEWPORT:
901                                 {
902                                         if (v3d->camera && (v3d->camera->data == wmn->reference)) {
903                                                 if (rv3d->persp == RV3D_CAMOB) {
904                                                         ED_region_tag_redraw(ar);
905                                                 }
906                                         }
907                                         break;
908                                 }
909                         }
910                         break;
911                 case NC_GROUP:
912                         /* all group ops for now */
913                         ED_region_tag_redraw(ar);
914                         break;
915                 case NC_BRUSH:
916                         switch (wmn->action) {
917                                 case NA_EDITED:
918                                         ED_region_tag_redraw_overlay(ar);
919                                         break;
920                                 case NA_SELECTED:
921                                         /* used on brush changes - needed because 3d cursor
922                                          * has to be drawn if clone brush is selected */
923                                         ED_region_tag_redraw(ar);
924                                         break;
925                         }
926                         break;
927                 case NC_MATERIAL:
928                         switch (wmn->data) {
929                                 case ND_SHADING:
930                                 case ND_NODES:
931                                         /* TODO(sergey) This is a bit too much updates, but needed to
932                                          * have proper material drivers update in the viewport.
933                                          *
934                                          * How to solve?
935                                          */
936                                         ED_region_tag_redraw(ar);
937                                         break;
938                                 case ND_SHADING_DRAW:
939                                 case ND_SHADING_LINKS:
940                                         ED_region_tag_redraw(ar);
941                                         break;
942                         }
943                         break;
944                 case NC_WORLD:
945                         switch (wmn->data) {
946                                 case ND_WORLD_DRAW:
947                                         /* handled by space_view3d_listener() for v3d access */
948                                         break;
949                                 case ND_WORLD:
950                                         /* Needed for updating world materials */
951                                         ED_region_tag_redraw(ar);
952                                         break;
953                         }
954                         break;
955                 case NC_LAMP:
956                         switch (wmn->data) {
957                                 case ND_LIGHTING:
958                                         /* TODO(sergey): This is a bit too much, but needed to
959                                          * handle updates from new depsgraph.
960                                          */
961                                         ED_region_tag_redraw(ar);
962                                         break;
963                                 case ND_LIGHTING_DRAW:
964                                         ED_region_tag_redraw(ar);
965                                         WM_gizmomap_tag_refresh(gzmap);
966                                         break;
967                         }
968                         break;
969                 case NC_LIGHTPROBE:
970                         ED_area_tag_refresh(sa);
971                         break;
972                 case NC_IMAGE:
973                         /* this could be more fine grained checks if we had
974                          * more context than just the region */
975                         ED_region_tag_redraw(ar);
976                         break;
977                 case NC_TEXTURE:
978                         /* same as above */
979                         ED_region_tag_redraw(ar);
980                         break;
981                 case NC_MOVIECLIP:
982                         if (wmn->data == ND_DISPLAY || wmn->action == NA_EDITED) {
983                                 ED_region_tag_redraw(ar);
984                         }
985                         break;
986                 case NC_SPACE:
987                         if (wmn->data == ND_SPACE_VIEW3D) {
988                                 if (wmn->subtype == NS_VIEW3D_GPU) {
989                                         rv3d->rflag |= RV3D_GPULIGHT_UPDATE;
990                                 }
991                                 ED_region_tag_redraw(ar);
992                                 WM_gizmomap_tag_refresh(gzmap);
993                         }
994                         break;
995                 case NC_ID:
996                         if (wmn->action == NA_RENAME) {
997                                 ED_region_tag_redraw(ar);
998                         }
999                         break;
1000                 case NC_SCREEN:
1001                         switch (wmn->data) {
1002                                 case ND_ANIMPLAY:
1003                                 case ND_SKETCH:
1004                                         ED_region_tag_redraw(ar);
1005                                         break;
1006                                 case ND_LAYOUTBROWSE:
1007                                 case ND_LAYOUTDELETE:
1008                                 case ND_LAYOUTSET:
1009                                         WM_gizmomap_tag_refresh(gzmap);
1010                                         ED_region_tag_redraw(ar);
1011                                         break;
1012                                 case ND_LAYER:
1013                                         ED_region_tag_redraw(ar);
1014                                         break;
1015                         }
1016
1017                         break;
1018                 case NC_GPENCIL:
1019                         if (wmn->data == ND_DATA || ELEM(wmn->action, NA_EDITED, NA_SELECTED)) {
1020                                 ED_region_tag_redraw(ar);
1021                         }
1022                         break;
1023         }
1024 }
1025
1026 static void view3d_main_region_message_subscribe(
1027         const struct bContext *C,
1028         struct WorkSpace *UNUSED(workspace), struct Scene *UNUSED(scene),
1029         struct bScreen *UNUSED(screen), struct ScrArea *sa, struct ARegion *ar,
1030         struct wmMsgBus *mbus)
1031 {
1032         /* Developer note: there are many properties that impact 3D view drawing,
1033          * so instead of subscribing to individual properties, just subscribe to types
1034          * accepting some redundant redraws.
1035          *
1036          * For other space types we might try avoid this, keep the 3D view as an exceptional case! */
1037         wmMsgParams_RNA msg_key_params = {{{0}}};
1038
1039         /* Only subscribe to types. */
1040         StructRNA *type_array[] = {
1041                 &RNA_Window,
1042
1043                 /* These object have properties that impact drawing. */
1044                 &RNA_AreaLight,
1045                 &RNA_Camera,
1046                 &RNA_Light,
1047                 &RNA_Speaker,
1048                 &RNA_SunLight,
1049
1050                 /* General types the 3D view depends on. */
1051                 &RNA_Object,
1052                 &RNA_UnitSettings,  /* grid-floor */
1053
1054                 &RNA_View3DOverlay,
1055                 &RNA_View3DShading,
1056                 &RNA_World,
1057         };
1058
1059         wmMsgSubscribeValue msg_sub_value_region_tag_redraw = {
1060                 .owner = ar,
1061                 .user_data = ar,
1062                 .notify = ED_region_do_msg_notify_tag_redraw,
1063         };
1064
1065         for (int i = 0; i < ARRAY_SIZE(type_array); i++) {
1066                 msg_key_params.ptr.type = type_array[i];
1067                 WM_msg_subscribe_rna_params(
1068                         mbus,
1069                         &msg_key_params,
1070                         &msg_sub_value_region_tag_redraw,
1071                         __func__);
1072         }
1073
1074         /* Subscribe to a handful of other properties. */
1075         RegionView3D *rv3d = ar->regiondata;
1076
1077         WM_msg_subscribe_rna_anon_prop(mbus, RenderSettings, engine, &msg_sub_value_region_tag_redraw);
1078         WM_msg_subscribe_rna_anon_prop(mbus, RenderSettings, resolution_x, &msg_sub_value_region_tag_redraw);
1079         WM_msg_subscribe_rna_anon_prop(mbus, RenderSettings, resolution_y, &msg_sub_value_region_tag_redraw);
1080         WM_msg_subscribe_rna_anon_prop(mbus, RenderSettings, pixel_aspect_x, &msg_sub_value_region_tag_redraw);
1081         WM_msg_subscribe_rna_anon_prop(mbus, RenderSettings, pixel_aspect_y, &msg_sub_value_region_tag_redraw);
1082         if (rv3d->persp == RV3D_CAMOB) {
1083                 WM_msg_subscribe_rna_anon_prop(mbus, RenderSettings, use_border, &msg_sub_value_region_tag_redraw);
1084         }
1085
1086         WM_msg_subscribe_rna_anon_type(mbus, SceneEEVEE, &msg_sub_value_region_tag_redraw);
1087         WM_msg_subscribe_rna_anon_type(mbus, SceneLANPR, &msg_sub_value_region_tag_redraw);
1088         WM_msg_subscribe_rna_anon_type(mbus, SceneDisplay, &msg_sub_value_region_tag_redraw);
1089         WM_msg_subscribe_rna_anon_type(mbus, ObjectDisplay, &msg_sub_value_region_tag_redraw);
1090
1091         ViewLayer *view_layer = CTX_data_view_layer(C);
1092         Object *obact = OBACT(view_layer);
1093         if (obact != NULL) {
1094                 switch (obact->mode) {
1095                         case OB_MODE_PARTICLE_EDIT:
1096                                 WM_msg_subscribe_rna_anon_type(mbus, ParticleEdit, &msg_sub_value_region_tag_redraw);
1097                                 break;
1098                         default:
1099                                 break;
1100                 }
1101         }
1102
1103         {
1104                 wmMsgSubscribeValue msg_sub_value_region_tag_refresh = {
1105                         .owner = ar,
1106                         .user_data = sa,
1107                         .notify = WM_toolsystem_do_msg_notify_tag_refresh,
1108                 };
1109                 WM_msg_subscribe_rna_anon_prop(
1110                         mbus, Object, mode,
1111                         &msg_sub_value_region_tag_refresh);
1112                 WM_msg_subscribe_rna_anon_prop(
1113                         mbus, LayerObjects, active,
1114                         &msg_sub_value_region_tag_refresh);
1115         }
1116 }
1117
1118 /* concept is to retrieve cursor type context-less */
1119 static void view3d_main_region_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
1120 {
1121         if (WM_cursor_set_from_tool(win, sa, ar)) {
1122                 return;
1123         }
1124
1125         ViewLayer *view_layer = WM_window_get_active_view_layer(win);
1126         Object *obedit = OBEDIT_FROM_VIEW_LAYER(view_layer);
1127         if (obedit) {
1128                 WM_cursor_set(win, CURSOR_EDIT);
1129         }
1130         else {
1131                 WM_cursor_set(win, CURSOR_STD);
1132         }
1133 }
1134
1135 /* add handlers, stuff you only do once or on area/region changes */
1136 static void view3d_header_region_init(wmWindowManager *wm, ARegion *ar)
1137 {
1138         wmKeyMap *keymap = WM_keymap_ensure(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
1139
1140         WM_event_add_keymap_handler(&ar->handlers, keymap);
1141
1142         ED_region_header_init(ar);
1143 }
1144
1145 static void view3d_header_region_draw(const bContext *C, ARegion *ar)
1146 {
1147         ED_region_header(C, ar);
1148 }
1149
1150 static void view3d_header_region_listener(
1151         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
1152         wmNotifier *wmn, const Scene *UNUSED(scene))
1153 {
1154         /* context changes */
1155         switch (wmn->category) {
1156                 case NC_SCENE:
1157                         switch (wmn->data) {
1158                                 case ND_FRAME:
1159                                 case ND_OB_ACTIVE:
1160                                 case ND_OB_SELECT:
1161                                 case ND_OB_VISIBLE:
1162                                 case ND_MODE:
1163                                 case ND_LAYER:
1164                                 case ND_TOOLSETTINGS:
1165                                 case ND_LAYER_CONTENT:
1166                                 case ND_RENDER_OPTIONS:
1167                                         ED_region_tag_redraw(ar);
1168                                         break;
1169                         }
1170                         break;
1171                 case NC_SPACE:
1172                         if (wmn->data == ND_SPACE_VIEW3D) {
1173                                 ED_region_tag_redraw(ar);
1174                         }
1175                         break;
1176                 case NC_GPENCIL:
1177                         if (wmn->data & ND_GPENCIL_EDITMODE) {
1178                                 ED_region_tag_redraw(ar);
1179                         }
1180                         break;
1181         }
1182 }
1183
1184 static void view3d_header_region_message_subscribe(
1185         const struct bContext *UNUSED(C),
1186         struct WorkSpace *UNUSED(workspace), struct Scene *UNUSED(scene),
1187         struct bScreen *UNUSED(screen), struct ScrArea *UNUSED(sa), struct ARegion *ar,
1188         struct wmMsgBus *mbus)
1189 {
1190         wmMsgParams_RNA msg_key_params = {{{0}}};
1191
1192         /* Only subscribe to types. */
1193         StructRNA *type_array[] = {
1194                 &RNA_View3DShading,
1195         };
1196
1197         wmMsgSubscribeValue msg_sub_value_region_tag_redraw = {
1198                 .owner = ar,
1199                 .user_data = ar,
1200                 .notify = ED_region_do_msg_notify_tag_redraw,
1201         };
1202
1203         for (int i = 0; i < ARRAY_SIZE(type_array); i++) {
1204                 msg_key_params.ptr.type = type_array[i];
1205                 WM_msg_subscribe_rna_params(
1206                         mbus,
1207                         &msg_key_params,
1208                         &msg_sub_value_region_tag_redraw,
1209                         __func__);
1210         }
1211 }
1212
1213
1214 /* add handlers, stuff you only do once or on area/region changes */
1215 static void view3d_buttons_region_init(wmWindowManager *wm, ARegion *ar)
1216 {
1217         wmKeyMap *keymap;
1218
1219         ED_region_panels_init(wm, ar);
1220
1221         keymap = WM_keymap_ensure(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
1222         WM_event_add_keymap_handler(&ar->handlers, keymap);
1223 }
1224
1225 static void view3d_buttons_region_draw(const bContext *C, ARegion *ar)
1226 {
1227         ED_region_panels_ex(C, ar, (const char * []){CTX_data_mode_string(C), NULL}, -1, true);
1228 }
1229
1230 static void view3d_buttons_region_listener(
1231         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
1232         wmNotifier *wmn, const Scene *UNUSED(scene))
1233 {
1234         /* context changes */
1235         switch (wmn->category) {
1236                 case NC_ANIMATION:
1237                         switch (wmn->data) {
1238                                 case ND_KEYFRAME_PROP:
1239                                 case ND_NLA_ACTCHANGE:
1240                                         ED_region_tag_redraw(ar);
1241                                         break;
1242                                 case ND_NLA:
1243                                 case ND_KEYFRAME:
1244                                         if (ELEM(wmn->action, NA_EDITED, NA_ADDED, NA_REMOVED)) {
1245                                                 ED_region_tag_redraw(ar);
1246                                         }
1247                                         break;
1248                         }
1249                         break;
1250                 case NC_SCENE:
1251                         switch (wmn->data) {
1252                                 case ND_FRAME:
1253                                 case ND_OB_ACTIVE:
1254                                 case ND_OB_SELECT:
1255                                 case ND_OB_VISIBLE:
1256                                 case ND_MODE:
1257                                 case ND_LAYER:
1258                                 case ND_LAYER_CONTENT:
1259                                 case ND_TOOLSETTINGS:
1260                                         ED_region_tag_redraw(ar);
1261                                         break;
1262                         }
1263                         switch (wmn->action) {
1264                                 case NA_EDITED:
1265                                         ED_region_tag_redraw(ar);
1266                                         break;
1267                         }
1268                         break;
1269                 case NC_OBJECT:
1270                         switch (wmn->data) {
1271                                 case ND_BONE_ACTIVE:
1272                                 case ND_BONE_SELECT:
1273                                 case ND_TRANSFORM:
1274                                 case ND_POSE:
1275                                 case ND_DRAW:
1276                                 case ND_KEYS:
1277                                 case ND_MODIFIER:
1278                                         ED_region_tag_redraw(ar);
1279                                         break;
1280                         }
1281                         break;
1282                 case NC_GEOM:
1283                         switch (wmn->data) {
1284                                 case ND_DATA:
1285                                 case ND_VERTEX_GROUP:
1286                                 case ND_SELECT:
1287                                         ED_region_tag_redraw(ar);
1288                                         break;
1289                         }
1290                         if (wmn->action == NA_EDITED) {
1291                                 ED_region_tag_redraw(ar);
1292                         }
1293                         break;
1294                 case NC_TEXTURE:
1295                 case NC_MATERIAL:
1296                         /* for brush textures */
1297                         ED_region_tag_redraw(ar);
1298                         break;
1299                 case NC_BRUSH:
1300                         /* NA_SELECTED is used on brush changes */
1301                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED)) {
1302                                 ED_region_tag_redraw(ar);
1303                         }
1304                         break;
1305                 case NC_SPACE:
1306                         if (wmn->data == ND_SPACE_VIEW3D) {
1307                                 ED_region_tag_redraw(ar);
1308                         }
1309                         break;
1310                 case NC_ID:
1311                         if (wmn->action == NA_RENAME) {
1312                                 ED_region_tag_redraw(ar);
1313                         }
1314                         break;
1315                 case NC_GPENCIL:
1316                         if ((wmn->data & (ND_DATA | ND_GPENCIL_EDITMODE)) || (wmn->action == NA_EDITED)) {
1317                                 ED_region_tag_redraw(ar);
1318                         }
1319                         break;
1320                 case NC_IMAGE:
1321                         /* Update for the image layers in texture paint. */
1322                         if (wmn->action == NA_EDITED) {
1323                                 ED_region_tag_redraw(ar);
1324                         }
1325                         break;
1326         }
1327 }
1328
1329 /* add handlers, stuff you only do once or on area/region changes */
1330 static void view3d_tools_region_init(wmWindowManager *wm, ARegion *ar)
1331 {
1332         wmKeyMap *keymap;
1333
1334         ED_region_panels_init(wm, ar);
1335
1336         keymap = WM_keymap_ensure(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
1337         WM_event_add_keymap_handler(&ar->handlers, keymap);
1338 }
1339
1340 static void view3d_tools_region_draw(const bContext *C, ARegion *ar)
1341 {
1342         ED_region_panels_ex(C, ar, (const char * []){CTX_data_mode_string(C), NULL}, -1, true);
1343 }
1344
1345 /* area (not region) level listener */
1346 static void space_view3d_listener(
1347         wmWindow *UNUSED(win), ScrArea *sa, struct wmNotifier *wmn, Scene *UNUSED(scene))
1348 {
1349         View3D *v3d = sa->spacedata.first;
1350
1351         /* context changes */
1352         switch (wmn->category) {
1353                 case NC_SCENE:
1354                         switch (wmn->data) {
1355                                 case ND_WORLD:
1356                                         if (v3d->flag2 & V3D_HIDE_OVERLAYS) {
1357                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1358                                         }
1359                                         break;
1360                         }
1361                         break;
1362                 case NC_WORLD:
1363                         switch (wmn->data) {
1364                                 case ND_WORLD_DRAW:
1365                                 case ND_WORLD:
1366                                         if (v3d->shading.background_type == V3D_SHADING_BACKGROUND_WORLD) {
1367                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1368                                         }
1369                                         break;
1370                         }
1371                         break;
1372                 case NC_MATERIAL:
1373                         switch (wmn->data) {
1374                                 case ND_NODES:
1375                                         if (v3d->shading.type == OB_TEXTURE) {
1376                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1377                                         }
1378                                         break;
1379                         }
1380                         break;
1381         }
1382 }
1383
1384 static void space_view3d_refresh(const bContext *C, ScrArea *UNUSED(sa))
1385 {
1386         Scene *scene = CTX_data_scene(C);
1387         LightCache *lcache = scene->eevee.light_cache;
1388
1389         if (lcache && (lcache->flag & LIGHTCACHE_UPDATE_AUTO) != 0) {
1390                 lcache->flag &= ~LIGHTCACHE_UPDATE_AUTO;
1391                 view3d_lightcache_update((bContext *)C);
1392         }
1393 }
1394
1395 const char *view3d_context_dir[] = {
1396         "active_base", "active_object", NULL,
1397 };
1398
1399 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
1400 {
1401         /* fallback to the scene layer,
1402          * allows duplicate and other object operators to run outside the 3d view */
1403
1404         if (CTX_data_dir(member)) {
1405                 CTX_data_dir_set(result, view3d_context_dir);
1406         }
1407         else if (CTX_data_equals(member, "active_base")) {
1408                 Scene *scene = CTX_data_scene(C);
1409                 ViewLayer *view_layer = CTX_data_view_layer(C);
1410                 if (view_layer->basact) {
1411                         Object *ob = view_layer->basact->object;
1412                         /* if hidden but in edit mode, we still display, can happen with animation */
1413                         if ((view_layer->basact->flag & BASE_VISIBLE) != 0 || (ob->mode & OB_MODE_EDIT)) {
1414                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, view_layer->basact);
1415                         }
1416                 }
1417
1418                 return 1;
1419         }
1420         else if (CTX_data_equals(member, "active_object")) {
1421                 ViewLayer *view_layer = CTX_data_view_layer(C);
1422                 if (view_layer->basact) {
1423                         Object *ob = view_layer->basact->object;
1424                         /* if hidden but in edit mode, we still display, can happen with animation */
1425                         if ((view_layer->basact->flag & BASE_VISIBLE) != 0 || (ob->mode & OB_MODE_EDIT) != 0) {
1426                                 CTX_data_id_pointer_set(result, &ob->id);
1427                         }
1428                 }
1429
1430                 return 1;
1431         }
1432         else {
1433                 return 0; /* not found */
1434         }
1435
1436         return -1; /* found but not available */
1437 }
1438
1439 static void view3d_id_remap(ScrArea *sa, SpaceLink *slink, ID *old_id, ID *new_id)
1440 {
1441         View3D *v3d;
1442         ARegion *ar;
1443         bool is_local = false;
1444
1445         if (!ELEM(GS(old_id->name), ID_OB, ID_MA, ID_IM, ID_MC)) {
1446                 return;
1447         }
1448
1449         for (v3d = (View3D *)slink; v3d; v3d = v3d->localvd, is_local = true) {
1450                 if ((ID *)v3d->camera == old_id) {
1451                         v3d->camera = (Object *)new_id;
1452                         if (!new_id) {
1453                                 /* 3D view might be inactive, in that case needs to use slink->regionbase */
1454                                 ListBase *regionbase = (slink == sa->spacedata.first) ? &sa->regionbase : &slink->regionbase;
1455                                 for (ar = regionbase->first; ar; ar = ar->next) {
1456                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1457                                                 RegionView3D *rv3d = is_local ? ((RegionView3D *)ar->regiondata)->localvd : ar->regiondata;
1458                                                 if (rv3d && (rv3d->persp == RV3D_CAMOB)) {
1459                                                         rv3d->persp = RV3D_PERSP;
1460                                                 }
1461                                         }
1462                                 }
1463                         }
1464                 }
1465
1466                 /* Values in local-view aren't used, see: T52663 */
1467                 if (is_local == false) {
1468                         if ((ID *)v3d->ob_centre == old_id) {
1469                                 v3d->ob_centre = (Object *)new_id;
1470                                 /* Otherwise, bonename may remain valid...
1471                                  * We could be smart and check this, too? */
1472                                 if (new_id == NULL) {
1473                                         v3d->ob_centre_bone[0] = '\0';
1474                                 }
1475                         }
1476                 }
1477
1478                 if (is_local) {
1479                         break;
1480                 }
1481         }
1482 }
1483
1484 /* only called once, from space/spacetypes.c */
1485 void ED_spacetype_view3d(void)
1486 {
1487         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype view3d");
1488         ARegionType *art;
1489
1490         st->spaceid = SPACE_VIEW3D;
1491         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
1492
1493         st->new = view3d_new;
1494         st->free = view3d_free;
1495         st->init = view3d_init;
1496         st->listener = space_view3d_listener;
1497         st->refresh = space_view3d_refresh;
1498         st->duplicate = view3d_duplicate;
1499         st->operatortypes = view3d_operatortypes;
1500         st->keymap = view3d_keymap;
1501         st->dropboxes = view3d_dropboxes;
1502         st->gizmos = view3d_widgets;
1503         st->context = view3d_context;
1504         st->id_remap = view3d_id_remap;
1505
1506         /* regions: main window */
1507         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d main region");
1508         art->regionid = RGN_TYPE_WINDOW;
1509         art->keymapflag = ED_KEYMAP_GIZMO | ED_KEYMAP_TOOL | ED_KEYMAP_GPENCIL;
1510         art->draw = view3d_main_region_draw;
1511         art->init = view3d_main_region_init;
1512         art->exit = view3d_main_region_exit;
1513         art->free = view3d_main_region_free;
1514         art->duplicate = view3d_main_region_duplicate;
1515         art->listener = view3d_main_region_listener;
1516         art->message_subscribe = view3d_main_region_message_subscribe;
1517         art->cursor = view3d_main_region_cursor;
1518         art->lock = 1;   /* can become flag, see BKE_spacedata_draw_locks */
1519         BLI_addhead(&st->regiontypes, art);
1520
1521         /* regions: listview/buttons */
1522         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d buttons region");
1523         art->regionid = RGN_TYPE_UI;
1524         art->prefsizex = 180; /* XXX */
1525         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1526         art->listener = view3d_buttons_region_listener;
1527         art->init = view3d_buttons_region_init;
1528         art->draw = view3d_buttons_region_draw;
1529         BLI_addhead(&st->regiontypes, art);
1530
1531         view3d_buttons_register(art);
1532
1533         /* regions: tool(bar) */
1534         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d tools region");
1535         art->regionid = RGN_TYPE_TOOLS;
1536         art->prefsizex = 58; /* XXX */
1537         art->prefsizey = 50; /* XXX */
1538         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1539         art->listener = view3d_buttons_region_listener;
1540         art->message_subscribe = ED_region_generic_tools_region_message_subscribe;
1541         art->snap_size = ED_region_generic_tools_region_snap_size;
1542         art->init = view3d_tools_region_init;
1543         art->draw = view3d_tools_region_draw;
1544         BLI_addhead(&st->regiontypes, art);
1545
1546         /* regions: header */
1547         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d header region");
1548         art->regionid = RGN_TYPE_HEADER;
1549         art->prefsizey = HEADERY;
1550         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
1551         art->listener = view3d_header_region_listener;
1552         art->init = view3d_header_region_init;
1553         art->draw = view3d_header_region_draw;
1554         art->message_subscribe = view3d_header_region_message_subscribe;
1555         BLI_addhead(&st->regiontypes, art);
1556
1557         /* regions: hud */
1558         art = ED_area_type_hud(st->spaceid);
1559         BLI_addhead(&st->regiontypes, art);
1560
1561         BKE_spacetype_register(st);
1562 }