[955] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 3 | * > www.orxonox.net < |
---|
[955] | 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 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[1062] | 29 | #include "InputBuffer.h" |
---|
| 30 | |
---|
[955] | 31 | #include <iostream> |
---|
| 32 | |
---|
[1062] | 33 | #include "util/Clipboard.h" |
---|
[1052] | 34 | #include "InputManager.h" |
---|
[955] | 35 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
[1052] | 38 | InputBuffer::InputBuffer() |
---|
[955] | 39 | { |
---|
[1312] | 40 | this->allowedChars_ = "abcdefghijklmnopqrstuvwxyz \ |
---|
| 41 | ABCDEFGHIJKLMNOPQRSTUVWXYZ \ |
---|
| 42 | äëïöüÄËÏÖÜáâàéêèíîìóôòúûù \ |
---|
| 43 | 0123456789 \ |
---|
| 44 | \\\"(){}[]<>.:,;_-+*/=!?|$&%^~#"; |
---|
[1052] | 45 | this->keyboard_ = InputManager::getSingleton().getKeyboard(); |
---|
[955] | 46 | this->buffer_ = ""; |
---|
[1313] | 47 | this->cursor_ = 0; |
---|
[1317] | 48 | |
---|
| 49 | InputManager::getSingleton().feedInputBuffer(this); |
---|
[955] | 50 | } |
---|
[1151] | 51 | |
---|
| 52 | InputBuffer::InputBuffer(const std::string allowedChars) |
---|
| 53 | { |
---|
| 54 | this->allowedChars_ = allowedChars; |
---|
| 55 | this->keyboard_ = InputManager::getSingleton().getKeyboard(); |
---|
| 56 | this->buffer_ = ""; |
---|
[1313] | 57 | this->cursor_ = 0; |
---|
[1317] | 58 | |
---|
| 59 | InputManager::getSingleton().feedInputBuffer(this); |
---|
[1151] | 60 | } |
---|
[955] | 61 | |
---|
[1313] | 62 | void InputBuffer::unregisterListener(InputBufferListener* listener) |
---|
[955] | 63 | { |
---|
[1313] | 64 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) |
---|
| 65 | { |
---|
| 66 | if ((*it).listener_ == listener) |
---|
| 67 | this->listeners_.erase(it++); |
---|
| 68 | else |
---|
| 69 | ++it; |
---|
| 70 | } |
---|
[955] | 71 | } |
---|
| 72 | |
---|
[1313] | 73 | void InputBuffer::set(const std::string& input, bool update) |
---|
[955] | 74 | { |
---|
[1313] | 75 | this->clear(false); |
---|
| 76 | this->insert(input, update); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void InputBuffer::insert(const std::string& input, bool update) |
---|
| 80 | { |
---|
| 81 | for (unsigned int i = 0; i < input.size(); ++i) |
---|
[955] | 82 | { |
---|
[1313] | 83 | this->insert(input[i], false); |
---|
[955] | 84 | |
---|
[1313] | 85 | if (update) |
---|
| 86 | this->updated(input[i], false); |
---|
[955] | 87 | } |
---|
[1313] | 88 | |
---|
| 89 | if (update) |
---|
| 90 | this->updated(); |
---|
[955] | 91 | } |
---|
| 92 | |
---|
[1313] | 93 | void InputBuffer::insert(const char& input, bool update) |
---|
[955] | 94 | { |
---|
| 95 | if (this->charIsAllowed(input)) |
---|
[1313] | 96 | { |
---|
| 97 | this->buffer_.insert(this->cursor_, 1, input); |
---|
| 98 | ++this->cursor_; |
---|
| 99 | } |
---|
[955] | 100 | |
---|
[1313] | 101 | if (update) |
---|
| 102 | this->updated(input, true); |
---|
[955] | 103 | } |
---|
| 104 | |
---|
[1313] | 105 | void InputBuffer::clear(bool update) |
---|
[955] | 106 | { |
---|
| 107 | this->buffer_ = ""; |
---|
[1313] | 108 | this->cursor_ = 0; |
---|
| 109 | |
---|
| 110 | if (update) |
---|
| 111 | this->updated(); |
---|
[955] | 112 | } |
---|
| 113 | |
---|
[1313] | 114 | void InputBuffer::removeBehindCursor(bool update) |
---|
[955] | 115 | { |
---|
[1313] | 116 | if (this->cursor_ > 0) |
---|
| 117 | { |
---|
| 118 | --this->cursor_; |
---|
| 119 | this->buffer_.erase(this->cursor_, 1); |
---|
| 120 | |
---|
| 121 | if (update) |
---|
| 122 | this->updated(); |
---|
| 123 | } |
---|
[955] | 124 | } |
---|
| 125 | |
---|
[1313] | 126 | void InputBuffer::removeAtCursor(bool update) |
---|
| 127 | { |
---|
| 128 | if (this->cursor_ < this->buffer_.size()) |
---|
| 129 | { |
---|
| 130 | this->buffer_.erase(this->cursor_, 1); |
---|
| 131 | |
---|
| 132 | if (update) |
---|
| 133 | this->updated(); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
[955] | 137 | void InputBuffer::updated() |
---|
| 138 | { |
---|
| 139 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
| 140 | { |
---|
| 141 | if ((*it).bListenToAllChanges_) |
---|
| 142 | (*(*it).listener_.*(*it).function_)(); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void InputBuffer::updated(const char& update, bool bSingleInput) |
---|
| 147 | { |
---|
| 148 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
| 149 | { |
---|
[1312] | 150 | if ((!(*it).trueKeyFalseChar_) && ((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput)) |
---|
[955] | 151 | (*(*it).listener_.*(*it).function_)(); |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | bool InputBuffer::charIsAllowed(const char& input) |
---|
| 156 | { |
---|
| 157 | if (this->allowedChars_ == "") |
---|
| 158 | return true; |
---|
| 159 | else |
---|
| 160 | return (this->allowedChars_.find(input) != std::string::npos); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | bool InputBuffer::keyPressed(const OIS::KeyEvent &e) |
---|
| 164 | { |
---|
[1334] | 165 | if (this->keyboard_->isModifierDown(OIS::Keyboard::Alt) && e.key == OIS::KC_TAB) |
---|
| 166 | return true; |
---|
| 167 | |
---|
[1312] | 168 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
[955] | 169 | { |
---|
[1312] | 170 | if ((*it).trueKeyFalseChar_ && ((*it).key_ == e.key)) |
---|
| 171 | (*(*it).listener_.*(*it).function_)(); |
---|
| 172 | } |
---|
[955] | 173 | |
---|
[966] | 174 | if (this->keyboard_->isModifierDown(OIS::Keyboard::Ctrl)) |
---|
| 175 | { |
---|
| 176 | if (e.key == OIS::KC_V) |
---|
| 177 | { |
---|
[1313] | 178 | this->insert(fromClipboard()); |
---|
[966] | 179 | return true; |
---|
| 180 | } |
---|
| 181 | else if (e.key == OIS::KC_C) |
---|
| 182 | { |
---|
| 183 | toClipboard(this->buffer_); |
---|
| 184 | return true; |
---|
| 185 | } |
---|
| 186 | else if (e.key == OIS::KC_X) |
---|
| 187 | { |
---|
| 188 | toClipboard(this->buffer_); |
---|
| 189 | this->clear(); |
---|
| 190 | return true; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | else if (this->keyboard_->isModifierDown(OIS::Keyboard::Shift)) |
---|
| 194 | { |
---|
| 195 | if (e.key == OIS::KC_INSERT) |
---|
| 196 | { |
---|
[1313] | 197 | this->insert(fromClipboard()); |
---|
[966] | 198 | return true; |
---|
| 199 | } |
---|
| 200 | else if (e.key == OIS::KC_DELETE) |
---|
| 201 | { |
---|
| 202 | toClipboard(this->buffer_); |
---|
| 203 | this->clear(); |
---|
| 204 | return true; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
[1313] | 208 | this->insert((char)e.text); |
---|
[955] | 209 | return true; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | bool InputBuffer::keyReleased(const OIS::KeyEvent &e) |
---|
| 213 | { |
---|
| 214 | return true; |
---|
| 215 | } |
---|
| 216 | } |
---|