]> git.lizzy.rs Git - dragonfireclient.git/blob - src/chat.h
Translated using Weblate (German)
[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(const std::wstring &a_name, const std::wstring &a_text):
42                 age(0.0),
43                 name(a_name),
44                 text(a_text)
45         {
46         }
47
48         ChatLine(const EnrichedString &a_name, const 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 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 columns, 0 if reformat has not been called yet.
100         u32 getColumns() const;
101         // Get number of rows, 0 if reformat has not been called yet.
102         u32 getRows() const;
103         // Update console size and reformat all formatted lines.
104         void reformat(u32 cols, u32 rows);
105         // Get formatted line for a given row (0 is top of screen).
106         // Only valid after reformat has been called at least once
107         const ChatFormattedLine& getFormattedLine(u32 row) const;
108         // Scrolling in formatted buffer (relative)
109         // positive rows == scroll up, negative rows == scroll down
110         void scroll(s32 rows);
111         // Scrolling in formatted buffer (absolute)
112         void scrollAbsolute(s32 scroll);
113         // Scroll to bottom of buffer (newest)
114         void scrollBottom();
115         // Scroll to top of buffer (oldest)
116         void scrollTop();
117
118         // Format a chat line for the given number of columns.
119         // Appends the formatted lines to the destination array and
120         // returns the number of formatted lines.
121         u32 formatChatLine(const ChatLine& line, u32 cols,
122                         std::vector<ChatFormattedLine>& destination) const;
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;
136         // Number of character rows in console
137         u32 m_rows;
138         // Scroll position (console's top line index into m_formatted)
139         s32 m_scroll;
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
146 class ChatPrompt
147 {
148 public:
149         ChatPrompt(const std::wstring &prompt, u32 history_limit);
150         ~ChatPrompt();
151
152         // Input character or string
153         void input(wchar_t ch);
154         void input(const std::wstring &str);
155
156         // Add a string to the history
157         void addToHistory(std::wstring line);
158
159         // Get current line
160         std::wstring getLine() const { return m_line; }
161
162         // Get section of line that is currently selected
163         std::wstring getSelection() const { return m_line.substr(m_cursor, m_cursor_len); }
164
165         // Clear the current line
166         void clear();
167
168         // Replace the current line with the given text
169         std::wstring replace(std::wstring line);
170
171         // Select previous command from history
172         void historyPrev();
173         // Select next command from history
174         void historyNext();
175
176         // Nick completion
177         void nickCompletion(const std::list<std::string>& names, bool backwards);
178
179         // Update console size and reformat the visible portion of the prompt
180         void reformat(u32 cols);
181         // Get visible portion of the prompt.
182         std::wstring getVisiblePortion() const;
183         // Get cursor position (relative to visible portion). -1 if invalid
184         s32 getVisibleCursorPosition() const;
185         // Get length of cursor selection
186         s32 getCursorLength() const { return m_cursor_len; }
187
188         // Cursor operations
189         enum CursorOp {
190                 CURSOROP_MOVE,
191                 CURSOROP_SELECT,
192                 CURSOROP_DELETE
193         };
194
195         // Cursor operation direction
196         enum CursorOpDir {
197                 CURSOROP_DIR_LEFT,
198                 CURSOROP_DIR_RIGHT
199         };
200
201         // Cursor operation scope
202         enum CursorOpScope {
203                 CURSOROP_SCOPE_CHARACTER,
204                 CURSOROP_SCOPE_WORD,
205                 CURSOROP_SCOPE_LINE,
206                 CURSOROP_SCOPE_SELECTION
207         };
208
209         // Cursor operation
210         // op specifies whether it's a move or delete operation
211         // dir specifies whether the operation goes left or right
212         // scope specifies how far the operation will reach (char/word/line)
213         // Examples:
214         //   cursorOperation(CURSOROP_MOVE, CURSOROP_DIR_RIGHT, CURSOROP_SCOPE_LINE)
215         //     moves the cursor to the end of the line.
216         //   cursorOperation(CURSOROP_DELETE, CURSOROP_DIR_LEFT, CURSOROP_SCOPE_WORD)
217         //     deletes the word to the left of the cursor.
218         void cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope);
219
220 protected:
221         // set m_view to ensure that 0 <= m_view <= m_cursor < m_view + m_cols
222         // if line can be fully shown, set m_view to zero
223         // else, also ensure m_view <= m_line.size() + 1 - m_cols
224         void clampView();
225
226 private:
227         // Prompt prefix
228         std::wstring m_prompt;
229         // Currently edited line
230         std::wstring m_line;
231         // History buffer
232         std::vector<std::wstring> m_history;
233         // History index (0 <= m_history_index <= m_history.size())
234         u32 m_history_index;
235         // Maximum number of history entries
236         u32 m_history_limit;
237
238         // Number of columns excluding columns reserved for the prompt
239         s32 m_cols;
240         // Start of visible portion (index into m_line)
241         s32 m_view;
242         // Cursor (index into m_line)
243         s32 m_cursor;
244         // Cursor length (length of selected portion of line)
245         s32 m_cursor_len;
246
247         // Last nick completion start (index into m_line)
248         s32 m_nick_completion_start;
249         // Last nick completion start (index into m_line)
250         s32 m_nick_completion_end;
251 };
252
253 class ChatBackend
254 {
255 public:
256         ChatBackend();
257         ~ChatBackend();
258
259         // Add chat message
260         void addMessage(std::wstring name, std::wstring text);
261         // Parse and add unparsed chat message
262         void addUnparsedMessage(std::wstring line);
263
264         // Get the console buffer
265         ChatBuffer& getConsoleBuffer();
266         // Get the recent messages buffer
267         ChatBuffer& getRecentBuffer();
268         // Concatenate all recent messages
269         EnrichedString getRecentChat();
270         // Get the console prompt
271         ChatPrompt& getPrompt();
272
273         // Reformat all buffers
274         void reformat(u32 cols, u32 rows);
275
276         // Clear all recent messages
277         void clearRecentChat();
278
279         // Age recent messages
280         void step(float dtime);
281
282         // Scrolling
283         void scroll(s32 rows);
284         void scrollPageDown();
285         void scrollPageUp();
286
287 private:
288         ChatBuffer m_console_buffer;
289         ChatBuffer m_recent_buffer;
290         ChatPrompt m_prompt;
291 };
292
293 #endif
294