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