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