[955] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Fabian 'x3n' Landau |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | #include <iostream> |
---|
| 29 | |
---|
| 30 | #include "InputBuffer.h" |
---|
| 31 | |
---|
| 32 | namespace orxonox |
---|
| 33 | { |
---|
| 34 | InputBuffer::InputBuffer(OIS::Keyboard* keyboard) |
---|
| 35 | { |
---|
| 36 | this->bActivated_ = false; |
---|
| 37 | this->allowedChars_ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜ0123456789 \"().:,;_-+*/=!?"; |
---|
| 38 | this->keyboard_ = keyboard; |
---|
| 39 | this->buffer_ = ""; |
---|
| 40 | |
---|
| 41 | this->keyboard_->setEventCallback(this); |
---|
| 42 | } |
---|
| 43 | /* |
---|
| 44 | void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), bool bOnlySingleInput) |
---|
| 45 | { |
---|
| 46 | struct InputBufferListenerTuple newListener = {listener, function, true, bOnlySingleInput, ' '}; |
---|
| 47 | this->listeners_.insert(this->listeners_.end(), newListener); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), char char_, bool bOnlySingleInput) |
---|
| 51 | { |
---|
| 52 | struct InputBufferListenerTuple newListener = {listener, function, false, bOnlySingleInput, char_}; |
---|
| 53 | this->listeners_.insert(this->listeners_.end(), newListener); |
---|
| 54 | } |
---|
| 55 | */ |
---|
| 56 | void InputBuffer::set(const std::string& input) |
---|
| 57 | { |
---|
| 58 | this->clear(); |
---|
| 59 | this->append(input); |
---|
| 60 | this->updated(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void InputBuffer::append(const std::string& input) |
---|
| 64 | { |
---|
| 65 | for (unsigned int i = 0; i < input.size(); i++) |
---|
| 66 | { |
---|
| 67 | if (this->charIsAllowed(input[i])) |
---|
| 68 | this->buffer_ += input[i]; |
---|
| 69 | |
---|
| 70 | this->updated(input[i], false); |
---|
| 71 | } |
---|
| 72 | this->updated(); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | void InputBuffer::append(const char& input) |
---|
| 76 | { |
---|
| 77 | if (this->charIsAllowed(input)) |
---|
| 78 | this->buffer_ += input; |
---|
| 79 | |
---|
| 80 | this->updated(input, true); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | void InputBuffer::clear() |
---|
| 84 | { |
---|
| 85 | this->buffer_ = ""; |
---|
| 86 | this->updated(); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void InputBuffer::removeLast() |
---|
| 90 | { |
---|
| 91 | this->buffer_ = this->buffer_.substr(0, this->buffer_.size() - 1); |
---|
| 92 | this->updated(); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void InputBuffer::updated() |
---|
| 96 | { |
---|
| 97 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
| 98 | { |
---|
| 99 | if ((*it).bListenToAllChanges_) |
---|
| 100 | (*(*it).listener_.*(*it).function_)(); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void InputBuffer::updated(const char& update, bool bSingleInput) |
---|
| 105 | { |
---|
| 106 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
| 107 | { |
---|
| 108 | if (((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput)) |
---|
| 109 | (*(*it).listener_.*(*it).function_)(); |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | void InputBuffer::activityChanged() const |
---|
| 114 | { |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | bool InputBuffer::charIsAllowed(const char& input) |
---|
| 118 | { |
---|
| 119 | if (this->allowedChars_ == "") |
---|
| 120 | return true; |
---|
| 121 | else |
---|
| 122 | return (this->allowedChars_.find(input) != std::string::npos); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | bool InputBuffer::keyPressed(const OIS::KeyEvent &e) |
---|
| 126 | { |
---|
| 127 | if (e.key == OIS::KC_NUMPADENTER) |
---|
| 128 | { |
---|
| 129 | this->setActivated(!this->isActivated()); |
---|
| 130 | this->clear(); |
---|
| 131 | return true; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | if (this->bActivated_) |
---|
| 135 | { |
---|
| 136 | //std::cout << this->keyboard_->getAsString(e.key) << " / " << (char)e.text << std::endl; |
---|
| 137 | /* |
---|
| 138 | std::string input = this->keyboard_->getAsString(e.key); |
---|
| 139 | if (input.size() >= 1) |
---|
| 140 | this->append(input[0]); |
---|
| 141 | */ |
---|
| 142 | this->append((char)e.text); |
---|
| 143 | } |
---|
| 144 | return true; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | bool InputBuffer::keyReleased(const OIS::KeyEvent &e) |
---|
| 148 | { |
---|
| 149 | return true; |
---|
| 150 | } |
---|
| 151 | } |
---|