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
60 // web link is empty for most frags
66 struct ChatFormattedLine
68 // Array of text fragments
69 std::vector<ChatFormattedFragment> fragments;
70 // true if first line of one formatted ChatLine
77 ChatBuffer(u32 scrollback);
78 ~ChatBuffer() = default;
81 // Removes oldest chat line if scrollback size is reached
82 void addLine(const std::wstring &name, const std::wstring &text);
84 // Remove all chat lines
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;
92 // Increase each chat line's age by dtime.
94 // Delete oldest N chat lines.
95 void deleteOldest(u32 count);
96 // Delete lines older than maxAge.
97 void deleteByAge(f32 maxAge);
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)
114 // Functions for keeping track of whether the lines were modified by any
115 // preceding operations
116 // If they were not changed, getLineCount() and getLine() output the same as
118 bool getLinesModified() const { return m_lines_modified; }
119 void resetLinesModified() { m_lines_modified = false; }
121 // Format a chat line for the given number of columns.
122 // Appends the formatted lines to the destination array and
123 // returns the number of formatted lines.
124 u32 formatChatLine(const ChatLine& line, u32 cols,
125 std::vector<ChatFormattedLine>& destination) const;
127 void resize(u32 scrollback);
130 s32 getTopScrollPos() const;
131 s32 getBottomScrollPos() const;
136 // Array of unformatted chat lines
137 std::vector<ChatLine> m_unformatted;
139 // Number of character columns in console
141 // Number of character rows in console
143 // Scroll position (console's top line index into m_formatted)
145 // Array of formatted lines
146 std::vector<ChatFormattedLine> m_formatted;
147 // Empty formatted line, for error returns
148 ChatFormattedLine m_empty_formatted_line;
150 // Enable clickable chat weblinks
151 bool m_cache_clickable_chat_weblinks;
152 // Color of clickable chat weblinks
153 irr::video::SColor m_cache_chat_weblink_color;
155 // Whether the lines were modified since last markLinesUnchanged()
156 // Is always set to true when m_unformatted is modified, because that's what
157 // determines the output of getLineCount() and getLine()
158 bool m_lines_modified = true;
164 ChatPrompt(const std::wstring &prompt, u32 history_limit);
165 ~ChatPrompt() = default;
167 // Input character or string
168 void input(wchar_t ch);
169 void input(const std::wstring &str);
171 // Add a string to the history
172 void addToHistory(const std::wstring &line);
175 std::wstring getLine() const { return m_line; }
177 // Get section of line that is currently selected
178 std::wstring getSelection() const { return m_line.substr(m_cursor, m_cursor_len); }
180 // Clear the current line
183 // Replace the current line with the given text
184 std::wstring replace(const std::wstring &line);
186 // Select previous command from history
188 // Select next command from history
192 void nickCompletion(const std::list<std::string>& names, bool backwards);
194 // Update console size and reformat the visible portion of the prompt
195 void reformat(u32 cols);
196 // Get visible portion of the prompt.
197 std::wstring getVisiblePortion() const;
198 // Get cursor position (relative to visible portion). -1 if invalid
199 s32 getVisibleCursorPosition() const;
200 // Get length of cursor selection
201 s32 getCursorLength() const { return m_cursor_len; }
210 // Cursor operation direction
216 // Cursor operation scope
218 CURSOROP_SCOPE_CHARACTER,
221 CURSOROP_SCOPE_SELECTION
225 // op specifies whether it's a move or delete operation
226 // dir specifies whether the operation goes left or right
227 // scope specifies how far the operation will reach (char/word/line)
229 // cursorOperation(CURSOROP_MOVE, CURSOROP_DIR_RIGHT, CURSOROP_SCOPE_LINE)
230 // moves the cursor to the end of the line.
231 // cursorOperation(CURSOROP_DELETE, CURSOROP_DIR_LEFT, CURSOROP_SCOPE_WORD)
232 // deletes the word to the left of the cursor.
233 void cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope);
236 // set m_view to ensure that 0 <= m_view <= m_cursor < m_view + m_cols
237 // if line can be fully shown, set m_view to zero
238 // else, also ensure m_view <= m_line.size() + 1 - m_cols
243 std::wstring m_prompt = L"";
244 // Currently edited line
245 std::wstring m_line = L"";
247 std::vector<std::wstring> m_history;
248 // History index (0 <= m_history_index <= m_history.size())
249 u32 m_history_index = 0;
250 // Maximum number of history entries
253 // Number of columns excluding columns reserved for the prompt
255 // Start of visible portion (index into m_line)
257 // Cursor (index into m_line)
259 // Cursor length (length of selected portion of line)
260 s32 m_cursor_len = 0;
262 // Last nick completion start (index into m_line)
263 s32 m_nick_completion_start = 0;
264 // Last nick completion start (index into m_line)
265 s32 m_nick_completion_end = 0;
272 ~ChatBackend() = default;
275 void addMessage(const std::wstring &name, std::wstring text);
276 // Parse and add unparsed chat message
277 void addUnparsedMessage(std::wstring line);
279 // Get the console buffer
280 ChatBuffer& getConsoleBuffer();
281 // Get the recent messages buffer
282 ChatBuffer& getRecentBuffer();
283 // Concatenate all recent messages
284 EnrichedString getRecentChat() const;
285 // Get the console prompt
286 ChatPrompt& getPrompt();
288 // Reformat all buffers
289 void reformat(u32 cols, u32 rows);
291 // Clear all recent messages
292 void clearRecentChat();
294 // Age recent messages
295 void step(float dtime);
298 void scroll(s32 rows);
299 void scrollPageDown();
302 // Resize recent buffer based on settings
303 void applySettings();
306 ChatBuffer m_console_buffer;
307 ChatBuffer m_recent_buffer;