Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/core/input/InputBuffer.cc @ 3275

Last change on this file since 3275 was 3274, checked in by rgrieder, 15 years ago

Added a few more generic parts to the input library:

  • Created Mouse and Keyboard to join JoyStick and provided them with a templated base class (InputDeviceTemplated) that does most of the work (reduces quite some redundancy)
  • Created InputPrereqs.h from InputInterfaces.h and destroyed the latter
  • Exported InputHandler to its own file and replaced KeyHandler, MouseHandler and JoyStickHandler with the single InputHandler.
  • Deleted the SimpleInputState: There is only one class now which fulfills all our needs.

In general there is now less code and the code itself has more 'pluses'. However I haven't really thrown away any feature at all.

  • Property svn:eol-style set to native
File size: 7.3 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/ConfigValueIncludes.h"
34
35namespace orxonox
36{
37    InputBuffer::InputBuffer()
38    {
39        RegisterRootObject(InputBuffer);
40
41        this->buffer_ = "";
42        this->cursor_ = 0;
43        this->allowedChars_ = "abcdefghijklmnopqrstuvwxyz \
44                               ABCDEFGHIJKLMNOPQRSTUVWXYZ \
45                               äëïöüÄËÏÖÜáâàéêèíîìóôòúûù \
46                               0123456789 \
47                               \\\"(){}[]<>.:,;_-+*/=!?|$&%^~#";
48
49        this->lastKey_ = KeyCode::Unassigned;
50        this->timeSinceKeyPressed_ = 0.0f;
51        this->timeSinceKeyRepeated_ = 0.0f;
52        this->keysToRepeat_ = 0;
53
54        setConfigValues();
55    }
56
57    InputBuffer::InputBuffer(const std::string& allowedChars)
58    {
59        RegisterRootObject(InputBuffer);
60
61        this->allowedChars_ = allowedChars;
62        this->buffer_ = "";
63        this->cursor_ = 0;
64
65        this->lastKey_ = KeyCode::Unassigned;
66        this->timeSinceKeyPressed_ = 0.0f;
67        this->timeSinceKeyRepeated_ = 0.0f;
68        this->keysToRepeat_ = 0;
69
70        setConfigValues();
71    }
72
73    InputBuffer::~InputBuffer()
74    {
75        for (std::list<BaseInputBufferListenerTuple*>::const_iterator it = this->listeners_.begin();
76            it != this->listeners_.end(); ++it)
77            delete *it;
78    }
79
80    void InputBuffer::setConfigValues()
81    {
82        SetConfigValue(keyRepeatDeleay_, 0.4).description("Key repeat deleay of the input buffer");
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
95    void InputBuffer::set(const std::string& input, bool update)
96    {
97        this->clear(false);
98        this->insert(input, update);
99    }
100
101    void InputBuffer::insert(const std::string& input, bool update)
102    {
103        for (unsigned int i = 0; i < input.size(); ++i)
104        {
105            this->insert(input[i], false);
106
107            if (update)
108                this->updated(input[i], false);
109        }
110
111        if (update)
112            this->updated();
113    }
114
115    void InputBuffer::insert(const char& input, bool update)
116    {
117        if (this->charIsAllowed(input))
118        {
119            this->buffer_.insert(this->cursor_, 1, input);
120            ++this->cursor_;
121        }
122
123        if (update)
124            this->updated(input, true);
125    }
126
127    void InputBuffer::clear(bool update)
128    {
129        this->buffer_ = "";
130        this->cursor_ = 0;
131
132        if (update)
133            this->updated();
134    }
135
136    void InputBuffer::removeBehindCursor(bool update)
137    {
138        if (this->cursor_ > 0)
139        {
140            --this->cursor_;
141            this->buffer_.erase(this->cursor_, 1);
142
143            if (update)
144                this->updated();
145        }
146    }
147
148    void InputBuffer::removeAtCursor(bool update)
149    {
150        if (this->cursor_ < this->buffer_.size())
151        {
152            this->buffer_.erase(this->cursor_, 1);
153
154            if (update)
155                this->updated();
156        }
157    }
158
159    void InputBuffer::updated()
160    {
161        for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
162        {
163            if ((*it)->bListenToAllChanges_)
164                (*it)->callFunction();
165        }
166    }
167
168    void InputBuffer::updated(const char& update, bool bSingleInput)
169    {
170        for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
171        {
172            if ((!(*it)->trueKeyFalseChar_) && ((*it)->bListenToAllChanges_ || ((*it)->char_ == update)) && (!(*it)->bOnlySingleInput_ || bSingleInput))
173                (*it)->callFunction();
174        }
175    }
176
177    bool InputBuffer::charIsAllowed(const char& input)
178    {
179        if (this->allowedChars_ == "")
180            return true;
181        else
182            return (this->allowedChars_.find(input) != std::string::npos);
183    }
184
185
186    void InputBuffer::processKey(const KeyEvent& evt)
187    {
188        if (evt.isModifierDown(KeyboardModifier::Alt) && evt.getKeyCode() == KeyCode::Tab)
189            return;
190
191        for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
192        {
193            if ((*it)->trueKeyFalseChar_ && ((*it)->key_ == evt.getKeyCode()))
194                (*it)->callFunction();
195        }
196
197        if (evt.isModifierDown(KeyboardModifier::Ctrl))
198        {
199            if (evt.getKeyCode() == KeyCode::V)
200                this->insert(fromClipboard());
201            else if (evt.getKeyCode() == KeyCode::C)
202                toClipboard(this->buffer_);
203            else if (evt.getKeyCode() == KeyCode::X)
204            {
205                toClipboard(this->buffer_);
206                this->clear();
207            }
208        }
209        else if (evt.isModifierDown(KeyboardModifier::Shift))
210        {
211            if (evt.getKeyCode() == KeyCode::Insert)
212                this->insert(fromClipboard());
213            else if (evt.getKeyCode() == KeyCode::Delete)
214            {
215                toClipboard(this->buffer_);
216                this->clear();
217            }
218        }
219
220        this->insert((char)evt.getText());
221    }
222
223    /**
224        @brief This update() function is called by the InputManager if the InputBuffer is active.
225        @param dt Delta time
226    */
227    void InputBuffer::keyboardUpdated(float dt)
228    {
229        timeSinceKeyPressed_ += dt;
230        if (keysToRepeat_ < 10 && timeSinceKeyPressed_ > keyRepeatDeleay_)
231        {
232            // initial time out has gone by, start repeating keys
233            while (timeSinceKeyPressed_ - timeSinceKeyRepeated_ > keyRepeatTime_)
234            {
235                timeSinceKeyRepeated_ += keyRepeatTime_;
236                keysToRepeat_++;
237            }
238        }
239    }
240
241    void InputBuffer::buttonPressed(const KeyEvent& evt)
242    {
243        lastKey_ = evt.getKeyCode();
244        timeSinceKeyPressed_ = 0.0;
245        timeSinceKeyRepeated_ = keyRepeatDeleay_;
246        keysToRepeat_ = 0;
247
248        processKey(evt);
249    }
250
251    void InputBuffer::buttonHeld(const KeyEvent& evt)
252    {
253        if (evt.getKeyCode() == lastKey_)
254        {
255            while (keysToRepeat_)
256            {
257                processKey(evt);
258                keysToRepeat_--;
259            }
260        }
261    }
262}
Note: See TracBrowser for help on using the repository browser.