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