]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/menu.qc
added a consistency check to focus changing
[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 entity main;
9 float mouseButtonsPressed;
10 vector mousePos;
11 float shiftState;
12 float prevTime;
13 float menuAlpha;
14
15 void SUB_Null() { };
16
17 void() m_init =
18 {
19         dprint_load();
20         GameCommand_Init();
21         
22         draw_setMousePointer("gfx/cursor");
23
24         main = spawnMainWindow(); main.configureMainWindow(main);
25         draw_reset();
26         main.resizeNotify(main, draw_shift, draw_scale, draw_shift, draw_scale);
27         main.focused = 1;
28         shiftState = 0;
29 };
30
31 void(float key, float ascii) m_keyup =
32 {
33         if(!Menu_Active)
34                 return;
35         draw_reset();
36         main.keyUp(main, key, ascii, shiftState);
37         if(key >= K_MOUSE1 && key <= K_MOUSE10)
38         {
39                 --mouseButtonsPressed;
40                 if(!mouseButtonsPressed)
41                         main.mouseRelease(main, mousePos);
42                 if(mouseButtonsPressed < 0)
43                 {
44                         mouseButtonsPressed = 0;
45                         print("Warning: released an already released button\n");
46                 }
47         }
48         if(key == K_ALT) shiftState -= (shiftState & S_ALT);
49         if(key == K_CTRL) shiftState -= (shiftState & S_CTRL);
50         if(key == K_SHIFT) shiftState -= (shiftState & S_SHIFT);
51 };
52
53 void(float key, float ascii) m_keydown =
54 {
55         if(!Menu_Active)
56                 return;
57         draw_reset();
58         if(!main.keyDown(main, key, ascii, shiftState))
59                 if(key == K_ESCAPE)
60                         if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
61                                 m_hide(); // disable menu on unhandled ESC
62         if(key >= K_MOUSE1 && key <= K_MOUSE10)
63         {
64                 if(!mouseButtonsPressed)
65                         main.mousePress(main, mousePos);
66                 ++mouseButtonsPressed;
67                 if(mouseButtonsPressed > 10)
68                 {
69                         mouseButtonsPressed = 10;
70                         print("Warning: pressed an already pressed button\n");
71                 }
72         }
73         if(key == K_ALT) shiftState |= S_ALT;
74         if(key == K_CTRL) shiftState |= S_CTRL;
75         if(key == K_SHIFT) shiftState |= S_SHIFT;
76 };
77
78 void() m_draw =
79 {
80         float t;
81         float realFrametime;
82         t = gettime();
83         realFrametime = frametime = min(0.2, t - prevTime);
84         prevTime = t;
85
86         if(cvar("cl_capturevideo"))
87                 frametime = 1 / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
88
89         if(Menu_Active)
90                 menuAlpha = min(1, menuAlpha + frametime * 5);
91         else
92                 menuAlpha = max(0, menuAlpha - frametime * 5);
93
94         if(menuAlpha <= 0)
95                 return;
96
97         dprint_load();
98         gamestatus = 0;
99         if(isserver())
100                 gamestatus = gamestatus | GAME_ISSERVER;
101         if(clientstate() == CS_CONNECTED)
102                 gamestatus = gamestatus | GAME_CONNECTED;
103         if(cvar("developer"))
104                 gamestatus = gamestatus | GAME_DEVELOPER;
105
106         draw_reset();
107         draw_alpha *= menuAlpha;
108
109         vector dMouse;
110         dMouse = getmousepos();
111         dMouse *= frametime / realFrametime; // for capturevideo
112         if(dMouse != '0 0 0')
113         {
114                 dMouse = globalToBoxSize(dMouse, draw_scale);
115                 mousePos += dMouse * 1; // TODO use a cvar here
116                 mousePos_x = bound(0, mousePos_x, 1);
117                 mousePos_y = bound(0, mousePos_y, 1);
118                 if(mouseButtonsPressed)
119                         main.mouseDrag(main, mousePos);
120                 else
121                         main.mouseMove(main, mousePos);
122         }
123         main.draw(main);
124         draw_drawMousePointer(mousePos);
125 };
126
127 void() m_display =
128 {
129         Menu_Active = true;
130         setkeydest(KEY_MENU);
131         setmousetarget(MT_MENU);
132
133         main.focusEnter(main);
134 };
135
136 void() m_hide =
137 {
138         Menu_Active = false;
139         setkeydest(KEY_GAME);
140         setmousetarget(MT_CLIENT);
141
142         main.focusLeave(main);
143 };
144
145 void() m_toggle =
146 {
147         if(Menu_Active)
148                 m_hide();
149         else
150                 m_display();
151 };
152
153 void() m_shutdown =
154 {
155         m_hide();
156 };