]> 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 c424ad06a4a501f61121798bf48a6ca5e07af6d5..1d5471e15c09d1ea2bb45980648f8477253e5d90 100644 (file)
@@ -5,15 +5,17 @@ CLASS(Image) EXTENDS(Item)
        METHOD(Image, toString, string(entity))
        METHOD(Image, resizeNotify, void(entity, vector, vector, vector, vector))
        METHOD(Image, updateAspect, void(entity))
+       METHOD(Image, initZoom, void(entity))
        METHOD(Image, setZoom, void(entity, float, float))
        METHOD(Image, drag_setStartPos, float(entity, vector))
        METHOD(Image, drag, float(entity, vector))
        ATTRIB(Image, src, string, string_null)
        ATTRIB(Image, color, vector, '1 1 1')
        ATTRIB(Image, forcedAspect, float, 0) // special values: -1 keep image aspect ratio, -2 keep image size but bound to the containing box, -3 always keep image size
-       ATTRIB(Image, initialForcedZoom, float, 0) // used by forcedAspect -2 when the image is larger than the containing box
+       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)
@@ -32,13 +34,17 @@ string Image_toString(entity me)
 void Image_configureImage(entity me, string path)
 {
        me.src = path;
+}
+void Image_initZoom(entity me)
+{
        me.zoomOffset = '0.5 0.5 0';
        me.zoomFactor = 1;
        if (me.forcedAspect == -2)
-               me.initialForcedZoom = -1; // calculate initialForcedZoom at the first updateAspect call
+               me.zoomBox = -1; // calculate zoomBox at the first updateAspect call
        if (me.zoomLimitedByTheBox)
                me.zoomMax = -1; // calculate zoomMax at the first updateAspect call
 }
+
 void Image_draw(entity me)
 {
        if(me.imgSize_x > 1 || me.imgSize_y > 1)
@@ -46,10 +52,11 @@ void Image_draw(entity me)
        draw_Picture(me.imgOrigin, me.src, me.imgSize, me.color, 1);
        if(me.imgSize_x > 1 || me.imgSize_y > 1)
                draw_ClearClip();
+       SUPER(Image).draw(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)
@@ -59,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
@@ -72,14 +85,14 @@ void Image_updateAspect(entity me)
                {
                        me.imgSize_x = sz_x / me.size_x;
                        me.imgSize_y = sz_y / me.size_y;
-                       if(me.initialForcedZoom < 0 && (me.imgSize_x > 1 || me.imgSize_y > 1))
+                       if(me.zoomBox < 0 && (me.imgSize_x > 1 || me.imgSize_y > 1))
                        {
                                // image larger than the containing box, zoom it out to fit into the box
                                if(me.size_x > asp * me.size_y)
-                                       me.initialForcedZoom = (me.size_y * asp / me.size_x) / me.imgSize_x;
+                                       me.zoomBox = (me.size_y * asp / me.size_x) / me.imgSize_x;
                                else
-                                       me.initialForcedZoom = (me.size_x / (asp * me.size_y)) / me.imgSize_y;
-                               me.zoomFactor = me.initialForcedZoom;
+                                       me.zoomBox = (me.size_x / (asp * me.size_y)) / me.imgSize_y;
+                               me.zoomFactor = me.zoomBox;
                        }
                }
                else
@@ -99,8 +112,8 @@ void Image_updateAspect(entity me)
 
        if (me.zoomMax < 0)
        {
-               if(me.initialForcedZoom > 0)
-                       me.zoomMax = me.initialForcedZoom;
+               if(me.zoomBox > 0)
+                       me.zoomMax = me.zoomBox;
                else
                {
                        if(me.size_x > asp * me.size_y)
@@ -117,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';
@@ -149,36 +177,36 @@ void Image_setZoom(entity me, float z, float atMousePosition)
 {
        float prev_zoomFactor;
        prev_zoomFactor = me.zoomFactor;
-       if (z < 0) // multiply by the current zoomFactor
+       if (z < 0) // multiply by the current zoomFactor (but can also snap to real dimensions or to box)
        {
                me.zoomFactor *= -z;
-               float one_in_the_middle, initialZoom_in_the_middle;
-               one_in_the_middle = ((prev_zoomFactor - 1) * (me.zoomFactor - 1) < 0);
-               initialZoom_in_the_middle = (me.initialForcedZoom > 0 && (prev_zoomFactor - me.initialForcedZoom) * (me.zoomFactor - me.initialForcedZoom) < 0);
-               if (one_in_the_middle && initialZoom_in_the_middle)
+               float realSize_in_the_middle, boxSize_in_the_middle;
+               realSize_in_the_middle = ((prev_zoomFactor - 1) * (me.zoomFactor - 1) < 0);
+               boxSize_in_the_middle = (me.zoomBox > 0 && (prev_zoomFactor - me.zoomBox) * (me.zoomFactor - me.zoomBox) < 0);
+               if (realSize_in_the_middle && boxSize_in_the_middle)
                {
                        // snap to real dimensions or to box
                        if (prev_zoomFactor < me.zoomFactor)
-                               me.zoomFactor = min(1, me.initialForcedZoom);
+                               me.zoomFactor = min(1, me.zoomBox);
                        else
-                               me.zoomFactor = max(1, me.initialForcedZoom);
+                               me.zoomFactor = max(1, me.zoomBox);
                }
-               else if (one_in_the_middle)
+               else if (realSize_in_the_middle)
                        me.zoomFactor = 1; // snap to real dimensions
-               else if (initialZoom_in_the_middle)
-                       me.zoomFactor = me.initialForcedZoom; // snap to box
+               else if (boxSize_in_the_middle)
+                       me.zoomFactor = me.zoomBox; // snap to box
        }
        else if (z == 0) // reset (no zoom)
        {
-               if (me.initialForcedZoom > 0)
-                       me.zoomFactor = me.initialForcedZoom;
+               if (me.zoomBox > 0)
+                       me.zoomFactor = me.zoomBox;
                else
                        me.zoomFactor = 1;
        }
        else // directly set
                me.zoomFactor = z;
        me.zoomFactor = bound(1/16, me.zoomFactor, 16);
-       if (me.zoomFactor > me.zoomMax)
+       if (me.zoomMax > 0 && me.zoomFactor > me.zoomMax)
                me.zoomFactor = me.zoomMax;
        if (prev_zoomFactor != me.zoomFactor)
        {
@@ -187,9 +215,9 @@ void Image_setZoom(entity me, float z, float atMousePosition)
                {
                        me.zoomOffset_x = me.start_zoomOffset_x + (me.start_coords_x - 0.5) / me.imgSize_x;
                        me.zoomOffset_y = me.start_zoomOffset_y + (me.start_coords_y - 0.5) / me.imgSize_y;
-                       // updateAspect will reset however zoomOffset to '0.5 0.5 0' if with
-                       // this zoomFactor the image will not be zoomed (updateAspect will check
-                       // the new values of imgSize).
+                       // updateAspect will reset zoomOffset to '0.5 0.5 0' if
+                       // with this zoomFactor the image will not be zoomed in
+                       // (updateAspect will check the new values of imgSize).
                }
        }
        me.updateAspect(me);