2 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef HEADER_SUPERTUX_SUPERTUX_CONSOLE_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_CONSOLE_HPP
26 #include "util/currenton.hpp"
27 #include "video/font_ptr.hpp"
28 #include "video/surface_ptr.hpp"
31 class ConsoleStreamBuffer;
32 class ConsoleCommandReceiver;
35 class Console : public Currenton<Console>
41 static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */
43 void input(char c); /**< add character to inputBuffer */
44 void backspace(); /**< delete character left of inputBufferPosition */
45 void eraseChar(); /**< delete character at inputBufferPosition */
46 void enter(); /**< process and clear input stream */
47 void scroll(int offset); /**< scroll console text up or down by @c offset lines */
48 void autocomplete(); /**< autocomplete current command */
49 void show_history(int offset); /**< move @c offset lines forward through history; Negative offset moves backward */
50 void move_cursor(int offset); /**< move the cursor @c offset chars to the right; Negative offset moves backward; 0xFFFF moves to the end */
52 void draw(DrawingContext& context); /**< draw the console in a DrawingContext */
53 void update(float elapsed_time);
55 void show(); /**< display the console */
56 void open(); /**< open the console for viewing for 6 seconds */
57 void hide(); /**< hide the console */
58 void toggle(); /**< display the console if hidden, hide otherwise */
60 bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */
63 std::list<std::string> m_history; /**< command history. New lines get added to back. */
64 std::list<std::string>::iterator m_history_position; /**< item of command history that is currently displayed */
65 std::list<std::string> m_lines; /**< backbuffer of lines sent to the console. New lines get added to front. */
67 SurfacePtr m_background; /**< console background image */
68 SurfacePtr m_background2; /**< second, moving console background image */
70 HSQUIRRELVM m_vm; /**< squirrel thread for the console (with custom roottable) */
71 HSQOBJECT m_vm_object;
73 int m_backgroundOffset; /**< current offset of scrolling background image */
74 float m_height; /**< height of the console in px */
76 int m_offset; /**< decrease to scroll text up */
77 bool m_focused; /**< true if console has input focus */
82 static int inputBufferPosition; /**< position in inputBuffer before which to append new characters */
83 static std::string inputBuffer; /**< string used for keyboard input */
84 static ConsoleStreamBuffer outputBuffer; /**< stream buffer used by output stream */
86 void addLines(std::string s); /**< display a string of (potentially) multiple lines in the console */
87 void addLine(std::string s); /**< display a line in the console */
88 void parse(std::string s); /**< react to a given command */
90 /** ready a virtual machine instance, creating a new thread and loading default .nut files if needed */
93 /** execute squirrel script and output result */
94 void execute_script(const std::string& s);
96 bool consoleCommand(std::string command, std::vector<std::string> arguments); /**< process internal command; return false if command was unknown, true otherwise */
98 friend class ConsoleStreamBuffer;
99 void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */
102 Console(const Console&);
103 Console & operator=(const Console&);
106 class ConsoleStreamBuffer : public std::stringbuf
111 int result = std::stringbuf::sync();
112 if(Console::current())
113 Console::current()->flush(this);