4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
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.
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.
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.
22 #include "console.hpp"
23 #include "video/drawing_context.hpp"
24 #include "video/surface.hpp"
25 #include "scripting/squirrel_error.hpp"
26 #include "scripting/wrapper_util.hpp"
27 #include "player_status.hpp"
28 #include "script_manager.hpp"
30 #include "resources.hpp"
32 /// speed (pixels/s) the console closes
33 static const float FADE_SPEED = 1;
36 : backgroundOffset(0), height(0), alpha(1.0), offset(0), focused(false),
39 font.reset(new Font("images/engine/fonts/white-small.png",
40 "images/engine/fonts/shadow-small.png", 8, 9, 1));
41 background.reset(new Surface("images/engine/console.png"));
42 background2.reset(new Surface("images/engine/console2.png"));
50 Console::flush(ConsoleStreamBuffer* buffer)
52 if (buffer == &outputBuffer) {
53 std::string s = outputBuffer.str();
54 if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
55 while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
57 outputBuffer.str(std::string());
60 if (buffer == &inputBuffer) {
61 std::string s = inputBuffer.str();
62 if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
63 while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
66 inputBuffer.str(std::string());
72 Console::execute_script(const std::string& command)
74 using namespace Scripting;
76 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
81 int oldtop = sq_gettop(vm);
83 if(SQ_FAILED(sq_compilebuffer(vm, command.c_str(), command.length(),
85 throw SquirrelError(vm, "Couldn't compile command");
88 if(SQ_FAILED(sq_call(vm, 1, SQTrue)))
89 throw SquirrelError(vm, "Problem while executing command");
91 if(sq_gettype(vm, -1) != OT_NULL)
92 addLine(squirrel2string(vm, -1));
93 } catch(std::exception& e) {
96 int newtop = sq_gettop(vm);
98 log_fatal << "Script destroyed squirrel stack..." << std::endl;
100 sq_settop(vm, oldtop);
107 std::string s = inputBuffer.str();
108 if (s.length() > 0) {
109 s.erase(s.length()-1);
111 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
116 Console::scroll(int numLines)
119 if (offset > 0) offset = 0;
123 Console::autocomplete()
125 std::string cmdPart = inputBuffer.str();
126 addLine("> "+cmdPart);
128 std::string cmdList = "";
130 for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
131 std::string cmdKnown = i->first;
132 if (cmdKnown.substr(0, cmdPart.length()) == cmdPart) {
133 if (cmdListLen > 0) cmdList = cmdList + ", ";
134 cmdList = cmdList + cmdKnown;
138 if (cmdListLen == 0) addLine("No known command starts with \""+cmdPart+"\"");
139 if (cmdListLen == 1) {
140 inputBuffer.str(cmdList);
141 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
143 if (cmdListLen > 1) addLine(cmdList);
147 Console::addLine(std::string s)
149 std::cerr << s << std::endl;
150 while (s.length() > 99) {
151 lines.push_front(s.substr(0, 99-3)+"...");
152 s = "..."+s.substr(99-3);
156 while (lines.size() >= 1000)
162 height += font->get_height();
171 Console::parse(std::string s)
173 // make sure we actually have something to parse
174 if (s.length() == 0) return;
176 // split line into list of args
177 std::vector<std::string> args;
181 start = s.find_first_not_of(" ,", end);
182 end = s.find_first_of(" ,", start);
183 if (start == s.npos) break;
184 args.push_back(s.substr(start, end-start));
187 // command is args[0]
188 if (args.size() == 0) return;
189 std::string command = args.front();
190 args.erase(args.begin());
192 // ignore if it's an internal command
193 if (consoleCommand(command,args)) return;
195 // look up registered ccr
196 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
197 if ((i == commands.end()) || (i->second.size() == 0)) {
200 } catch(std::exception& e) {
206 // send command to the most recently registered ccr
207 ConsoleCommandReceiver* ccr = i->second.front();
208 if (ccr->consoleCommand(command, args) != true) log_warning << "Sent command to registered ccr, but command was unhandled" << std::endl;
212 Console::consoleCommand(std::string command, std::vector<std::string> arguments)
214 if (command == "ccrs") {
215 if (arguments.size() != 1) {
216 log_info << "Usage: ccrs <command>" << std::endl;
219 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(arguments[0]);
220 if ((i == commands.end()) || (i->second.size() == 0)) {
221 log_info << "unknown command: \"" << arguments[0] << "\"" << std::endl;
225 std::ostringstream ccr_list;
226 std::list<ConsoleCommandReceiver*> &ccrs = i->second;
227 std::list<ConsoleCommandReceiver*>::iterator j;
228 for (j = ccrs.begin(); j != ccrs.end(); j++) {
229 if (j != ccrs.begin()) ccr_list << ", ";
230 ccr_list << "[" << *j << "]";
233 log_info << "registered ccrs for \"" << arguments[0] << "\": " << ccr_list.str() << std::endl;
261 // clear input buffer
262 inputBuffer.str(std::string());
268 if (Console::hasFocus()) {
277 Console::update(float elapsed_time)
280 stayOpen -= elapsed_time;
283 } else if(!focused && height > 0) {
284 alpha -= elapsed_time * FADE_SPEED;
293 Console::draw(DrawingContext& context)
298 int layer = LAYER_GUI + 1;
300 context.push_transform();
301 context.set_alpha(alpha);
302 context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 - background->get_width() + backgroundOffset, height - background->get_height()), layer);
303 context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 + backgroundOffset, height - background->get_height()), layer);
304 context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), layer);
305 backgroundOffset+=10;
306 if (backgroundOffset > (int)background->get_width()) backgroundOffset -= (int)background->get_width();
312 float py = height-4-1*9;
313 context.draw_text(font.get(), "> "+inputBuffer.str()+"_", Vector(4, py), LEFT_ALLIGN, layer);
316 int skipLines = -offset;
317 for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
318 if (skipLines-- > 0) continue;
320 float py = height-4-lineNo*9;
322 context.draw_text(font.get(), *i, Vector(4, py), LEFT_ALLIGN, layer);
325 context.pop_transform();
329 Console::registerCommand(std::string command, ConsoleCommandReceiver* ccr)
331 commands[command].push_front(ccr);
335 Console::unregisterCommand(std::string command, ConsoleCommandReceiver* ccr)
337 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
338 if ((i == commands.end()) || (i->second.size() == 0)) {
339 log_warning << "Command \"" << command << "\" not associated with a command receiver. Not dissociated." << std::endl;
342 std::list<ConsoleCommandReceiver*>::iterator j = find(i->second.begin(), i->second.end(), ccr);
343 if (j == i->second.end()) {
344 log_warning << "Command \"" << command << "\" not associated with given command receiver. Not dissociated." << std::endl;
351 Console::unregisterCommands(ConsoleCommandReceiver* ccr)
353 for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
354 std::list<ConsoleCommandReceiver*> &ccrs = i->second;
355 std::list<ConsoleCommandReceiver*>::iterator j;
356 while ((j = find(ccrs.begin(), ccrs.end(), ccr)) != ccrs.end()) {
362 Console* Console::instance = NULL;
363 ConsoleStreamBuffer Console::inputBuffer;
364 ConsoleStreamBuffer Console::outputBuffer;
365 std::ostream Console::input(&Console::inputBuffer);
366 std::ostream Console::output(&Console::outputBuffer);