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