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