Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/input/InputBuffer.cc @ 12183

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

Remove some non-ASCII characters to avoid encoding issues. This only worked on computers with German locale, if at all.

  • Property svn:eol-style set to native
File size: 7.4 KB
RevLine 
[1505]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"
[1519]32#include "core/CoreIncludes.h"
[9667]33#include "core/config/ConfigValueIncludes.h"
[1505]34
35namespace orxonox
36{
[9667]37    RegisterClassNoArgs(InputBuffer);
38
[1505]39    InputBuffer::InputBuffer()
40    {
[9667]41        RegisterObject(InputBuffer);
[1505]42
43        this->cursor_ = 0;
[6417]44        this->maxLength_ = 1024;
[1505]45        this->allowedChars_ = "abcdefghijklmnopqrstuvwxyz \
46                               ABCDEFGHIJKLMNOPQRSTUVWXYZ \
47                               0123456789 \
48                               \\\"(){}[]<>.:,;_-+*/=!?|$&%^~#";
49
50        this->lastKey_ = KeyCode::Unassigned;
51        this->timeSinceKeyPressed_ = 0.0f;
52        this->timeSinceKeyRepeated_ = 0.0f;
53        this->keysToRepeat_ = 0;
54
55        setConfigValues();
56    }
57
[3196]58    InputBuffer::InputBuffer(const std::string& allowedChars)
[1505]59    {
[9667]60        RegisterObject(InputBuffer);
[1505]61
[6417]62        this->maxLength_ = 1024;
[1505]63        this->allowedChars_ = allowedChars;
64        this->cursor_ = 0;
65
66        this->lastKey_ = KeyCode::Unassigned;
67        this->timeSinceKeyPressed_ = 0.0f;
68        this->timeSinceKeyRepeated_ = 0.0f;
69        this->keysToRepeat_ = 0;
70
71        setConfigValues();
72    }
73
[2662]74    InputBuffer::~InputBuffer()
75    {
[11071]76        for (BaseInputBufferListenerTuple* listener : this->listeners_)
77            delete listener;
[2662]78    }
79
[1505]80    void InputBuffer::setConfigValues()
81    {
[6105]82        SetConfigValue(keyRepeatDeleay_, 0.4).description("Key repeat delay of the input buffer");
[1505]83        SetConfigValue(keyRepeatTime_, 0.022).description("Key repeat time of the input buffer");
84
85        if (keyRepeatDeleay_ < 0.0)
86        {
87            ResetConfigValue(keyRepeatDeleay_);
88        }
89        if (keyRepeatTime_ < 0.0)
90        {
91            ResetConfigValue(keyRepeatTime_);
92        }
93    }
94
[6417]95    void InputBuffer::setMaxLength(unsigned int length)
96    {
97        this->maxLength_ = length;
98        if (this->buffer_.size() > length)
99            this->buffer_.resize(length);
100    }
101
[1505]102    void InputBuffer::set(const std::string& input, bool update)
103    {
104        this->clear(false);
105        this->insert(input, update);
106    }
107
108    void InputBuffer::insert(const std::string& input, bool update)
109    {
[11071]110        for (const char& inputChar : input)
[1505]111        {
[11071]112            this->insert(inputChar, false);
[1505]113
114            if (update)
[11071]115                this->updated(inputChar, false);
[1505]116        }
117
118        if (update)
119            this->updated();
120    }
121
122    void InputBuffer::insert(const char& input, bool update)
123    {
124        if (this->charIsAllowed(input))
125        {
[6417]126            if (this->buffer_.size() >= this->maxLength_)
127                return;
[1505]128            this->buffer_.insert(this->cursor_, 1, input);
129            ++this->cursor_;
130        }
131
132        if (update)
133            this->updated(input, true);
134    }
135
136    void InputBuffer::clear(bool update)
137    {
[6417]138        this->buffer_.clear();
[1505]139        this->cursor_ = 0;
140
141        if (update)
142            this->updated();
143    }
144
145    void InputBuffer::removeBehindCursor(bool update)
146    {
147        if (this->cursor_ > 0)
148        {
149            --this->cursor_;
150            this->buffer_.erase(this->cursor_, 1);
151
152            if (update)
153                this->updated();
154        }
155    }
156
157    void InputBuffer::removeAtCursor(bool update)
158    {
159        if (this->cursor_ < this->buffer_.size())
160        {
161            this->buffer_.erase(this->cursor_, 1);
162
163            if (update)
164                this->updated();
165        }
166    }
167
168    void InputBuffer::updated()
169    {
[11071]170        for (BaseInputBufferListenerTuple* listener : this->listeners_)
[1505]171        {
[11071]172            if (listener->bListenToAllChanges_)
173                listener->callFunction();
[1505]174        }
175    }
176
177    void InputBuffer::updated(const char& update, bool bSingleInput)
178    {
[11071]179        for (BaseInputBufferListenerTuple* listener : this->listeners_)
[1505]180        {
[11071]181            if ((!listener->trueKeyFalseChar_) && (listener->bListenToAllChanges_ || (listener->char_ == update)) && (!listener->bOnlySingleInput_ || bSingleInput))
182                listener->callFunction();
[1505]183        }
184    }
185
186    bool InputBuffer::charIsAllowed(const char& input)
187    {
[6417]188        if (this->allowedChars_.empty())
[1505]189            return true;
190        else
191            return (this->allowedChars_.find(input) != std::string::npos);
192    }
193
194
[3327]195    void InputBuffer::processKey(const KeyEvent& evt)
[1505]196    {
[6105]197        // Prevent disaster when switching applications
[3327]198        if (evt.isModifierDown(KeyboardModifier::Alt) && evt.getKeyCode() == KeyCode::Tab)
[1505]199            return;
200
[11071]201        for (BaseInputBufferListenerTuple* listener : this->listeners_)
[1505]202        {
[11071]203            if (listener->trueKeyFalseChar_ && (listener->key_ == evt.getKeyCode()))
204                listener->callFunction();
[1505]205        }
206
207        if (evt.isModifierDown(KeyboardModifier::Ctrl))
208        {
[3327]209            if (evt.getKeyCode() == KeyCode::V)
[1505]210                this->insert(fromClipboard());
[3327]211            else if (evt.getKeyCode() == KeyCode::C)
[1505]212                toClipboard(this->buffer_);
[3327]213            else if (evt.getKeyCode() == KeyCode::X)
[1505]214            {
215                toClipboard(this->buffer_);
216                this->clear();
217            }
218        }
219        else if (evt.isModifierDown(KeyboardModifier::Shift))
220        {
[3327]221            if (evt.getKeyCode() == KeyCode::Insert)
[1505]222                this->insert(fromClipboard());
[3327]223            else if (evt.getKeyCode() == KeyCode::Delete)
[1505]224            {
225                toClipboard(this->buffer_);
226                this->clear();
227            }
228        }
229
[3327]230        this->insert(static_cast<char>(evt.getText()));
[1505]231    }
232
233    /**
[6105]234        @brief This update() function is called by the InputState if the InputBuffer is active.
[1505]235        @param dt Delta time
236    */
[3327]237    void InputBuffer::keyboardUpdated(float dt)
[1505]238    {
239        timeSinceKeyPressed_ += dt;
240        if (keysToRepeat_ < 10 && timeSinceKeyPressed_ > keyRepeatDeleay_)
241        {
242            // initial time out has gone by, start repeating keys
243            while (timeSinceKeyPressed_ - timeSinceKeyRepeated_ > keyRepeatTime_)
244            {
245                timeSinceKeyRepeated_ += keyRepeatTime_;
246                keysToRepeat_++;
247            }
248        }
249    }
250
[3327]251    void InputBuffer::buttonPressed(const KeyEvent& evt)
[1505]252    {
[3327]253        lastKey_ = evt.getKeyCode();
[1505]254        timeSinceKeyPressed_ = 0.0;
255        timeSinceKeyRepeated_ = keyRepeatDeleay_;
256        keysToRepeat_ = 0;
257
258        processKey(evt);
259    }
260
[3327]261    void InputBuffer::buttonHeld(const KeyEvent& evt)
[1505]262    {
[3327]263        if (evt.getKeyCode() == lastKey_)
[1505]264        {
265            while (keysToRepeat_)
266            {
267                processKey(evt);
268                keysToRepeat_--;
269            }
270        }
271    }
272}
Note: See TracBrowser for help on using the repository browser.