Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/InputBuffer.cc @ 1049

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

config-value accepts now strings with special chars like \n or \t and hopefully every other. strings are enclosed by "…", but the quotes are only visible in the config-file. if you want something like " ← whitespace" you must add quotes, otherwise this would be stripped to "← whitespace".

File size: 5.5 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#include <iostream>
29
30#include "InputBuffer.h"
31#include "util/Clipboard.h"
32
33namespace orxonox
34{
35    InputBuffer::InputBuffer(OIS::Keyboard* keyboard)
36    {
37        this->bActivated_ = false;
38        this->allowedChars_ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜ0123456789 \\\"().:,;_-+*/=!?<>[|]";
39        this->keyboard_ = keyboard;
40        this->buffer_ = "";
41
42        this->keyboard_->setEventCallback(this);
43    }
44/*
45    void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), bool bOnlySingleInput)
46    {
47        struct InputBufferListenerTuple newListener = {listener, function, true, bOnlySingleInput, ' '};
48        this->listeners_.insert(this->listeners_.end(), newListener);
49    }
50
51    void InputBuffer::registerListener(InputBufferListener* listener, void (InputBufferListener::*function)(), char char_, bool bOnlySingleInput)
52    {
53        struct InputBufferListenerTuple newListener = {listener, function, false, bOnlySingleInput, char_};
54        this->listeners_.insert(this->listeners_.end(), newListener);
55    }
56*/
57    void InputBuffer::set(const std::string& input)
58    {
59        this->buffer_ = "";
60        this->append(input);
61    }
62
63    void InputBuffer::append(const std::string& input)
64    {
65        for (unsigned int i = 0; i < input.size(); i++)
66        {
67            if (this->charIsAllowed(input[i]))
68                this->buffer_ += input[i];
69
70            this->updated(input[i], false);
71        }
72        this->updated();
73    }
74
75    void InputBuffer::append(const char& input)
76    {
77        if (this->charIsAllowed(input))
78            this->buffer_ += input;
79
80        this->updated(input, true);
81    }
82
83    void InputBuffer::clear()
84    {
85        this->buffer_ = "";
86        this->updated();
87    }
88
89    void InputBuffer::removeLast()
90    {
91        this->buffer_ = this->buffer_.substr(0, this->buffer_.size() - 1);
92        this->updated();
93    }
94
95    void InputBuffer::updated()
96    {
97        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
98        {
99            if ((*it).bListenToAllChanges_)
100                (*(*it).listener_.*(*it).function_)();
101        }
102    }
103
104    void InputBuffer::updated(const char& update, bool bSingleInput)
105    {
106        for (std::list<InputBufferListenerTuple>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
107        {
108            if (((*it).bListenToAllChanges_ || ((*it).char_ == update)) && (!(*it).bOnlySingleInput_ || bSingleInput))
109                (*(*it).listener_.*(*it).function_)();
110        }
111    }
112
113    void InputBuffer::activityChanged() const
114    {
115    }
116
117    bool InputBuffer::charIsAllowed(const char& input)
118    {
119        if (this->allowedChars_ == "")
120            return true;
121        else
122            return (this->allowedChars_.find(input) != std::string::npos);
123    }
124
125    bool InputBuffer::keyPressed(const OIS::KeyEvent &e)
126    {
127        if (e.key == OIS::KC_NUMPADENTER)
128        {
129            this->setActivated(!this->isActivated());
130            this->clear();
131            return true;
132        }
133
134        if (this->keyboard_->isModifierDown(OIS::Keyboard::Ctrl))
135        {
136            if (e.key == OIS::KC_V)
137            {
138                this->append(fromClipboard());
139                return true;
140            }
141            else if (e.key == OIS::KC_C)
142            {
143                toClipboard(this->buffer_);
144                return true;
145            }
146            else if (e.key == OIS::KC_X)
147            {
148                toClipboard(this->buffer_);
149                this->clear();
150                return true;
151            }
152        }
153        else if (this->keyboard_->isModifierDown(OIS::Keyboard::Shift))
154        {
155            if (e.key == OIS::KC_INSERT)
156            {
157                this->append(fromClipboard());
158                return true;
159            }
160            else if (e.key == OIS::KC_DELETE)
161            {
162                toClipboard(this->buffer_);
163                this->clear();
164                return true;
165            }
166        }
167
168        if (this->bActivated_)
169        {
170//std::cout << this->keyboard_->getAsString(e.key) << " / " << (char)e.text << std::endl;
171/*
172            std::string input = this->keyboard_->getAsString(e.key);
173            if (input.size() >= 1)
174                this->append(input[0]);
175*/
176            this->append((char)e.text);
177        }
178        return true;
179    }
180
181    bool InputBuffer::keyReleased(const OIS::KeyEvent &e)
182    {
183        return true;
184    }
185}
Note: See TracBrowser for help on using the repository browser.