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