Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/InputBuffer.cc @ 1317

Last change on this file since 1317 was 1317, checked in by landauf, 17 years ago

changed some parts of InGameConsole to make it work with Shell, but I couldn't test it yet as there seems to be a bug in InputBuffer.

File size: 6.1 KB
RevLine 
[955]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[955]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 *      ...
26 *
27 */
28
[1062]29#include "InputBuffer.h"
30
[955]31#include <iostream>
32
[1062]33#include "util/Clipboard.h"
[1052]34#include "InputManager.h"
[955]35
36namespace orxonox
37{
[1052]38    InputBuffer::InputBuffer()
[955]39    {
[1312]40        this->allowedChars_ = "abcdefghijklmnopqrstuvwxyz \
41                               ABCDEFGHIJKLMNOPQRSTUVWXYZ \
42                               äëïöüÄËÏÖÜáâàéêèíîìóôòúûù \
43                               0123456789 \
44                               \\\"(){}[]<>.:,;_-+*/=!?|$&%^~#";
[1052]45        this->keyboard_ = InputManager::getSingleton().getKeyboard();
[955]46        this->buffer_ = "";
[1313]47        this->cursor_ = 0;
[1317]48
49        InputManager::getSingleton().feedInputBuffer(this);
[955]50    }
[1151]51
52    InputBuffer::InputBuffer(const std::string allowedChars)
53    {
54        this->allowedChars_ = allowedChars;
55        this->keyboard_ = InputManager::getSingleton().getKeyboard();
56        this->buffer_ = "";
[1313]57        this->cursor_ = 0;
[1317]58
59        InputManager::getSingleton().feedInputBuffer(this);
[1151]60    }
[955]61
[1313]62    void InputBuffer::unregisterListener(InputBufferListener* listener)
[955]63    {
[1313]64        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
65        {
66            if ((*it).listener_ == listener)
67                this->listeners_.erase(it++);
68            else
69                ++it;
70        }
[955]71    }
72
[1313]73    void InputBuffer::set(const std::string& input, bool update)
[955]74    {
[1313]75        this->clear(false);
76        this->insert(input, update);
77    }
78
79    void InputBuffer::insert(const std::string& input, bool update)
80    {
81        for (unsigned int i = 0; i < input.size(); ++i)
[955]82        {
[1313]83            this->insert(input[i], false);
[955]84
[1313]85            if (update)
86                this->updated(input[i], false);
[955]87        }
[1313]88
89        if (update)
90            this->updated();
[955]91    }
92
[1313]93    void InputBuffer::insert(const char& input, bool update)
[955]94    {
95        if (this->charIsAllowed(input))
[1313]96        {
97            this->buffer_.insert(this->cursor_, 1, input);
98            ++this->cursor_;
99        }
[955]100
[1313]101        if (update)
102            this->updated(input, true);
[955]103    }
104
[1313]105    void InputBuffer::clear(bool update)
[955]106    {
107        this->buffer_ = "";
[1313]108        this->cursor_ = 0;
109
110        if (update)
111            this->updated();
[955]112    }
113
[1313]114    void InputBuffer::removeBehindCursor(bool update)
[955]115    {
[1313]116        if (this->cursor_ > 0)
117        {
118            --this->cursor_;
119            this->buffer_.erase(this->cursor_, 1);
120
121            if (update)
122                this->updated();
123        }
[955]124    }
125
[1313]126    void InputBuffer::removeAtCursor(bool update)
127    {
128        if (this->cursor_ < this->buffer_.size())
129        {
130            this->buffer_.erase(this->cursor_, 1);
131
132            if (update)
133                this->updated();
134        }
135    }
136
[955]137    void InputBuffer::updated()
138    {
139        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
140        {
141            if ((*it).bListenToAllChanges_)
142                (*(*it).listener_.*(*it).function_)();
143        }
144    }
145
146    void InputBuffer::updated(const char& update, bool bSingleInput)
147    {
148        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
149        {
[1312]150            if ((!(*it).trueKeyFalseChar_) && ((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput))
[955]151                (*(*it).listener_.*(*it).function_)();
152        }
153    }
154
155    bool InputBuffer::charIsAllowed(const char& input)
156    {
157        if (this->allowedChars_ == "")
158            return true;
159        else
160            return (this->allowedChars_.find(input) != std::string::npos);
161    }
162
163    bool InputBuffer::keyPressed(const OIS::KeyEvent &e)
164    {
[1312]165        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
[955]166        {
[1312]167            if ((*it).trueKeyFalseChar_ && ((*it).key_ == e.key))
168                (*(*it).listener_.*(*it).function_)();
169        }
[955]170
[966]171        if (this->keyboard_->isModifierDown(OIS::Keyboard::Ctrl))
172        {
173            if (e.key == OIS::KC_V)
174            {
[1313]175                this->insert(fromClipboard());
[966]176                return true;
177            }
178            else if (e.key == OIS::KC_C)
179            {
180                toClipboard(this->buffer_);
181                return true;
182            }
183            else if (e.key == OIS::KC_X)
184            {
185                toClipboard(this->buffer_);
186                this->clear();
187                return true;
188            }
189        }
190        else if (this->keyboard_->isModifierDown(OIS::Keyboard::Shift))
191        {
192            if (e.key == OIS::KC_INSERT)
193            {
[1313]194                this->insert(fromClipboard());
[966]195                return true;
196            }
197            else if (e.key == OIS::KC_DELETE)
198            {
199                toClipboard(this->buffer_);
200                this->clear();
201                return true;
202            }
203        }
204
[1313]205        this->insert((char)e.text);
[955]206        return true;
207    }
208
209    bool InputBuffer::keyReleased(const OIS::KeyEvent &e)
210    {
211        return true;
212    }
213}
Note: See TracBrowser for help on using the repository browser.