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