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 "motd.h"
4
5#include <base/color.h>
6#include <base/log.h>
7#include <base/log_color.h>
8#include <base/time.h>
9
10#include <engine/graphics.h>
11#include <engine/keys.h>
12#include <engine/shared/config.h>
13#include <engine/textrender.h>
14
15#include <generated/protocol.h>
16
17#include <game/client/components/important_alert.h>
18#include <game/client/gameclient.h>
19
20#include <algorithm>
21#include <chrono>
22
23CMotd::CMotd()
24{
25 m_aServerMotd[0] = '\0';
26 m_ServerMotdTime = 0;
27 m_ServerMotdUpdateTime = 0;
28}
29
30void CMotd::Clear()
31{
32 m_ServerMotdTime = 0;
33 m_TouchRect.reset();
34 Graphics()->DeleteQuadContainer(ContainerIndex&: m_RectQuadContainer);
35 TextRender()->DeleteTextContainer(TextContainerIndex&: m_TextContainerIndex);
36}
37
38bool CMotd::IsActive() const
39{
40 return time() < m_ServerMotdTime;
41}
42
43void CMotd::OnStateChange(int NewState, int OldState)
44{
45 if(OldState == IClient::STATE_ONLINE || OldState == IClient::STATE_OFFLINE)
46 Clear();
47}
48
49void CMotd::OnWindowResize()
50{
51 Graphics()->DeleteQuadContainer(ContainerIndex&: m_RectQuadContainer);
52 TextRender()->DeleteTextContainer(TextContainerIndex&: m_TextContainerIndex);
53}
54
55void CMotd::OnRender()
56{
57 if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK)
58 return;
59
60 if(!IsActive())
61 return;
62
63 if(GameClient()->m_ImportantAlert.IsActive())
64 {
65 Clear();
66 return;
67 }
68
69 const int MaxLines = 24;
70 const float FontSize = 32.0f; // also the size of the margin and rect rounding
71 const float ScreenHeight = 40.0f * FontSize; // multiple of the font size to get perfect alignment
72 const float ScreenWidth = ScreenHeight * Graphics()->ScreenAspect();
73 Graphics()->MapScreenToSize(Width: ScreenWidth, Height: ScreenHeight);
74
75 const float RectHeight = (MaxLines + 2) * FontSize;
76 const float RectWidth = 630.0f + 2.0f * FontSize;
77 const float RectX = ScreenWidth / 2.0f - RectWidth / 2.0f;
78 const float RectY = 160.0f;
79 m_TouchRect = CUIRect{.x: RectX / ScreenWidth, .y: RectY / ScreenHeight, .w: RectWidth / ScreenWidth, .h: RectHeight / ScreenHeight};
80
81 if(m_RectQuadContainer == -1)
82 {
83 Graphics()->SetColor(r: 0.0f, g: 0.0f, b: 0.0f, a: 0.5f);
84 m_RectQuadContainer = Graphics()->CreateRectQuadContainer(x: RectX, y: RectY, w: RectWidth, h: RectHeight, r: FontSize, Corners: IGraphics::CORNER_ALL);
85 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
86 }
87
88 if(m_RectQuadContainer != -1)
89 {
90 Graphics()->TextureClear();
91 Graphics()->RenderQuadContainer(ContainerIndex: m_RectQuadContainer, QuadDrawNum: -1);
92 }
93
94 if(!m_TextContainerIndex.Valid())
95 {
96 CTextCursor Cursor;
97 Cursor.SetPosition(vec2(RectX + FontSize, RectY + FontSize));
98 // TODO: Set TEXTFLAG_ELLIPSIS_AT_END when https://github.com/ddnet/ddnet/issues/11419 is fixed
99 // Cursor.m_Flags |= TEXTFLAG_ELLIPSIS_AT_END;
100 Cursor.m_FontSize = FontSize;
101 Cursor.m_LineWidth = RectWidth - 2.0f * FontSize;
102 Cursor.m_MaxLines = MaxLines;
103 TextRender()->CreateTextContainer(TextContainerIndex&: m_TextContainerIndex, pCursor: &Cursor, pText: ServerMotd());
104 }
105
106 if(m_TextContainerIndex.Valid())
107 TextRender()->RenderTextContainer(TextContainerIndex: m_TextContainerIndex, TextColor: TextRender()->DefaultTextColor(), TextOutlineColor: TextRender()->DefaultTextOutlineColor());
108}
109
110void CMotd::OnMessage(int MsgType, void *pRawMsg)
111{
112 if(Client()->State() == IClient::STATE_DEMOPLAYBACK)
113 return;
114
115 if(MsgType == NETMSGTYPE_SV_MOTD)
116 {
117 const CNetMsg_Sv_Motd *pMsg = static_cast<CNetMsg_Sv_Motd *>(pRawMsg);
118
119 // copy it manually to process all \n
120 const char *pMsgStr = pMsg->m_pMessage;
121 const size_t MotdLen = str_length(str: pMsgStr) + 1;
122 const char *pLast = m_aServerMotd; // for console printing
123 const LOG_COLOR LogColor = color_cast<LOG_COLOR>(rgb: color_cast<ColorRGBA>(hsl: ColorHSLA(g_Config.m_ClMessageHighlightColor)));
124 for(size_t i = 0, k = 0; i < MotdLen && k < sizeof(m_aServerMotd); i++, k++)
125 {
126 // handle incoming "\\n"
127 if(pMsgStr[i] == '\\' && pMsgStr[i + 1] == 'n')
128 {
129 m_aServerMotd[k] = '\n';
130 i++; // skip the 'n'
131 }
132 else
133 {
134 m_aServerMotd[k] = pMsgStr[i];
135 }
136
137 // print the line to the console when receiving the newline character
138 if(g_Config.m_ClPrintMotd && m_aServerMotd[k] == '\n')
139 {
140 m_aServerMotd[k] = '\0';
141 log_info_color(LogColor, "motd", "%s", pLast);
142 m_aServerMotd[k] = '\n';
143 pLast = m_aServerMotd + k + 1;
144 }
145 }
146 m_aServerMotd[sizeof(m_aServerMotd) - 1] = '\0';
147 if(g_Config.m_ClPrintMotd && *pLast != '\0')
148 {
149 log_info_color(LogColor, "motd", "%s", pLast);
150 }
151
152 if(!IsActive())
153 m_ShownSince = time_get_nanoseconds();
154 m_ServerMotdUpdateTime = time();
155 if(m_aServerMotd[0] && g_Config.m_ClMotdTime)
156 m_ServerMotdTime = m_ServerMotdUpdateTime + time_freq() * g_Config.m_ClMotdTime;
157 else
158 m_ServerMotdTime = 0;
159 TextRender()->DeleteTextContainer(TextContainerIndex&: m_TextContainerIndex);
160 }
161}
162
163bool CMotd::OnInput(const IInput::CEvent &Event)
164{
165 if(IsActive() && Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE)
166 {
167 Clear();
168 return true;
169 }
170 return false;
171}
172
173bool CMotd::OnTouchState(std::vector<IInput::CTouchFingerState> &vTouchFingerStates)
174{
175 // Remove the finger that dismissed the MOTD so it does not activate other components until it is released.
176 if(m_DismissTouchFinger.has_value())
177 {
178 const auto DismissFingerState = std::find_if(first: vTouchFingerStates.begin(), last: vTouchFingerStates.end(), pred: [&](const IInput::CTouchFingerState &State) {
179 return State.m_Finger == *m_DismissTouchFinger;
180 });
181 if(DismissFingerState == vTouchFingerStates.end())
182 m_DismissTouchFinger.reset();
183 else
184 vTouchFingerStates.erase(position: DismissFingerState);
185 }
186
187 if(!IsActive())
188 return false;
189 if(GameClient()->m_Chat.IsActive() ||
190 GameClient()->m_GameConsole.IsActive() ||
191 GameClient()->m_Menus.IsActive() ||
192 GameClient()->m_Emoticon.IsActive() ||
193 GameClient()->m_Spectator.IsActive())
194 {
195 return false;
196 }
197
198 if(!m_TouchRect.has_value())
199 return false;
200 // Only fingers pressed down after the MOTD appeared and outside of its rect dismiss it.
201 const auto DismissFinger = std::find_if(first: vTouchFingerStates.begin(), last: vTouchFingerStates.end(), pred: [&](const IInput::CTouchFingerState &State) {
202 return State.m_PressTime > m_ShownSince && !m_TouchRect->Inside(Point: State.m_Position);
203 });
204 if(DismissFinger == vTouchFingerStates.end())
205 return false;
206 Clear();
207 m_DismissTouchFinger = DismissFinger->m_Finger;
208 vTouchFingerStates.erase(position: DismissFinger);
209 return false;
210}
211