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