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