62d264cd47d0ade8671f22b590b08e18af1c2e87
[supertux.git] / src / console.cpp
1 //  $Id: worldmap.cpp 3209 2006-04-02 22:19:22Z sommer $
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 #include <config.h>
21 #include <iostream>
22 #include "console.hpp"
23 #include "video/drawing_context.hpp"
24 #include "video/surface.hpp"
25 #include "player_status.hpp"
26 #include "main.hpp"
27 #include "resources.hpp"
28
29 namespace {
30   int ticks; // TODO: use a clock?
31 }
32
33 Console::Console()
34 {
35   background = new Surface("images/engine/console.jpg");
36 }
37
38 Console::~Console() 
39 {
40   delete background;
41 }
42
43 void 
44 Console::flush(ConsoleStreamBuffer* buffer) 
45 {
46   if (buffer == &outputBuffer) {
47     std::string s = outputBuffer.str();
48     if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
49       while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
50       addLine(s);
51       outputBuffer.str(std::string());
52     }
53   }
54   if (buffer == &inputBuffer) {
55     std::string s = inputBuffer.str();
56     if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
57       while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
58       addLine("> "+s);
59       parse(s);
60       inputBuffer.str(std::string());
61     }
62   }
63 }
64
65 void
66 Console::backspace()
67 {
68   std::string s = inputBuffer.str();
69   if (s.length() > 0) {
70     s.erase(s.length()-1);
71     inputBuffer.str(s);
72     inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
73   }
74 }
75
76 void
77 Console::scroll(int numLines)
78 {
79   offset += numLines;
80   if (offset > 0) offset = 0;
81 }
82
83 void
84 Console::autocomplete()
85 {
86   std::string cmdPart = inputBuffer.str();
87   addLine("> "+cmdPart);
88
89   std::string cmdList = "";
90   int cmdListLen = 0;
91   for (std::map<std::string, ConsoleCommandReceiver*>::iterator i = commands.begin(); i != commands.end(); i++) {
92     std::string cmdKnown = i->first;
93     if (cmdKnown.substr(0, cmdPart.length()) == cmdPart) {
94       if (cmdListLen == 0) {
95         inputBuffer.str(cmdKnown);
96         inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
97       } else {
98         cmdList = cmdList + ", ";
99       }
100       cmdList = cmdList + cmdKnown;
101       cmdListLen++;
102     }
103   }
104   if (cmdListLen == 0) addLine("No known command starts with \""+cmdPart+"\"");
105   if (cmdListLen > 1) addLine(cmdList);
106 }
107
108 void 
109 Console::addLine(std::string s) 
110 {
111   lines.push_front(s);
112   if (lines.size() >= 256) lines.pop_back();
113   if (height < 64) {
114     if (height < 4+9) height=4+9;
115     height+=9;
116   }
117   ticks=60;
118   std::cerr << s << std::endl;
119 }
120
121 void
122 Console::parse(std::string s) 
123 {
124   if (commands.find(s) == commands.end()) {
125     addLine("unknown command: \"" + s + "\"");
126     return;
127   }
128   ConsoleCommandReceiver* ccr = commands[s];
129   if (ccr->consoleCommand(s) != true) msg_warning("Sent command to registered ccr, but command was unhandled");
130 }
131
132 bool
133 Console::hasFocus() 
134 {
135   return focused;
136 }
137
138 void
139 Console::show()
140 {
141   focused = true;
142   height = 256;
143 }
144
145 void 
146 Console::hide()
147 {
148   focused = false;
149   height = 0;
150 }
151
152 void 
153 Console::draw(DrawingContext& context)
154 {
155   if (height == 0) return;
156   if (!focused) {
157     if (ticks-- < 0) {
158       height-=10;
159       ticks=0;
160       if (height < 0) height=0;
161     }
162     if (height == 0) return;
163   }
164
165   context.draw_surface(background, Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), LAYER_FOREGROUND1+1);
166
167   int lineNo = 0;
168
169   if (focused) {
170     lineNo++;
171     float py = height-4-1*9;
172     context.draw_text(white_small_text, "> "+inputBuffer.str()+"_", Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
173   }
174
175   int skipLines = -offset;
176   for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
177     if (skipLines-- > 0) continue;
178     lineNo++;
179     float py = height-4-lineNo*9;
180     if (py < -9) break;
181     context.draw_text(white_small_text, *i, Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
182   }
183 }
184
185 void 
186 Console::registerCommand(std::string command, ConsoleCommandReceiver* ccr)
187 {
188   if (commands.find(command) != commands.end()) {
189     msg_warning("Command \"" << command << "\" already associated with a command receiver. Not associated.");
190     return;
191   }
192   commands[command] = ccr;
193 }
194
195 void 
196 Console::unregisterCommand(std::string command, ConsoleCommandReceiver* ccr)
197 {
198   if (commands.find(command) == commands.end()) {
199     msg_warning("Command \"" << command << "\" not associated with a command receiver. Not dissociated.");
200     return;
201   }
202   if (commands[command] != ccr) {
203     msg_warning("Command \"" << command << "\" associated with another command receiver. Not dissociated.");
204     return;
205   }
206   commands.erase(command);
207 }
208
209 int Console::height = 0;
210 bool Console::focused = false;
211 std::list<std::string> Console::lines;
212 std::map<std::string, ConsoleCommandReceiver*> Console::commands;
213 ConsoleStreamBuffer Console::inputBuffer;
214 ConsoleStreamBuffer Console::outputBuffer;
215 std::ostream Console::input(&Console::inputBuffer);
216 std::ostream Console::output(&Console::outputBuffer);
217 int Console::offset = 0;
218