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