Debian Codegraph @

engine/core/gui/fifechan/console/commandline.cpp

raw
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
42 m_blinkTimer.setInterval(500);
43 m_blinkTimer.setCallback(std::bind(&CommandLine::toggleCaretVisible,this));
44 m_blinkTimer.start();
45
46 m_suppressBlinkTimer.setInterval(2000);
47 m_suppressBlinkTimer.setCallback(std::bind(&CommandLine::startBlinking,this));
48 }
49
50 CommandLine::~CommandLine() {
51 }
52
53 void CommandLine::toggleCaretVisible() {
54 m_caretVisible = !m_caretVisible;
55 }
56
57 void CommandLine::stopBlinking() {
58 m_suppressBlinkTimer.start();
59 m_blinkTimer.stop();
60 m_caretVisible = true;
61 }
62
63 void CommandLine::startBlinking() {
64 m_suppressBlinkTimer.stop();
65 m_blinkTimer.start();
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 }
97 --m_history_position;
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
146 void CommandLine::setCallback(const type_callback& cb) {
147 m_callback = cb;
148 }
149
150 }