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