3c850ae1689200fdee92a8bb3ef1e8eab6062dca
[blender.git] / source / blender / editors / space_image / space_image.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_image/space_image.c
28  *  \ingroup spimage
29  */
30
31 #include "DNA_gpencil_types.h"
32 #include "DNA_mesh_types.h"
33 #include "DNA_mask_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_image_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_threads.h"
44
45 #include "BKE_colortools.h"
46 #include "BKE_context.h"
47 #include "BKE_editmesh.h"
48 #include "BKE_image.h"
49 #include "BKE_layer.h"
50 #include "BKE_library.h"
51 #include "BKE_material.h"
52 #include "BKE_scene.h"
53 #include "BKE_screen.h"
54 #include "BKE_workspace.h"
55
56 #include "RNA_access.h"
57 #include "RNA_define.h"
58 #include "RNA_enum_types.h"
59
60 #include "DEG_depsgraph.h"
61
62 #include "IMB_imbuf_types.h"
63
64 #include "ED_image.h"
65 #include "ED_mask.h"
66 #include "ED_mesh.h"
67 #include "ED_node.h"
68 #include "ED_render.h"
69 #include "ED_space_api.h"
70 #include "ED_screen.h"
71 #include "ED_uvedit.h"
72 #include "ED_transform.h"
73
74 #include "BIF_gl.h"
75
76 #include "WM_api.h"
77 #include "WM_types.h"
78 #include "WM_message.h"
79
80 #include "UI_resources.h"
81 #include "UI_interface.h"
82 #include "UI_view2d.h"
83
84 #include "image_intern.h"
85 #include "GPU_framebuffer.h"
86
87 /**************************** common state *****************************/
88
89 static void image_scopes_tag_refresh(ScrArea *sa)
90 {
91         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
92         ARegion *ar;
93
94         /* only while histogram is visible */
95         for (ar = sa->regionbase.first; ar; ar = ar->next) {
96                 if (ar->regiontype == RGN_TYPE_TOOLS && ar->flag & RGN_FLAG_HIDDEN)
97                         return;
98         }
99
100         sima->scopes.ok = 0;
101 }
102
103 static void image_user_refresh_scene(const bContext *C, SpaceImage *sima)
104 {
105         if (sima->image && sima->image->type == IMA_TYPE_R_RESULT) {
106                 /* for render result, try to use the currently rendering scene */
107                 Scene *render_scene = ED_render_job_get_current_scene(C);
108                 if (render_scene) {
109                         sima->iuser.scene = render_scene;
110                         return;
111                 }
112         }
113         sima->iuser.scene = CTX_data_scene(C);
114 }
115
116 /* ******************** manage regions ********************* */
117
118 ARegion *image_has_buttons_region(ScrArea *sa)
119 {
120         ARegion *ar, *arnew;
121
122         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
123         if (ar) return ar;
124
125         /* add subdiv level; after header */
126         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
127
128         /* is error! */
129         if (ar == NULL) return NULL;
130
131         arnew = MEM_callocN(sizeof(ARegion), "buttons for image");
132
133         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
134         arnew->regiontype = RGN_TYPE_UI;
135         arnew->alignment = RGN_ALIGN_RIGHT;
136
137         arnew->flag = RGN_FLAG_HIDDEN;
138
139         return arnew;
140 }
141
142 ARegion *image_has_tools_region(ScrArea *sa)
143 {
144         ARegion *ar, *arnew;
145
146         ar = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
147         if (ar) return ar;
148
149         /* add subdiv level; after buttons */
150         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
151
152         /* is error! */
153         if (ar == NULL) return NULL;
154
155         arnew = MEM_callocN(sizeof(ARegion), "scopes for image");
156
157         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
158         arnew->regiontype = RGN_TYPE_TOOLS;
159         arnew->alignment = RGN_ALIGN_LEFT;
160
161         arnew->flag = RGN_FLAG_HIDDEN;
162
163         image_scopes_tag_refresh(sa);
164
165         return arnew;
166 }
167
168 /* ******************** default callbacks for image space ***************** */
169
170 static SpaceLink *image_new(const ScrArea *UNUSED(area), const Scene *UNUSED(scene))
171 {
172         ARegion *ar;
173         SpaceImage *simage;
174
175         simage = MEM_callocN(sizeof(SpaceImage), "initimage");
176         simage->spacetype = SPACE_IMAGE;
177         simage->zoom = 1.0f;
178         simage->lock = true;
179         simage->flag = SI_SHOW_GPENCIL | SI_USE_ALPHA | SI_COORDFLOATS;
180
181         simage->iuser.ok = true;
182         simage->iuser.frames = 100;
183         simage->iuser.flag = IMA_SHOW_STEREO | IMA_ANIM_ALWAYS;
184
185         scopes_new(&simage->scopes);
186         simage->sample_line_hist.height = 100;
187
188         /* header */
189         ar = MEM_callocN(sizeof(ARegion), "header for image");
190
191         BLI_addtail(&simage->regionbase, ar);
192         ar->regiontype = RGN_TYPE_HEADER;
193         ar->alignment = (U.uiflag & USER_HEADER_BOTTOM) ? RGN_ALIGN_BOTTOM : RGN_ALIGN_TOP;
194
195         /* buttons/list view */
196         ar = MEM_callocN(sizeof(ARegion), "buttons for image");
197
198         BLI_addtail(&simage->regionbase, ar);
199         ar->regiontype = RGN_TYPE_UI;
200         ar->alignment = RGN_ALIGN_RIGHT;
201         ar->flag = RGN_FLAG_HIDDEN;
202
203         /* scopes/uv sculpt/paint */
204         ar = MEM_callocN(sizeof(ARegion), "buttons for image");
205
206         BLI_addtail(&simage->regionbase, ar);
207         ar->regiontype = RGN_TYPE_TOOLS;
208         ar->alignment = RGN_ALIGN_LEFT;
209         ar->flag = RGN_FLAG_HIDDEN;
210
211         /* main area */
212         ar = MEM_callocN(sizeof(ARegion), "main area for image");
213
214         BLI_addtail(&simage->regionbase, ar);
215         ar->regiontype = RGN_TYPE_WINDOW;
216
217         return (SpaceLink *)simage;
218 }
219
220 /* not spacelink itself */
221 static void image_free(SpaceLink *sl)
222 {
223         SpaceImage *simage = (SpaceImage *) sl;
224
225         scopes_free(&simage->scopes);
226 }
227
228
229 /* spacetype; init callback, add handlers */
230 static void image_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
231 {
232         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
233
234         /* add drop boxes */
235         WM_event_add_dropbox_handler(&sa->handlers, lb);
236
237 }
238
239 static SpaceLink *image_duplicate(SpaceLink *sl)
240 {
241         SpaceImage *simagen = MEM_dupallocN(sl);
242
243         /* clear or remove stuff from old */
244
245         scopes_new(&simagen->scopes);
246
247         return (SpaceLink *)simagen;
248 }
249
250 static void image_operatortypes(void)
251 {
252         WM_operatortype_append(IMAGE_OT_view_all);
253         WM_operatortype_append(IMAGE_OT_view_pan);
254         WM_operatortype_append(IMAGE_OT_view_selected);
255         WM_operatortype_append(IMAGE_OT_view_zoom);
256         WM_operatortype_append(IMAGE_OT_view_zoom_in);
257         WM_operatortype_append(IMAGE_OT_view_zoom_out);
258         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
259         WM_operatortype_append(IMAGE_OT_view_zoom_border);
260 #ifdef WITH_INPUT_NDOF
261         WM_operatortype_append(IMAGE_OT_view_ndof);
262 #endif
263
264         WM_operatortype_append(IMAGE_OT_new);
265         WM_operatortype_append(IMAGE_OT_open);
266         WM_operatortype_append(IMAGE_OT_match_movie_length);
267         WM_operatortype_append(IMAGE_OT_replace);
268         WM_operatortype_append(IMAGE_OT_reload);
269         WM_operatortype_append(IMAGE_OT_save);
270         WM_operatortype_append(IMAGE_OT_save_as);
271         WM_operatortype_append(IMAGE_OT_save_sequence);
272         WM_operatortype_append(IMAGE_OT_pack);
273         WM_operatortype_append(IMAGE_OT_unpack);
274
275         WM_operatortype_append(IMAGE_OT_invert);
276
277         WM_operatortype_append(IMAGE_OT_cycle_render_slot);
278         WM_operatortype_append(IMAGE_OT_clear_render_slot);
279         WM_operatortype_append(IMAGE_OT_add_render_slot);
280         WM_operatortype_append(IMAGE_OT_remove_render_slot);
281
282         WM_operatortype_append(IMAGE_OT_sample);
283         WM_operatortype_append(IMAGE_OT_sample_line);
284         WM_operatortype_append(IMAGE_OT_curves_point_set);
285
286         WM_operatortype_append(IMAGE_OT_properties);
287         WM_operatortype_append(IMAGE_OT_toolshelf);
288
289         WM_operatortype_append(IMAGE_OT_change_frame);
290
291         WM_operatortype_append(IMAGE_OT_read_viewlayers);
292         WM_operatortype_append(IMAGE_OT_render_border);
293         WM_operatortype_append(IMAGE_OT_clear_render_border);
294 }
295
296 static void image_keymap(struct wmKeyConfig *keyconf)
297 {
298         WM_keymap_ensure(keyconf, "Image Generic", SPACE_IMAGE, 0);
299         WM_keymap_ensure(keyconf, "Image", SPACE_IMAGE, 0);
300 }
301
302 /* dropboxes */
303 static bool image_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
304 {
305         if (drag->type == WM_DRAG_PATH)
306                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE, ICON_FILE_BLANK)) /* rule might not work? */
307                         return 1;
308         return 0;
309 }
310
311 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
312 {
313         /* copy drag path to properties */
314         RNA_string_set(drop->ptr, "filepath", drag->path);
315 }
316
317 /* area+region dropbox definition */
318 static void image_dropboxes(void)
319 {
320         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
321
322         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
323 }
324
325 /**
326  * \note take care not to get into feedback loop here,
327  *       calling composite job causes viewer to refresh.
328  */
329 static void image_refresh(const bContext *C, ScrArea *sa)
330 {
331         Scene *scene = CTX_data_scene(C);
332         SpaceImage *sima = sa->spacedata.first;
333         Image *ima;
334
335         ima = ED_space_image(sima);
336
337         BKE_image_user_check_frame_calc(&sima->iuser, scene->r.cfra);
338
339         /* check if we have to set the image from the editmesh */
340         if (ima && (ima->source == IMA_SRC_VIEWER && sima->mode == SI_MODE_MASK)) {
341                 if (scene->nodetree) {
342                         Mask *mask = ED_space_image_get_mask(sima);
343                         if (mask) {
344                                 ED_node_composite_job(C, scene->nodetree, scene);
345                         }
346                 }
347         }
348 }
349
350 static void image_listener(wmWindow *win, ScrArea *sa, wmNotifier *wmn, Scene *UNUSED(scene))
351 {
352         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
353
354         /* context changes */
355         switch (wmn->category) {
356                 case NC_WINDOW:
357                         /* notifier comes from editing color space */
358                         image_scopes_tag_refresh(sa);
359                         ED_area_tag_redraw(sa);
360                         break;
361                 case NC_SCENE:
362                         switch (wmn->data) {
363                                 case ND_FRAME:
364                                         image_scopes_tag_refresh(sa);
365                                         ED_area_tag_refresh(sa);
366                                         ED_area_tag_redraw(sa);
367                                         break;
368                                 case ND_MODE:
369                                         if (wmn->subtype == NS_EDITMODE_MESH)
370                                                 ED_area_tag_refresh(sa);
371                                         ED_area_tag_redraw(sa);
372                                         break;
373                                 case ND_RENDER_RESULT:
374                                 case ND_RENDER_OPTIONS:
375                                 case ND_COMPO_RESULT:
376                                         if (ED_space_image_show_render(sima))
377                                                 image_scopes_tag_refresh(sa);
378                                         ED_area_tag_redraw(sa);
379                                         break;
380                         }
381                         break;
382                 case NC_IMAGE:
383                         if (wmn->reference == sima->image || !wmn->reference) {
384                                 if (wmn->action != NA_PAINTING) {
385                                         image_scopes_tag_refresh(sa);
386                                         ED_area_tag_refresh(sa);
387                                         ED_area_tag_redraw(sa);
388                                 }
389                         }
390                         break;
391                 case NC_SPACE:
392                         if (wmn->data == ND_SPACE_IMAGE) {
393                                 image_scopes_tag_refresh(sa);
394                                 ED_area_tag_redraw(sa);
395                         }
396                         break;
397                 case NC_MASK:
398                 {
399                         // Scene *scene = wmn->window->screen->scene;
400                         /* ideally would check for: ED_space_image_check_show_maskedit(scene, sima) but we cant get the scene */
401                         if (sima->mode == SI_MODE_MASK) {
402                                 switch (wmn->data) {
403                                         case ND_SELECT:
404                                                 ED_area_tag_redraw(sa);
405                                                 break;
406                                         case ND_DATA:
407                                         case ND_DRAW:
408                                                 /* causes node-recalc */
409                                                 ED_area_tag_redraw(sa);
410                                                 ED_area_tag_refresh(sa);
411                                                 break;
412                                 }
413                                 switch (wmn->action) {
414                                         case NA_SELECTED:
415                                                 ED_area_tag_redraw(sa);
416                                                 break;
417                                         case NA_EDITED:
418                                                 /* causes node-recalc */
419                                                 ED_area_tag_redraw(sa);
420                                                 ED_area_tag_refresh(sa);
421                                                 break;
422                                 }
423                         }
424                         break;
425                 }
426                 case NC_GEOM:
427                 {
428                         switch (wmn->data) {
429                                 case ND_DATA:
430                                 case ND_SELECT:
431                                         image_scopes_tag_refresh(sa);
432                                         ED_area_tag_refresh(sa);
433                                         ED_area_tag_redraw(sa);
434                                         break;
435                         }
436                         break;
437                 }
438                 case NC_OBJECT:
439                 {
440                         switch (wmn->data) {
441                                 case ND_TRANSFORM:
442                                 case ND_MODIFIER:
443                                 {
444                                         ViewLayer *view_layer = WM_window_get_active_view_layer(win);
445                                         Object *ob = OBACT(view_layer);
446                                         if (ob && (ob == wmn->reference) && (ob->mode & OB_MODE_EDIT)) {
447                                                 if (sima->lock && (sima->flag & SI_DRAWSHADOW)) {
448                                                         ED_area_tag_refresh(sa);
449                                                         ED_area_tag_redraw(sa);
450                                                 }
451                                         }
452                                         break;
453                                 }
454                         }
455
456                         break;
457                 }
458                 case NC_ID:
459                 {
460                         if (wmn->action == NA_RENAME) {
461                                 ED_area_tag_redraw(sa);
462                         }
463                         break;
464                 }
465                 case NC_WM:
466                         if (wmn->data == ND_UNDO) {
467                                 ED_area_tag_redraw(sa);
468                                 ED_area_tag_refresh(sa);
469                         }
470                         break;
471         }
472 }
473
474 const char *image_context_dir[] = {"edit_image", "edit_mask", NULL};
475
476 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
477 {
478         SpaceImage *sima = CTX_wm_space_image(C);
479
480         if (CTX_data_dir(member)) {
481                 CTX_data_dir_set(result, image_context_dir);
482         }
483         else if (CTX_data_equals(member, "edit_image")) {
484                 CTX_data_id_pointer_set(result, (ID *)ED_space_image(sima));
485                 return 1;
486         }
487         else if (CTX_data_equals(member, "edit_mask")) {
488                 Mask *mask = ED_space_image_get_mask(sima);
489                 if (mask) {
490                         CTX_data_id_pointer_set(result, &mask->id);
491                 }
492                 return true;
493         }
494         return 0;
495 }
496
497 static void IMAGE_GGT_gizmo2d(wmGizmoGroupType *gzgt)
498 {
499         gzgt->name = "UV Transform Gizmo";
500         gzgt->idname = "IMAGE_GGT_gizmo2d";
501
502         gzgt->gzmap_params.spaceid = SPACE_IMAGE;
503         gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
504
505         gzgt->poll = ED_widgetgroup_gizmo2d_poll;
506         gzgt->setup = ED_widgetgroup_gizmo2d_setup;
507         gzgt->refresh = ED_widgetgroup_gizmo2d_refresh;
508         gzgt->draw_prepare = ED_widgetgroup_gizmo2d_draw_prepare;
509 }
510
511 static void image_widgets(void)
512 {
513         WM_gizmogrouptype_append(IMAGE_GGT_gizmo2d);
514 }
515
516 /************************** main region ***************************/
517
518 /* sets up the fields of the View2D from zoom and offset */
519 static void image_main_region_set_view2d(SpaceImage *sima, ARegion *ar)
520 {
521         Image *ima = ED_space_image(sima);
522
523         int width, height;
524         ED_space_image_get_size(sima, &width, &height);
525
526         float w = width;
527         float h = height;
528
529         if (ima)
530                 h *= ima->aspy / ima->aspx;
531
532         int winx = BLI_rcti_size_x(&ar->winrct) + 1;
533         int winy = BLI_rcti_size_y(&ar->winrct) + 1;
534
535         /* For region overlap, move center so image doesn't overlap header. */
536         rcti visible_rect;
537         ED_region_visible_rect(ar, &visible_rect);
538         const int visible_winy = BLI_rcti_size_y(&visible_rect) + 1;
539         int visible_centerx = 0;
540         int visible_centery = visible_rect.ymin + (visible_winy - winy) / 2;
541
542         ar->v2d.tot.xmin = 0;
543         ar->v2d.tot.ymin = 0;
544         ar->v2d.tot.xmax = w;
545         ar->v2d.tot.ymax = h;
546
547         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
548         ar->v2d.mask.xmax = winx;
549         ar->v2d.mask.ymax = winy;
550
551         /* which part of the image space do we see? */
552         float x1 = ar->winrct.xmin + visible_centerx + (winx - sima->zoom * w) / 2.0f;
553         float y1 = ar->winrct.ymin + visible_centery + (winy - sima->zoom * h) / 2.0f;
554
555         x1 -= sima->zoom * sima->xof;
556         y1 -= sima->zoom * sima->yof;
557
558         /* relative display right */
559         ar->v2d.cur.xmin = ((ar->winrct.xmin - (float)x1) / sima->zoom);
560         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sima->zoom);
561
562         /* relative display left */
563         ar->v2d.cur.ymin = ((ar->winrct.ymin - (float)y1) / sima->zoom);
564         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sima->zoom);
565
566         /* normalize 0.0..1.0 */
567         ar->v2d.cur.xmin /= w;
568         ar->v2d.cur.xmax /= w;
569         ar->v2d.cur.ymin /= h;
570         ar->v2d.cur.ymax /= h;
571 }
572
573 /* add handlers, stuff you only do once or on area/region changes */
574 static void image_main_region_init(wmWindowManager *wm, ARegion *ar)
575 {
576         wmKeyMap *keymap;
577
578         // image space manages own v2d
579         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
580
581         /* mask polls mode */
582         keymap = WM_keymap_ensure(wm->defaultconf, "Mask Editing", 0, 0);
583         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
584
585         /* image paint polls for mode */
586         keymap = WM_keymap_ensure(wm->defaultconf, "Curve", 0, 0);
587         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
588
589         keymap = WM_keymap_ensure(wm->defaultconf, "Paint Curve", 0, 0);
590         WM_event_add_keymap_handler(&ar->handlers, keymap);
591
592         keymap = WM_keymap_ensure(wm->defaultconf, "Image Paint", 0, 0);
593         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
594
595         keymap = WM_keymap_ensure(wm->defaultconf, "UV Editor", 0, 0);
596         WM_event_add_keymap_handler(&ar->handlers, keymap);
597
598         keymap = WM_keymap_ensure(wm->defaultconf, "UV Sculpt", 0, 0);
599         WM_event_add_keymap_handler(&ar->handlers, keymap);
600
601         /* own keymaps */
602         keymap = WM_keymap_ensure(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
603         WM_event_add_keymap_handler(&ar->handlers, keymap);
604         keymap = WM_keymap_ensure(wm->defaultconf, "Image", SPACE_IMAGE, 0);
605         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
606 }
607
608 static void image_main_region_draw(const bContext *C, ARegion *ar)
609 {
610         /* draw entirely, view changes should be handled here */
611         SpaceImage *sima = CTX_wm_space_image(C);
612         Object *obact = CTX_data_active_object(C);
613         Object *obedit = CTX_data_edit_object(C);
614         Depsgraph *depsgraph = CTX_data_depsgraph(C);
615         Mask *mask = NULL;
616         bool curve = false;
617         Scene *scene = CTX_data_scene(C);
618         ViewLayer *view_layer = CTX_data_view_layer(C);
619         View2D *v2d = &ar->v2d;
620         //View2DScrollers *scrollers;
621         float col[3];
622
623         /* XXX not supported yet, disabling for now */
624         scene->r.scemode &= ~R_COMP_CROP;
625
626         /* clear and setup matrix */
627         UI_GetThemeColor3fv(TH_BACK, col);
628         GPU_clear_color(col[0], col[1], col[2], 0.0);
629         GPU_clear(GPU_COLOR_BIT);
630
631         image_user_refresh_scene(C, sima);
632
633         /* we set view2d from own zoom and offset each time */
634         image_main_region_set_view2d(sima, ar);
635
636         /* we draw image in pixelspace */
637         draw_image_main(C, ar);
638
639         /* and uvs in 0.0-1.0 space */
640         UI_view2d_view_ortho(v2d);
641
642         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
643
644         ED_uvedit_draw_main(sima, ar, scene, view_layer, obedit, obact, depsgraph);
645
646         /* check for mask (delay draw) */
647         if (ED_space_image_show_uvedit(sima, obedit)) {
648                 /* pass */
649         }
650         else if (sima->mode == SI_MODE_MASK) {
651                 mask = ED_space_image_get_mask(sima);
652         }
653         else if (ED_space_image_paint_curve(C)) {
654                 curve = true;
655         }
656
657         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
658
659         if (sima->flag & SI_SHOW_GPENCIL) {
660                 /* Grease Pencil too (in addition to UV's) */
661                 draw_image_grease_pencil((bContext *)C, true);
662         }
663
664         /* sample line */
665         draw_image_sample_line(sima);
666
667         UI_view2d_view_restore(C);
668
669         if (sima->flag & SI_SHOW_GPENCIL) {
670                 /* draw Grease Pencil - screen space only */
671                 draw_image_grease_pencil((bContext *)C, false);
672         }
673
674         if (mask) {
675                 Image *image = ED_space_image(sima);
676                 int width, height, show_viewer;
677                 float aspx, aspy;
678
679                 show_viewer = (image && image->source == IMA_SRC_VIEWER);
680
681                 if (show_viewer) {
682                         /* ED_space_image_get* will acquire image buffer which requires
683                          * lock here by the same reason why lock is needed in draw_image_main
684                          */
685                         BLI_thread_lock(LOCK_DRAW_IMAGE);
686                 }
687
688                 ED_space_image_get_size(sima, &width, &height);
689                 ED_space_image_get_aspect(sima, &aspx, &aspy);
690
691                 if (show_viewer)
692                         BLI_thread_unlock(LOCK_DRAW_IMAGE);
693
694                 ED_mask_draw_region(mask, ar,
695                                     sima->mask_info.draw_flag,
696                                     sima->mask_info.draw_type,
697                                     sima->mask_info.overlay_mode,
698                                     width, height,
699                                     aspx, aspy,
700                                     true, false,
701                                     NULL, C);
702
703                 UI_view2d_view_ortho(v2d);
704                 ED_image_draw_cursor(ar, sima->cursor);
705                 UI_view2d_view_restore(C);
706         }
707         else if (curve) {
708                 UI_view2d_view_ortho(v2d);
709                 ED_image_draw_cursor(ar, sima->cursor);
710                 UI_view2d_view_restore(C);
711         }
712
713         WM_gizmomap_draw(ar->gizmo_map, C, WM_GIZMOMAP_DRAWSTEP_2D);
714
715         draw_image_cache(C, ar);
716
717         /* scrollers? */
718 #if 0
719         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
720         UI_view2d_scrollers_draw(C, v2d, scrollers);
721         UI_view2d_scrollers_free(scrollers);
722 #endif
723 }
724
725 static void image_main_region_listener(
726         wmWindow *UNUSED(win), ScrArea *sa, ARegion *ar,
727         wmNotifier *wmn, const Scene *UNUSED(scene))
728 {
729         /* context changes */
730         switch (wmn->category) {
731                 case NC_GEOM:
732                         if (ELEM(wmn->data, ND_DATA, ND_SELECT))
733                                 WM_gizmomap_tag_refresh(ar->gizmo_map);
734                         break;
735                 case NC_GPENCIL:
736                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
737                                 ED_region_tag_redraw(ar);
738                         else if (wmn->data & ND_GPENCIL_EDITMODE)
739                                 ED_region_tag_redraw(ar);
740                         break;
741                 case NC_IMAGE:
742                         if (wmn->action == NA_PAINTING)
743                                 ED_region_tag_redraw(ar);
744                         WM_gizmomap_tag_refresh(ar->gizmo_map);
745                         break;
746                 case NC_MATERIAL:
747                         if (wmn->data == ND_SHADING_LINKS) {
748                                 SpaceImage *sima = sa->spacedata.first;
749
750                                 if (sima->iuser.scene && (sima->iuser.scene->toolsettings->uv_flag & UV_SHOW_SAME_IMAGE))
751                                         ED_region_tag_redraw(ar);
752                         }
753                         break;
754                 case NC_SCREEN:
755                         if (ELEM(wmn->data, ND_LAYER)) {
756                                 ED_region_tag_redraw(ar);
757                         }
758                         break;
759         }
760 }
761
762 /* *********************** buttons region ************************ */
763
764 /* add handlers, stuff you only do once or on area/region changes */
765 static void image_buttons_region_init(wmWindowManager *wm, ARegion *ar)
766 {
767         wmKeyMap *keymap;
768
769         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
770         ED_region_panels_init(wm, ar);
771
772         keymap = WM_keymap_ensure(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
773         WM_event_add_keymap_handler(&ar->handlers, keymap);
774 }
775
776 static void image_buttons_region_draw(const bContext *C, ARegion *ar)
777 {
778         ED_region_panels(C, ar);
779 }
780
781 static void image_buttons_region_listener(
782         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
783         wmNotifier *wmn, const Scene *UNUSED(scene))
784 {
785         /* context changes */
786         switch (wmn->category) {
787                 case NC_TEXTURE:
788                 case NC_MATERIAL:
789                         /* sending by texture render job and needed to properly update displaying
790                          * brush texture icon */
791                         ED_region_tag_redraw(ar);
792                         break;
793                 case NC_SCENE:
794                         switch (wmn->data) {
795                                 case ND_MODE:
796                                 case ND_RENDER_RESULT:
797                                 case ND_COMPO_RESULT:
798                                         ED_region_tag_redraw(ar);
799                                         break;
800                         }
801                         break;
802                 case NC_IMAGE:
803                         if (wmn->action != NA_PAINTING)
804                                 ED_region_tag_redraw(ar);
805                         break;
806                 case NC_NODE:
807                         ED_region_tag_redraw(ar);
808                         break;
809                 case NC_GPENCIL:
810                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
811                                 ED_region_tag_redraw(ar);
812                         break;
813         }
814 }
815
816 /* *********************** scopes region ************************ */
817
818 /* add handlers, stuff you only do once or on area/region changes */
819 static void image_tools_region_init(wmWindowManager *wm, ARegion *ar)
820 {
821         wmKeyMap *keymap;
822
823         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
824         ED_region_panels_init(wm, ar);
825
826         keymap = WM_keymap_ensure(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
827         WM_event_add_keymap_handler(&ar->handlers, keymap);
828 }
829
830 static void image_tools_region_draw(const bContext *C, ARegion *ar)
831 {
832         SpaceImage *sima = CTX_wm_space_image(C);
833         Scene *scene = CTX_data_scene(C);
834         void *lock;
835         ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
836         /* XXX performance regression if name of scopes category changes! */
837         PanelCategoryStack *category = UI_panel_category_active_find(ar, "Scopes");
838
839         /* only update scopes if scope category is active */
840         if (category) {
841                 if (ibuf) {
842                         if (!sima->scopes.ok) {
843                                 BKE_histogram_update_sample_line(&sima->sample_line_hist, ibuf, &scene->view_settings, &scene->display_settings);
844                         }
845                         if (sima->image->flag & IMA_VIEW_AS_RENDER)
846                                 ED_space_image_scopes_update(C, sima, ibuf, true);
847                         else
848                                 ED_space_image_scopes_update(C, sima, ibuf, false);
849                 }
850         }
851         ED_space_image_release_buffer(sima, ibuf, lock);
852
853         ED_region_panels(C, ar);
854 }
855
856 static void image_tools_region_listener(
857         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
858         wmNotifier *wmn, const Scene *UNUSED(scene))
859 {
860         /* context changes */
861         switch (wmn->category) {
862                 case NC_GPENCIL:
863                         if (wmn->data == ND_DATA || ELEM(wmn->action, NA_EDITED, NA_SELECTED))
864                                 ED_region_tag_redraw(ar);
865                         break;
866                 case NC_BRUSH:
867                         /* NA_SELECTED is used on brush changes */
868                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
869                                 ED_region_tag_redraw(ar);
870                         break;
871                 case NC_SCENE:
872                         switch (wmn->data) {
873                                 case ND_MODE:
874                                 case ND_RENDER_RESULT:
875                                 case ND_COMPO_RESULT:
876                                         ED_region_tag_redraw(ar);
877                                         break;
878                         }
879                         break;
880                 case NC_IMAGE:
881                         if (wmn->action != NA_PAINTING)
882                                 ED_region_tag_redraw(ar);
883                         break;
884                 case NC_NODE:
885                         ED_region_tag_redraw(ar);
886                         break;
887
888         }
889 }
890
891 /************************* header region **************************/
892
893 /* add handlers, stuff you only do once or on area/region changes */
894 static void image_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
895 {
896         ED_region_header_init(ar);
897 }
898
899 static void image_header_region_draw(const bContext *C, ARegion *ar)
900 {
901         ScrArea *sa = CTX_wm_area(C);
902         SpaceImage *sima = sa->spacedata.first;
903
904         image_user_refresh_scene(C, sima);
905
906         ED_region_header(C, ar);
907 }
908
909 static void image_header_region_listener(
910         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
911         wmNotifier *wmn, const Scene *UNUSED(scene))
912 {
913         /* context changes */
914         switch (wmn->category) {
915                 case NC_SCENE:
916                         switch (wmn->data) {
917                                 case ND_MODE:
918                                 case ND_TOOLSETTINGS:
919                                         ED_region_tag_redraw(ar);
920                                         break;
921                         }
922                         break;
923                 case NC_GEOM:
924                         switch (wmn->data) {
925                                 case ND_DATA:
926                                 case ND_SELECT:
927                                         ED_region_tag_redraw(ar);
928                                         break;
929                         }
930                         break;
931         }
932 }
933
934 static void image_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id)
935 {
936         SpaceImage *simg = (SpaceImage *)slink;
937
938         if (!ELEM(GS(old_id->name), ID_IM, ID_GD, ID_MSK)) {
939                 return;
940         }
941
942         if ((ID *)simg->image == old_id) {
943                 simg->image = (Image *)new_id;
944                 id_us_ensure_real(new_id);
945         }
946
947         if ((ID *)simg->gpd == old_id) {
948                 simg->gpd = (bGPdata *)new_id;
949                 id_us_min(old_id);
950                 id_us_plus(new_id);
951         }
952
953         if ((ID *)simg->mask_info.mask == old_id) {
954                 simg->mask_info.mask = (Mask *)new_id;
955                 id_us_ensure_real(new_id);
956         }
957 }
958
959 /**
960  * \note Used for splitting out a subset of modes is more involved,
961  * The previous non-uv-edit mode is stored so switching back to the
962  * image doesn't always reset the sub-mode.
963  */
964 static int image_space_subtype_get(ScrArea *sa)
965 {
966         SpaceImage *sima = sa->spacedata.first;
967         return sima->mode == SI_MODE_UV ? SI_MODE_UV : SI_MODE_VIEW;
968 }
969
970 static void image_space_subtype_set(ScrArea *sa, int value)
971 {
972         SpaceImage *sima = sa->spacedata.first;
973         if (value == SI_MODE_UV) {
974                 if (sima->mode != SI_MODE_UV) {
975                         sima->mode_prev = sima->mode;
976                 }
977                 sima->mode = value;
978         }
979         else {
980                 sima->mode = sima->mode_prev;
981         }
982 }
983
984 static void image_space_subtype_item_extend(
985         bContext *UNUSED(C), EnumPropertyItem **item, int *totitem)
986 {
987         RNA_enum_items_add(item, totitem, rna_enum_space_image_mode_items);
988 }
989
990 /**************************** spacetype *****************************/
991
992 /* only called once, from space/spacetypes.c */
993 void ED_spacetype_image(void)
994 {
995         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype image");
996         ARegionType *art;
997
998         st->spaceid = SPACE_IMAGE;
999         strncpy(st->name, "Image", BKE_ST_MAXNAME);
1000
1001         st->new = image_new;
1002         st->free = image_free;
1003         st->init = image_init;
1004         st->duplicate = image_duplicate;
1005         st->operatortypes = image_operatortypes;
1006         st->keymap = image_keymap;
1007         st->dropboxes = image_dropboxes;
1008         st->refresh = image_refresh;
1009         st->listener = image_listener;
1010         st->context = image_context;
1011         st->gizmos = image_widgets;
1012         st->id_remap = image_id_remap;
1013         st->space_subtype_item_extend = image_space_subtype_item_extend;
1014         st->space_subtype_get = image_space_subtype_get;
1015         st->space_subtype_set = image_space_subtype_set;
1016
1017         /* regions: main window */
1018         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1019         art->regionid = RGN_TYPE_WINDOW;
1020         art->keymapflag = ED_KEYMAP_GIZMO | ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
1021         art->init = image_main_region_init;
1022         art->draw = image_main_region_draw;
1023         art->listener = image_main_region_listener;
1024         BLI_addhead(&st->regiontypes, art);
1025
1026         /* regions: listview/buttons */
1027         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1028         art->regionid = RGN_TYPE_UI;
1029         art->prefsizex = 220; // XXX
1030         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1031         art->listener = image_buttons_region_listener;
1032         art->init = image_buttons_region_init;
1033         art->draw = image_buttons_region_draw;
1034         BLI_addhead(&st->regiontypes, art);
1035
1036         ED_uvedit_buttons_register(art);
1037         image_buttons_register(art);
1038
1039         /* regions: statistics/scope buttons */
1040         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1041         art->regionid = RGN_TYPE_TOOLS;
1042         art->prefsizex = 58; /* XXX */
1043         art->prefsizey = 50; /* XXX */
1044         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1045         art->listener = image_tools_region_listener;
1046         art->message_subscribe = ED_region_generic_tools_region_message_subscribe;
1047         art->snap_size = ED_region_generic_tools_region_snap_size;
1048         art->init = image_tools_region_init;
1049         art->draw = image_tools_region_draw;
1050         BLI_addhead(&st->regiontypes, art);
1051
1052         /* regions: header */
1053         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1054         art->regionid = RGN_TYPE_HEADER;
1055         art->prefsizey = HEADERY;
1056         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
1057         art->listener = image_header_region_listener;
1058         art->init = image_header_region_init;
1059         art->draw = image_header_region_draw;
1060
1061         BLI_addhead(&st->regiontypes, art);
1062
1063         /* regions: hud */
1064         art = ED_area_type_hud(st->spaceid);
1065         BLI_addhead(&st->regiontypes, art);
1066
1067         BKE_spacetype_register(st);
1068 }