]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu/menu.qc
oh, adding missing cvheck
[divverent/nexuiz.git] / data / qcsrc / menu / 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 menuInitialized;
16 float menuNotTheFirstFrame;
17 float menuMouseMode;
18
19 void SUB_Null() { };
20
21 void() m_init =
22 {
23         dprint_load();
24 }
25
26 void UpdateConWidthHeight()
27 {
28         float conwidth_s, conheight_s;
29         conwidth_s = conwidth;
30         conheight_s = conheight;
31         conwidth = cvar("vid_conwidth");
32         conheight = cvar("vid_conheight");
33         if(conwidth < 800)
34         {
35                 conheight *= 800 / conwidth;
36                 conwidth = 800;
37         }
38         if(conheight < 600)
39         {
40                 conwidth *= 600 / conheight;
41                 conheight = 600;
42         }
43         if(main)
44         {
45                 if(conwidth_s != conwidth || conheight_s != conheight)
46                 {
47                         draw_reset();
48                         main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
49                 }
50         }
51 }
52
53 void() m_init_delayed =
54 {
55         float fh, glob, n, i;
56         string s;
57
58         dprint_load();
59
60         menuInitialized = 0;
61         if(!preMenuInit())
62                 return;
63         menuInitialized = 1;
64         GameCommand_Init();
65
66         fh = -1;
67         if(cvar_string("menu_skin") != "")
68         {
69                 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
70                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
71         }
72         if(fh < 0)
73         if(cvar_defstring("menu_skin") != "")
74         {
75                 draw_currentSkin = strcat("gfx/menu/", cvar_defstring("menu_skin"));
76                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
77         }
78         if(fh < 0)
79         {
80                 draw_currentSkin = "gfx/menu/default";
81                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
82         }
83         draw_currentSkin = strzone(draw_currentSkin);
84         while((s = fgets(fh)))
85                 if(tokenize(s) == 2)
86                         Skin_ApplySetting(argv(0), argv(1));
87         fclose(fh);
88
89         glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
90         if(glob >= 0)
91         {
92                 n = search_getsize(glob);
93                 for(i = 0; i < n; ++i)
94                         precache_pic(search_getfilename(glob, i));
95                 search_end(glob);
96         }
97
98         draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
99
100         conwidth = conheight = -1;
101         draw_reset();
102         UpdateConWidthHeight();
103         main = spawnMainWindow(); main.configureMainWindow(main);
104         main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
105         main.focused = 1;
106         menuShiftState = 0;
107         menuMousePos = '0.5 0.5 0';
108
109         if(Menu_Active)
110                 m_display(); // delayed menu display
111 };
112
113 void(float key, float ascii) m_keyup =
114 {
115         if(!menuInitialized)
116                 return;
117         if(!Menu_Active)
118                 return;
119         draw_reset();
120         main.keyUp(main, key, ascii, menuShiftState);
121         if(key >= K_MOUSE1 && key <= K_MOUSE3)
122         {
123                 --mouseButtonsPressed;
124                 if(!mouseButtonsPressed)
125                         main.mouseRelease(main, menuMousePos);
126                 if(mouseButtonsPressed < 0)
127                 {
128                         mouseButtonsPressed = 0;
129                         print("Warning: released an already released button\n");
130                 }
131         }
132         if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
133         if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
134         if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
135 };
136
137 void(float key, float ascii) m_keydown =
138 {
139         if(!menuInitialized)
140                 return;
141         if(!Menu_Active)
142                 return;
143         if(keyGrabber)
144         {
145                 entity e;
146                 e = keyGrabber;
147                 keyGrabber = NULL;
148                 e.keyGrabbed(e, key, ascii);
149         }
150         else
151         {
152                 draw_reset();
153                 if(!main.keyDown(main, key, ascii, menuShiftState))
154                         if(key == K_ESCAPE)
155                                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
156                                         m_hide(); // disable menu on unhandled ESC
157                 if(key >= K_MOUSE1 && key <= K_MOUSE3)
158                         if(!mouseButtonsPressed)
159                                 main.mousePress(main, menuMousePos);
160         }
161         if(key >= K_MOUSE1 && key <= K_MOUSE3)
162         {
163                 ++mouseButtonsPressed;
164                 if(mouseButtonsPressed > 10)
165                 {
166                         mouseButtonsPressed = 10;
167                         print("Warning: pressed an already pressed button\n");
168                 }
169         }
170         if(key == K_ALT) menuShiftState |= S_ALT;
171         if(key == K_CTRL) menuShiftState |= S_CTRL;
172         if(key == K_SHIFT) menuShiftState |= S_SHIFT;
173 };
174
175 void(string img, float a) drawBackground =
176 {
177         vector sz;
178         vector isz;
179         sz = draw_PictureSize(img);
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, img, isz, '1 1 1', a);
196 }
197
198 void() m_draw =
199 {
200         float t;
201         float realFrametime;
202
203         menuMouseMode = cvar("menu_mouse_absolute");
204
205         if(main)
206                 UpdateConWidthHeight();
207
208         if(!menuInitialized)
209         {
210                 // TODO draw an info image about this situation
211                 m_init_delayed();
212                 return;
213         }
214         if(!menuNotTheFirstFrame)
215         {
216                 menuNotTheFirstFrame = 1;
217                 if(Menu_Active)
218                 if(!cvar("menu_video_played"))
219                 {
220                         localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/male/welcome.ogg\n");
221                         menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
222                 }
223         }
224
225         t = gettime();
226         realFrametime = frametime = min(0.2, t - menuPrevTime);
227         menuPrevTime = t;
228         time += frametime;
229
230         t = cvar("menu_slowmo");
231         if(t)
232         {
233                 frametime *= t;
234                 realFrametime *= t;
235         }
236         else
237                 t = 1;
238
239         if(Menu_Active)
240         {
241                 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
242                         setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
243                 else
244                         m_hide();
245         }
246
247         if(cvar("cl_capturevideo"))
248                 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
249
250         dprint_load();
251         gamestatus = 0;
252         if(isserver())
253                 gamestatus = gamestatus | GAME_ISSERVER;
254         if(clientstate() == CS_CONNECTED)
255                 gamestatus = gamestatus | GAME_CONNECTED;
256         if(cvar("developer"))
257                 gamestatus = gamestatus | GAME_DEVELOPER;
258
259         prevMenuAlpha = menuAlpha;
260         if(Menu_Active)
261         {
262                 if(menuAlpha == 0 && menuLogoAlpha < 2)
263                 {
264                         menuLogoAlpha = menuLogoAlpha + frametime * 2;
265                 }
266                 else
267                 {
268                         menuAlpha = min(1, menuAlpha + frametime * 5);
269                         menuLogoAlpha = 2;
270                 }
271         }
272         else
273         {
274                 menuAlpha = max(0, menuAlpha - frametime * 5);
275                 menuLogoAlpha = 2;
276         }
277
278         draw_reset();
279
280         if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
281         {
282                 if(menuLogoAlpha > 0)
283                 {
284                         drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1));
285                         if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
286                         {
287                                 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
288                                 draw_drawMousePointer(menuMousePos);
289                                 draw_alpha = 1;
290                         }
291                 }
292         }
293         else if(SKINALPHA_BACKGROUND_INGAME)
294         {
295                 if(menuAlpha > 0)
296                         drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME);
297         }
298
299         draw_reset();
300         preMenuDraw();
301         draw_reset();
302
303         if(menuAlpha <= 0)
304         {
305                 if(prevMenuAlpha > 0)
306                         main.initializeDialog(main, main.firstChild);
307                 draw_reset();
308                 postMenuDraw();
309                 return;
310         }
311
312         draw_alpha *= menuAlpha;
313
314         if(menuMouseMode)
315         {
316                 vector newMouse;
317                 newMouse = globalToBoxSize(getmousepos(), draw_scale);
318                 if(newMouse != '0 0 0')
319                         if(newMouse != menuMousePos)
320                         {
321                                 menuMousePos = newMouse;
322                                 if(mouseButtonsPressed)
323                                         main.mouseDrag(main, menuMousePos);
324                                 else
325                                         main.mouseMove(main, menuMousePos);
326                         }
327         }
328         else
329         {
330                 if(frametime > 0)
331                 {
332                         vector dMouse;
333                         dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
334                         if(dMouse != '0 0 0')
335                         {
336                                 dMouse = globalToBoxSize(dMouse, draw_scale);
337                                 menuMousePos += dMouse * cvar("menu_mouse_speed");
338                                 menuMousePos_x = bound(0, menuMousePos_x, 1);
339                                 menuMousePos_y = bound(0, menuMousePos_y, 1);
340                                 if(mouseButtonsPressed)
341                                         main.mouseDrag(main, menuMousePos);
342                                 else
343                                         main.mouseMove(main, menuMousePos);
344                         }
345                 }
346         }
347         main.draw(main);
348         draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
349
350         draw_drawMousePointer(menuMousePos);
351
352         draw_reset();
353         postMenuDraw();
354
355         frametime = 0;
356 };
357
358 void() m_display =
359 {
360         Menu_Active = true;
361         setkeydest(KEY_MENU);
362         setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
363
364         if(!menuInitialized)
365                 return;
366
367         if(mouseButtonsPressed)
368                 main.mouseRelease(main, menuMousePos);
369         mouseButtonsPressed = 0;
370
371         main.focusEnter(main);
372         main.showNotify(main);
373 };
374
375 void() m_hide =
376 {
377         Menu_Active = false;
378         setkeydest(KEY_GAME);
379         setmousetarget(MT_CLIENT);
380
381         if(!menuInitialized)
382                 return;
383
384         main.focusLeave(main);
385         main.hideNotify(main);
386 };
387
388 void() m_toggle =
389 {
390         if(Menu_Active)
391                 m_hide();
392         else
393                 m_display();
394 };
395
396 void() m_shutdown =
397 {
398         entity e;
399
400         m_hide();
401         for(e = NULL; (e = nextent(e)) != NULL; )
402         {
403                 if(e.destroy)
404                         e.destroy(e);
405         }
406 };
407
408 void(string itemname) m_goto =
409 {
410         entity e;
411         if(!menuInitialized)
412                 return;
413         if(itemname == "") // this can be called by GameCommand
414         {
415                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
416                         m_hide();
417                 else
418                 {
419                         main.initializeDialog(main, main.firstChild);
420                         m_display();
421                 }
422         }
423         else
424         {
425                 e = findstring(NULL, name, itemname);
426                 if(e && e.parent == main)
427                 {
428                         m_hide();
429                         main.initializeDialog(main, e);
430                         m_display();
431                 }
432         }
433 }