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