
Code:
#ifdef __CLIENT
#include "PlayerData.h"
#include "resdata.h"
#include "DPClient.h"
extern CDPClient g_DPlay;
#endif
namespace AnnounceRenderer {
#ifdef __CLIENT
void CAnnounceRenderer::AddTopAnnounce(const std::string strAnnounce, const std::uint32_t nPlayerIndex) {
AnnounceData data;
auto pOldFont = g_Neuz.m_2DRender.GetFont();
g_Neuz.m_2DRender.SetFont(CWndBase::m_Theme.m_pFontText);
CEditString str;
str.SetParsingString(strAnnounce.c_str());
auto nRightPosition = g_Neuz.m_2DRender.m_clipRect.Width();
std::string character;
std::uint16_t index = 0;
for (auto const& it : strAnnounce) {
textData chardata;
chardata.m_char = it;
CEditString strEdit;
strEdit.SetParsingString(character.c_str());
chardata.m_nPositionX = nRightPosition + g_Neuz.m_2DRender.m_pFont->GetTextExtent_EditString(strEdit).cx;
data.m_strAnnounce.push_back(chardata);
character.push_back(it);
index++;
}
g_Neuz.m_2DRender.SetFont(pOldFont);
data.m_nPlayerCasterIndex = nPlayerIndex;
m_vTopAnnounces.push_back(data);
}
void CAnnounceRenderer::AddCenterAnnounce(const std::string strAnnounce, const std::uint32_t nPlayerIndex) {
AnnounceData data;
auto pOldFont = g_Neuz.m_2DRender.GetFont();
g_Neuz.m_2DRender.SetFont(CWndBase::m_Theme.m_pFontText);
CEditString str;
str.SetParsingString(strAnnounce.c_str());
const std::uint16_t nRectWidth = 450;
int pointX = (g_Neuz.m_2DRender.m_clipRect.Width() / 2) - (nRectWidth / 2);
//int pointX = (g_Neuz.m_2DRender.m_clipRect.Width() / 2) - (g_Neuz.m_2DRender.m_pFont->GetTextExtent_EditString(str).cx / 2);
auto nRightPosition = pointX + nRectWidth;
std::string character;
std::uint16_t index = 0;
for (auto const& it : strAnnounce) {
textData chardata;
chardata.m_char = it;
CEditString strEdit;
strEdit.SetParsingString(character.c_str());
chardata.m_nPositionX = nRightPosition + g_Neuz.m_2DRender.m_pFont->GetTextExtent_EditString(strEdit).cx;
data.m_strAnnounce.push_back(chardata);
character.push_back(it);
index++;
}
g_Neuz.m_2DRender.SetFont(pOldFont);
data.m_nPlayerCasterIndex = nPlayerIndex;
m_vCenteredAnnounces.push_back(data);
}
bool CAnnounceRenderer::OnRightClickUp(UINT nFlags, CPoint point)
{
if (g_pPlayer == nullptr) return false;
if (m_vTopAnnounces.empty()) return false;
auto itAnnounce = m_vTopAnnounces.begin();
if (itAnnounce->m_strAnnounce.empty()) return false;
auto nLeftPoint = itAnnounce->m_strAnnounce.front().m_nPositionX;
auto nRightPoint = itAnnounce->m_strAnnounce.back().m_nPositionX;
CRect rect = CRect(nLeftPoint, 5, nRightPoint, 25);
if (rect.PtInRect(point))
{
if (itAnnounce->m_nPlayerCasterIndex == 0) return false;
if (itAnnounce->m_nPlayerCasterIndex == g_pPlayer->m_idPlayer)
{
g_WndMng.PutString("You cannot send messages to your own character !", nullptr, 0xFFFF0000);
return false;
}
CWndMessage* pWndMessage = g_WndMng.OpenMessage(CPlayerDataCenter::GetInstance()->GetPlayerString(itAnnounce->m_nPlayerCasterIndex));
return true;
}
return false;
}
void CAnnounceRenderer::ProcessAnnounce(std::vector<AnnounceData>& vAnnounce) {
if (vAnnounce.empty()) return;
__time64_t tTimeLeft;
__time64_t tCurrentTick = GetTickCount();
auto itAnnounce = vAnnounce.begin();
if (itAnnounce->m_tDelay == 0) return;
tTimeLeft = itAnnounce->m_tDelay - tCurrentTick;
if (tTimeLeft < 1000)
{
itAnnounce->m_nAlpha -= ((DOUBLE)tTimeLeft * 0.001);
}
else if (REMOVE_DELAY - tTimeLeft <= 500)
{
itAnnounce->m_nAlpha -= ((DOUBLE)tTimeLeft * 0.002);
}
if (itAnnounce->m_nAlpha <= 0)
vAnnounce.erase(itAnnounce);
}
BOOL CAnnounceRenderer::Process()
{
ProcessAnnounce(m_vCenteredAnnounces);
ProcessAnnounce(m_vTopAnnounces);
return true;
}
void CAnnounceRenderer::RenderRectangles(C2DRender* p2DRender, const CRect &drawRect, const std::uint32_t nAlpha, const std::uint32_t nOffset)
{
/** Left of Rect **/
p2DRender->RenderFillRect(CRect(drawRect.left - nOffset, drawRect.top, drawRect.left, drawRect.bottom), D3DCOLOR_ARGB(0, 0, 0, 0), D3DCOLOR_ARGB(nAlpha, 0, 0, 0), D3DCOLOR_ARGB(0, 0, 0, 0), D3DCOLOR_ARGB(nAlpha, 0, 0, 0));
/** Center of Rect **/
p2DRender->RenderFillRect(CRect(drawRect.left, drawRect.top, drawRect.right, drawRect.bottom), D3DCOLOR_ARGB(nAlpha, 0, 0, 0), D3DCOLOR_ARGB(nAlpha, 0, 0, 0), D3DCOLOR_ARGB(nAlpha, 0, 0, 0), D3DCOLOR_ARGB(nAlpha, 0, 0, 0));
/** Right of rect **/
p2DRender->RenderFillRect(CRect(drawRect.right, drawRect.top, drawRect.right + nOffset, drawRect.bottom), D3DCOLOR_ARGB(nAlpha, 0, 0, 0), D3DCOLOR_ARGB(0, 0, 0, 0), D3DCOLOR_ARGB(nAlpha, 0, 0, 0), D3DCOLOR_ARGB(0, 0, 0, 0));
p2DRender->RenderFillRect(CRect(drawRect.left - nOffset, drawRect.top - 2, drawRect.left, drawRect.top), D3DCOLOR_ARGB(0, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(0, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77));
p2DRender->RenderFillRect(CRect(drawRect.left, drawRect.top - 2, drawRect.right, drawRect.top), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77));
p2DRender->RenderFillRect(CRect(drawRect.right, drawRect.top - 2, drawRect.right + nOffset, drawRect.top), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(0, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(0, 246, 207, 77));
p2DRender->RenderFillRect(CRect(drawRect.left - nOffset, drawRect.bottom, drawRect.left, drawRect.bottom + 2), D3DCOLOR_ARGB(0, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(0, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77));
p2DRender->RenderFillRect(CRect(drawRect.left, drawRect.bottom, drawRect.right, drawRect.bottom + 2), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77));
p2DRender->RenderFillRect(CRect(drawRect.right, drawRect.bottom, drawRect.right + nOffset, drawRect.bottom + 2), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(0, 246, 207, 77), D3DCOLOR_ARGB(nAlpha, 246, 207, 77), D3DCOLOR_ARGB(0, 246, 207, 77));
}
void CAnnounceRenderer::RenderTopAnnounces(C2DRender* p2DRender) {
CPoint point = CPoint(50, 5);
if (m_vTopAnnounces.empty()) return;
auto itAnnounce = m_vTopAnnounces.begin();
RenderRectangles(p2DRender, CRect(point.x, point.y - 5, g_Neuz.m_2DRender.m_clipRect.Width() - 50, point.y + 20), itAnnounce->m_nAlpha, 50);
for (auto itLetter = itAnnounce->m_strAnnounce.begin(); itLetter != itAnnounce->m_strAnnounce.end();)
{
if (itLetter->m_nPositionX <= g_Neuz.m_2DRender.m_clipRect.Width())
{
std::string strLetter;
strLetter.push_back(itLetter->m_char);
CEditString string;
string.SetParsingString(strLetter.c_str(), D3DCOLOR_ARGB(255, 254, 254, 254));
p2DRender->TextOut_EditString(itLetter->m_nPositionX, point.y, string);
}
itLetter->m_nPositionX -= 2; // 2 before
if (itLetter->m_nPositionX <= 0)
{
itLetter->m_nPositionX += g_Neuz.m_2DRender.m_clipRect.Width();
itLetter->m_nRoundsCount++;
if (itLetter->m_nRoundsCount >= MAX_ROUNDS_TOP)
itLetter = itAnnounce->m_strAnnounce.erase(itLetter);
else
++itLetter;
}
else
++itLetter;
}
if (itAnnounce->m_strAnnounce.empty()) {
if (itAnnounce->m_tDelay == 0) {
if (std::distance(itAnnounce, m_vTopAnnounces.end()) == 1)
itAnnounce->m_tDelay = GetTickCount() + REMOVE_DELAY;
else
m_vTopAnnounces.erase(itAnnounce);
}
}
}
void CAnnounceRenderer::RenderCenterAnnounces(C2DRender* p2DRender) {
auto offSetY = (184 * p2DRender->m_clipRect.Height() / 768) - 120;
if (m_vCenteredAnnounces.empty()) return;
auto itAnnounce = m_vCenteredAnnounces.begin();
std::string strFull;
for (auto const [character, position, count] : itAnnounce->m_strAnnounce) {
strFull.push_back(character);
}
CEditString str;
str.SetParsingString(strFull.c_str());
const std::uint16_t nRectWidth = 450;
CPoint point = CPoint((p2DRender->m_clipRect.Width() / 2) - (nRectWidth / 2), offSetY);
RenderRectangles(p2DRender, CRect(point.x, point.y - 5, point.x + nRectWidth, point.y + 20), itAnnounce->m_nAlpha, 50);
for (auto itLetter = itAnnounce->m_strAnnounce.begin(); itLetter != itAnnounce->m_strAnnounce.end();)
{
if (itLetter->m_nPositionX <= point.x + nRectWidth && itLetter->m_nPositionX >= point.x)
{
std::string strLetter;
strLetter.push_back(itLetter->m_char);
CEditString string;
string.SetParsingString(strLetter.c_str(), D3DCOLOR_ARGB(255, 254, 254, 254));
p2DRender->TextOut_EditString(itLetter->m_nPositionX, point.y, string);
}
itLetter->m_nPositionX -= 2; // 2 before
if (std::distance(itLetter, itAnnounce->m_strAnnounce.end()) == 1 && itLetter->m_nPositionX <= point.x)
{
itAnnounce->m_strAnnounce.clear();
break;
}
else
++itLetter;
}
if (itAnnounce->m_strAnnounce.empty()) {
if (itAnnounce->m_tDelay == 0) {
if( std::distance(itAnnounce, m_vCenteredAnnounces.end()) == 1)
itAnnounce->m_tDelay = GetTickCount() + REMOVE_DELAY;
else
m_vCenteredAnnounces.erase(itAnnounce);
}
}
}
void CAnnounceRenderer::RenderAnnounces(C2DRender* p2DRender) {
auto pOldFont = p2DRender->GetFont();
p2DRender->SetFont(CWndBase::m_Theme.m_pFontText);
RenderCenterAnnounces(p2DRender);
RenderTopAnnounces(p2DRender);
p2DRender->SetFont(pOldFont);
}
/********************/
void CWndAnnouncement::SetUsedItem(const std::uint32_t nItemId) {
m_nItemId = nItemId;
}
BOOL CWndAnnouncement::OnChildNotify(UINT message, UINT nID, LRESULT* pLResult)
{
switch (nID)
{
case WIDC_BUTTON:
std::string str = m_pWndEdit->GetString();
if (str.length() < MAX_ANNOUNCE_SIZE)
{
g_DPlay.SendAnnouncement(str, m_nItemId);
Destroy();
}
else
{
CString strError;
strError.Format("Your announcement should not exceed %d name !", MAX_ANNOUNCE_SIZE);
g_WndMng.OpenMessageBox(strError);
}
break;
break;
}
return CWndNeuz::OnChildNotify(message, nID, pLResult);
}
void CWndAnnouncement::OnInitialUpdate()
{
CWndNeuz::OnInitialUpdate();
m_pWndEdit = (CWndEdit*)GetDlgItem(WIDC_EDIT);
MoveParentCenter();
}
BOOL CWndAnnouncement::Initialize(CWndBase* pWndParent, DWORD dwWndId)
{
return CWndNeuz::InitDialog(g_Neuz.GetSafeHwnd(), APP_ANNOUNCE, 0, CPoint(0, 0), pWndParent);
}
#endif
}
#ifdef __CLIENT
AnnounceRenderer::CAnnounceRenderer g_AnnounceRenderer = AnnounceRenderer::CAnnounceRenderer();
#endif






