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