Luanti 5.15.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
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:207
Definition chat.h:270
void addMessage(const std::wstring &name, std::wstring text)
Definition chat.cpp:774
ChatBackend()
Definition chat.cpp:767
void applySettings()
Definition chat.cpp:860
ChatPrompt m_prompt
Definition chat.h:309
ChatBuffer m_console_buffer
Definition chat.h:307
EnrichedString getRecentChat() const
Definition chat.cpp:822
ChatBuffer & getConsoleBuffer()
Definition chat.cpp:812
void step(float dtime)
Definition chat.cpp:867
ChatBuffer & getRecentBuffer()
Definition chat.cpp:817
void scrollPageUp()
Definition chat.cpp:885
void scroll(s32 rows)
Definition chat.cpp:875
void clearRecentChat()
Definition chat.cpp:854
~ChatBackend()=default
void addUnparsedMessage(std::wstring line)
Definition chat.cpp:789
void scrollPageDown()
Definition chat.cpp:880
void reformat(u32 cols, u32 rows)
Definition chat.cpp:844
ChatPrompt & getPrompt()
Definition chat.cpp:839
ChatBuffer m_recent_buffer
Definition chat.h:308
Definition chat.h:60
void reformat(u32 cols, u32 rows)
Definition chat.cpp:128
ChatBuffer(u32 scrollback)
Definition chat.cpp:15
s32 getTopScrollPos() const
Definition chat.cpp:413
u32 m_rows
Definition chat.h:128
u32 getLineCount() const
Definition chat.cpp:63
u32 formatChatLine(const ChatLine &line, u32 cols, std::vector< ChatFormattedLine > &destination) const
Definition chat.cpp:215
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:198
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:436
s32 getBottomScrollPos() const
Definition chat.cpp:426
void deleteOldest(u32 count)
Definition chat.cpp:81
const ChatLine & getLine(u32 index) const
Definition chat.cpp:68
std::vector< ChatLine > m_unformatted
Definition chat.h:123
void clear()
Definition chat.cpp:55
void addLine(const EnrichedString &name, const EnrichedString &text)
Definition chat.cpp:34
bool getLinesModified() const
Definition chat.h:103
void scrollBottom()
Definition chat.cpp:210
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:115
u32 getRows() const
Definition chat.cpp:123
std::vector< ChatFormattedLine > m_formatted
Definition chat.h:132
void step(f32 dtime)
Definition chat.cpp:74
const ChatFormattedLine & getFormattedLine(u32 row) const
Definition chat.cpp:184
void scroll(s32 rows)
Definition chat.cpp:193
~ChatBuffer()=default
Definition chat.h:148
void input(wchar_t ch)
Definition chat.cpp:477
s32 m_nick_completion_end
Definition chat.h:266
ChatPrompt(const std::wstring &prompt, u32 history_limit)
Definition chat.cpp:444
void historyNext()
Definition chat.cpp:557
void addToHistory(const std::wstring &line)
Definition chat.cpp:495
s32 getCursorLength() const
Definition chat.h:187
std::wstring getLine() const
Definition chat.h:161
~ChatPrompt()=default
s32 getVisibleCursorPosition() const
Definition chat.cpp:673
std::wstring & makeLineRef()
Definition chat.cpp:455
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:678
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:664
void clampView()
Definition chat.cpp:749
s32 m_view
Definition chat.h:257
void clear()
Definition chat.cpp:526
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:646
std::wstring replace(const std::wstring &line)
Definition chat.cpp:535
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:546
std::wstring m_line
Definition chat.h:246
void nickCompletion(const std::set< std::string > &names, bool backwards)
Definition chat.cpp:568
const std::wstring & getLineRef() const
Definition chat.cpp:450
u32 m_history_limit
Definition chat.h:252
Definition enriched_string.h:28
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: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:466
std::optional< std::wstring > saved
Definition chat.h:235
HistoryEntry(const std::wstring &line)
Definition chat.h:237