| 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 | // TODO: this is a major hack!!! |
|---|
| 69 | // Fix it properly |
|---|
| 70 | *((int*)(&newListener.listener_)) = (int)(listener); |
|---|
| 71 | |
|---|
| 72 | this->listeners_.insert(this->listeners_.end(), newListener); |
|---|
| 73 | } |
|---|
| 74 | template <class T> |
|---|
| 75 | void registerListener(T* listener, void (T::*function)() const, bool bOnlySingleInput) |
|---|
| 76 | { |
|---|
| 77 | struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned}; |
|---|
| 78 | *((int*)(&newListener.listener_)) = (int)(listener); |
|---|
| 79 | this->listeners_.insert(this->listeners_.end(), newListener); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | template <class T> |
|---|
| 83 | void registerListener(T* listener, void (T::*function)(), char _char, bool bOnlySingleInput) |
|---|
| 84 | { |
|---|
| 85 | struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned}; |
|---|
| 86 | *((int*)(&newListener.listener_)) = (int)(listener); |
|---|
| 87 | this->listeners_.insert(this->listeners_.end(), newListener); |
|---|
| 88 | } |
|---|
| 89 | template <class T> |
|---|
| 90 | void registerListener(T* listener, void (T::*function)() const, char _char, bool bOnlySingleInput) |
|---|
| 91 | { |
|---|
| 92 | struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned}; |
|---|
| 93 | *((int*)(&newListener.listener_)) = (int)(listener); |
|---|
| 94 | this->listeners_.insert(this->listeners_.end(), newListener); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | template <class T> |
|---|
| 98 | void registerListener(T* listener, void (T::*function)(), KeyCode::Enum key) |
|---|
| 99 | { |
|---|
| 100 | struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, false, true, true, '\0', key}; |
|---|
| 101 | *((int*)(&newListener.listener_)) = (int)(listener); |
|---|
| 102 | this->listeners_.insert(this->listeners_.end(), newListener); |
|---|
| 103 | } |
|---|
| 104 | template <class T> |
|---|
| 105 | void registerListener(T* listener, void (T::*function)() const, KeyCode::Enum key) |
|---|
| 106 | { |
|---|
| 107 | struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, false, true, true, '\0', key}; |
|---|
| 108 | *((int*)(&newListener.listener_)) = (int)(listener); |
|---|
| 109 | this->listeners_.insert(this->listeners_.end(), newListener); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void unregisterListener(InputBufferListener* listener); |
|---|
| 113 | |
|---|
| 114 | void set(const std::string& input, bool update = true); |
|---|
| 115 | void insert(const std::string& input, bool update = true); |
|---|
| 116 | void insert(const char& input, bool update = true); |
|---|
| 117 | void clear(bool update = true); |
|---|
| 118 | void removeAtCursor(bool update = true); |
|---|
| 119 | void removeBehindCursor(bool update = true); |
|---|
| 120 | |
|---|
| 121 | void updated(); |
|---|
| 122 | void updated(const char& update, bool bSingleInput); |
|---|
| 123 | |
|---|
| 124 | inline std::string get() const |
|---|
| 125 | { return this->buffer_; } |
|---|
| 126 | inline unsigned int getSize() const |
|---|
| 127 | { return this->buffer_.size(); } |
|---|
| 128 | |
|---|
| 129 | inline unsigned int getCursorPosition() const |
|---|
| 130 | { return this->cursor_; } |
|---|
| 131 | inline void setCursorPosition(unsigned int cursor) |
|---|
| 132 | { if (cursor <= this->buffer_.size()) { this->cursor_ = cursor; } } |
|---|
| 133 | inline void setCursorToEnd() |
|---|
| 134 | { this->cursor_ = this->buffer_.size(); } |
|---|
| 135 | inline void setCursorToBegin() |
|---|
| 136 | { this->cursor_ = 0; } |
|---|
| 137 | inline void increaseCursor() |
|---|
| 138 | { if (this->cursor_ < this->buffer_.size()) { ++this->cursor_; } } |
|---|
| 139 | inline void decreaseCursor() |
|---|
| 140 | { if (this->cursor_ > 0) { --this->cursor_; } } |
|---|
| 141 | |
|---|
| 142 | private: |
|---|
| 143 | bool charIsAllowed(const char& input); |
|---|
| 144 | |
|---|
| 145 | void keyPressed (const KeyEvent& evt); |
|---|
| 146 | void keyReleased(const KeyEvent& evt) { } |
|---|
| 147 | void keyHeld (const KeyEvent& evt); |
|---|
| 148 | void processKey (const KeyEvent &e); |
|---|
| 149 | |
|---|
| 150 | void tickInput(float dt, const HandlerState& state); |
|---|
| 151 | |
|---|
| 152 | std::string buffer_; |
|---|
| 153 | std::list<InputBufferListenerTuple> listeners_; |
|---|
| 154 | std::string allowedChars_; |
|---|
| 155 | unsigned int cursor_; |
|---|
| 156 | |
|---|
| 157 | KeyCode::Enum lastKey_; |
|---|
| 158 | float timeSinceKeyPressed_; |
|---|
| 159 | float timeSinceKeyRepeated_; |
|---|
| 160 | int keysToRepeat_; |
|---|
| 161 | |
|---|
| 162 | float keyRepeatDeleay_; |
|---|
| 163 | float keyRepeatTime_; |
|---|
| 164 | }; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | #endif /* _InputBuffer_H__ */ |
|---|