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