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