FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
console.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library 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 GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, 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"
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 
50  : fcn::Container(),
51  m_consoleexec(0),
52  m_input(new CommandLine()),
53  m_output(new fcn::TextBox()),
54  m_outputscrollarea(new fcn::ScrollArea(m_output)),
55  m_status(new fcn::Label()),
56  m_toolsbutton(new fcn::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 
73  m_fpsTimer.setCallback( boost::bind(&Console::updateCaption, this) );
74 
75  m_hiding = true;
76 
79 
80  m_toolsbutton->addActionListener(this);
81  m_toolsbutton->setFocusable(false);
82  m_input->addFocusListener(this);
83 
85  font->setColor(255,255,255);
86  setIOFont(font);
87  }
88 
90  int32_t w, h, b, input_h, bbar_h, button_w;
93  b = 0;
94  input_h = getFont()->getHeight();
95  bbar_h = input_h;
96  button_w = 80;
97 
98  fcn::Color black(0x00,0,0,0xff);
99  fcn::Color white(0xff,0xff,0xff,0xff);
100  fcn::Color dark(50,60,50,0xff);
101 
102  setSize(w, h);
103  setPosition((RenderBackend::instance()->getScreenWidth() - w) / 2,-h);
104  setFrameSize(0);
105 
106  setForegroundColor(white);
107  setBackgroundColor(black);
108  setBaseColor(dark);
109 
110  setSize(w, h);
111 
112  m_outputscrollarea->setSize(w - 2*b, h - input_h - 3*b - bbar_h);
113  m_outputscrollarea->setPosition(b,0);
114 
115  m_input->setPosition(b, h - input_h - b - bbar_h);
116  m_input->setSize(w - 2*b, input_h);
117 
118  m_status->setPosition(b, h - b - bbar_h);
119  m_status->setSize(w - 2*b, bbar_h);
120 
121  m_toolsbutton->setPosition(w - button_w, h - b - bbar_h);
122  m_toolsbutton->setSize(button_w, bbar_h);
123 
124  m_output->setBackgroundColor(black);
125  m_output->setFocusable(false);
126 
127  m_outputscrollarea->setBackgroundColor(black);
128  m_outputscrollarea->setBaseColor(dark);
129 
130  m_input->setForegroundColor(white);
131  m_input->setBackgroundColor(black);
132 
133  m_status->setForegroundColor(white);
134  m_status->setBackgroundColor(black);
135 
136  m_toolsbutton->setForegroundColor(white);
137  m_toolsbutton->setBackgroundColor(black);
138  m_toolsbutton->setBaseColor(dark);
139 
140  m_hiddenPos = -h;
141  m_animationDelta = h/6;
142  }
143 
145  doHide();
146 
147  remove(m_input);
148  remove(m_outputscrollarea);
149  remove(m_status);
150 
151  delete m_output;
152  delete m_input;
153  delete m_outputscrollarea;
154  delete m_status;
155  delete m_toolsbutton;
156  }
157 
159  std::string caption = "FIFE Console - FPS: ";
160  double fps = 1e3/TimeManager::instance()->getAverageFrameTime();
161  caption += boost::lexical_cast<std::string>(fps);
162  m_status->setCaption( caption );
163  }
164 
166  if (m_hiding){
167  setPosition(getX(), getY() - m_animationDelta);
168  if (getY() <= m_hiddenPos){
169  doHide();
171  }
172  }else{
173  setPosition(getX(), getY() + m_animationDelta);
174  if (getY() >= 0){
175  setPosition(getX(), 0);
177  }
178  }
179  }
180 
181  void Console::clear() {
182  m_output->setText("");
183  }
184 
186  if (m_isAttached)
187  return;
188  m_isAttached = true;
190  FifechanManager::instance()->getTopContainer()->moveToTop(this);
191  // Assure the input field is focused when shown.
192  m_input->requestFocus();
193 
194  m_fpsTimer.start();
195  }
196 
198  if (!m_isAttached)
199  return;
200  m_isAttached = false;
202  m_fpsTimer.stop();
203  }
204 
205  void Console::show() {
206  if(m_hiding) {
207  m_hiding = false;
208  doShow();
210  }
211  }
212 
213  void Console::hide() {
214  if(!m_hiding) {
215  m_hiding = true;
217  }
218  }
219 
221  m_hiding = !m_hiding;
222  if(!m_hiding)
223  doShow();
225  }
226 
227  void Console::execute(std::string cmd) {
228  FL_DBG(_log, LMsg("in execute with command ") << cmd);
229  if (cmd.empty())
230  return;
231 
232  // copy input to output
233  println(m_prompt + cmd);
234 
235  // run the command
236  try {
237  if (m_consoleexec) {
238  std::string resp = m_consoleexec->onConsoleCommand(cmd);
239  println(resp);
240  } else {
241  FL_WARN(_log, LMsg("ConsoleExecuter not bind, but command received: ") << cmd.c_str());
242  }
243  }
244  catch (const FIFE::Exception & e) {
245  FL_WARN(_log, LMsg("Console caught exception: ") << e.what());
246  println(e.what());
247  }
248  }
249 
250  void Console::println(const std::string & s) {
251  assert(m_output);
252 
253  // Add the text in rows
254  boost::char_separator<char> separator("\n");
255  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
256  tokenizer tokens(s,separator);
257  for(tokenizer::iterator i = tokens.begin(); i != tokens.end(); ++i) {
258  m_output->addRow(*i);
259  }
260 
261  // Assure the maximum number of rows
262  if( m_output->getNumberOfRows() > m_maxOutputRows ) {
263  unsigned rows = m_output->getNumberOfRows();
264  int32_t delta_rows = rows - m_maxOutputRows;
265  std::vector<std::string> rows_text;
266  for(size_t i=delta_rows; i != rows; ++i) {
267  rows_text.push_back(m_output->getTextRow(i));
268  }
269  m_output->setText("");
270  for(size_t i=0; i != rows_text.size(); ++i) {
271  m_output->addRow(rows_text[i]);
272  }
273  }
274 
275  // Assure the new text is visible
276  fcn::Rectangle rect(0,m_output->getHeight(),0,0);
277  m_outputscrollarea->showWidgetPart(m_output,rect);
278  }
279 
280  void Console::action(const fcn::ActionEvent & event) {
281  if (m_consoleexec) {
283  } else {
284  FL_WARN(_log, "ConsoleExecuter not bind, but tools button clicked");
285  }
286  }
287 
288  void Console::setConsoleExecuter(ConsoleExecuter* const consoleexec) {
289  m_consoleexec = consoleexec;
290  }
291 
293  m_consoleexec = NULL;
294  }
295 
297  m_input->setFont(font);
298  m_output->setFont(font);
299  }
300 
301  void Console::focusLost(const fcn::Event& ) {
302  hide();
303  }
304 }
305 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
#define FL_WARN(logger, msg)
Definition: logger.h:72
virtual void onToolsClick()=0
Called when console tools button is clicked.
void focusLost(const fcn::Event &event)
Hide if we loose focus.
Definition: console.cpp:301
void add(fcn::Widget *widget)
Adds a new widget.
virtual const char * what() const
Returns the error message.
Definition: exception.cpp:41
uint32_t getScreenHeight() const
void setConsoleExecuter(ConsoleExecuter *const consoleexec)
Sets executer for the console.
Definition: console.cpp:288
CommandLine * m_input
Definition: console.h:149
virtual std::string onConsoleCommand(const std::string &command)=0
Called when user has typed command to console and pressed enter.
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
void execute(std::string cmd)
Execute a command Normally just sends the command to runString() Checks whether the cmd is just one t...
Definition: console.cpp:227
void hide()
Hide the console Removes itself from the toplevel container and pops it's input context.
Definition: console.cpp:213
void setCallback(const type_callback &cb)
Set callback on pressing the ENTER key.
void updateAnimation()
Update the scroll in/out animation.
Definition: console.cpp:165
A Command line widget.
Definition: commandline.h:43
static Logger _log(LM_AUDIO)
void setInterval(int32_t msec)
Set the interval in milliseconds.
Definition: timer.cpp:60
void setIOFont(GuiFont *font)
Sets the font used for the input and output areas.
Definition: console.cpp:296
void removeConsoleExecuter()
Removes executer for the console.
Definition: console.cpp:292
Exception base class.
Definition: exception.h:43
void start()
Start the timer.
Definition: timer.cpp:45
GuiFont * createFont(const std::string &path="", uint32_t size=0, const std::string &glyphs="")
Gets font with given properties.
Timer m_animationTimer
Definition: console.h:164
static FifechanManager * instance()
Definition: singleton.h:84
int32_t m_hiddenPos
Definition: console.h:158
std::string m_prompt
Definition: console.h:156
Console executer is listener interface for console activity.
Definition: console.h:45
fcn::Button * m_toolsbutton
Definition: console.h:153
void doShow()
Definition: console.cpp:185
uint32_t getScreenWidth() const
void remove(fcn::Widget *widget)
Removes a widget.
double getAverageFrameTime() const
Gets average frame time.
Console()
Constructor.
Definition: console.cpp:49
void println(const std::string &s)
Print one or more lines to the console output.
Definition: console.cpp:250
void show()
Show the console Adds the Console to the fifechan toplevel container and pushes an input Context so t...
Definition: console.cpp:205
void toggleShowHide()
Toggle the console Toggles whether the Console is shown or not.
Definition: console.cpp:220
bool m_hiding
Definition: console.h:161
fcn::Label * m_status
Definition: console.h:152
virtual ~Console()
Destructor.
Definition: console.cpp:144
void stop()
Stop the timer.
Definition: timer.cpp:53
static const unsigned m_maxOutputRows
Definition: console.h:154
void updateCaption()
Update the FPS caption.
Definition: console.cpp:158
fcn::Container * getTopContainer() const
Gets the top container.
void doHide()
Definition: console.cpp:197
Timer m_fpsTimer
Definition: console.h:163
void reLayout()
Layouts the console to match e.g.
Definition: console.cpp:89
ConsoleExecuter * m_consoleexec
Definition: console.h:147
fcn::ScrollArea * m_outputscrollarea
Definition: console.h:151
bool m_isAttached
Definition: console.h:146
int32_t m_animationDelta
Definition: console.h:159
#define FL_DBG(logger, msg)
Definition: logger.h:70
void setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Set the color the text should be rendered in.
Definition: gui_font.cpp:130
void setCallback(const type_callback &callback)
Set the callback that will be called.
Definition: timer.cpp:64
fcn::TextBox * m_output
Definition: console.h:150
void action(const fcn::ActionEvent &event)
Callback from fifechan to respond to button press.
Definition: console.cpp:280
void clear()
Clear the console output.
Definition: console.cpp:181