]> git.lizzy.rs Git - dragonfireclient.git/blob - src/chat.h
Make chat web links clickable (#11092)
[dragonfireclient.git] / src / chat.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include <string>
23 #include <vector>
24 #include <list>
25
26 #include "irrlichttypes.h"
27 #include "util/enriched_string.h"
28 #include "settings.h"
29
30 // Chat console related classes
31
32 struct ChatLine
33 {
34         // age in seconds
35         f32 age = 0.0f;
36         // name of sending player, or empty if sent by server
37         EnrichedString name;
38         // message text
39         EnrichedString text;
40
41         ChatLine(const std::wstring &a_name, const std::wstring &a_text):
42                 name(a_name),
43                 text(a_text)
44         {
45         }
46
47         ChatLine(const EnrichedString &a_name, const EnrichedString &a_text):
48                 name(a_name),
49                 text(a_text)
50         {
51         }
52 };
53
54 struct ChatFormattedFragment
55 {
56         // text string
57         EnrichedString text;
58         // starting column
59         u32 column;
60         // web link is empty for most frags
61         std::string weblink;
62         // formatting
63         //u8 bold:1;
64 };
65
66 struct ChatFormattedLine
67 {
68         // Array of text fragments
69         std::vector<ChatFormattedFragment> fragments;
70         // true if first line of one formatted ChatLine
71         bool first;
72 };
73
74 class ChatBuffer
75 {
76 public:
77         ChatBuffer(u32 scrollback);
78         ~ChatBuffer() = default;
79
80         // Append chat line
81         // Removes oldest chat line if scrollback size is reached
82         void addLine(const std::wstring &name, const std::wstring &text);
83
84         // Remove all chat lines
85         void clear();
86
87         // Get number of lines currently in buffer.
88         u32 getLineCount() const;
89         // Get reference to i-th chat line.
90         const ChatLine& getLine(u32 index) const;
91
92         // Increase each chat line's age by dtime.
93         void step(f32 dtime);
94         // Delete oldest N chat lines.
95         void deleteOldest(u32 count);
96         // Delete lines older than maxAge.
97         void deleteByAge(f32 maxAge);
98
99         // Get number of rows, 0 if reformat has not been called yet.
100         u32 getRows() const;
101         // Update console size and reformat all formatted lines.
102         void reformat(u32 cols, u32 rows);
103         // Get formatted line for a given row (0 is top of screen).
104         // Only valid after reformat has been called at least once
105         const ChatFormattedLine& getFormattedLine(u32 row) const;
106         // Scrolling in formatted buffer (relative)
107         // positive rows == scroll up, negative rows == scroll down
108         void scroll(s32 rows);
109         // Scrolling in formatted buffer (absolute)
110         void scrollAbsolute(s32 scroll);
111         // Scroll to bottom of buffer (newest)
112         void scrollBottom();
113         // Scroll to top of buffer (oldest)
114         void scrollTop();
115
116         // Format a chat line for the given number of columns.
117         // Appends the formatted lines to the destination array and
118         // returns the number of formatted lines.
119         u32 formatChatLine(const ChatLine& line, u32 cols,
120                         std::vector<ChatFormattedLine>& destination) const;
121
122         void resize(u32 scrollback);
123
124 protected:
125         s32 getTopScrollPos() const;
126         s32 getBottomScrollPos() const;
127
128 private:
129         // Scrollback size
130         u32 m_scrollback;
131         // Array of unformatted chat lines
132         std::vector<ChatLine> m_unformatted;
133
134         // Number of character columns in console
135         u32 m_cols = 0;
136         // Number of character rows in console
137         u32 m_rows = 0;
138         // Scroll position (console's top line index into m_formatted)
139         s32 m_scroll = 0;
140         // Array of formatted lines
141         std::vector<ChatFormattedLine> m_formatted;
142         // Empty formatted line, for error returns
143         ChatFormattedLine m_empty_formatted_line;
144
145         // Enable clickable chat weblinks
146         bool m_cache_clickable_chat_weblinks;
147         // Color of clickable chat weblinks
148         irr::video::SColor m_cache_chat_weblink_color;
149 };
150
151 class ChatPrompt
152 {
153 public:
154         ChatPrompt(const std::wstring &prompt, u32 history_limit);
155         ~ChatPrompt() = default;
156
157         // Input character or string
158         void input(wchar_t ch);
159         void input(const std::wstring &str);
160
161         // Add a string to the history
162         void addToHistory(const std::wstring &line);
163
164         // Get current line
165         std::wstring getLine() const { return m_line; }
166
167         // Get section of line that is currently selected
168         std::wstring getSelection() const { return m_line.substr(m_cursor, m_cursor_len); }
169
170         // Clear the current line
171         void clear();
172
173         // Replace the current line with the given text
174         std::wstring replace(const std::wstring &line);
175
176         // Select previous command from history
177         void historyPrev();
178         // Select next command from history
179         void historyNext();
180
181         // Nick completion
182         void nickCompletion(const std::list<std::string>& names, bool backwards);
183
184         // Update console size and reformat the visible portion of the prompt
185         void reformat(u32 cols);
186         // Get visible portion of the prompt.
187         std::wstring getVisiblePortion() const;
188         // Get cursor position (relative to visible portion). -1 if invalid
189         s32 getVisibleCursorPosition() const;
190         // Get length of cursor selection
191         s32 getCursorLength() const { return m_cursor_len; }
192
193         // Cursor operations
194         enum CursorOp {
195                 CURSOROP_MOVE,
196                 CURSOROP_SELECT,
197                 CURSOROP_DELETE
198         };
199
200         // Cursor operation direction
201         enum CursorOpDir {
202                 CURSOROP_DIR_LEFT,
203                 CURSOROP_DIR_RIGHT
204         };
205
206         // Cursor operation scope
207         enum CursorOpScope {
208                 CURSOROP_SCOPE_CHARACTER,
209                 CURSOROP_SCOPE_WORD,
210                 CURSOROP_SCOPE_LINE,
211                 CURSOROP_SCOPE_SELECTION
212         };
213
214         // Cursor operation
215         // op specifies whether it's a move or delete operation
216         // dir specifies whether the operation goes left or right
217         // scope specifies how far the operation will reach (char/word/line)
218         // Examples:
219         //   cursorOperation(CURSOROP_MOVE, CURSOROP_DIR_RIGHT, CURSOROP_SCOPE_LINE)
220         //     moves the cursor to the end of the line.
221         //   cursorOperation(CURSOROP_DELETE, CURSOROP_DIR_LEFT, CURSOROP_SCOPE_WORD)
222         //     deletes the word to the left of the cursor.
223         void cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope);
224
225 protected:
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
231 private:
232         // Prompt prefix
233         std::wstring m_prompt = L"";
234         // Currently edited line
235         std::wstring m_line = L"";
236         // History buffer
237         std::vector<std::wstring> m_history;
238         // History index (0 <= m_history_index <= m_history.size())
239         u32 m_history_index = 0;
240         // Maximum number of history entries
241         u32 m_history_limit;
242
243         // Number of columns excluding columns reserved for the prompt
244         s32 m_cols = 0;
245         // Start of visible portion (index into m_line)
246         s32 m_view = 0;
247         // Cursor (index into m_line)
248         s32 m_cursor = 0;
249         // Cursor length (length of selected portion of line)
250         s32 m_cursor_len = 0;
251
252         // Last nick completion start (index into m_line)
253         s32 m_nick_completion_start = 0;
254         // Last nick completion start (index into m_line)
255         s32 m_nick_completion_end = 0;
256 };
257
258 class ChatBackend
259 {
260 public:
261         ChatBackend();
262         ~ChatBackend() = default;
263
264         // Add chat message
265         void addMessage(const std::wstring &name, std::wstring text);
266         // Parse and add unparsed chat message
267         void addUnparsedMessage(std::wstring line);
268
269         // Get the console buffer
270         ChatBuffer& getConsoleBuffer();
271         // Get the recent messages buffer
272         ChatBuffer& getRecentBuffer();
273         // Concatenate all recent messages
274         EnrichedString getRecentChat() const;
275         // Get the console prompt
276         ChatPrompt& getPrompt();
277
278         // Reformat all buffers
279         void reformat(u32 cols, u32 rows);
280
281         // Clear all recent messages
282         void clearRecentChat();
283
284         // Age recent messages
285         void step(float dtime);
286
287         // Scrolling
288         void scroll(s32 rows);
289         void scrollPageDown();
290         void scrollPageUp();
291
292         // Resize recent buffer based on settings
293         void applySettings();
294
295 private:
296         ChatBuffer m_console_buffer;
297         ChatBuffer m_recent_buffer;
298         ChatPrompt m_prompt;
299 };