Cheating handled by Console
[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(DrawingContext* context) : context(context)
34 {
35   background = new Surface("images/engine/console.jpg");
36 }
37
38 Console::~Console() 
39 {
40   delete background;
41 }
42
43 void 
44 Console::flush(ConsoleStreamBuffer* buffer) 
45 {
46   if (buffer == &outputBuffer) {
47     std::string s = outputBuffer.str();
48     if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
49       while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
50       addLine(s);
51       outputBuffer.str(std::string());
52     }
53   }
54   if (buffer == &inputBuffer) {
55     std::string s = inputBuffer.str();
56     if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
57       while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
58       addLine("> "+s);
59       parse(s);
60       inputBuffer.str(std::string());
61     }
62   }
63 }
64
65 void 
66 Console::addLine(std::string s) 
67 {
68   lines.push_front(s);
69   if (lines.size() >= 256) lines.pop_back();
70   if (height < 64) {
71     if (height < 4+9) height=4+9;
72     height+=9;
73   }
74   ticks=120;
75   std::cerr << s << std::endl;
76 }
77
78 void
79 Console::parse(std::string s) 
80 {
81   for (std::list<ConsoleCommandReceiver*>::iterator i = commandReceivers.begin(); i != commandReceivers.end(); i++) {
82     ConsoleCommandReceiver* ccr = *i;
83     if (ccr->consoleCommand(s) == true) return;
84   }
85   addLine("unknown command: \"" + s + "\"");
86 }
87
88 bool
89 Console::hasFocus() 
90 {
91   return focused;
92 }
93
94 void
95 Console::show()
96 {
97   focused = true;
98   height = 128;
99 }
100
101 void 
102 Console::hide()
103 {
104   focused = false;
105   ticks = 0;
106 }
107
108 void 
109 Console::draw() 
110 {
111   if (height == 0) return;
112   if (!focused) {
113     if (ticks-- < 0) {
114       height-=1;
115       ticks=0;
116       if (height < 0) height=0;
117     }
118     if (height == 0) return;
119   }
120
121   context->draw_surface(background, Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), LAYER_FOREGROUND1+1);
122
123   int lineNo = 0;
124
125   if (focused) {
126     lineNo++;
127     float py = height-4-1*9;
128     context->draw_text(white_small_text, "> "+inputBuffer.str(), Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
129   }
130
131   for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
132     lineNo++;
133     float py = height-4-lineNo*9;
134     if (py < -9) break;
135     context->draw_text(white_small_text, *i, Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
136   }
137 }
138
139 void 
140 Console::registerCommandReceiver(ConsoleCommandReceiver* ccr)
141 {
142   commandReceivers.push_front(ccr);
143 }
144
145 void 
146 Console::unregisterCommandReceiver(ConsoleCommandReceiver* ccr)
147 {
148   std::list<ConsoleCommandReceiver*>::iterator i = find(commandReceivers.begin(), commandReceivers.end(), ccr);
149   if (i != commandReceivers.end()) commandReceivers.erase(i);
150 }
151
152 int Console::height = 0;
153 bool Console::focused = false;
154 std::list<std::string> Console::lines;
155 std::list<ConsoleCommandReceiver*> Console::commandReceivers;
156 ConsoleStreamBuffer Console::inputBuffer;
157 ConsoleStreamBuffer Console::outputBuffer;
158 std::ostream Console::input(&Console::inputBuffer);
159 std::ostream Console::output(&Console::outputBuffer);
160