ddnet/src/game/client/components/chat.h

174 lines
4.6 KiB
C
Raw Normal View History

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
#ifndef GAME_CLIENT_COMPONENTS_CHAT_H
#define GAME_CLIENT_COMPONENTS_CHAT_H
#include <vector>
2022-05-29 16:33:38 +00:00
#include <engine/console.h>
2020-10-13 20:08:52 +00:00
#include <engine/shared/config.h>
2022-06-16 14:35:08 +00:00
#include <engine/shared/protocol.h>
2011-03-20 15:17:06 +00:00
#include <engine/shared/ringbuffer.h>
2010-05-29 07:25:38 +00:00
#include <game/client/component.h>
#include <game/client/lineinput.h>
2021-07-12 09:29:59 +00:00
#include <game/client/skin.h>
2010-05-29 07:25:38 +00:00
class CChat : public CComponent
{
Port line input and IME support from 0.7 Port the line input (UI edit boxes, chat, console) and Input Method Editor (IME) support from upstream. Closes #4397. General ------------------------------ Fix issues with the text input. Closes #4346. Closes #4524. Word skipping (when holding Ctrl) is overhauled to be consistent with the Windows / Firefox experience that I took as reference. Improve usability by not blinking (i.e. always rendering) the caret shortly after is has been moved. UI text input ------------------------------ Fix inconsistent mouse-based left and right scrolling (closes #4347). Support smooth left and right scrolling. Chat ------------------------------ Support keyboard-based text selection of the chat input. Mouse-based selection could be support in the future when we decide to add something like an ingame UI cursor. Support smooth up and down scrolling of the chat input, removing the old hack that offsets the input string to simulate scrolling. Console ------------------------------ Also support mouse-based text selection of the command input. Only text from either the command input or the console log can be selected at the same time. This ensures that Ctrl+C will always copy the text that is currently visually selected in the console. Check for Ctrl+C input event in event handler instead of in render function, to hopefully fix the issue that copying does not work sometimes (closes #5974 until further notice). When Ctrl+C is used to copy text from the console log, the selection is cleared. This should make it more clear when text was copied from the log. Fix an issue that was preventing the console log selection from being cleared, when all log lines are selected. Remove Ctrl+A/E hotkeys that move cursor to beginning/end respectively. Ctrl+A now selectes all text like for all other inputs. Home and End keys can still be used to go the beginning and end. Remove Ctrl+U/K hotkeys that clear everything before/after the cursor respectively. Hold shift and use Home/End to select everything instead. IME support ------------------------------ Render list of IME candidates in the client on Windows, so the candidate list can also be viewed in fullscreen mode. There is no API available to retrieve a candidate list on the other operating systems. Improve composition rendering by underlining the composition text instead of putting it in square brackets. Track active input globally to properly activate and deactivate IME through the SDL functions. Closes #1030. Closes #1008. Password rendering ------------------------------ Fix rendering of passwords containing unicode. Instead of rendering one star character for each UTF-8 `char`, render on star for every unicode codepoint. Show the composition text also for passwords. Without seeing the composition text it's hard to type a password containing those characters. The candidate window exposes the composition anyway. If you don't want to expose your password this way, e.g. while streaming, you could: 1. Use a latin password and switch off the IME for the password input with the IME hotkey. 2. Blank your screen with an external program while you are streaming and entering passwords. 3. Use binds to authenticate in rcon or to set the server browser password. Refactoring ------------------------------ Move all text input logic and general rendering to `CLineInput`. A `CLineInput` is associated with a particular `char` buffer given as a pointer either in the constructor or with `SetBuffer`. The maximum byte size of the buffer must also be specified. The maximum length in unicode codepoints can also be specified separately (e.g. on upstream, name are limited by the number of unicode codepoints instead). Add `CLineInputBuffered`, which is a `CLineInput` that own a `char` buffer of a fixed size, which is specified as a template argument. As `CLineInput` does not own a buffer anymore, this reduces duplicate code for line inputs that need their own buffer. Add `CLineInputNumber` which has additional convenience functions to consider the text as an `int` or `float`, to reduce duplicate code in those cases. In the future we could also add an input filter function so that only numbers can be entered in the number input. Add `CLineInput::SetClipboardLineCallback` to handle the case that multiple lines of text are pasted into a lineinput. This reduces duplicate code, as this behavior was previously implemented separately for chat and console. The behavior is also fixed to be consistent with the console on Windows, so the first line being pasted edits the current input text and then sends it instead of being sent on its own without the existing input text. Add `CalcFontSizeAndBoundingBox` to UI to reduce duplicate code. Expose `CalcAlignedCursorPos` as static member function to reuse it for line input. Dispatch input events to UI inputs through the event handler instead of storing them in a duplicate buffer. Use `size_t` for line input cursor position, length etc. and for `str_utf8_stats`. Add `IButtonColorFunction` to UI to describe a functions that defines colors for the Default, Active and Hovered states of UI elements. Add some default button color functions. Use button color function to reduce duplicate code in scrollbar rendering. Use `vec2` instead of two `floats` to represent the mouse positions in the text renderer. Remove `CaretPosition` again, as it does not calculate the correct Y position near line breaks due to the wrapping being different when not rendering the entire string. Instead, calculate the exact caret position when rending a text container and store the caret position in the text cursor for later use. IME usage guide (Windows) ------------------------------ 1. Install the respective language and the Microsoft-IME keyboard (e.g. for Chinese, Japanese or Korean). 2. Launch the game (or a text editor to first try out the IME). Note that Windows may track the input language separately for every application. You can change this in the Windows input settings so the input language is changed globally. 2. Switch the input language using the hotkey Windows+Space or another hotkey that you configured in the Windows input settings (Alt+Shift is the default, but you should consider disabling it, to avoid accidentally changing the input language while playing). 3. Switch from Latin/English input mode to the respective asian input mode. - Chinese: Use Ctrl+Space to switch between English and Chinese input mode. You can change this hotkey in the IME's settings. - Japanese: Use Ctrl+Space to switch between Alphanumeric and Hiragana/Katakana input mode. You can change this hotkey in the IME's settings. - Korean: Use Right Alt to switch between English and Hangul input mode. You cannot change this hotkey as of yet. - Note that the input mode is also tracked per application, but there is no setting to change this behavior as far as I know, so you'll need to switch for every application separately. 4. Start typing. The underlined text is the current composition text. While a composition is active, you can only edit the composition text. Confirm the composition with Space or by selecting a candidate from the candidate list with the arrow keys. Cancel the composition with Escape or by using Backspace to delete the composition text. Note that not all languages offer a candidate list. SDL version-specific issues ------------------------------ - 2.26.5, 2.24.2, 2.0.22: IME candidates work. But there are minor bugs when moving the composition cursor. - 2.0.18, 2.0.20: IME candidates work. - 2.0.16 (our current version): IME candidates cannot be determined with Windows API. Windows tries to draw the composition window like before, so this does not work in fullscreen mode. - 2.0.8 (upstream 0.7): IME candidates work. But this SDL version is too old for us.
2023-01-03 21:28:38 +00:00
CLineInputBuffered<512> m_Input;
static constexpr float CHAT_WIDTH = 200.0f;
static constexpr float CHAT_HEIGHT_FULL = 200.0f;
static constexpr float CHAT_HEIGHT_MIN = 50.0f;
2020-10-13 20:08:52 +00:00
enum
2010-05-29 07:25:38 +00:00
{
2020-10-13 20:08:52 +00:00
MAX_LINES = 25
2010-05-29 07:25:38 +00:00
};
struct CLine
{
2021-06-23 05:05:49 +00:00
int64_t m_Time;
float m_aYOffset[2];
int m_ClientID;
int m_TeamNumber;
2021-03-08 00:08:38 +00:00
bool m_Team;
bool m_Whisper;
2010-05-29 07:25:38 +00:00
int m_NameColor;
char m_aName[64];
char m_aText[512];
bool m_Friend;
bool m_Highlighted;
2018-03-13 20:50:49 +00:00
STextContainerIndex m_TextContainerIndex;
2020-10-13 20:08:52 +00:00
int m_QuadContainerIndex;
char m_aSkinName[std::size(g_Config.m_ClPlayerSkin)];
2020-10-13 20:08:52 +00:00
CSkin::SSkinTextures m_RenderSkin;
2020-11-08 05:39:16 +00:00
CSkin::SSkinMetrics m_RenderSkinMetrics;
2020-10-13 20:08:52 +00:00
bool m_CustomColoredSkin;
ColorRGBA m_ColorBody;
ColorRGBA m_ColorFeet;
bool m_HasRenderTee;
2018-03-13 20:50:49 +00:00
float m_TextYOffset;
2020-09-20 15:25:27 +00:00
int m_TimesRepeated;
2010-05-29 07:25:38 +00:00
};
bool m_PrevScoreBoardShowed;
2018-03-13 20:50:49 +00:00
bool m_PrevShowChat;
2010-05-29 07:25:38 +00:00
CLine m_aLines[MAX_LINES];
int m_CurrentLine;
enum
{
// client IDs for special messages
CLIENT_MSG = -2,
SERVER_MSG = -1,
2020-09-22 16:02:03 +00:00
MODE_NONE = 0,
2010-05-29 07:25:38 +00:00
MODE_ALL,
MODE_TEAM,
2020-09-22 16:02:03 +00:00
CHAT_SERVER = 0,
CHAT_HIGHLIGHT,
CHAT_CLIENT,
CHAT_NUM,
2010-05-29 07:25:38 +00:00
};
int m_Mode;
bool m_Show;
bool m_CompletionUsed;
int m_CompletionChosen;
char m_aCompletionBuffer[256];
int m_PlaceholderOffset;
int m_PlaceholderLength;
2023-08-20 18:35:56 +00:00
static char ms_aDisplayText[512];
struct CRateablePlayer
{
int ClientID;
int Score;
};
CRateablePlayer m_aPlayerCompletionList[MAX_CLIENTS];
int m_PlayerCompletionListLength;
struct CCommand
{
const char *m_pName;
const char *m_pParams;
CCommand() = default;
CCommand(const char *pName, const char *pParams) :
m_pName(pName), m_pParams(pParams)
{
}
bool operator<(const CCommand &Other) const { return str_comp(m_pName, Other.m_pName) < 0; }
bool operator<=(const CCommand &Other) const { return str_comp(m_pName, Other.m_pName) <= 0; }
bool operator==(const CCommand &Other) const { return str_comp(m_pName, Other.m_pName) == 0; }
};
std::vector<CCommand> m_vCommands;
2012-01-09 22:13:51 +00:00
struct CHistoryEntry
{
int m_Team;
char m_aText[1];
};
CHistoryEntry *m_pHistoryEntry;
2020-10-27 17:57:14 +00:00
CStaticRingBuffer<CHistoryEntry, 64 * 1024, CRingBufferBase::FLAG_RECYCLE> m_History;
2012-01-09 22:13:51 +00:00
int m_PendingChatCounter;
2021-06-23 05:05:49 +00:00
int64_t m_LastChatSend;
int64_t m_aLastSoundPlayed[CHAT_NUM];
2023-08-20 18:35:56 +00:00
bool m_IsInputCensored;
2010-05-29 07:25:38 +00:00
static void ConSay(IConsole::IResult *pResult, void *pUserData);
static void ConSayTeam(IConsole::IResult *pResult, void *pUserData);
static void ConChat(IConsole::IResult *pResult, void *pUserData);
static void ConShowChat(IConsole::IResult *pResult, void *pUserData);
static void ConEcho(IConsole::IResult *pResult, void *pUserData);
2020-12-22 18:24:48 +00:00
static void ConchainChatOld(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
2020-10-13 20:08:52 +00:00
2015-07-22 13:37:00 +00:00
bool LineShouldHighlight(const char *pLine, const char *pName);
void StoreSave(const char *pText);
2015-07-22 13:37:00 +00:00
2010-05-29 07:25:38 +00:00
public:
CChat();
int Sizeof() const override { return sizeof(*this); }
2010-05-29 07:25:38 +00:00
static constexpr float MESSAGE_PADDING_X = 5.0f;
static constexpr float MESSAGE_TEE_SIZE = 7.0f;
static constexpr float MESSAGE_TEE_PADDING_RIGHT = 0.5f;
static constexpr float FONT_SIZE = 6.0f;
static constexpr float MESSAGE_PADDING_Y = 1.0f;
static constexpr float MESSAGE_ROUNDING = 3.0f;
static_assert(FONT_SIZE + MESSAGE_PADDING_Y >= MESSAGE_ROUNDING * 2.0f, "Corners for background chat are too huge for this combination of font size and message padding.");
2010-05-29 07:25:38 +00:00
bool IsActive() const { return m_Mode != MODE_NONE; }
void AddLine(int ClientID, int Team, const char *pLine);
2010-05-29 07:25:38 +00:00
void EnableMode(int Team);
void DisableMode();
2010-05-29 07:25:38 +00:00
void Say(int Team, const char *pLine);
void SayChat(const char *pLine);
void RegisterCommand(const char *pName, const char *pParams, int flags, const char *pHelp);
void Echo(const char *pString);
void OnWindowResize() override;
void OnConsoleInit() override;
void OnStateChange(int NewState, int OldState) override;
void OnRender() override;
void RefindSkins();
void OnPrepareLines();
void Reset();
void OnRelease() override;
void OnMessage(int MsgType, void *pRawMsg) override;
bool OnInput(const IInput::CEvent &Event) override;
void OnInit() override;
2020-10-13 20:08:52 +00:00
void RebuildChat();
2010-05-29 07:25:38 +00:00
};
#endif