]> git.lizzy.rs Git - dragonfireclient.git/blob - src/chat.h
e3961b3fa2667120a3492f5d413e0dc880652cd9
[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 #ifndef CHAT_HEADER
21 #define CHAT_HEADER
22
23 #include <string>
24 #include <vector>
25 #include <list>
26
27 #include "irrlichttypes.h"
28 #include "util/enriched_string.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         // formatting
61         //u8 bold:1;
62 };
63
64 struct ChatFormattedLine
65 {
66         // Array of text fragments
67         std::vector<ChatFormattedFragment> fragments;
68         // true if first line of one formatted ChatLine
69         bool first;
70 };
71
72 class ChatBuffer
73 {
74 public:
75         ChatBuffer(u32 scrollback);
76         ~ChatBuffer() = default;
77
78         // Append chat line
79         // Removes oldest chat line if scrollback size is reached
80         void addLine(std::wstring name, std::wstring text);
81
82         // Remove all chat lines
83         void clear();
84
85         // Get number of lines currently in buffer.
86         u32 getLineCount() const;
87         // Get reference to i-th chat line.
88         const ChatLine& getLine(u32 index) const;
89
90         // Increase each chat line's age by dtime.
91         void step(f32 dtime);
92         // Delete oldest N chat lines.
93         void deleteOldest(u32 count);
94         // Delete lines older than maxAge.
95         void deleteByAge(f32 maxAge);
96
97         // Get number of columns, 0 if reformat has not been called yet.
98         u32 getColumns() const;
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 protected:
123         s32 getTopScrollPos() const;
124         s32 getBottomScrollPos() const;
125
126 private:
127         // Scrollback size
128         u32 m_scrollback;
129         // Array of unformatted chat lines
130         std::vector<ChatLine> m_unformatted;
131
132         // Number of character columns in console
133         u32 m_cols = 0;
134         // Number of character rows in console
135         u32 m_rows = 0;
136         // Scroll position (console's top line index into m_formatted)
137         s32 m_scroll = 0;
138         // Array of formatted lines
139         std::vector<ChatFormattedLine> m_formatted;
140         // Empty formatted line, for error returns
141         ChatFormattedLine m_empty_formatted_line;
142 };
143
144 class ChatPrompt
145 {
146 public:
147         ChatPrompt(const std::wstring &prompt, u32 history_limit);
148         ~ChatPrompt() = default;
149
150         // Input character or string
151         void input(wchar_t ch);
152         void input(const std::wstring &str);
153
154         // Add a string to the history
155         void addToHistory(std::wstring line);
156
157         // Get current line
158         std::wstring getLine() const { return m_line; }
159
160         // Get section of line that is currently selected
161         std::wstring getSelection() const { return m_line.substr(m_cursor, m_cursor_len); }
162
163         // Clear the current line
164         void clear();
165
166         // Replace the current line with the given text
167         std::wstring replace(std::wstring line);
168
169         // Select previous command from history
170         void historyPrev();
171         // Select next command from history
172         void historyNext();
173
174         // Nick completion
175         void nickCompletion(const std::list<std::string>& names, bool backwards);
176
177         // Update console size and reformat the visible portion of the prompt
178         void reformat(u32 cols);
179         // Get visible portion of the prompt.
180         std::wstring getVisiblePortion() const;
181         // Get cursor position (relative to visible portion). -1 if invalid
182         s32 getVisibleCursorPosition() const;
183         // Get length of cursor selection
184         s32 getCursorLength() const { return m_cursor_len; }
185
186         // Cursor operations
187         enum CursorOp {
188                 CURSOROP_MOVE,
189                 CURSOROP_SELECT,
190                 CURSOROP_DELETE
191         };
192
193         // Cursor operation direction
194         enum CursorOpDir {
195                 CURSOROP_DIR_LEFT,
196                 CURSOROP_DIR_RIGHT
197         };
198
199         // Cursor operation scope
200         enum CursorOpScope {
201                 CURSOROP_SCOPE_CHARACTER,
202                 CURSOROP_SCOPE_WORD,
203                 CURSOROP_SCOPE_LINE,
204                 CURSOROP_SCOPE_SELECTION
205         };
206
207         // Cursor operation
208         // op specifies whether it's a move or delete operation
209         // dir specifies whether the operation goes left or right
210         // scope specifies how far the operation will reach (char/word/line)
211         // Examples:
212         //   cursorOperation(CURSOROP_MOVE, CURSOROP_DIR_RIGHT, CURSOROP_SCOPE_LINE)
213         //     moves the cursor to the end of the line.
214         //   cursorOperation(CURSOROP_DELETE, CURSOROP_DIR_LEFT, CURSOROP_SCOPE_WORD)
215         //     deletes the word to the left of the cursor.
216         void cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope);
217
218 protected:
219         // set m_view to ensure that 0 <= m_view <= m_cursor < m_view + m_cols
220         // if line can be fully shown, set m_view to zero
221         // else, also ensure m_view <= m_line.size() + 1 - m_cols
222         void clampView();
223
224 private:
225         // Prompt prefix
226         std::wstring m_prompt = L"";
227         // Currently edited line
228         std::wstring m_line = L"";
229         // History buffer
230         std::vector<std::wstring> m_history;
231         // History index (0 <= m_history_index <= m_history.size())
232         u32 m_history_index = 0;
233         // Maximum number of history entries
234         u32 m_history_limit;
235
236         // Number of columns excluding columns reserved for the prompt
237         s32 m_cols = 0;
238         // Start of visible portion (index into m_line)
239         s32 m_view = 0;
240         // Cursor (index into m_line)
241         s32 m_cursor = 0;
242         // Cursor length (length of selected portion of line)
243         s32 m_cursor_len = 0;
244
245         // Last nick completion start (index into m_line)
246         s32 m_nick_completion_start = 0;
247         // Last nick completion start (index into m_line)
248         s32 m_nick_completion_end = 0;
249 };
250
251 class ChatBackend
252 {
253 public:
254         ChatBackend();
255         ~ChatBackend() = default;
256
257         // Add chat message
258         void addMessage(std::wstring name, std::wstring text);
259         // Parse and add unparsed chat message
260         void addUnparsedMessage(std::wstring line);
261
262         // Get the console buffer
263         ChatBuffer& getConsoleBuffer();
264         // Get the recent messages buffer
265         ChatBuffer& getRecentBuffer();
266         // Concatenate all recent messages
267         EnrichedString getRecentChat();
268         // Get the console prompt
269         ChatPrompt& getPrompt();
270
271         // Reformat all buffers
272         void reformat(u32 cols, u32 rows);
273
274         // Clear all recent messages
275         void clearRecentChat();
276
277         // Age recent messages
278         void step(float dtime);
279
280         // Scrolling
281         void scroll(s32 rows);
282         void scrollPageDown();
283         void scrollPageUp();
284
285 private:
286         ChatBuffer m_console_buffer;
287         ChatBuffer m_recent_buffer;
288         ChatPrompt m_prompt;
289 };
290
291 #endif
292