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