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