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