Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
chat.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <set>
8#include <string>
9#include <vector>
10#include <list>
11#include <optional>
12
13#include "irrlichttypes.h"
15
16// Chat console related classes
17
19{
20 // age in seconds
21 f32 age = 0.0f;
22 // name of sending player, or empty if sent by server
24 // message text
26
27 ChatLine(const std::wstring &a_name, const std::wstring &a_text):
28 name(a_name),
29 text(a_text)
30 {
31 }
32
33 ChatLine(const EnrichedString &a_name, const EnrichedString &a_text):
34 name(a_name),
35 text(a_text)
36 {
37 }
38};
39
41{
42 // text string
44 // starting column
45 u32 column;
46 // web link is empty for most frags
47 std::string weblink;
48 // formatting
49 //u8 bold:1;
50};
51
53{
54 // Array of text fragments
55 std::vector<ChatFormattedFragment> fragments;
56 // true if first line of one formatted ChatLine
57 bool first;
58};
59
61{
62public:
63 ChatBuffer(u32 scrollback);
64 ~ChatBuffer() = default;
65
66 // Append chat line
67 // Removes oldest chat line if scrollback size is reached
68 void addLine(const std::wstring &name, const std::wstring &text);
69
70 // Remove all chat lines
71 void clear();
72
73 // Get number of lines currently in buffer.
74 u32 getLineCount() const;
75 // Get reference to i-th chat line.
76 const ChatLine& getLine(u32 index) const;
77
78 // Increase each chat line's age by dtime.
79 void step(f32 dtime);
80 // Delete oldest N chat lines.
81 void deleteOldest(u32 count);
82 // Delete lines older than maxAge.
83 void deleteByAge(f32 maxAge);
84
85 // Get number of rows, 0 if reformat has not been called yet.
86 u32 getRows() const;
87 // Update console size and reformat all formatted lines.
88 void reformat(u32 cols, u32 rows);
89 // Get formatted line for a given row (0 is top of screen).
90 // Only valid after reformat has been called at least once
91 const ChatFormattedLine& getFormattedLine(u32 row) const;
92 // Scrolling in formatted buffer (relative)
93 // positive rows == scroll up, negative rows == scroll down
94 void scroll(s32 rows);
95 // Scrolling in formatted buffer (absolute)
96 void scrollAbsolute(s32 scroll);
97 // Scroll to bottom of buffer (newest)
98 void scrollBottom();
99
100 // Functions for keeping track of whether the lines were modified by any
101 // preceding operations
102 // If they were not changed, getLineCount() and getLine() output the same as
103 // before
104 bool getLinesModified() const { return m_lines_modified; }
106
107 // Format a chat line for the given number of columns.
108 // Appends the formatted lines to the destination array and
109 // returns the number of formatted lines.
110 u32 formatChatLine(const ChatLine& line, u32 cols,
111 std::vector<ChatFormattedLine>& destination) const;
112
113 void resize(u32 scrollback);
114
115protected:
116 s32 getTopScrollPos() const;
117 s32 getBottomScrollPos() const;
118
119private:
120 // Scrollback size
122 // Array of unformatted chat lines
123 std::vector<ChatLine> m_unformatted;
124
125 // Number of character columns in console
126 u32 m_cols = 0;
127 // Number of character rows in console
128 u32 m_rows = 0;
129 // Scroll position (console's top line index into m_formatted)
130 s32 m_scroll = 0;
131 // Array of formatted lines
132 std::vector<ChatFormattedLine> m_formatted;
133 // Empty formatted line, for error returns
135
136 // Enable clickable chat weblinks
138 // Color of clickable chat weblinks
139 irr::video::SColor m_cache_chat_weblink_color;
140
141 // Whether the lines were modified since last markLinesUnchanged()
142 // Is always set to true when m_unformatted is modified, because that's what
143 // determines the output of getLineCount() and getLine()
144 bool m_lines_modified = true;
145};
146
148{
149public:
150 ChatPrompt(const std::wstring &prompt, u32 history_limit);
151 ~ChatPrompt() = default;
152
153 // Input character or string
154 void input(wchar_t ch);
155 void input(const std::wstring &str);
156
157 // Add a string to the history
158 void addToHistory(const std::wstring &line);
159
160 // Get current line
161 std::wstring getLine() const { return getLineRef(); }
162
163 // Get section of line that is currently selected
164 std::wstring getSelection() const { return getLineRef().substr(m_cursor, m_cursor_len); }
165
166 // Clear the current line
167 void clear();
168
169 // Replace the current line with the given text
170 std::wstring replace(const std::wstring &line);
171
172 // Select previous command from history
173 void historyPrev();
174 // Select next command from history
175 void historyNext();
176
177 // Nick completion
178 void nickCompletion(const std::set<std::string> &names, bool backwards);
179
180 // Update console size and reformat the visible portion of the prompt
181 void reformat(u32 cols);
182 // Get visible portion of the prompt.
183 std::wstring getVisiblePortion() const;
184 // Get cursor position (relative to visible portion). -1 if invalid
185 s32 getVisibleCursorPosition() const;
186 // Get length of cursor selection
187 s32 getCursorLength() const { return m_cursor_len; }
188
189 // Cursor operations
195
196 // Cursor operation direction
201
202 // Cursor operation scope
209
210 // Cursor operation
211 // op specifies whether it's a move or delete operation
212 // dir specifies whether the operation goes left or right
213 // scope specifies how far the operation will reach (char/word/line)
214 // Examples:
215 // cursorOperation(CURSOROP_MOVE, CURSOROP_DIR_RIGHT, CURSOROP_SCOPE_LINE)
216 // moves the cursor to the end of the line.
217 // cursorOperation(CURSOROP_DELETE, CURSOROP_DIR_LEFT, CURSOROP_SCOPE_WORD)
218 // deletes the word to the left of the cursor.
220
221protected:
222 const std::wstring &getLineRef() const;
223
224 std::wstring &makeLineRef();
225
226 // set m_view to ensure that 0 <= m_view <= m_cursor < m_view + m_cols
227 // if line can be fully shown, set m_view to zero
228 // else, also ensure m_view <= m_line.size() + 1 - m_cols
229 void clampView();
230
231private:
233 std::wstring line;
234 // If line is edited, saved holds the unedited version.
235 std::optional<std::wstring> saved;
236
237 HistoryEntry(const std::wstring &line): line(line) {}
238
239 bool operator==(const HistoryEntry &other);
240 bool operator!=(const HistoryEntry &other) { return !(*this == other); }
241 };
242
243 // Prompt prefix
244 std::wstring m_prompt = L"";
245 // Non-historical edited line
246 std::wstring m_line = L"";
247 // History buffer
248 std::vector<HistoryEntry> m_history;
249 // History index (0 <= m_history_index <= m_history.size())
251 // Maximum number of history entries
253
254 // Number of columns excluding columns reserved for the prompt
255 s32 m_cols = 0;
256 // Start of visible portion (index into m_line)
257 s32 m_view = 0;
258 // Cursor (index into m_line)
259 s32 m_cursor = 0;
260 // Cursor length (length of selected portion of line)
262
263 // Last nick completion start (index into m_line)
265 // Last nick completion start (index into m_line)
267};
268
270{
271public:
272 ChatBackend();
273 ~ChatBackend() = default;
274
275 // Add chat message
276 void addMessage(const std::wstring &name, std::wstring text);
277 // Parse and add unparsed chat message
278 void addUnparsedMessage(std::wstring line);
279
280 // Get the console buffer
282 // Get the recent messages buffer
284 // Concatenate all recent messages
286 // Get the console prompt
288
289 // Reformat all buffers
290 void reformat(u32 cols, u32 rows);
291
292 // Clear all recent messages
293 void clearRecentChat();
294
295 // Age recent messages
296 void step(float dtime);
297
298 // Scrolling
299 void scroll(s32 rows);
300 void scrollPageDown();
301 void scrollPageUp();
302
303 // Resize recent buffer based on settings
304 void applySettings();
305
306private:
310};
static v2f dir(const v2f &pos_dist)
Definition camera.cpp:191
Definition chat.h:270
void addMessage(const std::wstring &name, std::wstring text)
Definition chat.cpp:777
ChatBackend()
Definition chat.cpp:770
void applySettings()
Definition chat.cpp:861
ChatPrompt m_prompt
Definition chat.h:309
ChatBuffer m_console_buffer
Definition chat.h:307
EnrichedString getRecentChat() const
Definition chat.cpp:823
ChatBuffer & getConsoleBuffer()
Definition chat.cpp:813
void step(float dtime)
Definition chat.cpp:868
ChatBuffer & getRecentBuffer()
Definition chat.cpp:818
void scrollPageUp()
Definition chat.cpp:886
void scroll(s32 rows)
Definition chat.cpp:876
void clearRecentChat()
Definition chat.cpp:855
~ChatBackend()=default
void addUnparsedMessage(std::wstring line)
Definition chat.cpp:790
void scrollPageDown()
Definition chat.cpp:881
void reformat(u32 cols, u32 rows)
Definition chat.cpp:845
ChatPrompt & getPrompt()
Definition chat.cpp:840
ChatBuffer m_recent_buffer
Definition chat.h:308
Definition chat.h:61
void reformat(u32 cols, u32 rows)
Definition chat.cpp:131
ChatBuffer(u32 scrollback)
Definition chat.cpp:18
s32 getTopScrollPos() const
Definition chat.cpp:416
u32 m_rows
Definition chat.h:128
u32 getLineCount() const
Definition chat.cpp:66
u32 formatChatLine(const ChatLine &line, u32 cols, std::vector< ChatFormattedLine > &destination) const
Definition chat.cpp:218
bool m_cache_clickable_chat_weblinks
Definition chat.h:137
ChatFormattedLine m_empty_formatted_line
Definition chat.h:134
u32 m_cols
Definition chat.h:126
s32 m_scroll
Definition chat.h:130
void scrollAbsolute(s32 scroll)
Definition chat.cpp:201
irr::video::SColor m_cache_chat_weblink_color
Definition chat.h:139
void resize(u32 scrollback)
Definition chat.cpp:439
s32 getBottomScrollPos() const
Definition chat.cpp:429
void deleteOldest(u32 count)
Definition chat.cpp:84
const ChatLine & getLine(u32 index) const
Definition chat.cpp:71
std::vector< ChatLine > m_unformatted
Definition chat.h:123
void clear()
Definition chat.cpp:58
bool getLinesModified() const
Definition chat.h:104
void scrollBottom()
Definition chat.cpp:213
bool m_lines_modified
Definition chat.h:144
void resetLinesModified()
Definition chat.h:105
u32 m_scrollback
Definition chat.h:121
void deleteByAge(f32 maxAge)
Definition chat.cpp:118
void addLine(const std::wstring &name, const std::wstring &text)
Definition chat.cpp:37
u32 getRows() const
Definition chat.cpp:126
std::vector< ChatFormattedLine > m_formatted
Definition chat.h:132
void step(f32 dtime)
Definition chat.cpp:77
const ChatFormattedLine & getFormattedLine(u32 row) const
Definition chat.cpp:187
void scroll(s32 rows)
Definition chat.cpp:196
~ChatBuffer()=default
Definition chat.h:148
void input(wchar_t ch)
Definition chat.cpp:480
s32 m_nick_completion_end
Definition chat.h:266
ChatPrompt(const std::wstring &prompt, u32 history_limit)
Definition chat.cpp:447
void historyNext()
Definition chat.cpp:560
void addToHistory(const std::wstring &line)
Definition chat.cpp:498
s32 getCursorLength() const
Definition chat.h:187
std::wstring getLine() const
Definition chat.h:161
~ChatPrompt()=default
s32 getVisibleCursorPosition() const
Definition chat.cpp:676
std::wstring & makeLineRef()
Definition chat.cpp:458
s32 m_nick_completion_start
Definition chat.h:264
std::wstring getSelection() const
Definition chat.h:164
void cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope)
Definition chat.cpp:681
CursorOp
Definition chat.h:190
@ CURSOROP_DELETE
Definition chat.h:193
@ CURSOROP_MOVE
Definition chat.h:191
@ CURSOROP_SELECT
Definition chat.h:192
std::wstring getVisiblePortion() const
Definition chat.cpp:667
void clampView()
Definition chat.cpp:752
s32 m_view
Definition chat.h:257
void clear()
Definition chat.cpp:529
CursorOpDir
Definition chat.h:197
@ CURSOROP_DIR_RIGHT
Definition chat.h:199
@ CURSOROP_DIR_LEFT
Definition chat.h:198
void reformat(u32 cols)
Definition chat.cpp:649
std::wstring replace(const std::wstring &line)
Definition chat.cpp:538
std::wstring m_prompt
Definition chat.h:244
CursorOpScope
Definition chat.h:203
@ CURSOROP_SCOPE_LINE
Definition chat.h:206
@ CURSOROP_SCOPE_SELECTION
Definition chat.h:207
@ CURSOROP_SCOPE_CHARACTER
Definition chat.h:204
@ CURSOROP_SCOPE_WORD
Definition chat.h:205
u32 m_history_index
Definition chat.h:250
std::vector< HistoryEntry > m_history
Definition chat.h:248
s32 m_cols
Definition chat.h:255
s32 m_cursor
Definition chat.h:259
s32 m_cursor_len
Definition chat.h:261
void historyPrev()
Definition chat.cpp:549
std::wstring m_line
Definition chat.h:246
void nickCompletion(const std::set< std::string > &names, bool backwards)
Definition chat.cpp:571
const std::wstring & getLineRef() const
Definition chat.cpp:453
u32 m_history_limit
Definition chat.h:252
Definition enriched_string.h:29
Definition chat.h:41
std::string weblink
Definition chat.h:47
u32 column
Definition chat.h:45
EnrichedString text
Definition chat.h:43
Definition chat.h:53
std::vector< ChatFormattedFragment > fragments
Definition chat.h:55
bool first
Definition chat.h:57
Definition chat.h:19
EnrichedString name
Definition chat.h:23
EnrichedString text
Definition chat.h:25
f32 age
Definition chat.h:21
ChatLine(const std::wstring &a_name, const std::wstring &a_text)
Definition chat.h:27
ChatLine(const EnrichedString &a_name, const EnrichedString &a_text)
Definition chat.h:33
Definition chat.h:232
bool operator!=(const HistoryEntry &other)
Definition chat.h:240
std::wstring line
Definition chat.h:233
bool operator==(const HistoryEntry &other)
Definition chat.cpp:469
std::optional< std::wstring > saved
Definition chat.h:235
HistoryEntry(const std::wstring &line)
Definition chat.h:237