Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1334 was 1334, checked in by landauf, 16 years ago
  • added colored output in the InGameConsole, depending on the output level (error = red, …).
  • increased performance of InGameConsole and Shell
File size: 6.2 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        if (this->keyboard_->isModifierDown(OIS::Keyboard::Alt) && e.key == OIS::KC_TAB)
166            return true;
167
168        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
169        {
170            if ((*it).trueKeyFalseChar_ && ((*it).key_ == e.key))
171                (*(*it).listener_.*(*it).function_)();
172        }
173
174        if (this->keyboard_->isModifierDown(OIS::Keyboard::Ctrl))
175        {
176            if (e.key == OIS::KC_V)
177            {
178                this->insert(fromClipboard());
179                return true;
180            }
181            else if (e.key == OIS::KC_C)
182            {
183                toClipboard(this->buffer_);
184                return true;
185            }
186            else if (e.key == OIS::KC_X)
187            {
188                toClipboard(this->buffer_);
189                this->clear();
190                return true;
191            }
192        }
193        else if (this->keyboard_->isModifierDown(OIS::Keyboard::Shift))
194        {
195            if (e.key == OIS::KC_INSERT)
196            {
197                this->insert(fromClipboard());
198                return true;
199            }
200            else if (e.key == OIS::KC_DELETE)
201            {
202                toClipboard(this->buffer_);
203                this->clear();
204                return true;
205            }
206        }
207
208        this->insert((char)e.text);
209        return true;
210    }
211
212    bool InputBuffer::keyReleased(const OIS::KeyEvent &e)
213    {
214        return true;
215    }
216}
Note: See TracBrowser for help on using the repository browser.