3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
26 #include "irrlichttypes.h"
27 #include "util/enriched_string.h"
30 // Chat console related classes
36 // name of sending player, or empty if sent by server
41 ChatLine(const std::wstring &a_name, const std::wstring &a_text):
47 ChatLine(const EnrichedString &a_name, const EnrichedString &a_text):
54 struct ChatFormattedFragment
64 struct ChatFormattedLine
66 // Array of text fragments
67 std::vector<ChatFormattedFragment> fragments;
68 // true if first line of one formatted ChatLine
75 ChatBuffer(u32 scrollback);
76 ~ChatBuffer() = default;
79 // Removes oldest chat line if scrollback size is reached
80 void addLine(std::wstring name, std::wstring text);
82 // Remove all chat lines
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;
90 // Increase each chat line's age by dtime.
92 // Delete oldest N chat lines.
93 void deleteOldest(u32 count);
94 // Delete lines older than maxAge.
95 void deleteByAge(f32 maxAge);
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.
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)
113 // Scroll to top of buffer (oldest)
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;
122 void resize(u32 scrollback);
124 s32 getTopScrollPos() const;
125 s32 getBottomScrollPos() const;
130 // Array of unformatted chat lines
131 std::vector<ChatLine> m_unformatted;
133 // Number of character columns in console
135 // Number of character rows in console
137 // Scroll position (console's top line index into m_formatted)
139 // Array of formatted lines
140 std::vector<ChatFormattedLine> m_formatted;
141 // Empty formatted line, for error returns
142 ChatFormattedLine m_empty_formatted_line;
148 ChatPrompt(const std::wstring &prompt, u32 history_limit);
149 ~ChatPrompt() = default;
151 // Input character or string
152 void input(wchar_t ch);
153 void input(const std::wstring &str);
155 // Add a string to the history
156 void addToHistory(std::wstring line);
159 std::wstring getLine() const { return m_line; }
161 // Get section of line that is currently selected
162 std::wstring getSelection() const { return m_line.substr(m_cursor, m_cursor_len); }
164 // Clear the current line
167 // Replace the current line with the given text
168 std::wstring replace(std::wstring line);
170 // Select previous command from history
172 // Select next command from history
176 void nickCompletion(const std::list<std::string>& names, bool backwards);
178 // Update console size and reformat the visible portion of the prompt
179 void reformat(u32 cols);
180 // Get visible portion of the prompt.
181 std::wstring getVisiblePortion() const;
182 // Get cursor position (relative to visible portion). -1 if invalid
183 s32 getVisibleCursorPosition() const;
184 // Get length of cursor selection
185 s32 getCursorLength() const { return m_cursor_len; }
194 // Cursor operation direction
200 // Cursor operation scope
202 CURSOROP_SCOPE_CHARACTER,
205 CURSOROP_SCOPE_SELECTION
209 // op specifies whether it's a move or delete operation
210 // dir specifies whether the operation goes left or right
211 // scope specifies how far the operation will reach (char/word/line)
213 // cursorOperation(CURSOROP_MOVE, CURSOROP_DIR_RIGHT, CURSOROP_SCOPE_LINE)
214 // moves the cursor to the end of the line.
215 // cursorOperation(CURSOROP_DELETE, CURSOROP_DIR_LEFT, CURSOROP_SCOPE_WORD)
216 // deletes the word to the left of the cursor.
217 void cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope);
220 // set m_view to ensure that 0 <= m_view <= m_cursor < m_view + m_cols
221 // if line can be fully shown, set m_view to zero
222 // else, also ensure m_view <= m_line.size() + 1 - m_cols
227 std::wstring m_prompt = L"";
228 // Currently edited line
229 std::wstring m_line = L"";
231 std::vector<std::wstring> m_history;
232 // History index (0 <= m_history_index <= m_history.size())
233 u32 m_history_index = 0;
234 // Maximum number of history entries
237 // Number of columns excluding columns reserved for the prompt
239 // Start of visible portion (index into m_line)
241 // Cursor (index into m_line)
243 // Cursor length (length of selected portion of line)
244 s32 m_cursor_len = 0;
246 // Last nick completion start (index into m_line)
247 s32 m_nick_completion_start = 0;
248 // Last nick completion start (index into m_line)
249 s32 m_nick_completion_end = 0;
256 ~ChatBackend() = default;
259 void addMessage(std::wstring name, std::wstring text);
260 // Parse and add unparsed chat message
261 void addUnparsedMessage(std::wstring line);
263 // Get the console buffer
264 ChatBuffer& getConsoleBuffer();
265 // Get the recent messages buffer
266 ChatBuffer& getRecentBuffer();
267 // Concatenate all recent messages
268 EnrichedString getRecentChat();
269 // Get the console prompt
270 ChatPrompt& getPrompt();
272 // Reformat all buffers
273 void reformat(u32 cols, u32 rows);
275 // Clear all recent messages
276 void clearRecentChat();
278 // Age recent messages
279 void step(float dtime);
282 void scroll(s32 rows);
283 void scrollPageDown();
286 // Resize recent buffer based on settings
287 void applySettings();
290 ChatBuffer m_console_buffer;
291 ChatBuffer m_recent_buffer;