1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#include "important_alert.h"
4
5#include <base/log.h>
6#include <base/time.h>
7
8#include <engine/client.h>
9#include <engine/graphics.h>
10#include <engine/shared/config.h>
11#include <engine/textrender.h>
12
13#include <generated/protocol.h>
14
15#include <game/client/components/menus.h>
16#include <game/client/gameclient.h>
17#include <game/localization.h>
18
19#include <algorithm>
20
21inline constexpr LOG_COLOR IMPORTANT_ALERT_COLOR{.r: 255, .g: 0, .b: 0};
22inline constexpr float MINIMUM_ACTIVE_SECONDS = 5.0f;
23
24void CImportantAlert::OnReset()
25{
26 m_Active = false;
27 m_DismissTouchRect.reset();
28 DeleteTextContainers();
29}
30
31void CImportantAlert::OnWindowResize()
32{
33 DeleteTextContainers();
34}
35
36void CImportantAlert::OnRender()
37{
38 if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK)
39 {
40 return;
41 }
42 if(!m_Active)
43 {
44 return;
45 }
46#if defined(CONF_VIDEORECORDER)
47 if(IVideo::Current() && !g_Config.m_ClVideoShowImportantAlerts)
48 {
49 return;
50 }
51#endif
52 RenderImportantAlert();
53}
54
55void CImportantAlert::DeleteTextContainers()
56{
57 TextRender()->DeleteTextContainer(TextContainerIndex&: m_TitleTextContainerIndex);
58 TextRender()->DeleteTextContainer(TextContainerIndex&: m_MessageTextContainerIndex);
59 TextRender()->DeleteTextContainer(TextContainerIndex&: m_CloseHintTextContainerIndex);
60}
61
62void CImportantAlert::RenderImportantAlert()
63{
64 if(m_FadeOutSince >= 0.0f && Client()->GlobalTime() - m_FadeOutSince >= 1.0f)
65 {
66 OnReset();
67 return;
68 }
69
70 const float Seconds = SecondsActive();
71
72 const float Height = 300.0f;
73 const float Width = Height * Graphics()->ScreenAspect();
74 Graphics()->MapScreenToSize(Width, Height);
75
76 const float TitleFontSize = 20.0f;
77 const float MessageFontSize = 16.0f;
78 const float CloseHintFontSize = 6.0f;
79 const float Alpha = m_FadeOutSince >= 0.0f ? 1.0f - (Client()->GlobalTime() - m_FadeOutSince) : 1.0f;
80
81 if(!m_TitleTextContainerIndex.Valid())
82 {
83 CTextCursor Cursor;
84 Cursor.m_FontSize = TitleFontSize;
85 Cursor.m_LineWidth = Width;
86 TextRender()->CreateTextContainer(TextContainerIndex&: m_TitleTextContainerIndex, pCursor: &Cursor, pText: m_aTitleText);
87 }
88 if(m_TitleTextContainerIndex.Valid())
89 {
90 TextRender()->RenderTextContainer(TextContainerIndex: m_TitleTextContainerIndex,
91 TextColor: ColorRGBA(1.0f, 0.0f, 0.0f, Alpha),
92 TextOutlineColor: TextRender()->DefaultTextOutlineColor().WithMultipliedAlpha(alpha: Alpha),
93 X: Width / 2.0f - TextRender()->GetBoundingBoxTextContainer(TextContainerIndex: m_TitleTextContainerIndex).m_W / 2.0f,
94 Y: 40.0f);
95 }
96
97 if(!m_MessageTextContainerIndex.Valid())
98 {
99 CTextCursor Cursor;
100 Cursor.m_FontSize = MessageFontSize;
101 Cursor.m_LineWidth = Width;
102 TextRender()->CreateTextContainer(TextContainerIndex&: m_MessageTextContainerIndex, pCursor: &Cursor, pText: m_aMessageText);
103 }
104 if(m_MessageTextContainerIndex.Valid())
105 {
106 TextRender()->RenderTextContainer(TextContainerIndex: m_MessageTextContainerIndex,
107 TextColor: TextRender()->DefaultTextColor().WithMultipliedAlpha(alpha: Alpha),
108 TextOutlineColor: TextRender()->DefaultTextOutlineColor().WithMultipliedAlpha(alpha: Alpha),
109 X: Width / 2.0f - TextRender()->GetBoundingBoxTextContainer(TextContainerIndex: m_MessageTextContainerIndex).m_W / 2.0f, Y: 40.0f + TitleFontSize + 10.0f);
110 }
111
112 if(Seconds > MINIMUM_ACTIVE_SECONDS)
113 {
114 const float CloseAlpha = Alpha * (Seconds < (MINIMUM_ACTIVE_SECONDS + 1.0f) ? (Seconds - MINIMUM_ACTIVE_SECONDS) : 1.0f);
115 const float CloseHintY = 40.0f - CloseHintFontSize - 2.0f;
116 const bool TouchActive = g_Config.m_ClTouchControls != 0;
117 if(m_CloseHintTextContainerIndex.Valid() && m_CloseHintShownForTouch != TouchActive)
118 {
119 TextRender()->DeleteTextContainer(TextContainerIndex&: m_CloseHintTextContainerIndex);
120 }
121 if(!m_CloseHintTextContainerIndex.Valid())
122 {
123 CTextCursor Cursor;
124 Cursor.m_FontSize = CloseHintFontSize;
125 Cursor.m_LineWidth = Width;
126 TextRender()->CreateTextContainer(TextContainerIndex&: m_CloseHintTextContainerIndex, pCursor: &Cursor,
127 pText: TouchActive ? Localize(pStr: "Tap to dismiss…", pContext: "Important alert message") : Localize(pStr: "Press Esc or Tab to dismiss…", pContext: "Important alert message"));
128 m_CloseHintShownForTouch = TouchActive;
129 }
130 if(m_CloseHintTextContainerIndex.Valid())
131 {
132 TextRender()->RenderTextContainer(TextContainerIndex: m_CloseHintTextContainerIndex,
133 TextColor: TextRender()->DefaultTextColor().WithMultipliedAlpha(alpha: CloseAlpha),
134 TextOutlineColor: TextRender()->DefaultTextOutlineColor().WithMultipliedAlpha(alpha: CloseAlpha),
135 X: Width / 2.0f - TextRender()->GetBoundingBoxTextContainer(TextContainerIndex: m_CloseHintTextContainerIndex).m_W / 2.0f, Y: CloseHintY);
136 }
137 if(TouchActive)
138 {
139 // The dismiss touch area covers the hint, title and message text with some padding.
140 const float TouchPadding = 10.0f;
141 float TextWidth = 0.0f;
142 float TextBottom = 40.0f;
143 if(m_TitleTextContainerIndex.Valid())
144 {
145 const STextBoundingBox BoundingBox = TextRender()->GetBoundingBoxTextContainer(TextContainerIndex: m_TitleTextContainerIndex);
146 TextWidth = BoundingBox.m_W;
147 TextBottom = 40.0f + BoundingBox.m_H;
148 }
149 if(m_MessageTextContainerIndex.Valid())
150 {
151 const STextBoundingBox BoundingBox = TextRender()->GetBoundingBoxTextContainer(TextContainerIndex: m_MessageTextContainerIndex);
152 TextWidth = std::max(a: TextWidth, b: BoundingBox.m_W);
153 TextBottom = 40.0f + TitleFontSize + 10.0f + BoundingBox.m_H;
154 }
155 m_DismissTouchRect = CUIRect{
156 .x: (Width / 2.0f - TextWidth / 2.0f - TouchPadding) / Width,
157 .y: (CloseHintY - TouchPadding) / Height,
158 .w: (TextWidth + 2.0f * TouchPadding) / Width,
159 .h: (TextBottom - CloseHintY + 2.0f * TouchPadding) / Height};
160 }
161 }
162}
163
164void CImportantAlert::DoImportantAlert(const char *pTitle, const char *pLogGroup, const char *pMessage)
165{
166 str_copy(dst&: m_aTitleText, src: pTitle);
167 str_copy(dst&: m_aMessageText, src: pMessage);
168 m_Active = true;
169 m_ActiveSince = Client()->GlobalTime();
170 m_ActiveSinceNanos = time_get_nanoseconds();
171 m_FadeOutSince = -1.0f;
172 m_DismissTouchRect.reset();
173 DeleteTextContainers();
174
175 char aLine[sizeof(m_aMessageText)];
176 while((pMessage = str_next_token(str: pMessage, delim: "\n", buffer: aLine, buffer_size: sizeof(aLine))))
177 {
178 if(aLine[0] != '\0')
179 {
180 log_info_color(IMPORTANT_ALERT_COLOR, pLogGroup, "%s", aLine);
181 }
182 }
183
184 if(Client()->State() != IClient::STATE_DEMOPLAYBACK)
185 {
186 Client()->Notify(pTitle: m_aTitleText, pMessage: m_aMessageText);
187 }
188}
189
190float CImportantAlert::SecondsActive() const
191{
192 return Client()->GlobalTime() - m_ActiveSince;
193}
194
195void CImportantAlert::OnMessage(int MsgType, void *pRawMsg)
196{
197 if(MsgType == NETMSGTYPE_SV_SERVERALERT)
198 {
199 const CNetMsg_Sv_ServerAlert *pMsg = static_cast<const CNetMsg_Sv_ServerAlert *>(pRawMsg);
200 DoImportantAlert(pTitle: Localize(pStr: "Server alert"), pLogGroup: "server_alert", pMessage: pMsg->m_pMessage);
201 }
202 else if(MsgType == NETMSGTYPE_SV_MODERATORALERT)
203 {
204 const CNetMsg_Sv_ModeratorAlert *pMsg = static_cast<const CNetMsg_Sv_ModeratorAlert *>(pRawMsg);
205 DoImportantAlert(pTitle: Localize(pStr: "Moderator alert"), pLogGroup: "moderator_alert", pMessage: pMsg->m_pMessage);
206 }
207}
208
209bool CImportantAlert::OnInput(const IInput::CEvent &Event)
210{
211 if(IsActive() &&
212 !GameClient()->m_Menus.IsActive() &&
213 SecondsActive() >= MINIMUM_ACTIVE_SECONDS &&
214 m_FadeOutSince < 0.0f &&
215 (Event.m_Flags & IInput::FLAG_PRESS) != 0 &&
216 (Event.m_Flags & IInput::FLAG_REPEAT) == 0 &&
217 (Event.m_Key == KEY_ESCAPE || Event.m_Key == KEY_TAB))
218 {
219 m_FadeOutSince = Client()->GlobalTime();
220 return true;
221 }
222 return false;
223}
224
225bool CImportantAlert::OnTouchState(std::vector<IInput::CTouchFingerState> &vTouchFingerStates)
226{
227 // Remove the finger that dismissed the alert so it does not activate other components until it is released.
228 if(m_DismissTouchFinger.has_value())
229 {
230 const auto DismissFingerState = std::find_if(first: vTouchFingerStates.begin(), last: vTouchFingerStates.end(), pred: [&](const IInput::CTouchFingerState &State) {
231 return State.m_Finger == *m_DismissTouchFinger;
232 });
233 if(DismissFingerState == vTouchFingerStates.end())
234 {
235 m_DismissTouchFinger.reset();
236 }
237 else
238 {
239 vTouchFingerStates.erase(position: DismissFingerState);
240 }
241 }
242
243 if(!g_Config.m_ClTouchControls ||
244 !IsActive() ||
245 SecondsActive() < MINIMUM_ACTIVE_SECONDS ||
246 m_FadeOutSince >= 0.0f ||
247 GameClient()->m_Chat.IsActive() ||
248 GameClient()->m_GameConsole.IsActive() ||
249 GameClient()->m_Menus.IsActive() ||
250 GameClient()->m_Emoticon.IsActive() ||
251 GameClient()->m_Spectator.IsActive())
252 {
253 return false;
254 }
255
256 if(!m_DismissTouchRect.has_value())
257 {
258 return false;
259 }
260 // Only fingers pressed down after the alert appeared can dismiss it.
261 const auto DismissFinger = std::find_if(first: vTouchFingerStates.begin(), last: vTouchFingerStates.end(), pred: [&](const IInput::CTouchFingerState &State) {
262 return State.m_PressTime > m_ActiveSinceNanos && m_DismissTouchRect->Inside(Point: State.m_Position);
263 });
264 if(DismissFinger == vTouchFingerStates.end())
265 {
266 return false;
267 }
268 m_FadeOutSince = Client()->GlobalTime();
269 m_DismissTouchFinger = DismissFinger->m_Finger;
270 vTouchFingerStates.erase(position: DismissFinger);
271 return false;
272}
273
274bool CImportantAlert::IsActive() const
275{
276 return m_Active;
277}
278