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