Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[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
30 class Console;
31 class ConsoleStreamBuffer;
32 class ConsoleCommandReceiver;
33 class DrawingContext;
34 class Surface;
35 class Font;
36
37 class Console 
38 {
39 public:
40   Console();
41   ~Console();
42
43   static Console* instance;
44
45   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. */
46   static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */
47
48   void backspace(); /**< delete last character sent to the input stream */
49   void scroll(int offset); /**< scroll console text up or down by @c offset lines */
50   void autocomplete(); /**< autocomplete current command */
51
52   void draw(DrawingContext& context); /**< draw the console in a DrawingContext */
53   void update(float elapsed_time);
54   
55   void show(); /**< display the console */
56   void hide(); /**< hide the console */
57   void toggle(); /**< display the console if hidden, hide otherwise */
58
59   bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */
60   void registerCommand(std::string command, ConsoleCommandReceiver* ccr); /**< associate command with the given CCR */
61   void unregisterCommand(std::string command, ConsoleCommandReceiver* ccr); /**< dissociate command and CCR */
62   void unregisterCommands(ConsoleCommandReceiver* ccr); /**< dissociate all commands of given CCR */
63
64   template<typename T> static bool string_is(std::string s) {
65     std::istringstream iss(s);
66     T i;
67     if ((iss >> i) && iss.eof()) {
68       return true;
69     } else {
70       return false;
71     }
72   }
73
74   template<typename T> static T string_to(std::string s) {
75     std::istringstream iss(s);
76     T i;
77     if ((iss >> i) && iss.eof()) {
78       return i;
79     } else {
80       return T();
81     }
82   }
83
84 private:
85   std::list<std::string> lines; /**< backbuffer of lines sent to the console */
86   std::map<std::string, std::list<ConsoleCommandReceiver*> > commands; /**< map of console commands and a list of associated ConsoleCommandReceivers */
87   
88   std::auto_ptr<Surface> background; /**< console background image */
89   std::auto_ptr<Surface> background2; /**< second, moving console background image */
90   
91   int backgroundOffset; /**< current offset of scrolling background image */
92   float height; /**< height of the console in px */
93   float alpha;
94   int offset; /**< decrease to scroll text up */
95   bool focused; /**< true if console has input focus */
96   std::auto_ptr<Font> font;
97
98   float stayOpen;
99
100   static ConsoleStreamBuffer inputBuffer; /**< stream buffer used by input stream */
101   static ConsoleStreamBuffer outputBuffer; /**< stream buffer used by output stream */
102
103   void addLine(std::string s); /**< display a line in the console */
104   void parse(std::string s); /**< react to a given command */
105     
106   /** execute squirrel script and output result */
107   void execute_script(const std::string& s);
108     
109   bool consoleCommand(std::string command, std::vector<std::string> arguments); /**< process internal command; return false if command was unknown, true otherwise */
110
111   friend class ConsoleStreamBuffer;
112   void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */
113 };
114
115 class ConsoleStreamBuffer : public std::stringbuf 
116 {
117   public:
118     int sync() 
119     {
120       int result = std::stringbuf::sync();
121       if(Console::instance != NULL)
122         Console::instance->flush(this);
123       return result;
124     }
125 };
126
127 class ConsoleCommandReceiver
128 {
129 public:
130   virtual ~ConsoleCommandReceiver()
131   {
132     Console::instance->unregisterCommands(this);
133   }
134    
135   /**
136    * callback from Console; return false if command was unknown,
137    * true otherwise
138    */
139   virtual bool consoleCommand(std::string command, std::vector<std::string> arguments) = 0;
140 };
141
142 #endif