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