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 "player_status.hpp"
27 #include "resources.hpp"
30 int ticks; // TODO: use a clock?
35 background = new Surface("images/engine/console.png");
36 background2 = new Surface("images/engine/console2.png");
46 Console::flush(ConsoleStreamBuffer* buffer)
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);
53 outputBuffer.str(std::string());
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);
62 inputBuffer.str(std::string());
70 std::string s = inputBuffer.str();
72 s.erase(s.length()-1);
74 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
79 Console::scroll(int numLines)
82 if (offset > 0) offset = 0;
86 Console::autocomplete()
88 std::string cmdPart = inputBuffer.str();
89 addLine("> "+cmdPart);
91 std::string cmdList = "";
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;
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);
106 if (cmdListLen > 1) addLine(cmdList);
110 Console::addLine(std::string s)
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);
118 while (lines.size() >= 65535) lines.pop_back();
120 if (height < 4+9) height=4+9;
127 Console::parse(std::string s)
129 // make sure we actually have something to parse
130 if (s.length() == 0) return;
132 // split line into list of args
133 std::vector<std::string> args;
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));
143 // command is args[0]
144 std::string command = args.front();
145 args.erase(args.begin());
147 // look up registered ccr
148 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
149 if ((i == commands.end()) || (i->second.size() == 0)) {
150 addLine("unknown command: \"" + command + "\"");
154 // send command to the most recently registered ccr
155 ConsoleCommandReceiver* ccr = i->second.front();
156 if (ccr->consoleCommand(command, args) != true) msg_warning << "Sent command to registered ccr, but command was unhandled" << std::endl;
178 // clear input buffer
179 inputBuffer.str(std::string());
185 if (Console::hasFocus()) {
194 Console::draw(DrawingContext& context)
196 if (height == 0) return;
201 if (height < 0) height=0;
203 if (height == 0) return;
206 context.draw_surface(background2, Vector(SCREEN_WIDTH/2 - background->get_width()/2 - background->get_width() + backgroundOffset, height - background->get_height()), LAYER_FOREGROUND1+1);
207 context.draw_surface(background2, Vector(SCREEN_WIDTH/2 - background->get_width()/2 + backgroundOffset, height - background->get_height()), LAYER_FOREGROUND1+1);
208 context.draw_surface(background, Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), LAYER_FOREGROUND1+1);
209 backgroundOffset+=10;
210 if (backgroundOffset > (int)background->get_width()) backgroundOffset -= (int)background->get_width();
216 float py = height-4-1*9;
217 context.draw_text(white_small_text, "> "+inputBuffer.str()+"_", Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
220 int skipLines = -offset;
221 for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
222 if (skipLines-- > 0) continue;
224 float py = height-4-lineNo*9;
226 context.draw_text(white_small_text, *i, Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
231 Console::registerCommand(std::string command, ConsoleCommandReceiver* ccr)
233 commands[command].push_front(ccr);
237 Console::unregisterCommand(std::string command, ConsoleCommandReceiver* ccr)
239 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
240 if ((i == commands.end()) || (i->second.size() == 0)) {
241 msg_warning << "Command \"" << command << "\" not associated with a command receiver. Not dissociated." << std::endl;
244 std::list<ConsoleCommandReceiver*>::iterator j = find(i->second.begin(), i->second.end(), ccr);
245 if (j == i->second.end()) {
246 msg_warning << "Command \"" << command << "\" not associated with given command receiver. Not dissociated." << std::endl;
252 int Console::height = 0;
253 bool Console::focused = false;
254 std::list<std::string> Console::lines;
255 std::map<std::string, std::list<ConsoleCommandReceiver*> > Console::commands;
256 ConsoleStreamBuffer Console::inputBuffer;
257 ConsoleStreamBuffer Console::outputBuffer;
258 std::ostream Console::input(&Console::inputBuffer);
259 std::ostream Console::output(&Console::outputBuffer);
260 int Console::offset = 0;
261 int Console::backgroundOffset = 0;