2 CLASS(Image) EXTENDS(Item)
3 METHOD(Image, configureImage, void(entity, string))
4 METHOD(Image, draw, void(entity))
5 METHOD(Image, toString, string(entity))
6 METHOD(Image, resizeNotify, void(entity, vector, vector, vector, vector))
7 METHOD(Image, updateAspect, void(entity))
8 METHOD(Image, setZoom, void(entity, float, float))
9 METHOD(Image, drag_setStartPos, float(entity, vector))
10 METHOD(Image, drag, float(entity, vector))
11 ATTRIB(Image, src, string, string_null)
12 ATTRIB(Image, color, vector, '1 1 1')
13 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
14 ATTRIB(Image, initialForcedZoom, float, 0) // used by forcedAspect -2 when the image is larger than the containing box
15 ATTRIB(Image, zoomFactor, float, 1)
16 ATTRIB(Image, zoomOffset, vector, '0.5 0.5 0')
17 ATTRIB(Image, zoomTime, float, 0)
18 ATTRIB(Image, start_zoomOffset, vector, '0 0 0')
19 ATTRIB(Image, start_coords, vector, '0 0 0')
20 ATTRIB(Image, imgOrigin, vector, '0 0 0')
21 ATTRIB(Image, imgSize, vector, '0 0 0')
26 string Image_toString(entity me)
30 void Image_configureImage(entity me, string path)
33 me.zoomOffset = '0.5 0.5 0';
35 if (me.forcedAspect == -2)
36 me.initialForcedZoom = -1; // calculate initialForcedZoom at the first updateAspect call
38 void Image_draw(entity me)
40 if(me.imgSize_x > 1 || me.imgSize_y > 1)
42 draw_Picture(me.imgOrigin, me.src, me.imgSize, me.color, 1);
43 if(me.imgSize_x > 1 || me.imgSize_y > 1)
46 void Image_updateAspect(entity me)
49 if(me.size_x <= 0 || me.size_y <= 0)
51 if(me.forcedAspect == 0)
53 me.imgOrigin = '0 0 0';
58 if(me.forcedAspect < 0)
61 sz = draw_PictureSize(me.src);
63 if(me.forcedAspect <= -2)
65 me.imgSize_x = sz_x / me.size_x;
66 me.imgSize_y = sz_y / me.size_y;
70 asp = me.forcedAspect;
72 if(me.initialForcedZoom < 0 && (me.imgSize_x > 1 || me.imgSize_y > 1))
74 // image larger than the containing box, zoom it out to fit into the box
75 if(me.size_x > asp * me.size_y)
76 me.initialForcedZoom = (me.size_y * asp / me.size_x) / me.imgSize_x;
78 me.initialForcedZoom = (me.size_x / (asp * me.size_y)) / me.imgSize_y;
79 me.zoomFactor = me.initialForcedZoom;
81 else if (me.forcedAspect > -2)
83 if(me.size_x > asp * me.size_y)
85 // x too large, so center x-wise
86 me.imgSize = eY + eX * (me.size_y * asp / me.size_x);
90 // y too large, so center y-wise
91 me.imgSize = eX + eY * (me.size_x / (asp * me.size_y));
97 if (me.zoomFactor > 1)
99 me.zoomOffset_x = bound(0, me.zoomOffset_x, 1);
100 me.zoomOffset_y = bound(0, me.zoomOffset_y, 1);
102 me.imgSize = me.imgSize * me.zoomFactor;
104 me.imgOrigin_x = 0.5 - me.zoomOffset_x * me.imgSize_x;
105 me.imgOrigin_y = 0.5 - me.zoomOffset_y * me.imgSize_y;
108 float Image_drag_setStartPos(entity me, vector coords)
110 //if (me.zoomFactor > 1) //mousewheel zoom may start from a non-zoomed-in image
112 me.start_zoomOffset = me.zoomOffset;
113 me.start_coords = coords;
117 float Image_drag(entity me, vector coords)
119 if(me.imgSize_x > 1 || me.imgSize_y > 1)
121 me.zoomOffset_x = me.start_zoomOffset_x + (me.start_coords_x - coords_x) / me.imgSize_x;
122 me.zoomOffset_y = me.start_zoomOffset_y + (me.start_coords_y - coords_y) / me.imgSize_y;
127 void Image_setZoom(entity me, float z, float atMousePosition)
129 float prev_zoomFactor;
130 prev_zoomFactor = me.zoomFactor;
131 if (z < 0) // multiply by the current zoomFactor
134 float one_in_the_middle, initialZoom_in_the_middle;
135 one_in_the_middle = ((prev_zoomFactor - 1) * (me.zoomFactor - 1) < 0);
136 initialZoom_in_the_middle = (me.initialForcedZoom > 0 && (prev_zoomFactor - me.initialForcedZoom) * (me.zoomFactor - me.initialForcedZoom) < 0);
137 if (one_in_the_middle && initialZoom_in_the_middle)
139 // snap to real dimensions or to box
140 if (prev_zoomFactor < me.zoomFactor)
141 me.zoomFactor = min(1, me.initialForcedZoom);
143 me.zoomFactor = max(1, me.initialForcedZoom);
145 else if (one_in_the_middle)
146 me.zoomFactor = 1; // snap to real dimensions
147 else if (initialZoom_in_the_middle)
148 me.zoomFactor = me.initialForcedZoom; // snap to box
150 else if (z == 0) // reset (no zoom)
152 if (me.initialForcedZoom > 0)
153 me.zoomFactor = me.initialForcedZoom;
159 me.zoomFactor = bound(1/16, me.zoomFactor, 16);
160 if (me.zoomFactor <= 1)
161 me.zoomOffset = '0.5 0.5 0';
162 else if (atMousePosition && prev_zoomFactor != me.zoomFactor)
163 me.zoomOffset = me.start_zoomOffset + (me.start_coords - '0.5 0.5 0') * (1/prev_zoomFactor);
164 if (prev_zoomFactor != me.zoomFactor)
168 void Image_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
170 SUPER(Image).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);