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