mask/image viewer now works with non 1:1 image aspect, editing masks in the image...
[blender.git] / source / blender / editors / space_image / image_edit.c
index 725c76d8bb62a273df618c941d49f9ae8a4b802f..f80210fe4d7cba1edf4e81858dfe416ef7420a76 100644 (file)
  *  \ingroup spimage
  */
 
+#include "DNA_mask_types.h"
 #include "DNA_object_types.h"
 #include "DNA_scene_types.h"
 
 #include "BLI_math.h"
 
-#include "BKE_image.h"
+#include "BKE_context.h"
 #include "BKE_global.h"
+#include "BKE_image.h"
 #include "BKE_main.h"
 #include "BKE_tessmesh.h"
 
@@ -45,6 +47,8 @@
 #include "ED_screen.h"
 #include "ED_uvedit.h"
 
+#include "UI_view2d.h"
+
 #include "WM_api.h"
 #include "WM_types.h"
 
@@ -64,8 +68,11 @@ void ED_space_image_set(SpaceImage *sima, Scene *scene, Object *obedit, Image *i
         * to check if the face is displayed in UV-localview */
        sima->image = ima;
 
-       if (ima == NULL || ima->type == IMA_TYPE_R_RESULT || ima->type == IMA_TYPE_COMPOSITE)
-               sima->flag &= ~SI_DRAWTOOL;
+       if (ima == NULL || ima->type == IMA_TYPE_R_RESULT || ima->type == IMA_TYPE_COMPOSITE) {
+               if (sima->mode == SI_MODE_PAINT) {
+                       sima->mode = SI_MODE_VIEW;
+               }
+       }
 
        if (sima->image)
                BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
@@ -79,6 +86,20 @@ void ED_space_image_set(SpaceImage *sima, Scene *scene, Object *obedit, Image *i
        WM_main_add_notifier(NC_SPACE | ND_SPACE_IMAGE, NULL);
 }
 
+Mask *ED_space_image_get_mask(SpaceImage *sima)
+{
+       return sima->mask_info.mask;
+}
+
+void ED_space_image_set_mask(bContext *C, SpaceImage *sima, Mask *mask)
+{
+       sima->mask_info.mask = mask;
+
+       if (C) {
+               WM_event_add_notifier(C, NC_MASK | NA_SELECTED, mask);
+       }
+}
+
 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **lock_r)
 {
        ImBuf *ibuf;
@@ -117,7 +138,7 @@ int ED_space_image_has_buffer(SpaceImage *sima)
        return has_buffer;
 }
 
-void ED_image_size(Image *ima, int *width, int *height)
+void ED_image_get_size(Image *ima, int *width, int *height)
 {
        ImBuf *ibuf = NULL;
        void *lock;
@@ -130,15 +151,15 @@ void ED_image_size(Image *ima, int *width, int *height)
                *height = ibuf->y;
        }
        else {
-               *width = 256;
-               *height = 256;
+               *width  = IMG_SIZE_FALLBACK;
+               *height = IMG_SIZE_FALLBACK;
        }
 
        if (ima)
                BKE_image_release_ibuf(ima, lock);
 }
 
-void ED_space_image_size(SpaceImage *sima, int *width, int *height)
+void ED_space_image_get_size(SpaceImage *sima, int *width, int *height)
 {
        Scene *scene = sima->iuser.scene;
        ImBuf *ibuf;
@@ -164,14 +185,14 @@ void ED_space_image_size(SpaceImage *sima, int *width, int *height)
        /* I know a bit weak... but preview uses not actual image size */
        // XXX else if (image_preview_active(sima, width, height));
        else {
-               *width = 256;
-               *height = 256;
+               *width  = IMG_SIZE_FALLBACK;
+               *height = IMG_SIZE_FALLBACK;
        }
 
        ED_space_image_release_buffer(sima, lock);
 }
 
