[1293] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 3 | * > www.orxonox.net < |
---|
[1293] | 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: |
---|
[955] | 23 | * Fabian 'x3n' Landau |
---|
[1293] | 24 | * Co-authors: |
---|
| 25 | * Reto Grieder |
---|
| 26 | * |
---|
| 27 | */ |
---|
[955] | 28 | |
---|
[1062] | 29 | #include "InputBuffer.h" |
---|
| 30 | |
---|
[955] | 31 | #include <iostream> |
---|
| 32 | |
---|
[1062] | 33 | #include "util/Clipboard.h" |
---|
[1293] | 34 | #include "CoreIncludes.h" |
---|
| 35 | #include "ConfigValueIncludes.h" |
---|
[955] | 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
[1293] | 39 | InputBuffer::InputBuffer(const std::string allowedChars) : |
---|
| 40 | buffer_(""), |
---|
| 41 | allowedChars_(allowedChars), |
---|
| 42 | lastKey_(KeyCode::Unassigned), |
---|
| 43 | timeSinceKeyPressed_(0.0f), |
---|
| 44 | timeSinceKeyRepeated_(0.0f), |
---|
| 45 | keysToRepeat_(0) |
---|
| 46 | { |
---|
| 47 | RegisterObject(InputBuffer); |
---|
| 48 | setConfigValues(); |
---|
| 49 | } |
---|
[955] | 50 | |
---|
[1293] | 51 | void InputBuffer::setConfigValues() |
---|
| 52 | { |
---|
| 53 | SetConfigValue(keyRepeatDeleay_, 0.4).description("Key repeat deleay of the input buffer"); |
---|
| 54 | SetConfigValue(keyRepeatTime_, 0.022).description("Key repeat time of the input buffer"); |
---|
| 55 | if (keyRepeatDeleay_ < 0.0) |
---|
[1214] | 56 | { |
---|
[1293] | 57 | ResetConfigValue(keyRepeatDeleay_); |
---|
[1214] | 58 | } |
---|
[1293] | 59 | if (keyRepeatTime_ < 0.0) |
---|
[955] | 60 | { |
---|
[1293] | 61 | ResetConfigValue(keyRepeatTime_); |
---|
[955] | 62 | } |
---|
[1293] | 63 | } |
---|
| 64 | /* |
---|
| 65 | void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), bool bOnlySingleInput) |
---|
| 66 | { |
---|
| 67 | struct InputBufferListenerTuple newListener = {listener, function, true, bOnlySingleInput, ' '}; |
---|
| 68 | listeners_.insert(listeners_.end(), newListener); |
---|
| 69 | } |
---|
[955] | 70 | |
---|
[1293] | 71 | void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), char char_, bool bOnlySingleInput) |
---|
| 72 | { |
---|
| 73 | struct InputBufferListenerTuple newListener = {listener, function, false, bOnlySingleInput, char_}; |
---|
| 74 | listeners_.insert(listeners_.end(), newListener); |
---|
| 75 | } |
---|
| 76 | */ |
---|
| 77 | void InputBuffer::set(const std::string& input) |
---|
| 78 | { |
---|
| 79 | buffer_ = ""; |
---|
| 80 | append(input); |
---|
| 81 | } |
---|
[955] | 82 | |
---|
[1293] | 83 | void InputBuffer::append(const std::string& input) |
---|
| 84 | { |
---|
| 85 | for (unsigned int i = 0; i < input.size(); i++) |
---|
[955] | 86 | { |
---|
[1293] | 87 | if (charIsAllowed(input[i])) |
---|
| 88 | buffer_ += input[i]; |
---|
[955] | 89 | |
---|
[1293] | 90 | updated(input[i], false); |
---|
[955] | 91 | } |
---|
[1293] | 92 | updated(); |
---|
| 93 | } |
---|
[955] | 94 | |
---|
[1293] | 95 | void InputBuffer::append(const char& input) |
---|
| 96 | { |
---|
| 97 | if (charIsAllowed(input)) |
---|
| 98 | buffer_ += input; |
---|
[955] | 99 | |
---|
[1293] | 100 | updated(input, true); |
---|
| 101 | } |
---|
[955] | 102 | |
---|
[1293] | 103 | void InputBuffer::clear() |
---|
| 104 | { |
---|
| 105 | buffer_ = ""; |
---|
| 106 | updated(); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | void InputBuffer::removeLast() |
---|
| 110 | { |
---|
| 111 | buffer_ = buffer_.substr(0, buffer_.size() - 1); |
---|
| 112 | updated(); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void InputBuffer::updated() |
---|
| 116 | { |
---|
| 117 | for (std::list<InputBufferListenerTuple>::iterator it = listeners_.begin(); it != listeners_.end(); ++it) |
---|
[955] | 118 | { |
---|
[1293] | 119 | if ((*it).bListenToAllChanges_) |
---|
| 120 | (*(*it).listener_.*(*it).function_)(); |
---|
[955] | 121 | } |
---|
[1293] | 122 | } |
---|
[955] | 123 | |
---|
[1293] | 124 | void InputBuffer::updated(const char& update, bool bSingleInput) |
---|
| 125 | { |
---|
| 126 | for (std::list<InputBufferListenerTuple>::iterator it = listeners_.begin(); it != listeners_.end(); ++it) |
---|
[955] | 127 | { |
---|
[1293] | 128 | if (((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput)) |
---|
| 129 | (*(*it).listener_.*(*it).function_)(); |
---|
[955] | 130 | } |
---|
[1293] | 131 | } |
---|
[955] | 132 | |
---|
[1293] | 133 | bool InputBuffer::charIsAllowed(const char& input) |
---|
| 134 | { |
---|
| 135 | if (allowedChars_ == "") |
---|
| 136 | return true; |
---|
| 137 | else |
---|
| 138 | return (allowedChars_.find(input) != std::string::npos); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | void InputBuffer::processKey(const KeyEvent &evt) |
---|
| 143 | { |
---|
| 144 | if (evt.isModifierDown(KeyboardModifier::Ctrl)) |
---|
[955] | 145 | { |
---|
[1293] | 146 | if (evt.key == KeyCode::V) |
---|
| 147 | append(fromClipboard()); |
---|
| 148 | else if (evt.key == KeyCode::C) |
---|
| 149 | toClipboard(buffer_); |
---|
| 150 | else if (evt.key == KeyCode::X) |
---|
| 151 | { |
---|
| 152 | toClipboard(buffer_); |
---|
| 153 | clear(); |
---|
| 154 | } |
---|
[955] | 155 | } |
---|
[1293] | 156 | else if (evt.isModifierDown(KeyboardModifier::Shift)) |
---|
[955] | 157 | { |
---|
[1293] | 158 | if (evt.key == KeyCode::Insert) |
---|
| 159 | { |
---|
| 160 | append(fromClipboard()); |
---|
| 161 | } |
---|
| 162 | else if (evt.key == KeyCode::Delete) |
---|
| 163 | { |
---|
| 164 | toClipboard(buffer_); |
---|
| 165 | clear(); |
---|
| 166 | } |
---|
[955] | 167 | } |
---|
[1293] | 168 | //std::string input = keyboard_->getAsString(e.key); |
---|
| 169 | /*if (input.size() >= 1) |
---|
| 170 | append(input[0]);*/ |
---|
[955] | 171 | |
---|
[1293] | 172 | append((char)evt.text); |
---|
| 173 | } |
---|
[955] | 174 | |
---|
[1349] | 175 | /** |
---|
| 176 | * This tick() function is called by the InputManager if the InputBuffer is active. |
---|
| 177 | * @param dt Delta time |
---|
| 178 | */ |
---|
[1293] | 179 | void InputBuffer::tick(float dt) |
---|
| 180 | { |
---|
| 181 | timeSinceKeyPressed_ += dt; |
---|
| 182 | if (keysToRepeat_ < 10 && timeSinceKeyPressed_ > keyRepeatDeleay_) |
---|
[955] | 183 | { |
---|
[1293] | 184 | // initial time out has gone by, start repeating keys |
---|
| 185 | while (timeSinceKeyPressed_ - timeSinceKeyRepeated_ > keyRepeatTime_) |
---|
| 186 | { |
---|
| 187 | timeSinceKeyRepeated_ += keyRepeatTime_; |
---|
| 188 | keysToRepeat_++; |
---|
| 189 | } |
---|
[955] | 190 | } |
---|
[1293] | 191 | } |
---|
[955] | 192 | |
---|
[1349] | 193 | void InputBuffer::keyPressed(const KeyEvent &evt) |
---|
[1293] | 194 | { |
---|
| 195 | lastKey_ = evt.key; |
---|
| 196 | timeSinceKeyPressed_ = 0.0; |
---|
| 197 | timeSinceKeyRepeated_ = keyRepeatDeleay_; |
---|
| 198 | keysToRepeat_ = 0; |
---|
[955] | 199 | |
---|
[1293] | 200 | processKey(evt); |
---|
| 201 | } |
---|
[966] | 202 | |
---|
[1349] | 203 | void InputBuffer::keyHeld(const KeyEvent& evt) |
---|
[1293] | 204 | { |
---|
| 205 | if (evt.key == lastKey_) |
---|
| 206 | { |
---|
| 207 | while (keysToRepeat_) |
---|
| 208 | { |
---|
| 209 | processKey(evt); |
---|
| 210 | keysToRepeat_--; |
---|
| 211 | } |
---|
[955] | 212 | } |
---|
[1293] | 213 | } |
---|
[955] | 214 | |
---|
| 215 | } |
---|