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.
23 #include "irrlichttypes.h"
28 // Chat console related classes
34 // name of sending player, or empty if sent by server
39 ChatLine(std::wstring a_name, std::wstring a_text):
47 struct ChatFormattedFragment
57 struct ChatFormattedLine
59 // Array of text fragments
60 std::vector<ChatFormattedFragment> fragments;
61 // true if first line of one formatted ChatLine
68 ChatBuffer(u32 scrollback);
72 // Removes oldest chat line if scrollback size is reached
73 void addLine(std::wstring name, std::wstring text);
75 // Remove all chat lines
78 // Get number of lines currently in buffer.
79 u32 getLineCount() const;
80 // Get scrollback size, maximum number of lines in buffer.
81 u32 getScrollback() const;
82 // Get reference to i-th chat line.
83 const ChatLine& getLine(u32 index) const;
85 // Increase each chat line's age by dtime.
87 // Delete oldest N chat lines.
88 void deleteOldest(u32 count);
89 // Delete lines older than maxAge.
90 void deleteByAge(f32 maxAge);
92 // Get number of columns, 0 if reformat has not been called yet.
93 u32 getColumns() const;
94 // Get number of rows, 0 if reformat has not been called yet.
96 // Update console size and reformat all formatted lines.
97 void reformat(u32 cols, u32 rows);
98 // Get formatted line for a given row (0 is top of screen).
99 // Only valid after reformat has been called at least once
100 const ChatFormattedLine& getFormattedLine(u32 row) const;
101 // Scrolling in formatted buffer (relative)
102 // positive rows == scroll up, negative rows == scroll down
103 void scroll(s32 rows);
104 // Scrolling in formatted buffer (absolute)
105 void scrollAbsolute(s32 scroll);
106 // Scroll to bottom of buffer (newest)
108 // Scroll to top of buffer (oldest)
111 // Format a chat line for the given number of columns.
112 // Appends the formatted lines to the destination array and
113 // returns the number of formatted lines.
114 u32 formatChatLine(const ChatLine& line, u32 cols,
115 std::vector<ChatFormattedLine>& destination) const;
118 s32 getTopScrollPos() const;
119 s32 getBottomScrollPos() const;
124 // Array of unformatted chat lines
125 std::vector<ChatLine> m_unformatted;
127 // Number of character columns in console
129 // Number of character rows in console
131 // Scroll position (console's top line index into m_formatted)
133 // Array of formatted lines
134 std::vector<ChatFormattedLine> m_formatted;
135 // Empty formatted line, for error returns
136 ChatFormattedLine m_empty_formatted_line;
142 ChatPrompt(std::wstring prompt, u32 history_limit);
145 // Input character or string
146 void input(wchar_t ch);
147 void input(const std::wstring &str);
149 // Submit, clear and return current line
150 std::wstring submit();
152 // Clear the current line
155 // Replace the current line with the given text
156 void replace(std::wstring line);
158 // Select previous command from history
160 // Select next command from history
164 void nickCompletion(const std::list<std::string>& names, bool backwards);
166 // Update console size and reformat the visible portion of the prompt
167 void reformat(u32 cols);
168 // Get visible portion of the prompt.
169 std::wstring getVisiblePortion() const;
170 // Get cursor position (relative to visible portion). -1 if invalid
171 s32 getVisibleCursorPosition() const;
179 // Cursor operation direction
185 // Cursor operation scope
187 CURSOROP_SCOPE_CHARACTER,
193 // op specifies whether it's a move or delete operation
194 // dir specifies whether the operation goes left or right
195 // scope specifies how far the operation will reach (char/word/line)
197 // cursorOperation(CURSOROP_MOVE, CURSOROP_DIR_RIGHT, CURSOROP_SCOPE_LINE)
198 // moves the cursor to the end of the line.
199 // cursorOperation(CURSOROP_DELETE, CURSOROP_DIR_LEFT, CURSOROP_SCOPE_WORD)
200 // deletes the word to the left of the cursor.
201 void cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope);
204 // set m_view to ensure that 0 <= m_view <= m_cursor < m_view + m_cols
205 // if line can be fully shown, set m_view to zero
206 // else, also ensure m_view <= m_line.size() + 1 - m_cols
211 std::wstring m_prompt;
212 // Currently edited line
215 std::vector<std::wstring> m_history;
216 // History index (0 <= m_history_index <= m_history.size())
218 // Maximum number of history entries
221 // Number of columns excluding columns reserved for the prompt
223 // Start of visible portion (index into m_line)
225 // Cursor (index into m_line)
228 // Last nick completion start (index into m_line)
229 s32 m_nick_completion_start;
230 // Last nick completion start (index into m_line)
231 s32 m_nick_completion_end;
241 void addMessage(std::wstring name, std::wstring text);
242 // Parse and add unparsed chat message
243 void addUnparsedMessage(std::wstring line);
245 // Get the console buffer
246 ChatBuffer& getConsoleBuffer();
247 // Get the recent messages buffer
248 ChatBuffer& getRecentBuffer();
249 // Concatenate all recent messages
250 std::wstring getRecentChat();
251 // Get the console prompt
252 ChatPrompt& getPrompt();
254 // Reformat all buffers
255 void reformat(u32 cols, u32 rows);
257 // Clear all recent messages
258 void clearRecentChat();
260 // Age recent messages
261 void step(float dtime);
264 void scroll(s32 rows);
265 void scrollPageDown();
269 ChatBuffer m_console_buffer;
270 ChatBuffer m_recent_buffer;