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"
32 /// speed (pixels/s) the console closes
33 static const float CLOSE_SPEED = 50;
36 : backgroundOffset(0), height(0), offset(0), focused(false), stayOpen(0)
38 background.reset(new Surface("images/engine/console.png"));
39 background2.reset(new Surface("images/engine/console2.png"));
47 Console::flush(ConsoleStreamBuffer* buffer)
49 if (buffer == &outputBuffer) {
50 std::string s = outputBuffer.str();
51 if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
52 while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
54 outputBuffer.str(std::string());
57 if (buffer == &inputBuffer) {
58 std::string s = inputBuffer.str();
59 if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
60 while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
63 inputBuffer.str(std::string());
69 Console::execute_script(const std::string& command)
71 using namespace Scripting;
73 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
78 int oldtop = sq_gettop(vm);
80 if(SQ_FAILED(sq_compilebuffer(vm, command.c_str(), command.length(),
82 throw SquirrelError(vm, "Couldn't compile command");
85 if(SQ_FAILED(sq_call(vm, 1, SQTrue)))
86 throw SquirrelError(vm, "Problem while executing command");
88 if(sq_gettype(vm, -1) != OT_NULL)
89 addLine(squirrel2string(vm, -1));
90 } catch(std::exception& e) {
93 int newtop = sq_gettop(vm);
95 msg_fatal << "Script destroyed squirrel stack..." << std::endl;
97 sq_settop(vm, oldtop);
104 std::string s = inputBuffer.str();
105 if (s.length() > 0) {
106 s.erase(s.length()-1);
108 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
113 Console::scroll(int numLines)
116 if (offset > 0) offset = 0;
120 Console::autocomplete()
122 std::string cmdPart = inputBuffer.str();
123 addLine("> "+cmdPart);
125 std::string cmdList = "";
127 for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
128 std::string cmdKnown = i->first;
129 if (cmdKnown.substr(0, cmdPart.length()) == cmdPart) {
130 if (cmdListLen > 0) cmdList = cmdList + ", ";
131 cmdList = cmdList + cmdKnown;
135 if (cmdListLen == 0) addLine("No known command starts with \""+cmdPart+"\"");
136 if (cmdListLen == 1) {
137 inputBuffer.str(cmdList);
138 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
140 if (cmdListLen > 1) addLine(cmdList);
144 Console::addLine(std::string s)
146 std::cerr << s << std::endl;
147 while (s.length() > 99) {
148 lines.push_front(s.substr(0, 99-3)+"...");
149 s = "..."+s.substr(99-3);
152 while (lines.size() >= 65535) lines.pop_back();
154 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;
252 // clear input buffer
253 inputBuffer.str(std::string());
259 if (Console::hasFocus()) {
268 Console::update(float elapsed_time)
271 stayOpen -= elapsed_time;
274 } else if(!focused && height > 0) {
275 height -= elapsed_time * CLOSE_SPEED;
282 Console::draw(DrawingContext& context)
287 context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 - background->get_width() + backgroundOffset, height - background->get_height()), LAYER_FOREGROUND1+1);
288 context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 + backgroundOffset, height - background->get_height()), LAYER_FOREGROUND1+1);
289 context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), LAYER_FOREGROUND1+1);
290 backgroundOffset+=10;
291 if (backgroundOffset > (int)background->get_width()) backgroundOffset -= (int)background->get_width();
297 float py = height-4-1*9;
298 context.draw_text(white_small_text, "> "+inputBuffer.str()+"_", Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
301 int skipLines = -offset;
302 for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
303 if (skipLines-- > 0) continue;
305 float py = height-4-lineNo*9;
307 context.draw_text(white_small_text, *i, Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
312 Console::registerCommand(std::string command, ConsoleCommandReceiver* ccr)
314 commands[command].push_front(ccr);
318 Console::unregisterCommand(std::string command, ConsoleCommandReceiver* ccr)
320 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
321 if ((i == commands.end()) || (i->second.size() == 0)) {
322 msg_warning << "Command \"" << command << "\" not associated with a command receiver. Not dissociated." << std::endl;
325 std::list<ConsoleCommandReceiver*>::iterator j = find(i->second.begin(), i->second.end(), ccr);
326 if (j == i->second.end()) {
327 msg_warning << "Command \"" << command << "\" not associated with given command receiver. Not dissociated." << std::endl;
334 Console::unregisterCommands(ConsoleCommandReceiver* ccr)
336 for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
337 std::list<ConsoleCommandReceiver*> &ccrs = i->second;
338 std::list<ConsoleCommandReceiver*>::iterator j;
339 while ((j = find(ccrs.begin(), ccrs.end(), ccr)) != ccrs.end()) {
345 Console* Console::instance = NULL;
346 ConsoleStreamBuffer Console::inputBuffer;
347 ConsoleStreamBuffer Console::outputBuffer;
348 std::ostream Console::input(&Console::inputBuffer);
349 std::ostream Console::output(&Console::outputBuffer);