Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/core/InputBuffer.cc @ 1481

Last change on this file since 1481 was 1446, checked in by landauf, 16 years ago

merged console branch into network branch

after several heavy troubles it compiles, but there is still a bug I couldn't fix: orxonox crashes as soon as one presses a key after opening the console… maybe someone else sees the problem?

File size: 7.7 KB
Line 
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#include "InputBuffer.h"
30
31#include <iostream>
32
33#include "util/Clipboard.h"
34#include "CoreIncludes.h"
35#include "ConfigValueIncludes.h"
36
37namespace orxonox
38{
39    InputBuffer::InputBuffer()
40    {
41        RegisterObject(InputBuffer);
42
43        this->buffer_ = "";
44        this->cursor_ = 0;
45        this->allowedChars_ = "abcdefghijklmnopqrstuvwxyz \
46                               ABCDEFGHIJKLMNOPQRSTUVWXYZ \
47                               äëïöüÄËÏÖÜáâàéêèíîìóôòúûù \
48                               0123456789 \
49                               \\\"(){}[]<>.:,;_-+*/=!?|$&%^~#";
50
51        this->lastKey_ = KeyCode::Unassigned;
52        this->timeSinceKeyPressed_ = 0.0f;
53        this->timeSinceKeyRepeated_ = 0.0f;
54        this->keysToRepeat_ = 0;
55
56        setConfigValues();
57    }
58
59    InputBuffer::InputBuffer(const std::string allowedChars)
60    {
61        RegisterObject(InputBuffer);
62
63        this->allowedChars_ = allowedChars;
64        this->buffer_ = "";
65        this->cursor_ = 0;
66
67        this->lastKey_ = KeyCode::Unassigned;
68        this->timeSinceKeyPressed_ = 0.0f;
69        this->timeSinceKeyRepeated_ = 0.0f;
70        this->keysToRepeat_ = 0;
71
72        setConfigValues();
73    }
74
75    void InputBuffer::setConfigValues()
76    {
77        SetConfigValue(keyRepeatDeleay_, 0.4).description("Key repeat deleay of the input buffer");
78        SetConfigValue(keyRepeatTime_, 0.022).description("Key repeat time of the input buffer");
79
80        if (keyRepeatDeleay_ < 0.0)
81        {
82            ResetConfigValue(keyRepeatDeleay_);
83        }
84        if (keyRepeatTime_ < 0.0)
85        {
86            ResetConfigValue(keyRepeatTime_);
87        }
88    }
89
90    void InputBuffer::unregisterListener(InputBufferListener* listener)
91    {
92        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
93        {
94            if ((*it).listener_ == listener)
95                this->listeners_.erase(it++);
96            else
97                ++it;
98        }
99    }
100
101    void InputBuffer::set(const std::string& input, bool update)
102    {
103        this->clear(false);
104        this->insert(input, update);
105    }
106
107    void InputBuffer::insert(const std::string& input, bool update)
108    {
109        for (unsigned int i = 0; i < input.size(); ++i)
110        {
111            this->insert(input[i], false);
112
113            if (update)
114                this->updated(input[i], false);
115        }
116
117        if (update)
118            this->updated();
119    }
120
121    void InputBuffer::insert(const char& input, bool update)
122    {
123        if (this->charIsAllowed(input))
124        {
125            this->buffer_.insert(this->cursor_, 1, input);
126            ++this->cursor_;
127        }
128
129        if (update)
130            this->updated(input, true);
131    }
132
133    void InputBuffer::clear(bool update)
134    {
135        this->buffer_ = "";
136        this->cursor_ = 0;
137
138        if (update)
139            this->updated();
140    }
141
142    void InputBuffer::removeBehindCursor(bool update)
143    {
144        if (this->cursor_ > 0)
145        {
146            --this->cursor_;
147            this->buffer_.erase(this->cursor_, 1);
148
149            if (update)
150                this->updated();
151        }
152    }
153
154    void InputBuffer::removeAtCursor(bool update)
155    {
156        if (this->cursor_ < this->buffer_.size())
157        {
158            this->buffer_.erase(this->cursor_, 1);
159
160            if (update)
161                this->updated();
162        }
163    }
164
165    void InputBuffer::updated()
166    {
167        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
168        {
169            if ((*it).bListenToAllChanges_)
170                (*(*it).listener_.*(*it).function_)();
171        }
172    }
173
174    void InputBuffer::updated(const char& update, bool bSingleInput)
175    {
176        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
177        {
178            if ((!(*it).trueKeyFalseChar_) && ((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput))
179                (*(*it).listener_.*(*it).function_)();
180        }
181    }
182
183    bool InputBuffer::charIsAllowed(const char& input)
184    {
185        if (this->allowedChars_ == "")
186            return true;
187        else
188            return (this->allowedChars_.find(input) != std::string::npos);
189    }
190
191
192    void InputBuffer::processKey(const KeyEvent &evt)
193    {
194        if (evt.isModifierDown(KeyboardModifier::Alt) && evt.key == KeyCode::Tab)
195            return;
196
197        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
198        {
199            if ((*it).trueKeyFalseChar_ && ((*it).key_ == evt.key))
200                (*(*it).listener_.*(*it).function_)();
201        }
202
203        if (evt.isModifierDown(KeyboardModifier::Ctrl))
204        {
205            if (evt.key == KeyCode::V)
206                this->insert(fromClipboard());
207            else if (evt.key == KeyCode::C)
208                toClipboard(this->buffer_);
209            else if (evt.key == KeyCode::X)
210            {
211                toClipboard(this->buffer_);
212                this->clear();
213            }
214        }
215        else if (evt.isModifierDown(KeyboardModifier::Shift))
216        {
217            if (evt.key == KeyCode::Insert)
218                this->insert(fromClipboard());
219            else if (evt.key == KeyCode::Delete)
220            {
221                toClipboard(this->buffer_);
222                this->clear();
223            }
224        }
225
226        this->insert((char)evt.text);
227    }
228
229    /**
230        @brief This tick() function is called by the InputManager if the InputBuffer is active.
231        @param dt Delta time
232    */
233    void InputBuffer::tickInput(float dt, const HandlerState& state)
234    {
235        timeSinceKeyPressed_ += dt;
236        if (keysToRepeat_ < 10 && timeSinceKeyPressed_ > keyRepeatDeleay_)
237        {
238            // initial time out has gone by, start repeating keys
239            while (timeSinceKeyPressed_ - timeSinceKeyRepeated_ > keyRepeatTime_)
240            {
241                timeSinceKeyRepeated_ += keyRepeatTime_;
242                keysToRepeat_++;
243            }
244        }
245    }
246
247    void InputBuffer::keyPressed(const KeyEvent &evt)
248    {
249        lastKey_ = evt.key;
250        timeSinceKeyPressed_ = 0.0;
251        timeSinceKeyRepeated_ = keyRepeatDeleay_;
252        keysToRepeat_ = 0;
253
254        processKey(evt);
255    }
256
257    void InputBuffer::keyHeld(const KeyEvent& evt)
258    {
259        if (evt.key == lastKey_)
260        {
261            while (keysToRepeat_)
262            {
263                processKey(evt);
264                keysToRepeat_--;
265            }
266        }
267    }
268}
Note: See TracBrowser for help on using the repository browser.