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