-void ED_image_aspect(Image *ima, float *aspx, float *aspy)
+void ED_image_get_aspect(Image *ima, float *aspx, float *aspy)
 {
        *aspx = *aspy = 1.0;
 
@@ -185,27 +206,27 @@ void ED_image_aspect(Image *ima, float *aspx, float *aspy)
        *aspy = ima->aspy / ima->aspx;
 }
 
-void ED_space_image_aspect(SpaceImage *sima, float *aspx, float *aspy)
+void ED_space_image_get_aspect(SpaceImage *sima, float *aspx, float *aspy)
 {
-       ED_image_aspect(ED_space_image(sima), aspx, aspy);
+       ED_image_get_aspect(ED_space_image(sima), aspx, aspy);
 }
 
-void ED_space_image_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
+void ED_space_image_get_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
 {
        int width, height;
 
-       ED_space_image_size(sima, &width, &height);
+       ED_space_image_get_size(sima, &width, &height);
 
        *zoomx = (float)(ar->winrct.xmax - ar->winrct.xmin + 1) / (float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin) * width);
        *zoomy = (float)(ar->winrct.ymax - ar->winrct.ymin + 1) / (float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin) * height);
 }
 
-void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
+void ED_space_image_get_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
 {
        int w, h;
 
-       ED_space_image_aspect(sima, aspx, aspy);
-       ED_space_image_size(sima, &w, &h);
+       ED_space_image_get_aspect(sima, aspx, aspy);
+       ED_space_image_get_size(sima, &w, &h);
 
        *aspx *= (float)w;
        *aspy *= (float)h;
@@ -220,17 +241,31 @@ void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
        }
 }
 
-void ED_image_uv_aspect(Image *ima, float *aspx, float *aspy)
+void ED_image_get_uv_aspect(Image *ima, float *aspx, float *aspy)
 {
        int w, h;
 
-       ED_image_aspect(ima, aspx, aspy);
-       ED_image_size(ima, &w, &h);
+       ED_image_get_aspect(ima, aspx, aspy);
+       ED_image_get_size(ima, &w, &h);
 
        *aspx *= (float)w;
        *aspy *= (float)h;
 }
 
+void ED_image_mouse_pos(SpaceImage *sima, ARegion *ar, wmEvent *event, float co[2])
+{
+       int sx, sy, width, height;
+       float zoomx, zoomy;
+
+       ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
+       ED_space_image_get_size(sima, &width, &height);
+
+       UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &sx, &sy);
+
+       co[0] = ((event->mval[0] - sx) / zoomx) / width;
+       co[1] = ((event->mval[1] - sy) / zoomy) / height;
+}
+
 int ED_space_image_show_render(SpaceImage *sima)
 {
        return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
@@ -241,7 +276,7 @@ int ED_space_image_show_paint(SpaceImage *sima)
        if (ED_space_image_show_render(sima))
                return 0;
 
-       return (sima->flag & SI_DRAWTOOL);
+       return (sima->mode == SI_MODE_PAINT);
 }
 
 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
@@ -279,6 +314,38 @@ int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
        return 0;
 }
 
+/* matches clip function */
+int ED_space_image_check_show_maskedit(SpaceImage *sima)
+{
+       return (sima->mode == SI_MODE_MASK);
+}
+
+int ED_space_image_maskedit_poll(bContext *C)
+{
+       SpaceImage *sima = CTX_wm_space_image(C);
+
+       if (sima && sima->image) {
+               return ED_space_image_check_show_maskedit(sima);
+       }
+
+       return FALSE;
+}
+
+int ED_space_image_maskedit_mask_poll(bContext *C)
+{
+       if (ED_space_image_maskedit_poll(C)) {
+               Image *ima = CTX_data_edit_image(C);
+
+               if (ima) {
+                       SpaceImage *sima = CTX_wm_space_image(C);
+
+                       return sima->mask_info.mask != NULL;
+               }
+       }
+
+       return FALSE;
+}
+
 /******************** TODO ********************/
 
 /* XXX notifier? */