Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1312 was 1312, checked in by landauf, 16 years ago

changed InputBuffer, added OutputBuffer and made a first sketch of Shell.

File size: 5.0 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    }
48
49    InputBuffer::InputBuffer(const std::string allowedChars)
50    {
51        this->allowedChars_ = allowedChars;
52        this->keyboard_ = InputManager::getSingleton().getKeyboard();
53        this->buffer_ = "";
54    }
55
56    void InputBuffer::set(const std::string& input)
57    {
58        this->buffer_ = "";
59        this->append(input);
60    }
61
62    void InputBuffer::append(const std::string& input)
63    {
64        for (unsigned int i = 0; i < input.size(); i++)
65        {
66            if (this->charIsAllowed(input[i]))
67                this->buffer_ += input[i];
68
69            this->updated(input[i], false);
70        }
71        this->updated();
72    }
73
74    void InputBuffer::append(const char& input)
75    {
76        if (this->charIsAllowed(input))
77            this->buffer_ += input;
78
79        this->updated(input, true);
80    }
81
82    void InputBuffer::clear()
83    {
84        this->buffer_ = "";
85        this->updated();
86    }
87
88    void InputBuffer::removeLast()
89    {
90        this->buffer_ = this->buffer_.substr(0, this->buffer_.size() - 1);
91        this->updated();
92    }
93
94    void InputBuffer::updated()
95    {
96        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
97        {
98            if ((*it).bListenToAllChanges_)
99                (*(*it).listener_.*(*it).function_)();
100        }
101    }
102
103    void InputBuffer::updated(const char& update, bool bSingleInput)
104    {
105        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
106        {
107            if ((!(*it).trueKeyFalseChar_) && ((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput))
108                (*(*it).listener_.*(*it).function_)();
109        }
110    }
111
112    bool InputBuffer::charIsAllowed(const char& input)
113    {
114        if (this->allowedChars_ == "")
115            return true;
116        else
117            return (this->allowedChars_.find(input) != std::string::npos);
118    }
119
120    bool InputBuffer::keyPressed(const OIS::KeyEvent &e)
121    {
122        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
123        {
124            if ((*it).trueKeyFalseChar_ && ((*it).key_ == e.key))
125                (*(*it).listener_.*(*it).function_)();
126        }
127
128        if (this->keyboard_->isModifierDown(OIS::Keyboard::Ctrl))
129        {
130            if (e.key == OIS::KC_V)
131            {
132                this->append(fromClipboard());
133                return true;
134            }
135            else if (e.key == OIS::KC_C)
136            {
137                toClipboard(this->buffer_);
138                return true;
139            }
140            else if (e.key == OIS::KC_X)
141            {
142                toClipboard(this->buffer_);
143                this->clear();
144                return true;
145            }
146        }
147        else if (this->keyboard_->isModifierDown(OIS::Keyboard::Shift))
148        {
149            if (e.key == OIS::KC_INSERT)
150            {
151                this->append(fromClipboard());
152                return true;
153            }
154            else if (e.key == OIS::KC_DELETE)
155            {
156                toClipboard(this->buffer_);
157                this->clear();
158                return true;
159            }
160        }
161
162        this->append((char)e.text);
163        return true;
164    }
165
166    bool InputBuffer::keyReleased(const OIS::KeyEvent &e)
167    {
168        return true;
169    }
170}
Note: See TracBrowser for help on using the repository browser.