4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #ifndef SUPERTUX_CONSOLE_H
21 #define SUPERTUX_CONSOLE_H
32 class ConsoleStreamBuffer;
33 class ConsoleCommandReceiver;
44 static Console* instance;
46 static std::ostream input; /**< stream of keyboard input to send to the console. Do not forget to send std::endl or to flush the stream. */
47 static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */
51 void backspace(); /**< delete last character sent to the input stream */
52 void enter(); /**< process and clear input stream */
53 void scroll(int offset); /**< scroll console text up or down by @c offset lines */
54 void autocomplete(); /**< autocomplete current command */
55 void show_history(int offset); /**< move @c offset lines forward through history; Negative offset moves backward */
56 void move_cursor(int offset); /**< move the cursor @c offset chars to the right; Negative offset moves backward; 0xFFFF moves to the end */
58 void draw(DrawingContext& context); /**< draw the console in a DrawingContext */
59 void update(float elapsed_time);
61 void show(); /**< display the console */
62 void hide(); /**< hide the console */
63 void toggle(); /**< display the console if hidden, hide otherwise */
65 bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */
67 template<typename T> static bool string_is(std::string s) {
68 std::istringstream iss(s);
70 if ((iss >> i) && iss.eof()) {
77 template<typename T> static T string_to(std::string s) {
78 std::istringstream iss(s);
80 if ((iss >> i) && iss.eof()) {
88 std::list<std::string> history; /**< command history. New lines get added to back. */
89 std::list<std::string>::iterator history_position; /**< item of command history that is currently displayed */
90 std::list<std::string> lines; /**< backbuffer of lines sent to the console. New lines get added to front. */
92 std::auto_ptr<Surface> background; /**< console background image */
93 std::auto_ptr<Surface> background2; /**< second, moving console background image */
95 HSQUIRRELVM vm; /**< squirrel thread for the console (with custom roottable) */
98 int backgroundOffset; /**< current offset of scrolling background image */
99 float height; /**< height of the console in px */
101 int offset; /**< decrease to scroll text up */
102 bool focused; /**< true if console has input focus */
103 std::auto_ptr<Font> font;
104 float fontheight; /**< height of the font (this is a separate var, because the font could not be initialized yet but is needed in the addLine message */
108 static ConsoleStreamBuffer inputBuffer; /**< stream buffer used by input stream */
109 static ConsoleStreamBuffer outputBuffer; /**< stream buffer used by output stream */
111 void addLines(std::string s); /**< display a string of (potentially) multiple lines in the console */
112 void addLine(std::string s); /**< display a line in the console */
113 void parse(std::string s); /**< react to a given command */
115 /** ready a virtual machine instance, creating a new thread and loading default .nut files if needed */
118 /** execute squirrel script and output result */
119 void execute_script(const std::string& s);
121 bool consoleCommand(std::string command, std::vector<std::string> arguments); /**< process internal command; return false if command was unknown, true otherwise */
123 friend class ConsoleStreamBuffer;
124 void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */
127 class ConsoleStreamBuffer : public std::stringbuf
132 int result = std::stringbuf::sync();
133 if(Console::instance != NULL)
134 Console::instance->flush(this);