1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * Reto Grieder |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "InputBuffer.h" |
---|
30 | |
---|
31 | #include "util/Clipboard.h" |
---|
32 | #include "core/CoreIncludes.h" |
---|
33 | #include "core/ConfigValueIncludes.h" |
---|
34 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | InputBuffer::InputBuffer() |
---|
38 | { |
---|
39 | RegisterRootObject(InputBuffer); |
---|
40 | |
---|
41 | this->buffer_ = ""; |
---|
42 | this->cursor_ = 0; |
---|
43 | this->allowedChars_ = "abcdefghijklmnopqrstuvwxyz \ |
---|
44 | ABCDEFGHIJKLMNOPQRSTUVWXYZ \ |
---|
45 | äëïöüÄËÏÖÜáâàéêèíîìóôòúûù \ |
---|
46 | 0123456789 \ |
---|
47 | \\\"(){}[]<>.:,;_-+*/=!?|$&%^~#"; |
---|
48 | |
---|
49 | this->lastKey_ = KeyCode::Unassigned; |
---|
50 | this->timeSinceKeyPressed_ = 0.0f; |
---|
51 | this->timeSinceKeyRepeated_ = 0.0f; |
---|
52 | this->keysToRepeat_ = 0; |
---|
53 | |
---|
54 | setConfigValues(); |
---|
55 | } |
---|
56 | |
---|
57 | InputBuffer::InputBuffer(const std::string& allowedChars) |
---|
58 | { |
---|
59 | RegisterRootObject(InputBuffer); |
---|
60 | |
---|
61 | this->allowedChars_ = allowedChars; |
---|
62 | this->buffer_ = ""; |
---|
63 | this->cursor_ = 0; |
---|
64 | |
---|
65 | this->lastKey_ = KeyCode::Unassigned; |
---|
66 | this->timeSinceKeyPressed_ = 0.0f; |
---|
67 | this->timeSinceKeyRepeated_ = 0.0f; |
---|
68 | this->keysToRepeat_ = 0; |
---|
69 | |
---|
70 | setConfigValues(); |
---|
71 | } |
---|
72 | |
---|
73 | InputBuffer::~InputBuffer() |
---|
74 | { |
---|
75 | for (std::list<BaseInputBufferListenerTuple*>::const_iterator it = this->listeners_.begin(); |
---|
76 | it != this->listeners_.end(); ++it) |
---|
77 | delete *it; |
---|
78 | } |
---|
79 | |
---|
80 | void InputBuffer::setConfigValues() |
---|
81 | { |
---|
82 | SetConfigValue(keyRepeatDeleay_, 0.4).description("Key repeat delay of the input buffer"); |
---|
83 | SetConfigValue(keyRepeatTime_, 0.022).description("Key repeat time of the input buffer"); |
---|
84 | |
---|
85 | if (keyRepeatDeleay_ < 0.0) |
---|
86 | { |
---|
87 | ResetConfigValue(keyRepeatDeleay_); |
---|
88 | } |
---|
89 | if (keyRepeatTime_ < 0.0) |
---|
90 | { |
---|
91 | ResetConfigValue(keyRepeatTime_); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | void InputBuffer::set(const std::string& input, bool update) |
---|
96 | { |
---|
97 | this->clear(false); |
---|
98 | this->insert(input, update); |
---|
99 | } |
---|
100 | |
---|
101 | void InputBuffer::insert(const std::string& input, bool update) |
---|
102 | { |
---|
103 | for (unsigned int i = 0; i < input.size(); ++i) |
---|
104 | { |
---|
105 | this->insert(input[i], false); |
---|
106 | |
---|
107 | if (update) |
---|
108 | this->updated(input[i], false); |
---|
109 | } |
---|
110 | |
---|
111 | if (update) |
---|
112 | this->updated(); |
---|
113 | } |
---|
114 | |
---|
115 | void InputBuffer::insert(const char& input, bool update) |
---|
116 | { |
---|
117 | if (this->charIsAllowed(input)) |
---|
118 | { |
---|
119 | this->buffer_.insert(this->cursor_, 1, input); |
---|
120 | ++this->cursor_; |
---|
121 | } |
---|
122 | |
---|
123 | if (update) |
---|
124 | this->updated(input, true); |
---|
125 | } |
---|
126 | |
---|
127 | void InputBuffer::clear(bool update) |
---|
128 | { |
---|
129 | this->buffer_ = ""; |
---|
130 | this->cursor_ = 0; |
---|
131 | |
---|
132 | if (update) |
---|
133 | this->updated(); |
---|
134 | } |
---|
135 | |
---|
136 | void InputBuffer::removeBehindCursor(bool update) |
---|
137 | { |
---|
138 | if (this->cursor_ > 0) |
---|
139 | { |
---|
140 | --this->cursor_; |
---|
141 | this->buffer_.erase(this->cursor_, 1); |
---|
142 | |
---|
143 | if (update) |
---|
144 | this->updated(); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | void InputBuffer::removeAtCursor(bool update) |
---|
149 | { |
---|
150 | if (this->cursor_ < this->buffer_.size()) |
---|
151 | { |
---|
152 | this->buffer_.erase(this->cursor_, 1); |
---|
153 | |
---|
154 | if (update) |
---|
155 | this->updated(); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | void InputBuffer::updated() |
---|
160 | { |
---|
161 | for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
162 | { |
---|
163 | if ((*it)->bListenToAllChanges_) |
---|
164 | (*it)->callFunction(); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | void InputBuffer::updated(const char& update, bool bSingleInput) |
---|
169 | { |
---|
170 | for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
171 | { |
---|
172 | if ((!(*it)->trueKeyFalseChar_) && ((*it)->bListenToAllChanges_ || ((*it)->char_ == update)) && (!(*it)->bOnlySingleInput_ || bSingleInput)) |
---|
173 | (*it)->callFunction(); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | bool InputBuffer::charIsAllowed(const char& input) |
---|
178 | { |
---|
179 | if (this->allowedChars_ == "") |
---|
180 | return true; |
---|
181 | else |
---|
182 | return (this->allowedChars_.find(input) != std::string::npos); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | void InputBuffer::processKey(const KeyEvent& evt) |
---|
187 | { |
---|
188 | // Prevent disaster when switching applications |
---|
189 | if (evt.isModifierDown(KeyboardModifier::Alt) && evt.getKeyCode() == KeyCode::Tab) |
---|
190 | return; |
---|
191 | |
---|
192 | for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
193 | { |
---|
194 | if ((*it)->trueKeyFalseChar_ && ((*it)->key_ == evt.getKeyCode())) |
---|
195 | (*it)->callFunction(); |
---|
196 | } |
---|
197 | |
---|
198 | if (evt.isModifierDown(KeyboardModifier::Ctrl)) |
---|
199 | { |
---|
200 | if (evt.getKeyCode() == KeyCode::V) |
---|
201 | this->insert(fromClipboard()); |
---|
202 | else if (evt.getKeyCode() == KeyCode::C) |
---|
203 | toClipboard(this->buffer_); |
---|
204 | else if (evt.getKeyCode() == KeyCode::X) |
---|
205 | { |
---|
206 | toClipboard(this->buffer_); |
---|
207 | this->clear(); |
---|
208 | } |
---|
209 | } |
---|
210 | else if (evt.isModifierDown(KeyboardModifier::Shift)) |
---|
211 | { |
---|
212 | if (evt.getKeyCode() == KeyCode::Insert) |
---|
213 | this->insert(fromClipboard()); |
---|
214 | else if (evt.getKeyCode() == KeyCode::Delete) |
---|
215 | { |
---|
216 | toClipboard(this->buffer_); |
---|
217 | this->clear(); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | this->insert(static_cast<char>(evt.getText())); |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | @brief This update() function is called by the InputState if the InputBuffer is active. |
---|
226 | @param dt Delta time |
---|
227 | */ |
---|
228 | void InputBuffer::keyboardUpdated(float dt) |
---|
229 | { |
---|
230 | timeSinceKeyPressed_ += dt; |
---|
231 | if (keysToRepeat_ < 10 && timeSinceKeyPressed_ > keyRepeatDeleay_) |
---|
232 | { |
---|
233 | // initial time out has gone by, start repeating keys |
---|
234 | while (timeSinceKeyPressed_ - timeSinceKeyRepeated_ > keyRepeatTime_) |
---|
235 | { |
---|
236 | timeSinceKeyRepeated_ += keyRepeatTime_; |
---|
237 | keysToRepeat_++; |
---|
238 | } |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | void InputBuffer::buttonPressed(const KeyEvent& evt) |
---|
243 | { |
---|
244 | lastKey_ = evt.getKeyCode(); |
---|
245 | timeSinceKeyPressed_ = 0.0; |
---|
246 | timeSinceKeyRepeated_ = keyRepeatDeleay_; |
---|
247 | keysToRepeat_ = 0; |
---|
248 | |
---|
249 | processKey(evt); |
---|
250 | } |
---|
251 | |
---|
252 | void InputBuffer::buttonHeld(const KeyEvent& evt) |
---|
253 | { |
---|
254 | if (evt.getKeyCode() == lastKey_) |
---|
255 | { |
---|
256 | while (keysToRepeat_) |
---|
257 | { |
---|
258 | processKey(evt); |
---|
259 | keysToRepeat_--; |
---|
260 | } |
---|
261 | } |
---|
262 | } |
---|
263 | } |
---|