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