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