]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/notify.qc
Purge autocvars.qh from the client-side codebase, cvars are defined in the headers...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / notify.qc
1 #include "notify.qh"
2
3 #include <client/draw.qh>
4
5 // Notifications (#4)
6
7 void HUD_Notify_Export(int fh)
8 {
9         // allow saving cvars that aesthetically change the panel into hud skin files
10         HUD_Write_Cvar("hud_panel_notify_flip");
11         HUD_Write_Cvar("hud_panel_notify_fontsize");
12         HUD_Write_Cvar("hud_panel_notify_time");
13         HUD_Write_Cvar("hud_panel_notify_fadetime");
14         HUD_Write_Cvar("hud_panel_notify_icon_aspect");
15 }
16
17 void HUD_Notify_Push(string icon, string attacker, string victim)
18 {
19         if (icon == "")
20                 return;
21
22         ++notify_count;
23         --notify_index;
24
25         if (notify_index == -1)
26                 notify_index = NOTIFY_MAX_ENTRIES-1;
27
28         // Free old strings
29         if (notify_attackers[notify_index])
30                 strunzone(notify_attackers[notify_index]);
31
32         if (notify_victims[notify_index])
33                 strunzone(notify_victims[notify_index]);
34
35         if (notify_icons[notify_index])
36                 strunzone(notify_icons[notify_index]);
37
38         // Allocate new strings
39         if (victim != "")
40         {
41                 notify_attackers[notify_index] = strzone(attacker);
42                 notify_victims[notify_index] = strzone(victim);
43         }
44         else
45         {
46                 // In case of a notification without a victim, the attacker
47                 // is displayed on the victim's side. Instead of special
48                 // treatment later on, we can simply switch them here.
49                 notify_attackers[notify_index] = string_null;
50                 notify_victims[notify_index] = strzone(attacker);
51         }
52
53         notify_icons[notify_index] = strzone(icon);
54         notify_times[notify_index] = time;
55 }
56
57 void HUD_Notify()
58 {
59         if (!autocvar__hud_configure)
60                 if (!autocvar_hud_panel_notify)
61                         return;
62
63         HUD_Panel_LoadCvars();
64
65         if (autocvar_hud_panel_notify_dynamichud)
66                 HUD_Scale_Enable();
67         else
68                 HUD_Scale_Disable();
69         HUD_Panel_DrawBg();
70
71         if (!autocvar__hud_configure)
72                 if (notify_count == 0)
73                         return;
74
75         vector pos, size;
76         pos  = panel_pos;
77         size = panel_size;
78
79         if (panel_bg_padding)
80         {
81                 pos  += '1 1 0' * panel_bg_padding;
82                 size -= '2 2 0' * panel_bg_padding;
83         }
84
85         float fade_start = max(0, autocvar_hud_panel_notify_time);
86         float fade_time = max(0, autocvar_hud_panel_notify_fadetime);
87         float icon_aspect = max(1, autocvar_hud_panel_notify_icon_aspect);
88
89         int entry_count = bound(1, floor(NOTIFY_MAX_ENTRIES * size.y / size.x), NOTIFY_MAX_ENTRIES);
90         float entry_height = size.y / entry_count;
91
92         float panel_width_half = size.x * 0.5;
93         float icon_width_half = entry_height * icon_aspect / 2;
94         float name_maxwidth = panel_width_half - icon_width_half - size.x * NOTIFY_ICON_MARGIN;
95
96         vector font_size = '0.5 0.5 0' * entry_height * autocvar_hud_panel_notify_fontsize;
97         vector icon_size = vec2(icon_aspect, 1) * entry_height;
98         vector icon_left = eX * (panel_width_half - icon_width_half);
99         vector attacker_right = eX * name_maxwidth;
100         vector victim_left = eX * (size.x - name_maxwidth);
101
102         vector attacker_pos, victim_pos, icon_pos;
103         string attacker, victim, icon;
104         int i, j, count, step, limit;
105         float alpha;
106
107         if (autocvar_hud_panel_notify_flip)
108         {
109                 // Order items from the top down
110                 i = 0;
111                 step = +1;
112                 limit = entry_count;
113         }
114         else
115         {
116                 // Order items from the bottom up
117                 i = entry_count - 1;
118                 step = -1;
119                 limit = -1;
120         }
121
122         for (j = notify_index, count = 0; i != limit; i += step, ++j, ++count)
123         {
124                 if(autocvar__hud_configure)
125                 {
126                         attacker = sprintf(_("Player %d"), count + 1);
127                         victim = sprintf(_("Player %d"), count + 2);
128                         icon = REGISTRY_GET(Weapons, min(WEP_FIRST + count * 2, WEP_LAST)).model2;
129                         alpha = bound(0, 1.2 - count / entry_count, 1);
130                 }
131                 else
132                 {
133                         if (j == NOTIFY_MAX_ENTRIES)
134                                 j = 0;
135
136                         if (notify_times[j] + fade_start > time)
137                                 alpha = 1;
138                         else if (fade_time != 0)
139                         {
140                                 alpha = bound(0, (notify_times[j] + fade_start + fade_time - time) / fade_time, 1);
141                                 if (alpha == 0)
142                                         break;
143                         }
144                         else
145                                 break;
146
147                         attacker = notify_attackers[j];
148                         victim = notify_victims[j];
149                         icon = notify_icons[j];
150                 }
151
152                 if (icon != "" && victim != "")
153                 {
154                         vector name_top = eY * (i * entry_height + 0.5 * (entry_height - font_size.y));
155
156                         icon_pos = pos + icon_left + eY * i * entry_height;
157                         drawpic_aspect_skin(icon_pos, icon, icon_size, '1 1 1', panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
158
159                         victim = textShortenToWidth(ColorTranslateRGB(victim), name_maxwidth, font_size, stringwidth_colors);
160                         victim_pos = pos + victim_left + name_top;
161                         drawcolorcodedstring(victim_pos, victim, font_size, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
162
163                         if (attacker != "")
164                         {
165                                 attacker = textShortenToWidth(ColorTranslateRGB(attacker), name_maxwidth, font_size, stringwidth_colors);
166                                 attacker_pos = pos + attacker_right - eX * stringwidth(attacker, true, font_size) + name_top;
167                                 drawcolorcodedstring(attacker_pos, attacker, font_size, panel_fg_alpha * alpha, DRAWFLAG_NORMAL);
168                         }
169                 }
170         }
171
172         notify_count = count;
173 }