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