1 // $Id: worldmap.cpp 3209 2006-04-02 22:19:22Z sommer $
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"
33 int ticks; // TODO: use a clock?
38 background = new Surface("images/engine/console.png");
39 background2 = new Surface("images/engine/console2.png");
49 Console::flush(ConsoleStreamBuffer* buffer)
51 if (buffer == &outputBuffer) {
52 std::string s = outputBuffer.str();
53 if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
54 while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
56 outputBuffer.str(std::string());
59 if (buffer == &inputBuffer) {
60 std::string s = inputBuffer.str();
61 if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
62 while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
65 inputBuffer.str(std::string());
71 Console::execute_script(const std::string& command)
73 using namespace Scripting;
75 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
80 int oldtop = sq_gettop(vm);
82 if(SQ_FAILED(sq_compilebuffer(vm, command.c_str(), command.length(),
84 throw SquirrelError(vm, "Couldn't compile command");
87 if(SQ_FAILED(sq_call(vm, 1, SQTrue)))
88 throw SquirrelError(vm, "Problem while executing command");
90 if(sq_gettype(vm, -1) != OT_NULL)
91 addLine(squirrel2string(vm, -1));
92 } catch(std::exception& e) {
95 int newtop = sq_gettop(vm);
97 msg_fatal << "Script destroyed squirrel stack..." << std::endl;
99 sq_settop(vm, oldtop);
106 std::string s = inputBuffer.str();
107 if (s.length() > 0) {
108 s.erase(s.length()-1);
110 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
115 Console::scroll(int numLines)
118 if (offset > 0) offset = 0;
122 Console::autocomplete()
124 std::string cmdPart = inputBuffer.str();
125 addLine("> "+cmdPart);
127 std::string cmdList = "";
129 for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
130 std::string cmdKnown = i->first;
131 if (cmdKnown.substr(0, cmdPart.length()) == cmdPart) {
132 if (cmdListLen > 0) cmdList = cmdList + ", ";
133 cmdList = cmdList + cmdKnown;
137 if (cmdListLen == 0) addLine("No known command starts with \""+cmdPart+"\"");
138 if (cmdListLen == 1) {
139 inputBuffer.str(cmdList);
140 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
142 if (cmdListLen > 1) addLine(cmdList);
146 Console::addLine(std::string s)
148 std::cerr << s << std::endl;
149 while (s.length() > 99) {
150 lines.push_front(s.substr(0, 99-3)+"...");
151 s = "..."+s.substr(99-3);
154 while (lines.size() >= 65535) lines.pop_back();
156 if (height < 4+9) height=4+9;
163 Console::parse(std::string s)
165 // make sure we actually have something to parse
166 if (s.length() == 0) return;
168 // split line into list of args
169 std::vector<std::string> args;
173 start = s.find_first_not_of(" ,", end);
174 end = s.find_first_of(" ,", start);
175 if (start == s.npos) break;
176 args.push_back(s.substr(start, end-start));
179 // command is args[0]
180 if (args.size() == 0) return;
181 std::string command = args.front();
182 args.erase(args.begin());
184 // ignore if it's an internal command
185 if (consoleCommand(command,args)) return;
187 // look up registered ccr
188 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
189 if ((i == commands.end()) || (i->second.size() == 0)) {
192 } catch(std::exception& e) {
198 // send command to the most recently registered ccr
199 ConsoleCommandReceiver* ccr = i->second.front();
200 if (ccr->consoleCommand(command, args) != true) msg_warning << "Sent command to registered ccr, but command was unhandled" << std::endl;
204 Console::consoleCommand(std::string command, std::vector<std::string> arguments)
206 if (command == "ccrs") {
207 if (arguments.size() != 1) {
208 msg_info << "Usage: ccrs <command>" << std::endl;
211 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(arguments[0]);
212 if ((i == commands.end()) || (i->second.size() == 0)) {
213 msg_info << "unknown command: \"" << arguments[0] << "\"" << std::endl;
217 std::ostringstream ccr_list;
218 std::list<ConsoleCommandReceiver*> &ccrs = i->second;
219 std::list<ConsoleCommandReceiver*>::iterator j;
220 for (j = ccrs.begin(); j != ccrs.end(); j++) {
221 if (j != ccrs.begin()) ccr_list << ", ";
222 ccr_list << "[" << *j << "]";
225 msg_info << "registered ccrs for \"" << arguments[0] << "\": " << ccr_list.str() << std::endl;
251 // clear input buffer
252 inputBuffer.str(std::string());
258 if (Console::hasFocus()) {
267 Console::draw(DrawingContext& context)
269 if (height == 0) return;
274 if (height < 0) height=0;
276 if (height == 0) return;
279 context.draw_surface(background2, Vector(SCREEN_WIDTH/2 - background->get_width()/2 - background->get_width() + backgroundOffset, height - background->get_height()), LAYER_FOREGROUND1+1);
280 context.draw_surface(background2, Vector(SCREEN_WIDTH/2 - background->get_width()/2 + backgroundOffset, height - background->get_height()), LAYER_FOREGROUND1+1);
281 context.draw_surface(background, Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), LAYER_FOREGROUND1+1);
282 backgroundOffset+=10;
283 if (backgroundOffset > (int)background->get_width()) backgroundOffset -= (int)background->get_width();
289 float py = height-4-1*9;
290 context.draw_text(white_small_text, "> "+inputBuffer.str()+"_", Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
293 int skipLines = -offset;
294 for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
295 if (skipLines-- > 0) continue;
297 float py = height-4-lineNo*9;
299 context.draw_text(white_small_text, *i, Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
304 Console::registerCommand(std::string command, ConsoleCommandReceiver* ccr)
306 commands[command].push_front(ccr);
310 Console::unregisterCommand(std::string command, ConsoleCommandReceiver* ccr)
312 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
313 if ((i == commands.end()) || (i->second.size() == 0)) {
314 msg_warning << "Command \"" << command << "\" not associated with a command receiver. Not dissociated." << std::endl;
317 std::list<ConsoleCommandReceiver*>::iterator j = find(i->second.begin(), i->second.end(), ccr);
318 if (j == i->second.end()) {
319 msg_warning << "Command \"" << command << "\" not associated with given command receiver. Not dissociated." << std::endl;
326 Console::unregisterCommands(ConsoleCommandReceiver* ccr)
328 for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
329 std::list<ConsoleCommandReceiver*> &ccrs = i->second;
330 std::list<ConsoleCommandReceiver*>::iterator j;
331 while ((j = find(ccrs.begin(), ccrs.end(), ccr)) != ccrs.end()) {
337 int Console::height = 0;
338 bool Console::focused = false;
339 std::list<std::string> Console::lines;
340 std::map<std::string, std::list<ConsoleCommandReceiver*> > Console::commands;
341 ConsoleStreamBuffer Console::inputBuffer;
342 ConsoleStreamBuffer Console::outputBuffer;
343 std::ostream Console::input(&Console::inputBuffer);
344 std::ostream Console::output(&Console::outputBuffer);
345 int Console::offset = 0;
346 int Console::backgroundOffset = 0;