]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/radar.qc
Add a common handler for cursors, allowing multiple menus to be open at the same...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / radar.qc
1 #include "radar.qh"
2
3 #include <client/autocvars.qh>
4 #include <client/defs.qh>
5 #include <client/miscfunctions.qh>
6 #include <common/ent_cs.qh>
7 #include <common/mapinfo.qh>
8 #include <client/mapvoting.qh>
9 #include <client/teamradar.qh>
10 #include <common/mutators/mutator/waypoints/all.qh>
11
12 // Radar (#6)
13
14 bool HUD_Radar_Clickable()
15 {
16         return hud_panel_radar_mouse && !hud_panel_radar_temp_hidden;
17 }
18
19 void HUD_Radar_Show_Maximized(bool doshow, bool clickable)
20 {
21     TC(bool, doshow);
22         hud_panel_radar_maximized = doshow;
23         hud_panel_radar_temp_hidden = 0;
24
25         if ( doshow )
26         {
27                 if (clickable)
28                 {
29                         hud_panel_radar_mouse = 1;
30
31                         // we must unset the player's buttons, as they aren't released elsewhere
32                         localcmd("-fire\n");
33                         localcmd("-fire2\n");
34                         localcmd("-use\n");
35                         localcmd("-hook\n");
36                         localcmd("-jump\n");
37                 }
38         }
39         else if ( hud_panel_radar_mouse )
40         {
41                 hud_panel_radar_mouse = 0;
42                 mouseClicked = 0;
43         }
44 }
45 void HUD_Radar_Hide_Maximized()
46 {
47         HUD_Radar_Show_Maximized(false,false);
48 }
49
50
51 float HUD_Radar_InputEvent(int bInputType, float nPrimary, float nSecondary)
52 {
53     TC(int, bInputType);
54         if(!hud_panel_radar_maximized || !hud_panel_radar_mouse ||
55                 autocvar__hud_configure || mv_active)
56                 return false;
57
58         if(bInputType == 3)
59         {
60                 mousepos_x = nPrimary;
61                 mousepos_y = nSecondary;
62                 return true;
63         }
64
65         if(nPrimary == K_MOUSE1)
66         {
67                 if(bInputType == 0) // key pressed
68                         mouseClicked |= S_MOUSE1;
69                 else if(bInputType == 1) // key released
70                         mouseClicked -= (mouseClicked & S_MOUSE1);
71         }
72         else if(nPrimary == K_MOUSE2)
73         {
74                 if(bInputType == 0) // key pressed
75                         mouseClicked |= S_MOUSE2;
76                 else if(bInputType == 1) // key released
77                         mouseClicked -= (mouseClicked & S_MOUSE2);
78         }
79         else if ( nPrimary == K_ESCAPE && bInputType == 0 )
80         {
81                 HUD_Radar_Hide_Maximized();
82         }
83         else
84         {
85                 // allow console/use binds to work without hiding the map
86                 string con_keys = strcat(findkeysforcommand("toggleconsole", 0), " ", findkeysforcommand("+use", 0)) ;
87                 int keys = tokenize(con_keys); // findkeysforcommand returns data for this
88                 int i;
89                 for (i = 0; i < keys; ++i)
90                 {
91                         if(nPrimary == stof(argv(i)))
92                                 return false;
93                 }
94
95                 if ( STAT(HEALTH) <= 0 )
96                 {
97                         // Show scoreboard
98                         if ( bInputType < 2 )
99                         {
100                                 con_keys = findkeysforcommand("+showscores", 0);
101                                 keys = tokenize(con_keys);
102                                 for (i = 0; i < keys; ++i)
103                                 {
104                                         if ( nPrimary == stof(argv(i)) )
105                                         {
106                                                 hud_panel_radar_temp_hidden = bInputType == 0;
107                                                 return false;
108                                         }
109                                 }
110                         }
111                 }
112                 else if ( bInputType == 0 )
113                         HUD_Radar_Hide_Maximized();
114
115                 return false;
116         }
117
118         return true;
119 }
120
121 void HUD_Radar_Mouse()
122 {
123         if ( !hud_panel_radar_mouse ) return;
124         if(mv_active) return;
125
126         if ( intermission )
127         {
128                 HUD_Radar_Hide_Maximized();
129                 return;
130         }
131
132         if(mouseClicked & S_MOUSE2)
133         {
134                 HUD_Radar_Hide_Maximized();
135                 return;
136         }
137
138         panel = HUD_PANEL(RADAR);
139         HUD_Panel_LoadCvars();
140
141
142         panel_size = autocvar_hud_panel_radar_maximized_size;
143         panel_size_x = bound(0.2, panel_size_x, 1) * vid_conwidth;
144         panel_size_y = bound(0.2, panel_size_y, 1) * vid_conheight;
145         panel_pos_x = (vid_conwidth - panel_size_x) / 2;
146         panel_pos_y = (vid_conheight - panel_size_y) / 2;
147
148         if(mouseClicked & S_MOUSE1)
149         {
150                 // click outside
151                 if ( mousepos_x < panel_pos_x || mousepos_x > panel_pos_x + panel_size_x ||
152                          mousepos_y < panel_pos_y || mousepos_y > panel_pos_y + panel_size_y )
153                 {
154                         HUD_Radar_Hide_Maximized();
155                         return;
156                 }
157                 vector pos = teamradar_texcoord_to_3dcoord(teamradar_2dcoord_to_texcoord(mousepos),view_origin_z);
158                 localcmd(sprintf("cmd ons_spawn %f %f %f",pos_x,pos_y,pos_z));
159
160                 HUD_Radar_Hide_Maximized();
161                 return;
162         }
163 }
164
165 void HUD_Radar()
166 {
167         if (!autocvar__hud_configure)
168         {
169                 if (hud_panel_radar_maximized)
170                 {
171                         if (!hud_draw_maximized) return;
172                 }
173                 else
174                 {
175                         if (autocvar_hud_panel_radar == 0) return;
176                         if (autocvar_hud_panel_radar != 2 && !teamplay) return;
177                         if(radar_panel_modified)
178                         {
179                                 panel.update_time = time; // forces reload of panel attributes
180                                 radar_panel_modified = false;
181                         }
182                 }
183         }
184
185         if ( hud_panel_radar_temp_hidden )
186                 return;
187
188         HUD_Panel_LoadCvars();
189
190         float f = 0;
191
192         if (hud_panel_radar_maximized && !autocvar__hud_configure)
193         {
194                 panel_size = autocvar_hud_panel_radar_maximized_size;
195                 panel_size.x = bound(0.2, panel_size.x, 1) * vid_conwidth;
196                 panel_size.y = bound(0.2, panel_size.y, 1) * vid_conheight;
197                 panel_pos.x = (vid_conwidth - panel_size.x) / 2;
198                 panel_pos.y = (vid_conheight - panel_size.y) / 2;
199
200                 string panel_bg;
201                 panel_bg = strcat(hud_skin_path, "/border_default"); // always use the default border when maximized
202                 if(precache_pic(panel_bg) == "")
203                         panel_bg = "gfx/hud/default/border_default"; // fallback
204                 if(!radar_panel_modified && panel_bg != panel.current_panel_bg)
205                         radar_panel_modified = true;
206                 strcpy(panel.current_panel_bg, panel_bg);
207
208                 switch(hud_panel_radar_maximized_zoommode)
209                 {
210                         default:
211                         case 0:
212                                 f = current_zoomfraction;
213                                 break;
214                         case 1:
215                                 f = 1 - current_zoomfraction;
216                                 break;
217                         case 2:
218                                 f = 0;
219                                 break;
220                         case 3:
221                                 f = 1;
222                                 break;
223                 }
224
225                 switch(hud_panel_radar_maximized_rotation)
226                 {
227                         case 0:
228                                 teamradar_angle = view_angles.y - 90;
229                                 break;
230                         default:
231                                 teamradar_angle = 90 * hud_panel_radar_maximized_rotation;
232                                 break;
233                 }
234         }
235         if (!hud_panel_radar_maximized && !autocvar__hud_configure)
236         {
237                 switch(hud_panel_radar_zoommode)
238                 {
239                         default:
240                         case 0:
241                                 f = current_zoomfraction;
242                                 break;
243                         case 1:
244                                 f = 1 - current_zoomfraction;
245                                 break;
246                         case 2:
247                                 f = 0;
248                                 break;
249                         case 3:
250                                 f = 1;
251                                 break;
252                 }
253
254                 switch(hud_panel_radar_rotation)
255                 {
256                         case 0:
257                                 teamradar_angle = view_angles.y - 90;
258                                 break;
259                         default:
260                                 teamradar_angle = 90 * hud_panel_radar_rotation;
261                                 break;
262                 }
263         }
264
265         vector pos, mySize;
266         pos = panel_pos;
267         mySize = panel_size;
268
269         if (autocvar_hud_panel_radar_dynamichud)
270                 HUD_Scale_Enable();
271         else
272                 HUD_Scale_Disable();
273         HUD_Panel_DrawBg();
274         if(panel_bg_padding)
275         {
276                 pos += '1 1 0' * panel_bg_padding;
277                 mySize -= '2 2 0' * panel_bg_padding;
278         }
279
280         int color2;
281         float scale2d, normalsize, bigsize;
282
283         teamradar_origin2d = HUD_Shift(pos + 0.5 * mySize);
284         teamradar_size2d = mySize;
285
286         if(minimapname == "")
287                 return;
288
289         teamradar_loadcvars();
290
291         scale2d = vlen_maxnorm2d(mi_picmax - mi_picmin);
292         teamradar_size2d = HUD_Scale(mySize);
293
294         teamradar_extraclip_mins = teamradar_extraclip_maxs = '0 0 0'; // we always center
295
296         // pixels per world qu to match the teamradar_size2d_x range in the longest dimension
297         if((hud_panel_radar_rotation == 0 && !hud_panel_radar_maximized) || (hud_panel_radar_maximized_rotation == 0 && hud_panel_radar_maximized))
298         {
299                 // max-min distance must fit the radar in any rotation
300                 bigsize = vlen_minnorm2d(teamradar_size2d) * scale2d / (1.05 * vlen(vec2(mi_scale)));
301         }
302         else
303         {
304                 vector c0, c1, c2, c3, span;
305                 c0 = Rotate(mi_min, teamradar_angle * DEG2RAD);
306                 c1 = Rotate(mi_max, teamradar_angle * DEG2RAD);
307                 c2 = Rotate('1 0 0' * mi_min.x + '0 1 0' * mi_max.y, teamradar_angle * DEG2RAD);
308                 c3 = Rotate('1 0 0' * mi_max.x + '0 1 0' * mi_min.y, teamradar_angle * DEG2RAD);
309                 span = '0 0 0';
310                 span.x = max(c0_x, c1_x, c2_x, c3_x) - min(c0_x, c1_x, c2_x, c3_x);
311                 span.y = max(c0_y, c1_y, c2_y, c3_y) - min(c0_y, c1_y, c2_y, c3_y);
312
313                 // max-min distance must fit the radar in x=x, y=y
314                 bigsize = min(
315                         teamradar_size2d.x * scale2d / (1.05 * span.x),
316                         teamradar_size2d.y * scale2d / (1.05 * span.y)
317                 );
318         }
319
320         normalsize = vlen_maxnorm2d(teamradar_size2d) * scale2d / hud_panel_radar_scale;
321         if(bigsize > normalsize)
322                 normalsize = bigsize;
323
324         teamradar_size =
325                   f * bigsize
326                 + (1 - f) * normalsize;
327         teamradar_origin3d_in_texcoord = teamradar_3dcoord_to_texcoord(
328                   f * mi_center
329                 + (1 - f) * view_origin);
330
331         drawsetcliparea(
332                 pos.x,
333                 pos.y,
334                 mySize.x,
335                 mySize.y
336         );
337
338         draw_teamradar_background(hud_panel_radar_foreground_alpha);
339
340         IL_EACH(g_radarlinks, true, draw_teamradar_link(it.origin, it.velocity, it.team));
341
342         IL_EACH(g_radaricons, it.teamradar_icon, {
343                 if ( hud_panel_radar_mouse )
344                 if ( GetResourceAmount(it, RESOURCE_HEALTH) >= 0 )
345                 if ( it.team == myteam + 1 || gametype == MAPINFO_TYPE_RACE || !teamplay )
346                 {
347                         vector coord = teamradar_texcoord_to_2dcoord(teamradar_3dcoord_to_texcoord(it.origin));
348                         if(vdist((mousepos - coord), <, 8))
349                         {
350                                 vector brightcolor;
351                                 brightcolor_x = min(1,it.teamradar_color_x*1.5);
352                                 brightcolor_y = min(1,it.teamradar_color_y*1.5);
353                                 brightcolor_z = min(1,it.teamradar_color_z*1.5);
354                                 drawpic(coord - '8 8 0', "gfx/teamradar_icon_glow", '16 16 0', brightcolor, panel_fg_alpha, 0);
355                         }
356                 }
357                 entity icon = RadarIcons_from(it.teamradar_icon);
358                 draw_teamradar_icon(it.origin, icon, it, spritelookupcolor(it, icon.netname, it.teamradar_color), panel_fg_alpha);
359         });
360         AL_EACH(_entcs, e, it != NULL, {
361                 if (!it.m_entcs_private) continue;
362                 if (it.sv_entnum == current_player) continue;
363                 color2 = entcs_GetTeam(it.sv_entnum);
364                 draw_teamradar_player(it.origin, it.angles, Team_ColorRGB(color2));
365         });
366         draw_teamradar_player(entcs_receiver(current_player).origin, view_angles, '1 1 1');
367
368         drawresetcliparea();
369
370         if ( hud_panel_radar_mouse )
371         {
372                 string message = "Click to select teleport destination";
373
374                 if ( STAT(HEALTH) <= 0 )
375                 {
376                         message = "Click to select spawn location";
377                 }
378
379                 drawcolorcodedstring(pos + '0.5 0 0' * (mySize_x - stringwidth(message, true, hud_fontsize)) - '0 1 0' * hud_fontsize_y * 2,
380                                                          message, hud_fontsize, hud_panel_radar_foreground_alpha, DRAWFLAG_NORMAL);
381
382                 hud_panel_radar_bottom = pos_y + mySize_y + hud_fontsize_y;
383         }
384 }