FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
commandline.cpp
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 *
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 gcn;
39 
40 
41  CommandLine::CommandLine() : gcn::UTF8TextField(), m_history_position(0) {
42 
43  m_blinkTimer.setInterval(500);
44  m_blinkTimer.setCallback(boost::bind(&CommandLine::toggleCaretVisible,this));
45  m_blinkTimer.start();
46 
47  m_suppressBlinkTimer.setInterval(2000);
48  m_suppressBlinkTimer
49  .setCallback(boost::bind(&CommandLine::startBlinking,this));
50  }
51 
53  }
54 
56  m_caretVisible = !m_caretVisible;
57  }
58 
60  m_suppressBlinkTimer.start();
61  m_blinkTimer.stop();
62  m_caretVisible = true;
63  }
64 
66  m_suppressBlinkTimer.stop();
67  m_blinkTimer.start();
68  }
69 
70  void CommandLine::keyPressed(gcn::KeyEvent &keyEvent) {
71  gcn::Key key = keyEvent.getKey();
72  int32_t keyType = key.getValue();
73 
74  if (keyType == Key::LEFT && mCaretPosition > 0)
75  {
76  UTF8TextField::keyPressed(keyEvent);
77  }
78  else if (keyType == Key::RIGHT && mCaretPosition < mText.size())
79  {
80  UTF8TextField::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 = mText;
98  }
99  --m_history_position;
100  setText( m_history[m_history_position] );
101  };
102  }
103  else if (keyType == Key::DELETE && mCaretPosition < mText.size())
104  {
105  UTF8TextField::keyPressed(keyEvent);
106  }
107  else if (keyType == Key::BACKSPACE && mCaretPosition > 0)
108  {
109  UTF8TextField::keyPressed(keyEvent);
110  }
111  else if (keyType == Key::ENTER)
112  {
113  if( mText != "" ) {
114  if(m_callback) {
115  m_callback( mText );
116  }
117  m_history.push_back( mText );
118  m_history_position = m_history.size();
119  setText("");
120  }
121  }
122  else if (keyType == Key::HOME)
123  {
124  mCaretPosition = 0;
125  }
126  else if (keyType == Key::END)
127  {
128  mCaretPosition = mText.size();
129  }
130  else if (key.isCharacter())
131  {
132  UTF8TextField::keyPressed(keyEvent);
133  }
134  stopBlinking();
135  fixScroll();
136  }
137 
138  void CommandLine::drawCaret(gcn::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 
148  void CommandLine::setCallback(const type_callback& cb) {
149  m_callback = cb;
150  }
151 
152 }
153 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
void setCallback(const type_callback &cb)
virtual void keyPressed(KeyEvent &keyEvent)
void toggleCaretVisible()
Definition: commandline.cpp:55