| 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 <iostream> | 
|---|
| 32 |  | 
|---|
| 33 | #include "util/Clipboard.h" | 
|---|
| 34 | #include "core/CoreIncludes.h" | 
|---|
| 35 | #include "core/ConfigValueIncludes.h" | 
|---|
| 36 |  | 
|---|
| 37 | namespace orxonox | 
|---|
| 38 | { | 
|---|
| 39 | InputBuffer::InputBuffer() | 
|---|
| 40 | { | 
|---|
| 41 | RegisterRootObject(InputBuffer); | 
|---|
| 42 |  | 
|---|
| 43 | this->buffer_ = ""; | 
|---|
| 44 | this->cursor_ = 0; | 
|---|
| 45 | this->allowedChars_ = "abcdefghijklmnopqrstuvwxyz \ | 
|---|
| 46 | ABCDEFGHIJKLMNOPQRSTUVWXYZ \ | 
|---|
| 47 | äëïöüÄËÏÖÜáâàéêèíîìóôòúûù \ | 
|---|
| 48 | 0123456789 \ | 
|---|
| 49 | \\\"(){}[]<>.:,;_-+*/=!?|$&%^~#"; | 
|---|
| 50 |  | 
|---|
| 51 | this->lastKey_ = KeyCode::Unassigned; | 
|---|
| 52 | this->timeSinceKeyPressed_ = 0.0f; | 
|---|
| 53 | this->timeSinceKeyRepeated_ = 0.0f; | 
|---|
| 54 | this->keysToRepeat_ = 0; | 
|---|
| 55 |  | 
|---|
| 56 | setConfigValues(); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | InputBuffer::InputBuffer(const std::string allowedChars) | 
|---|
| 60 | { | 
|---|
| 61 | RegisterRootObject(InputBuffer); | 
|---|
| 62 |  | 
|---|
| 63 | this->allowedChars_ = allowedChars; | 
|---|
| 64 | this->buffer_ = ""; | 
|---|
| 65 | this->cursor_ = 0; | 
|---|
| 66 |  | 
|---|
| 67 | this->lastKey_ = KeyCode::Unassigned; | 
|---|
| 68 | this->timeSinceKeyPressed_ = 0.0f; | 
|---|
| 69 | this->timeSinceKeyRepeated_ = 0.0f; | 
|---|
| 70 | this->keysToRepeat_ = 0; | 
|---|
| 71 |  | 
|---|
| 72 | setConfigValues(); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | void InputBuffer::setConfigValues() | 
|---|
| 76 | { | 
|---|
| 77 | SetConfigValue(keyRepeatDeleay_, 0.4).description("Key repeat deleay of the input buffer"); | 
|---|
| 78 | SetConfigValue(keyRepeatTime_, 0.022).description("Key repeat time of the input buffer"); | 
|---|
| 79 |  | 
|---|
| 80 | if (keyRepeatDeleay_ < 0.0) | 
|---|
| 81 | { | 
|---|
| 82 | ResetConfigValue(keyRepeatDeleay_); | 
|---|
| 83 | } | 
|---|
| 84 | if (keyRepeatTime_ < 0.0) | 
|---|
| 85 | { | 
|---|
| 86 | ResetConfigValue(keyRepeatTime_); | 
|---|
| 87 | } | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | void InputBuffer::set(const std::string& input, bool update) | 
|---|
| 91 | { | 
|---|
| 92 | this->clear(false); | 
|---|
| 93 | this->insert(input, update); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | void InputBuffer::insert(const std::string& input, bool update) | 
|---|
| 97 | { | 
|---|
| 98 | for (unsigned int i = 0; i < input.size(); ++i) | 
|---|
| 99 | { | 
|---|
| 100 | this->insert(input[i], false); | 
|---|
| 101 |  | 
|---|
| 102 | if (update) | 
|---|
| 103 | this->updated(input[i], false); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | if (update) | 
|---|
| 107 | this->updated(); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | void InputBuffer::insert(const char& input, bool update) | 
|---|
| 111 | { | 
|---|
| 112 | if (this->charIsAllowed(input)) | 
|---|
| 113 | { | 
|---|
| 114 | this->buffer_.insert(this->cursor_, 1, input); | 
|---|
| 115 | ++this->cursor_; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | if (update) | 
|---|
| 119 | this->updated(input, true); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | void InputBuffer::clear(bool update) | 
|---|
| 123 | { | 
|---|
| 124 | this->buffer_ = ""; | 
|---|
| 125 | this->cursor_ = 0; | 
|---|
| 126 |  | 
|---|
| 127 | if (update) | 
|---|
| 128 | this->updated(); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | void InputBuffer::removeBehindCursor(bool update) | 
|---|
| 132 | { | 
|---|
| 133 | if (this->cursor_ > 0) | 
|---|
| 134 | { | 
|---|
| 135 | --this->cursor_; | 
|---|
| 136 | this->buffer_.erase(this->cursor_, 1); | 
|---|
| 137 |  | 
|---|
| 138 | if (update) | 
|---|
| 139 | this->updated(); | 
|---|
| 140 | } | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | void InputBuffer::removeAtCursor(bool update) | 
|---|
| 144 | { | 
|---|
| 145 | if (this->cursor_ < this->buffer_.size()) | 
|---|
| 146 | { | 
|---|
| 147 | this->buffer_.erase(this->cursor_, 1); | 
|---|
| 148 |  | 
|---|
| 149 | if (update) | 
|---|
| 150 | this->updated(); | 
|---|
| 151 | } | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | void InputBuffer::updated() | 
|---|
| 155 | { | 
|---|
| 156 | for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) | 
|---|
| 157 | { | 
|---|
| 158 | if ((*it)->bListenToAllChanges_) | 
|---|
| 159 | (*it)->callFunction(); | 
|---|
| 160 | } | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void InputBuffer::updated(const char& update, bool bSingleInput) | 
|---|
| 164 | { | 
|---|
| 165 | for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) | 
|---|
| 166 | { | 
|---|
| 167 | if ((!(*it)->trueKeyFalseChar_) && ((*it)->bListenToAllChanges_ || ((*it)->char_ == update)) && (!(*it)->bOnlySingleInput_ || bSingleInput)) | 
|---|
| 168 | (*it)->callFunction(); | 
|---|
| 169 | } | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | bool InputBuffer::charIsAllowed(const char& input) | 
|---|
| 173 | { | 
|---|
| 174 | if (this->allowedChars_ == "") | 
|---|
| 175 | return true; | 
|---|
| 176 | else | 
|---|
| 177 | return (this->allowedChars_.find(input) != std::string::npos); | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 |  | 
|---|
| 181 | void InputBuffer::processKey(const KeyEvent &evt) | 
|---|
| 182 | { | 
|---|
| 183 | if (evt.isModifierDown(KeyboardModifier::Alt) && evt.key == KeyCode::Tab) | 
|---|
| 184 | return; | 
|---|
| 185 |  | 
|---|
| 186 | for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) | 
|---|
| 187 | { | 
|---|
| 188 | if ((*it)->trueKeyFalseChar_ && ((*it)->key_ == evt.key)) | 
|---|
| 189 | (*it)->callFunction(); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | if (evt.isModifierDown(KeyboardModifier::Ctrl)) | 
|---|
| 193 | { | 
|---|
| 194 | if (evt.key == KeyCode::V) | 
|---|
| 195 | this->insert(fromClipboard()); | 
|---|
| 196 | else if (evt.key == KeyCode::C) | 
|---|
| 197 | toClipboard(this->buffer_); | 
|---|
| 198 | else if (evt.key == KeyCode::X) | 
|---|
| 199 | { | 
|---|
| 200 | toClipboard(this->buffer_); | 
|---|
| 201 | this->clear(); | 
|---|
| 202 | } | 
|---|
| 203 | } | 
|---|
| 204 | else if (evt.isModifierDown(KeyboardModifier::Shift)) | 
|---|
| 205 | { | 
|---|
| 206 | if (evt.key == KeyCode::Insert) | 
|---|
| 207 | this->insert(fromClipboard()); | 
|---|
| 208 | else if (evt.key == KeyCode::Delete) | 
|---|
| 209 | { | 
|---|
| 210 | toClipboard(this->buffer_); | 
|---|
| 211 | this->clear(); | 
|---|
| 212 | } | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | this->insert((char)evt.text); | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | /** | 
|---|
| 219 | @brief This tick() function is called by the InputManager if the InputBuffer is active. | 
|---|
| 220 | @param dt Delta time | 
|---|
| 221 | */ | 
|---|
| 222 | void InputBuffer::tickInput(float dt) | 
|---|
| 223 | { | 
|---|
| 224 | timeSinceKeyPressed_ += dt; | 
|---|
| 225 | if (keysToRepeat_ < 10 && timeSinceKeyPressed_ > keyRepeatDeleay_) | 
|---|
| 226 | { | 
|---|
| 227 | // initial time out has gone by, start repeating keys | 
|---|
| 228 | while (timeSinceKeyPressed_ - timeSinceKeyRepeated_ > keyRepeatTime_) | 
|---|
| 229 | { | 
|---|
| 230 | timeSinceKeyRepeated_ += keyRepeatTime_; | 
|---|
| 231 | keysToRepeat_++; | 
|---|
| 232 | } | 
|---|
| 233 | } | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | void InputBuffer::keyPressed(const KeyEvent &evt) | 
|---|
| 237 | { | 
|---|
| 238 | lastKey_ = evt.key; | 
|---|
| 239 | timeSinceKeyPressed_ = 0.0; | 
|---|
| 240 | timeSinceKeyRepeated_ = keyRepeatDeleay_; | 
|---|
| 241 | keysToRepeat_ = 0; | 
|---|
| 242 |  | 
|---|
| 243 | processKey(evt); | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | void InputBuffer::keyHeld(const KeyEvent& evt) | 
|---|
| 247 | { | 
|---|
| 248 | if (evt.key == lastKey_) | 
|---|
| 249 | { | 
|---|
| 250 | while (keysToRepeat_) | 
|---|
| 251 | { | 
|---|
| 252 | processKey(evt); | 
|---|
| 253 | keysToRepeat_--; | 
|---|
| 254 | } | 
|---|
| 255 | } | 
|---|
| 256 | } | 
|---|
| 257 | } | 
|---|