]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/menu/item/image.c
Merge branch 'master' into samual/combined_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / image.c
index 4f13f997cf1a89c443b9a78767ea09dfb51e3c42..1d5471e15c09d1ea2bb45980648f8477253e5d90 100644 (file)
@@ -15,6 +15,7 @@ CLASS(Image) EXTENDS(Item)
        ATTRIB(Image, zoomBox, float, 0) // used by forcedAspect -2 when the image is larger than the containing box
        ATTRIB(Image, zoomFactor, float, 1)
        ATTRIB(Image, zoomOffset, vector, '0.5 0.5 0')
+       ATTRIB(Image, zoomSnapToTheBox, float, 1) // snap the zoomed in image to the box borders when zooming/dragging it
        ATTRIB(Image, zoomTime, float, 0)
        ATTRIB(Image, zoomLimitedByTheBox, float, 0) // forbids zoom if image would be larger than the containing box
        ATTRIB(Image, zoomMax, float, 0)
@@ -55,7 +56,7 @@ void Image_draw(entity me)
 }
 void Image_updateAspect(entity me)
 {
-       float asp;
+       float asp = 0;
        if(me.size_x <= 0 || me.size_y <= 0)
                return;
        if(me.forcedAspect == 0)
@@ -65,10 +66,16 @@ void Image_updateAspect(entity me)
        }
        else
        {
+               vector sz = '0 0 0';
                if(me.forcedAspect < 0)
                {
-                       vector sz;
                        sz = draw_PictureSize(me.src);
+                       if(sz_x <= 0 || sz_y <= 0)
+                       {
+                               // image is broken or doesn't exist, set the size for the placeholder image
+                               sz_x = me.size_x;
+                               sz_y = me.size_y;
+                       }
                        asp = sz_x / sz_y;
                }
                else
@@ -123,8 +130,23 @@ void Image_updateAspect(entity me)
 
        if(me.imgSize_x > 1 || me.imgSize_y > 1)
        {
-               me.zoomOffset_x = bound(0, me.zoomOffset_x, 1);
-               me.zoomOffset_y = bound(0, me.zoomOffset_y, 1);
+               if(me.zoomSnapToTheBox)
+               {
+                       if(me.imgSize_x > 1)
+                               me.zoomOffset_x = bound(0.5/me.imgSize_x, me.zoomOffset_x, 1 - 0.5/me.imgSize_x);
+                       else
+                               me.zoomOffset_x = bound(1 - 0.5/me.imgSize_x, me.zoomOffset_x, 0.5/me.imgSize_x);
+
+                       if(me.imgSize_y > 1)
+                               me.zoomOffset_y = bound(0.5/me.imgSize_y, me.zoomOffset_y, 1 - 0.5/me.imgSize_y);
+                       else
+                               me.zoomOffset_y = bound(1 - 0.5/me.imgSize_y, me.zoomOffset_y, 0.5/me.imgSize_y);
+               }
+               else
+               {
+                       me.zoomOffset_x = bound(0, me.zoomOffset_x, 1);
+                       me.zoomOffset_y = bound(0, me.zoomOffset_y, 1);
+               }
        }
        else
                me.zoomOffset = '0.5 0.5 0';