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