2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_image/space_image.c
31 #include "DNA_mesh_types.h"
32 #include "DNA_mask_types.h"
33 #include "DNA_meshdata_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_scene_types.h"
37 #include "MEM_guardedalloc.h"
39 #include "BLI_blenlib.h"
41 #include "BLI_threads.h"
43 #include "BKE_colortools.h"
44 #include "BKE_context.h"
45 #include "BKE_image.h"
46 #include "BKE_global.h"
47 #include "BKE_scene.h"
48 #include "BKE_screen.h"
49 #include "BKE_tessmesh.h"
50 #include "BKE_sequencer.h"
53 #include "IMB_imbuf_types.h"
59 #include "ED_space_api.h"
60 #include "ED_screen.h"
61 #include "ED_uvedit.h"
65 #include "RNA_access.h"
70 #include "UI_resources.h"
71 #include "UI_view2d.h"
73 #include "image_intern.h"
75 /**************************** common state *****************************/
77 static void image_scopes_tag_refresh(ScrArea *sa)
79 SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
82 /* only while histogram is visible */
83 for (ar = sa->regionbase.first; ar; ar = ar->next) {
84 if (ar->regiontype == RGN_TYPE_PREVIEW && ar->flag & RGN_FLAG_HIDDEN)
92 /* ******************** manage regions ********************* */
94 ARegion *image_has_buttons_region(ScrArea *sa)
98 ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
101 /* add subdiv level; after header */
102 ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
105 if (ar == NULL) return NULL;
107 arnew = MEM_callocN(sizeof(ARegion), "buttons for image");
109 BLI_insertlinkafter(&sa->regionbase, ar, arnew);
110 arnew->regiontype = RGN_TYPE_UI;
111 arnew->alignment = RGN_ALIGN_LEFT;
113 arnew->flag = RGN_FLAG_HIDDEN;
118 ARegion *image_has_scope_region(ScrArea *sa)
122 ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
125 /* add subdiv level; after buttons */
126 ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
129 if (ar == NULL) return NULL;
131 arnew = MEM_callocN(sizeof(ARegion), "scopes for image");
133 BLI_insertlinkafter(&sa->regionbase, ar, arnew);
134 arnew->regiontype = RGN_TYPE_PREVIEW;
135 arnew->alignment = RGN_ALIGN_RIGHT;
137 arnew->flag = RGN_FLAG_HIDDEN;
139 image_scopes_tag_refresh(sa);
144 /* ******************** default callbacks for image space ***************** */
146 static SpaceLink *image_new(const bContext *UNUSED(C))
151 simage = MEM_callocN(sizeof(SpaceImage), "initimage");
152 simage->spacetype = SPACE_IMAGE;
155 simage->flag = SI_SHOW_GPENCIL | SI_USE_ALPHA;
157 simage->iuser.ok = TRUE;
158 simage->iuser.fie_ima = 2;
159 simage->iuser.frames = 100;
161 scopes_new(&simage->scopes);
162 simage->sample_line_hist.height = 100;
165 ar = MEM_callocN(sizeof(ARegion), "header for image");
167 BLI_addtail(&simage->regionbase, ar);
168 ar->regiontype = RGN_TYPE_HEADER;
169 ar->alignment = RGN_ALIGN_BOTTOM;
171 /* buttons/list view */
172 ar = MEM_callocN(sizeof(ARegion), "buttons for image");
174 BLI_addtail(&simage->regionbase, ar);
175 ar->regiontype = RGN_TYPE_UI;
176 ar->alignment = RGN_ALIGN_LEFT;
177 ar->flag = RGN_FLAG_HIDDEN;
180 ar = MEM_callocN(sizeof(ARegion), "buttons for image");
182 BLI_addtail(&simage->regionbase, ar);
183 ar->regiontype = RGN_TYPE_PREVIEW;
184 ar->alignment = RGN_ALIGN_RIGHT;
185 ar->flag = RGN_FLAG_HIDDEN;
188 ar = MEM_callocN(sizeof(ARegion), "main area for image");
190 BLI_addtail(&simage->regionbase, ar);
191 ar->regiontype = RGN_TYPE_WINDOW;
193 return (SpaceLink *)simage;
196 /* not spacelink itself */
197 static void image_free(SpaceLink *sl)
199 SpaceImage *simage = (SpaceImage *) sl;
201 scopes_free(&simage->scopes);
205 /* spacetype; init callback, add handlers */
206 static void image_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
208 ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
211 WM_event_add_dropbox_handler(&sa->handlers, lb);
215 static SpaceLink *image_duplicate(SpaceLink *sl)
217 SpaceImage *simagen = MEM_dupallocN(sl);
219 /* clear or remove stuff from old */
221 scopes_new(&simagen->scopes);
223 return (SpaceLink *)simagen;
226 static void image_operatortypes(void)
228 WM_operatortype_append(IMAGE_OT_view_all);
229 WM_operatortype_append(IMAGE_OT_view_pan);
230 WM_operatortype_append(IMAGE_OT_view_selected);
231 WM_operatortype_append(IMAGE_OT_view_zoom);
232 WM_operatortype_append(IMAGE_OT_view_zoom_in);
233 WM_operatortype_append(IMAGE_OT_view_zoom_out);
234 WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
235 WM_operatortype_append(IMAGE_OT_view_ndof);
237 WM_operatortype_append(IMAGE_OT_new);
238 WM_operatortype_append(IMAGE_OT_open);
239 WM_operatortype_append(IMAGE_OT_match_movie_length);
240 WM_operatortype_append(IMAGE_OT_replace);
241 WM_operatortype_append(IMAGE_OT_reload);
242 WM_operatortype_append(IMAGE_OT_save);
243 WM_operatortype_append(IMAGE_OT_save_as);
244 WM_operatortype_append(IMAGE_OT_save_sequence);
245 WM_operatortype_append(IMAGE_OT_pack);
246 WM_operatortype_append(IMAGE_OT_unpack);
248 WM_operatortype_append(IMAGE_OT_invert);
250 WM_operatortype_append(IMAGE_OT_cycle_render_slot);
252 WM_operatortype_append(IMAGE_OT_sample);
253 WM_operatortype_append(IMAGE_OT_sample_line);
254 WM_operatortype_append(IMAGE_OT_curves_point_set);
256 WM_operatortype_append(IMAGE_OT_properties);
257 WM_operatortype_append(IMAGE_OT_scopes);
260 static void image_keymap(struct wmKeyConfig *keyconf)
262 wmKeyMap *keymap = WM_keymap_find(keyconf, "Image Generic", SPACE_IMAGE, 0);
266 WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
267 WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
268 WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
269 WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
270 WM_keymap_add_item(keymap, "IMAGE_OT_save_as", F3KEY, KM_PRESS, 0, 0);
271 WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
272 WM_keymap_add_item(keymap, "IMAGE_OT_scopes", TKEY, KM_PRESS, 0, 0);
274 WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, 0, 0);
275 RNA_boolean_set(WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, KM_ALT, 0)->ptr, "reverse", TRUE);
277 keymap = WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
279 WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
280 WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
281 WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
282 WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
283 WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
285 WM_keymap_add_item(keymap, "IMAGE_OT_view_all", NDOF_BUTTON_FIT, KM_PRESS, 0, 0); // or view selected?
286 WM_keymap_add_item(keymap, "IMAGE_OT_view_ndof", NDOF_MOTION, 0, 0, 0);
288 WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
289 WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
290 WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
291 WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
292 WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
293 WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
294 WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEPAN, 0, KM_CTRL, 0);
296 /* ctrl now works as well, shift + numpad works as arrow keys on Windows */
297 RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 8.0f);
298 RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 4.0f);
299 RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 2.0f);
300 RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
301 RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
302 RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
304 RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
305 RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
306 RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
307 RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
309 WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
310 RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
311 RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
313 /* toggle editmode is handy to have while UV unwrapping */
314 kmi = WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
315 RNA_enum_set(kmi->ptr, "mode", OB_MODE_EDIT);
316 RNA_boolean_set(kmi->ptr, "toggle", TRUE);
318 /* fast switch to render slots */
319 for (i = 0; i < MAX2(IMA_MAX_RENDER_SLOT, 9); i++) {
320 kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_int", ONEKEY + i, KM_PRESS, 0, 0);
321 RNA_string_set(kmi->ptr, "data_path", "space_data.image.render_slot");
322 RNA_int_set(kmi->ptr, "value", i);
326 kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", COMMAKEY, KM_PRESS, 0, 0);
327 RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
328 RNA_string_set(kmi->ptr, "value", "CENTER");
330 kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", COMMAKEY, KM_PRESS, KM_CTRL, 0);
331 RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
332 RNA_string_set(kmi->ptr, "value", "MEDIAN");
334 kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", PERIODKEY, KM_PRESS, 0, 0);
335 RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
336 RNA_string_set(kmi->ptr, "value", "CURSOR");
340 static int image_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
342 if (drag->type == WM_DRAG_PATH)
343 if (ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK)) /* rule might not work? */
348 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
350 /* copy drag path to properties */
351 RNA_string_set(drop->ptr, "filepath", drag->path);
354 /* area+region dropbox definition */
355 static void image_dropboxes(void)
357 ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
359 WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
363 * \note take care not to get into feedback loop here,
364 * calling composite job causes viewer to refresh.
366 static void image_refresh(const bContext *C, ScrArea *sa)
368 Scene *scene = CTX_data_scene(C);
369 SpaceImage *sima = sa->spacedata.first;
370 Object *obedit = CTX_data_edit_object(C);
373 ima = ED_space_image(sima);
375 BKE_image_user_check_frame_calc(&sima->iuser, scene->r.cfra, 0);
377 /* check if we have to set the image from the editmesh */
378 if (ima && (ima->source == IMA_SRC_VIEWER && sima->mode == SI_MODE_MASK)) {
379 if (sima->lock == FALSE && G.moving) {
383 if (scene->nodetree) {
384 Mask *mask = ED_space_image_get_mask(sima);
386 ED_node_composite_job(C, scene->nodetree, scene);
391 else if (ima && (ima->source == IMA_SRC_VIEWER || sima->pin)) {
394 else if (obedit && obedit->type == OB_MESH) {
395 Mesh *me = (Mesh *)obedit->data;
396 struct BMEditMesh *em = me->edit_btmesh;
397 int sloppy = TRUE; /* partially selected face is ok */
398 int selected = !(scene->toolsettings->uv_flag & UV_SYNC_SELECTION); /* only selected active face? */
400 if (BKE_scene_use_new_shading_nodes(scene)) {
401 /* new shading system, get image from material */
402 BMFace *efa = BM_active_face_get(em->bm, sloppy, selected);
406 ED_object_get_active_image(obedit, efa->mat_nr + 1, &node_ima, NULL, NULL);
409 sima->image = node_ima;
413 /* old shading system, we set texface */
416 if (em && EDBM_mtexpoly_check(em)) {
417 tf = EDBM_mtexpoly_active_get(em, NULL, sloppy, selected);
420 /* don't need to check for pin here, see above */
421 sima->image = tf->tpage;
423 if ((sima->flag & SI_EDITTILE) == 0) {
424 sima->curtile = tf->tile;
432 static void image_listener(ScrArea *sa, wmNotifier *wmn)
434 SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
436 /* context changes */
437 switch (wmn->category) {
439 /* notifier comes from editing color space */
440 image_scopes_tag_refresh(sa);
441 ED_area_tag_redraw(sa);
446 image_scopes_tag_refresh(sa);
447 ED_area_tag_refresh(sa);
448 ED_area_tag_redraw(sa);
451 if (wmn->subtype == NS_EDITMODE_MESH)
452 ED_area_tag_refresh(sa);
453 ED_area_tag_redraw(sa);
455 case ND_RENDER_RESULT:
456 case ND_RENDER_OPTIONS:
457 case ND_COMPO_RESULT:
458 if (ED_space_image_show_render(sima))
459 image_scopes_tag_refresh(sa);
460 ED_area_tag_redraw(sa);
465 if (wmn->reference == sima->image || !wmn->reference) {
466 image_scopes_tag_refresh(sa);
467 ED_area_tag_refresh(sa);
468 ED_area_tag_redraw(sa);
472 if (wmn->data == ND_SPACE_IMAGE) {
473 image_scopes_tag_refresh(sa);
474 ED_area_tag_redraw(sa);
479 // Scene *scene = wmn->window->screen->scene;
480 /* ideally would check for: ED_space_image_check_show_maskedit(scene, sima) but we cant get the scene */
481 if (sima->mode == SI_MODE_MASK) {
484 ED_area_tag_redraw(sa);
488 /* causes node-recalc */
489 ED_area_tag_redraw(sa);
490 ED_area_tag_refresh(sa);
493 switch (wmn->action) {
495 ED_area_tag_redraw(sa);
498 /* causes node-recalc */
499 ED_area_tag_redraw(sa);
500 ED_area_tag_refresh(sa);
510 image_scopes_tag_refresh(sa);
511 ED_area_tag_refresh(sa);
512 ED_area_tag_redraw(sa);
517 Object *ob = (Object *)wmn->reference;
521 if (ob && (ob->mode & OB_MODE_EDIT) && sima->lock && (sima->flag & SI_DRAWSHADOW)) {
522 ED_area_tag_refresh(sa);
523 ED_area_tag_redraw(sa);
531 const char *image_context_dir[] = {"edit_image", "edit_mask", NULL};
533 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
535 SpaceImage *sima = CTX_wm_space_image(C);
537 if (CTX_data_dir(member)) {
538 CTX_data_dir_set(result, image_context_dir);
540 else if (CTX_data_equals(member, "edit_image")) {
541 CTX_data_id_pointer_set(result, (ID *)ED_space_image(sima));
544 else if (CTX_data_equals(member, "edit_mask")) {
545 Mask *mask = ED_space_image_get_mask(sima);
547 CTX_data_id_pointer_set(result, &mask->id);
554 /************************** main region ***************************/
556 /* sets up the fields of the View2D from zoom and offset */
557 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
559 Image *ima = ED_space_image(sima);
561 int width, height, winx, winy;
564 if (image_preview_active(curarea, &width, &height)) {}
567 ED_space_image_get_size(sima, &width, &height);
573 h *= ima->aspy / ima->aspx;
575 winx = BLI_rcti_size_x(&ar->winrct) + 1;
576 winy = BLI_rcti_size_y(&ar->winrct) + 1;
578 ar->v2d.tot.xmin = 0;
579 ar->v2d.tot.ymin = 0;
580 ar->v2d.tot.xmax = w;
581 ar->v2d.tot.ymax = h;
583 ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
584 ar->v2d.mask.xmax = winx;
585 ar->v2d.mask.ymax = winy;
587 /* which part of the image space do we see? */
588 x1 = ar->winrct.xmin + (winx - sima->zoom * w) / 2.0f;
589 y1 = ar->winrct.ymin + (winy - sima->zoom * h) / 2.0f;
591 x1 -= sima->zoom * sima->xof;
592 y1 -= sima->zoom * sima->yof;
594 /* relative display right */
595 ar->v2d.cur.xmin = ((ar->winrct.xmin - (float)x1) / sima->zoom);
596 ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sima->zoom);
598 /* relative display left */
599 ar->v2d.cur.ymin = ((ar->winrct.ymin - (float)y1) / sima->zoom);
600 ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sima->zoom);
602 /* normalize 0.0..1.0 */
603 ar->v2d.cur.xmin /= w;
604 ar->v2d.cur.xmax /= w;
605 ar->v2d.cur.ymin /= h;
606 ar->v2d.cur.ymax /= h;
609 /* add handlers, stuff you only do once or on area/region changes */
610 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
614 // image space manages own v2d
615 // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
617 /* mask polls mode */
618 keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
619 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
621 /* image paint polls for mode */
622 keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
623 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
625 keymap = WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
626 WM_event_add_keymap_handler(&ar->handlers, keymap);
628 keymap = WM_keymap_find(wm->defaultconf, "UV Sculpt", 0, 0);
629 WM_event_add_keymap_handler(&ar->handlers, keymap);
632 keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
633 WM_event_add_keymap_handler(&ar->handlers, keymap);
634 keymap = WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
635 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
639 static void image_main_area_draw(const bContext *C, ARegion *ar)
641 /* draw entirely, view changes should be handled here */
642 SpaceImage *sima = CTX_wm_space_image(C);
643 Object *obact = CTX_data_active_object(C);
644 Object *obedit = CTX_data_edit_object(C);
646 Scene *scene = CTX_data_scene(C);
647 View2D *v2d = &ar->v2d;
648 //View2DScrollers *scrollers;
651 /* XXX not supported yet, disabling for now */
652 scene->r.scemode &= ~R_COMP_CROP;
654 /* clear and setup matrix */
655 UI_GetThemeColor3fv(TH_BACK, col);
656 glClearColor(col[0], col[1], col[2], 0.0);
657 glClear(GL_COLOR_BUFFER_BIT);
659 /* put scene context variable in iuser */
660 sima->iuser.scene = scene;
662 /* we set view2d from own zoom and offset each time */
663 image_main_area_set_view2d(sima, ar);
665 /* we draw image in pixelspace */
666 draw_image_main(C, ar);
668 /* and uvs in 0.0-1.0 space */
669 UI_view2d_view_ortho(v2d);
671 ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
673 draw_uvedit_main(sima, ar, scene, obedit, obact);
675 /* check for mask (delay draw) */
676 if (ED_space_image_show_uvedit(sima, obedit)) {
679 else if (sima->mode == SI_MODE_MASK) {
680 mask = ED_space_image_get_mask(sima);
681 draw_image_cursor(sima, ar);
684 ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
686 if (sima->flag & SI_SHOW_GPENCIL) {
687 /* Grease Pencil too (in addition to UV's) */
688 draw_image_grease_pencil((bContext *)C, TRUE);
692 draw_image_sample_line(sima);
694 UI_view2d_view_restore(C);
696 if (sima->flag & SI_SHOW_GPENCIL) {
697 /* draw Grease Pencil - screen space only */
698 draw_image_grease_pencil((bContext *)C, FALSE);
702 Image *image = ED_space_image(sima);
703 int width, height, show_viewer;
706 show_viewer = (image && image->source == IMA_SRC_VIEWER);
709 /* ED_space_image_get* will acquire image buffer which requires
710 * lock here by the same reason why lock is needed in draw_image_main
712 BLI_lock_thread(LOCK_DRAW_IMAGE);
715 ED_space_image_get_size(sima, &width, &height);
716 ED_space_image_get_aspect(sima, &aspx, &aspy);
719 BLI_unlock_thread(LOCK_DRAW_IMAGE);
721 ED_mask_draw_region(mask, ar,
722 sima->mask_info.draw_flag, sima->mask_info.draw_type,
728 ED_mask_draw_frames(mask, ar, CFRA, mask->sfra, mask->efra);
730 draw_image_cursor(sima, ar);
735 scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
736 UI_view2d_scrollers_draw(C, v2d, scrollers);
737 UI_view2d_scrollers_free(scrollers);
741 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
743 /* context changes */
744 switch (wmn->category) {
746 if (wmn->action == NA_EDITED)
747 ED_region_tag_redraw(ar);
752 /* *********************** buttons region ************************ */
754 /* add handlers, stuff you only do once or on area/region changes */
755 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
759 ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
760 ED_region_panels_init(wm, ar);
762 keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
763 WM_event_add_keymap_handler(&ar->handlers, keymap);
766 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
768 ED_region_panels(C, ar, 1, NULL, -1);
771 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
773 /* context changes */
774 switch (wmn->category) {
776 if (wmn->data == ND_DATA)
777 ED_region_tag_redraw(ar);
780 if (wmn->action == NA_EDITED)
781 ED_region_tag_redraw(ar);
785 /* sending by texture render job and needed to properly update displaying
786 * brush texture icon */
787 ED_region_tag_redraw(ar);
792 /* *********************** scopes region ************************ */
794 /* add handlers, stuff you only do once or on area/region changes */
795 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
799 ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
800 ED_region_panels_init(wm, ar);
802 keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
803 WM_event_add_keymap_handler(&ar->handlers, keymap);
806 static void image_scope_area_draw(const bContext *C, ARegion *ar)
808 SpaceImage *sima = CTX_wm_space_image(C);
809 Scene *scene = CTX_data_scene(C);
811 ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
814 if (!sima->scopes.ok) {
815 BKE_histogram_update_sample_line(&sima->sample_line_hist, ibuf, &scene->view_settings, &scene->display_settings);
817 if (sima->image->flag & IMA_VIEW_AS_RENDER)
818 scopes_update(&sima->scopes, ibuf, &scene->view_settings, &scene->display_settings);
820 scopes_update(&sima->scopes, ibuf, NULL, &scene->display_settings);
822 ED_space_image_release_buffer(sima, ibuf, lock);
824 ED_region_panels(C, ar, 1, NULL, -1);
827 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
829 /* context changes */
830 switch (wmn->category) {
834 case ND_RENDER_RESULT:
835 case ND_COMPO_RESULT:
836 ED_region_tag_redraw(ar);
841 ED_region_tag_redraw(ar);
844 ED_region_tag_redraw(ar);
850 /************************* header region **************************/
852 /* add handlers, stuff you only do once or on area/region changes */
853 static void image_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
855 ED_region_header_init(ar);
858 static void image_header_area_draw(const bContext *C, ARegion *ar)
860 ED_region_header(C, ar);
863 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
865 /* context changes */
866 switch (wmn->category) {
870 case ND_TOOLSETTINGS:
871 ED_region_tag_redraw(ar);
879 ED_region_tag_redraw(ar);
886 /**************************** spacetype *****************************/
888 /* only called once, from space/spacetypes.c */
889 void ED_spacetype_image(void)
891 SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype image");
894 st->spaceid = SPACE_IMAGE;
895 strncpy(st->name, "Image", BKE_ST_MAXNAME);
898 st->free = image_free;
899 st->init = image_init;
900 st->duplicate = image_duplicate;
901 st->operatortypes = image_operatortypes;
902 st->keymap = image_keymap;
903 st->dropboxes = image_dropboxes;
904 st->refresh = image_refresh;
905 st->listener = image_listener;
906 st->context = image_context;
908 /* regions: main window */
909 art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
910 art->regionid = RGN_TYPE_WINDOW;
911 art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
912 art->init = image_main_area_init;
913 art->draw = image_main_area_draw;
914 art->listener = image_main_area_listener;
916 BLI_addhead(&st->regiontypes, art);
918 /* regions: listview/buttons */
919 art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
920 art->regionid = RGN_TYPE_UI;
921 art->prefsizex = 220; // XXX
922 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
923 art->listener = image_buttons_area_listener;
924 art->init = image_buttons_area_init;
925 art->draw = image_buttons_area_draw;
926 BLI_addhead(&st->regiontypes, art);
928 image_buttons_register(art);
929 ED_uvedit_buttons_register(art);
931 /* regions: statistics/scope buttons */
932 art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
933 art->regionid = RGN_TYPE_PREVIEW;
934 art->prefsizex = 220; // XXX
935 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
936 art->listener = image_scope_area_listener;
937 art->init = image_scope_area_init;
938 art->draw = image_scope_area_draw;
939 BLI_addhead(&st->regiontypes, art);
941 /* regions: header */
942 art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
943 art->regionid = RGN_TYPE_HEADER;
944 art->prefsizey = HEADERY;
945 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
946 art->listener = image_header_area_listener;
947 art->init = image_header_area_init;
948 art->draw = image_header_area_draw;
950 BLI_addhead(&st->regiontypes, art);
952 BKE_spacetype_register(st);