]> git.lizzy.rs Git - dragonfireclient.git/blob - src/chat.h
Add colored text (not only colored chat).
[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;
36         // name of sending player, or empty if sent by server
37         EnrichedString name;
38         // message text
39         EnrichedString text;
40
41         ChatLine(std::wstring a_name, std::wstring a_text):
42                 age(0.0),
43                 name(a_name),
44                 text(a_text)
45         {
46         }
47
48         ChatLine(EnrichedString a_name, EnrichedString a_text):
49                 age(0.0),
50                 name(a_name),
51                 text(a_text)
52         {
53         }
54 };
55
56 struct ChatFormattedFragment
57 {
58         // text string
59         EnrichedString text;
60         // starting column
61         u32 column;
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();
79
80         // Append chat line
81         // Removes oldest chat line if scrollback size is reached
82         void addLine(std::wstring name, 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 scrollback size, maximum number of lines in buffer.
90         u32 getScrollback() const;
91         // Get reference to i-th chat line.
92         const ChatLine& getLine(u32 index) const;
93
94         // Increase each chat line's age by dtime.
95         void step(f32 dtime);
96         // Delete oldest N chat lines.
97         void deleteOldest(u32 count);
98         // Delete lines older than maxAge.
99         void deleteByAge(f32 maxAge);
100
101         // Get number of columns, 0 if reformat has not been called yet.
102         u32 getColumns() const;
103         // Get number of rows, 0 if reformat has not been called yet.
104         u32 getRows() const;
105         // Update console size and reformat all formatted lines.
106         void reformat(u32 cols, u32 rows);
107         // Get formatted line for a given row (0 is top of screen).
108         // Only valid after reformat has been called at least once
109         const ChatFormattedLine& getFormattedLine(u32 row) const;
110         // Scrolling in formatted buffer (relative)
111         // positive rows == scroll up, negative rows == scroll down
112         void scroll(s32 rows);
113         // Scrolling in formatted buffer (absolute)
114         void scrollAbsolute(s32 scroll);
115         // Scroll to bottom of buffer (newest)
116         void scrollBottom();
117         // Scroll to top of buffer (oldest)
118         void scrollTop();
119
120         // Format a chat line for the given number of columns.
121         // Appends the formatted lines to the destination array and
122         // returns the number of formatted lines.
123         u32 formatChatLine(const ChatLine& line, u32 cols,
124                         std::vector<ChatFormattedLine>& destination) const;
125
126 protected:
127         s32 getTopScrollPos() const;
128         s32 getBottomScrollPos() const;
129
130 private:
131         // Scrollback size
132         u32 m_scrollback;
133         // Array of unformatted chat lines
134         std::vector<ChatLine> m_unformatted;
135
136         // Number of character columns in console
137         u32 m_cols;
138         // Number of character rows in console
139         u32 m_rows;
140         // Scroll position (console's top line index into m_formatted)
141         s32 m_scroll;
142         // Array of formatted lines
143         std::vector<ChatFormattedLine> m_formatted;
144         // Empty formatted line, for error returns
145         ChatFormattedLine m_empty_formatted_line;
146 };
147
148 class ChatPrompt
149 {
150 public:
151         ChatPrompt(std::wstring prompt, u32 history_limit);
152         ~ChatPrompt();
153
154         // Input character or string
155         void input(wchar_t ch);
156         void input(const std::wstring &str);
157
158         // Add a string to the history
159         void addToHistory(std::wstring line);
160
161         // Get current line
162         std::wstring getLine() const { return m_line; }
163
164         // Get section of line that is currently selected
165         std::wstring getSelection() const
166                 { return m_line.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(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::list<std::string>& names, bool backwards);
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
192         enum CursorOp {
193                 CURSOROP_MOVE,
194                 CURSOROP_SELECT,
195                 CURSOROP_DELETE
196         };
197
198         // Cursor operation direction
199         enum CursorOpDir {
200                 CURSOROP_DIR_LEFT,
201                 CURSOROP_DIR_RIGHT
202         };
203
204         // Cursor operation scope
205         enum CursorOpScope {
206                 CURSOROP_SCOPE_CHARACTER,
207                 CURSOROP_SCOPE_WORD,
208                 CURSOROP_SCOPE_LINE,
209                 CURSOROP_SCOPE_SELECTION
210         };
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.
221         void cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope);
222
223 protected:
224         // set m_view to ensure that 0 <= m_view <= m_cursor < m_view + m_cols
225         // if line can be fully shown, set m_view to zero
226         // else, also ensure m_view <= m_line.size() + 1 - m_cols
227         void clampView();
228
229 private:
230         // Prompt prefix
231         std::wstring m_prompt;
232         // Currently edited line
233         std::wstring m_line;
234         // History buffer
235         std::vector<std::wstring> m_history;
236         // History index (0 <= m_history_index <= m_history.size())
237         u32 m_history_index;
238         // Maximum number of history entries
239         u32 m_history_limit;
240
241         // Number of columns excluding columns reserved for the prompt
242         s32 m_cols;
243         // Start of visible portion (index into m_line)
244         s32 m_view;
245         // Cursor (index into m_line)
246         s32 m_cursor;
247         // Cursor length (length of selected portion of line)
248         s32 m_cursor_len;
249
250         // Last nick completion start (index into m_line)
251         s32 m_nick_completion_start;
252         // Last nick completion start (index into m_line)
253         s32 m_nick_completion_end;
254 };
255
256 class ChatBackend
257 {
258 public:
259         ChatBackend();
260         ~ChatBackend();
261
262         // Add chat message
263         void addMessage(std::wstring name, std::wstring text);
264         // Parse and add unparsed chat message
265         void addUnparsedMessage(std::wstring line);
266
267         // Get the console buffer
268         ChatBuffer& getConsoleBuffer();
269         // Get the recent messages buffer
270         ChatBuffer& getRecentBuffer();
271         // Concatenate all recent messages
272         EnrichedString getRecentChat();
273         // Get the console prompt
274         ChatPrompt& getPrompt();
275
276         // Reformat all buffers
277         void reformat(u32 cols, u32 rows);
278
279         // Clear all recent messages
280         void clearRecentChat();
281
282         // Age recent messages
283         void step(float dtime);
284
285         // Scrolling
286         void scroll(s32 rows);
287         void scrollPageDown();
288         void scrollPageUp();
289
290 private:
291         ChatBuffer m_console_buffer;
292         ChatBuffer m_recent_buffer;
293         ChatPrompt m_prompt;
294 };
295
296 #endif
297