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