]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu/menu.qc
sbar_hudselector 0 is still default so do not invert the checkbox
[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(string img, float a) drawBackground =
169 {
170         vector sz;
171         vector isz;
172         sz = draw_PictureSize(img);
173         // keep aspect of image
174         if(sz_x * draw_scale_y >= sz_y * draw_scale_x)
175         {
176                 // that is, sz_x/sz_y >= draw_scale_x/draw_scale_y
177                 // match up the height
178                 isz_y = 1;
179                 isz_x = isz_y * (sz_x / sz_y) * (draw_scale_y / draw_scale_x);
180         }
181         else
182         {
183                 // that is, sz_x/sz_y <= draw_scale_x/draw_scale_y
184                 // match up the width
185                 isz_x = 1;
186                 isz_y = isz_x * (sz_y / sz_x) * (draw_scale_x / draw_scale_y);
187         }
188         draw_Picture('0.5 0.5 0' - 0.5 * isz, img, isz, '1 1 1', a);
189 }
190
191 void() m_draw =
192 {
193         float t;
194         float realFrametime;
195
196         if(main)
197                 UpdateConWidthHeight();
198
199         if(!menuInitialized)
200         {
201                 // TODO draw an info image about this situation
202                 m_init_delayed();
203                 return;
204         }
205         if(!menuNotTheFirstFrame)
206         {
207                 menuNotTheFirstFrame = 1;
208                 if(Menu_Active)
209                 if(!cvar("menu_video_played"))
210                 {
211                         localcmd("set menu_video_played 1; cd loop 1; play sound/announcer/male/welcome.ogg\n");
212                         menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
213                 }
214         }
215
216         t = gettime();
217         realFrametime = frametime = min(0.2, t - menuPrevTime);
218         menuPrevTime = t;
219         time += frametime;
220
221         t = cvar("menu_slowmo");
222         if(t)
223         {
224                 frametime *= t;
225                 realFrametime *= t;
226         }
227         else
228                 t = 1;
229
230         if(Menu_Active)
231         {
232                 if(getmousetarget() == MT_MENU && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
233                         setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
234                 else
235                         m_hide();
236         }
237
238         if(cvar("cl_capturevideo"))
239                 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
240
241         dprint_load();
242         gamestatus = 0;
243         if(isserver())
244                 gamestatus = gamestatus | GAME_ISSERVER;
245         if(clientstate() == CS_CONNECTED)
246                 gamestatus = gamestatus | GAME_CONNECTED;
247         if(cvar("developer"))
248                 gamestatus = gamestatus | GAME_DEVELOPER;
249
250         prevMenuAlpha = menuAlpha;
251         if(Menu_Active)
252         {
253                 if(menuAlpha == 0 && menuLogoAlpha < 2)
254                 {
255                         menuLogoAlpha = menuLogoAlpha + frametime * 2;
256                 }
257                 else
258                 {
259                         menuAlpha = min(1, menuAlpha + frametime * 5);
260                         menuLogoAlpha = 2;
261                 }
262         }
263         else
264         {
265                 menuAlpha = max(0, menuAlpha - frametime * 5);
266                 menuLogoAlpha = 2;
267         }
268
269         draw_reset();
270
271         if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
272         {
273                 if(menuLogoAlpha > 0)
274                 {
275                         drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1));
276                         if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
277                         {
278                                 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
279                                 draw_drawMousePointer(menuMousePos);
280                                 draw_alpha = 1;
281                         }
282                 }
283         }
284         else if(SKINALPHA_BACKGROUND_INGAME)
285         {
286                 if(menuAlpha > 0)
287                         drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME);
288         }
289
290         draw_reset();
291         preMenuDraw();
292         draw_reset();
293
294         if(menuAlpha <= 0)
295         {
296                 if(prevMenuAlpha > 0)
297                         main.initializeDialog(main, main.firstChild);
298                 draw_reset();
299                 postMenuDraw();
300                 return;
301         }
302
303         draw_alpha *= menuAlpha;
304
305         if(frametime > 0)
306         {
307                 vector dMouse;
308                 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
309                 if(dMouse != '0 0 0')
310                 {
311                         dMouse = globalToBoxSize(dMouse, draw_scale);
312                         menuMousePos += dMouse * 1; // TODO use a cvar here
313                         menuMousePos_x = bound(0, menuMousePos_x, 1);
314                         menuMousePos_y = bound(0, menuMousePos_y, 1);
315                         if(mouseButtonsPressed)
316                                 main.mouseDrag(main, menuMousePos);
317                         else
318                                 main.mouseMove(main, menuMousePos);
319                 }
320         }
321         main.draw(main);
322         draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
323
324         draw_drawMousePointer(menuMousePos);
325
326         draw_reset();
327         postMenuDraw();
328
329         frametime = 0;
330 };
331
332 void() m_display =
333 {
334         Menu_Active = true;
335         setkeydest(KEY_MENU);
336         setmousetarget(MT_MENU);
337
338         if(!menuInitialized)
339                 return;
340
341         if(mouseButtonsPressed)
342                 main.mouseRelease(main, menuMousePos);
343         mouseButtonsPressed = 0;
344
345         main.focusEnter(main);
346         main.showNotify(main);
347 };
348
349 void() m_hide =
350 {
351         Menu_Active = false;
352         setkeydest(KEY_GAME);
353         setmousetarget(MT_CLIENT);
354
355         if(!menuInitialized)
356                 return;
357
358         main.focusLeave(main);
359         main.hideNotify(main);
360 };
361
362 void() m_toggle =
363 {
364         if(Menu_Active)
365                 m_hide();
366         else
367                 m_display();
368 };
369
370 void() m_shutdown =
371 {
372         m_hide();
373 };
374
375 void(string itemname) m_goto =
376 {
377         entity e;
378         if(!menuInitialized)
379                 return;
380         if(itemname == "") // this can be called by GameCommand
381         {
382                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
383                         m_hide();
384                 else
385                 {
386                         main.initializeDialog(main, main.firstChild);
387                         m_display();
388                 }
389         }
390         else
391         {
392                 e = findstring(NULL, name, itemname);
393                 if(e && e.parent == main)
394                 {
395                         m_hide();
396                         main.initializeDialog(main, e);
397                         m_display();
398                 }
399         }
400 }