]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud_config.qc
Merge branch 'master' into terencehill/itemstime
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud_config.qc
1 #include "hud_config.qh"
2 #include "_all.qh"
3
4 #include "hud.qh"
5
6 #include "../common/constants.qh"
7
8 #include "../dpdefs/keycodes.qh"
9
10
11 #define HUD_Write(s) fputs(fh, s)
12 // q: quoted, n: not quoted
13 #define HUD_Write_Cvar_n(cvar) HUD_Write(strcat("seta ", cvar, " ", cvar_string(cvar), "\n"))
14 #define HUD_Write_Cvar_q(cvar) HUD_Write(strcat("seta ", cvar, " \"", cvar_string(cvar), "\"\n"))
15 #define HUD_Write_PanelCvar_n(cvar_suf) HUD_Write_Cvar_n(strcat("hud_panel_", panel.panel_name, cvar_suf))
16 #define HUD_Write_PanelCvar_q(cvar_suf) HUD_Write_Cvar_q(strcat("hud_panel_", panel.panel_name, cvar_suf))
17 // Save the config
18 void HUD_Panel_ExportCfg(string cfgname)
19 {
20         float fh;
21         string filename = strcat("hud_", autocvar_hud_skin, "_", cfgname, ".cfg");
22         fh = fopen(filename, FILE_WRITE);
23         if(fh >= 0)
24         {
25                 HUD_Write_Cvar_q("hud_skin");
26                 HUD_Write_Cvar_q("hud_panel_bg");
27                 HUD_Write_Cvar_q("hud_panel_bg_color");
28                 HUD_Write_Cvar_q("hud_panel_bg_color_team");
29                 HUD_Write_Cvar_q("hud_panel_bg_alpha");
30                 HUD_Write_Cvar_q("hud_panel_bg_border");
31                 HUD_Write_Cvar_q("hud_panel_bg_padding");
32                 HUD_Write_Cvar_q("hud_panel_fg_alpha");
33                 HUD_Write("\n");
34
35                 HUD_Write_Cvar_q("hud_dock");
36                 HUD_Write_Cvar_q("hud_dock_color");
37                 HUD_Write_Cvar_q("hud_dock_color_team");
38                 HUD_Write_Cvar_q("hud_dock_alpha");
39                 HUD_Write("\n");
40
41                 HUD_Write_Cvar_q("hud_progressbar_alpha");
42                 HUD_Write_Cvar_q("hud_progressbar_strength_color");
43                 HUD_Write_Cvar_q("hud_progressbar_shield_color");
44                 HUD_Write_Cvar_q("hud_progressbar_health_color");
45                 HUD_Write_Cvar_q("hud_progressbar_armor_color");
46                 HUD_Write_Cvar_q("hud_progressbar_fuel_color");
47                 HUD_Write_Cvar_q("hud_progressbar_nexball_color");
48                 HUD_Write_Cvar_q("hud_progressbar_speed_color");
49                 HUD_Write_Cvar_q("hud_progressbar_acceleration_color");
50                 HUD_Write_Cvar_q("hud_progressbar_acceleration_neg_color");
51                 HUD_Write("\n");
52
53                 HUD_Write_Cvar_q("_hud_panelorder");
54                 HUD_Write("\n");
55
56                 HUD_Write_Cvar_q("hud_configure_grid");
57                 HUD_Write_Cvar_q("hud_configure_grid_xsize");
58                 HUD_Write_Cvar_q("hud_configure_grid_ysize");
59                 HUD_Write("\n");
60
61                 // common cvars for all panels
62                 int i;
63                 for (i = 0; i < HUD_PANEL_NUM; ++i)
64                 {
65                         panel = hud_panel[i];
66
67                         HUD_Write_PanelCvar_n("");
68                         HUD_Write_PanelCvar_q("_pos");
69                         HUD_Write_PanelCvar_q("_size");
70                         HUD_Write_PanelCvar_q("_bg");
71                         HUD_Write_PanelCvar_q("_bg_color");
72                         HUD_Write_PanelCvar_q("_bg_color_team");
73                         HUD_Write_PanelCvar_q("_bg_alpha");
74                         HUD_Write_PanelCvar_q("_bg_border");
75                         HUD_Write_PanelCvar_q("_bg_padding");
76                         switch(i) {
77                                 case HUD_PANEL_WEAPONS:
78                                         HUD_Write_PanelCvar_q("_accuracy");
79                                         HUD_Write_PanelCvar_q("_label");
80                                         HUD_Write_PanelCvar_q("_label_scale");
81                                         HUD_Write_PanelCvar_q("_complainbubble");
82                                         HUD_Write_PanelCvar_q("_complainbubble_padding");
83                                         HUD_Write_PanelCvar_q("_complainbubble_time");
84                                         HUD_Write_PanelCvar_q("_complainbubble_fadetime");
85                                         HUD_Write_PanelCvar_q("_complainbubble_color_outofammo");
86                                         HUD_Write_PanelCvar_q("_complainbubble_color_donthave");
87                                         HUD_Write_PanelCvar_q("_complainbubble_color_unavailable");
88                                         HUD_Write_PanelCvar_q("_ammo");
89                                         HUD_Write_PanelCvar_q("_ammo_color");
90                                         HUD_Write_PanelCvar_q("_ammo_alpha");
91                                         HUD_Write_PanelCvar_q("_aspect");
92                                         HUD_Write_PanelCvar_q("_timeout");
93                                         HUD_Write_PanelCvar_q("_timeout_effect");
94                                         HUD_Write_PanelCvar_q("_timeout_fadebgmin");
95                                         HUD_Write_PanelCvar_q("_timeout_fadefgmin");
96                                         HUD_Write_PanelCvar_q("_timeout_speed_in");
97                                         HUD_Write_PanelCvar_q("_timeout_speed_out");
98                                         HUD_Write_PanelCvar_q("_onlyowned");
99                                         HUD_Write_PanelCvar_q("_noncurrent_alpha");
100                                         HUD_Write_PanelCvar_q("_noncurrent_scale");
101                                         break;
102                                 case HUD_PANEL_AMMO:
103                                         HUD_Write_PanelCvar_q("_onlycurrent");
104                                         HUD_Write_PanelCvar_q("_noncurrent_alpha");
105                                         HUD_Write_PanelCvar_q("_noncurrent_scale");
106                                         HUD_Write_PanelCvar_q("_iconalign");
107                                         HUD_Write_PanelCvar_q("_progressbar");
108                                         HUD_Write_PanelCvar_q("_progressbar_name");
109                                         HUD_Write_PanelCvar_q("_progressbar_xoffset");
110                                         HUD_Write_PanelCvar_q("_text");
111                                         break;
112                                 case HUD_PANEL_POWERUPS:
113                                         HUD_Write_PanelCvar_q("_iconalign");
114                                         HUD_Write_PanelCvar_q("_baralign");
115                                         HUD_Write_PanelCvar_q("_progressbar");
116                                         HUD_Write_PanelCvar_q("_text");
117                                         break;
118                                 case HUD_PANEL_HEALTHARMOR:
119                                         HUD_Write_PanelCvar_q("_flip");
120                                         HUD_Write_PanelCvar_q("_iconalign");
121                                         HUD_Write_PanelCvar_q("_baralign");
122                                         HUD_Write_PanelCvar_q("_progressbar");
123                                         HUD_Write_PanelCvar_q("_progressbar_health");
124                                         HUD_Write_PanelCvar_q("_progressbar_armor");
125                                         HUD_Write_PanelCvar_q("_progressbar_gfx");
126                                         HUD_Write_PanelCvar_q("_progressbar_gfx_smooth");
127                                         HUD_Write_PanelCvar_q("_text");
128                                         break;
129                                 case HUD_PANEL_NOTIFY:
130                                         HUD_Write_PanelCvar_q("_flip");
131                                         HUD_Write_PanelCvar_q("_fontsize");
132                                         HUD_Write_PanelCvar_q("_time");
133                                         HUD_Write_PanelCvar_q("_fadetime");
134                                         HUD_Write_PanelCvar_q("_icon_aspect");
135                                         break;
136                                 case HUD_PANEL_TIMER:
137                                         HUD_Write_PanelCvar_q("_increment");
138                                         break;
139                                 case HUD_PANEL_RADAR:
140                                         HUD_Write_PanelCvar_q("_foreground_alpha");
141                                         HUD_Write_PanelCvar_q("_rotation");
142                                         HUD_Write_PanelCvar_q("_zoommode");
143                                         HUD_Write_PanelCvar_q("_scale");
144                                         HUD_Write_PanelCvar_q("_maximized_scale");
145                                         HUD_Write_PanelCvar_q("_maximized_size");
146                                         HUD_Write_PanelCvar_q("_maximized_rotation");
147                                         HUD_Write_PanelCvar_q("_maximized_zoommode");
148                                         break;
149                                 case HUD_PANEL_SCORE:
150                                         HUD_Write_PanelCvar_q("_rankings");
151                                         break;
152                                 case HUD_PANEL_VOTE:
153                                         HUD_Write_PanelCvar_q("_alreadyvoted_alpha");
154                                         break;
155                                 case HUD_PANEL_MODICONS:
156                                         HUD_Write_PanelCvar_q("_ca_layout");
157                                         HUD_Write_PanelCvar_q("_dom_layout");
158                                         HUD_Write_PanelCvar_q("_freezetag_layout");
159                                         break;
160                                 case HUD_PANEL_PRESSEDKEYS:
161                                         HUD_Write_PanelCvar_q("_aspect");
162                                         HUD_Write_PanelCvar_q("_attack");
163                                         break;
164                                 case HUD_PANEL_ENGINEINFO:
165                                         HUD_Write_PanelCvar_q("_framecounter_time");
166                                         HUD_Write_PanelCvar_q("_framecounter_decimals");
167                                         break;
168                                 case HUD_PANEL_INFOMESSAGES:
169                                         HUD_Write_PanelCvar_q("_flip");
170                                         break;
171                                 case HUD_PANEL_PHYSICS:
172                                         HUD_Write_PanelCvar_q("_speed_unit");
173                                         HUD_Write_PanelCvar_q("_speed_unit_show");
174                                         HUD_Write_PanelCvar_q("_speed_max");
175                                         HUD_Write_PanelCvar_q("_speed_vertical");
176                                         HUD_Write_PanelCvar_q("_topspeed");
177                                         HUD_Write_PanelCvar_q("_topspeed_time");
178                                         HUD_Write_PanelCvar_q("_acceleration_max");
179                                         HUD_Write_PanelCvar_q("_acceleration_vertical");
180                                         HUD_Write_PanelCvar_q("_flip");
181                                         HUD_Write_PanelCvar_q("_baralign");
182                                         HUD_Write_PanelCvar_q("_progressbar");
183                                         HUD_Write_PanelCvar_q("_progressbar_acceleration_mode");
184                                         HUD_Write_PanelCvar_q("_progressbar_acceleration_scale");
185                                         HUD_Write_PanelCvar_q("_progressbar_acceleration_nonlinear");
186                                         HUD_Write_PanelCvar_q("_text");
187                                         HUD_Write_PanelCvar_q("_text_scale");
188                                         break;
189                                 case HUD_PANEL_CENTERPRINT:
190                                         HUD_Write_PanelCvar_q("_align");
191                                         HUD_Write_PanelCvar_q("_flip");
192                                         HUD_Write_PanelCvar_q("_fontscale");
193                                         HUD_Write_PanelCvar_q("_time");
194                                         HUD_Write_PanelCvar_q("_fade_in");
195                                         HUD_Write_PanelCvar_q("_fade_out");
196                                         HUD_Write_PanelCvar_q("_fade_subsequent");
197                                         HUD_Write_PanelCvar_q("_fade_subsequent_passone");
198                                         HUD_Write_PanelCvar_q("_fade_subsequent_passone_minalpha");
199                                         HUD_Write_PanelCvar_q("_fade_subsequent_passtwo");
200                                         HUD_Write_PanelCvar_q("_fade_subsequent_passtwo_minalpha");
201                                         HUD_Write_PanelCvar_q("_fade_subsequent_minfontsize");
202                                         HUD_Write_PanelCvar_q("_fade_minfontsize");
203                                         break;
204                                 case HUD_PANEL_ITEMSTIME:
205                                         HUD_Write_PanelCvar_q("_iconalign");
206                                         HUD_Write_PanelCvar_q("_progressbar");
207                                         HUD_Write_PanelCvar_q("_progressbar_name");
208                                         HUD_Write_PanelCvar_q("_progressbar_reduced");
209                                         HUD_Write_PanelCvar_q("_text");
210                                         HUD_Write_PanelCvar_q("_ratio");
211                                         HUD_Write_PanelCvar_q("_dynamicsize");
212                                         break;
213                         }
214                         HUD_Write("\n");
215                 }
216                 HUD_Write("menu_sync\n"); // force the menu to reread the cvars, so that the dialogs are updated
217
218                 printf(_("^2Successfully exported to %s! (Note: It's saved in data/data/)\n"), filename);
219                 fclose(fh);
220         }
221         else
222                 printf(_("^1Couldn't write to %s\n"), filename);
223 }
224
225 void HUD_Configure_Exit_Force()
226 {
227         if (menu_enabled)
228         {
229                 menu_enabled = 0;
230                 localcmd("togglemenu\n");
231         }
232         cvar_set("_hud_configure", "0");
233 }
234
235 // check if move will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
236 vector HUD_Panel_CheckMove(vector myPos, vector mySize)
237 {
238         vector myCenter, targCenter;
239         vector myTarget = myPos;
240         int i;
241         for (i = 0; i < HUD_PANEL_NUM; ++i) {
242                 panel = hud_panel[i];
243                 if(panel == HUD_PANEL(MAPVOTE)) continue;
244                 if(panel == highlightedPanel) continue;
245                 HUD_Panel_UpdatePosSize();
246                 if(!panel_enabled) continue;
247
248                 panel_pos -= '1 1 0' * panel_bg_border;
249                 panel_size += '2 2 0' * panel_bg_border;
250
251                 if(myPos.y + mySize.y < panel_pos.y)
252                         continue;
253                 if(myPos.y > panel_pos.y + panel_size.y)
254                         continue;
255
256                 if(myPos.x + mySize.x < panel_pos.x)
257                         continue;
258                 if(myPos.x > panel_pos.x + panel_size.x)
259                         continue;
260
261                 // OK, there IS a collision.
262
263                 myCenter.x = myPos.x + 0.5 * mySize.x;
264                 myCenter.y = myPos.y + 0.5 * mySize.y;
265
266                 targCenter.x = panel_pos.x + 0.5 * panel_size.x;
267                 targCenter.y = panel_pos.y + 0.5 * panel_size.y;
268
269                 if(myCenter.x < targCenter.x && myCenter.y < targCenter.y) // top left (of the target panel)
270                 {
271                         if(myPos.x + mySize.x - panel_pos.x < myPos.y + mySize.y - panel_pos.y) // push it to the side
272                                 myTarget.x = panel_pos.x - mySize.x;
273                         else // push it upwards
274                                 myTarget.y = panel_pos.y - mySize.y;
275                 }
276                 else if(myCenter.x > targCenter.x && myCenter.y < targCenter.y) // top right
277                 {
278                         if(panel_pos.x + panel_size.x - myPos.x < myPos.y + mySize.y - panel_pos.y) // push it to the side
279                                 myTarget.x = panel_pos.x + panel_size.x;
280                         else // push it upwards
281                                 myTarget.y = panel_pos.y - mySize.y;
282                 }
283                 else if(myCenter.x < targCenter.x && myCenter.y > targCenter.y) // bottom left
284                 {
285                         if(myPos.x + mySize.x - panel_pos.x < panel_pos.y + panel_size.y - myPos.y) // push it to the side
286                                 myTarget.x = panel_pos.x - mySize.x;
287                         else // push it downwards
288                                 myTarget.y = panel_pos.y + panel_size.y;
289                 }
290                 else if(myCenter.x > targCenter.x && myCenter.y > targCenter.y) // bottom right
291                 {
292                         if(panel_pos.x + panel_size.x - myPos.x < panel_pos.y + panel_size.y - myPos.y) // push it to the side
293                                 myTarget.x = panel_pos.x + panel_size.x;
294                         else // push it downwards
295                                 myTarget.y = panel_pos.y + panel_size.y;
296                 }
297                 //if(cvar("hud_configure_checkcollisions_debug"))
298                         //drawfill(panel_pos, panel_size, '1 1 0', .3, DRAWFLAG_NORMAL);
299         }
300
301         return myTarget;
302 }
303
304 void HUD_Panel_SetPos(vector pos)
305 {
306         panel = highlightedPanel;
307         HUD_Panel_UpdatePosSize();
308         vector mySize;
309         mySize = panel_size;
310
311         //if(cvar("hud_configure_checkcollisions_debug"))
312                 //drawfill(pos, mySize, '1 1 1', .2, DRAWFLAG_NORMAL);
313
314         if(autocvar_hud_configure_grid)
315         {
316                 pos.x = floor((pos.x/vid_conwidth)/hud_configure_gridSize.x + 0.5) * hud_configure_realGridSize.x;
317                 pos.y = floor((pos.y/vid_conheight)/hud_configure_gridSize.y + 0.5) * hud_configure_realGridSize.y;
318         }
319
320         if(hud_configure_checkcollisions)
321                 pos = HUD_Panel_CheckMove(pos, mySize);
322
323         pos.x = bound(0, pos.x, vid_conwidth - mySize.x);
324         pos.y = bound(0, pos.y, vid_conheight - mySize.y);
325
326         string s;
327         s = strcat(ftos(pos.x/vid_conwidth), " ", ftos(pos.y/vid_conheight));
328
329         cvar_set(strcat("hud_panel_", highlightedPanel.panel_name, "_pos"), s);
330 }
331
332 // check if resize will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
333 vector HUD_Panel_CheckResize(vector mySize, vector resizeorigin) {
334         vector targEndPos;
335         vector dist;
336         float ratio = mySize.x/mySize.y;
337         int i;
338         for (i = 0; i < HUD_PANEL_NUM; ++i) {
339                 panel = hud_panel[i];
340                 if(panel == HUD_PANEL(MAPVOTE)) continue;
341                 if(panel == highlightedPanel) continue;
342                 HUD_Panel_UpdatePosSize();
343                 if(!panel_enabled) continue;
344
345                 panel_pos -= '1 1 0' * panel_bg_border;
346                 panel_size += '2 2 0' * panel_bg_border;
347
348                 targEndPos = panel_pos + panel_size;
349
350                 // resizeorigin is WITHIN target panel, just abort any collision testing against that particular panel to produce expected behaviour!
351                 if(resizeorigin.x > panel_pos.x && resizeorigin.x < targEndPos.x && resizeorigin.y > panel_pos.y && resizeorigin.y < targEndPos.y)
352                         continue;
353
354                 if (resizeCorner == 1)
355                 {
356                         // check if this panel is on our way
357                         if (resizeorigin.x <= panel_pos.x)
358                                 continue;
359                         if (resizeorigin.y <= panel_pos.y)
360                                 continue;
361                         if (targEndPos.x <= resizeorigin.x - mySize.x)
362                                 continue;
363                         if (targEndPos.y <= resizeorigin.y - mySize.y)
364                                 continue;
365
366                         // there is a collision:
367                         // detect which side of the panel we are facing is actually limiting the resizing
368                         // (which side the resize direction finds for first) and reduce the size up to there
369                         //
370                         // dist is the distance between resizeorigin and the "analogous" point of the panel
371                         // in this case between resizeorigin (bottom-right point) and the bottom-right point of the panel
372                         dist.x = resizeorigin.x - targEndPos.x;
373                         dist.y = resizeorigin.y - targEndPos.y;
374                         if (dist.y <= 0 || dist.x / dist.y > ratio)
375                                 mySize.x = min(mySize.x, dist.x);
376                         else
377                                 mySize.y = min(mySize.y, dist.y);
378                 }
379                 else if (resizeCorner == 2)
380                 {
381                         if (resizeorigin.x >= targEndPos.x)
382                                 continue;
383                         if (resizeorigin.y <= panel_pos.y)
384                                 continue;
385                         if (panel_pos.x >= resizeorigin.x + mySize.x)
386                                 continue;
387                         if (targEndPos.y <= resizeorigin.y - mySize.y)
388                                 continue;
389
390                         dist.x = panel_pos.x - resizeorigin.x;
391                         dist.y = resizeorigin.y - targEndPos.y;
392                         if (dist.y <= 0 || dist.x / dist.y > ratio)
393                                 mySize.x = min(mySize.x, dist.x);
394                         else
395                                 mySize.y = min(mySize.y, dist.y);
396                 }
397                 else if (resizeCorner == 3)
398                 {
399                         if (resizeorigin.x <= panel_pos.x)
400                                 continue;
401                         if (resizeorigin.y >= targEndPos.y)
402                                 continue;
403                         if (targEndPos.x <= resizeorigin.x - mySize.x)
404                                 continue;
405                         if (panel_pos.y >= resizeorigin.y + mySize.y)
406                                 continue;
407
408                         dist.x = resizeorigin.x - targEndPos.x;
409                         dist.y = panel_pos.y - resizeorigin.y;
410                         if (dist.y <= 0 || dist.x / dist.y > ratio)
411                                 mySize.x = min(mySize.x, dist.x);
412                         else
413                                 mySize.y = min(mySize.y, dist.y);
414                 }
415                 else if (resizeCorner == 4)
416                 {
417                         if (resizeorigin.x >= targEndPos.x)
418                                 continue;
419                         if (resizeorigin.y >= targEndPos.y)
420                                 continue;
421                         if (panel_pos.x >= resizeorigin.x + mySize.x)
422                                 continue;
423                         if (panel_pos.y >= resizeorigin.y + mySize.y)
424                                 continue;
425
426                         dist.x = panel_pos.x - resizeorigin.x;
427                         dist.y = panel_pos.y - resizeorigin.y;
428                         if (dist.y <= 0 || dist.x / dist.y > ratio)
429                                 mySize.x = min(mySize.x, dist.x);
430                         else
431                                 mySize.y = min(mySize.y, dist.y);
432                 }
433                 //if(cvar("hud_configure_checkcollisions_debug"))
434                         //drawfill(panel_pos, panel_size, '1 1 0', .3, DRAWFLAG_NORMAL);
435         }
436
437         return mySize;
438 }
439
440 void HUD_Panel_SetPosSize(vector mySize)
441 {
442         panel = highlightedPanel;
443         HUD_Panel_UpdatePosSize();
444         vector resizeorigin = panel_click_resizeorigin;
445         vector myPos;
446
447         // minimum panel size cap
448         mySize.x = max(0.025 * vid_conwidth, mySize.x);
449         mySize.y = max(0.025 * vid_conheight, mySize.y);
450
451         if(highlightedPanel == HUD_PANEL(CHAT)) // some panels have their own restrictions, like the chat panel (which actually only moves the engine chat print around). Looks bad if it's too small.
452         {
453                 mySize.x = max(17 * autocvar_con_chatsize, mySize.x);
454                 mySize.y = max(2 * autocvar_con_chatsize + 2 * panel_bg_padding, mySize.y);
455         }
456
457         // collision testing|
458         // -----------------+
459
460         // we need to know pos at this stage, but it might still change later if we hit a screen edge/other panel (?)
461         if(resizeCorner == 1) {
462                 myPos.x = resizeorigin.x - mySize.x;
463                 myPos.y = resizeorigin.y - mySize.y;
464         } else if(resizeCorner == 2) {
465                 myPos.x = resizeorigin.x;
466                 myPos.y = resizeorigin.y - mySize.y;
467         } else if(resizeCorner == 3) {
468                 myPos.x = resizeorigin.x - mySize.x;
469                 myPos.y = resizeorigin.y;
470         } else { // resizeCorner == 4
471                 myPos.x = resizeorigin.x;
472                 myPos.y = resizeorigin.y;
473         }
474
475         // left/top screen edges
476         if(myPos.x < 0)
477                 mySize.x = mySize.x + myPos.x;
478         if(myPos.y < 0)
479                 mySize.y = mySize.y + myPos.y;
480
481         // bottom/right screen edges
482         if(myPos.x + mySize.x > vid_conwidth)
483                 mySize.x = vid_conwidth - myPos.x;
484         if(myPos.y + mySize.y > vid_conheight)
485                 mySize.y = vid_conheight - myPos.y;
486
487         //if(cvar("hud_configure_checkcollisions_debug"))
488                 //drawfill(myPos, mySize, '1 1 1', .2, DRAWFLAG_NORMAL);
489
490         // before checkresize, otherwise panel can be snapped partially inside another panel or panel aspect ratio can be broken
491         if(autocvar_hud_configure_grid)
492         {
493                 mySize.x = floor((mySize.x/vid_conwidth)/hud_configure_gridSize.x + 0.5) * hud_configure_realGridSize.x;
494                 mySize.y = floor((mySize.y/vid_conheight)/hud_configure_gridSize.y + 0.5) * hud_configure_realGridSize.y;
495         }
496
497         if(hud_configure_checkcollisions)
498                 mySize = HUD_Panel_CheckResize(mySize, resizeorigin);
499
500         // minimum panel size cap, do this once more so we NEVER EVER EVER have a panel smaller than this, JUST IN CASE above code still makes the panel eg negative (impossible to resize back without changing cvars manually then)
501         mySize.x = max(0.025 * vid_conwidth, mySize.x);
502         mySize.y = max(0.025 * vid_conheight, mySize.y);
503
504         // do another pos check, as size might have changed by now
505         if(resizeCorner == 1) {
506                 myPos.x = resizeorigin.x - mySize.x;
507                 myPos.y = resizeorigin.y - mySize.y;
508         } else if(resizeCorner == 2) {
509                 myPos.x = resizeorigin.x;
510                 myPos.y = resizeorigin.y - mySize.y;
511         } else if(resizeCorner == 3) {
512                 myPos.x = resizeorigin.x - mySize.x;
513                 myPos.y = resizeorigin.y;
514         } else { // resizeCorner == 4
515                 myPos.x = resizeorigin.x;
516                 myPos.y = resizeorigin.y;
517         }
518
519         //if(cvar("hud_configure_checkcollisions_debug"))
520                 //drawfill(myPos, mySize, '0 1 0', .3, DRAWFLAG_NORMAL);
521
522         string s;
523         s = strcat(ftos(mySize.x/vid_conwidth), " ", ftos(mySize.y/vid_conheight));
524         cvar_set(strcat("hud_panel_", highlightedPanel.panel_name, "_size"), s);
525
526         s = strcat(ftos(myPos.x/vid_conwidth), " ", ftos(myPos.y/vid_conheight));
527         cvar_set(strcat("hud_panel_", highlightedPanel.panel_name, "_pos"), s);
528 }
529
530 float pressed_key_time;
531 vector highlightedPanel_initial_pos, highlightedPanel_initial_size;
532 void HUD_Panel_Arrow_Action(float nPrimary)
533 {
534         if(!highlightedPanel)
535                 return;
536
537         hud_configure_checkcollisions = (!(hudShiftState & S_CTRL) && autocvar_hud_configure_checkcollisions);
538
539         float step;
540         if(autocvar_hud_configure_grid)
541         {
542                 if (nPrimary == K_UPARROW || nPrimary == K_DOWNARROW)
543                 {
544                         if (hudShiftState & S_SHIFT)
545                                 step = hud_configure_realGridSize.y;
546                         else
547                                 step = 2 * hud_configure_realGridSize.y;
548                 }
549                 else
550                 {
551                         if (hudShiftState & S_SHIFT)
552                                 step = hud_configure_realGridSize.x;
553                         else
554                                 step = 2 * hud_configure_realGridSize.x;
555                 }
556         }
557         else
558         {
559                 if (nPrimary == K_UPARROW || nPrimary == K_DOWNARROW)
560                         step = vid_conheight;
561                 else
562                         step = vid_conwidth;
563                 if (hudShiftState & S_SHIFT)
564                         step = (step / 256); // more precision
565                 else
566                         step = (step / 64) * (1 + 2 * (time - pressed_key_time));
567         }
568
569         panel = highlightedPanel;
570         HUD_Panel_UpdatePosSize();
571
572         highlightedPanel_initial_pos = panel_pos;
573         highlightedPanel_initial_size = panel_size;
574
575         if (hudShiftState & S_ALT) // resize
576         {
577                 if(nPrimary == K_UPARROW)
578                         resizeCorner = 1;
579                 else if(nPrimary == K_RIGHTARROW)
580                         resizeCorner = 2;
581                 else if(nPrimary == K_LEFTARROW)
582                         resizeCorner = 3;
583                 else // if(nPrimary == K_DOWNARROW)
584                         resizeCorner = 4;
585
586                 // ctrl+arrow reduces the size, instead of increasing it
587                 // Note that ctrl disables collisions check too, but it's fine
588                 // since we don't collide with anything reducing the size
589                 if (hudShiftState & S_CTRL) {
590                         step = -step;
591                         resizeCorner = 5 - resizeCorner;
592                 }
593
594                 vector mySize;
595                 mySize = panel_size;
596                 panel_click_resizeorigin = panel_pos;
597                 if(resizeCorner == 1) {
598                         panel_click_resizeorigin += mySize;
599                         mySize.y += step;
600                 } else if(resizeCorner == 2) {
601                         panel_click_resizeorigin.y += mySize.y;
602                         mySize.x += step;
603                 } else if(resizeCorner == 3) {
604                         panel_click_resizeorigin.x += mySize.x;
605                         mySize.x += step;
606                 } else { // resizeCorner == 4
607                         mySize.y += step;
608                 }
609                 HUD_Panel_SetPosSize(mySize);
610         }
611         else // move
612         {
613                 vector pos;
614                 pos = panel_pos;
615                 if(nPrimary == K_UPARROW)
616                         pos.y -= step;
617                 else if(nPrimary == K_DOWNARROW)
618                         pos.y += step;
619                 else if(nPrimary == K_LEFTARROW)
620                         pos.x -= step;
621                 else // if(nPrimary == K_RIGHTARROW)
622                         pos.x += step;
623
624                 HUD_Panel_SetPos(pos);
625         }
626
627         panel = highlightedPanel;
628         HUD_Panel_UpdatePosSize();
629
630         if (highlightedPanel_initial_pos != panel_pos || highlightedPanel_initial_size != panel_size)
631         {
632                 // backup!
633                 panel_pos_backup = highlightedPanel_initial_pos;
634                 panel_size_backup = highlightedPanel_initial_size;
635                 highlightedPanel_backup = highlightedPanel;
636         }
637 }
638
639 const int S_MOUSE1 = 1;
640 const int S_MOUSE2 = 2;
641 const int S_MOUSE3 = 4;
642 int mouseClicked;
643 int prevMouseClicked; // previous state
644 float prevMouseClickedTime; // time during previous left mouse click, to check for doubleclicks
645 vector prevMouseClickedPos; // pos during previous left mouse click, to check for doubleclicks
646
647 void HUD_Panel_EnableMenu();
648 entity tab_panels[HUD_PANEL_MAX];
649 entity tab_panel;
650 vector tab_panel_pos;
651 float tab_backward;
652 void HUD_Panel_FirstInDrawQ(float id);
653 void reset_tab_panels()
654 {
655         int i;
656         for(i = 0; i < HUD_PANEL_NUM; ++i)
657                 tab_panels[i] = world;
658 }
659 float HUD_Panel_InputEvent(float bInputType, float nPrimary, float nSecondary)
660 {
661         string s;
662
663         if(bInputType == 2)
664                 return false;
665
666         if(!autocvar__hud_configure)
667                 return false;
668
669         if(bInputType == 3)
670         {
671                 mousepos.x = nPrimary;
672                 mousepos.y = nSecondary;
673                 return true;
674         }
675
676         // block any input while a menu dialog is fading
677         // don't block mousepos read as it leads to cursor jumps in the interaction with the menu
678         if(autocvar__menu_alpha)
679         {
680                 hudShiftState = 0;
681                 mouseClicked = 0;
682                 return true;
683         }
684
685         // allow console bind to work
686         string con_keys;
687         float keys;
688         con_keys = findkeysforcommand("toggleconsole", 0);
689         keys = tokenize(con_keys); // findkeysforcommand returns data for this
690
691         bool hit_con_bind = false;
692         int i;
693         for (i = 0; i < keys; ++i)
694         {
695                 if(nPrimary == stof(argv(i)))
696                         hit_con_bind = true;
697         }
698
699         if(bInputType == 0) {
700                 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
701                 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
702                 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
703         }
704         else if(bInputType == 1) {
705                 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
706                 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
707                 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
708         }
709
710         if(nPrimary == K_CTRL)
711         {
712                 if (bInputType == 1) //ctrl has been released
713                 {
714                         if (tab_panel)
715                         {
716                                 //switch to selected panel
717                                 highlightedPanel = tab_panel;
718                                 highlightedAction = 0;
719                                 HUD_Panel_FirstInDrawQ(highlightedPanel.panel_id);
720                         }
721                         tab_panel = world;
722                         reset_tab_panels();
723                 }
724         }
725
726         if(nPrimary == K_MOUSE1)
727         {
728                 if(bInputType == 0) // key pressed
729                         mouseClicked |= S_MOUSE1;
730                 else if(bInputType == 1) // key released
731                         mouseClicked -= (mouseClicked & S_MOUSE1);
732         }
733         else if(nPrimary == K_MOUSE2)
734         {
735                 if(bInputType == 0) // key pressed
736                         mouseClicked |= S_MOUSE2;
737                 else if(bInputType == 1) // key released
738                         mouseClicked -= (mouseClicked & S_MOUSE2);
739         }
740         else if(nPrimary == K_ESCAPE)
741         {
742                 if (bInputType == 1)
743                         return true;
744                 menu_enabled = 1;
745                 localcmd("menu_showhudexit\n");
746         }
747         else if(nPrimary == K_BACKSPACE && hudShiftState & S_CTRL)
748         {
749                 if (bInputType == 1)
750                         return true;
751                 if (!menu_enabled)
752                         cvar_set("_hud_configure", "0");
753         }
754         else if(nPrimary == K_TAB && hudShiftState & S_CTRL) // switch panel
755         {
756                 if (bInputType == 1 || mouseClicked)
757                         return true;
758
759                 // FIXME minor bug: if a panel is highlighted, has the same pos_x and
760                 // lays in the same level of another panel then the next consecutive
761                 // CTRL TAB presses will reselect once more the highlighted panel
762
763                 entity starting_panel;
764                 entity old_tab_panel = tab_panel;
765                 if (!tab_panel) //first press of TAB
766                 {
767                         if (highlightedPanel)
768                         {
769                                 panel = highlightedPanel;
770                                 HUD_Panel_UpdatePosSize();
771                         }
772                         else
773                                 panel_pos = '0 0 0';
774                         starting_panel = highlightedPanel;
775                         tab_panel_pos = panel_pos; //to compute level
776                 }
777                 else
778                 {
779                         if ( ((!tab_backward) && (hudShiftState & S_SHIFT)) || (tab_backward && !(hudShiftState & S_SHIFT)) ) //tab direction changed?
780                                 reset_tab_panels();
781                         starting_panel = tab_panel;
782                 }
783                 tab_backward = (hudShiftState & S_SHIFT);
784
785                 float k, level = 0, start_posX;
786                 vector candidate_pos = '0 0 0';
787                 const float LEVELS_NUM = 4;
788                 float level_height = vid_conheight / LEVELS_NUM;
789 :find_tab_panel
790                 level = floor(tab_panel_pos.y / level_height) * level_height; //starting level
791                 candidate_pos.x = (!tab_backward) ? vid_conwidth : 0;
792                 start_posX = tab_panel_pos.x;
793                 tab_panel = world;
794                 k=0;
795                 while(++k)
796                 {
797                         for(i = 0; i < HUD_PANEL_NUM; ++i)
798                         {
799                                 panel = hud_panel[i];
800                                 if(panel == HUD_PANEL(MAPVOTE))
801                                         continue;
802                                 if (panel == tab_panels[i] || panel == starting_panel)
803                                         continue;
804                                 HUD_Panel_UpdatePosSize();
805                                 if (panel_pos.y >= level && (panel_pos.y - level) < level_height)
806                                 if (  ( !tab_backward && panel_pos.x >= start_posX && (panel_pos.x < candidate_pos.x || (panel_pos.x == candidate_pos.x && panel_pos.y <= candidate_pos.y)) )
807                                         || ( tab_backward && panel_pos.x <= start_posX && (panel_pos.x > candidate_pos.x || (panel_pos.x == candidate_pos.x && panel_pos.y >= candidate_pos.y)) )  )
808                                 {
809                                         tab_panel = panel;
810                                         tab_panel_pos = candidate_pos = panel_pos;
811                                 }
812                         }
813                         if (tab_panel)
814                                 break;
815                         if (k == LEVELS_NUM) //tab_panel not found
816                         {
817                                 reset_tab_panels();
818                                 if (!old_tab_panel)
819                                 {
820                                         tab_panel = world;
821                                         return true;
822                                 }
823                                 starting_panel = old_tab_panel;
824                                 old_tab_panel = world;
825                                 goto find_tab_panel; //u must find tab_panel!
826                         }
827                         if (!tab_backward)
828                         {
829                                 level = (level + level_height) % vid_conheight;
830                                 start_posX = 0;
831                                 candidate_pos.x = vid_conwidth;
832                         }
833                         else
834                         {
835                                 level = (level - level_height) % vid_conheight;
836                                 start_posX = vid_conwidth;
837                                 candidate_pos.x = 0;
838                         }
839                 }
840
841                 tab_panels[tab_panel.panel_id] = tab_panel;
842         }
843         else if(nPrimary == K_SPACE && hudShiftState & S_CTRL) // enable/disable highlighted panel or dock
844         {
845                 if (bInputType == 1 || mouseClicked)
846                         return true;
847
848                 if (highlightedPanel)
849                         cvar_set(strcat("hud_panel_", highlightedPanel.panel_name), ftos(!cvar(strcat("hud_panel_", highlightedPanel.panel_name))));
850                 else
851                         cvar_set(strcat("hud_dock"), (autocvar_hud_dock == "") ? "dock" : "");
852         }
853         else if(nPrimary == 'c' && hudShiftState & S_CTRL) // copy highlighted panel size
854         {
855                 if (bInputType == 1 || mouseClicked)
856                         return true;
857
858                 if (highlightedPanel)
859                 {
860                         panel = highlightedPanel;
861                         HUD_Panel_UpdatePosSize();
862                         panel_size_copied = panel_size;
863                 }
864         }
865         else if(nPrimary == 'v' && hudShiftState & S_CTRL) // past copied size on the highlighted panel
866         {
867                 if (bInputType == 1 || mouseClicked)
868                         return true;
869
870                 if (panel_size_copied == '0 0 0' || !highlightedPanel)
871                         return true;
872
873                 panel = highlightedPanel;
874                 HUD_Panel_UpdatePosSize();
875
876                 // reduce size if it'd go beyond screen boundaries
877                 vector tmp_size = panel_size_copied;
878                 if (panel_pos.x + panel_size_copied.x > vid_conwidth)
879                         tmp_size.x = vid_conwidth - panel_pos.x;
880                 if (panel_pos.y + panel_size_copied.y > vid_conheight)
881                         tmp_size.y = vid_conheight - panel_pos.y;
882
883                 if (panel_size == tmp_size)
884                         return true;
885
886                 // backup first!
887                 panel_pos_backup = panel_pos;
888                 panel_size_backup = panel_size;
889                 highlightedPanel_backup = highlightedPanel;
890
891                 s = strcat(ftos(tmp_size.x/vid_conwidth), " ", ftos(tmp_size.y/vid_conheight));
892                 cvar_set(strcat("hud_panel_", highlightedPanel.panel_name, "_size"), s);
893         }
894         else if(nPrimary == 'z' && hudShiftState & S_CTRL) // undo last action
895         {
896                 if (bInputType == 1 || mouseClicked)
897                         return true;
898                 //restore previous values
899                 if (highlightedPanel_backup)
900                 {
901                         s = strcat(ftos(panel_pos_backup.x/vid_conwidth), " ", ftos(panel_pos_backup.y/vid_conheight));
902                         cvar_set(strcat("hud_panel_", highlightedPanel_backup.panel_name, "_pos"), s);
903                         s = strcat(ftos(panel_size_backup.x/vid_conwidth), " ", ftos(panel_size_backup.y/vid_conheight));
904                         cvar_set(strcat("hud_panel_", highlightedPanel_backup.panel_name, "_size"), s);
905                         highlightedPanel_backup = world;
906                 }
907         }
908         else if(nPrimary == 's' && hudShiftState & S_CTRL) // save config
909         {
910                 if (bInputType == 1 || mouseClicked)
911                         return true;
912                 localcmd("hud save myconfig\n");
913         }
914         else if(nPrimary == K_UPARROW || nPrimary == K_DOWNARROW || nPrimary == K_LEFTARROW || nPrimary == K_RIGHTARROW)
915         {
916                 if (bInputType == 1)
917                 {
918                         pressed_key_time = 0;
919                         return true;
920                 }
921                 else if (pressed_key_time == 0)
922                         pressed_key_time = time;
923
924                 if (!mouseClicked)
925                         HUD_Panel_Arrow_Action(nPrimary); //move or resize panel
926         }
927         else if(nPrimary == K_ENTER || nPrimary == K_SPACE || nPrimary == K_KP_ENTER)
928         {
929                 if (bInputType == 1)
930                         return true;
931                 if (highlightedPanel)
932                         HUD_Panel_EnableMenu();
933         }
934         else if(hit_con_bind || nPrimary == K_PAUSE)
935                 return false;
936
937         return true;
938 }
939
940 float HUD_Panel_Check_Mouse_Pos(float allow_move)
941 {
942         int i, j = 0;
943         while(j < HUD_PANEL_NUM)
944         {
945                 i = panel_order[j];
946                 j += 1;
947
948                 panel = hud_panel[i];
949                 if(panel == HUD_PANEL(MAPVOTE)) continue;
950                 HUD_Panel_UpdatePosSize();
951
952                 float border = max(8, panel_bg_border); // FORCED border so a small border size doesn't mean you can't resize
953
954                 // move
955                 if(allow_move && mousepos.x > panel_pos.x && mousepos.y > panel_pos.y && mousepos.x < panel_pos.x + panel_size.x && mousepos.y < panel_pos.y + panel_size.y)
956                 {
957                         return 1;
958                 }
959                 // resize from topleft border
960                 else if(mousepos.x >= panel_pos.x - border && mousepos.y >= panel_pos.y - border && mousepos.x <= panel_pos.x + 0.5 * panel_size.x && mousepos.y <= panel_pos.y + 0.5 * panel_size.y)
961                 {
962                         return 2;
963                 }
964                 // resize from topright border
965                 else if(mousepos.x >= panel_pos.x + 0.5 * panel_size.x && mousepos.y >= panel_pos.y - border && mousepos.x <= panel_pos.x + panel_size.x + border && mousepos.y <= panel_pos.y + 0.5 * panel_size.y)
966                 {
967                         return 3;
968                 }
969                 // resize from bottomleft border
970                 else if(mousepos.x >= panel_pos.x - border && mousepos.y >= panel_pos.y + 0.5 * panel_size.y && mousepos.x <= panel_pos.x + 0.5 * panel_size.x && mousepos.y <= panel_pos.y + panel_size.y + border)
971                 {
972                         return 3;
973                 }
974                 // resize from bottomright border
975                 else if(mousepos.x >= panel_pos.x + 0.5 * panel_size.x && mousepos.y >= panel_pos.y + 0.5 * panel_size.y && mousepos.x <= panel_pos.x + panel_size.x + border && mousepos.y <= panel_pos.y + panel_size.y + border)
976                 {
977                         return 2;
978                 }
979         }
980         return 0;
981 }
982
983 // move a panel to the beginning of the panel order array (which means it gets drawn last, on top of everything else)
984 void HUD_Panel_FirstInDrawQ(float id)
985 {
986         int i;
987         int place = -1;
988         // find out where in the array our current id is, save into place
989         for(i = 0; i < HUD_PANEL_NUM; ++i)
990         {
991                 if(panel_order[i] == id)
992                 {
993                         place = i;
994                         break;
995                 }
996         }
997         // place last if we didn't find a place for it yet (probably new panel, or screwed up cvar)
998         if(place == -1)
999                 place = HUD_PANEL_NUM - 1;
1000
1001         // move all ids up by one step in the array until "place"
1002         for(i = place; i > 0; --i)
1003         {
1004                 panel_order[i] = panel_order[i-1];
1005         }
1006         // now save the new top id
1007         panel_order[0] = id;
1008
1009         // let's save them into the cvar by some strcat trickery
1010         string s = "";
1011         for(i = 0; i < HUD_PANEL_NUM; ++i)
1012         {
1013                 s = strcat(s, ftos(panel_order[i]), " ");
1014         }
1015         cvar_set("_hud_panelorder", s);
1016         if(hud_panelorder_prev)
1017                 strunzone(hud_panelorder_prev);
1018         hud_panelorder_prev = strzone(autocvar__hud_panelorder); // prevent HUD_Main from doing useless update, we already updated here
1019 }
1020
1021 void HUD_Panel_Highlight(float allow_move)
1022 {
1023         int i, j = 0;
1024
1025         while(j < HUD_PANEL_NUM)
1026         {
1027                 i = panel_order[j];
1028                 j += 1;
1029
1030                 panel = hud_panel[i];
1031                 if(panel == HUD_PANEL(MAPVOTE))
1032                         continue;
1033                 HUD_Panel_UpdatePosSize();
1034
1035                 float border = max(8, panel_bg_border); // FORCED border so a small border size doesn't mean you can't resize
1036
1037                 // move
1038                 if(allow_move && mousepos.x > panel_pos.x && mousepos.y > panel_pos.y && mousepos.x < panel_pos.x + panel_size.x && mousepos.y < panel_pos.y + panel_size.y)
1039                 {
1040                         highlightedPanel = hud_panel[i];
1041                         HUD_Panel_FirstInDrawQ(i);
1042                         highlightedAction = 1;
1043                         panel_click_distance = mousepos - panel_pos;
1044                         return;
1045                 }
1046                 // resize from topleft border
1047                 else if(mousepos.x >= panel_pos.x - border && mousepos.y >= panel_pos.y - border && mousepos.x <= panel_pos.x + 0.5 * panel_size.x && mousepos.y <= panel_pos.y + 0.5 * panel_size.y)
1048                 {
1049                         highlightedPanel = hud_panel[i];
1050                         HUD_Panel_FirstInDrawQ(i);
1051                         highlightedAction = 2;
1052                         resizeCorner = 1;
1053                         panel_click_distance = mousepos - panel_pos;
1054                         panel_click_resizeorigin = panel_pos + panel_size;
1055                         return;
1056                 }
1057                 // resize from topright border
1058                 else if(mousepos.x >= panel_pos.x + 0.5 * panel_size.x && mousepos.y >= panel_pos.y - border && mousepos.x <= panel_pos.x + panel_size.x + border && mousepos.y <= panel_pos.y + 0.5 * panel_size.y)
1059                 {
1060                         highlightedPanel = hud_panel[i];
1061                         HUD_Panel_FirstInDrawQ(i);
1062                         highlightedAction = 2;
1063                         resizeCorner = 2;
1064                         panel_click_distance.x = panel_size.x - mousepos.x + panel_pos.x;
1065                         panel_click_distance.y = mousepos.y - panel_pos.y;
1066                         panel_click_resizeorigin = panel_pos + eY * panel_size.y;
1067                         return;
1068                 }
1069                 // resize from bottomleft border
1070                 else if(mousepos.x >= panel_pos.x - border && mousepos.y >= panel_pos.y + 0.5 * panel_size.y && mousepos.x <= panel_pos.x + 0.5 * panel_size.x && mousepos.y <= panel_pos.y + panel_size.y + border)
1071                 {
1072                         highlightedPanel = hud_panel[i];
1073                         HUD_Panel_FirstInDrawQ(i);
1074                         highlightedAction = 2;
1075                         resizeCorner = 3;
1076                         panel_click_distance.x = mousepos.x - panel_pos.x;
1077                         panel_click_distance.y = panel_size.y - mousepos.y + panel_pos.y;
1078                         panel_click_resizeorigin = panel_pos + eX * panel_size.x;
1079                         return;
1080                 }
1081                 // resize from bottomright border
1082                 else if(mousepos.x >= panel_pos.x + 0.5 * panel_size.x && mousepos.y >= panel_pos.y + 0.5 * panel_size.y && mousepos.x <= panel_pos.x + panel_size.x + border && mousepos.y <= panel_pos.y + panel_size.y + border)
1083                 {
1084                         highlightedPanel = hud_panel[i];
1085                         HUD_Panel_FirstInDrawQ(i);
1086                         highlightedAction = 2;
1087                         resizeCorner = 4;
1088                         panel_click_distance = panel_size - mousepos + panel_pos;
1089                         panel_click_resizeorigin = panel_pos;
1090                         return;
1091                 }
1092         }
1093         highlightedPanel = world;
1094         highlightedAction = 0;
1095 }
1096
1097 void HUD_Panel_EnableMenu()
1098 {
1099         menu_enabled = 2;
1100         localcmd("menu_showhudoptions ", highlightedPanel.panel_name, "\n");
1101 }
1102 float mouse_over_panel;
1103 void HUD_Panel_Mouse()
1104 {
1105         if(autocvar__menu_alpha == 1)
1106                 return;
1107
1108         if (!autocvar_hud_cursormode)
1109         {
1110                 mousepos = mousepos + getmousepos() * autocvar_menu_mouse_speed;
1111
1112                 mousepos.x = bound(0, mousepos.x, vid_conwidth);
1113                 mousepos.y = bound(0, mousepos.y, vid_conheight);
1114         }
1115
1116         if(mouseClicked)
1117         {
1118                 if(prevMouseClicked == 0)
1119                 {
1120                         if (tab_panel)
1121                         {
1122                                 //stop ctrl-tab selection
1123                                 tab_panel = world;
1124                                 reset_tab_panels();
1125                         }
1126                         HUD_Panel_Highlight(mouseClicked & S_MOUSE1); // sets highlightedPanel, highlightedAction, panel_click_distance, panel_click_resizeorigin
1127                                                                         // and calls HUD_Panel_UpdatePosSize() for the highlighted panel
1128                         if (highlightedPanel)
1129                         {
1130                                 highlightedPanel_initial_pos = panel_pos;
1131                                 highlightedPanel_initial_size = panel_size;
1132                         }
1133                         // doubleclick check
1134                         if ((mouseClicked & S_MOUSE1) && time - prevMouseClickedTime < 0.4 && highlightedPanel && prevMouseClickedPos == mousepos)
1135                         {
1136                                 mouseClicked = 0; // to prevent spam, I guess.
1137                                 HUD_Panel_EnableMenu();
1138                         }
1139                         else
1140                         {
1141                                 if (mouseClicked & S_MOUSE1)
1142                                 {
1143                                         prevMouseClickedTime = time;
1144                                         prevMouseClickedPos = mousepos;
1145                                 }
1146                                 mouse_over_panel = HUD_Panel_Check_Mouse_Pos(mouseClicked & S_MOUSE1);
1147                         }
1148                 }
1149                 else
1150                 {
1151                         panel = highlightedPanel;
1152                         HUD_Panel_UpdatePosSize();
1153                 }
1154
1155                 if (highlightedPanel)
1156                 {
1157                         drawfill(panel_pos - '1 1 0' * panel_bg_border, panel_size + '2 2 0' * panel_bg_border, '1 1 1', .1, DRAWFLAG_NORMAL);
1158                         if (highlightedPanel_initial_pos != panel_pos || highlightedPanel_initial_size != panel_size)
1159                         {
1160                                 hud_configure_checkcollisions = (!(hudShiftState & S_CTRL) && autocvar_hud_configure_checkcollisions);
1161                                 // backup!
1162                                 panel_pos_backup = highlightedPanel_initial_pos;
1163                                 panel_size_backup = highlightedPanel_initial_size;
1164                                 highlightedPanel_backup = highlightedPanel;
1165                         }
1166                         else
1167                                 // in case the clicked panel is inside another panel and we aren't
1168                                 // moving it, avoid the immediate "fix" of its position/size
1169                                 // (often unwanted and hateful) by disabling collisions check
1170                                 hud_configure_checkcollisions = false;
1171                 }
1172
1173                 if(highlightedAction == 1)
1174                         HUD_Panel_SetPos(mousepos - panel_click_distance);
1175                 else if(highlightedAction == 2)
1176                 {
1177                         vector mySize = '0 0 0';
1178                         if(resizeCorner == 1) {
1179                                 mySize.x = panel_click_resizeorigin.x - (mousepos.x - panel_click_distance.x);
1180                                 mySize.y = panel_click_resizeorigin.y - (mousepos.y - panel_click_distance.y);
1181                         } else if(resizeCorner == 2) {
1182                                 mySize.x = mousepos.x + panel_click_distance.x - panel_click_resizeorigin.x;
1183                                 mySize.y = panel_click_distance.y + panel_click_resizeorigin.y - mousepos.y;
1184                         } else if(resizeCorner == 3) {
1185                                 mySize.x = panel_click_resizeorigin.x + panel_click_distance.x - mousepos.x;
1186                                 mySize.y = mousepos.y + panel_click_distance.y - panel_click_resizeorigin.y;
1187                         } else { // resizeCorner == 4
1188                                 mySize.x = mousepos.x - (panel_click_resizeorigin.x - panel_click_distance.x);
1189                                 mySize.y = mousepos.y - (panel_click_resizeorigin.y - panel_click_distance.y);
1190                         }
1191                         HUD_Panel_SetPosSize(mySize);
1192                 }
1193         }
1194         else
1195         {
1196                 if(prevMouseClicked)
1197                         highlightedAction = 0;
1198                 if(menu_enabled == 2)
1199                         mouse_over_panel = 0;
1200                 else
1201                         mouse_over_panel = HUD_Panel_Check_Mouse_Pos(true);
1202                 if (mouse_over_panel && !tab_panel)
1203                         drawfill(panel_pos - '1 1 0' * panel_bg_border, panel_size + '2 2 0' * panel_bg_border, '1 1 1', .1, DRAWFLAG_NORMAL);
1204         }
1205         // draw cursor after performing move/resize to have the panel pos/size updated before mouse_over_panel
1206         const vector cursorsize = '32 32 0';
1207         float cursor_alpha = 1 - autocvar__menu_alpha;
1208
1209         if(!mouse_over_panel)
1210                 drawpic(mousepos, strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"), cursorsize, '1 1 1', cursor_alpha, DRAWFLAG_NORMAL);
1211         else if(mouse_over_panel == 1)
1212                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", autocvar_menu_skin, "/cursor_move.tga"), cursorsize, '1 1 1', cursor_alpha, DRAWFLAG_NORMAL);
1213         else if(mouse_over_panel == 2)
1214                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", autocvar_menu_skin, "/cursor_resize.tga"), cursorsize, '1 1 1', cursor_alpha, DRAWFLAG_NORMAL);
1215         else
1216                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", autocvar_menu_skin, "/cursor_resize2.tga"), cursorsize, '1 1 1', cursor_alpha, DRAWFLAG_NORMAL);
1217
1218         prevMouseClicked = mouseClicked;
1219 }
1220 void HUD_Configure_DrawGrid()
1221 {
1222         float i;
1223         if(autocvar_hud_configure_grid && autocvar_hud_configure_grid_alpha)
1224         {
1225                 hud_configure_gridSize.x = bound(0.005, cvar("hud_configure_grid_xsize"), 0.2);
1226                 hud_configure_gridSize.y = bound(0.005, cvar("hud_configure_grid_ysize"), 0.2);
1227                 hud_configure_realGridSize.x = hud_configure_gridSize.x * vid_conwidth;
1228                 hud_configure_realGridSize.y = hud_configure_gridSize.y * vid_conheight;
1229                 vector s;
1230                 // x-axis
1231                 s = eX + eY * vid_conheight;
1232                 for(i = 1; i < 1/hud_configure_gridSize.x; ++i)
1233                         drawfill(eX * i * hud_configure_realGridSize.x, s, '0.5 0.5 0.5', autocvar_hud_configure_grid_alpha, DRAWFLAG_NORMAL);
1234                 // y-axis
1235                 s = eY + eX * vid_conwidth;
1236                 for(i = 1; i < 1/hud_configure_gridSize.y; ++i)
1237                         drawfill(eY * i * hud_configure_realGridSize.y, s, '0.5 0.5 0.5', autocvar_hud_configure_grid_alpha, DRAWFLAG_NORMAL);
1238         }
1239 }
1240
1241 float _menu_alpha_prev;
1242 void HUD_Configure_Frame()
1243 {
1244         int i;
1245         if(autocvar__hud_configure)
1246         {
1247                 if(isdemo() || intermission == 2)
1248                 {
1249                         HUD_Configure_Exit_Force();
1250                         return;
1251                 }
1252
1253                 if(!hud_configure_prev)
1254                 {
1255                         if(autocvar_hud_cursormode)
1256                                 setcursormode(1);
1257                         hudShiftState = 0;
1258                         for(i = HUD_PANEL_NUM - 1; i >= 0; --i)
1259                                 hud_panel[panel_order[i]].update_time = time;
1260                 }
1261
1262                 // NOTE this check is necessary because _menu_alpha isn't updated the frame the menu gets enabled
1263                 if(autocvar__menu_alpha != _menu_alpha_prev)
1264                 {
1265                         if(autocvar__menu_alpha == 0)
1266                                 menu_enabled = 0;
1267                         _menu_alpha_prev = autocvar__menu_alpha;
1268                 }
1269
1270                 HUD_Configure_DrawGrid();
1271         }
1272         else if(hud_configure_prev)
1273         {
1274                 if(menu_enabled)
1275                         menu_enabled = 0;
1276                 if(autocvar_hud_cursormode)
1277                         setcursormode(0);
1278         }
1279 }
1280
1281 const float hlBorderSize = 2;
1282 const string hlBorder = "gfx/hud/default/border_highlighted";
1283 const string hlBorder2 = "gfx/hud/default/border_highlighted2";
1284 void HUD_Panel_HlBorder(float myBorder, vector color, float theAlpha)
1285 {
1286         drawfill(panel_pos - '1 1 0' * myBorder, panel_size + '2 2 0' * myBorder, '0 0.5 1', .5 * theAlpha, DRAWFLAG_NORMAL);
1287         drawpic_tiled(panel_pos - '1 1 0' * myBorder, hlBorder, '8 1 0' * hlBorderSize, eX * (panel_size.x + 2 * myBorder) + eY * hlBorderSize, color, theAlpha, DRAWFLAG_NORMAL);
1288         drawpic_tiled(panel_pos - '1 1 0' * myBorder + eY * (panel_size.y + 2 * myBorder - hlBorderSize), hlBorder, '8 1 0' * hlBorderSize, eX * (panel_size.x + 2 * myBorder) + eY * hlBorderSize, color, theAlpha, DRAWFLAG_NORMAL);
1289         drawpic_tiled(panel_pos - '1 1 0' * myBorder + eY * hlBorderSize, hlBorder2, '1 8 0' * hlBorderSize, eY * (panel_size.y + 2 * myBorder - 2 * hlBorderSize) + eX * hlBorderSize, color, theAlpha, DRAWFLAG_NORMAL);
1290         drawpic_tiled(panel_pos - '1 1 0' * myBorder + eY * hlBorderSize + eX * (panel_size.x + 2 * myBorder - hlBorderSize), hlBorder2, '1 8 0' * hlBorderSize, eY * (panel_size.y + 2 * myBorder - 2 * hlBorderSize) + eX * hlBorderSize, color, theAlpha, DRAWFLAG_NORMAL);
1291 }
1292
1293 void HUD_Configure_PostDraw()
1294 {
1295         if(autocvar__hud_configure)
1296         {
1297                 if(tab_panel)
1298                 {
1299                         panel = tab_panel;
1300                         HUD_Panel_UpdatePosSize();
1301                         drawfill(panel_pos - '1 1 0' * panel_bg_border, panel_size + '2 2 0' * panel_bg_border, '1 1 1', .2, DRAWFLAG_NORMAL);
1302                 }
1303                 if(highlightedPanel)
1304                 {
1305                         panel = highlightedPanel;
1306                         HUD_Panel_UpdatePosSize();
1307                         HUD_Panel_HlBorder(panel_bg_border * hlBorderSize, '0 0.5 1', 0.4 * (1 - autocvar__menu_alpha));
1308                 }
1309         }
1310 }