From 469c82babeeb8e1f78c1ba057c3c7699be82ce18 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Sat, 26 Oct 2024 22:38:54 +0200 Subject: [PATCH] Client/Chatbox: Make message disappear over time --- include/ClientLib/Chatbox.hpp | 13 +++++- include/CommonLib/GameConstants.hpp | 2 + src/ClientLib/Chatbox.cpp | 63 +++++++++++++++++++++-------- src/Game/States/GameState.cpp | 1 + 4 files changed, 62 insertions(+), 17 deletions(-) diff --git a/include/ClientLib/Chatbox.hpp b/include/ClientLib/Chatbox.hpp index 1a9f2e94..046588e7 100644 --- a/include/ClientLib/Chatbox.hpp +++ b/include/ClientLib/Chatbox.hpp @@ -8,6 +8,7 @@ #define TSOM_CLIENTLIB_CHATBOX_HPP #include +#include #include #include @@ -53,6 +54,8 @@ namespace tsom void SendMessage(); void SetFocus(); + void Update(); + Chatbox& operator=(const Chatbox&) = delete; Chatbox& operator=(Chatbox&&) = delete; @@ -62,10 +65,18 @@ namespace tsom void Layout() override; void Refresh(); - std::vector> m_chatLines; + struct Entry + { + std::vector items; + Nz::Timestamp disappearTime; + }; + + std::vector m_chatEntries; + Nz::BaseWidget* m_chatboxBackground; Nz::RichTextAreaWidget* m_chatboxHistory; Nz::ScrollAreaWidget* m_chatboxScrollArea; Nz::TextAreaWidget* m_chatEnteringBox; + Nz::Timestamp m_nextDisappearTime; }; } diff --git a/include/CommonLib/GameConstants.hpp b/include/CommonLib/GameConstants.hpp index 1fa6e9fd..def51dec 100644 --- a/include/CommonLib/GameConstants.hpp +++ b/include/CommonLib/GameConstants.hpp @@ -7,6 +7,7 @@ #ifndef TSOM_COMMONLIB_GAMECONSTANTS_HPP #define TSOM_COMMONLIB_GAMECONSTANTS_HPP +#include #include #include #include @@ -18,6 +19,7 @@ namespace tsom::Constants constexpr std::size_t ChatMaxLines = 100; constexpr std::size_t ChatMaxMessageLength = 1024; constexpr std::size_t ChatMaxPlayerMessageLength = 256; + constexpr Nz::Time ChatPlayerMessageDisplayTime = Nz::Time::Seconds(60); // Player constants constexpr std::size_t PlayerMaxNicknameLength = 16; diff --git a/src/ClientLib/Chatbox.cpp b/src/ClientLib/Chatbox.cpp index a34c5c5b..7ca9658b 100644 --- a/src/ClientLib/Chatbox.cpp +++ b/src/ClientLib/Chatbox.cpp @@ -21,17 +21,18 @@ namespace tsom //Nz::FontRef chatboxFont = Nz::FontLibrary::Get("BW_Chatbox"); //assert(chatboxFont); + m_chatboxBackground = Add(); + m_chatboxBackground->SetBackgroundColor(Nz::Color(0.f, 0.f, 0.f, 0.5f)); + m_chatboxHistory = Add(); - m_chatboxHistory->EnableBackground(false); m_chatboxHistory->EnableLineWrap(true); - m_chatboxHistory->SetBackgroundColor(Nz::Color(0.f, 0.f, 0.f, 0.5f)); m_chatboxHistory->SetCharacterSize(22); m_chatboxHistory->SetTextColor(Nz::Color::White()); m_chatboxHistory->SetTextOutlineColor(Nz::Color::Black()); m_chatboxHistory->SetTextOutlineThickness(1.f); m_chatboxHistory->SetReadOnly(true); - m_chatboxScrollArea = Add(m_chatboxHistory); + m_chatboxScrollArea = m_chatboxBackground->Add(m_chatboxHistory); m_chatboxScrollArea->Resize({ 480.f, 0.f }); m_chatboxScrollArea->EnableScrollbar(false); @@ -42,11 +43,13 @@ namespace tsom //m_chatEnteringBox->SetTextFont(chatboxFont); m_chatEnteringBox->Hide(); m_chatEnteringBox->SetMaximumTextLength(Constants::ChatMaxPlayerMessageLength); + + Refresh(); } void Chatbox::Clear() { - m_chatLines.clear(); + m_chatEntries.clear(); m_chatboxHistory->Clear(); } @@ -66,26 +69,36 @@ namespace tsom { if (shouldOpen) { - m_chatboxHistory->EnableBackground(true); + m_chatboxBackground->EnableBackground(true); m_chatboxScrollArea->EnableScrollbar(true); m_chatEnteringBox->Show(); m_chatEnteringBox->SetFocus(); } else { - m_chatboxHistory->EnableBackground(false); + m_chatboxBackground->EnableBackground(false); m_chatboxScrollArea->EnableScrollbar(false); m_chatEnteringBox->Clear(); m_chatEnteringBox->Hide(); } + + Refresh(); } } - void Chatbox::PrintMessage(std::vector message) + void Chatbox::PrintMessage(std::vector items) { - m_chatLines.emplace_back(std::move(message)); - if (m_chatLines.size() > Constants::ChatMaxLines) - m_chatLines.erase(m_chatLines.begin()); + return PrintMessage(items, Constants::ChatPlayerMessageDisplayTime); + } + + void Chatbox::PrintMessage(std::vector items, Nz::Time disappearTime) + { + if (m_chatEntries.size() >= Constants::ChatMaxLines) + m_chatEntries.erase(m_chatEntries.begin()); + + auto& chatEntry = m_chatEntries.emplace_back(); + chatEntry.disappearTime = Nz::Timestamp::Now() + disappearTime; + chatEntry.items = std::move(items); Refresh(); } @@ -102,6 +115,13 @@ namespace tsom m_chatEnteringBox->SetFocus(); } + void Chatbox::Update() + { + Nz::Timestamp now = Nz::Timestamp::Now(); + if (now > m_nextDisappearTime) + Refresh(); + } + void Chatbox::Layout() { BaseWidget::Layout(); @@ -110,16 +130,28 @@ namespace tsom m_chatEnteringBox->Resize({ size.x, m_chatEnteringBox->GetPreferredHeight() }); m_chatEnteringBox->SetPosition({ 0.f, 0.f, 0.f }); - m_chatboxScrollArea->Resize({ size.x / 3.f, size.y / 3.f }); - m_chatboxScrollArea->SetPosition({ 5.f, m_chatEnteringBox->GetPosition().y + m_chatEnteringBox->GetHeight() + 5.f, 0.f }); + m_chatboxBackground->Resize({ size.x / 3.f, size.y / 3.f }); + m_chatboxBackground->SetPosition({ 5.f, m_chatEnteringBox->GetPosition().y + m_chatEnteringBox->GetHeight() + 5.f, 0.f }); } void Chatbox::Refresh() { + Nz::Timestamp now = Nz::Timestamp::Now(); + + m_nextDisappearTime = Nz::Timestamp::FromNanoseconds(Nz::MaxValue()); //< max timestamp + m_chatboxHistory->Clear(); - for (const auto& lineItems : m_chatLines) + for (const auto& entry : m_chatEntries) { - for (const Item& lineItem : lineItems) + if (now > entry.disappearTime) + { + if (!IsOpen()) + continue; + } + else + m_nextDisappearTime = std::min(m_nextDisappearTime, entry.disappearTime); + + for (const Item& lineItem : entry.items) { std::visit([&](auto&& item) { @@ -145,8 +177,7 @@ namespace tsom } m_chatboxHistory->Resize({ m_chatboxHistory->GetWidth(), m_chatboxHistory->GetPreferredHeight() }); - m_chatboxScrollArea->Resize(m_chatboxScrollArea->GetSize()); // force layout update - m_chatboxScrollArea->SetPosition({ 5.f, m_chatEnteringBox->GetPosition().y + m_chatEnteringBox->GetHeight() + 5.f, 0.f}); + m_chatboxScrollArea->Resize({ m_chatboxBackground->GetWidth(), std::min(m_chatboxBackground->GetHeight(), m_chatboxHistory->GetHeight()) }); m_chatboxScrollArea->ScrollToRatio(1.f); } diff --git a/src/Game/States/GameState.cpp b/src/Game/States/GameState.cpp index d6e34289..c3ea5514 100644 --- a/src/Game/States/GameState.cpp +++ b/src/Game/States/GameState.cpp @@ -662,6 +662,7 @@ namespace tsom return true; m_timerManager.Update(elapsedTime); + m_chatBox->Update(); if (m_debugOverlay) m_debugOverlay->textDrawer.Clear();