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