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