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