Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v2/src/libraries/core/input/InputBuffer.cc @ 10821

Last change on this file since 10821 was 10821, checked in by muemart, 8 years ago

Run clang-modernize -loop-convert

  • Again, not all possible loops were converted
  • It can do pretty cool transformations, but I had to fix a few compile errors, so there might be some runtime errors lurking around too
  • Property svn:eol-style set to native
File size: 7.4 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 "util/Clipboard.h"
32#include "core/CoreIncludes.h"
33#include "core/config/ConfigValueIncludes.h"
34
35namespace orxonox
36{
37    RegisterClassNoArgs(InputBuffer);
38
39    InputBuffer::InputBuffer()
40    {
41        RegisterObject(InputBuffer);
42
43        this->cursor_ = 0;
44        this->maxLength_ = 1024;
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->maxLength_ = 1024;
64        this->allowedChars_ = allowedChars;
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    InputBuffer::~InputBuffer()
76    {
77        for (std::list<BaseInputBufferListenerTuple*>::const_iterator it = this->listeners_.begin();
78            it != this->listeners_.end(); ++it)
79            delete *it;
80    }
81
82    void InputBuffer::setConfigValues()
83    {
84        SetConfigValue(keyRepeatDeleay_, 0.4).description("Key repeat delay of the input buffer");
85        SetConfigValue(keyRepeatTime_, 0.022).description("Key repeat time of the input buffer");
86
87        if (keyRepeatDeleay_ < 0.0)
88        {
89            ResetConfigValue(keyRepeatDeleay_);
90        }
91        if (keyRepeatTime_ < 0.0)
92        {
93            ResetConfigValue(keyRepeatTime_);
94        }
95    }
96
97    void InputBuffer::setMaxLength(unsigned int length)
98    {
99        this->maxLength_ = length;
100        if (this->buffer_.size() > length)
101            this->buffer_.resize(length);
102    }
103
104    void InputBuffer::set(const std::string& input, bool update)
105    {
106        this->clear(false);
107        this->insert(input, update);
108    }
109
110    void InputBuffer::insert(const std::string& input, bool update)
111    {
112        for (auto & elem : input)
113        {
114            this->insert(elem, false);
115
116            if (update)
117                this->updated(elem, false);
118        }
119
120        if (update)
121            this->updated();
122    }
123
124    void InputBuffer::insert(const char& input, bool update)
125    {
126        if (this->charIsAllowed(input))
127        {
128            if (this->buffer_.size() >= this->maxLength_)
129                return;
130            this->buffer_.insert(this->cursor_, 1, input);
131            ++this->cursor_;
132        }
133
134        if (update)
135            this->updated(input, true);
136    }
137
138    void InputBuffer::clear(bool update)
139    {
140        this->buffer_.clear();
141        this->cursor_ = 0;
142
143        if (update)
144            this->updated();
145    }
146
147    void InputBuffer::removeBehindCursor(bool update)
148    {
149        if (this->cursor_ > 0)
150        {
151            --this->cursor_;
152            this->buffer_.erase(this->cursor_, 1);
153
154            if (update)
155                this->updated();
156        }
157    }
158
159    void InputBuffer::removeAtCursor(bool update)
160    {
161        if (this->cursor_ < this->buffer_.size())
162        {
163            this->buffer_.erase(this->cursor_, 1);
164
165            if (update)
166                this->updated();
167        }
168    }
169
170    void InputBuffer::updated()
171    {
172        for (auto & elem : this->listeners_)
173        {
174            if ((elem)->bListenToAllChanges_)
175                (elem)->callFunction();
176        }
177    }
178
179    void InputBuffer::updated(const char& update, bool bSingleInput)
180    {
181        for (auto & elem : this->listeners_)
182        {
183            if ((!(elem)->trueKeyFalseChar_) && ((elem)->bListenToAllChanges_ || ((elem)->char_ == update)) && (!(elem)->bOnlySingleInput_ || bSingleInput))
184                (elem)->callFunction();
185        }
186    }
187
188    bool InputBuffer::charIsAllowed(const char& input)
189    {
190        if (this->allowedChars_.empty())
191            return true;
192        else
193            return (this->allowedChars_.find(input) != std::string::npos);
194    }
195
196
197    void InputBuffer::processKey(const KeyEvent& evt)
198    {
199        // Prevent disaster when switching applications
200        if (evt.isModifierDown(KeyboardModifier::Alt) && evt.getKeyCode() == KeyCode::Tab)
201            return;
202
203        for (auto & elem : this->listeners_)
204        {
205            if ((elem)->trueKeyFalseChar_ && ((elem)->key_ == evt.getKeyCode()))
206                (elem)->callFunction();
207        }
208
209        if (evt.isModifierDown(KeyboardModifier::Ctrl))
210        {
211            if (evt.getKeyCode() == KeyCode::V)
212                this->insert(fromClipboard());
213            else if (evt.getKeyCode() == KeyCode::C)
214                toClipboard(this->buffer_);
215            else if (evt.getKeyCode() == KeyCode::X)
216            {
217                toClipboard(this->buffer_);
218                this->clear();
219            }
220        }
221        else if (evt.isModifierDown(KeyboardModifier::Shift))
222        {
223            if (evt.getKeyCode() == KeyCode::Insert)
224                this->insert(fromClipboard());
225            else if (evt.getKeyCode() == KeyCode::Delete)
226            {
227                toClipboard(this->buffer_);
228                this->clear();
229            }
230        }
231
232        this->insert(static_cast<char>(evt.getText()));
233    }
234
235    /**
236        @brief This update() function is called by the InputState if the InputBuffer is active.
237        @param dt Delta time
238    */
239    void InputBuffer::keyboardUpdated(float dt)
240    {
241        timeSinceKeyPressed_ += dt;
242        if (keysToRepeat_ < 10 && timeSinceKeyPressed_ > keyRepeatDeleay_)
243        {
244            // initial time out has gone by, start repeating keys
245            while (timeSinceKeyPressed_ - timeSinceKeyRepeated_ > keyRepeatTime_)
246            {
247                timeSinceKeyRepeated_ += keyRepeatTime_;
248                keysToRepeat_++;
249            }
250        }
251    }
252
253    void InputBuffer::buttonPressed(const KeyEvent& evt)
254    {
255        lastKey_ = evt.getKeyCode();
256        timeSinceKeyPressed_ = 0.0;
257        timeSinceKeyRepeated_ = keyRepeatDeleay_;
258        keysToRepeat_ = 0;
259
260        processKey(evt);
261    }
262
263    void InputBuffer::buttonHeld(const KeyEvent& evt)
264    {
265        if (evt.getKeyCode() == lastKey_)
266        {
267            while (keysToRepeat_)
268            {
269                processKey(evt);
270                keysToRepeat_--;
271            }
272        }
273    }
274}
Note: See TracBrowser for help on using the repository browser.