Include gmqcc binaries for Windows and Linux
[voretournament/voretournament.git] / data / qcsrc / menu / item / nexposee.c
1 #ifdef INTERFACE\r
2 CLASS(Nexposee) EXTENDS(Container)\r
3         METHOD(Nexposee, draw, void(entity))\r
4         METHOD(Nexposee, keyDown, float(entity, float, float, float))\r
5         METHOD(Nexposee, keyUp, float(entity, float, float, float))\r
6         METHOD(Nexposee, mousePress, float(entity, vector))\r
7         METHOD(Nexposee, mouseMove, float(entity, vector))\r
8         METHOD(Nexposee, mouseRelease, float(entity, vector))\r
9         METHOD(Nexposee, mouseDrag, float(entity, vector))\r
10         METHOD(Nexposee, resizeNotify, void(entity, vector, vector, vector, vector))\r
11         METHOD(Nexposee, focusEnter, void(entity))\r
12         METHOD(Nexposee, close, void(entity))\r
13 \r
14         ATTRIB(Nexposee, animationState, float, -1)\r
15         ATTRIB(Nexposee, animationFactor, float, 0)\r
16         ATTRIB(Nexposee, selectedChild, entity, NULL)\r
17         ATTRIB(Nexposee, mouseFocusedChild, entity, NULL)\r
18         METHOD(Nexposee, addItem, void(entity, entity, vector, vector, float))\r
19         METHOD(Nexposee, calc, void(entity))\r
20         METHOD(Nexposee, setNexposee, void(entity, entity, vector, float, float))\r
21         ATTRIB(Nexposee, mousePosition, vector, '0 0 0')\r
22         METHOD(Nexposee, pullNexposee, void(entity, entity, vector))\r
23 ENDCLASS(Nexposee)\r
24 \r
25 void ExposeeCloseButton_Click(entity button, entity other); // un-exposees the current state\r
26 #endif\r
27 \r
28 // animation states:\r
29 //   0 = thumbnails seen\r
30 //   1 = zooming in\r
31 //   2 = zoomed in\r
32 //   3 = zooming out\r
33 // animation factor: 0 = minimum theSize, 1 = maximum theSize\r
34 \r
35 #ifdef IMPLEMENTATION\r
36 \r
37 .vector Nexposee_initialSize;\r
38 .vector Nexposee_initialOrigin;\r
39 .float Nexposee_initialAlpha;\r
40 \r
41 .vector Nexposee_smallSize;\r
42 .vector Nexposee_smallOrigin;\r
43 .float Nexposee_smallAlpha;\r
44 .float Nexposee_mediumAlpha;\r
45 .vector Nexposee_scaleCenter;\r
46 .vector Nexposee_align;\r
47 .float Nexposee_animationFactor;\r
48 \r
49 void closeNexposee(entity me)\r
50 {\r
51         // user must override this\r
52 }\r
53 \r
54 void ExposeeCloseButton_Click(entity button, entity other)\r
55 {\r
56         other.selectedChild = other.focusedChild;\r
57         other.setFocus(other, NULL);\r
58         other.animationState = 3;\r
59 }\r
60 \r
61 void resizeNotifyNexposee(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)\r
62 {\r
63         me.calc(me);\r
64         me.resizeNotifyLie(me, relOrigin, relSize, absOrigin, absSize, Nexposee_initialOrigin, Nexposee_initialSize);\r
65 }\r
66 \r
67 void Nexposee_Calc_Scale(entity me, float scale)\r
68 {\r
69         entity e;\r
70         for(e = me.firstChild; e; e = e.nextSibling)\r
71         {\r
72                 e.Nexposee_smallOrigin = (e.Nexposee_initialOrigin - e.Nexposee_scaleCenter) * scale + e.Nexposee_scaleCenter;\r
73                 e.Nexposee_smallSize = e.Nexposee_initialSize * scale;\r
74                 if(e.Nexposee_align_x > 0)\r
75                         e.Nexposee_smallOrigin_x = 1 - e.Nexposee_align_x * scale;\r
76                 if(e.Nexposee_align_x < 0)\r
77                         e.Nexposee_smallOrigin_x = -e.Nexposee_smallSize_x + e.Nexposee_align_x * scale;\r
78                 if(e.Nexposee_align_y > 0)\r
79                         e.Nexposee_smallOrigin_y = 1 - e.Nexposee_align_y * scale;\r
80                 if(e.Nexposee_align_y < 0)\r
81                         e.Nexposee_smallOrigin_y = -e.Nexposee_smallSize_y + e.Nexposee_align_y * scale;\r
82         }\r
83 }\r
84 \r
85 void calcNexposee(entity me)\r
86 {\r
87         /*\r
88          * patented by Apple\r
89          * can't put that here ;)\r
90          */\r
91         float scale;\r
92         entity e, e2;\r
93         vector emins, emaxs, e2mins, e2maxs;\r
94         \r
95         for(scale = 0.7;; scale *= 0.99)\r
96         {\r
97                 Nexposee_Calc_Scale(me, scale);\r
98 \r
99                 for(e = me.firstChild; e; e = e.nextSibling)\r
100                 {\r
101                         emins = e.Nexposee_smallOrigin;\r
102                         emaxs = emins + e.Nexposee_smallSize;\r
103                         for(e2 = e.nextSibling; e2; e2 = e2.nextSibling)\r
104                         {\r
105                                 e2mins = e2.Nexposee_smallOrigin;\r
106                                 e2maxs = e2mins + e2.Nexposee_smallSize;\r
107 \r
108                                 // two intervals [amins, amaxs] and [bmins, bmaxs] overlap if:\r
109                                 //   amins < bmins < amaxs < bmaxs\r
110                                 // for which suffices\r
111                                 //   bmins < amaxs\r
112                                 //   amins < bmaxs\r
113                                 if((e2mins_x - emaxs_x) * (emins_x - e2maxs_x) > 0) // x overlap\r
114                                         if((e2mins_y - emaxs_y) * (emins_y - e2maxs_y) > 0) // y overlap\r
115                                         {\r
116                                                 goto have_overlap;\r
117                                         }\r
118                         }\r
119                 }\r
120 \r
121                 break;\r
122 :have_overlap\r
123         }\r
124 \r
125         scale *= 0.95;\r
126 \r
127         Nexposee_Calc_Scale(me, scale);\r
128 }\r
129 \r
130 void setNexposeeNexposee(entity me, entity other, vector scalecenter, float a0, float a1)\r
131 {\r
132         other.Nexposee_scaleCenter = scalecenter;\r
133         other.Nexposee_smallAlpha = a0;\r
134         me.setAlphaOf(me, other, a0);\r
135         other.Nexposee_mediumAlpha = a1;\r
136 }\r
137 \r
138 void drawNexposee(entity me)\r
139 {\r
140         float a;\r
141         float a0;\r
142         entity e;\r
143         float f;\r
144 \r
145         if(me.animationState == -1)\r
146         {\r
147                 me.animationState = 0;\r
148         }\r
149 \r
150         //print(ftos(me.animationState), "\n");\r
151 \r
152         f = min(1, frametime * 5);\r
153         switch(me.animationState)\r
154         {\r
155                 case 0:\r
156                         me.animationFactor = 0;\r
157                         break;\r
158                 case 1:\r
159                         me.animationFactor += f;\r
160                         if(me.animationFactor >= 1)\r
161                         {\r
162                                 me.animationFactor = 1;\r
163                                 me.animationState = 2;\r
164                                 setFocusContainer(me, me.selectedChild);\r
165                         }\r
166                         break;\r
167                 case 2:\r
168                         me.animationFactor = 1;\r
169                         break;\r
170                 case 3:\r
171                         me.animationFactor -= f;\r
172                         me.mouseFocusedChild = me.itemFromPoint(me, me.mousePosition);\r
173                         if(me.animationFactor <= 0)\r
174                         {\r
175                                 me.animationFactor = 0;\r
176                                 me.animationState = 0;\r
177                                 me.selectedChild = me.mouseFocusedChild;\r
178                         }\r
179                         break;\r
180         }\r
181 \r
182         f = min(1, frametime * 10);\r
183         for(e = me.firstChild; e; e = e.nextSibling)\r
184         {\r
185                 if(e == me.selectedChild)\r
186                 {\r
187                         e.Container_origin = e.Nexposee_smallOrigin * (1 - me.animationFactor) + e.Nexposee_initialOrigin * me.animationFactor;\r
188                         e.Container_size = e.Nexposee_smallSize * (1 - me.animationFactor) + e.Nexposee_initialSize * me.animationFactor;\r
189                         e.Nexposee_animationFactor = me.animationFactor;\r
190                         a0 = e.Nexposee_mediumAlpha;\r
191                         if(me.animationState == 3)\r
192                                 if(e != me.mouseFocusedChild)\r
193                                         a0 = e.Nexposee_smallAlpha;\r
194                         a = a0 * (1 - me.animationFactor) + me.animationFactor;\r
195                 }\r
196                 else\r
197                 {\r
198                         // minimum theSize counts\r
199                         e.Container_origin = e.Nexposee_smallOrigin;\r
200                         e.Container_size = e.Nexposee_smallSize;\r
201                         e.Nexposee_animationFactor = 0;\r
202                         a = e.Nexposee_smallAlpha * (1 - me.animationFactor);\r
203                 }\r
204                 me.setAlphaOf(me, e, e.Container_alpha * (1 - f) + a * f);\r
205 \r
206                 e.Container_fontscale = globalToBoxSize(e.Container_size, e.Nexposee_initialSize);\r
207         }\r
208 \r
209         drawContainer(me);\r
210 };\r
211 \r
212 float mousePressNexposee(entity me, vector pos)\r
213 {\r
214         if(me.animationState == 0)\r
215         {\r
216                 me.mouseFocusedChild = NULL;\r
217                 mouseMoveNexposee(me, pos);\r
218                 if(me.mouseFocusedChild)\r
219                 {\r
220                         me.animationState = 1;\r
221                         setFocusContainer(me, NULL);\r
222                 }\r
223                 else\r
224                         me.close(me);\r
225                 return 1;\r
226         }\r
227         else if(me.animationState == 2)\r
228         {\r
229                 if not(mousePressContainer(me, pos))\r
230                 {\r
231                         me.animationState = 3;\r
232                         setFocusContainer(me, NULL);\r
233                 }\r
234                 return 1;\r
235         }\r
236         return 0;\r
237 }\r
238 \r
239 float mouseReleaseNexposee(entity me, vector pos)\r
240 {\r
241         if(me.animationState == 2)\r
242                 return mouseReleaseContainer(me, pos);\r
243         return 0;\r
244 }\r
245 \r
246 float mouseDragNexposee(entity me, vector pos)\r
247 {\r
248         if(me.animationState == 2)\r
249                 return mouseDragContainer(me, pos);\r
250         return 0;\r
251 }\r
252 \r
253 float mouseMoveNexposee(entity me, vector pos)\r
254 {\r
255         entity e;\r
256         me.mousePosition = pos;\r
257         e = me.mouseFocusedChild;\r
258         me.mouseFocusedChild = me.itemFromPoint(me, pos);\r
259         if(me.animationState == 2)\r
260                 return mouseMoveContainer(me, pos);\r
261         if(me.animationState == 0)\r
262         {\r
263                 if(me.mouseFocusedChild)\r
264                         if(me.mouseFocusedChild != e)\r
265                                 me.selectedChild = me.mouseFocusedChild;\r
266                 return 1;\r
267         }\r
268         return 0;\r
269 }\r
270 \r
271 float keyUpNexposee(entity me, float scan, float ascii, float shift)\r
272 {\r
273         if(me.animationState == 2)\r
274                 return keyUpContainer(me, scan, ascii, shift);\r
275         return 0;\r
276 }\r
277 \r
278 float keyDownNexposee(entity me, float scan, float ascii, float shift)\r
279 {\r
280         float nexposeeKey;\r
281         if(me.animationState == 2)\r
282                 if(keyDownContainer(me, scan, ascii, shift))\r
283                         return 1;\r
284         if(scan == K_TAB)\r
285         {\r
286                 if(me.animationState == 0)\r
287                 {\r
288                         if(shift & S_SHIFT)\r
289                         {\r
290                                 if(me.selectedChild)\r
291                                         me.selectedChild = me.selectedChild.prevSibling;\r
292                                 if not(me.selectedChild)\r
293                                         me.selectedChild = me.lastChild;\r
294                         }\r
295                         else\r
296                         {\r
297                                 if(me.selectedChild)\r
298                                         me.selectedChild = me.selectedChild.nextSibling;\r
299                                 if not(me.selectedChild)\r
300                                         me.selectedChild = me.firstChild;\r
301                         }\r
302                 }\r
303         }\r
304         switch(me.animationState)\r
305         {\r
306                 case 0:\r
307                 case 3:\r
308                         nexposeeKey = ((scan == K_SPACE) || (scan == K_ENTER));\r
309                         break;\r
310                 case 1:\r
311                 case 2:\r
312                         nexposeeKey = (scan == K_ESCAPE);\r
313                         break;\r
314         }\r
315         if(nexposeeKey)\r
316         {\r
317                 switch(me.animationState)\r
318                 {\r
319                         case 0:\r
320                         case 3:\r
321                                 me.animationState = 1;\r
322                                 break;\r
323                         case 1:\r
324                         case 2:\r
325                                 me.animationState = 3;\r
326                                 break;\r
327                 }\r
328                 if(me.focusedChild)\r
329                         me.selectedChild = me.focusedChild;\r
330                 if not(me.selectedChild)\r
331                         me.animationState = 0;\r
332                 setFocusContainer(me, NULL);\r
333                 return 1;\r
334         }\r
335         return 0;\r
336 }\r
337 \r
338 void addItemNexposee(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)\r
339 {\r
340         addItemContainer(me, other, theOrigin, theSize, theAlpha);\r
341         other.Nexposee_initialSize = other.Container_size;\r
342         other.Nexposee_initialOrigin = other.Container_origin;\r
343         other.Nexposee_initialAlpha = other.Container_alpha;\r
344 }\r
345 \r
346 void focusEnterNexposee(entity me)\r
347 {\r
348         if(me.animationState == 2)\r
349                 setFocusContainer(me, me.selectedChild);\r
350 }\r
351 \r
352 void pullNexposeeNexposee(entity me, entity other, vector theAlign)\r
353 {\r
354         other.Nexposee_align = theAlign;\r
355 }\r
356 #endif\r