forked from rosebudai/OpenRCT2-Web
Move Chat handling to UI project (#27080)
This commit is contained in:
1 parent
caad293b2d
commit
d99eb5dcf7
15 files changed
+409
-385
No files matched your search
@@ -15,6 +15,7 @@
|
||||
#include "WindowManager.h"
|
||||
#include "drawing/engines/DrawingEngineFactory.hpp"
|
||||
#include "input/ShortcutManager.h"
|
||||
#include "interface/Chat.h"
|
||||
#include "interface/InGameConsole.h"
|
||||
#include "interface/Theme.h"
|
||||
#include "scripting/UiExtensions.h"
|
||||
@@ -40,7 +41,6 @@
|
||||
#include <openrct2/drawing/IDrawingEngine.h>
|
||||
#include <openrct2/drawing/NewDrawing.h>
|
||||
#include <openrct2/drawing/RenderTarget.h>
|
||||
#include <openrct2/interface/Chat.h>
|
||||
#include <openrct2/interface/ScreenCoords.hpp>
|
||||
#include <openrct2/interface/Viewport.h>
|
||||
#include <openrct2/scenes/title/TitleSequencePlayer.h>
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "WindowManager.h"
|
||||
|
||||
#include "interface/Chat.h"
|
||||
#include "interface/FileBrowser.h"
|
||||
#include "interface/Theme.h"
|
||||
#include "interface/Window.h"
|
||||
@@ -582,6 +583,18 @@ public:
|
||||
case INTENT_ACTION_REFRESH_PLAYER_LIST:
|
||||
MultiplayerRefreshList();
|
||||
break;
|
||||
case INTENT_ACTION_INIT_CHAT:
|
||||
ChatInit();
|
||||
break;
|
||||
case INTENT_ACTION_UPDATE_CHAT:
|
||||
ChatUpdate();
|
||||
break;
|
||||
case INTENT_ACTION_CHAT_ADD_HISTORY:
|
||||
{
|
||||
auto message = intent.GetStringExtra(INTENT_EXTRA_MESSAGE);
|
||||
ChatAddHistory(message);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <openrct2-ui/UiContext.h>
|
||||
#include <openrct2-ui/input/MouseInput.h>
|
||||
#include <openrct2-ui/input/ShortcutManager.h>
|
||||
#include <openrct2-ui/interface/Chat.h>
|
||||
#include <openrct2-ui/interface/InGameConsole.h>
|
||||
#include <openrct2-ui/interface/Window.h>
|
||||
#include <openrct2-ui/windows/Windows.h>
|
||||
@@ -24,7 +25,6 @@
|
||||
#include <openrct2/Input.h>
|
||||
#include <openrct2/OpenRCT2.h>
|
||||
#include <openrct2/config/Config.h>
|
||||
#include <openrct2/interface/Chat.h>
|
||||
#include <openrct2/interface/Viewport.h>
|
||||
#include <openrct2/paint/VirtualFloor.h>
|
||||
#include <openrct2/ui/WindowManager.h>
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#include <functional>
|
||||
#include <openrct2-ui/UiContext.h>
|
||||
#include <openrct2-ui/interface/Chat.h>
|
||||
#include <openrct2-ui/interface/InGameConsole.h>
|
||||
#include <openrct2-ui/interface/Widget.h>
|
||||
#include <openrct2-ui/interface/Window.h>
|
||||
@@ -31,7 +32,6 @@
|
||||
#include <openrct2/config/Config.h>
|
||||
#include <openrct2/core/EnumUtils.hpp>
|
||||
#include <openrct2/drawing/Drawing.Screen.h>
|
||||
#include <openrct2/interface/Chat.h>
|
||||
#include <openrct2/interface/Screenshot.h>
|
||||
#include <openrct2/interface/Viewport.h>
|
||||
#include <openrct2/interface/WidgetIndexGlobals.h>
|
||||
|
||||
@@ -0,0 +1,324 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2026 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#include "Chat.h"
|
||||
|
||||
#include <openrct2/Context.h>
|
||||
#include <openrct2/Input.h>
|
||||
#include <openrct2/audio/Audio.h>
|
||||
#include <openrct2/audio/AudioMixer.h>
|
||||
#include <openrct2/core/UTF8.h>
|
||||
#include <openrct2/drawing/Drawing.String.h>
|
||||
#include <openrct2/drawing/FilterPaletteIds.h>
|
||||
#include <openrct2/drawing/NewDrawing.h>
|
||||
#include <openrct2/drawing/Rectangle.h>
|
||||
#include <openrct2/drawing/Text.h>
|
||||
#include <openrct2/interface/ColourWithFlags.h>
|
||||
#include <openrct2/interface/ScreenCoords.hpp>
|
||||
#include <openrct2/localisation/Formatting.h>
|
||||
#include <openrct2/network/Network.h>
|
||||
#include <openrct2/platform/Platform.h>
|
||||
|
||||
using namespace OpenRCT2::Audio;
|
||||
using namespace OpenRCT2::Drawing;
|
||||
|
||||
namespace OpenRCT2
|
||||
{
|
||||
bool gChatOpen = false;
|
||||
static u8string _chatCurrentLine;
|
||||
static std::deque<u8string> _chatHistory;
|
||||
static std::deque<uint32_t> _chatHistoryTime;
|
||||
static uint32_t _chatCaretTicks = 0;
|
||||
static int32_t _chatLeft;
|
||||
static int32_t _chatTop;
|
||||
static int32_t _chatRight;
|
||||
static int32_t _chatBottom;
|
||||
static int32_t _chatWidth;
|
||||
static int32_t _chatHeight;
|
||||
static TextInputSession* _chatTextInputSession;
|
||||
|
||||
static const u8string& ChatGetHistory(size_t index);
|
||||
static uint32_t ChatHistoryGetTime(size_t index);
|
||||
static void ChatClearInput();
|
||||
static int32_t ChatHistoryDrawString(RenderTarget& rt, const char* text, const ScreenCoordsXY& screenCoords, int32_t width);
|
||||
|
||||
bool ChatAvailable()
|
||||
{
|
||||
return Network::GetMode() != Network::Mode::none && Network::GetStatus() == Network::Status::connected
|
||||
&& Network::GetAuthstatus() == Network::Auth::ok;
|
||||
}
|
||||
|
||||
void ChatOpen()
|
||||
{
|
||||
gChatOpen = true;
|
||||
_chatTextInputSession = ContextStartTextInput(_chatCurrentLine, kChatMaxMessageLength);
|
||||
}
|
||||
|
||||
void ChatClose()
|
||||
{
|
||||
gChatOpen = false;
|
||||
ContextStopTextInput();
|
||||
}
|
||||
|
||||
void ChatToggle()
|
||||
{
|
||||
if (gChatOpen)
|
||||
{
|
||||
ChatClose();
|
||||
}
|
||||
else
|
||||
{
|
||||
ChatOpen();
|
||||
}
|
||||
}
|
||||
|
||||
void ChatInit()
|
||||
{
|
||||
_chatHistory.clear();
|
||||
_chatHistoryTime.clear();
|
||||
}
|
||||
|
||||
void ChatUpdate()
|
||||
{
|
||||
// Flash the caret
|
||||
_chatCaretTicks = (_chatCaretTicks + 1) % 30;
|
||||
}
|
||||
|
||||
void ChatDraw(RenderTarget& rt, ColourWithFlags chatBackgroundColor)
|
||||
{
|
||||
thread_local std::string lineBuffer;
|
||||
|
||||
if (!ChatAvailable())
|
||||
{
|
||||
gChatOpen = false;
|
||||
return;
|
||||
}
|
||||
|
||||
_chatLeft = 10;
|
||||
_chatRight = std::min<int16_t>((ContextGetWidth() - 10), kChatMaxWindowWidth);
|
||||
_chatWidth = _chatRight - _chatLeft;
|
||||
_chatBottom = ContextGetHeight() - 45;
|
||||
_chatTop = _chatBottom - 10;
|
||||
|
||||
const char* inputLine = _chatCurrentLine.c_str();
|
||||
int32_t inputLineHeight = 10;
|
||||
|
||||
// Draw chat window
|
||||
if (gChatOpen)
|
||||
{
|
||||
inputLineHeight = ChatStringWrappedGetHeight(inputLine, _chatWidth - 10);
|
||||
_chatTop -= inputLineHeight;
|
||||
|
||||
for (const auto& entry : _chatHistory)
|
||||
{
|
||||
if (entry.empty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
lineBuffer = entry;
|
||||
int32_t lineHeight = ChatStringWrappedGetHeight(lineBuffer, _chatWidth - 10);
|
||||
_chatTop -= (lineHeight + 5);
|
||||
}
|
||||
|
||||
_chatHeight = _chatBottom - _chatTop;
|
||||
|
||||
if (_chatTop < 50)
|
||||
{
|
||||
_chatTop = 50;
|
||||
}
|
||||
else if (_chatHeight < 150)
|
||||
{ // Min height
|
||||
_chatTop = _chatBottom - 150;
|
||||
_chatHeight = 150;
|
||||
}
|
||||
|
||||
ScreenCoordsXY topLeft{ _chatLeft, _chatTop };
|
||||
ScreenCoordsXY bottomRight{ _chatRight, _chatBottom };
|
||||
ScreenCoordsXY bottomLeft{ _chatLeft, _chatBottom };
|
||||
GfxSetDirtyBlocks(
|
||||
{ topLeft - ScreenCoordsXY{ 0, 5 }, bottomRight + ScreenCoordsXY{ 0, 5 } }); // Background area + Textbox
|
||||
Rectangle::filter(
|
||||
rt, { topLeft - ScreenCoordsXY{ 0, 5 }, bottomRight + ScreenCoordsXY{ 0, 5 } },
|
||||
FilterPaletteID::palette51); // Opaque grey background
|
||||
Rectangle::fillInset(
|
||||
rt, { topLeft - ScreenCoordsXY{ 0, 5 }, bottomRight + ScreenCoordsXY{ 0, 5 } }, chatBackgroundColor,
|
||||
Rectangle::BorderStyle::outset, Rectangle::FillBrightness::light, Rectangle::FillMode::none);
|
||||
Rectangle::fillInset(
|
||||
rt, { topLeft + ScreenCoordsXY{ 1, -4 }, bottomRight - ScreenCoordsXY{ 1, inputLineHeight + 6 } },
|
||||
chatBackgroundColor, Rectangle::BorderStyle::inset);
|
||||
Rectangle::fillInset(
|
||||
rt, { bottomLeft + ScreenCoordsXY{ 1, -inputLineHeight - 5 }, bottomRight + ScreenCoordsXY{ -1, 4 } },
|
||||
chatBackgroundColor,
|
||||
Rectangle::BorderStyle::inset); // Textbox
|
||||
}
|
||||
|
||||
auto screenCoords = ScreenCoordsXY{ _chatLeft + 5, _chatBottom - inputLineHeight - 20 };
|
||||
int32_t stringHeight = 0;
|
||||
|
||||
// Draw chat history
|
||||
for (size_t i = 0; i < kChatHistorySize; i++, screenCoords.y -= stringHeight)
|
||||
{
|
||||
if (i >= _chatHistory.size())
|
||||
break;
|
||||
|
||||
uint32_t expireTime = ChatHistoryGetTime(i) + 10000;
|
||||
if (!gChatOpen && Platform::GetTicks() > expireTime)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
lineBuffer = ChatGetHistory(i);
|
||||
auto lineCh = lineBuffer.c_str();
|
||||
stringHeight = ChatHistoryDrawString(rt, lineCh, screenCoords, _chatWidth - 10) + 5;
|
||||
GfxSetDirtyBlocks(
|
||||
{ { screenCoords - ScreenCoordsXY{ 0, stringHeight } }, { screenCoords + ScreenCoordsXY{ _chatWidth, 20 } } });
|
||||
|
||||
if ((screenCoords.y - stringHeight) < 50)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Draw current chat input
|
||||
if (gChatOpen)
|
||||
{
|
||||
lineBuffer.assign("{OUTLINE}{CELADON}");
|
||||
lineBuffer += _chatCurrentLine;
|
||||
|
||||
screenCoords.y = _chatBottom - inputLineHeight - 5;
|
||||
|
||||
inputLineHeight = drawTextWrapped(
|
||||
rt, screenCoords + ScreenCoordsXY{ 0, 3 }, _chatWidth - 10, lineBuffer, { OpenRCT2::Drawing::kColourNull });
|
||||
GfxSetDirtyBlocks({ screenCoords, { screenCoords + ScreenCoordsXY{ _chatWidth, inputLineHeight + 15 } } });
|
||||
|
||||
// TODO: Show caret if the input text has multiple lines
|
||||
if (_chatCaretTicks < 15 && getStringWidth(lineBuffer, FontStyle::medium) < (_chatWidth - 10))
|
||||
{
|
||||
lineBuffer.assign(_chatCurrentLine.c_str(), _chatTextInputSession->SelectionStart);
|
||||
int32_t caretX = screenCoords.x + getStringWidth(lineBuffer, FontStyle::medium);
|
||||
int32_t caretY = screenCoords.y + 14;
|
||||
|
||||
Rectangle::fill(rt, { { caretX, caretY }, { caretX + 6, caretY + 1 } }, PaletteIndex::yellow10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* strftime wrapper which appends to an existing string.
|
||||
*/
|
||||
static size_t StrCatFTime(char* buffer, size_t bufferSize, const char* format, const struct tm* tp)
|
||||
{
|
||||
size_t stringLen = strnlen(buffer, bufferSize);
|
||||
if (stringLen < bufferSize)
|
||||
{
|
||||
char* dst = buffer + stringLen;
|
||||
size_t dstMaxSize = bufferSize - stringLen;
|
||||
return strftime(dst, dstMaxSize, format, tp);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ChatAddHistory(std::string_view s)
|
||||
{
|
||||
// Format a timestamp
|
||||
time_t timer{};
|
||||
time(&timer);
|
||||
auto tmInfo = localtime(&timer);
|
||||
char timeBuffer[64]{};
|
||||
StrCatFTime(timeBuffer, sizeof(timeBuffer), "[%H:%M] ", tmInfo);
|
||||
|
||||
std::string buffer = timeBuffer;
|
||||
buffer += s;
|
||||
|
||||
if (_chatHistory.size() >= kChatHistorySize)
|
||||
{
|
||||
_chatHistory.pop_back();
|
||||
_chatHistoryTime.pop_back();
|
||||
}
|
||||
|
||||
_chatHistory.push_front(buffer);
|
||||
_chatHistoryTime.push_front(Platform::GetTicks());
|
||||
|
||||
// Log to file (src only as logging does its own timestamp)
|
||||
Network::AppendChatLog(s);
|
||||
|
||||
CreateAudioChannel(SoundId::newsItem, false, kMixerVolumeMax, 0.5f, 1.5f, true);
|
||||
}
|
||||
|
||||
void ChatInput(enum ChatInput input)
|
||||
{
|
||||
switch (input)
|
||||
{
|
||||
case ChatInput::send:
|
||||
if (!_chatCurrentLine.empty())
|
||||
{
|
||||
Network::SendChat(_chatCurrentLine.c_str());
|
||||
}
|
||||
ChatClearInput();
|
||||
ChatClose();
|
||||
break;
|
||||
case ChatInput::close:
|
||||
ChatClose();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static const u8string& ChatGetHistory(size_t index)
|
||||
{
|
||||
return _chatHistory[index];
|
||||
}
|
||||
|
||||
static uint32_t ChatHistoryGetTime(size_t index)
|
||||
{
|
||||
return _chatHistoryTime[index];
|
||||
}
|
||||
|
||||
static void ChatClearInput()
|
||||
{
|
||||
_chatCurrentLine.clear();
|
||||
}
|
||||
|
||||
// This method is the same as gfx_draw_string_left_wrapped.
|
||||
// But this adjusts the initial Y coordinate depending of the number of lines.
|
||||
static int32_t ChatHistoryDrawString(RenderTarget& rt, const char* text, const ScreenCoordsXY& screenCoords, int32_t width)
|
||||
{
|
||||
int32_t numLines;
|
||||
u8string wrappedString;
|
||||
wrapString(FormatString("{OUTLINE}{WHITE}{STRING}", text), width, FontStyle::medium, &wrappedString, &numLines);
|
||||
auto lineHeight = FontGetLineHeight(FontStyle::medium);
|
||||
|
||||
int32_t expectedY = screenCoords.y - (numLines * lineHeight);
|
||||
if (expectedY < 50)
|
||||
{
|
||||
return (numLines * lineHeight); // Skip drawing, return total height.
|
||||
}
|
||||
|
||||
const utf8* bufferPtr = wrappedString.data();
|
||||
int32_t lineY = screenCoords.y;
|
||||
for (int32_t line = 0; line <= numLines; ++line)
|
||||
{
|
||||
drawText(rt, { screenCoords.x, lineY - (numLines * lineHeight) }, bufferPtr, { OpenRCT2::Drawing::kColourNull });
|
||||
bufferPtr = GetStringEnd(bufferPtr) + 1;
|
||||
lineY += lineHeight;
|
||||
}
|
||||
return lineY - screenCoords.y;
|
||||
}
|
||||
|
||||
// Wrap string without drawing, useful to get the height of a wrapped string.
|
||||
// Almost the same as gfx_draw_string_left_wrapped
|
||||
int32_t ChatStringWrappedGetHeight(u8string_view args, int32_t width)
|
||||
{
|
||||
int32_t numLines;
|
||||
wrapString(FormatStringID(STR_STRING, args), width, FontStyle::medium, nullptr, &numLines);
|
||||
const int32_t lineHeight = FontGetLineHeight(FontStyle::medium);
|
||||
return lineHeight * (numLines + 1);
|
||||
}
|
||||
} // namespace OpenRCT2
|
||||
@@ -0,0 +1,51 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2026 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <openrct2/core/StringTypes.h>
|
||||
#include <string_view>
|
||||
|
||||
namespace OpenRCT2
|
||||
{
|
||||
struct ColourWithFlags;
|
||||
|
||||
constexpr int8_t kChatHistorySize = 10;
|
||||
constexpr int16_t kChatInputSize = 1024;
|
||||
constexpr uint8_t kChatMaxMessageLength = 200;
|
||||
constexpr int16_t kChatMaxWindowWidth = 600;
|
||||
|
||||
enum class ChatInput : uint8_t
|
||||
{
|
||||
none,
|
||||
send,
|
||||
close,
|
||||
};
|
||||
|
||||
extern bool gChatOpen;
|
||||
|
||||
namespace Drawing
|
||||
{
|
||||
struct RenderTarget;
|
||||
}
|
||||
|
||||
bool ChatAvailable();
|
||||
void ChatOpen();
|
||||
void ChatClose();
|
||||
void ChatToggle();
|
||||
|
||||
void ChatInit();
|
||||
void ChatUpdate();
|
||||
void ChatDraw(Drawing::RenderTarget& rt, ColourWithFlags chatBackgroundColour);
|
||||
|
||||
void ChatAddHistory(std::string_view s);
|
||||
void ChatInput(ChatInput input);
|
||||
|
||||
int32_t ChatStringWrappedGetHeight(u8string_view args, int32_t width);
|
||||
} // namespace OpenRCT2
|
||||
@@ -68,6 +68,7 @@
|
||||
<ClInclude Include="input\MouseInput.h" />
|
||||
<ClInclude Include="input\ShortcutIds.h" />
|
||||
<ClInclude Include="input\ShortcutManager.h" />
|
||||
<ClInclude Include="interface\Chat.h" />
|
||||
<ClInclude Include="interface\Dropdown.h" />
|
||||
<ClInclude Include="interface\Graph.h" />
|
||||
<ClInclude Include="interface\FileBrowser.h" />
|
||||
@@ -136,6 +137,7 @@
|
||||
<ClCompile Include="input\MouseInput.cpp" />
|
||||
<ClCompile Include="input\ShortcutInput.cpp" />
|
||||
<ClCompile Include="input\ShortcutManager.cpp" />
|
||||
<ClCompile Include="interface\Chat.cpp" />
|
||||
<ClCompile Include="interface\Graph.cpp" />
|
||||
<ClCompile Include="interface\FileBrowser.cpp" />
|
||||
<ClCompile Include="interface\InGameConsole.cpp" />
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "../interface/Theme.h"
|
||||
|
||||
#include <openrct2-ui/interface/Chat.h>
|
||||
#include <openrct2-ui/interface/Widget.h>
|
||||
#include <openrct2-ui/interface/Window.h>
|
||||
#include <openrct2-ui/windows/Windows.h>
|
||||
@@ -19,7 +20,6 @@
|
||||
#include <openrct2/config/Config.h>
|
||||
#include <openrct2/core/String.hpp>
|
||||
#include <openrct2/drawing/Text.h>
|
||||
#include <openrct2/interface/Chat.h>
|
||||
#include <openrct2/network/Network.h>
|
||||
#include <openrct2/ui/WindowManager.h>
|
||||
#include <openrct2/windows/Intent.h>
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "../interface/InGameConsole.h"
|
||||
#include "../scripting/CustomMenu.h"
|
||||
|
||||
#include <openrct2-ui/interface/Chat.h>
|
||||
#include <openrct2-ui/interface/Dropdown.h>
|
||||
#include <openrct2-ui/interface/Widget.h>
|
||||
#include <openrct2-ui/interface/Window.h>
|
||||
@@ -32,7 +33,6 @@
|
||||
#include <openrct2/core/String.hpp>
|
||||
#include <openrct2/drawing/Drawing.h>
|
||||
#include <openrct2/drawing/Text.h>
|
||||
#include <openrct2/interface/Chat.h>
|
||||
#include <openrct2/interface/ColourWithFlags.h>
|
||||
#include <openrct2/interface/Screenshot.h>
|
||||
#include <openrct2/interface/Viewport.h>
|
||||
|
||||
@@ -51,7 +51,6 @@
|
||||
#include "drawing/PickupPeep.h"
|
||||
#include "entity/EntityTweener.h"
|
||||
#include "entity/PatrolArea.h"
|
||||
#include "interface/Chat.h"
|
||||
#include "interface/StdInOutConsole.h"
|
||||
#include "interface/Viewport.h"
|
||||
#include "localisation/Formatter.h"
|
||||
@@ -470,7 +469,8 @@ namespace OpenRCT2
|
||||
Audio::gGameSoundsOff = !Config::Get().sound.masterSoundEnabled;
|
||||
}
|
||||
|
||||
ChatInit();
|
||||
auto intent = Intent(INTENT_ACTION_INIT_CHAT);
|
||||
_uiContext->GetWindowManager()->BroadcastIntent(intent);
|
||||
CopyOriginalUserFilesOver();
|
||||
|
||||
if (!gOpenRCT2NoGraphics)
|
||||
@@ -1411,7 +1411,8 @@ namespace OpenRCT2
|
||||
}
|
||||
#endif
|
||||
|
||||
ChatUpdate();
|
||||
auto intent = Intent(INTENT_ACTION_UPDATE_CHAT);
|
||||
_uiContext->GetWindowManager()->BroadcastIntent(intent);
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
if (auto* activeScene = _sceneManager->getActiveScene(); activeScene != _sceneManager->getPreloaderScene())
|
||||
{
|
||||
|
||||
@@ -1,322 +0,0 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2026 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#include "Chat.h"
|
||||
|
||||
#include "../Context.h"
|
||||
#include "../Input.h"
|
||||
#include "../audio/Audio.h"
|
||||
#include "../audio/AudioMixer.h"
|
||||
#include "../core/UTF8.h"
|
||||
#include "../drawing/Drawing.String.h"
|
||||
#include "../drawing/FilterPaletteIds.h"
|
||||
#include "../drawing/NewDrawing.h"
|
||||
#include "../drawing/Rectangle.h"
|
||||
#include "../drawing/Text.h"
|
||||
#include "../interface/ColourWithFlags.h"
|
||||
#include "../interface/ScreenCoords.hpp"
|
||||
#include "../localisation/Formatting.h"
|
||||
#include "../network/Network.h"
|
||||
#include "../platform/Platform.h"
|
||||
|
||||
using namespace OpenRCT2;
|
||||
using namespace OpenRCT2::Audio;
|
||||
using namespace OpenRCT2::Drawing;
|
||||
|
||||
bool gChatOpen = false;
|
||||
static u8string _chatCurrentLine;
|
||||
static std::deque<u8string> _chatHistory;
|
||||
static std::deque<uint32_t> _chatHistoryTime;
|
||||
static uint32_t _chatCaretTicks = 0;
|
||||
static int32_t _chatLeft;
|
||||
static int32_t _chatTop;
|
||||
static int32_t _chatRight;
|
||||
static int32_t _chatBottom;
|
||||
static int32_t _chatWidth;
|
||||
static int32_t _chatHeight;
|
||||
static TextInputSession* _chatTextInputSession;
|
||||
|
||||
static const u8string& ChatGetHistory(size_t index);
|
||||
static uint32_t ChatHistoryGetTime(size_t index);
|
||||
static void ChatClearInput();
|
||||
static int32_t ChatHistoryDrawString(RenderTarget& rt, const char* text, const ScreenCoordsXY& screenCoords, int32_t width);
|
||||
|
||||
bool ChatAvailable()
|
||||
{
|
||||
return Network::GetMode() != Network::Mode::none && Network::GetStatus() == Network::Status::connected
|
||||
&& Network::GetAuthstatus() == Network::Auth::ok;
|
||||
}
|
||||
|
||||
void ChatOpen()
|
||||
{
|
||||
gChatOpen = true;
|
||||
_chatTextInputSession = ContextStartTextInput(_chatCurrentLine, kChatMaxMessageLength);
|
||||
}
|
||||
|
||||
void ChatClose()
|
||||
{
|
||||
gChatOpen = false;
|
||||
ContextStopTextInput();
|
||||
}
|
||||
|
||||
void ChatToggle()
|
||||
{
|
||||
if (gChatOpen)
|
||||
{
|
||||
ChatClose();
|
||||
}
|
||||
else
|
||||
{
|
||||
ChatOpen();
|
||||
}
|
||||
}
|
||||
|
||||
void ChatInit()
|
||||
{
|
||||
_chatHistory.clear();
|
||||
_chatHistoryTime.clear();
|
||||
}
|
||||
|
||||
void ChatUpdate()
|
||||
{
|
||||
// Flash the caret
|
||||
_chatCaretTicks = (_chatCaretTicks + 1) % 30;
|
||||
}
|
||||
|
||||
void ChatDraw(RenderTarget& rt, ColourWithFlags chatBackgroundColor)
|
||||
{
|
||||
thread_local std::string lineBuffer;
|
||||
|
||||
if (!ChatAvailable())
|
||||
{
|
||||
gChatOpen = false;
|
||||
return;
|
||||
}
|
||||
|
||||
_chatLeft = 10;
|
||||
_chatRight = std::min<int16_t>((ContextGetWidth() - 10), kChatMaxWindowWidth);
|
||||
_chatWidth = _chatRight - _chatLeft;
|
||||
_chatBottom = ContextGetHeight() - 45;
|
||||
_chatTop = _chatBottom - 10;
|
||||
|
||||
const char* inputLine = _chatCurrentLine.c_str();
|
||||
int32_t inputLineHeight = 10;
|
||||
|
||||
// Draw chat window
|
||||
if (gChatOpen)
|
||||
{
|
||||
inputLineHeight = ChatStringWrappedGetHeight(inputLine, _chatWidth - 10);
|
||||
_chatTop -= inputLineHeight;
|
||||
|
||||
for (const auto& entry : _chatHistory)
|
||||
{
|
||||
if (entry.empty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
lineBuffer = entry;
|
||||
int32_t lineHeight = ChatStringWrappedGetHeight(lineBuffer, _chatWidth - 10);
|
||||
_chatTop -= (lineHeight + 5);
|
||||
}
|
||||
|
||||
_chatHeight = _chatBottom - _chatTop;
|
||||
|
||||
if (_chatTop < 50)
|
||||
{
|
||||
_chatTop = 50;
|
||||
}
|
||||
else if (_chatHeight < 150)
|
||||
{ // Min height
|
||||
_chatTop = _chatBottom - 150;
|
||||
_chatHeight = 150;
|
||||
}
|
||||
|
||||
ScreenCoordsXY topLeft{ _chatLeft, _chatTop };
|
||||
ScreenCoordsXY bottomRight{ _chatRight, _chatBottom };
|
||||
ScreenCoordsXY bottomLeft{ _chatLeft, _chatBottom };
|
||||
GfxSetDirtyBlocks(
|
||||
{ topLeft - ScreenCoordsXY{ 0, 5 }, bottomRight + ScreenCoordsXY{ 0, 5 } }); // Background area + Textbox
|
||||
Rectangle::filter(
|
||||
rt, { topLeft - ScreenCoordsXY{ 0, 5 }, bottomRight + ScreenCoordsXY{ 0, 5 } },
|
||||
FilterPaletteID::palette51); // Opaque grey background
|
||||
Rectangle::fillInset(
|
||||
rt, { topLeft - ScreenCoordsXY{ 0, 5 }, bottomRight + ScreenCoordsXY{ 0, 5 } }, chatBackgroundColor,
|
||||
Rectangle::BorderStyle::outset, Rectangle::FillBrightness::light, Rectangle::FillMode::none);
|
||||
Rectangle::fillInset(
|
||||
rt, { topLeft + ScreenCoordsXY{ 1, -4 }, bottomRight - ScreenCoordsXY{ 1, inputLineHeight + 6 } },
|
||||
chatBackgroundColor, Rectangle::BorderStyle::inset);
|
||||
Rectangle::fillInset(
|
||||
rt, { bottomLeft + ScreenCoordsXY{ 1, -inputLineHeight - 5 }, bottomRight + ScreenCoordsXY{ -1, 4 } },
|
||||
chatBackgroundColor,
|
||||
Rectangle::BorderStyle::inset); // Textbox
|
||||
}
|
||||
|
||||
auto screenCoords = ScreenCoordsXY{ _chatLeft + 5, _chatBottom - inputLineHeight - 20 };
|
||||
int32_t stringHeight = 0;
|
||||
|
||||
// Draw chat history
|
||||
for (size_t i = 0; i < kChatHistorySize; i++, screenCoords.y -= stringHeight)
|
||||
{
|
||||
if (i >= _chatHistory.size())
|
||||
break;
|
||||
|
||||
uint32_t expireTime = ChatHistoryGetTime(i) + 10000;
|
||||
if (!gChatOpen && Platform::GetTicks() > expireTime)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
lineBuffer = ChatGetHistory(i);
|
||||
auto lineCh = lineBuffer.c_str();
|
||||
stringHeight = ChatHistoryDrawString(rt, lineCh, screenCoords, _chatWidth - 10) + 5;
|
||||
GfxSetDirtyBlocks(
|
||||
{ { screenCoords - ScreenCoordsXY{ 0, stringHeight } }, { screenCoords + ScreenCoordsXY{ _chatWidth, 20 } } });
|
||||
|
||||
if ((screenCoords.y - stringHeight) < 50)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Draw current chat input
|
||||
if (gChatOpen)
|
||||
{
|
||||
lineBuffer.assign("{OUTLINE}{CELADON}");
|
||||
lineBuffer += _chatCurrentLine;
|
||||
|
||||
screenCoords.y = _chatBottom - inputLineHeight - 5;
|
||||
|
||||
inputLineHeight = drawTextWrapped(
|
||||
rt, screenCoords + ScreenCoordsXY{ 0, 3 }, _chatWidth - 10, lineBuffer, { OpenRCT2::Drawing::kColourNull });
|
||||
GfxSetDirtyBlocks({ screenCoords, { screenCoords + ScreenCoordsXY{ _chatWidth, inputLineHeight + 15 } } });
|
||||
|
||||
// TODO: Show caret if the input text has multiple lines
|
||||
if (_chatCaretTicks < 15 && getStringWidth(lineBuffer, FontStyle::medium) < (_chatWidth - 10))
|
||||
{
|
||||
lineBuffer.assign(_chatCurrentLine.c_str(), _chatTextInputSession->SelectionStart);
|
||||
int32_t caretX = screenCoords.x + getStringWidth(lineBuffer, FontStyle::medium);
|
||||
int32_t caretY = screenCoords.y + 14;
|
||||
|
||||
Rectangle::fill(rt, { { caretX, caretY }, { caretX + 6, caretY + 1 } }, PaletteIndex::yellow10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* strftime wrapper which appends to an existing string.
|
||||
*/
|
||||
static size_t StrCatFTime(char* buffer, size_t bufferSize, const char* format, const struct tm* tp)
|
||||
{
|
||||
size_t stringLen = strnlen(buffer, bufferSize);
|
||||
if (stringLen < bufferSize)
|
||||
{
|
||||
char* dst = buffer + stringLen;
|
||||
size_t dstMaxSize = bufferSize - stringLen;
|
||||
return strftime(dst, dstMaxSize, format, tp);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ChatAddHistory(std::string_view s)
|
||||
{
|
||||
// Format a timestamp
|
||||
time_t timer{};
|
||||
time(&timer);
|
||||
auto tmInfo = localtime(&timer);
|
||||
char timeBuffer[64]{};
|
||||
StrCatFTime(timeBuffer, sizeof(timeBuffer), "[%H:%M] ", tmInfo);
|
||||
|
||||
std::string buffer = timeBuffer;
|
||||
buffer += s;
|
||||
|
||||
if (_chatHistory.size() >= kChatHistorySize)
|
||||
{
|
||||
_chatHistory.pop_back();
|
||||
_chatHistoryTime.pop_back();
|
||||
}
|
||||
|
||||
_chatHistory.push_front(buffer);
|
||||
_chatHistoryTime.push_front(Platform::GetTicks());
|
||||
|
||||
// Log to file (src only as logging does its own timestamp)
|
||||
Network::AppendChatLog(s);
|
||||
|
||||
CreateAudioChannel(SoundId::newsItem, false, kMixerVolumeMax, 0.5f, 1.5f, true);
|
||||
}
|
||||
|
||||
void ChatInput(enum ChatInput input)
|
||||
{
|
||||
switch (input)
|
||||
{
|
||||
case ChatInput::send:
|
||||
if (!_chatCurrentLine.empty())
|
||||
{
|
||||
Network::SendChat(_chatCurrentLine.c_str());
|
||||
}
|
||||
ChatClearInput();
|
||||
ChatClose();
|
||||
break;
|
||||
case ChatInput::close:
|
||||
ChatClose();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static const u8string& ChatGetHistory(size_t index)
|
||||
{
|
||||
return _chatHistory[index];
|
||||
}
|
||||
|
||||
static uint32_t ChatHistoryGetTime(size_t index)
|
||||
{
|
||||
return _chatHistoryTime[index];
|
||||
}
|
||||
|
||||
static void ChatClearInput()
|
||||
{
|
||||
_chatCurrentLine.clear();
|
||||
}
|
||||
|
||||
// This method is the same as gfx_draw_string_left_wrapped.
|
||||
// But this adjusts the initial Y coordinate depending of the number of lines.
|
||||
static int32_t ChatHistoryDrawString(RenderTarget& rt, const char* text, const ScreenCoordsXY& screenCoords, int32_t width)
|
||||
{
|
||||
int32_t numLines;
|
||||
u8string wrappedString;
|
||||
wrapString(FormatString("{OUTLINE}{WHITE}{STRING}", text), width, FontStyle::medium, &wrappedString, &numLines);
|
||||
auto lineHeight = FontGetLineHeight(FontStyle::medium);
|
||||
|
||||
int32_t expectedY = screenCoords.y - (numLines * lineHeight);
|
||||
if (expectedY < 50)
|
||||
{
|
||||
return (numLines * lineHeight); // Skip drawing, return total height.
|
||||
}
|
||||
|
||||
const utf8* bufferPtr = wrappedString.data();
|
||||
int32_t lineY = screenCoords.y;
|
||||
for (int32_t line = 0; line <= numLines; ++line)
|
||||
{
|
||||
drawText(rt, { screenCoords.x, lineY - (numLines * lineHeight) }, bufferPtr, { OpenRCT2::Drawing::kColourNull });
|
||||
bufferPtr = GetStringEnd(bufferPtr) + 1;
|
||||
lineY += lineHeight;
|
||||
}
|
||||
return lineY - screenCoords.y;
|
||||
}
|
||||
|
||||
// Wrap string without drawing, useful to get the height of a wrapped string.
|
||||
// Almost the same as gfx_draw_string_left_wrapped
|
||||
int32_t ChatStringWrappedGetHeight(u8string_view args, int32_t width)
|
||||
{
|
||||
int32_t numLines;
|
||||
wrapString(FormatStringID(STR_STRING, args), width, FontStyle::medium, nullptr, &numLines);
|
||||
const int32_t lineHeight = FontGetLineHeight(FontStyle::medium);
|
||||
return lineHeight * (numLines + 1);
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2026 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../core/StringTypes.h"
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace OpenRCT2
|
||||
{
|
||||
struct ColourWithFlags;
|
||||
}
|
||||
|
||||
constexpr int8_t kChatHistorySize = 10;
|
||||
constexpr int16_t kChatInputSize = 1024;
|
||||
constexpr uint8_t kChatMaxMessageLength = 200;
|
||||
constexpr int16_t kChatMaxWindowWidth = 600;
|
||||
|
||||
enum class ChatInput : uint8_t
|
||||
{
|
||||
none,
|
||||
send,
|
||||
close,
|
||||
};
|
||||
|
||||
extern bool gChatOpen;
|
||||
|
||||
namespace OpenRCT2::Drawing
|
||||
{
|
||||
struct RenderTarget;
|
||||
}
|
||||
|
||||
bool ChatAvailable();
|
||||
void ChatOpen();
|
||||
void ChatClose();
|
||||
void ChatToggle();
|
||||
|
||||
void ChatInit();
|
||||
void ChatUpdate();
|
||||
void ChatDraw(OpenRCT2::Drawing::RenderTarget& rt, OpenRCT2::ColourWithFlags chatBackgroundColour);
|
||||
|
||||
void ChatAddHistory(std::string_view s);
|
||||
void ChatInput(ChatInput input);
|
||||
|
||||
int32_t ChatStringWrappedGetHeight(u8string_view args, int32_t width);
|
||||
@@ -301,7 +301,6 @@
|
||||
<ClInclude Include="GameStateSnapshots.h" />
|
||||
<ClInclude Include="Identifiers.h" />
|
||||
<ClInclude Include="Input.h" />
|
||||
<ClInclude Include="interface\Chat.h" />
|
||||
<ClInclude Include="interface\ColourWithFlags.h" />
|
||||
<ClInclude Include="interface\Cursors.h" />
|
||||
<ClInclude Include="interface\FontFamilies.h" />
|
||||
@@ -948,7 +947,6 @@
|
||||
<ClCompile Include="GameState.cpp" />
|
||||
<ClCompile Include="GameStateSnapshots.cpp" />
|
||||
<ClCompile Include="Input.cpp" />
|
||||
<ClCompile Include="interface\Chat.cpp" />
|
||||
<ClCompile Include="interface\ColourWithFlags.cpp" />
|
||||
<ClCompile Include="interface\Cursors.cpp" />
|
||||
<ClCompile Include="interface\FontFamilies.cpp" />
|
||||
|
||||
@@ -73,7 +73,6 @@ static constexpr uint32_t kMaxPacketsPerTick = 100;
|
||||
#include "../core/MemoryStream.h"
|
||||
#include "../core/Path.hpp"
|
||||
#include "../core/String.hpp"
|
||||
#include "../interface/Chat.h"
|
||||
#include "../object/ObjectManager.h"
|
||||
#include "../object/ObjectRepository.h"
|
||||
#include "../scenario/Scenario.h"
|
||||
@@ -104,6 +103,13 @@ namespace OpenRCT2::Network
|
||||
static u8string GetPrivateKeyPath(u8string_view playerName);
|
||||
static u8string GetPublicKeyPath(u8string_view playerName, u8string_view hash);
|
||||
|
||||
static void ChatAddHistory(const u8string& text)
|
||||
{
|
||||
auto intent = Intent(INTENT_ACTION_CHAT_ADD_HISTORY);
|
||||
intent.PutExtra(INTENT_EXTRA_MESSAGE, text);
|
||||
ContextBroadcastIntent(&intent);
|
||||
}
|
||||
|
||||
NetworkBase::NetworkBase(IContext& context)
|
||||
: System(context)
|
||||
{
|
||||
|
||||
@@ -58,6 +58,9 @@ namespace OpenRCT2
|
||||
INTENT_ACTION_REMOVE_PROVISIONAL_FOOTPATH,
|
||||
INTENT_ACTION_REMOVE_PROVISIONAL_TRACK_PIECE,
|
||||
INTENT_ACTION_REFRESH_PLAYER_LIST,
|
||||
INTENT_ACTION_INIT_CHAT,
|
||||
INTENT_ACTION_UPDATE_CHAT,
|
||||
INTENT_ACTION_CHAT_ADD_HISTORY,
|
||||
|
||||
INTENT_ACTION_NULL = 255,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user