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