FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
commandline.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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 
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "util/time/timeevent.h"
33 #include "util/time/timemanager.h"
34 
35 #include "commandline.h"
36 
37 namespace FIFE {
38  using namespace fcn;
39 
40 
41  CommandLine::CommandLine() : fcn::TextField(), m_history_position(0) {
42 
46 
49  .setCallback(boost::bind(&CommandLine::startBlinking,this));
50  }
51 
53  }
54 
57  }
58 
62  m_caretVisible = true;
63  }
64 
68  }
69 
70  void CommandLine::keyPressed(fcn::KeyEvent &keyEvent) {
71  fcn::Key key = keyEvent.getKey();
72  int32_t keyType = key.getValue();
73 
74  if (keyType == Key::Left && getCaretPosition() > 0)
75  {
76  TextField::keyPressed(keyEvent);
77  }
78  else if (keyType == Key::Right && getCaretPosition() < getText().size())
79  {
80  TextField::keyPressed(keyEvent);
81  }
82  else if (keyType == Key::Down && !m_history.empty())
83  {
84  if( m_history_position < m_history.size() ) {
85 
86  if( ++m_history_position == m_history.size() ) {
87  setText( m_cmdline );
88  } else {
89  setText( m_history[m_history_position] );
90  }
91  };
92  }
93  else if (keyType == Key::Up && !m_history.empty())
94  {
95  if( m_history_position > 0 ) {
96  if( m_history_position == m_history.size() ) {
97  m_cmdline = getText();
98  }
100  setText( m_history[m_history_position] );
101  };
102  }
103  else if (keyType == Key::Delete && getCaretPosition() < getText().size())
104  {
105  TextField::keyPressed(keyEvent);
106  }
107  else if (keyType == Key::Backspace && getCaretPosition() > 0)
108  {
109  TextField::keyPressed(keyEvent);
110  }
111  else if (keyType == Key::Enter)
112  {
113  if( getText() != "" ) {
114  if(m_callback) {
115  m_callback( getText() );
116  }
117  m_history.push_back( getText() );
118  m_history_position = m_history.size();
119  setText("");
120  }
121  }
122  else if (keyType == Key::Home)
123  {
124  setCaretPosition(0);
125  }
126  else if (keyType == Key::End)
127  {
128  setCaretPosition(getText().size());
129  }
130  else if (key.isCharacter() || static_cast<uint32_t>(key.getValue()) > 255)
131  {
132  TextField::keyPressed(keyEvent);
133  }
134  stopBlinking();
135  fixScroll();
136  }
137 
138  void CommandLine::drawCaret(fcn::Graphics * graphics, int32_t x) {
139  if( !m_caretVisible )
140  return;
141 
142  graphics->setColor(getForegroundColor());
143  graphics->drawLine(x, getHeight() - 2, x, 1);
144  graphics->drawLine(x+1, getHeight() - 2, x+1, 1);
145  }
146 
147 
149  m_callback = cb;
150  }
151 
152 }
std::string m_cmdline
Definition: commandline.h:77
void stopBlinking()
Stop blinking the caret for a few seconds.
Definition: commandline.cpp:59
void setCallback(const type_callback &cb)
Set callback on pressing the ENTER key.
type_callback m_callback
Definition: commandline.h:74
size_t m_history_position
Definition: commandline.h:76
void setInterval(int32_t msec)
Set the interval in milliseconds.
Definition: timer.cpp:60
void start()
Start the timer.
Definition: timer.cpp:45
void keyPressed(fcn::KeyEvent &keyEvent)
Definition: commandline.cpp:70
boost::function1< void, std::string > type_callback
Definition: commandline.h:45
void toggleCaretVisible()
Toggle the caret visibility.
Definition: commandline.cpp:55
void startBlinking()
Start blinking the caret.
Definition: commandline.cpp:65
void stop()
Stop the timer.
Definition: timer.cpp:53
std::vector< std::string > m_history
Definition: commandline.h:75
unsigned int uint32_t
Definition: core.h:40
~CommandLine()
Destructor.
Definition: commandline.cpp:52
virtual void drawCaret(fcn::Graphics *graphics, int32_t x)
CommandLine()
Constructor.
Definition: commandline.cpp:41
void setCallback(const type_callback &callback)
Set the callback that will be called.
Definition: timer.cpp:64
Timer m_suppressBlinkTimer
Definition: commandline.h:80