]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/menu.qc
slightly changed fading
[divverent/nexuiz.git] / data / qcsrc / menu-div0test / menu.qc
1 ///////////////////////////////////////////////
2 // Menu Source File
3 ///////////////////////
4 // This file belongs to dpmod/darkplaces
5 // AK contains all menu functions (especially the required ones)
6 ///////////////////////////////////////////////
7
8 float mouseButtonsPressed;
9 vector menuMousePos;
10 float menuShiftState;
11 float menuPrevTime;
12 float menuAlpha;
13 float menuLogoAlpha;
14 float prevMenuAlpha;
15 float menuLoadedMaplist;
16 float menuNotTheFirstFrame;
17
18 void SUB_Null() { };
19
20 void() m_init =
21 {
22         dprint_load();
23 }
24
25 void() m_init_delayed =
26 {
27         dprint_load();
28
29         menuLoadedMaplist = 0;
30         MapInfo_Enumerate();
31         if(!MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 1))
32                 return;
33         menuLoadedMaplist = 1;
34
35         GameCommand_Init();
36
37         if(cvar("developer") == 42)
38                 draw_currentSkin = "qcsrc/menu-div0test/xawskin";
39         else
40                 draw_currentSkin = "qcsrc/menu-div0test/defaultskin";
41         // TODO load SKIN constants from file later
42         
43         draw_setMousePointer("cursor");
44
45         main = spawnMainWindow(); main.configureMainWindow(main);
46         draw_reset();
47         main.resizeNotify(main, draw_shift, draw_scale, draw_shift, draw_scale);
48         main.focused = 1;
49         menuShiftState = 0;
50
51         if(Menu_Active)
52                 m_display(); // delayed menu display
53 };
54
55 void(float key, float ascii) m_keyup =
56 {
57         if(!menuLoadedMaplist)
58                 return;
59         if(!Menu_Active)
60                 return;
61         draw_reset();
62         main.keyUp(main, key, ascii, menuShiftState);
63         if(key >= K_MOUSE1 && key <= K_MOUSE3)
64         {
65                 --mouseButtonsPressed;
66                 if(!mouseButtonsPressed)
67                         main.mouseRelease(main, menuMousePos);
68                 if(mouseButtonsPressed < 0)
69                 {
70                         mouseButtonsPressed = 0;
71                         print("Warning: released an already released button\n");
72                 }
73         }
74         if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
75         if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
76         if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
77 };
78
79 void(float key, float ascii) m_keydown =
80 {
81         if(!menuLoadedMaplist)
82                 return;
83         if(!Menu_Active)
84                 return;
85         if(keyGrabber)
86         {
87                 entity e;
88                 e = keyGrabber;
89                 keyGrabber = NULL;
90                 e.keyGrabbed(e, key, ascii);
91         }
92         else
93         {
94                 draw_reset();
95                 if(!main.keyDown(main, key, ascii, menuShiftState))
96                         if(key == K_ESCAPE)
97                                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
98                                         m_hide(); // disable menu on unhandled ESC
99                 if(key >= K_MOUSE1 && key <= K_MOUSE3)
100                         if(!mouseButtonsPressed)
101                                 main.mousePress(main, menuMousePos);
102         }
103         if(key >= K_MOUSE1 && key <= K_MOUSE3)
104         {
105                 ++mouseButtonsPressed;
106                 if(mouseButtonsPressed > 10)
107                 {
108                         mouseButtonsPressed = 10;
109                         print("Warning: pressed an already pressed button\n");
110                 }
111         }
112         if(key == K_ALT) menuShiftState |= S_ALT;
113         if(key == K_CTRL) menuShiftState |= S_CTRL;
114         if(key == K_SHIFT) menuShiftState |= S_SHIFT;
115 };
116
117 void() m_draw =
118 {
119         float t;
120         float realFrametime;
121
122         if(!menuNotTheFirstFrame)
123         {
124                 menuNotTheFirstFrame = 1;
125                 if(Menu_Active)
126                 if(!cvar("menu_video_played"))
127                 {
128                         localcmd("set menu_video_played 1; cd loop 1; play sound/announcer/male/welcome.ogg\n");
129                         menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
130                 }
131         }
132         if(!menuLoadedMaplist)
133         {
134                 // TODO draw an info image about this situation
135                 m_init_delayed();
136                 return;
137         }
138         t = gettime();
139         realFrametime = frametime = min(0.2, t - menuPrevTime);
140         menuPrevTime = t;
141         time += frametime;
142
143         if(getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED)
144         {
145                 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
146         }
147         else
148         {
149                 if(Menu_Active)
150                         if(getmousetarget() != MT_MENU)
151                                 m_hide();
152         }
153
154         if(cvar("cl_capturevideo"))
155                 frametime = 1 / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
156
157         prevMenuAlpha = menuAlpha;
158         if(Menu_Active)
159         {
160                 if(menuAlpha == 0 && menuLogoAlpha < 2)
161                 {
162                         menuLogoAlpha = menuLogoAlpha + frametime * 2;
163                 }
164                 else
165                 {
166                         menuAlpha = min(1, menuAlpha + frametime * 5);
167                         menuLogoAlpha = 3;
168                 }
169         }
170         else
171                 menuAlpha = max(0, menuAlpha - frametime * 5);
172
173         draw_reset();
174
175         if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
176         {
177                 vector sz;
178                 vector isz;
179                 sz = draw_PictureSize(SKINGFX_BACKGROUND);
180                 // keep aspect of image
181                 if(sz_x * draw_scale_y >= sz_y * draw_scale_x)
182                 {
183                         // that is, sz_x/sz_y >= draw_scale_x/draw_scale_y
184                         // match up the height
185                         isz_y = 1;
186                         isz_x = isz_y * (sz_x / sz_y) * (draw_scale_y / draw_scale_x);
187                 }
188                 else
189                 {
190                         // that is, sz_x/sz_y <= draw_scale_x/draw_scale_y
191                         // match up the width
192                         isz_x = 1;
193                         isz_y = isz_x * (sz_y / sz_x) * (draw_scale_x / draw_scale_y);
194                 }
195                 draw_Picture('0.5 0.5 0' - 0.5 * isz, SKINGFX_BACKGROUND, isz, '1 1 1', bound(0, menuLogoAlpha, 1));
196         }
197
198         if(menuAlpha <= 0)
199         {
200                 if(prevMenuAlpha > 0)
201                         main.initializeDialog(main, main.firstChild);
202                 return;
203         }
204
205         dprint_load();
206         gamestatus = 0;
207         if(isserver())
208                 gamestatus = gamestatus | GAME_ISSERVER;
209         if(clientstate() == CS_CONNECTED)
210                 gamestatus = gamestatus | GAME_CONNECTED;
211         if(cvar("developer"))
212                 gamestatus = gamestatus | GAME_DEVELOPER;
213
214         draw_alpha *= menuAlpha;
215
216         if(frametime > 0)
217         {
218                 vector dMouse;
219                 dMouse = getmousepos();
220                 dMouse *= frametime / realFrametime; // for capturevideo
221                 if(dMouse != '0 0 0')
222                 {
223                         dMouse = globalToBoxSize(dMouse, draw_scale);
224                         menuMousePos += dMouse * 1; // TODO use a cvar here
225                         menuMousePos_x = bound(0, menuMousePos_x, 1);
226                         menuMousePos_y = bound(0, menuMousePos_y, 1);
227                         if(mouseButtonsPressed)
228                                 main.mouseDrag(main, menuMousePos);
229                         else
230                                 main.mouseMove(main, menuMousePos);
231                 }
232         }
233         main.draw(main);
234         draw_drawMousePointer(menuMousePos);
235
236         frametime = 0;
237 };
238
239 void() m_display =
240 {
241         Menu_Active = true;
242         setkeydest(KEY_MENU);
243         setmousetarget(MT_MENU);
244
245         if(!menuLoadedMaplist)
246                 return;
247
248         if(mouseButtonsPressed)
249                 main.mouseRelease(main, menuMousePos);
250         mouseButtonsPressed = 0;
251
252         main.focusEnter(main);
253         main.showNotify(main);
254 };
255
256 void() m_hide =
257 {
258         Menu_Active = false;
259         setkeydest(KEY_GAME);
260         setmousetarget(MT_CLIENT);
261
262         if(!menuLoadedMaplist)
263                 return;
264
265         main.focusLeave(main);
266         main.hideNotify(main);
267 };
268
269 void() m_toggle =
270 {
271         if(Menu_Active)
272                 m_hide();
273         else
274                 m_display();
275 };
276
277 void() m_shutdown =
278 {
279         m_hide();
280 };
281
282 void(string itemname) m_goto =
283 {
284         entity e;
285         if(!menuLoadedMaplist)
286                 return;
287         if(itemname == "") // this can be called by GameCommand
288         {
289                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
290                         m_hide();
291                 else
292                 {
293                         main.initializeDialog(main, main.firstChild);
294                         m_display();
295                 }
296         }
297         else
298         {
299                 e = findstring(NULL, name, itemname);
300                 if(e && e.parent == main)
301                 {
302                         m_hide();
303                         main.initializeDialog(main, e);
304                         m_display();
305                 }
306         }
307 }