Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/core/InputBuffer.h @ 1052

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

merged core2 back to trunk
there might be some errors, wasn't able to test it yet due to some strange g++ and linker behaviour.

File size: 4.4 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#ifndef _InputBuffer_H__
29#define _InputBuffer_H__
30
31#include <string>
32#include <list>
33
34#ifdef WIN32
35#include <OIS/OISKeyboard.h>
36#else
37#include <OISKeyboard.h>
38#endif
39
40#include "CorePrereqs.h"
41
42namespace orxonox
43{
44    class _CoreExport InputBufferListener
45    {
46    };
47
48    class _CoreExport InputBuffer : public OIS::KeyListener
49    {
50        struct InputBufferListenerTuple
51        {
52            InputBufferListener* listener_;
53            void (InputBufferListener::*function_)();
54            bool bListenToAllChanges_;
55            bool bOnlySingleInput_;
56            char char_;
57        };
58
59        public:
60            InputBuffer();
61
62            template <class T>
63            void registerListener(T* listener, void (T::*function)(), bool bOnlySingleInput)
64            {
65                struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, true, bOnlySingleInput, ' '};
66                this->listeners_.insert(this->listeners_.end(), newListener);
67            }
68            template <class T>
69            void registerListener(T* listener, void (T::*function)() const, bool bOnlySingleInput)
70            {
71                struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, true, bOnlySingleInput, ' '};
72                this->listeners_.insert(this->listeners_.end(), newListener);
73            }
74
75            template <class T>
76            void registerListener(T* listener, void (T::*function)(), char char_, bool bOnlySingleInput)
77            {
78                struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, false, bOnlySingleInput, char_};
79                this->listeners_.insert(this->listeners_.end(), newListener);
80            }
81            template <class T>
82            void registerListener(T* listener, void (T::*function)() const, char char_, bool bOnlySingleInput)
83            {
84                struct InputBufferListenerTuple newListener = {listener, (void (InputBufferListener::*)())function, false, bOnlySingleInput, char_};
85                this->listeners_.insert(this->listeners_.end(), newListener);
86            }
87
88            void set(const std::string& input);
89            void append(const std::string& input);
90            void append(const char& input);
91            void removeLast();
92            void clear();
93
94            void updated();
95            void updated(const char& update, bool bSingleInput);
96
97            inline std::string get() const
98                { return this->buffer_; }
99
100            inline void activate()
101                { this->setActivated(true); }
102            inline void deactivate()
103                { this->setActivated(false); }
104            inline void setActivated(bool bActivated)
105                { if (this->bActivated_ != bActivated) { this->bActivated_ = bActivated; this->activityChanged(); } else { this->bActivated_ = bActivated; } }
106            inline bool isActivated() const
107                { return this->bActivated_; }
108
109            void activityChanged() const;
110
111        private:
112            bool charIsAllowed(const char& input);
113
114            bool keyPressed(const OIS::KeyEvent &e);
115            bool keyReleased(const OIS::KeyEvent &e);
116
117            OIS::Keyboard* keyboard_;
118            bool bActivated_;
119            std::string buffer_;
120            std::list<InputBufferListenerTuple> listeners_;
121            std::string allowedChars_;
122    };
123}
124
125#endif /* _InputBuffer_H__ */
Note: See TracBrowser for help on using the repository browser.