Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/InputBuffer.cc @ 1504

Last change on this file since 1504 was 1504, checked in by rgrieder, 17 years ago

Once again, set all the svn:eol-style property to native. I really hope this doesn't pose more problems than it solves..

  • Property eol-style set to native
File size: 7.3 KB
RevLine 
[1502]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[1502]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:
[955]23 *      Fabian 'x3n' Landau
[1502]24 *   Co-authors:
25 *      Reto Grieder
26 *
27 */
[955]28
[1062]29#include "InputBuffer.h"
30
[955]31#include <iostream>
32
[1062]33#include "util/Clipboard.h"
[1293]34#include "CoreIncludes.h"
35#include "ConfigValueIncludes.h"
[955]36
37namespace orxonox
38{
[1502]39    InputBuffer::InputBuffer()
40    {
41        RegisterObject(InputBuffer);
[955]42
[1502]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)
[1214]60    {
[1502]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();
[1214]73    }
[1502]74
75    void InputBuffer::setConfigValues()
[955]76    {
[1502]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        }
[955]88    }
89
[1502]90    void InputBuffer::set(const std::string& input, bool update)
91    {
92        this->clear(false);
93        this->insert(input, update);
94    }
[955]95
[1502]96    void InputBuffer::insert(const std::string& input, bool update)
[955]97    {
[1502]98        for (unsigned int i = 0; i < input.size(); ++i)
99        {
100            this->insert(input[i], false);
[955]101
[1502]102            if (update)
103                this->updated(input[i], false);
104        }
105
106        if (update)
107            this->updated();
[955]108    }
109
[1502]110    void InputBuffer::insert(const char& input, bool update)
111    {
112        if (this->charIsAllowed(input))
113        {
114            this->buffer_.insert(this->cursor_, 1, input);
115            ++this->cursor_;
116        }
[955]117
[1502]118        if (update)
119            this->updated(input, true);
120    }
[955]121
[1502]122    void InputBuffer::clear(bool update)
123    {
124        this->buffer_ = "";
125        this->cursor_ = 0;
[1293]126
[1502]127        if (update)
128            this->updated();
129    }
[1293]130
[1502]131    void InputBuffer::removeBehindCursor(bool update)
[955]132    {
[1502]133        if (this->cursor_ > 0)
134        {
135            --this->cursor_;
136            this->buffer_.erase(this->cursor_, 1);
137
138            if (update)
139                this->updated();
140        }
[955]141    }
142
[1502]143    void InputBuffer::removeAtCursor(bool update)
[955]144    {
[1502]145        if (this->cursor_ < this->buffer_.size())
146        {
147            this->buffer_.erase(this->cursor_, 1);
148
149            if (update)
150                this->updated();
151        }
[955]152    }
153
[1502]154    void InputBuffer::updated()
155    {
156        for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
157        {
158            if ((*it)->bListenToAllChanges_)
159                (*it)->callFunction();
160        }
161    }
[1293]162
[1502]163    void InputBuffer::updated(const char& update, bool bSingleInput)
[955]164    {
[1502]165        for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
166        {
167            if ((!(*it)->trueKeyFalseChar_) && ((*it)->bListenToAllChanges_ || ((*it)->char_ == update)) && (!(*it)->bOnlySingleInput_ || bSingleInput))
168                (*it)->callFunction();
169        }
[955]170    }
[1502]171
172    bool InputBuffer::charIsAllowed(const char& input)
[955]173    {
[1502]174        if (this->allowedChars_ == "")
175            return true;
176        else
177            return (this->allowedChars_.find(input) != std::string::npos);
[955]178    }
179
180
[1502]181    void InputBuffer::processKey(const KeyEvent &evt)
[955]182    {
[1502]183        if (evt.isModifierDown(KeyboardModifier::Alt) && evt.key == KeyCode::Tab)
184            return;
185
186        for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
187        {
188            if ((*it)->trueKeyFalseChar_ && ((*it)->key_ == evt.key))
189                (*it)->callFunction();
190        }
191
192        if (evt.isModifierDown(KeyboardModifier::Ctrl))
193        {
194            if (evt.key == KeyCode::V)
195                this->insert(fromClipboard());
196            else if (evt.key == KeyCode::C)
197                toClipboard(this->buffer_);
198            else if (evt.key == KeyCode::X)
199            {
200                toClipboard(this->buffer_);
201                this->clear();
202            }
203        }
204        else if (evt.isModifierDown(KeyboardModifier::Shift))
205        {
206            if (evt.key == KeyCode::Insert)
207                this->insert(fromClipboard());
208            else if (evt.key == KeyCode::Delete)
209            {
210                toClipboard(this->buffer_);
211                this->clear();
212            }
213        }
214
215        this->insert((char)evt.text);
[955]216    }
217
[1502]218    /**
219        @brief This tick() function is called by the InputManager if the InputBuffer is active.
220        @param dt Delta time
221    */
222    void InputBuffer::tickInput(float dt, const HandlerState& state)
223    {
224        timeSinceKeyPressed_ += dt;
225        if (keysToRepeat_ < 10 && timeSinceKeyPressed_ > keyRepeatDeleay_)
226        {
227            // initial time out has gone by, start repeating keys
228            while (timeSinceKeyPressed_ - timeSinceKeyRepeated_ > keyRepeatTime_)
229            {
230                timeSinceKeyRepeated_ += keyRepeatTime_;
231                keysToRepeat_++;
232            }
233        }
234    }
[955]235
[1502]236    void InputBuffer::keyPressed(const KeyEvent &evt)
237    {
238        lastKey_ = evt.key;
239        timeSinceKeyPressed_ = 0.0;
240        timeSinceKeyRepeated_ = keyRepeatDeleay_;
241        keysToRepeat_ = 0;
[966]242
[1293]243        processKey(evt);
[955]244    }
245
[1502]246    void InputBuffer::keyHeld(const KeyEvent& evt)
247    {
248        if (evt.key == lastKey_)
249        {
250            while (keysToRepeat_)
251            {
252                processKey(evt);
253                keysToRepeat_--;
254            }
255        }
256    }
[955]257}
Note: See TracBrowser for help on using the repository browser.