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