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