Changeset 1495 for code/branches/network/src/core/InputBuffer.h
- Timestamp:
- Jun 1, 2008, 1:10:46 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/src/core/InputBuffer.h
r1494 r1495 1 /* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Fabian 'x3n' Landau * Co-authors: * Reto Grieder * */#ifndef _InputBuffer_H__#define _InputBuffer_H__#include "CorePrereqs.h"#include <string>#include <list>#include "InputInterfaces.h"#include "OrxonoxClass.h"namespace orxonox{ class BaseInputBufferListenerTuple { public: BaseInputBufferListenerTuple(bool bListenToAllChanges, bool bOnlySingleInput, bool trueKeyFalseChar, char _char, KeyCode::Enum key) : bListenToAllChanges_(bListenToAllChanges), bOnlySingleInput_(bOnlySingleInput), trueKeyFalseChar_(trueKeyFalseChar), char_(_char), key_(key) { } virtual ~BaseInputBufferListenerTuple() { } virtual void callFunction() = 0; bool bListenToAllChanges_; bool bOnlySingleInput_; bool trueKeyFalseChar_; char char_; KeyCode::Enum key_; }; template <class T> class InputBufferListenerTuple : public BaseInputBufferListenerTuple { public: InputBufferListenerTuple(T* listener, void (T::*function)(), bool bListenToAllChanges, bool bOnlySingleInput, bool trueKeyFalseChar, char _char, KeyCode::Enum key) : BaseInputBufferListenerTuple(bListenToAllChanges, bOnlySingleInput, trueKeyFalseChar, _char, key), listener_(listener), function_(function) { } 2 virtual ~InputBufferListenerTuple() { } void callFunction() { (listener_->*function_)(); } T* listener_; void (T::*function_)(); }; class _CoreExport InputBuffer : public KeyHandler, public OrxonoxClass { public: InputBuffer(); InputBuffer(const std::string allowedChars); void setConfigValues(); template <class T> void registerListener(T* listener, void (T::*function)(), bool bOnlySingleInput) { InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned); this->listeners_.insert(this->listeners_.end(), newTuple); } template <class T> void registerListener(T* listener, void (T::*function)() const, bool bOnlySingleInput) { InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned); this->listeners_.insert(this->listeners_.end(), newTuple); } template <class T> void registerListener(T* listener, void (T::*function)(), char _char, bool bOnlySingleInput) { InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned); this->listeners_.insert(this->listeners_.end(), newTuple); } template <class T> void registerListener(T* listener, void (T::*function)() const, char _char, bool bOnlySingleInput) { InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned); this->listeners_.insert(this->listeners_.end(), newTuple); } template <class T> void registerListener(T* listener, void (T::*function)(), KeyCode::Enum key) { InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, true, true, '\0', key); this->listeners_.insert(this->listeners_.end(), newTuple); } template <class T> void registerListener(T* listener, void (T::*function)() const, KeyCode::Enum key) { InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, true, true, '\0', key); this->listeners_.insert(this->listeners_.end(), newTuple); } template <class T> void unregisterListener(T* listener) { for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) { InputBufferListenerTuple<T>* refListener = dynamic_cast<InputBufferListenerTuple<T>*>(*it); if (refListener && refListener->listener_ == listener) this->listeners_.erase(it++); else it++; } } void set(const std::string& input, bool update = true); void insert(const std::string& input, bool update = true); void insert(const char& input, bool update = true); void clear(bool update = true); void removeAtCursor(bool update = true); void removeBehindCursor(bool update = true); void updated(); void updated(const char& update, bool bSingleInput); inline std::string get() const { return this->buffer_; } inline unsigned int getSize() const { return this->buffer_.size(); } inline unsigned int getCursorPosition() const { return this->cursor_; } inline void setCursorPosition(unsigned int cursor) { if (cursor <= this->buffer_.size()) { this->cursor_ = cursor; } } inline void setCursorToEnd() { this->cursor_ = this->buffer_.size(); } inline void setCursorToBegin() { this->cursor_ = 0; } inline void increaseCursor() { if (this->cursor_ < this->buffer_.size()) { ++this->cursor_; } } inline void decreaseCursor() { if (this->cursor_ > 0) { --this->cursor_; } } private: bool charIsAllowed(const char& input); void keyPressed (const KeyEvent& evt); void keyReleased(const KeyEvent& evt) { } void keyHeld (const KeyEvent& evt); void processKey (const KeyEvent &e); void tickInput(float dt, const HandlerState& state); std::string buffer_; std::list<BaseInputBufferListenerTuple*> listeners_; std::string allowedChars_; unsigned int cursor_; KeyCode::Enum lastKey_; float timeSinceKeyPressed_; float timeSinceKeyRepeated_; int keysToRepeat_; float keyRepeatDeleay_; float keyRepeatTime_; };}#endif /* _InputBuffer_H__ */ 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 BaseInputBufferListenerTuple 43 { 44 public: 45 BaseInputBufferListenerTuple(bool bListenToAllChanges, bool bOnlySingleInput, 46 bool trueKeyFalseChar, char _char, KeyCode::Enum key) 47 : bListenToAllChanges_(bListenToAllChanges), bOnlySingleInput_(bOnlySingleInput), 48 trueKeyFalseChar_(trueKeyFalseChar), char_(_char), key_(key) 49 { } 50 virtual ~BaseInputBufferListenerTuple() { } 51 virtual void callFunction() = 0; 52 bool bListenToAllChanges_; 53 bool bOnlySingleInput_; 54 bool trueKeyFalseChar_; 55 char char_; 56 KeyCode::Enum key_; 57 }; 58 59 template <class T> 60 class InputBufferListenerTuple : public BaseInputBufferListenerTuple 61 { 62 public: 63 InputBufferListenerTuple(T* listener, void (T::*function)(), bool bListenToAllChanges, 64 bool bOnlySingleInput, bool trueKeyFalseChar, char _char, KeyCode::Enum key) 65 : BaseInputBufferListenerTuple(bListenToAllChanges, bOnlySingleInput, trueKeyFalseChar, _char, key), 66 listener_(listener), function_(function) 67 { } 68 virtual ~InputBufferListenerTuple() { } 69 void callFunction() 70 { 71 (listener_->*function_)(); 72 } 73 T* listener_; 74 void (T::*function_)(); 75 }; 76 77 class _CoreExport InputBuffer : public KeyHandler, public OrxonoxClass 78 { 79 public: 80 InputBuffer(); 81 InputBuffer(const std::string allowedChars); 82 83 void setConfigValues(); 84 85 template <class T> 86 void registerListener(T* listener, void (T::*function)(), bool bOnlySingleInput) 87 { 88 InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned); 89 this->listeners_.insert(this->listeners_.end(), newTuple); 90 } 91 template <class T> 92 void registerListener(T* listener, void (T::*function)() const, bool bOnlySingleInput) 93 { 94 InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned); 95 this->listeners_.insert(this->listeners_.end(), newTuple); 96 } 97 template <class T> 98 void registerListener(T* listener, void (T::*function)(), char _char, bool bOnlySingleInput) 99 { 100 InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned); 101 this->listeners_.insert(this->listeners_.end(), newTuple); 102 } 103 template <class T> 104 void registerListener(T* listener, void (T::*function)() const, char _char, bool bOnlySingleInput) 105 { 106 InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned); 107 this->listeners_.insert(this->listeners_.end(), newTuple); 108 } 109 110 template <class T> 111 void registerListener(T* listener, void (T::*function)(), KeyCode::Enum key) 112 { 113 InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, true, true, '\0', key); 114 this->listeners_.insert(this->listeners_.end(), newTuple); 115 } 116 template <class T> 117 void registerListener(T* listener, void (T::*function)() const, KeyCode::Enum key) 118 { 119 InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, true, true, '\0', key); 120 this->listeners_.insert(this->listeners_.end(), newTuple); 121 } 122 123 template <class T> 124 void unregisterListener(T* listener) 125 { 126 for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) 127 { 128 InputBufferListenerTuple<T>* refListener = dynamic_cast<InputBufferListenerTuple<T>*>(*it); 129 if (refListener && refListener->listener_ == listener) 130 this->listeners_.erase(it++); 131 else 132 it++; 133 } 134 } 135 136 void set(const std::string& input, bool update = true); 137 void insert(const std::string& input, bool update = true); 138 void insert(const char& input, bool update = true); 139 void clear(bool update = true); 140 void removeAtCursor(bool update = true); 141 void removeBehindCursor(bool update = true); 142 143 void updated(); 144 void updated(const char& update, bool bSingleInput); 145 146 inline std::string get() const 147 { return this->buffer_; } 148 inline unsigned int getSize() const 149 { return this->buffer_.size(); } 150 151 inline unsigned int getCursorPosition() const 152 { return this->cursor_; } 153 inline void setCursorPosition(unsigned int cursor) 154 { if (cursor <= this->buffer_.size()) { this->cursor_ = cursor; } } 155 inline void setCursorToEnd() 156 { this->cursor_ = this->buffer_.size(); } 157 inline void setCursorToBegin() 158 { this->cursor_ = 0; } 159 inline void increaseCursor() 160 { if (this->cursor_ < this->buffer_.size()) { ++this->cursor_; } } 161 inline void decreaseCursor() 162 { if (this->cursor_ > 0) { --this->cursor_; } } 163 164 private: 165 bool charIsAllowed(const char& input); 166 167 void keyPressed (const KeyEvent& evt); 168 void keyReleased(const KeyEvent& evt) { } 169 void keyHeld (const KeyEvent& evt); 170 void processKey (const KeyEvent &e); 171 172 void tickInput(float dt, const HandlerState& state); 173 174 std::string buffer_; 175 std::list<BaseInputBufferListenerTuple*> listeners_; 176 std::string allowedChars_; 177 unsigned int cursor_; 178 179 KeyCode::Enum lastKey_; 180 float timeSinceKeyPressed_; 181 float timeSinceKeyRepeated_; 182 int keysToRepeat_; 183 184 float keyRepeatDeleay_; 185 float keyRepeatTime_; 186 }; 187 } 188 189 #endif /* _InputBuffer_H__ */
Note: See TracChangeset
for help on using the changeset viewer.