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