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 1c488fe4ac7f569c19bf5143c3a0968207496a3d..f80210fe4d7cba1edf4e81858dfe416ef7420a76 100644 (file)
@@ -47,6 +47,8 @@
 #include "ED_screen.h"
 #include "ED_uvedit.h"
 
+#include "UI_view2d.h"
+
 #include "WM_api.h"
 #include "WM_types.h"
 
@@ -66,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);
@@ -133,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;
@@ -146,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;
@@ -180,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;
 
@@ -201,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;
@@ -236,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));
@@ -257,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)
@@ -298,9 +317,7 @@ int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
 /* matches clip function */
 int ED_space_image_check_show_maskedit(SpaceImage *sima)
 {
-       /* MASKTODO - whem we have a mask edit option */
-       (void)sima;
-       return TRUE;
+       return (sima->mode == SI_MODE_MASK);
 }
 
 int ED_space_image_maskedit_poll(bContext *C)