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