[1446] | 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 | * Reto Grieder |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #ifndef _InputBuffer_H__ |
---|
| 30 | #define _InputBuffer_H__ |
---|
| 31 | |
---|
| 32 | #include "CorePrereqs.h" |
---|
| 33 | |
---|
| 34 | #include <string> |
---|
| 35 | #include <list> |
---|
| 36 | |
---|
| 37 | #include "InputInterfaces.h" |
---|
| 38 | #include "OrxonoxClass.h" |
---|
| 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | class _CoreExport InputBufferListener |
---|
| 43 | {}; |
---|
| 44 | |
---|
| 45 | class _CoreExport InputBuffer : public KeyHandler, public OrxonoxClass |
---|
| 46 | { |
---|
| 47 | struct InputBufferListenerTuple |
---|
| 48 | { |
---|
| 49 | InputBufferListener* listener_; |
---|
| 50 | void (InputBufferListener::*function_)(); |
---|
| 51 | bool bListenToAllChanges_; |
---|
| 52 | bool bOnlySingleInput_; |
---|
| 53 | bool trueKeyFalseChar_; |
---|
| 54 | char char_; |
---|
| 55 | KeyCode::Enum key_; |
---|
| 56 | }; |
---|
| 57 | |
---|
| 58 | public: |
---|
| 59 | InputBuffer(); |
---|
| 60 | InputBuffer(const std::string allowedChars); |
---|
| 61 | |
---|
| 62 | void setConfigValues(); |
---|
| 63 | |
---|
| 64 | template <class T> |
---|
| 65 | void registerListener(T* listener, void (T::*function)(), bool bOnlySingleInput) |
---|
| 66 | { |
---|
| 67 | struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned}; |
---|
| 68 | this->listeners_.insert(this->listeners_.end(), newListener); |
---|
| 69 | } |
---|
| 70 | template <class T> |
---|
| 71 | void registerListener(T* listener, void (T::*function)() const, bool bOnlySingleInput) |
---|
| 72 | { |
---|
| 73 | struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned}; |
---|
| 74 | this->listeners_.insert(this->listeners_.end(), newListener); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | template <class T> |
---|
| 78 | void registerListener(T* listener, void (T::*function)(), char _char, bool bOnlySingleInput) |
---|
| 79 | { |
---|
| 80 | struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned}; |
---|
| 81 | this->listeners_.insert(this->listeners_.end(), newListener); |
---|
| 82 | } |
---|
| 83 | template <class T> |
---|
| 84 | void registerListener(T* listener, void (T::*function)() const, char _char, bool bOnlySingleInput) |
---|
| 85 | { |
---|
| 86 | struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned}; |
---|
| 87 | this->listeners_.insert(this->listeners_.end(), newListener); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | template <class T> |
---|
| 91 | void registerListener(T* listener, void (T::*function)(), KeyCode::Enum key) |
---|
| 92 | { |
---|
| 93 | struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, false, true, true, '\0', key}; |
---|
| 94 | this->listeners_.insert(this->listeners_.end(), newListener); |
---|
| 95 | } |
---|
| 96 | template <class T> |
---|
| 97 | void registerListener(T* listener, void (T::*function)() const, KeyCode::Enum key) |
---|
| 98 | { |
---|
| 99 | struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, false, true, true, '\0', key}; |
---|
| 100 | this->listeners_.insert(this->listeners_.end(), newListener); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void unregisterListener(InputBufferListener* listener); |
---|
| 104 | |
---|
| 105 | void set(const std::string& input, bool update = true); |
---|
| 106 | void insert(const std::string& input, bool update = true); |
---|
| 107 | void insert(const char& input, bool update = true); |
---|
| 108 | void clear(bool update = true); |
---|
| 109 | void removeAtCursor(bool update = true); |
---|
| 110 | void removeBehindCursor(bool update = true); |
---|
| 111 | |
---|
| 112 | void updated(); |
---|
| 113 | void updated(const char& update, bool bSingleInput); |
---|
| 114 | |
---|
| 115 | inline std::string get() const |
---|
| 116 | { return this->buffer_; } |
---|
| 117 | inline unsigned int getSize() const |
---|
| 118 | { return this->buffer_.size(); } |
---|
| 119 | |
---|
| 120 | inline unsigned int getCursorPosition() const |
---|
| 121 | { return this->cursor_; } |
---|
| 122 | inline void setCursorPosition(unsigned int cursor) |
---|
| 123 | { if (cursor <= this->buffer_.size()) { this->cursor_ = cursor; } } |
---|
| 124 | inline void setCursorToEnd() |
---|
| 125 | { this->cursor_ = this->buffer_.size(); } |
---|
| 126 | inline void setCursorToBegin() |
---|
| 127 | { this->cursor_ = 0; } |
---|
| 128 | inline void increaseCursor() |
---|
| 129 | { if (this->cursor_ < this->buffer_.size()) { ++this->cursor_; } } |
---|
| 130 | inline void decreaseCursor() |
---|
| 131 | { if (this->cursor_ > 0) { --this->cursor_; } } |
---|
| 132 | |
---|
| 133 | private: |
---|
| 134 | bool charIsAllowed(const char& input); |
---|
| 135 | |
---|
| 136 | void keyPressed (const KeyEvent& evt); |
---|
| 137 | void keyReleased(const KeyEvent& evt) { } |
---|
| 138 | void keyHeld (const KeyEvent& evt); |
---|
| 139 | void processKey (const KeyEvent &e); |
---|
| 140 | |
---|
| 141 | void tickInput(float dt, const HandlerState& state); |
---|
| 142 | |
---|
| 143 | std::string buffer_; |
---|
| 144 | std::list<InputBufferListenerTuple> listeners_; |
---|
| 145 | std::string allowedChars_; |
---|
| 146 | unsigned int cursor_; |
---|
| 147 | |
---|
| 148 | KeyCode::Enum lastKey_; |
---|
| 149 | float timeSinceKeyPressed_; |
---|
| 150 | float timeSinceKeyRepeated_; |
---|
| 151 | int keysToRepeat_; |
---|
| 152 | |
---|
| 153 | float keyRepeatDeleay_; |
---|
| 154 | float keyRepeatTime_; |
---|
| 155 | }; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | #endif /* _InputBuffer_H__ */ |
---|