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