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