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