Moved some console commands' implementations nearer to target classes
[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.png");
36   background2 = new Surface("images/engine/console2.png");
37 }
38
39 Console::~Console() 
40 {
41   delete background;
42   delete background2;
43 }
44
45 void 
46 Console::flush(ConsoleStreamBuffer* buffer) 
47 {
48   if (buffer == &outputBuffer) {
49     std::string s = outputBuffer.str();
50     if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
51       while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
52       addLine(s);
53       outputBuffer.str(std::string());
54     }
55   }
56   if (buffer == &inputBuffer) {
57     std::string s = inputBuffer.str();
58     if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
59       while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
60       addLine("> "+s);
61       parse(s);
62       inputBuffer.str(std::string());
63     }
64   }
65 }
66
67 void
68 Console::backspace()
69 {
70   std::string s = inputBuffer.str();
71   if (s.length() > 0) {
72     s.erase(s.length()-1);
73     inputBuffer.str(s);
74     inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
75   }
76 }
77
78 void
79 Console::scroll(int numLines)
80 {
81   offset += numLines;
82   if (offset > 0) offset = 0;
83 }
84
85 void
86 Console::autocomplete()
87 {
88   std::string cmdPart = inputBuffer.str();
89   addLine("> "+cmdPart);
90
91   std::string cmdList = "";
92   int cmdListLen = 0;
93   for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
94     std::string cmdKnown = i->first;
95     if (cmdKnown.substr(0, cmdPart.length()) == cmdPart) {
96       if (cmdListLen > 0) cmdList = cmdList + ", ";
97       cmdList = cmdList + cmdKnown;
98       cmdListLen++;
99     }
100   }
101   if (cmdListLen == 0) addLine("No known command starts with \""+cmdPart+"\"");
102   if (cmdListLen == 1) {
103     inputBuffer.str(cmdList);
104     inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
105   }
106   if (cmdListLen > 1) addLine(cmdList);
107 }
108
109 void 
110 Console::addLine(std::string s) 
111 {
112   std::cerr << s << std::endl;
113   while (s.length() > 99) {
114     lines.push_front(s.substr(0, 99-3)+"...");
115     s = "..."+s.substr(99-3);
116   }
117   lines.push_front(s);
118   while (lines.size() >= 65535) lines.pop_back();
119   if (height < 64) {
120     if (height < 4+9) height=4+9;
121     height+=9;
122   }
123   ticks=60;
124 }
125
126 void
127 Console::parse(std::string s) 
128 {
129   // make sure we actually have something to parse
130   if (s.length() == 0) return;
131         
132   // split line into list of args
133   std::vector<std::string> args;
134   size_t start = 0;
135   size_t end = 0;
136   while (1) {
137     start = s.find_first_not_of(" ,", end);
138     end = s.find_first_of(" ,", start);
139     if (start == s.npos) break;
140     args.push_back(s.substr(start, end-start));
141   }
142
143   // command is args[0]
144   std::string command = args.front();
145   args.erase(args.begin());
146
147   // ignore if it's an internal command
148   if (consoleCommand(command,args)) return;
149
150   // look up registered ccr
151   std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
152   if ((i == commands.end()) || (i->second.size() == 0)) {
153     addLine("unknown command: \"" + command + "\"");
154     return;
155   }
156
157   // send command to the most recently registered ccr
158   ConsoleCommandReceiver* ccr = i->second.front();
159   if (ccr->consoleCommand(command, args) != true) msg_warning << "Sent command to registered ccr, but command was unhandled" << std::endl;
160 }
161
162 bool
163 Console::consoleCommand(std::string command, std::vector<std::string> arguments) 
164 {
165   if (command == "ccrs") {
166     if (arguments.size() != 1) {
167       msg_info << "Usage: ccrs <command>" << std::endl;
168       return true;
169     }
170     std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(arguments[0]);
171     if ((i == commands.end()) || (i->second.size() == 0)) {
172       msg_info << "unknown command: \"" << arguments[0] << "\"" << std::endl;
173       return true;
174     }
175
176     std::ostringstream ccr_list;
177     std::list<ConsoleCommandReceiver*> &ccrs = i->second;
178     std::list<ConsoleCommandReceiver*>::iterator j;
179     for (j = ccrs.begin(); j != ccrs.end(); j++) {
180       if (j != ccrs.begin()) ccr_list << ", ";
181       ccr_list << "[" << *j << "]";
182     }
183
184     msg_info << "registered ccrs for \"" << arguments[0] << "\": " << ccr_list.str() << std::endl;
185     return true;
186   }
187
188   return false;
189 }
190
191 bool
192 Console::hasFocus() 
193 {
194   return focused;
195 }
196
197 void
198 Console::show()
199 {
200   focused = true;
201   height = 256;
202 }
203
204 void 
205 Console::hide()
206 {
207   focused = false;
208   height = 0;
209
210   // clear input buffer
211   inputBuffer.str(std::string());
212 }
213
214 void 
215 Console::toggle()
216 {
217   if (Console::hasFocus()) {
218     Console::hide(); 
219   } 
220   else { 
221     Console::show();
222   }
223 }
224
225 void 
226 Console::draw(DrawingContext& context)
227 {
228   if (height == 0) return;
229   if (!focused) {
230     if (ticks-- < 0) {
231       height-=10;
232       ticks=0;
233       if (height < 0) height=0;
234     }
235     if (height == 0) return;
236   }
237
238   context.draw_surface(background2, Vector(SCREEN_WIDTH/2 - background->get_width()/2 - background->get_width() + backgroundOffset, height - background->get_height()), LAYER_FOREGROUND1+1);
239   context.draw_surface(background2, Vector(SCREEN_WIDTH/2 - background->get_width()/2 + backgroundOffset, height - background->get_height()), LAYER_FOREGROUND1+1);
240   context.draw_surface(background, Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), LAYER_FOREGROUND1+1);
241   backgroundOffset+=10;
242   if (backgroundOffset > (int)background->get_width()) backgroundOffset -= (int)background->get_width();
243
244   int lineNo = 0;
245
246   if (focused) {
247     lineNo++;
248     float py = height-4-1*9;
249     context.draw_text(white_small_text, "> "+inputBuffer.str()+"_", Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
250   }
251
252   int skipLines = -offset;
253   for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
254     if (skipLines-- > 0) continue;
255     lineNo++;
256     float py = height-4-lineNo*9;
257     if (py < -9) break;
258     context.draw_text(white_small_text, *i, Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
259   }
260 }
261
262 void 
263 Console::registerCommand(std::string command, ConsoleCommandReceiver* ccr)
264 {
265   commands[command].push_front(ccr);
266 }
267
268 void 
269 Console::unregisterCommand(std::string command, ConsoleCommandReceiver* ccr)
270 {
271   std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
272   if ((i == commands.end()) || (i->second.size() == 0)) {
273     msg_warning << "Command \"" << command << "\" not associated with a command receiver. Not dissociated." << std::endl;
274     return;
275   }
276   std::list<ConsoleCommandReceiver*>::iterator j = find(i->second.begin(), i->second.end(), ccr);
277   if (j == i->second.end()) {
278     msg_warning << "Command \"" << command << "\" not associated with given command receiver. Not dissociated." << std::endl;
279     return;
280   }
281   i->second.erase(j);
282 }
283
284 void 
285 Console::unregisterCommands(ConsoleCommandReceiver* ccr)
286 {
287   for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
288     std::list<ConsoleCommandReceiver*> &ccrs = i->second;
289     std::list<ConsoleCommandReceiver*>::iterator j;
290     while ((j = find(ccrs.begin(), ccrs.end(), ccr)) != ccrs.end()) {
291       ccrs.erase(j);
292     }
293   }
294 }
295
296 int Console::height = 0;
297 bool Console::focused = false;
298 std::list<std::string> Console::lines;
299 std::map<std::string, std::list<ConsoleCommandReceiver*> > Console::commands;
300 ConsoleStreamBuffer Console::inputBuffer;
301 ConsoleStreamBuffer Console::outputBuffer;
302 std::ostream Console::input(&Console::inputBuffer);
303 std::ostream Console::output(&Console::outputBuffer);
304 int Console::offset = 0;
305 int Console::backgroundOffset = 0;
306