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