comparison engine/core/gui/console/console.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (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 *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <cassert>
24
25 // 3rd party library includes
26 #include <boost/bind.hpp>
27 #include <boost/lexical_cast.hpp>
28 #include <boost/regex.hpp>
29 #include <boost/tokenizer.hpp>
30
31 // FIFE includes
32 // These includes are split up in two parts, separated by one empty line
33 // First block: files included from the FIFE root src directory
34 // Second block: files included from the same folder
35 #include "video/renderbackend.h"
36 #include "util/time/timemanager.h"
37 #include "util/log/logger.h"
38 #include "util/base/exception.h"
39 #include "gui/guimanager.h"
40 #include "gui/base/gui_font.h"
41
42 #include "commandline.h"
43 #include "console.h"
44
45 namespace FIFE {
46 const unsigned Console::m_maxOutputRows = 50;
47 static Logger _log(LM_CONSOLE);
48
49 Console::Console()
50 : gcn::Container(),
51 m_consoleexec(0),
52 m_input(new CommandLine()),
53 m_output(new gcn::TextBox("")),
54 m_outputscrollarea(new gcn::ScrollArea(m_output)),
55 m_status(new gcn::Label()),
56 m_toolsbutton(new gcn::Button("Tools"))
57 {
58 reLayout();
59
60 add(m_outputscrollarea);
61 add(m_input);
62 add(m_status);
63 add(m_toolsbutton);
64
65 setOpaque(true);
66
67 m_input->setCallback( std::bind1st( std::mem_fun(&Console::execute), this) );
68 m_prompt = "-- ";
69
70 m_isAttached = false;
71
72 m_fpsTimer.setInterval(500);
73 m_fpsTimer.setCallback( boost::bind(&Console::updateCaption, this) );
74
75 m_hiding = true;
76
77 m_animationTimer.setInterval(20);
78 m_animationTimer.setCallback( boost::bind(&Console::updateAnimation, this) );
79
80 m_toolsbutton->addActionListener(this);
81
82 GuiFont* font = GUIManager::instance()->createFont();
83 font->setColor(255,255,255);
84 setIOFont(font);
85 }
86
87 void Console::reLayout() {
88 Image* screen = RenderBackend::instance()->getScreenImage();
89 assert(screen);
90
91 int w, h, b, input_h, bbar_h, button_w;
92 w = screen->getWidth() * 4/5;
93 h = screen->getHeight() * 4/5;
94 b = 0;
95 input_h = getFont()->getHeight();
96 bbar_h = input_h;
97 button_w = 80;
98
99 gcn::Color black(0x00,0,0,0xff);
100 gcn::Color white(0xff,0xff,0xff,0xff);
101 gcn::Color dark(50,60,50,0xff);
102
103 setSize(w, h);
104 setPosition((screen->getWidth() - w) / 2,-h);
105 setFrameSize(0);
106
107 setForegroundColor(white);
108 setBackgroundColor(black);
109 setBaseColor(dark);
110
111 setSize(w, h);
112
113 m_outputscrollarea->setSize(w - 2*b, h - input_h - 3*b - bbar_h);
114 m_outputscrollarea->setPosition(b,0);
115
116 m_input->setPosition(b, h - input_h - b - bbar_h);
117 m_input->setSize(w - 2*b, input_h);
118
119 m_status->setPosition(b, h - b - bbar_h);
120 m_status->setSize(w - 2*b, bbar_h);
121
122 m_toolsbutton->setPosition(w - button_w, h - b - bbar_h);
123 m_toolsbutton->setSize(button_w, bbar_h);
124
125 m_output->setBackgroundColor(black);
126 m_output->setFocusable(false);
127
128 m_outputscrollarea->setBackgroundColor(black);
129 m_outputscrollarea->setBaseColor(dark);
130
131 m_input->setForegroundColor(white);
132 m_input->setBackgroundColor(black);
133
134 m_status->setForegroundColor(white);
135 m_status->setBackgroundColor(black);
136
137 m_toolsbutton->setForegroundColor(white);
138 m_toolsbutton->setBackgroundColor(black);
139 m_toolsbutton->setBaseColor(dark);
140
141 m_hiddenPos = -h;
142 m_animationDelta = h/6;
143 }
144
145 Console::~Console() {
146 doHide();
147
148 remove(m_input);
149 remove(m_outputscrollarea);
150 remove(m_status);
151
152 delete m_output;
153 delete m_input;
154 delete m_outputscrollarea;
155 delete m_status;
156 delete m_toolsbutton;
157 }
158
159 void Console::updateCaption() {
160 std::string caption = "FIFE Console - FPS: ";
161 float fps = 1e3/double(TimeManager::instance()->getAverageFrameTime());
162 caption += boost::lexical_cast<std::string>(fps);
163 m_status->setCaption( caption );
164 }
165
166 void Console::updateAnimation() {
167 if( m_hiding ) {
168 setPosition( getX(), getY() - m_animationDelta);
169 } else {
170 setPosition( getX(), getY() + m_animationDelta);
171 }
172
173 if( !m_hiding && getY() >= 0 ) {
174 setPosition( getX(), 0 );
175 m_animationTimer.stop();
176 }
177 if( m_hiding && getY() <= m_hiddenPos ) {
178 doHide();
179 m_animationTimer.stop();
180 }
181 }
182
183 void Console::clear() {
184 m_output->setText("");
185 }
186
187 void Console::doShow() {
188 if (m_isAttached)
189 return;
190 m_isAttached = true;
191 GUIManager::instance()->add(this);
192 GUIManager::instance()->getTopContainer()->moveToTop(this);
193 // Assure the input field is focused when shown.
194 m_input->requestFocus();
195
196 m_fpsTimer.start();
197 }
198
199 void Console::doHide() {
200 if (!m_isAttached)
201 return;
202 m_isAttached = false;
203 GUIManager::instance()->remove(this);
204 m_fpsTimer.stop();
205 }
206
207 void Console::show() {
208 if(m_hiding) {
209 m_hiding = false;
210 doShow();
211 m_animationTimer.start();
212 }
213 }
214
215 void Console::hide() {
216 if(!m_hiding) {
217 m_hiding = true;
218 m_animationTimer.start();
219 }
220 }
221
222 void Console::toggleShowHide() {
223 m_hiding = !m_hiding;
224 if(!m_hiding)
225 doShow();
226 m_animationTimer.start();
227 }
228
229 void Console::execute(std::string cmd) {
230 FL_DBG(_log, LMsg("in execute with command ") << cmd);
231 if (cmd.empty())
232 return;
233
234 // copy input to output
235 println(m_prompt + cmd);
236
237 // run the command
238 try {
239 if (m_consoleexec) {
240 std::string resp = m_consoleexec->onConsoleCommand(cmd);
241 println(resp);
242 } else {
243 FL_WARN(_log, LMsg("ConsoleExecuter not bind, but command received: ") << cmd.c_str());
244 }
245 }
246 catch (FIFE::Exception & e) {
247 FL_WARN(_log, LMsg("Console caught exception: ") << e.getMessage());
248 println(e.getMessage());
249 }
250 }
251
252 void Console::println(const std::string & s) {
253 assert(m_output);
254
255 // Add the text in rows
256 boost::char_separator<char> separator("\n");
257 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
258 tokenizer tokens(s,separator);
259 for(tokenizer::iterator i = tokens.begin(); i != tokens.end(); ++i) {
260 m_output->addRow(*i);
261 }
262
263 // Assure the maximum number of rows
264 if( m_output->getNumberOfRows() > m_maxOutputRows ) {
265 unsigned rows = m_output->getNumberOfRows();
266 int delta_rows = rows - m_maxOutputRows;
267 std::vector<std::string> rows_text;
268 for(size_t i=delta_rows; i != rows; ++i) {
269 rows_text.push_back(m_output->getTextRow(i));
270 }
271 m_output->setText("");
272 for(size_t i=0; i != rows_text.size(); ++i) {
273 m_output->addRow(rows_text[i]);
274 }
275 }
276
277 // Assure the new text is visible
278 gcn::Rectangle rect(0,m_output->getHeight(),0,0);
279 m_outputscrollarea->showWidgetPart(m_output,rect);
280 }
281
282 void Console::action(const gcn::ActionEvent & event) {
283 if (m_consoleexec) {
284 m_consoleexec->onToolsClick();
285 } else {
286 FL_WARN(_log, "ConsoleExecuter not bind, but tools button clicked");
287 }
288 }
289
290 void Console::setConsoleExecuter(ConsoleExecuter* const consoleexec) {
291 m_consoleexec = consoleexec;
292 }
293
294 void Console::removeConsoleExecuter() {
295 m_consoleexec = NULL;
296 }
297
298 void Console::setIOFont(GuiFont* font) {
299 m_input->setFont(font);
300 m_output->setFont(font);
301 }
302 }
303 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */