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