| 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 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "InputBuffer.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include <iostream> | 
|---|
| 32 |  | 
|---|
| 33 | #include "util/Clipboard.h" | 
|---|
| 34 | #include "InputManager.h" | 
|---|
| 35 |  | 
|---|
| 36 | namespace orxonox | 
|---|
| 37 | { | 
|---|
| 38 | InputBuffer::InputBuffer() | 
|---|
| 39 | { | 
|---|
| 40 | //this->bActivated_ = false; | 
|---|
| 41 | this->allowedChars_ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜ0123456789 \\\"(){}[]<>.:,;_-+*/=!?|$&%^"; | 
|---|
| 42 | this->keyboard_ = InputManager::getKeyboard(); | 
|---|
| 43 | this->buffer_ = ""; | 
|---|
| 44 |  | 
|---|
| 45 | //this->keyboard_->setEventCallback(this); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | InputBuffer::InputBuffer(const std::string allowedChars) | 
|---|
| 49 | { | 
|---|
| 50 | //this->bActivated_ = false; | 
|---|
| 51 | this->allowedChars_ = allowedChars; | 
|---|
| 52 | this->keyboard_ = InputManager::getKeyboard(); | 
|---|
| 53 | this->buffer_ = ""; | 
|---|
| 54 | } | 
|---|
| 55 | /* | 
|---|
| 56 | void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), bool bOnlySingleInput) | 
|---|
| 57 | { | 
|---|
| 58 | struct InputBufferListenerTuple newListener = {listener, function, true, bOnlySingleInput, ' '}; | 
|---|
| 59 | this->listeners_.insert(this->listeners_.end(), newListener); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), char char_, bool bOnlySingleInput) | 
|---|
| 63 | { | 
|---|
| 64 | struct InputBufferListenerTuple newListener = {listener, function, false, bOnlySingleInput, char_}; | 
|---|
| 65 | this->listeners_.insert(this->listeners_.end(), newListener); | 
|---|
| 66 | } | 
|---|
| 67 | */ | 
|---|
| 68 | void InputBuffer::set(const std::string& input) | 
|---|
| 69 | { | 
|---|
| 70 | this->buffer_ = ""; | 
|---|
| 71 | this->append(input); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | void InputBuffer::append(const std::string& input) | 
|---|
| 75 | { | 
|---|
| 76 | for (unsigned int i = 0; i < input.size(); i++) | 
|---|
| 77 | { | 
|---|
| 78 | if (this->charIsAllowed(input[i])) | 
|---|
| 79 | this->buffer_ += input[i]; | 
|---|
| 80 |  | 
|---|
| 81 | this->updated(input[i], false); | 
|---|
| 82 | } | 
|---|
| 83 | this->updated(); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | void InputBuffer::append(const char& input) | 
|---|
| 87 | { | 
|---|
| 88 | if (this->charIsAllowed(input)) | 
|---|
| 89 | this->buffer_ += input; | 
|---|
| 90 |  | 
|---|
| 91 | this->updated(input, true); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | void InputBuffer::clear() | 
|---|
| 95 | { | 
|---|
| 96 | this->buffer_ = ""; | 
|---|
| 97 | this->updated(); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | void InputBuffer::removeLast() | 
|---|
| 101 | { | 
|---|
| 102 | this->buffer_ = this->buffer_.substr(0, this->buffer_.size() - 1); | 
|---|
| 103 | this->updated(); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void InputBuffer::updated() | 
|---|
| 107 | { | 
|---|
| 108 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) | 
|---|
| 109 | { | 
|---|
| 110 | if ((*it).bListenToAllChanges_) | 
|---|
| 111 | (*(*it).listener_.*(*it).function_)(); | 
|---|
| 112 | } | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | void InputBuffer::updated(const char& update, bool bSingleInput) | 
|---|
| 116 | { | 
|---|
| 117 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) | 
|---|
| 118 | { | 
|---|
| 119 | if (((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput)) | 
|---|
| 120 | (*(*it).listener_.*(*it).function_)(); | 
|---|
| 121 | } | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | /*void InputBuffer::activityChanged() const | 
|---|
| 125 | { | 
|---|
| 126 | }*/ | 
|---|
| 127 |  | 
|---|
| 128 | bool InputBuffer::charIsAllowed(const char& input) | 
|---|
| 129 | { | 
|---|
| 130 | if (this->allowedChars_ == "") | 
|---|
| 131 | return true; | 
|---|
| 132 | else | 
|---|
| 133 | return (this->allowedChars_.find(input) != std::string::npos); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | bool InputBuffer::keyPressed(const OIS::KeyEvent &e) | 
|---|
| 137 | { | 
|---|
| 138 | /*if (e.key == OIS::KC_NUMPADENTER) | 
|---|
| 139 | { | 
|---|
| 140 | this->setActivated(!this->isActivated()); | 
|---|
| 141 | this->clear(); | 
|---|
| 142 | return true; | 
|---|
| 143 | }*/ | 
|---|
| 144 |  | 
|---|
| 145 | if (this->keyboard_->isModifierDown(OIS::Keyboard::Ctrl)) | 
|---|
| 146 | { | 
|---|
| 147 | if (e.key == OIS::KC_V) | 
|---|
| 148 | { | 
|---|
| 149 | this->append(fromClipboard()); | 
|---|
| 150 | return true; | 
|---|
| 151 | } | 
|---|
| 152 | else if (e.key == OIS::KC_C) | 
|---|
| 153 | { | 
|---|
| 154 | toClipboard(this->buffer_); | 
|---|
| 155 | return true; | 
|---|
| 156 | } | 
|---|
| 157 | else if (e.key == OIS::KC_X) | 
|---|
| 158 | { | 
|---|
| 159 | toClipboard(this->buffer_); | 
|---|
| 160 | this->clear(); | 
|---|
| 161 | return true; | 
|---|
| 162 | } | 
|---|
| 163 | } | 
|---|
| 164 | else if (this->keyboard_->isModifierDown(OIS::Keyboard::Shift)) | 
|---|
| 165 | { | 
|---|
| 166 | if (e.key == OIS::KC_INSERT) | 
|---|
| 167 | { | 
|---|
| 168 | this->append(fromClipboard()); | 
|---|
| 169 | return true; | 
|---|
| 170 | } | 
|---|
| 171 | else if (e.key == OIS::KC_DELETE) | 
|---|
| 172 | { | 
|---|
| 173 | toClipboard(this->buffer_); | 
|---|
| 174 | this->clear(); | 
|---|
| 175 | return true; | 
|---|
| 176 | } | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | //std::cout << this->keyboard_->getAsString(e.key) << " / " << (char)e.text << std::endl; | 
|---|
| 180 |  | 
|---|
| 181 | std::string input = this->keyboard_->getAsString(e.key); | 
|---|
| 182 | /*if (input.size() >= 1) | 
|---|
| 183 | this->append(input[0]);*/ | 
|---|
| 184 |  | 
|---|
| 185 | this->append((char)e.text); | 
|---|
| 186 | return true; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | bool InputBuffer::keyReleased(const OIS::KeyEvent &e) | 
|---|
| 190 | { | 
|---|
| 191 | return true; | 
|---|
| 192 | } | 
|---|
| 193 | } | 
|---|