1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Fabian 'x3n' Landau |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include <iostream> |
---|
29 | |
---|
30 | #include "InputBuffer.h" |
---|
31 | #include "InputManager.h" |
---|
32 | #include "util/Clipboard.h" |
---|
33 | |
---|
34 | namespace orxonox |
---|
35 | { |
---|
36 | InputBuffer::InputBuffer() |
---|
37 | { |
---|
38 | this->bActivated_ = false; |
---|
39 | this->allowedChars_ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜ0123456789 \\\"().:,;_-+*/=!?<>[|]"; |
---|
40 | this->keyboard_ = InputManager::getSingleton().getKeyboard(); |
---|
41 | this->buffer_ = ""; |
---|
42 | |
---|
43 | this->keyboard_->setEventCallback(this); |
---|
44 | } |
---|
45 | /* |
---|
46 | void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), bool bOnlySingleInput) |
---|
47 | { |
---|
48 | struct InputBufferListenerTuple newListener = {listener, function, true, bOnlySingleInput, ' '}; |
---|
49 | this->listeners_.insert(this->listeners_.end(), newListener); |
---|
50 | } |
---|
51 | |
---|
52 | void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), char char_, bool bOnlySingleInput) |
---|
53 | { |
---|
54 | struct InputBufferListenerTuple newListener = {listener, function, false, bOnlySingleInput, char_}; |
---|
55 | this->listeners_.insert(this->listeners_.end(), newListener); |
---|
56 | } |
---|
57 | */ |
---|
58 | void InputBuffer::set(const std::string& input) |
---|
59 | { |
---|
60 | this->buffer_ = ""; |
---|
61 | this->append(input); |
---|
62 | } |
---|
63 | |
---|
64 | void InputBuffer::append(const std::string& input) |
---|
65 | { |
---|
66 | for (unsigned int i = 0; i < input.size(); i++) |
---|
67 | { |
---|
68 | if (this->charIsAllowed(input[i])) |
---|
69 | this->buffer_ += input[i]; |
---|
70 | |
---|
71 | this->updated(input[i], false); |
---|
72 | } |
---|
73 | this->updated(); |
---|
74 | } |
---|
75 | |
---|
76 | void InputBuffer::append(const char& input) |
---|
77 | { |
---|
78 | if (this->charIsAllowed(input)) |
---|
79 | this->buffer_ += input; |
---|
80 | |
---|
81 | this->updated(input, true); |
---|
82 | } |
---|
83 | |
---|
84 | void InputBuffer::clear() |
---|
85 | { |
---|
86 | this->buffer_ = ""; |
---|
87 | this->updated(); |
---|
88 | } |
---|
89 | |
---|
90 | void InputBuffer::removeLast() |
---|
91 | { |
---|
92 | this->buffer_ = this->buffer_.substr(0, this->buffer_.size() - 1); |
---|
93 | this->updated(); |
---|
94 | } |
---|
95 | |
---|
96 | void InputBuffer::updated() |
---|
97 | { |
---|
98 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
99 | { |
---|
100 | if ((*it).bListenToAllChanges_) |
---|
101 | (*(*it).listener_.*(*it).function_)(); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | void InputBuffer::updated(const char& update, bool bSingleInput) |
---|
106 | { |
---|
107 | for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
108 | { |
---|
109 | if (((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput)) |
---|
110 | (*(*it).listener_.*(*it).function_)(); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | void InputBuffer::activityChanged() const |
---|
115 | { |
---|
116 | } |
---|
117 | |
---|
118 | bool InputBuffer::charIsAllowed(const char& input) |
---|
119 | { |
---|
120 | if (this->allowedChars_ == "") |
---|
121 | return true; |
---|
122 | else |
---|
123 | return (this->allowedChars_.find(input) != std::string::npos); |
---|
124 | } |
---|
125 | |
---|
126 | bool InputBuffer::keyPressed(const OIS::KeyEvent &e) |
---|
127 | { |
---|
128 | if (e.key == OIS::KC_NUMPADENTER) |
---|
129 | { |
---|
130 | this->setActivated(!this->isActivated()); |
---|
131 | this->clear(); |
---|
132 | return true; |
---|
133 | } |
---|
134 | |
---|
135 | if (this->keyboard_->isModifierDown(OIS::Keyboard::Ctrl)) |
---|
136 | { |
---|
137 | if (e.key == OIS::KC_V) |
---|
138 | { |
---|
139 | this->append(fromClipboard()); |
---|
140 | return true; |
---|
141 | } |
---|
142 | else if (e.key == OIS::KC_C) |
---|
143 | { |
---|
144 | toClipboard(this->buffer_); |
---|
145 | return true; |
---|
146 | } |
---|
147 | else if (e.key == OIS::KC_X) |
---|
148 | { |
---|
149 | toClipboard(this->buffer_); |
---|
150 | this->clear(); |
---|
151 | return true; |
---|
152 | } |
---|
153 | } |
---|
154 | else if (this->keyboard_->isModifierDown(OIS::Keyboard::Shift)) |
---|
155 | { |
---|
156 | if (e.key == OIS::KC_INSERT) |
---|
157 | { |
---|
158 | this->append(fromClipboard()); |
---|
159 | return true; |
---|
160 | } |
---|
161 | else if (e.key == OIS::KC_DELETE) |
---|
162 | { |
---|
163 | toClipboard(this->buffer_); |
---|
164 | this->clear(); |
---|
165 | return true; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | if (this->bActivated_) |
---|
170 | { |
---|
171 | //std::cout << this->keyboard_->getAsString(e.key) << " / " << (char)e.text << std::endl; |
---|
172 | /* |
---|
173 | std::string input = this->keyboard_->getAsString(e.key); |
---|
174 | if (input.size() >= 1) |
---|
175 | this->append(input[0]); |
---|
176 | */ |
---|
177 | this->append((char)e.text); |
---|
178 | } |
---|
179 | return true; |
---|
180 | } |
---|
181 | |
---|
182 | bool InputBuffer::keyReleased(const OIS::KeyEvent &e) |
---|
183 | { |
---|
184 | return true; |
---|
185 | } |
---|
186 | } |
---|