comparison engine/core/gui/console/commandline.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
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::TextField("") {
42 m_history_position = 0;
43
44 m_blinkTimer.setInterval(500);
45 m_blinkTimer.setCallback(boost::bind(&CommandLine::toggleCaretVisible,this));
46 m_blinkTimer.start();
47
48 m_suppressBlinkTimer.setInterval(2000);
49 m_suppressBlinkTimer
50 .setCallback(boost::bind(&CommandLine::startBlinking,this));
51 }
52
53 CommandLine::~CommandLine() {
54 }
55
56 void CommandLine::toggleCaretVisible() {
57 m_caretVisible = !m_caretVisible;
58 }
59
60 void CommandLine::stopBlinking() {
61 m_suppressBlinkTimer.start();
62 m_blinkTimer.stop();
63 m_caretVisible = true;
64 }
65
66 void CommandLine::startBlinking() {
67 m_suppressBlinkTimer.stop();
68 m_blinkTimer.start();
69 }
70
71 void CommandLine::keyPressed(gcn::KeyEvent &keyEvent) {
72 gcn::Key key = keyEvent.getKey();
73
74 if (key.getValue() == Key::LEFT && mCaretPosition > 0)
75 {
76 --mCaretPosition;
77 }
78 else if (key.getValue() == Key::RIGHT && mCaretPosition < mText.size())
79 {
80 ++mCaretPosition;
81 }
82 else if (key.getValue() == Key::DOWN && !m_history.empty())
83 {
84 if( m_history_position < m_history.size() ) {
85 ++m_history_position;
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 (key.getValue() == 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 (key.getValue() == Key::DELETE && mCaretPosition < mText.size())
104 {
105 mText.erase(mCaretPosition, 1);
106 }
107 else if (key.getValue() == Key::BACKSPACE && mCaretPosition > 0)
108 {
109 mText.erase(mCaretPosition - 1, 1);
110 --mCaretPosition;
111 }
112 else if (key.getValue() == Key::ENTER)
113 {
114 if( mText != "" ) {
115 if(m_callback) {
116 m_callback( mText );
117 }
118 m_history.push_back( mText );
119 m_history_position = m_history.size();
120 setText("");
121 }
122 }
123 else if (key.getValue() == Key::HOME)
124 {
125 mCaretPosition = 0;
126 }
127 else if (key.getValue() == Key::END)
128 {
129 mCaretPosition = mText.size();
130 }
131 else if (key.isCharacter())
132 {
133 m_cmdline = mText;
134 m_history_position = m_history.size();
135
136 mText.insert(mCaretPosition, std::string(1,char(key.getValue())));
137 ++mCaretPosition;
138 }
139 stopBlinking();
140 fixScroll();
141 }
142
143 void CommandLine::drawCaret(gcn::Graphics * graphics, int x) {
144 if( !m_caretVisible )
145 return;
146
147 graphics->setColor(getForegroundColor());
148 graphics->drawLine(x, getHeight() - 2, x, 1);
149 graphics->drawLine(x+1, getHeight() - 2, x+1, 1);
150 }
151
152
153 void CommandLine::setCallback(const type_callback& cb) {
154 m_callback = cb;
155 }
156
157 }
158 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */