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