Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / supertux / console.hpp
1 //  SuperTux - Console
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #ifndef HEADER_SUPERTUX_SUPERTUX_CONSOLE_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_CONSOLE_HPP
19
20 #include <list>
21 #include <memory>
22 #include <squirrel.h>
23 #include <sstream>
24 #include <vector>
25
26 class Console;
27 class ConsoleStreamBuffer;
28 class ConsoleCommandReceiver;
29 class DrawingContext;
30 class Surface;
31 class Font;
32
33 class Console
34 {
35 public:
36   Console();
37   ~Console();
38
39   static Console* instance;
40
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. */
42
43   void init_graphics();
44
45   void input(char c); /**< add character to inputBuffer */
46   void backspace(); /**< delete character left of inputBufferPosition */
47   void eraseChar(); /**< delete character at inputBufferPosition */
48   void enter(); /**< process and clear input stream */
49   void scroll(int offset); /**< scroll console text up or down by @c offset lines */
50   void autocomplete(); /**< autocomplete current command */
51   void show_history(int offset); /**< move @c offset lines forward through history; Negative offset moves backward */
52   void move_cursor(int offset); /**< move the cursor @c offset chars to the right; Negative offset moves backward; 0xFFFF moves to the end */
53
54   void draw(DrawingContext& context); /**< draw the console in a DrawingContext */
55   void update(float elapsed_time);
56
57   void show(); /**< display the console */
58   void hide(); /**< hide the console */
59   void toggle(); /**< display the console if hidden, hide otherwise */
60
61   bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */
62
63   template<typename T> static bool string_is(std::string s) {
64     std::istringstream iss(s);
65     T i;
66     if ((iss >> i) && iss.eof()) {
67       return true;
68     } else {
69       return false;
70     }
71   }
72
73   template<typename T> static T string_to(std::string s) {
74     std::istringstream iss(s);
75     T i;
76     if ((iss >> i) && iss.eof()) {
77       return i;
78     } else {
79       return T();
80     }
81   }
82
83 private:
84   std::list<std::string> history; /**< command history. New lines get added to back. */
85   std::list<std::string>::iterator history_position; /**< item of command history that is currently displayed */
86   std::list<std::string> lines; /**< backbuffer of lines sent to the console. New lines get added to front. */
87
88   std::auto_ptr<Surface> background; /**< console background image */
89   std::auto_ptr<Surface> background2; /**< second, moving console background image */
90
91   HSQUIRRELVM vm; /**< squirrel thread for the console (with custom roottable) */
92   HSQOBJECT vm_object;
93
94   int backgroundOffset; /**< current offset of scrolling background image */
95   float height; /**< height of the console in px */
96   float alpha;
97   int offset; /**< decrease to scroll text up */
98   bool focused; /**< true if console has input focus */
99   std::auto_ptr<Font> font;
100   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 */
101
102   float stayOpen;
103
104   static int inputBufferPosition; /**< position in inputBuffer before which to append new characters */
105   static std::string inputBuffer; /**< string used for keyboard input */
106   static ConsoleStreamBuffer outputBuffer; /**< stream buffer used by output stream */
107
108   void addLines(std::string s); /**< display a string of (potentially) multiple lines in the console */
109   void addLine(std::string s); /**< display a line in the console */
110   void parse(std::string s); /**< react to a given command */
111
112   /** ready a virtual machine instance, creating a new thread and loading default .nut files if needed */
113   void ready_vm();
114
115   /** execute squirrel script and output result */
116   void execute_script(const std::string& s);
117
118   bool consoleCommand(std::string command, std::vector<std::string> arguments); /**< process internal command; return false if command was unknown, true otherwise */
119
120   friend class ConsoleStreamBuffer;
121   void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */
122
123 private:
124   Console(const Console&);
125   Console & operator=(const Console&);
126 };
127
128 class ConsoleStreamBuffer : public std::stringbuf
129 {
130 public:
131   int sync()
132   {
133     int result = std::stringbuf::sync();
134     if(Console::instance != NULL)
135       Console::instance->flush(this);
136     return result;
137   }
138 };
139
140 #endif
141
142 /* EOF */