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
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 (BaseInputBufferListenerTuple* listener : this->listeners_)
78            delete listener;
79    }
80
81    void InputBuffer::setConfigValues()
82    {
83        SetConfigValue(keyRepeatDeleay_, 0.4).description("Key repeat delay of the input buffer");
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
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
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    {
111        for (const char& inputChar : input)
112        {
113            this->insert(inputChar, false);
114
115            if (update)
116                this->updated(inputChar, false);
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        {
127            if (this->buffer_.size() >= this->maxLength_)
128                return;
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    {
139        this->buffer_.clear();
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    {
171        for (BaseInputBufferListenerTuple* listener : this->listeners_)
172        {
173            if (listener->bListenToAllChanges_)
174                listener->callFunction();
175        }
176    }
177
178    void InputBuffer::updated(const char& update, bool bSingleInput)
179    {
180        for (BaseInputBufferListenerTuple* listener : this->listeners_)
181        {
182            if ((!listener->trueKeyFalseChar_) && (listener->bListenToAllChanges_ || (listener->char_ == update)) && (!listener->bOnlySingleInput_ || bSingleInput))
183                listener->callFunction();
184        }
185    }
186
187    bool InputBuffer::charIsAllowed(const char& input)
188    {
189        if (this->allowedChars_.empty())
190            return true;
191        else
192            return (this->allowedChars_.find(input) != std::string::npos);
193    }
194
195
196    void InputBuffer::processKey(const KeyEvent& evt)
197    {
198        // Prevent disaster when switching applications
199        if (evt.isModifierDown(KeyboardModifier::Alt) && evt.getKeyCode() == KeyCode::Tab)
200            return;
201
202        for (BaseInputBufferListenerTuple* listener : this->listeners_)
203        {
204            if (listener->trueKeyFalseChar_ && (listener->key_ == evt.getKeyCode()))
205                listener->callFunction();
206        }
207
208        if (evt.isModifierDown(KeyboardModifier::Ctrl))
209        {
210            if (evt.getKeyCode() == KeyCode::V)
211                this->insert(fromClipboard());
212            else if (evt.getKeyCode() == KeyCode::C)
213                toClipboard(this->buffer_);
214            else if (evt.getKeyCode() == KeyCode::X)
215            {
216                toClipboard(this->buffer_);
217                this->clear();
218            }
219        }
220        else if (evt.isModifierDown(KeyboardModifier::Shift))
221        {
222            if (evt.getKeyCode() == KeyCode::Insert)
223                this->insert(fromClipboard());
224            else if (evt.getKeyCode() == KeyCode::Delete)
225            {
226                toClipboard(this->buffer_);
227                this->clear();
228            }
229        }
230
231        this->insert(static_cast<char>(evt.getText()));
232    }
233
234    /**
235        @brief This update() function is called by the InputState if the InputBuffer is active.
236        @param dt Delta time
237    */
238    void InputBuffer::keyboardUpdated(float dt)
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
252    void InputBuffer::buttonPressed(const KeyEvent& evt)
253    {
254        lastKey_ = evt.getKeyCode();
255        timeSinceKeyPressed_ = 0.0;
256        timeSinceKeyRepeated_ = keyRepeatDeleay_;
257        keysToRepeat_ = 0;
258
259        processKey(evt);
260    }
261
262    void InputBuffer::buttonHeld(const KeyEvent& evt)
263    {
264        if (evt.getKeyCode() == lastKey_)
265        {
266            while (keysToRepeat_)
267            {
268                processKey(evt);
269                keysToRepeat_--;
270            }
271        }
272    }
273}
Note: See TracBrowser for help on using the repository browser.