Removed some unused code from Console
[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 #include "video/font_ptr.hpp"
27 #include "video/surface_ptr.hpp"
28
29 class Console;
30 class ConsoleStreamBuffer;
31 class ConsoleCommandReceiver;
32 class DrawingContext;
33
34 class Console
35 {
36 public:
37   Console();
38   ~Console();
39
40   static Console* instance;
41
42   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
44   void init_graphics();
45
46   void input(char c); /**< add character to inputBuffer */
47   void backspace(); /**< delete character left of inputBufferPosition */
48   void eraseChar(); /**< delete character at inputBufferPosition */
49   void enter(); /**< process and clear input stream */
50   void scroll(int offset); /**< scroll console text up or down by @c offset lines */
51   void autocomplete(); /**< autocomplete current command */
52   void show_history(int offset); /**< move @c offset lines forward through history; Negative offset moves backward */
53   void move_cursor(int offset); /**< move the cursor @c offset chars to the right; Negative offset moves backward; 0xFFFF moves to the end */
54
55   void draw(DrawingContext& context); /**< draw the console in a DrawingContext */
56   void update(float elapsed_time);
57
58   void show(); /**< display the console */
59   void open(); /**< open the console for viewing for 6 seconds */
60   void hide(); /**< hide the console */
61   void toggle(); /**< display the console if hidden, hide otherwise */
62
63   bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */
64
65 private:
66   std::list<std::string> history; /**< command history. New lines get added to back. */
67   std::list<std::string>::iterator history_position; /**< item of command history that is currently displayed */
68   std::list<std::string> lines; /**< backbuffer of lines sent to the console. New lines get added to front. */
69
70   SurfacePtr background; /**< console background image */
71   SurfacePtr background2; /**< second, moving console background image */
72
73   HSQUIRRELVM vm; /**< squirrel thread for the console (with custom roottable) */
74   HSQOBJECT vm_object;
75
76   int backgroundOffset; /**< current offset of scrolling background image */
77   float height; /**< height of the console in px */
78   float alpha;
79   int offset; /**< decrease to scroll text up */
80   bool focused; /**< true if console has input focus */
81   FontPtr font;
82   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 */
83
84   float stayOpen;
85
86   static int inputBufferPosition; /**< position in inputBuffer before which to append new characters */
87   static std::string inputBuffer; /**< string used for keyboard input */
88   static ConsoleStreamBuffer outputBuffer; /**< stream buffer used by output stream */
89
90   void addLines(std::string s); /**< display a string of (potentially) multiple lines in the console */
91   void addLine(std::string s); /**< display a line in the console */
92   void parse(std::string s); /**< react to a given command */
93
94   /** ready a virtual machine instance, creating a new thread and loading default .nut files if needed */
95   void ready_vm();
96
97   /** execute squirrel script and output result */
98   void execute_script(const std::string& s);
99
100   bool consoleCommand(std::string command, std::vector<std::string> arguments); /**< process internal command; return false if command was unknown, true otherwise */
101
102   friend class ConsoleStreamBuffer;
103   void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */
104
105 private:
106   Console(const Console&);
107   Console & operator=(const Console&);
108 };
109
110 class ConsoleStreamBuffer : public std::stringbuf
111 {
112 public:
113   int sync()
114   {
115     int result = std::stringbuf::sync();
116     if(Console::instance != NULL)
117       Console::instance->flush(this);
118     return result;
119   }
120 };
121
122 #endif
123
124 /* EOF */