]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/image.qc
Merge branch 'master' into Mario/ctf_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / image.qc
1 #ifndef ITEM_IMAGE_H
2 #define ITEM_IMAGE_H
3 #include "../item.qc"
4 CLASS(Image, Item)
5         METHOD(Image, configureImage, void(entity, string))
6         METHOD(Image, draw, void(entity))
7         METHOD(Image, toString, string(entity))
8         METHOD(Image, resizeNotify, void(entity, vector, vector, vector, vector))
9         METHOD(Image, updateAspect, void(entity))
10         METHOD(Image, initZoom, void(entity))
11         METHOD(Image, setZoom, void(entity, float, float))
12         METHOD(Image, drag_setStartPos, float(entity, vector))
13         METHOD(Image, drag, float(entity, vector))
14         ATTRIB(Image, src, string, string_null)
15         ATTRIB(Image, color, vector, '1 1 1')
16         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
17         ATTRIB(Image, zoomBox, float, 0) // used by forcedAspect -2 when the image is larger than the containing box
18         ATTRIB(Image, zoomFactor, float, 1)
19         ATTRIB(Image, zoomOffset, vector, '0.5 0.5 0')
20         ATTRIB(Image, zoomSnapToTheBox, float, 1) // snap the zoomed in image to the box borders when zooming/dragging it
21         ATTRIB(Image, zoomTime, float, 0)
22         ATTRIB(Image, zoomLimitedByTheBox, float, 0) // forbids zoom if image would be larger than the containing box
23         ATTRIB(Image, zoomMax, float, 0)
24         ATTRIB(Image, start_zoomOffset, vector, '0 0 0')
25         ATTRIB(Image, start_coords, vector, '0 0 0')
26         ATTRIB(Image, imgOrigin, vector, '0 0 0')
27         ATTRIB(Image, imgSize, vector, '0 0 0')
28 ENDCLASS(Image)
29 #endif
30
31 #ifdef IMPLEMENTATION
32 string Image_toString(entity me)
33 {
34         return me.src;
35 }
36 void Image_configureImage(entity me, string path)
37 {
38         me.src = path;
39 }
40 void Image_initZoom(entity me)
41 {
42         me.zoomOffset = '0.5 0.5 0';
43         me.zoomFactor = 1;
44         if (me.forcedAspect == -2)
45                 me.zoomBox = -1; // calculate zoomBox at the first updateAspect call
46         if (me.zoomLimitedByTheBox)
47                 me.zoomMax = -1; // calculate zoomMax at the first updateAspect call
48 }
49
50 void Image_draw(entity me)
51 {
52         if(me.imgSize.x > 1 || me.imgSize.y > 1)
53                 draw_SetClip();
54         draw_Picture(me.imgOrigin, me.src, me.imgSize, me.color, 1);
55         if(me.imgSize.x > 1 || me.imgSize.y > 1)
56                 draw_ClearClip();
57         SUPER(Image).draw(me);
58 }
59 void Image_updateAspect(entity me)
60 {
61         float asp = 0;
62         if(me.size.x <= 0 || me.size.y <= 0)
63                 return;
64         if(me.forcedAspect == 0)
65         {
66                 me.imgOrigin = '0 0 0';
67                 me.imgSize = '1 1 0';
68         }
69         else
70         {
71                 vector sz = '0 0 0';
72                 if(me.forcedAspect < 0)
73                 {
74                         if (me.src != "")
75                                 sz = draw_PictureSize(me.src);
76                         if(sz.x <= 0 || sz.y <= 0)
77                         {
78                                 // image is broken or doesn't exist, set the size for the placeholder image
79                                 sz.x = me.size.x;
80                                 sz.y = me.size.y;
81                         }
82                         asp = sz.x / sz.y;
83                 }
84                 else
85                         asp = me.forcedAspect;
86
87                 if(me.forcedAspect <= -2)
88                 {
89                         me.imgSize_x = sz.x / me.size.x;
90                         me.imgSize_y = sz.y / me.size.y;
91                         if(me.zoomBox < 0 && (me.imgSize.x > 1 || me.imgSize.y > 1))
92                         {
93                                 // image larger than the containing box, zoom it out to fit into the box
94                                 if(me.size.x > asp * me.size.y)
95                                         me.zoomBox = (me.size.y * asp / me.size.x) / me.imgSize.x;
96                                 else
97                                         me.zoomBox = (me.size.x / (asp * me.size.y)) / me.imgSize.y;
98                                 me.zoomFactor = me.zoomBox;
99                         }
100                 }
101                 else
102                 {
103                         if(me.size.x > asp * me.size.y)
104                         {
105                                 // x too large, so center x-wise
106                                 me.imgSize = eY + eX * (me.size.y * asp / me.size.x);
107                         }
108                         else
109                         {
110                                 // y too large, so center y-wise
111                                 me.imgSize = eX + eY * (me.size.x / (asp * me.size.y));
112                         }
113                 }
114         }
115
116         if (me.zoomMax < 0)
117         {
118                 if(me.zoomBox > 0)
119                         me.zoomMax = me.zoomBox;
120                 else
121                 {
122                         if(me.size.x > asp * me.size.y)
123                                 me.zoomMax = (me.size.y * asp / me.size.x) / me.imgSize.x;
124                         else
125                                 me.zoomMax = (me.size.x / (asp * me.size.y)) / me.imgSize.y;
126                 }
127         }
128
129         if (me.zoomMax > 0 && me.zoomFactor > me.zoomMax)
130                 me.zoomFactor = me.zoomMax;
131         if (me.zoomFactor)
132                 me.imgSize = me.imgSize * me.zoomFactor;
133
134         if(me.imgSize.x > 1 || me.imgSize.y > 1)
135         {
136                 if(me.zoomSnapToTheBox)
137                 {
138                         if(me.imgSize.x > 1)
139                                 me.zoomOffset_x = bound(0.5/me.imgSize.x, me.zoomOffset.x, 1 - 0.5/me.imgSize.x);
140                         else
141                                 me.zoomOffset_x = bound(1 - 0.5/me.imgSize.x, me.zoomOffset.x, 0.5/me.imgSize.x);
142
143                         if(me.imgSize.y > 1)
144                                 me.zoomOffset_y = bound(0.5/me.imgSize.y, me.zoomOffset.y, 1 - 0.5/me.imgSize.y);
145                         else
146                                 me.zoomOffset_y = bound(1 - 0.5/me.imgSize.y, me.zoomOffset.y, 0.5/me.imgSize.y);
147                 }
148                 else
149                 {
150                         me.zoomOffset_x = bound(0, me.zoomOffset.x, 1);
151                         me.zoomOffset_y = bound(0, me.zoomOffset.y, 1);
152                 }
153         }
154         else
155                 me.zoomOffset = '0.5 0.5 0';
156
157         me.imgOrigin_x = 0.5 - me.zoomOffset.x * me.imgSize.x;
158         me.imgOrigin_y = 0.5 - me.zoomOffset.y * me.imgSize.y;
159 }
160 float Image_drag_setStartPos(entity me, vector coords)
161 {
162         //if(me.imgSize_x > 1 || me.imgSize_y > 1) // check disabled: mousewheel zoom may start from a non-zoomed-in image
163         {
164                 me.start_zoomOffset = me.zoomOffset;
165                 me.start_coords = coords;
166         }
167         return 1;
168 }
169 float Image_drag(entity me, vector coords)
170 {
171         if(me.imgSize.x > 1 || me.imgSize.y > 1)
172         {
173                 me.zoomOffset_x = me.start_zoomOffset.x + (me.start_coords.x - coords.x) / me.imgSize.x;
174                 me.zoomOffset_y = me.start_zoomOffset.y + (me.start_coords.y - coords.y) / me.imgSize.y;
175                 me.updateAspect(me);
176         }
177         return 1;
178 }
179 void Image_setZoom(entity me, float z, float atMousePosition)
180 {
181         float prev_zoomFactor;
182         prev_zoomFactor = me.zoomFactor;
183         if (z < 0) // multiply by the current zoomFactor (but can also snap to real dimensions or to box)
184         {
185                 me.zoomFactor *= -z;
186                 float realSize_in_the_middle, boxSize_in_the_middle;
187                 realSize_in_the_middle = ((prev_zoomFactor - 1) * (me.zoomFactor - 1) < 0);
188                 boxSize_in_the_middle = (me.zoomBox > 0 && (prev_zoomFactor - me.zoomBox) * (me.zoomFactor - me.zoomBox) < 0);
189                 if (realSize_in_the_middle && boxSize_in_the_middle)
190                 {
191                         // snap to real dimensions or to box
192                         if (prev_zoomFactor < me.zoomFactor)
193                                 me.zoomFactor = min(1, me.zoomBox);
194                         else
195                                 me.zoomFactor = max(1, me.zoomBox);
196                 }
197                 else if (realSize_in_the_middle)
198                         me.zoomFactor = 1; // snap to real dimensions
199                 else if (boxSize_in_the_middle)
200                         me.zoomFactor = me.zoomBox; // snap to box
201         }
202         else if (z == 0) // reset (no zoom)
203         {
204                 if (me.zoomBox > 0)
205                         me.zoomFactor = me.zoomBox;
206                 else
207                         me.zoomFactor = 1;
208         }
209         else // directly set
210                 me.zoomFactor = z;
211         me.zoomFactor = bound(1/16, me.zoomFactor, 16);
212         if (me.zoomMax > 0 && me.zoomFactor > me.zoomMax)
213                 me.zoomFactor = me.zoomMax;
214         if (prev_zoomFactor != me.zoomFactor)
215         {
216                 me.zoomTime = time;
217                 if (atMousePosition)
218                 {
219                         me.zoomOffset_x = me.start_zoomOffset.x + (me.start_coords.x - 0.5) / me.imgSize.x;
220                         me.zoomOffset_y = me.start_zoomOffset.y + (me.start_coords.y - 0.5) / me.imgSize.y;
221                         // updateAspect will reset zoomOffset to '0.5 0.5 0' if
222                         // with this zoomFactor the image will not be zoomed in
223                         // (updateAspect will check the new values of imgSize).
224                 }
225         }
226         me.updateAspect(me);
227 }
228 void Image_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
229 {
230         SUPER(Image).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
231         me.updateAspect(me);
232 }
233 #endif