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