]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu/menu.qc
r6574 | tzork | 2009-04-23 14:56:15 +0200 (Thu, 23 Apr 2009) | 1 line
[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         check_unacceptable_compiler_bugs();
25 }
26
27 void UpdateConWidthHeight()
28 {
29         float conwidth_s, conheight_s;
30         conwidth_s = conwidth;
31         conheight_s = conheight;
32         conwidth = cvar("vid_conwidth");
33         conheight = cvar("vid_conheight");
34         if(conwidth < 800)
35         {
36                 conheight *= 800 / conwidth;
37                 conwidth = 800;
38         }
39         if(conheight < 600)
40         {
41                 conwidth *= 600 / conheight;
42                 conheight = 600;
43         }
44         if(main)
45         {
46                 if(conwidth_s != conwidth || conheight_s != conheight)
47                 {
48                         draw_reset();
49                         main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
50                 }
51         }
52 }
53
54 void() m_init_delayed =
55 {
56         float fh, glob, n, i;
57         string s, s1, s2;
58
59         dprint_load();
60
61         menuInitialized = 0;
62         if(!preMenuInit())
63                 return;
64         menuInitialized = 1;
65         GameCommand_Init();
66
67         RegisterWeapons();
68
69         fh = -1;
70         if(cvar_string("menu_skin") != "")
71         {
72                 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
73                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
74         }
75         if(fh < 0)
76         if(cvar_defstring("menu_skin") != "")
77         {
78                 draw_currentSkin = strcat("gfx/menu/", cvar_defstring("menu_skin"));
79                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
80         }
81         if(fh < 0)
82         {
83                 draw_currentSkin = "gfx/menu/default";
84                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
85         }
86         draw_currentSkin = strzone(draw_currentSkin);
87         while((s = fgets(fh)))
88         {
89                 // these two are handled by skinlist.qc
90                 if(substring(s, 0, 6) == "title ")
91                         continue;
92                 if(substring(s, 0, 7) == "author ")
93                         continue;
94                 n = tokenize_sane(s);
95                 if(n >= 2)
96                 {
97                         s1 = argv(0);
98                         s2 = substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1));
99                         Skin_ApplySetting(s1, s2); // fteqcc sucks
100                 }
101         }
102         fclose(fh);
103
104         glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
105         if(glob >= 0)
106         {
107                 n = search_getsize(glob);
108                 for(i = 0; i < n; ++i)
109                         precache_pic(search_getfilename(glob, i));
110                 search_end(glob);
111         }
112
113         draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
114
115         conwidth = conheight = -1;
116         draw_reset();
117         UpdateConWidthHeight();
118         main = spawnMainWindow(); main.configureMainWindow(main);
119         main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
120         main.focused = 1;
121         menuShiftState = 0;
122         menuMousePos = '0.5 0.5 0';
123
124         if(Menu_Active)
125                 m_display(); // delayed menu display
126 };
127
128 void(float key, float ascii) m_keyup =
129 {
130         if(!menuInitialized)
131                 return;
132         if(!Menu_Active)
133                 return;
134         draw_reset();
135         main.keyUp(main, key, ascii, menuShiftState);
136         if(key >= K_MOUSE1 && key <= K_MOUSE3)
137         {
138                 --mouseButtonsPressed;
139                 if(!mouseButtonsPressed)
140                         main.mouseRelease(main, menuMousePos);
141                 if(mouseButtonsPressed < 0)
142                 {
143                         mouseButtonsPressed = 0;
144                         print("Warning: released an already released button\n");
145                 }
146         }
147         if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
148         if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
149         if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
150 };
151
152 void(float key, float ascii) m_keydown =
153 {
154         if(!menuInitialized)
155                 return;
156         if(!Menu_Active)
157                 return;
158         if(keyGrabber)
159         {
160                 entity e;
161                 e = keyGrabber;
162                 keyGrabber = NULL;
163                 e.keyGrabbed(e, key, ascii);
164         }
165         else
166         {
167                 draw_reset();
168                 if(key >= K_MOUSE1 && key <= K_MOUSE3)
169                         if(!mouseButtonsPressed)
170                                 main.mousePress(main, menuMousePos);
171                 if(!main.keyDown(main, key, ascii, menuShiftState))
172                         if(key == K_ESCAPE)
173                                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
174                                         m_hide(); // disable menu on unhandled ESC
175         }
176         if(key >= K_MOUSE1 && key <= K_MOUSE3)
177         {
178                 ++mouseButtonsPressed;
179                 if(mouseButtonsPressed > 10)
180                 {
181                         mouseButtonsPressed = 10;
182                         print("Warning: pressed an already pressed button\n");
183                 }
184         }
185         if(key == K_ALT) menuShiftState |= S_ALT;
186         if(key == K_CTRL) menuShiftState |= S_CTRL;
187         if(key == K_SHIFT) menuShiftState |= S_SHIFT;
188 };
189
190 void(string img, float a) drawBackground =
191 {
192         vector sz;
193         vector isz;
194         sz = draw_PictureSize(img);
195         // keep aspect of image
196         if(sz_x * draw_scale_y >= sz_y * draw_scale_x)
197         {
198                 // that is, sz_x/sz_y >= draw_scale_x/draw_scale_y
199                 // match up the height
200                 isz_y = 1;
201                 isz_x = isz_y * (sz_x / sz_y) * (draw_scale_y / draw_scale_x);
202         }
203         else
204         {
205                 // that is, sz_x/sz_y <= draw_scale_x/draw_scale_y
206                 // match up the width
207                 isz_x = 1;
208                 isz_y = isz_x * (sz_y / sz_x) * (draw_scale_x / draw_scale_y);
209         }
210         draw_Picture('0.5 0.5 0' - 0.5 * isz, img, isz, '1 1 1', a);
211 }
212
213 void() m_draw =
214 {
215         float t;
216         float realFrametime;
217
218         menuMouseMode = cvar("menu_mouse_absolute");
219
220         if(main)
221                 UpdateConWidthHeight();
222
223         if(!menuInitialized)
224         {
225                 // TODO draw an info image about this situation
226                 m_init_delayed();
227                 return;
228         }
229         if(!menuNotTheFirstFrame)
230         {
231                 menuNotTheFirstFrame = 1;
232                 if(Menu_Active)
233                 if(!cvar("menu_video_played"))
234                 {
235                         localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/male/welcome.ogg\n");
236                         menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
237                 }
238         }
239
240         t = gettime();
241         realFrametime = frametime = min(0.2, t - menuPrevTime);
242         menuPrevTime = t;
243         time += frametime;
244
245         t = cvar("menu_slowmo");
246         if(t)
247         {
248                 frametime *= t;
249                 realFrametime *= t;
250         }
251         else
252                 t = 1;
253
254         if(Menu_Active)
255         {
256                 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
257                         setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
258                 else
259                         m_hide();
260         }
261
262         if(cvar("cl_capturevideo"))
263                 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
264
265         dprint_load();
266         gamestatus = 0;
267         if(isserver())
268                 gamestatus = gamestatus | GAME_ISSERVER;
269         if(clientstate() == CS_CONNECTED)
270                 gamestatus = gamestatus | GAME_CONNECTED;
271         if(cvar("developer"))
272                 gamestatus = gamestatus | GAME_DEVELOPER;
273
274         prevMenuAlpha = menuAlpha;
275         if(Menu_Active)
276         {
277                 if(menuAlpha == 0 && menuLogoAlpha < 2)
278                 {
279                         menuLogoAlpha = menuLogoAlpha + frametime * 2;
280                 }
281                 else
282                 {
283                         menuAlpha = min(1, menuAlpha + frametime * 5);
284                         menuLogoAlpha = 2;
285                 }
286         }
287         else
288         {
289                 menuAlpha = max(0, menuAlpha - frametime * 5);
290                 menuLogoAlpha = 2;
291         }
292
293         draw_reset();
294
295         if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
296         {
297                 if(menuLogoAlpha > 0)
298                 {
299                         drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1));
300                         if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
301                         {
302                                 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
303                                 draw_drawMousePointer(menuMousePos);
304                                 draw_alpha = 1;
305                         }
306                 }
307         }
308         else if(SKINALPHA_BACKGROUND_INGAME)
309         {
310                 if(menuAlpha > 0)
311                         drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME);
312         }
313
314         draw_reset();
315         preMenuDraw();
316         draw_reset();
317
318         if(menuAlpha <= 0)
319         {
320                 if(prevMenuAlpha > 0)
321                         main.initializeDialog(main, main.firstChild);
322                 draw_reset();
323                 postMenuDraw();
324                 return;
325         }
326
327         draw_alpha *= menuAlpha;
328
329         if(menuMouseMode)
330         {
331                 vector newMouse;
332                 newMouse = globalToBoxSize(getmousepos(), draw_scale);
333                 if(newMouse != '0 0 0')
334                         if(newMouse != menuMousePos)
335                         {
336                                 menuMousePos = newMouse;
337                                 if(mouseButtonsPressed)
338                                         main.mouseDrag(main, menuMousePos);
339                                 else
340                                         main.mouseMove(main, menuMousePos);
341                         }
342         }
343         else
344         {
345                 if(frametime > 0)
346                 {
347                         vector dMouse;
348                         dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
349                         if(dMouse != '0 0 0')
350                         {
351                                 dMouse = globalToBoxSize(dMouse, draw_scale);
352                                 menuMousePos += dMouse * cvar("menu_mouse_speed");
353                                 menuMousePos_x = bound(0, menuMousePos_x, 1);
354                                 menuMousePos_y = bound(0, menuMousePos_y, 1);
355                                 if(mouseButtonsPressed)
356                                         main.mouseDrag(main, menuMousePos);
357                                 else
358                                         main.mouseMove(main, menuMousePos);
359                         }
360                 }
361         }
362         main.draw(main);
363         draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
364
365         draw_drawMousePointer(menuMousePos);
366
367         draw_reset();
368         postMenuDraw();
369
370         frametime = 0;
371 };
372
373 void() m_display =
374 {
375         Menu_Active = true;
376         setkeydest(KEY_MENU);
377         setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
378
379         if(!menuInitialized)
380                 return;
381
382         if(mouseButtonsPressed)
383                 main.mouseRelease(main, menuMousePos);
384         mouseButtonsPressed = 0;
385
386         main.focusEnter(main);
387         main.showNotify(main);
388 };
389
390 void() m_hide =
391 {
392         Menu_Active = false;
393         setkeydest(KEY_GAME);
394         setmousetarget(MT_CLIENT);
395
396         if(!menuInitialized)
397                 return;
398
399         main.focusLeave(main);
400         main.hideNotify(main);
401 };
402
403 void() m_toggle =
404 {
405         if(Menu_Active)
406                 m_hide();
407         else
408                 m_display();
409 };
410
411 void() m_shutdown =
412 {
413         entity e;
414
415         m_hide();
416         for(e = NULL; (e = nextent(e)) != NULL; )
417         {
418                 if(e.destroy)
419                         e.destroy(e);
420         }
421 };
422
423 void m_focus_item_chain(entity outermost, entity innermost)
424 {
425         if(innermost.parent != outermost)
426                 m_focus_item_chain(outermost, innermost.parent);
427         innermost.parent.setFocus(innermost.parent, innermost);
428 }
429
430 void m_activate_window(entity wnd)
431 {
432         entity par;
433         par = wnd.parent;
434         if(par)
435                 m_activate_window(par);
436
437         if(par.instanceOfModalController)
438         {
439                 if(wnd.tabSelectingButton)
440                         // tabs
441                         TabButton_Click(wnd.tabSelectingButton, wnd);
442                 else
443                         // root
444                         par.initializeDialog(par, wnd);
445         }
446         else if(par.instanceOfNexposee)
447         {
448                 // nexposee (sorry for violating abstraction here)
449                 par.selectedChild = wnd;
450                 par.animationState = 1;
451                 setFocusContainer(par, NULL);
452         }
453         else if(par.instanceOfContainer)
454         {
455                 // other containers
456                 if(par.focused)
457                         par.setFocus(par, wnd);
458         }
459 }
460
461 void m_setpointerfocus(entity wnd)
462 {
463         if(wnd.instanceOfContainer)
464         {
465                 entity focus = wnd.preferredFocusedGrandChild(wnd);
466                 if(focus)
467                 {
468                         menuMousePos = focus.origin + 0.5 * focus.size;
469                         menuMousePos_x *= 1 / conwidth;
470                         menuMousePos_y *= 1 / conheight;
471                         if(wnd.focused) // why does this never happen?
472                                 m_focus_item_chain(wnd, focus);
473                 }
474         }
475 }
476
477 void(string itemname) m_goto =
478 {
479         entity e;
480         if(!menuInitialized)
481                 return;
482         if(itemname == "") // this can be called by GameCommand
483         {
484                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
485                         m_hide();
486                 else
487                 {
488                         m_activate_window(main.mainNexposee);
489                         m_display();
490                 }
491         }
492         else
493         {
494                 e = findstring(NULL, name, itemname);
495                 if(e)
496                 {
497                         m_hide();
498                         m_activate_window(e);
499                         m_setpointerfocus(e);
500                         m_display();
501                 }
502         }
503 }
504
505 void() m_goto_skin_selector =
506 {
507         if(!menuInitialized)
508                 return;
509         // TODO add code to switch back to the skin selector (no idea how to do it now)
510         m_goto("skinselector");
511 }