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