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