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, 16 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
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 *      ...
26 *
27 */
28
29#include "InputBuffer.h"
30
31#include <iostream>
32
33#include "util/Clipboard.h"
34#include "InputManager.h"
35
36namespace orxonox
37{
38    InputBuffer::InputBuffer()
39    {
40        this->allowedChars_ = "abcdefghijklmnopqrstuvwxyz \
41                               ABCDEFGHIJKLMNOPQRSTUVWXYZ \
42                               äëïöüÄËÏÖÜáâàéêèíîìóôòúûù \
43                               0123456789 \
44                               \\\"(){}[]<>.:,;_-+*/=!?|$&%^~#";
45        this->keyboard_ = InputManager::getSingleton().getKeyboard();
46        this->buffer_ = "";
47        this->cursor_ = 0;
48
49        InputManager::getSingleton().feedInputBuffer(this);
50    }
51
52    InputBuffer::InputBuffer(const std::string allowedChars)
53    {
54        this->allowedChars_ = allowedChars;
55        this->keyboard_ = InputManager::getSingleton().getKeyboard();
56        this->buffer_ = "";
57        this->cursor_ = 0;
58
59        InputManager::getSingleton().feedInputBuffer(this);
60    }
61
62    void InputBuffer::unregisterListener(InputBufferListener* listener)
63    {
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        }
71    }
72
73    void InputBuffer::set(const std::string& input, bool update)
74    {
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)
82        {
83            this->insert(input[i], false);
84
85            if (update)
86                this->updated(input[i], false);
87        }
88
89        if (update)
90            this->updated();
91    }
92
93    void InputBuffer::insert(const char& input, bool update)
94    {
95        if (this->charIsAllowed(input))
96        {
97            this->buffer_.insert(this->cursor_, 1, input);
98            ++this->cursor_;
99        }
100
101        if (update)
102            this->updated(input, true);
103    }
104
105    void InputBuffer::clear(bool update)
106    {
107        this->buffer_ = "";
108        this->cursor_ = 0;
109
110        if (update)
111            this->updated();
112    }
113
114    void InputBuffer::removeBehindCursor(bool update)
115    {
116        if (this->cursor_ > 0)
117        {
118            --this->cursor_;
119            this->buffer_.erase(this->cursor_, 1);
120
121            if (update)
122                this->updated();
123        }
124    }
125
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
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        {
150            if ((!(*it).trueKeyFalseChar_) && ((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput))
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    {
165        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
166        {
167            if ((*it).trueKeyFalseChar_ && ((*it).key_ == e.key))
168                (*(*it).listener_.*(*it).function_)();
169        }
170
171        if (this->keyboard_->isModifierDown(OIS::Keyboard::Ctrl))
172        {
173            if (e.key == OIS::KC_V)
174            {
175                this->insert(fromClipboard());
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            {
194                this->insert(fromClipboard());
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
205        this->insert((char)e.text);
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.