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.
27 #include "irrlichttypes.h"
28 #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):
48 ChatLine(const EnrichedString &a_name, const EnrichedString &a_text):
56 struct ChatFormattedFragment
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);
81 // Removes oldest chat line if scrollback size is reached
82 void addLine(std::wstring name, std::wstring text);
84 // Remove all chat lines
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;
94 // Increase each chat line's age by dtime.
96 // Delete oldest N chat lines.
97 void deleteOldest(u32 count);
98 // Delete lines older than maxAge.
99 void deleteByAge(f32 maxAge);
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.
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)
117 // Scroll to top of buffer (oldest)
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;
127 s32 getTopScrollPos() const;
128 s32 getBottomScrollPos() const;
133 // Array of unformatted chat lines
134 std::vector<ChatLine> m_unformatted;
136 // Number of character columns in console
138 // Number of character rows in console
140 // Scroll position (console's top line index into m_formatted)
142 // Array of formatted lines
143 std::vector<ChatFormattedLine> m_formatted;
144 // Empty formatted line, for error returns
145 ChatFormattedLine m_empty_formatted_line;
151 ChatPrompt(const std::wstring &prompt, u32 history_limit);
154 // Input character or string
155 void input(wchar_t ch);
156 void input(const std::wstring &str);
158 // Add a string to the history
159 void addToHistory(std::wstring line);
162 std::wstring getLine() const { return m_line; }
164 // Get section of line that is currently selected
165 std::wstring getSelection() const
166 { return m_line.substr(m_cursor, m_cursor_len); }
168 // Clear the current line
171 // Replace the current line with the given text
172 std::wstring replace(std::wstring line);
174 // Select previous command from history
176 // Select next command from history
180 void nickCompletion(const std::list<std::string>& names, bool backwards);
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; }
198 // Cursor operation direction
204 // Cursor operation scope
206 CURSOROP_SCOPE_CHARACTER,
209 CURSOROP_SCOPE_SELECTION
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)
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);
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
231 std::wstring m_prompt;
232 // Currently edited line
235 std::vector<std::wstring> m_history;
236 // History index (0 <= m_history_index <= m_history.size())
238 // Maximum number of history entries
241 // Number of columns excluding columns reserved for the prompt
243 // Start of visible portion (index into m_line)
245 // Cursor (index into m_line)
247 // Cursor length (length of selected portion of line)
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;
263 void addMessage(std::wstring name, std::wstring text);
264 // Parse and add unparsed chat message
265 void addUnparsedMessage(std::wstring line);
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();
276 // Reformat all buffers
277 void reformat(u32 cols, u32 rows);
279 // Clear all recent messages
280 void clearRecentChat();
282 // Age recent messages
283 void step(float dtime);
286 void scroll(s32 rows);
287 void scrollPageDown();
291 ChatBuffer m_console_buffer;
292 ChatBuffer m_recent_buffer;