2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <engine/graphics.h>
|
|
|
|
#include <engine/keys.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <engine/shared/config.h>
|
|
|
|
#include <engine/textrender.h>
|
2008-08-27 15:48:50 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <game/client/gameclient.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <game/generated/protocol.h>
|
2008-08-27 15:48:50 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
#include "motd.h"
|
2008-08-30 08:26:36 +00:00
|
|
|
|
2023-02-19 11:17:56 +00:00
|
|
|
CMotd::CMotd()
|
|
|
|
{
|
|
|
|
m_aServerMotd[0] = '\0';
|
|
|
|
m_ServerMotdTime = 0;
|
|
|
|
m_ServerMotdUpdateTime = 0;
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CMotd::Clear()
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_ServerMotdTime = 0;
|
2023-02-14 22:56:47 +00:00
|
|
|
Graphics()->DeleteQuadContainer(m_RectQuadContainer);
|
|
|
|
TextRender()->DeleteTextContainer(m_TextContainerIndex);
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 22:56:27 +00:00
|
|
|
bool CMotd::IsActive() const
|
2008-08-30 08:26:36 +00:00
|
|
|
{
|
2016-08-30 23:39:59 +00:00
|
|
|
return time() < m_ServerMotdTime;
|
2008-08-30 08:26:36 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CMotd::OnStateChange(int NewState, int OldState)
|
2009-06-15 09:46:25 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(OldState == IClient::STATE_ONLINE || OldState == IClient::STATE_OFFLINE)
|
|
|
|
Clear();
|
2009-06-15 09:46:25 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 22:56:47 +00:00
|
|
|
void CMotd::OnWindowResize()
|
|
|
|
{
|
|
|
|
Graphics()->DeleteQuadContainer(m_RectQuadContainer);
|
|
|
|
TextRender()->DeleteTextContainer(m_TextContainerIndex);
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CMotd::OnRender()
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!IsActive())
|
2008-08-30 08:26:36 +00:00
|
|
|
return;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2023-02-14 22:56:47 +00:00
|
|
|
const float FontSize = 32.0f; // also the size of the margin and rect rounding
|
|
|
|
const float ScreenHeight = 40.0f * FontSize; // multiple of the font size to get perfect alignment
|
|
|
|
const float ScreenWidth = ScreenHeight * Graphics()->ScreenAspect();
|
|
|
|
Graphics()->MapScreen(0.0f, 0.0f, ScreenWidth, ScreenHeight);
|
2008-08-30 08:26:36 +00:00
|
|
|
|
2023-02-14 22:56:47 +00:00
|
|
|
const float RectHeight = 26.0f * FontSize;
|
|
|
|
const float RectWidth = 630.0f + 2.0f * FontSize;
|
|
|
|
const float RectX = ScreenWidth / 2.0f - RectWidth / 2.0f;
|
|
|
|
const float RectY = 160.0f;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2023-02-14 22:56:47 +00:00
|
|
|
if(m_RectQuadContainer == -1)
|
|
|
|
{
|
|
|
|
Graphics()->SetColor(0.0f, 0.0f, 0.0f, 0.5f);
|
|
|
|
m_RectQuadContainer = Graphics()->CreateRectQuadContainer(RectX, RectY, RectWidth, RectHeight, FontSize, IGraphics::CORNER_ALL);
|
|
|
|
Graphics()->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
}
|
2008-08-27 15:48:50 +00:00
|
|
|
|
2023-02-14 22:56:47 +00:00
|
|
|
if(m_RectQuadContainer != -1)
|
|
|
|
Graphics()->RenderQuadContainer(m_RectQuadContainer, -1);
|
|
|
|
|
|
|
|
const float TextWidth = RectWidth - 2.0f * FontSize;
|
|
|
|
const float TextX = RectX + FontSize;
|
|
|
|
const float TextY = RectY + FontSize;
|
|
|
|
|
|
|
|
if(m_TextContainerIndex == -1)
|
|
|
|
{
|
|
|
|
CTextCursor Cursor;
|
|
|
|
TextRender()->SetCursor(&Cursor, TextX, TextY, FontSize, TEXTFLAG_RENDER);
|
|
|
|
Cursor.m_LineWidth = TextWidth;
|
|
|
|
TextRender()->CreateTextContainer(m_TextContainerIndex, &Cursor, ServerMotd());
|
|
|
|
}
|
2008-08-27 15:48:50 +00:00
|
|
|
|
2023-02-14 22:56:47 +00:00
|
|
|
if(m_TextContainerIndex != -1)
|
|
|
|
TextRender()->RenderTextContainer(m_TextContainerIndex, TextRender()->DefaultTextColor(), TextRender()->DefaultTextOutlineColor());
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CMotd::OnMessage(int MsgType, void *pRawMsg)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Client()->State() == IClient::STATE_DEMOPLAYBACK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(MsgType == NETMSGTYPE_SV_MOTD)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2023-02-14 22:56:47 +00:00
|
|
|
const CNetMsg_Sv_Motd *pMsg = static_cast<CNetMsg_Sv_Motd *>(pRawMsg);
|
2008-08-27 15:48:50 +00:00
|
|
|
|
2017-04-03 10:29:31 +00:00
|
|
|
// copy it manually to process all \n
|
|
|
|
const char *pMsgStr = pMsg->m_pMessage;
|
2023-02-14 22:56:47 +00:00
|
|
|
const size_t MotdLen = str_length(pMsgStr) + 1;
|
2017-04-03 10:29:31 +00:00
|
|
|
const char *pLast = m_aServerMotd; // for console printing
|
2023-02-14 22:56:47 +00:00
|
|
|
for(size_t i = 0, k = 0; i < MotdLen && k < sizeof(m_aServerMotd); i++, k++)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2017-04-03 10:29:31 +00:00
|
|
|
// handle incoming "\\n"
|
2020-09-26 19:41:58 +00:00
|
|
|
if(pMsgStr[i] == '\\' && pMsgStr[i + 1] == 'n')
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2017-04-03 10:29:31 +00:00
|
|
|
m_aServerMotd[k] = '\n';
|
|
|
|
i++; // skip the 'n'
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_aServerMotd[k] = pMsgStr[i];
|
|
|
|
|
|
|
|
// print the line to the console when receiving the newline character
|
|
|
|
if(g_Config.m_ClPrintMotd && m_aServerMotd[k] == '\n')
|
|
|
|
{
|
|
|
|
m_aServerMotd[k] = '\0';
|
2021-03-08 00:08:38 +00:00
|
|
|
m_pClient->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "motd", pLast, color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageHighlightColor)));
|
2017-04-03 10:29:31 +00:00
|
|
|
m_aServerMotd[k] = '\n';
|
2020-09-26 19:41:58 +00:00
|
|
|
pLast = m_aServerMotd + k + 1;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-03 10:29:31 +00:00
|
|
|
m_aServerMotd[sizeof(m_aServerMotd) - 1] = '\0';
|
|
|
|
if(g_Config.m_ClPrintMotd && *pLast != '\0')
|
2021-03-08 00:08:38 +00:00
|
|
|
m_pClient->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "motd", pLast, color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageHighlightColor)));
|
2008-08-27 15:48:50 +00:00
|
|
|
|
2023-02-19 11:17:56 +00:00
|
|
|
m_ServerMotdUpdateTime = time();
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_aServerMotd[0] && g_Config.m_ClMotdTime)
|
2023-02-19 11:17:56 +00:00
|
|
|
m_ServerMotdTime = m_ServerMotdUpdateTime + time_freq() * g_Config.m_ClMotdTime;
|
2008-08-27 15:48:50 +00:00
|
|
|
else
|
2010-05-29 07:25:38 +00:00
|
|
|
m_ServerMotdTime = 0;
|
2023-02-14 22:56:47 +00:00
|
|
|
TextRender()->DeleteTextContainer(m_TextContainerIndex);
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
bool CMotd::OnInput(IInput::CEvent Event)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
if(IsActive() && Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE)
|
2008-08-30 08:26:36 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
Clear();
|
2008-08-30 08:26:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-08-27 15:48:50 +00:00
|
|
|
return false;
|
|
|
|
}
|