new images for editor
[supertux.git] / src / console.hpp
1 //  $Id$
2 //
3 //  SuperTux - Console
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #ifndef SUPERTUX_CONSOLE_H
21 #define SUPERTUX_CONSOLE_H
22
23 #include <list>
24 #include <map>
25 #include <vector>
26 #include <string>
27 #include <sstream>
28 #include <iostream>
29 #include <squirrel.h>
30
31 class Console;
32 class ConsoleStreamBuffer;
33 class ConsoleCommandReceiver;
34 class DrawingContext;
35 class Surface;
36 class Font;
37
38 class Console 
39 {
40 public:
41   Console();
42   ~Console();
43
44   static Console* instance;
45
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. */
48
49   void backspace(); /**< delete last character sent to the input stream */
50   void scroll(int offset); /**< scroll console text up or down by @c offset lines */
51   void autocomplete(); /**< autocomplete current command */
52
53   void draw(DrawingContext& context); /**< draw the console in a DrawingContext */
54   void update(float elapsed_time);
55   
56   void show(); /**< display the console */
57   void hide(); /**< hide the console */
58   void toggle(); /**< display the console if hidden, hide otherwise */
59
60   bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */
61   void registerCommand(std::string command, ConsoleCommandReceiver* ccr); /**< associate command with the given CCR */
62   void unregisterCommand(std::string command, ConsoleCommandReceiver* ccr); /**< dissociate command and CCR */
63   void unregisterCommands(ConsoleCommandReceiver* ccr); /**< dissociate all commands of given CCR */
64
65   template<typename T> static bool string_is(std::string s) {
66     std::istringstream iss(s);
67     T i;
68     if ((iss >> i) && iss.eof()) {
69       return true;
70     } else {
71       return false;
72     }
73   }
74
75   template<typename T> static T string_to(std::string s) {
76     std::istringstream iss(s);
77     T i;
78     if ((iss >> i) && iss.eof()) {
79       return i;
80     } else {
81       return T();
82     }
83   }
84
85 private:
86   std::list<std::string> lines; /**< backbuffer of lines sent to the console */
87   std::map<std::string, std::list<ConsoleCommandReceiver*> > commands; /**< map of console commands and a list of associated ConsoleCommandReceivers */
88   
89   std::auto_ptr<Surface> background; /**< console background image */
90   std::auto_ptr<Surface> background2; /**< second, moving console background image */
91
92   HSQUIRRELVM vm; /**< squirrel thread for the console (with custom roottable */
93   HSQOBJECT vm_object;
94   
95   int backgroundOffset; /**< current offset of scrolling background image */
96   float height; /**< height of the console in px */
97   float alpha;
98   int offset; /**< decrease to scroll text up */
99   bool focused; /**< true if console has input focus */
100   std::auto_ptr<Font> font;
101
102   float stayOpen;
103
104   static ConsoleStreamBuffer inputBuffer; /**< stream buffer used by input stream */
105   static ConsoleStreamBuffer outputBuffer; /**< stream buffer used by output stream */
106
107   void addLine(std::string s); /**< display a line in the console */
108   void parse(std::string s); /**< react to a given command */
109     
110   /** execute squirrel script and output result */
111   void execute_script(const std::string& s);
112     
113   bool consoleCommand(std::string command, std::vector<std::string> arguments); /**< process internal command; return false if command was unknown, true otherwise */
114
115   friend class ConsoleStreamBuffer;
116   void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */
117 };
118
119 class ConsoleStreamBuffer : public std::stringbuf 
120 {
121   public:
122     int sync() 
123     {
124       int result = std::stringbuf::sync();
125       if(Console::instance != NULL)
126         Console::instance->flush(this);
127       return result;
128     }
129 };
130
131 class ConsoleCommandReceiver
132 {
133 public:
134   virtual ~ConsoleCommandReceiver()
135   {
136     Console::instance->unregisterCommands(this);
137   }
138    
139   /**
140    * callback from Console; return false if command was unknown,
141    * true otherwise
142    */
143   virtual bool consoleCommand(std::string command, std::vector<std::string> arguments) = 0;
144 };
145
146 #endif