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