Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11071 was 11071, checked in by landauf, 8 years ago

merged branch cpp11_v3 back to trunk

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