Orxonox  0.0.5 Codename: Arcturus
InputBuffer.h
Go to the documentation of this file.
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 "InputPrereqs.h"
33 
34 #include <list>
35 #include <string>
37 #include "InputHandler.h"
38 
39 namespace orxonox
40 {
42  {
43  public:
44  BaseInputBufferListenerTuple(bool bListenToAllChanges, bool bOnlySingleInput,
45  bool trueKeyFalseChar, char _char, KeyCode::ByEnum key)
46  : bListenToAllChanges_(bListenToAllChanges), bOnlySingleInput_(bOnlySingleInput),
47  trueKeyFalseChar_(trueKeyFalseChar), char_(_char), key_(key)
48  { }
49  virtual ~BaseInputBufferListenerTuple() = default;
50  virtual void callFunction() = 0;
54  char char_;
56  };
57 
58  template <class T>
60  {
61  public:
62  InputBufferListenerTuple(T* listener, void (T::*function)(), bool bListenToAllChanges,
63  bool bOnlySingleInput, bool trueKeyFalseChar, char _char, KeyCode::ByEnum key)
64  : BaseInputBufferListenerTuple(bListenToAllChanges, bOnlySingleInput, trueKeyFalseChar, _char, key),
65  listener_(listener), function_(function)
66  { }
67  virtual ~InputBufferListenerTuple() = default;
68  virtual void callFunction() override
69  {
70  (listener_->*function_)();
71  }
73  void (T::*function_)();
74  };
75 
77  {
78  public:
79  InputBuffer();
80  ~InputBuffer();
81  InputBuffer(const std::string& allowedChars);
82 
83  void setConfigValues();
84 
85  unsigned int getMaxLength() const { return this->maxLength_; }
86  void setMaxLength(unsigned int length);
87 
88  template <class T>
89  void registerListener(T* listener, void (T::*function)(), bool bOnlySingleInput)
90  {
91  InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned);
92  this->listeners_.insert(this->listeners_.end(), newTuple);
93  }
94  template <class T>
95  void registerListener(T* listener, void (T::*function)() const, bool bOnlySingleInput)
96  {
97  InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned);
98  this->listeners_.insert(this->listeners_.end(), newTuple);
99  }
100  template <class T>
101  void registerListener(T* listener, void (T::*function)(), char _char, bool bOnlySingleInput)
102  {
103  InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned);
104  this->listeners_.insert(this->listeners_.end(), newTuple);
105  }
106  template <class T>
107  void registerListener(T* listener, void (T::*function)() const, char _char, bool bOnlySingleInput)
108  {
109  InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned);
110  this->listeners_.insert(this->listeners_.end(), newTuple);
111  }
112 
113  template <class T>
114  void registerListener(T* listener, void (T::*function)(), KeyCode::ByEnum key)
115  {
116  InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, true, true, '\0', key);
117  this->listeners_.insert(this->listeners_.end(), newTuple);
118  }
119  template <class T>
120  void registerListener(T* listener, void (T::*function)() const, KeyCode::ByEnum key)
121  {
122  InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, true, true, '\0', key);
123  this->listeners_.insert(this->listeners_.end(), newTuple);
124  }
125 
126  template <class T>
127  void unregisterListener(T* listener)
128  {
129  for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
130  {
131  InputBufferListenerTuple<T>* refListener = static_cast<InputBufferListenerTuple<T>*>(*it);
132  if (refListener && refListener->listener_ == listener)
133  this->listeners_.erase(it++);
134  else
135  it++;
136  }
137  }
138 
139  void set(const std::string& input, bool update = true);
140  void insert(const std::string& input, bool update = true);
141  void insert(const char& input, bool update = true);
142  void clear(bool update = true);
143  void removeAtCursor(bool update = true);
144  void removeBehindCursor(bool update = true);
145 
146  void updated();
147  void updated(const char& update, bool bSingleInput);
148 
149  inline const std::string& get() const
150  { return this->buffer_; }
151  inline unsigned int getSize() const
152  { return this->buffer_.size(); }
153 
154  inline unsigned int getCursorPosition() const
155  { return this->cursor_; }
156  inline void setCursorPosition(unsigned int cursor)
157  { if (cursor <= this->buffer_.size()) { this->cursor_ = cursor; } }
158  inline void setCursorToEnd()
159  { this->cursor_ = this->buffer_.size(); }
160  inline void setCursorToBegin()
161  { this->cursor_ = 0; }
162  inline void increaseCursor()
163  { if (this->cursor_ < this->buffer_.size()) { ++this->cursor_; } }
164  inline void decreaseCursor()
165  { if (this->cursor_ > 0) { --this->cursor_; } }
166 
167  virtual void buttonPressed(const KeyEvent& evt) override;
168 
169  private:
170  bool charIsAllowed(const char& input);
171 
172  virtual void buttonHeld (const KeyEvent& evt) override;
173  void processKey (const KeyEvent& evt);
174 
175  virtual void keyboardUpdated(float dt) override;
176 
178  std::list<BaseInputBufferListenerTuple*> listeners_;
180  unsigned int maxLength_;
181  unsigned int cursor_;
182 
187 
190  };
191 }
192 
193 #endif /* _InputBuffer_H__ */
void registerListener(T *listener, void(T::*function)() const, char _char, bool bOnlySingleInput)
Definition: InputBuffer.h:107
Definition: InputBuffer.h:59
Base class for all input handlers like KeyBinder, InputBuffer, etc.
Definition: InputHandler.h:118
void registerListener(T *listener, void(T::*function)() const, bool bOnlySingleInput)
Definition: InputBuffer.h:95
void unregisterListener(T *listener)
Definition: InputBuffer.h:127
int keysToRepeat_
Definition: InputBuffer.h:186
float keyRepeatDeleay_
Definition: InputBuffer.h:188
char char_
Definition: InputBuffer.h:54
void setCursorPosition(unsigned int cursor)
Definition: InputBuffer.h:156
::std::string string
Definition: gtest-port.h:756
bool bListenToAllChanges_
Definition: InputBuffer.h:51
KeyCode::ByEnum lastKey_
Definition: InputBuffer.h:183
unsigned int getSize() const
Definition: InputBuffer.h:151
This is the base class of all objects which may contain config values.
Definition: Configurable.h:47
typedef void(ENET_CALLBACK *ENetPacketFreeCallback)(struct _ENetPacket *)
InputBufferListenerTuple(T *listener, void(T::*function)(), bool bListenToAllChanges, bool bOnlySingleInput, bool trueKeyFalseChar, char _char, KeyCode::ByEnum key)
Definition: InputBuffer.h:62
void setCursorToEnd()
Definition: InputBuffer.h:158
unsigned int getMaxLength() const
Definition: InputBuffer.h:85
bool bOnlySingleInput_
Definition: InputBuffer.h:52
std::list< BaseInputBufferListenerTuple * > listeners_
Definition: InputBuffer.h:178
Declarations of all key/button/axis code enumeration and string literals and an input device enumerat...
T * listener_
Definition: InputBuffer.h:72
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
#define _CoreExport
Definition: CorePrereqs.h:61
BaseInputBufferListenerTuple(bool bListenToAllChanges, bool bOnlySingleInput, bool trueKeyFalseChar, char _char, KeyCode::ByEnum key)
Definition: InputBuffer.h:44
bool trueKeyFalseChar_
Definition: InputBuffer.h:53
void setCursorToBegin()
Definition: InputBuffer.h:160
Event argument for key events.
Definition: InputHandler.h:76
Definition: InputPrereqs.h:78
Declaration of Configurable, the base class of all objects which may contain config values...
std::string buffer_
Definition: InputBuffer.h:177
unsigned int cursor_
Definition: InputBuffer.h:181
float keyRepeatTime_
Definition: InputBuffer.h:189
KeyCode::ByEnum key_
Definition: InputBuffer.h:55
unsigned int maxLength_
Definition: InputBuffer.h:180
virtual void callFunction() override
Definition: InputBuffer.h:68
unsigned int getCursorPosition() const
Definition: InputBuffer.h:154
float timeSinceKeyRepeated_
Definition: InputBuffer.h:185
void increaseCursor()
Definition: InputBuffer.h:162
void registerListener(T *listener, void(T::*function)() const, KeyCode::ByEnum key)
Definition: InputBuffer.h:120
ByEnum
Key codes as enumeration.
Definition: InputPrereqs.h:56
Definition: InputPrereqs.h:58
void decreaseCursor()
Definition: InputBuffer.h:164
Definition: InputBuffer.h:76
float timeSinceKeyPressed_
Definition: InputBuffer.h:184
Definition: InputBuffer.h:41
void registerListener(T *listener, void(T::*function)(), bool bOnlySingleInput)
Definition: InputBuffer.h:89
std::string allowedChars_
Definition: InputBuffer.h:179
void registerListener(T *listener, void(T::*function)(), char _char, bool bOnlySingleInput)
Definition: InputBuffer.h:101
virtual ~BaseInputBufferListenerTuple()=default
void registerListener(T *listener, void(T::*function)(), KeyCode::ByEnum key)
Definition: InputBuffer.h:114