]> icculus.org git repositories - divverent/nexuiz.git/blob - qcsrc/menu-div0test/menu.qc
play intro video on startup by QC now
[divverent/nexuiz.git] / 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 prevMenuAlpha;
14 float menuLoadedMaplist;
15 float menuNotTheFirstFrame;
16
17 void SUB_Null() { };
18
19 void() m_init =
20 {
21         dprint_load();
22 }
23
24 void() m_init_delayed =
25 {
26         dprint_load();
27
28         menuLoadedMaplist = 0;
29         MapInfo_Enumerate();
30         if(!MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 1))
31                 return;
32         menuLoadedMaplist = 1;
33
34         GameCommand_Init();
35
36         if(cvar("developer") == 42)
37                 draw_currentSkin = "qcsrc/menu-div0test/xawskin";
38         else
39                 draw_currentSkin = "qcsrc/menu-div0test/defaultskin";
40         // TODO load SKIN constants from file later
41         
42         draw_setMousePointer("cursor");
43
44         main = spawnMainWindow(); main.configureMainWindow(main);
45         draw_reset();
46         main.resizeNotify(main, draw_shift, draw_scale, draw_shift, draw_scale);
47         main.focused = 1;
48         menuShiftState = 0;
49
50         if(Menu_Active)
51                 m_display(); // delayed menu display
52 };
53
54 void(float key, float ascii) m_keyup =
55 {
56         if(!menuLoadedMaplist)
57                 return;
58         if(!Menu_Active)
59                 return;
60         draw_reset();
61         main.keyUp(main, key, ascii, menuShiftState);
62         if(key >= K_MOUSE1 && key <= K_MOUSE3)
63         {
64                 --mouseButtonsPressed;
65                 if(!mouseButtonsPressed)
66                         main.mouseRelease(main, menuMousePos);
67                 if(mouseButtonsPressed < 0)
68                 {
69                         mouseButtonsPressed = 0;
70                         print("Warning: released an already released button\n");
71                 }
72         }
73         if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
74         if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
75         if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
76 };
77
78 void(float key, float ascii) m_keydown =
79 {
80         if(!menuLoadedMaplist)
81                 return;
82         if(!Menu_Active)
83                 return;
84         if(keyGrabber)
85         {
86                 entity e;
87                 e = keyGrabber;
88                 keyGrabber = NULL;
89                 e.keyGrabbed(e, key, ascii);
90         }
91         else
92         {
93                 draw_reset();
94                 if(!main.keyDown(main, key, ascii, menuShiftState))
95                         if(key == K_ESCAPE)
96                                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
97                                         m_hide(); // disable menu on unhandled ESC
98                 if(key >= K_MOUSE1 && key <= K_MOUSE3)
99                         if(!mouseButtonsPressed)
100                                 main.mousePress(main, menuMousePos);
101         }
102         if(key >= K_MOUSE1 && key <= K_MOUSE3)
103         {
104                 ++mouseButtonsPressed;
105                 if(mouseButtonsPressed > 10)
106                 {
107                         mouseButtonsPressed = 10;
108                         print("Warning: pressed an already pressed button\n");
109                 }
110         }
111         if(key == K_ALT) menuShiftState |= S_ALT;
112         if(key == K_CTRL) menuShiftState |= S_CTRL;
113         if(key == K_SHIFT) menuShiftState |= S_SHIFT;
114 };
115
116 void() m_draw =
117 {
118         float t;
119         float realFrametime;
120         if(!menuNotTheFirstFrame)
121         {
122                 menuNotTheFirstFrame = 1;
123                 if(Menu_Active)
124                 if(!cvar("menu_video_played"))
125                         localcmd("set menu_video_played 1; playvideo logo; cd loop 1; play sound/announcer/male/welcome.ogg\n");
126         }
127         if(!menuLoadedMaplist)
128         {
129                 // TODO draw an info image about this situation
130                 m_init_delayed();
131                 return;
132         }
133         t = gettime();
134         realFrametime = frametime = min(0.2, t - menuPrevTime);
135         menuPrevTime = t;
136         time += frametime;
137
138         if(getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED)
139         {
140                 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
141         }
142         else
143         {
144                 if(Menu_Active)
145                         if(getmousetarget() != MT_MENU)
146                                 m_hide();
147         }
148
149         if(cvar("cl_capturevideo"))
150                 frametime = 1 / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
151
152         prevMenuAlpha = menuAlpha;
153         if(Menu_Active)
154                 menuAlpha = min(1, menuAlpha + frametime * 5);
155         else
156                 menuAlpha = max(0, menuAlpha - frametime * 5);
157
158         if(menuAlpha <= 0)
159         {
160                 if(prevMenuAlpha > 0)
161                         main.initializeDialog(main, main.firstChild);
162                 return;
163         }
164
165         dprint_load();
166         gamestatus = 0;
167         if(isserver())
168                 gamestatus = gamestatus | GAME_ISSERVER;
169         if(clientstate() == CS_CONNECTED)
170                 gamestatus = gamestatus | GAME_CONNECTED;
171         if(cvar("developer"))
172                 gamestatus = gamestatus | GAME_DEVELOPER;
173
174         draw_reset();
175         draw_alpha *= menuAlpha;
176
177         if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
178         {
179                 vector sz;
180                 vector isz;
181                 sz = draw_PictureSize(SKINGFX_BACKGROUND);
182                 // keep aspect of image
183                 if(sz_x * draw_scale_y >= sz_y * draw_scale_x)
184                 {
185                         // that is, sz_x/sz_y >= draw_scale_x/draw_scale_y
186                         // match up the height
187                         isz_y = 1;
188                         isz_x = isz_y * (sz_x / sz_y) * (draw_scale_y / draw_scale_x);
189                 }
190                 else
191                 {
192                         // that is, sz_x/sz_y <= draw_scale_x/draw_scale_y
193                         // match up the width
194                         isz_x = 1;
195                         isz_y = isz_x * (sz_y / sz_x) * (draw_scale_x / draw_scale_y);
196                 }
197                 draw_Picture('0.5 0.5 0' - 0.5 * isz, SKINGFX_BACKGROUND, isz, '1 1 1', 1);
198         }
199
200         if(frametime > 0)
201         {
202                 vector dMouse;
203                 dMouse = getmousepos();
204                 dMouse *= frametime / realFrametime; // for capturevideo
205                 if(dMouse != '0 0 0')
206                 {
207                         dMouse = globalToBoxSize(dMouse, draw_scale);
208                         menuMousePos += dMouse * 1; // TODO use a cvar here
209                         menuMousePos_x = bound(0, menuMousePos_x, 1);
210                         menuMousePos_y = bound(0, menuMousePos_y, 1);
211                         if(mouseButtonsPressed)
212                                 main.mouseDrag(main, menuMousePos);
213                         else
214                                 main.mouseMove(main, menuMousePos);
215                 }
216         }
217         main.draw(main);
218         draw_drawMousePointer(menuMousePos);
219
220         frametime = 0;
221 };
222
223 void() m_display =
224 {
225         Menu_Active = true;
226         setkeydest(KEY_MENU);
227         setmousetarget(MT_MENU);
228
229         if(!menuLoadedMaplist)
230                 return;
231
232         if(mouseButtonsPressed)
233                 main.mouseRelease(main, menuMousePos);
234         mouseButtonsPressed = 0;
235
236         main.focusEnter(main);
237         main.showNotify(main);
238 };
239
240 void() m_hide =
241 {
242         Menu_Active = false;
243         setkeydest(KEY_GAME);
244         setmousetarget(MT_CLIENT);
245
246         if(!menuLoadedMaplist)
247                 return;
248
249         main.focusLeave(main);
250         main.hideNotify(main);
251 };
252
253 void() m_toggle =
254 {
255         if(Menu_Active)
256                 m_hide();
257         else
258                 m_display();
259 };
260
261 void() m_shutdown =
262 {
263         m_hide();
264 };
265
266 void(string itemname) m_goto =
267 {
268         entity e;
269         if(!menuLoadedMaplist)
270                 return;
271         if(itemname == "") // this can be called by GameCommand
272         {
273                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
274                         m_hide();
275                 else
276                 {
277                         main.initializeDialog(main, main.firstChild);
278                         m_display();
279                 }
280         }
281         else
282         {
283                 e = findstring(NULL, name, itemname);
284                 if(e && e.parent == main)
285                 {
286                         m_hide();
287                         main.initializeDialog(main, e);
288                         m_display();
289                 }
290         }
291 }