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