]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud_config.qc
Properly hide minigame panels from the hud editor
[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                 for (int i = 0; i < hud_panels_COUNT; ++i)
63                 {
64                         panel = hud_panels[i];
65
66                         HUD_Write_PanelCvar_n("");
67                         HUD_Write_PanelCvar_q("_pos");
68                         HUD_Write_PanelCvar_q("_size");
69                         HUD_Write_PanelCvar_q("_bg");
70                         HUD_Write_PanelCvar_q("_bg_color");
71                         HUD_Write_PanelCvar_q("_bg_color_team");
72                         HUD_Write_PanelCvar_q("_bg_alpha");
73                         HUD_Write_PanelCvar_q("_bg_border");
74                         HUD_Write_PanelCvar_q("_bg_padding");
75                         switch(panel) {
76                                 case HUD_PANEL_WEAPONS:
77                                         HUD_Write_PanelCvar_q("_accuracy");
78                                         HUD_Write_PanelCvar_q("_label");
79                                         HUD_Write_PanelCvar_q("_label_scale");
80                                         HUD_Write_PanelCvar_q("_complainbubble");
81                                         HUD_Write_PanelCvar_q("_complainbubble_padding");
82                                         HUD_Write_PanelCvar_q("_complainbubble_time");
83                                         HUD_Write_PanelCvar_q("_complainbubble_fadetime");
84                                         HUD_Write_PanelCvar_q("_complainbubble_color_outofammo");
85                                         HUD_Write_PanelCvar_q("_complainbubble_color_donthave");
86                                         HUD_Write_PanelCvar_q("_complainbubble_color_unavailable");
87                                         HUD_Write_PanelCvar_q("_ammo");
88                                         HUD_Write_PanelCvar_q("_ammo_color");
89                                         HUD_Write_PanelCvar_q("_ammo_alpha");
90                                         HUD_Write_PanelCvar_q("_aspect");
91                                         HUD_Write_PanelCvar_q("_timeout");
92                                         HUD_Write_PanelCvar_q("_timeout_effect");
93                                         HUD_Write_PanelCvar_q("_timeout_fadebgmin");
94                                         HUD_Write_PanelCvar_q("_timeout_fadefgmin");
95                                         HUD_Write_PanelCvar_q("_timeout_speed_in");
96                                         HUD_Write_PanelCvar_q("_timeout_speed_out");
97                                         HUD_Write_PanelCvar_q("_onlyowned");
98                                         HUD_Write_PanelCvar_q("_noncurrent_alpha");
99                                         HUD_Write_PanelCvar_q("_noncurrent_scale");
100                                         break;
101                                 case HUD_PANEL_AMMO:
102                                         HUD_Write_PanelCvar_q("_onlycurrent");
103                                         HUD_Write_PanelCvar_q("_noncurrent_alpha");
104                                         HUD_Write_PanelCvar_q("_noncurrent_scale");
105                                         HUD_Write_PanelCvar_q("_iconalign");
106                                         HUD_Write_PanelCvar_q("_progressbar");
107                                         HUD_Write_PanelCvar_q("_progressbar_name");
108                                         HUD_Write_PanelCvar_q("_progressbar_xoffset");
109                                         HUD_Write_PanelCvar_q("_text");
110                                         break;
111                                 case HUD_PANEL_POWERUPS:
112                                         HUD_Write_PanelCvar_q("_iconalign");
113                                         HUD_Write_PanelCvar_q("_baralign");
114                                         HUD_Write_PanelCvar_q("_progressbar");
115                                         HUD_Write_PanelCvar_q("_text");
116                                         break;
117                                 case HUD_PANEL_HEALTHARMOR:
118                                         HUD_Write_PanelCvar_q("_flip");
119                                         HUD_Write_PanelCvar_q("_iconalign");
120                                         HUD_Write_PanelCvar_q("_baralign");
121                                         HUD_Write_PanelCvar_q("_progressbar");
122                                         HUD_Write_PanelCvar_q("_progressbar_health");
123                                         HUD_Write_PanelCvar_q("_progressbar_armor");
124                                         HUD_Write_PanelCvar_q("_progressbar_gfx");
125                                         HUD_Write_PanelCvar_q("_progressbar_gfx_smooth");
126                                         HUD_Write_PanelCvar_q("_text");
127                                         break;
128                                 case HUD_PANEL_NOTIFY:
129                                         HUD_Write_PanelCvar_q("_flip");
130                                         HUD_Write_PanelCvar_q("_fontsize");
131                                         HUD_Write_PanelCvar_q("_time");
132                                         HUD_Write_PanelCvar_q("_fadetime");
133                                         HUD_Write_PanelCvar_q("_icon_aspect");
134                                         break;
135                                 case HUD_PANEL_TIMER:
136                                         HUD_Write_PanelCvar_q("_increment");
137                                         break;
138                                 case HUD_PANEL_RADAR:
139                                         HUD_Write_PanelCvar_q("_foreground_alpha");
140                                         HUD_Write_PanelCvar_q("_rotation");
141                                         HUD_Write_PanelCvar_q("_zoommode");
142                                         HUD_Write_PanelCvar_q("_scale");
143                                         HUD_Write_PanelCvar_q("_maximized_scale");
144                                         HUD_Write_PanelCvar_q("_maximized_size");
145                                         HUD_Write_PanelCvar_q("_maximized_rotation");
146                                         HUD_Write_PanelCvar_q("_maximized_zoommode");
147                                         break;
148                                 case HUD_PANEL_SCORE:
149                                         HUD_Write_PanelCvar_q("_rankings");
150                                         break;
151                                 case HUD_PANEL_VOTE:
152                                         HUD_Write_PanelCvar_q("_alreadyvoted_alpha");
153                                         break;
154                                 case HUD_PANEL_MODICONS:
155                                         HUD_Write_PanelCvar_q("_ca_layout");
156                                         HUD_Write_PanelCvar_q("_dom_layout");
157                                         HUD_Write_PanelCvar_q("_freezetag_layout");
158                                         break;
159                                 case HUD_PANEL_PRESSEDKEYS:
160                                         HUD_Write_PanelCvar_q("_aspect");
161                                         HUD_Write_PanelCvar_q("_attack");
162                                         break;
163                                 case HUD_PANEL_ENGINEINFO:
164                                         HUD_Write_PanelCvar_q("_framecounter_time");
165                                         HUD_Write_PanelCvar_q("_framecounter_decimals");
166                                         break;
167                                 case HUD_PANEL_INFOMESSAGES:
168                                         HUD_Write_PanelCvar_q("_flip");
169                                         break;
170                                 case HUD_PANEL_PHYSICS:
171                                         HUD_Write_PanelCvar_q("_speed_unit");
172                                         HUD_Write_PanelCvar_q("_speed_unit_show");
173                                         HUD_Write_PanelCvar_q("_speed_max");
174                                         HUD_Write_PanelCvar_q("_speed_vertical");
175                                         HUD_Write_PanelCvar_q("_topspeed");
176                                         HUD_Write_PanelCvar_q("_topspeed_time");
177                                         HUD_Write_PanelCvar_q("_acceleration_max");
178                                         HUD_Write_PanelCvar_q("_acceleration_vertical");
179                                         HUD_Write_PanelCvar_q("_flip");
180                                         HUD_Write_PanelCvar_q("_baralign");
181                                         HUD_Write_PanelCvar_q("_progressbar");
182                                         HUD_Write_PanelCvar_q("_progressbar_acceleration_mode");
183                                         HUD_Write_PanelCvar_q("_progressbar_acceleration_scale");
184                                         HUD_Write_PanelCvar_q("_progressbar_acceleration_nonlinear");
185                                         HUD_Write_PanelCvar_q("_text");
186                                         HUD_Write_PanelCvar_q("_text_scale");
187                                         break;
188                                 case HUD_PANEL_CENTERPRINT:
189                                         HUD_Write_PanelCvar_q("_align");
190                                         HUD_Write_PanelCvar_q("_flip");
191                                         HUD_Write_PanelCvar_q("_fontscale");
192                                         HUD_Write_PanelCvar_q("_time");
193                                         HUD_Write_PanelCvar_q("_fade_in");
194                                         HUD_Write_PanelCvar_q("_fade_out");
195                                         HUD_Write_PanelCvar_q("_fade_subsequent");
196                                         HUD_Write_PanelCvar_q("_fade_subsequent_passone");
197                                         HUD_Write_PanelCvar_q("_fade_subsequent_passone_minalpha");
198                                         HUD_Write_PanelCvar_q("_fade_subsequent_passtwo");
199                                         HUD_Write_PanelCvar_q("_fade_subsequent_passtwo_minalpha");
200                                         HUD_Write_PanelCvar_q("_fade_subsequent_minfontsize");
201                                         HUD_Write_PanelCvar_q("_fade_minfontsize");
202                                         break;
203                                 case HUD_PANEL_ITEMSTIME:
204                                         HUD_Write_PanelCvar_q("_iconalign");
205                                         HUD_Write_PanelCvar_q("_progressbar");
206                                         HUD_Write_PanelCvar_q("_progressbar_name");
207                                         HUD_Write_PanelCvar_q("_progressbar_reduced");
208                                         HUD_Write_PanelCvar_q("_text");
209                                         HUD_Write_PanelCvar_q("_ratio");
210                                         HUD_Write_PanelCvar_q("_dynamicsize");
211                                 case HUD_PANEL_QUICKMENU:
212                                         HUD_Write_PanelCvar_q("_align");
213                                         break;
214                         }
215                         HUD_Write("\n");
216                 }
217                 HUD_Write("menu_sync\n"); // force the menu to reread the cvars, so that the dialogs are updated
218
219                 LOG_INFOF(_("^2Successfully exported to %s! (Note: It's saved in data/data/)\n"), filename);
220                 fclose(fh);
221         }
222         else
223                 LOG_INFOF(_("^1Couldn't write to %s\n"), filename);
224 }
225
226 void HUD_Configure_Exit_Force()
227 {
228         if (menu_enabled)
229         {
230                 menu_enabled = 0;
231                 localcmd("togglemenu\n");
232         }
233         cvar_set("_hud_configure", "0");
234 }
235
236 // check if move will result in panel being moved into another panel. If so, return snapped vector, otherwise return the given vector
237 vector HUD_Panel_CheckMove(vector myPos, vector mySize)
238 {
239         vector myCenter, targCenter;
240         vector myTarget = myPos;
241         int i;
242         for (i = 0; i < hud_panels_COUNT; ++i) {
243                 panel = hud_panels[i];
244                 if(!(panel.panel_configflags & PANEL_CONFIG_MAIN)) continue;
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_panels_COUNT; ++i) {
340                 panel = hud_panels[i];
341                 if(!(panel.panel_configflags & PANEL_CONFIG_MAIN)) continue;
342                 if(panel == highlightedPanel) continue;
343                 HUD_Panel_UpdatePosSize();
344                 if(!panel_enabled) continue;
345
346                 panel_pos -= '1 1 0' * panel_bg_border;
347                 panel_size += '2 2 0' * panel_bg_border;
348
349                 targEndPos = panel_pos + panel_size;
350
351                 // resizeorigin is WITHIN target panel, just abort any collision testing against that particular panel to produce expected behaviour!
352                 if(resizeorigin.x > panel_pos.x && resizeorigin.x < targEndPos.x && resizeorigin.y > panel_pos.y && resizeorigin.y < targEndPos.y)
353                         continue;
354
355                 if (resizeCorner == 1)
356                 {
357                         // check if this panel is on our way
358                         if (resizeorigin.x <= panel_pos.x)
359                                 continue;
360                         if (resizeorigin.y <= panel_pos.y)
361                                 continue;
362                         if (targEndPos.x <= resizeorigin.x - mySize.x)
363                                 continue;
364                         if (targEndPos.y <= resizeorigin.y - mySize.y)
365                                 continue;
366
367                         // there is a collision:
368                         // detect which side of the panel we are facing is actually limiting the resizing
369                         // (which side the resize direction finds for first) and reduce the size up to there
370                         //
371                         // dist is the distance between resizeorigin and the "analogous" point of the panel
372                         // in this case between resizeorigin (bottom-right point) and the bottom-right point of the panel
373                         dist.x = resizeorigin.x - targEndPos.x;
374                         dist.y = resizeorigin.y - targEndPos.y;
375                         if (dist.y <= 0 || dist.x / dist.y > ratio)
376                                 mySize.x = min(mySize.x, dist.x);
377                         else
378                                 mySize.y = min(mySize.y, dist.y);
379                 }
380                 else if (resizeCorner == 2)
381                 {
382                         if (resizeorigin.x >= targEndPos.x)
383                                 continue;
384                         if (resizeorigin.y <= panel_pos.y)
385                                 continue;
386                         if (panel_pos.x >= resizeorigin.x + mySize.x)
387                                 continue;
388                         if (targEndPos.y <= resizeorigin.y - mySize.y)
389                                 continue;
390
391                         dist.x = panel_pos.x - resizeorigin.x;
392                         dist.y = resizeorigin.y - targEndPos.y;
393                         if (dist.y <= 0 || dist.x / dist.y > ratio)
394                                 mySize.x = min(mySize.x, dist.x);
395                         else
396                                 mySize.y = min(mySize.y, dist.y);
397                 }
398                 else if (resizeCorner == 3)
399                 {
400                         if (resizeorigin.x <= panel_pos.x)
401                                 continue;
402                         if (resizeorigin.y >= targEndPos.y)
403                                 continue;
404                         if (targEndPos.x <= resizeorigin.x - mySize.x)
405                                 continue;
406                         if (panel_pos.y >= resizeorigin.y + mySize.y)
407                                 continue;
408
409                         dist.x = resizeorigin.x - targEndPos.x;
410                         dist.y = panel_pos.y - resizeorigin.y;
411                         if (dist.y <= 0 || dist.x / dist.y > ratio)
412                                 mySize.x = min(mySize.x, dist.x);
413                         else
414                                 mySize.y = min(mySize.y, dist.y);
415                 }
416                 else if (resizeCorner == 4)
417                 {
418                         if (resizeorigin.x >= targEndPos.x)
419                                 continue;
420                         if (resizeorigin.y >= targEndPos.y)
421                                 continue;
422                         if (panel_pos.x >= resizeorigin.x + mySize.x)
423                                 continue;
424                         if (panel_pos.y >= resizeorigin.y + mySize.y)
425                                 continue;
426
427                         dist.x = panel_pos.x - resizeorigin.x;
428                         dist.y = panel_pos.y - resizeorigin.y;
429                         if (dist.y <= 0 || dist.x / dist.y > ratio)
430                                 mySize.x = min(mySize.x, dist.x);
431                         else
432                                 mySize.y = min(mySize.y, dist.y);
433                 }
434                 //if(cvar("hud_configure_checkcollisions_debug"))
435                         //drawfill(panel_pos, panel_size, '1 1 0', .3, DRAWFLAG_NORMAL);
436         }
437
438         return mySize;
439 }
440
441 void HUD_Panel_SetPosSize(vector mySize)
442 {
443         panel = highlightedPanel;
444         HUD_Panel_UpdatePosSize();
445         vector 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                 if(nPrimary == K_UPARROW)
579                         resizeCorner = 1;
580                 else if(nPrimary == K_RIGHTARROW)
581                         resizeCorner = 2;
582                 else if(nPrimary == K_LEFTARROW)
583                         resizeCorner = 3;
584                 else // if(nPrimary == K_DOWNARROW)
585                         resizeCorner = 4;
586
587                 // ctrl+arrow reduces the size, instead of increasing it
588                 // Note that ctrl disables collisions check too, but it's fine
589                 // since we don't collide with anything reducing the size
590                 if (hudShiftState & S_CTRL) {
591                         step = -step;
592                         resizeCorner = 5 - resizeCorner;
593                 }
594
595                 vector mySize;
596                 mySize = panel_size;
597                 panel_click_resizeorigin = panel_pos;
598                 if(resizeCorner == 1) {
599                         panel_click_resizeorigin += mySize;
600                         mySize.y += step;
601                 } else if(resizeCorner == 2) {
602                         panel_click_resizeorigin.y += mySize.y;
603                         mySize.x += step;
604                 } else if(resizeCorner == 3) {
605                         panel_click_resizeorigin.x += mySize.x;
606                         mySize.x += step;
607                 } else { // resizeCorner == 4
608                         mySize.y += step;
609                 }
610                 HUD_Panel_SetPosSize(mySize);
611         }
612         else // move
613         {
614                 vector pos;
615                 pos = panel_pos;
616                 if(nPrimary == K_UPARROW)
617                         pos.y -= step;
618                 else if(nPrimary == K_DOWNARROW)
619                         pos.y += step;
620                 else if(nPrimary == K_LEFTARROW)
621                         pos.x -= step;
622                 else // if(nPrimary == K_RIGHTARROW)
623                         pos.x += step;
624
625                 HUD_Panel_SetPos(pos);
626         }
627
628         panel = highlightedPanel;
629         HUD_Panel_UpdatePosSize();
630
631         if (highlightedPanel_initial_pos != panel_pos || highlightedPanel_initial_size != panel_size)
632         {
633                 // backup!
634                 panel_pos_backup = highlightedPanel_initial_pos;
635                 panel_size_backup = highlightedPanel_initial_size;
636                 highlightedPanel_backup = highlightedPanel;
637         }
638 }
639
640 void HUD_Panel_EnableMenu();
641 entity tab_panels[hud_panels_MAX];
642 entity tab_panel;
643 vector tab_panel_pos;
644 float tab_backward;
645 void HUD_Panel_FirstInDrawQ(float id);
646 void reset_tab_panels()
647 {
648         int i;
649         for(i = 0; i < hud_panels_COUNT; ++i)
650                 tab_panels[i] = world;
651 }
652 float HUD_Panel_InputEvent(float bInputType, float nPrimary, float nSecondary)
653 {
654         string s;
655
656         if(bInputType == 2)
657                 return false;
658
659         if(!autocvar__hud_configure)
660                 return false;
661
662         if(bInputType == 3)
663         {
664                 mousepos.x = nPrimary;
665                 mousepos.y = nSecondary;
666                 return true;
667         }
668
669         // block any input while a menu dialog is fading
670         // don't block mousepos read as it leads to cursor jumps in the interaction with the menu
671         if(autocvar__menu_alpha)
672         {
673                 hudShiftState = 0;
674                 mouseClicked = 0;
675                 return true;
676         }
677
678         // allow console bind to work
679         string con_keys;
680         float keys;
681         con_keys = findkeysforcommand("toggleconsole", 0);
682         keys = tokenize(con_keys); // findkeysforcommand returns data for this
683
684         bool hit_con_bind = false;
685         int i;
686         for (i = 0; i < keys; ++i)
687         {
688                 if(nPrimary == stof(argv(i)))
689                         hit_con_bind = true;
690         }
691
692         if(bInputType == 0) {
693                 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
694                 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
695                 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
696         }
697         else if(bInputType == 1) {
698                 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
699                 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
700                 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
701         }
702
703         if(nPrimary == K_CTRL)
704         {
705                 if (bInputType == 1) //ctrl has been released
706                 {
707                         if (tab_panel)
708                         {
709                                 //switch to selected panel
710                                 highlightedPanel = tab_panel;
711                                 highlightedAction = 0;
712                                 HUD_Panel_FirstInDrawQ(highlightedPanel.panel_id);
713                         }
714                         tab_panel = world;
715                         reset_tab_panels();
716                 }
717         }
718
719         if(nPrimary == K_MOUSE1)
720         {
721                 if(bInputType == 0) // key pressed
722                         mouseClicked |= S_MOUSE1;
723                 else if(bInputType == 1) // key released
724                         mouseClicked -= (mouseClicked & S_MOUSE1);
725         }
726         else if(nPrimary == K_MOUSE2)
727         {
728                 if(bInputType == 0) // key pressed
729                         mouseClicked |= S_MOUSE2;
730                 else if(bInputType == 1) // key released
731                         mouseClicked -= (mouseClicked & S_MOUSE2);
732         }
733         else if(nPrimary == K_ESCAPE)
734         {
735                 if (bInputType == 1)
736                         return true;
737                 menu_enabled = 1;
738                 localcmd("menu_showhudexit\n");
739         }
740         else if(nPrimary == K_BACKSPACE && hudShiftState & S_CTRL)
741         {
742                 if (bInputType == 1)
743                         return true;
744                 if (!menu_enabled)
745                         cvar_set("_hud_configure", "0");
746         }
747         else if(nPrimary == K_TAB && hudShiftState & S_CTRL) // switch panel
748         {
749                 if (bInputType == 1 || mouseClicked)
750                         return true;
751
752                 // FIXME minor bug: if a panel is highlighted, has the same pos_x and
753                 // lays in the same level of another panel then the next consecutive
754                 // CTRL TAB presses will reselect once more the highlighted panel
755
756                 entity starting_panel;
757                 entity old_tab_panel = tab_panel;
758                 if (!tab_panel) //first press of TAB
759                 {
760                         if (highlightedPanel)
761                         {
762                                 panel = highlightedPanel;
763                                 HUD_Panel_UpdatePosSize();
764                         }
765                         else
766                                 panel_pos = '0 0 0';
767                         starting_panel = highlightedPanel;
768                         tab_panel_pos = panel_pos; //to compute level
769                 }
770                 else
771                 {
772                         if ( ((!tab_backward) && (hudShiftState & S_SHIFT)) || (tab_backward && !(hudShiftState & S_SHIFT)) ) //tab direction changed?
773                                 reset_tab_panels();
774                         starting_panel = tab_panel;
775                 }
776                 tab_backward = (hudShiftState & S_SHIFT);
777
778                 float k, level = 0, start_posX;
779                 vector candidate_pos = '0 0 0';
780                 const float LEVELS_NUM = 4;
781                 float level_height = vid_conheight / LEVELS_NUM;
782 :find_tab_panel
783                 level = floor(tab_panel_pos.y / level_height) * level_height; //starting level
784                 candidate_pos.x = (!tab_backward) ? vid_conwidth : 0;
785                 start_posX = tab_panel_pos.x;
786                 tab_panel = world;
787                 k=0;
788                 while(++k)
789                 {
790                         for(i = 0; i < hud_panels_COUNT; ++i)
791                         {
792                                 panel = hud_panels[i];
793                                 if(!(panel.panel_configflags & PANEL_CONFIG_MAIN))
794                                         continue;
795                                 if (panel == tab_panels[i] || panel == starting_panel)
796                                         continue;
797                                 HUD_Panel_UpdatePosSize();
798                                 if (panel_pos.y >= level && (panel_pos.y - level) < level_height)
799                                 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)) )
800                                         || ( 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)) )  )
801                                 {
802                                         tab_panel = panel;
803                                         tab_panel_pos = candidate_pos = panel_pos;
804                                 }
805                         }
806                         if (tab_panel)
807                                 break;
808                         if (k == LEVELS_NUM) //tab_panel not found
809                         {
810                                 reset_tab_panels();
811                                 if (!old_tab_panel)
812                                 {
813                                         tab_panel = world;
814                                         return true;
815                                 }
816                                 starting_panel = old_tab_panel;
817                                 old_tab_panel = world;
818                                 goto find_tab_panel; //u must find tab_panel!
819                         }
820                         if (!tab_backward)
821                         {
822                                 level = (level + level_height) % vid_conheight;
823                                 start_posX = 0;
824                                 candidate_pos.x = vid_conwidth;
825                         }
826                         else
827                         {
828                                 level = (level - level_height) % vid_conheight;
829                                 start_posX = vid_conwidth;
830                                 candidate_pos.x = 0;
831                         }
832                 }
833
834                 tab_panels[tab_panel.panel_id] = tab_panel;
835         }
836         else if(nPrimary == K_SPACE && hudShiftState & S_CTRL) // enable/disable highlighted panel or dock
837         {
838                 if (bInputType == 1 || mouseClicked)
839                         return true;
840
841                 if (highlightedPanel)
842                         cvar_set(strcat("hud_panel_", highlightedPanel.panel_name), ftos(!cvar(strcat("hud_panel_", highlightedPanel.panel_name))));
843                 else
844                         cvar_set(strcat("hud_dock"), (autocvar_hud_dock == "") ? "dock" : "");
845         }
846         else if(nPrimary == 'c' && hudShiftState & S_CTRL) // copy highlighted panel size
847         {
848                 if (bInputType == 1 || mouseClicked)
849                         return true;
850
851                 if (highlightedPanel)
852                 {
853                         panel = highlightedPanel;
854                         HUD_Panel_UpdatePosSize();
855                         panel_size_copied = panel_size;
856                 }
857         }
858         else if(nPrimary == 'v' && hudShiftState & S_CTRL) // past copied size on the highlighted panel
859         {
860                 if (bInputType == 1 || mouseClicked)
861                         return true;
862
863                 if (panel_size_copied == '0 0 0' || !highlightedPanel)
864                         return true;
865
866                 panel = highlightedPanel;
867                 HUD_Panel_UpdatePosSize();
868
869                 // reduce size if it'd go beyond screen boundaries
870                 vector tmp_size = panel_size_copied;
871                 if (panel_pos.x + panel_size_copied.x > vid_conwidth)
872                         tmp_size.x = vid_conwidth - panel_pos.x;
873                 if (panel_pos.y + panel_size_copied.y > vid_conheight)
874                         tmp_size.y = vid_conheight - panel_pos.y;
875
876                 if (panel_size == tmp_size)
877                         return true;
878
879                 // backup first!
880                 panel_pos_backup = panel_pos;
881                 panel_size_backup = panel_size;
882                 highlightedPanel_backup = highlightedPanel;
883
884                 s = strcat(ftos(tmp_size.x/vid_conwidth), " ", ftos(tmp_size.y/vid_conheight));
885                 cvar_set(strcat("hud_panel_", highlightedPanel.panel_name, "_size"), s);
886         }
887         else if(nPrimary == 'z' && hudShiftState & S_CTRL) // undo last action
888         {
889                 if (bInputType == 1 || mouseClicked)
890                         return true;
891                 //restore previous values
892                 if (highlightedPanel_backup)
893                 {
894                         s = strcat(ftos(panel_pos_backup.x/vid_conwidth), " ", ftos(panel_pos_backup.y/vid_conheight));
895                         cvar_set(strcat("hud_panel_", highlightedPanel_backup.panel_name, "_pos"), s);
896                         s = strcat(ftos(panel_size_backup.x/vid_conwidth), " ", ftos(panel_size_backup.y/vid_conheight));
897                         cvar_set(strcat("hud_panel_", highlightedPanel_backup.panel_name, "_size"), s);
898                         highlightedPanel_backup = world;
899                 }
900         }
901         else if(nPrimary == 's' && hudShiftState & S_CTRL) // save config
902         {
903                 if (bInputType == 1 || mouseClicked)
904                         return true;
905                 localcmd("hud save myconfig\n");
906         }
907         else if(nPrimary == K_UPARROW || nPrimary == K_DOWNARROW || nPrimary == K_LEFTARROW || nPrimary == K_RIGHTARROW)
908         {
909                 if (bInputType == 1)
910                 {
911                         pressed_key_time = 0;
912                         return true;
913                 }
914                 else if (pressed_key_time == 0)
915                         pressed_key_time = time;
916
917                 if (!mouseClicked)
918                         HUD_Panel_Arrow_Action(nPrimary); //move or resize panel
919         }
920         else if(nPrimary == K_ENTER || nPrimary == K_SPACE || nPrimary == K_KP_ENTER)
921         {
922                 if (bInputType == 1)
923                         return true;
924                 if (highlightedPanel)
925                         HUD_Panel_EnableMenu();
926         }
927         else if(hit_con_bind || nPrimary == K_PAUSE)
928                 return false;
929
930         return true;
931 }
932
933 float HUD_Panel_Check_Mouse_Pos(float allow_move)
934 {
935         int i, j = 0;
936         while(j < hud_panels_COUNT)
937         {
938                 i = panel_order[j];
939                 j += 1;
940
941                 panel = hud_panels[i];
942                 if(!(panel.panel_configflags & PANEL_CONFIG_MAIN)) continue;
943                 HUD_Panel_UpdatePosSize();
944
945                 float border = max(8, panel_bg_border); // FORCED border so a small border size doesn't mean you can't resize
946
947                 // move
948                 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)
949                 {
950                         return 1;
951                 }
952                 // resize from topleft border
953                 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)
954                 {
955                         return 2;
956                 }
957                 // resize from topright border
958                 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)
959                 {
960                         return 3;
961                 }
962                 // resize from bottomleft border
963                 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)
964                 {
965                         return 3;
966                 }
967                 // resize from bottomright border
968                 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)
969                 {
970                         return 2;
971                 }
972         }
973         return 0;
974 }
975
976 // move a panel to the beginning of the panel order array (which means it gets drawn last, on top of everything else)
977 void HUD_Panel_FirstInDrawQ(float id)
978 {
979         int i;
980         int place = -1;
981         // find out where in the array our current id is, save into place
982         for(i = 0; i < hud_panels_COUNT; ++i)
983         {
984                 if(panel_order[i] == id)
985                 {
986                         place = i;
987                         break;
988                 }
989         }
990         // place last if we didn't find a place for it yet (probably new panel, or screwed up cvar)
991         if(place == -1)
992                 place = hud_panels_COUNT - 1;
993
994         // move all ids up by one step in the array until "place"
995         for(i = place; i > 0; --i)
996         {
997                 panel_order[i] = panel_order[i-1];
998         }
999         // now save the new top id
1000         panel_order[0] = id;
1001
1002         // let's save them into the cvar by some strcat trickery
1003         string s = "";
1004         for(i = 0; i < hud_panels_COUNT; ++i)
1005         {
1006                 s = strcat(s, ftos(panel_order[i]), " ");
1007         }
1008         cvar_set("_hud_panelorder", s);
1009         if(hud_panelorder_prev)
1010                 strunzone(hud_panelorder_prev);
1011         hud_panelorder_prev = strzone(autocvar__hud_panelorder); // prevent HUD_Main from doing useless update, we already updated here
1012 }
1013
1014 void HUD_Panel_Highlight(float allow_move)
1015 {
1016         int i, j = 0;
1017
1018         while(j < hud_panels_COUNT)
1019         {
1020                 i = panel_order[j];
1021                 j += 1;
1022
1023                 panel = hud_panels[i];
1024                 if(!(panel.panel_configflags & PANEL_CONFIG_MAIN))
1025                         continue;
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_panels[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_panels[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_panels[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_panels[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_panels[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(prevMouseClicked)
1190                         highlightedAction = 0;
1191                 if(menu_enabled == 2)
1192                         mouse_over_panel = 0;
1193                 else
1194                         mouse_over_panel = HUD_Panel_Check_Mouse_Pos(true);
1195                 if (mouse_over_panel && !tab_panel)
1196                         drawfill(panel_pos - '1 1 0' * panel_bg_border, panel_size + '2 2 0' * panel_bg_border, '1 1 1', .1, DRAWFLAG_NORMAL);
1197         }
1198         // draw cursor after performing move/resize to have the panel pos/size updated before mouse_over_panel
1199         const vector cursorsize = '32 32 0';
1200         float cursor_alpha = 1 - autocvar__menu_alpha;
1201
1202         if(!mouse_over_panel)
1203                 drawpic(mousepos, strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"), cursorsize, '1 1 1', cursor_alpha, DRAWFLAG_NORMAL);
1204         else if(mouse_over_panel == 1)
1205                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", autocvar_menu_skin, "/cursor_move.tga"), cursorsize, '1 1 1', cursor_alpha, DRAWFLAG_NORMAL);
1206         else if(mouse_over_panel == 2)
1207                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", autocvar_menu_skin, "/cursor_resize.tga"), cursorsize, '1 1 1', cursor_alpha, DRAWFLAG_NORMAL);
1208         else
1209                 drawpic(mousepos - cursorsize * 0.5, strcat("gfx/menu/", autocvar_menu_skin, "/cursor_resize2.tga"), cursorsize, '1 1 1', cursor_alpha, DRAWFLAG_NORMAL);
1210
1211         prevMouseClicked = mouseClicked;
1212 }
1213 void HUD_Configure_DrawGrid()
1214 {
1215         float i;
1216         if(autocvar_hud_configure_grid && autocvar_hud_configure_grid_alpha)
1217         {
1218                 hud_configure_gridSize.x = bound(0.005, cvar("hud_configure_grid_xsize"), 0.2);
1219                 hud_configure_gridSize.y = bound(0.005, cvar("hud_configure_grid_ysize"), 0.2);
1220                 hud_configure_realGridSize.x = hud_configure_gridSize.x * vid_conwidth;
1221                 hud_configure_realGridSize.y = hud_configure_gridSize.y * vid_conheight;
1222                 vector s;
1223                 // x-axis
1224                 s = eX + eY * vid_conheight;
1225                 for(i = 1; i < 1/hud_configure_gridSize.x; ++i)
1226                         drawfill(eX * i * hud_configure_realGridSize.x, s, '0.5 0.5 0.5', autocvar_hud_configure_grid_alpha, DRAWFLAG_NORMAL);
1227                 // y-axis
1228                 s = eY + eX * vid_conwidth;
1229                 for(i = 1; i < 1/hud_configure_gridSize.y; ++i)
1230                         drawfill(eY * i * hud_configure_realGridSize.y, s, '0.5 0.5 0.5', autocvar_hud_configure_grid_alpha, DRAWFLAG_NORMAL);
1231         }
1232 }
1233
1234 float _menu_alpha_prev;
1235 void HUD_Configure_Frame()
1236 {
1237         int i;
1238         if(autocvar__hud_configure)
1239         {
1240                 if(isdemo() || intermission == 2)
1241                 {
1242                         HUD_Configure_Exit_Force();
1243                         return;
1244                 }
1245
1246                 if(!hud_configure_prev)
1247                 {
1248                         if(autocvar_hud_cursormode)
1249                                 setcursormode(1);
1250                         hudShiftState = 0;
1251                         for(i = hud_panels_COUNT - 1; i >= 0; --i)
1252                                 hud_panels[panel_order[i]].update_time = time;
1253                 }
1254
1255                 // NOTE this check is necessary because _menu_alpha isn't updated the frame the menu gets enabled
1256                 if(autocvar__menu_alpha != _menu_alpha_prev)
1257                 {
1258                         if(autocvar__menu_alpha == 0)
1259                                 menu_enabled = 0;
1260                         _menu_alpha_prev = autocvar__menu_alpha;
1261                 }
1262
1263                 HUD_Configure_DrawGrid();
1264         }
1265         else if(hud_configure_prev)
1266         {
1267                 if(menu_enabled)
1268                         menu_enabled = 0;
1269                 if(autocvar_hud_cursormode)
1270                         setcursormode(0);
1271         }
1272 }
1273
1274 const float hlBorderSize = 2;
1275 const string hlBorder = "gfx/hud/default/border_highlighted";
1276 const string hlBorder2 = "gfx/hud/default/border_highlighted2";
1277 void HUD_Panel_HlBorder(float myBorder, vector color, float theAlpha)
1278 {
1279         drawfill(panel_pos - '1 1 0' * myBorder, panel_size + '2 2 0' * myBorder, '0 0.5 1', .5 * theAlpha, DRAWFLAG_NORMAL);
1280         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);
1281         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);
1282         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);
1283         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);
1284 }
1285
1286 void HUD_Configure_PostDraw()
1287 {
1288         if(autocvar__hud_configure)
1289         {
1290                 if(tab_panel)
1291                 {
1292                         panel = tab_panel;
1293                         HUD_Panel_UpdatePosSize();
1294                         drawfill(panel_pos - '1 1 0' * panel_bg_border, panel_size + '2 2 0' * panel_bg_border, '1 1 1', .2, DRAWFLAG_NORMAL);
1295                 }
1296                 if(highlightedPanel)
1297                 {
1298                         panel = highlightedPanel;
1299                         HUD_Panel_UpdatePosSize();
1300                         HUD_Panel_HlBorder(panel_bg_border * hlBorderSize, '0 0.5 1', 0.4 * (1 - autocvar__menu_alpha));
1301                 }
1302         }
1303 }