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