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