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