Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/InputBuffer.cc @ 1056

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

don't panic, no codechanges!
added a link to www.orxonox.net

File size: 5.6 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 <iostream>
30
31#include "InputBuffer.h"
32#include "InputManager.h"
33#include "util/Clipboard.h"
34
35namespace orxonox
36{
37    InputBuffer::InputBuffer()
38    {
39        this->bActivated_ = false;
40        this->allowedChars_ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜ0123456789 \\\"().:,;_-+*/=!?<>[|]";
41        this->keyboard_ = InputManager::getSingleton().getKeyboard();
42        this->buffer_ = "";
43
44        this->keyboard_->setEventCallback(this);
45    }
46/*
47    void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), bool bOnlySingleInput)
48    {
49        struct InputBufferListenerTuple newListener = {listener, function, true, bOnlySingleInput, ' '};
50        this->listeners_.insert(this->listeners_.end(), newListener);
51    }
52
53    void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), char char_, bool bOnlySingleInput)
54    {
55        struct InputBufferListenerTuple newListener = {listener, function, false, bOnlySingleInput, char_};
56        this->listeners_.insert(this->listeners_.end(), newListener);
57    }
58*/
59    void InputBuffer::set(const std::string& input)
60    {
61        this->buffer_ = "";
62        this->append(input);
63    }
64
65    void InputBuffer::append(const std::string& input)
66    {
67        for (unsigned int i = 0; i < input.size(); i++)
68        {
69            if (this->charIsAllowed(input[i]))
70                this->buffer_ += input[i];
71
72            this->updated(input[i], false);
73        }
74        this->updated();
75    }
76
77    void InputBuffer::append(const char& input)
78    {
79        if (this->charIsAllowed(input))
80            this->buffer_ += input;
81
82        this->updated(input, true);
83    }
84
85    void InputBuffer::clear()
86    {
87        this->buffer_ = "";
88        this->updated();
89    }
90
91    void InputBuffer::removeLast()
92    {
93        this->buffer_ = this->buffer_.substr(0, this->buffer_.size() - 1);
94        this->updated();
95    }
96
97    void InputBuffer::updated()
98    {
99        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
100        {
101            if ((*it).bListenToAllChanges_)
102                (*(*it).listener_.*(*it).function_)();
103        }
104    }
105
106    void InputBuffer::updated(const char& update, bool bSingleInput)
107    {
108        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
109        {
110            if (((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput))
111                (*(*it).listener_.*(*it).function_)();
112        }
113    }
114
115    void InputBuffer::activityChanged() const
116    {
117    }
118
119    bool InputBuffer::charIsAllowed(const char& input)
120    {
121        if (this->allowedChars_ == "")
122            return true;
123        else
124            return (this->allowedChars_.find(input) != std::string::npos);
125    }
126
127    bool InputBuffer::keyPressed(const OIS::KeyEvent &e)
128    {
129        if (e.key == OIS::KC_NUMPADENTER)
130        {
131            this->setActivated(!this->isActivated());
132            this->clear();
133            return true;
134        }
135
136        if (this->keyboard_->isModifierDown(OIS::Keyboard::Ctrl))
137        {
138            if (e.key == OIS::KC_V)
139            {
140                this->append(fromClipboard());
141                return true;
142            }
143            else if (e.key == OIS::KC_C)
144            {
145                toClipboard(this->buffer_);
146                return true;
147            }
148            else if (e.key == OIS::KC_X)
149            {
150                toClipboard(this->buffer_);
151                this->clear();
152                return true;
153            }
154        }
155        else if (this->keyboard_->isModifierDown(OIS::Keyboard::Shift))
156        {
157            if (e.key == OIS::KC_INSERT)
158            {
159                this->append(fromClipboard());
160                return true;
161            }
162            else if (e.key == OIS::KC_DELETE)
163            {
164                toClipboard(this->buffer_);
165                this->clear();
166                return true;
167            }
168        }
169
170        if (this->bActivated_)
171        {
172//std::cout << this->keyboard_->getAsString(e.key) << " / " << (char)e.text << std::endl;
173/*
174            std::string input = this->keyboard_->getAsString(e.key);
175            if (input.size() >= 1)
176                this->append(input[0]);
177*/
178            this->append((char)e.text);
179        }
180        return true;
181    }
182
183    bool InputBuffer::keyReleased(const OIS::KeyEvent &e)
184    {
185        return true;
186    }
187}
Note: See TracBrowser for help on using the repository browser.