Luanti 5.17.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 <optional>
11
12#include "irrlichttypes.h"
14
15// Chat console related classes
16
18{
19 // age in seconds
20 f32 age = 0.0f;
21 // name of sending player, or empty if sent by server
23 // message text
25
26 ChatLine(const std::wstring &a_name, const std::wstring &a_text):
27 name(a_name),
28 text(a_text)
29 {
30 }
31
32 ChatLine(const EnrichedString &a_name, const EnrichedString &a_text):
33 name(a_name),
34 text(a_text)
35 {
36 }
37};
38
40{
41 // text string
43 // starting column
44 u32 column;
45 // web link is empty for most frags
46 std::string weblink;
47 // formatting
48 //u8 bold:1;
49};
50
52{
53 // Array of text fragments
54 std::vector<ChatFormattedFragment> fragments;
55 // true if first line of one formatted ChatLine
56 bool first;
57};
58
60{
61public:
62 ChatBuffer(u32 scrollback);
63 ~ChatBuffer() = default;
64
65 // Append chat line
66 // Removes oldest chat line if scrollback size is reached
67 void addLine(const EnrichedString &name, const EnrichedString &text);
68
69 // Remove all chat lines
70 void clear();
71
72 // Get number of lines currently in buffer.
73 u32 getLineCount() const;
74 // Get reference to i-th chat line.
75 const ChatLine& getLine(u32 index) const;
76
77 // Increase each chat line's age by dtime.
78 void step(f32 dtime);
79 // Delete oldest N chat lines.
80 void deleteOldest(u32 count);
81 // Delete lines older than maxAge.
82 void deleteByAge(f32 maxAge);
83
84 // Get number of rows, 0 if reformat has not been called yet.
85 u32 getRows() const;
86 // Update console size and reformat all formatted lines.
87 void reformat(u32 cols, u32 rows);
88 // Get formatted line for a given row (0 is top of screen).
89 // Only valid after reformat has been called at least once
90 const ChatFormattedLine& getFormattedLine(u32 row) const;
91 // Scrolling in formatted buffer (relative)
92 // positive rows == scroll up, negative rows == scroll down
93 void scroll(s32 rows);
94 // Scrolling in formatted buffer (absolute)
95 void scrollAbsolute(s32 scroll);
96 // Scroll to bottom of buffer (newest)
97 void scrollBottom();
98
99 // Functions for keeping track of whether the lines were modified by any
100 // preceding operations
101 // If they were not changed, getLineCount() and getLine() output the same as
102 // before
103 bool getLinesModified() const { return m_lines_modified; }
105
106 // Format a chat line for the given number of columns.
107 // Appends the formatted lines to the destination array and
108 // returns the number of formatted lines.
109 u32 formatChatLine(const ChatLine& line, u32 cols,
110 std::vector<ChatFormattedLine>& destination) const;
111
112 void resize(u32 scrollback);
113
114 // Get the current scroll position
115 s32 getScrollPosition() const { return m_scroll; }
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
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
154
155 // Input character or string
156 void input(wchar_t ch);
157 void input(const std::wstring &str);
158
159 // Add a string to the history
160 void addToHistory(const std::wstring &line);
161
162 // Get current line
163 std::wstring getLine() const { return getLineRef(); }
164
165 // Get section of line that is currently selected
166 std::wstring getSelection() const { return getLineRef().substr(m_cursor, m_cursor_len); }
167
168 // Clear the current line
169 void clear();
170
171 // Replace the current line with the given text
172 std::wstring replace(const std::wstring &line);
173
174 // Select previous command from history
175 void historyPrev();
176 // Select next command from history
177 void historyNext();
178
179 // Nick completion
180 void nickCompletion(const std::set<std::string> &names);
181
182 // Update console size and reformat the visible portion of the prompt
183 void reformat(u32 cols);
184 // Get visible portion of the prompt.
185 std::wstring getVisiblePortion() const;
186 // Get cursor position (relative to visible portion). -1 if invalid
187 s32 getVisibleCursorPosition() const;
188 // Get length of cursor selection
189 s32 getCursorLength() const { return m_cursor_len; }
190
191 // Cursor operations
197
198 // Cursor operation direction
203
204 // Cursor operation scope
211
212 // Cursor operation
213 // op specifies whether it's a move or delete operation
214 // dir specifies whether the operation goes left or right
215 // scope specifies how far the operation will reach (char/word/line)
216 // Examples:
217 // cursorOperation(CURSOROP_MOVE, CURSOROP_DIR_RIGHT, CURSOROP_SCOPE_LINE)
218 // moves the cursor to the end of the line.
219 // cursorOperation(CURSOROP_DELETE, CURSOROP_DIR_LEFT, CURSOROP_SCOPE_WORD)
220 // deletes the word to the left of the cursor.
222
223protected:
224 const std::wstring &getLineRef() const;
225
226 std::wstring &makeLineRef();
227
228 // set m_view to ensure that 0 <= m_view <= m_cursor < m_view + m_cols
229 // if line can be fully shown, set m_view to zero
230 // else, also ensure m_view <= m_line.size() + 1 - m_cols
231 void clampView();
232
233private:
235 std::wstring line;
236 // If line is edited, saved holds the unedited version.
237 std::optional<std::wstring> saved;
238
239 HistoryEntry(const std::wstring &line): line(line) {}
240
241 bool operator==(const HistoryEntry &other);
242 bool operator!=(const HistoryEntry &other) { return !(*this == other); }
243 };
244
245 // Prompt prefix
246 std::wstring m_prompt = L"";
247 // Non-historical edited line
248 std::wstring m_line = L"";
249 // History buffer
250 std::vector<HistoryEntry> m_history;
251 // History index (0 <= m_history_index <= m_history.size())
253 // Maximum number of history entries
255
256 // Number of columns excluding columns reserved for the prompt
257 s32 m_cols = 0;
258 // Start of visible portion (index into m_line)
259 s32 m_view = 0;
260 // Cursor (index into m_line)
261 s32 m_cursor = 0;
262 // Cursor length (length of selected portion of line)
264
265 // To print nick autocompletion
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:205
void addMessage(const std::wstring &name, std::wstring text)
Definition chat.cpp:776
ChatBackend()
Definition chat.cpp:768
void applySettings()
Definition chat.cpp:862
ChatPrompt m_prompt
Definition chat.h:309
ChatBuffer m_console_buffer
Definition chat.h:307
EnrichedString getRecentChat() const
Definition chat.cpp:824
ChatBuffer & getConsoleBuffer()
Definition chat.cpp:814
void step(float dtime)
Definition chat.cpp:869
ChatBuffer & getRecentBuffer()
Definition chat.cpp:819
void scrollPageUp()
Definition chat.cpp:887
void scroll(s32 rows)
Definition chat.cpp:877
void clearRecentChat()
Definition chat.cpp:856
~ChatBackend()=default
void addUnparsedMessage(std::wstring line)
Definition chat.cpp:791
void scrollPageDown()
Definition chat.cpp:882
void reformat(u32 cols, u32 rows)
Definition chat.cpp:846
ChatPrompt & getPrompt()
Definition chat.cpp:841
ChatBuffer m_recent_buffer
Definition chat.h:308
Definition chat.h:60
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
s32 getScrollPosition() const
Definition chat.h:115
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
void addLine(const EnrichedString &name, const EnrichedString &text)
Definition chat.cpp:37
bool getLinesModified() const
Definition chat.h:103
void scrollBottom()
Definition chat.cpp:213
bool m_lines_modified
Definition chat.h:144
void resetLinesModified()
Definition chat.h:104
u32 m_scrollback
Definition chat.h:121
void deleteByAge(f32 maxAge)
Definition chat.cpp:118
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
ChatPrompt(const std::wstring &prompt, u32 history_limit)
Definition chat.cpp:447
void historyNext()
Definition chat.cpp:550
ChatBuffer * m_chat_buffer
Definition chat.h:266
void addToHistory(const std::wstring &line)
Definition chat.cpp:494
s32 getCursorLength() const
Definition chat.h:189
std::wstring getLine() const
Definition chat.h:163
~ChatPrompt()=default
s32 getVisibleCursorPosition() const
Definition chat.cpp:677
std::wstring & makeLineRef()
Definition chat.cpp:458
void nickCompletion(const std::set< std::string > &names)
Definition chat.cpp:559
std::wstring getSelection() const
Definition chat.h:166
void cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope)
Definition chat.cpp:682
CursorOp
Definition chat.h:192
@ CURSOROP_DELETE
Definition chat.h:195
@ CURSOROP_MOVE
Definition chat.h:193
@ CURSOROP_SELECT
Definition chat.h:194
std::wstring getVisiblePortion() const
Definition chat.cpp:668
void clampView()
Definition chat.cpp:750
s32 m_view
Definition chat.h:259
void clear()
Definition chat.cpp:525
CursorOpDir
Definition chat.h:199
@ CURSOROP_DIR_RIGHT
Definition chat.h:201
@ CURSOROP_DIR_LEFT
Definition chat.h:200
void reformat(u32 cols)
Definition chat.cpp:650
std::wstring replace(const std::wstring &line)
Definition chat.cpp:532
std::wstring m_prompt
Definition chat.h:246
CursorOpScope
Definition chat.h:205
@ CURSOROP_SCOPE_LINE
Definition chat.h:208
@ CURSOROP_SCOPE_SELECTION
Definition chat.h:209
@ CURSOROP_SCOPE_CHARACTER
Definition chat.h:206
@ CURSOROP_SCOPE_WORD
Definition chat.h:207
u32 m_history_index
Definition chat.h:252
std::vector< HistoryEntry > m_history
Definition chat.h:250
s32 m_cols
Definition chat.h:257
s32 m_cursor
Definition chat.h:261
s32 m_cursor_len
Definition chat.h:263
void historyPrev()
Definition chat.cpp:541
void setChatBuffer(ChatBuffer *b)
Definition chat.h:153
std::wstring m_line
Definition chat.h:248
const std::wstring & getLineRef() const
Definition chat.cpp:453
u32 m_history_limit
Definition chat.h:254
Definition enriched_string.h:27
Definition chat.h:40
std::string weblink
Definition chat.h:46
u32 column
Definition chat.h:44
EnrichedString text
Definition chat.h:42
Definition chat.h:52
std::vector< ChatFormattedFragment > fragments
Definition chat.h:54
bool first
Definition chat.h:56
Definition chat.h:18
EnrichedString name
Definition chat.h:22
EnrichedString text
Definition chat.h:24
f32 age
Definition chat.h:20
ChatLine(const std::wstring &a_name, const std::wstring &a_text)
Definition chat.h:26
ChatLine(const EnrichedString &a_name, const EnrichedString &a_text)
Definition chat.h:32
Definition chat.h:234
bool operator!=(const HistoryEntry &other)
Definition chat.h:242
std::wstring line
Definition chat.h:235
bool operator==(const HistoryEntry &other)
Definition chat.cpp:469
std::optional< std::wstring > saved
Definition chat.h:237
HistoryEntry(const std::wstring &line)
Definition chat.h:239