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