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 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "InputBuffer.h" |
---|
30 | |
---|
31 | #include <iostream> |
---|
32 | |
---|
33 | #include "util/Clipboard.h" |
---|
34 | #include "InputManager.h" |
---|
35 | |
---|
36 | namespace orxonox |
---|
37 | { |
---|
38 | InputBuffer::InputBuffer() |
---|
39 | { |
---|
40 | this->allowedChars_ = "abcdefghijklmnopqrstuvwxyz \ |
---|
41 | ABCDEFGHIJKLMNOPQRSTUVWXYZ \ |
---|
42 | äëïöüÄËÏÖÜáâàéêèíîìóôòúûù \ |
---|
43 | 0123456789 \ |
---|
44 | \\\"(){}[]<>.:,;_-+*/=!?|$&%^~#"; |
---|
45 | this->keyboard_ = InputManager::getSingleton().getKeyboard(); |
---|
46 | this->buffer_ = ""; |
---|
47 | this->cursor_ = 0; |
---|
48 | } |
---|
49 | |
---|
50 | InputBuffer::InputBuffer(const std::string allowedChars) |
---|
51 | { |
---|
52 | this->allowedChars_ = allowedChars; |
---|
53 | this->keyboard_ = InputManager::getSingleton().getKeyboard(); |
---|
54 | this->buffer_ = ""; |
---|
55 | this->cursor_ = 0; |
---|
56 | } |
---|
57 | |
---|
58 | void InputBuffer::unregisterListener(InputBufferListener* listener) |
---|
59 | { |
---|
60 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) |
---|
61 | { |
---|
62 | if ((*it).listener_ == listener) |
---|
63 | this->listeners_.erase(it++); |
---|
64 | else |
---|
65 | ++it; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | void InputBuffer::set(const std::string& input, bool update) |
---|
70 | { |
---|
71 | this->clear(false); |
---|
72 | this->insert(input, update); |
---|
73 | } |
---|
74 | |
---|
75 | void InputBuffer::insert(const std::string& input, bool update) |
---|
76 | { |
---|
77 | for (unsigned int i = 0; i < input.size(); ++i) |
---|
78 | { |
---|
79 | this->insert(input[i], false); |
---|
80 | |
---|
81 | if (update) |
---|
82 | this->updated(input[i], false); |
---|
83 | } |
---|
84 | |
---|
85 | if (update) |
---|
86 | this->updated(); |
---|
87 | } |
---|
88 | |
---|
89 | void InputBuffer::insert(const char& input, bool update) |
---|
90 | { |
---|
91 | if (this->charIsAllowed(input)) |
---|
92 | { |
---|
93 | this->buffer_.insert(this->cursor_, 1, input); |
---|
94 | ++this->cursor_; |
---|
95 | } |
---|
96 | |
---|
97 | if (update) |
---|
98 | this->updated(input, true); |
---|
99 | } |
---|
100 | |
---|
101 | void InputBuffer::clear(bool update) |
---|
102 | { |
---|
103 | this->buffer_ = ""; |
---|
104 | this->cursor_ = 0; |
---|
105 | |
---|
106 | if (update) |
---|
107 | this->updated(); |
---|
108 | } |
---|
109 | |
---|
110 | void InputBuffer::removeBehindCursor(bool update) |
---|
111 | { |
---|
112 | if (this->cursor_ > 0) |
---|
113 | { |
---|
114 | --this->cursor_; |
---|
115 | this->buffer_.erase(this->cursor_, 1); |
---|
116 | |
---|
117 | if (update) |
---|
118 | this->updated(); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | void InputBuffer::removeAtCursor(bool update) |
---|
123 | { |
---|
124 | if (this->cursor_ < this->buffer_.size()) |
---|
125 | { |
---|
126 | this->buffer_.erase(this->cursor_, 1); |
---|
127 | |
---|
128 | if (update) |
---|
129 | this->updated(); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | void InputBuffer::updated() |
---|
134 | { |
---|
135 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
136 | { |
---|
137 | if ((*it).bListenToAllChanges_) |
---|
138 | (*(*it).listener_.*(*it).function_)(); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | void InputBuffer::updated(const char& update, bool bSingleInput) |
---|
143 | { |
---|
144 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
145 | { |
---|
146 | if ((!(*it).trueKeyFalseChar_) && ((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput)) |
---|
147 | (*(*it).listener_.*(*it).function_)(); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | bool InputBuffer::charIsAllowed(const char& input) |
---|
152 | { |
---|
153 | if (this->allowedChars_ == "") |
---|
154 | return true; |
---|
155 | else |
---|
156 | return (this->allowedChars_.find(input) != std::string::npos); |
---|
157 | } |
---|
158 | |
---|
159 | bool InputBuffer::keyPressed(const OIS::KeyEvent &e) |
---|
160 | { |
---|
161 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
162 | { |
---|
163 | if ((*it).trueKeyFalseChar_ && ((*it).key_ == e.key)) |
---|
164 | (*(*it).listener_.*(*it).function_)(); |
---|
165 | } |
---|
166 | |
---|
167 | if (this->keyboard_->isModifierDown(OIS::Keyboard::Ctrl)) |
---|
168 | { |
---|
169 | if (e.key == OIS::KC_V) |
---|
170 | { |
---|
171 | this->insert(fromClipboard()); |
---|
172 | return true; |
---|
173 | } |
---|
174 | else if (e.key == OIS::KC_C) |
---|
175 | { |
---|
176 | toClipboard(this->buffer_); |
---|
177 | return true; |
---|
178 | } |
---|
179 | else if (e.key == OIS::KC_X) |
---|
180 | { |
---|
181 | toClipboard(this->buffer_); |
---|
182 | this->clear(); |
---|
183 | return true; |
---|
184 | } |
---|
185 | } |
---|
186 | else if (this->keyboard_->isModifierDown(OIS::Keyboard::Shift)) |
---|
187 | { |
---|
188 | if (e.key == OIS::KC_INSERT) |
---|
189 | { |
---|
190 | this->insert(fromClipboard()); |
---|
191 | return true; |
---|
192 | } |
---|
193 | else if (e.key == OIS::KC_DELETE) |
---|
194 | { |
---|
195 | toClipboard(this->buffer_); |
---|
196 | this->clear(); |
---|
197 | return true; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | this->insert((char)e.text); |
---|
202 | return true; |
---|
203 | } |
---|
204 | |
---|
205 | bool InputBuffer::keyReleased(const OIS::KeyEvent &e) |
---|
206 | { |
---|
207 | return true; |
---|
208 | } |
---|
209 | } |
---|