Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/core/InputBuffer.h @ 1489

Last change on this file since 1489 was 1489, checked in by FelixSchulthess, 16 years ago

changed 1 line in input buffer

File size: 7.7 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 *      Reto Grieder
26 *
27 */
28
29#ifndef _InputBuffer_H__
30#define _InputBuffer_H__
31
32#include "CorePrereqs.h"
33
34#include <string>
35#include <list>
36
37#include "InputInterfaces.h"
38#include "OrxonoxClass.h"
39
40namespace orxonox
41{
42    class BaseInputBufferListenerTuple
43    {
44    public:
45        BaseInputBufferListenerTuple(bool bListenToAllChanges, bool bOnlySingleInput,
46            bool trueKeyFalseChar, char _char, KeyCode::Enum key)
47            : bListenToAllChanges_(bListenToAllChanges), bOnlySingleInput_(bOnlySingleInput),
48              trueKeyFalseChar_(trueKeyFalseChar), char_(_char), key_(key)
49        { }
50        virtual void callFunction() = 0;
51        bool bListenToAllChanges_;
52        bool bOnlySingleInput_;
53        bool trueKeyFalseChar_;
54        char char_;
55        KeyCode::Enum key_;
56    };
57
58    template <class T>
59    class InputBufferListenerTuple : public BaseInputBufferListenerTuple
60    {
61    public:
62        InputBufferListenerTuple(T* listener, void (T::*function)(), bool bListenToAllChanges,
63            bool bOnlySingleInput, bool trueKeyFalseChar, char _char, KeyCode::Enum key)
64            : BaseInputBufferListenerTuple(bListenToAllChanges, bOnlySingleInput, trueKeyFalseChar, _char, key),
65              listener_(listener), function_(function)
66        { }
67        void callFunction()
68        {
69            (listener_->*function_)();
70        }
71        T* listener_;
72        void (T::*function_)();
73    };
74
75    class _CoreExport InputBuffer : public KeyHandler, public OrxonoxClass
76    {
77        public:
78            InputBuffer();
79            InputBuffer(const std::string allowedChars);
80
81            void setConfigValues();
82
83            template <class T>
84            void registerListener(T* listener, void (T::*function)(), bool bOnlySingleInput)
85            {
86                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned);
87                this->listeners_.insert(this->listeners_.end(), newTuple);
88            }
89            template <class T>
90            void registerListener(T* listener, void (T::*function)() const, bool bOnlySingleInput)
91            {
92                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned);
93                this->listeners_.insert(this->listeners_.end(), newTuple);
94            }
95            template <class T>
96            void registerListener(T* listener, void (T::*function)(), char _char, bool bOnlySingleInput)
97            {
98                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned);
99                this->listeners_.insert(this->listeners_.end(), newTuple);
100            }
101            template <class T>
102            void registerListener(T* listener, void (T::*function)() const, char _char, bool bOnlySingleInput)
103            {
104                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned);
105                this->listeners_.insert(this->listeners_.end(), newTuple);
106            }
107
108            template <class T>
109            void registerListener(T* listener, void (T::*function)(), KeyCode::Enum key)
110            {
111                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, true, true, '\0', key);
112                this->listeners_.insert(this->listeners_.end(), newTuple);
113            }
114            template <class T>
115            void registerListener(T* listener, void (T::*function)() const, KeyCode::Enum key)
116            {
117                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, true, true, '\0', key);
118                this->listeners_.insert(this->listeners_.end(), newTuple);
119            }
120
121            template <class T>
122            void unregisterListener(T* listener)
123            {
124                for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
125                {
126                    InputBufferListenerTuple<T>* refListener = dynamic_cast<InputBufferListenerTuple<T>*>(*it);
127                    if (refListener && refListener->listener_ == listener)
128                        this->listeners_.erase(it++);
129                    else
130                        it++;
131                }
132            }
133
134            void set(const std::string& input, bool update = true);
135            void insert(const std::string& input, bool update = true);
136            void insert(const char& input, bool update = true);
137            void clear(bool update = true);
138            void removeAtCursor(bool update = true);
139            void removeBehindCursor(bool update = true);
140
141            void updated();
142            void updated(const char& update, bool bSingleInput);
143
144            inline std::string get() const
145                { return this->buffer_; }
146            inline unsigned int getSize() const
147                { return this->buffer_.size(); }
148
149            inline unsigned int getCursorPosition() const
150                { return this->cursor_; }
151            inline void setCursorPosition(unsigned int cursor)
152                { if (cursor <= this->buffer_.size()) { this->cursor_ = cursor; } }
153            inline void setCursorToEnd()
154                { this->cursor_ = this->buffer_.size(); }
155            inline void setCursorToBegin()
156                { this->cursor_ = 0; }
157            inline void increaseCursor()
158                { if (this->cursor_ < this->buffer_.size()) { ++this->cursor_; } }
159            inline void decreaseCursor()
160                { if (this->cursor_ > 0) { --this->cursor_; } }
161
162        private:
163            bool charIsAllowed(const char& input);
164
165            void keyPressed (const KeyEvent& evt);
166            void keyReleased(const KeyEvent& evt) { }
167            void keyHeld    (const KeyEvent& evt);
168            void processKey (const KeyEvent &e);
169
170            void tickInput(float dt, const HandlerState& state);
171
172            std::string buffer_;
173            std::list<BaseInputBufferListenerTuple*> listeners_;
174            std::string allowedChars_;
175            unsigned int cursor_;
176
177            KeyCode::Enum lastKey_;
178            float timeSinceKeyPressed_;
179            float timeSinceKeyRepeated_;
180            int keysToRepeat_;
181
182            float keyRepeatDeleay_;
183            float keyRepeatTime_;
184    };
185}
186
187#endif /* _InputBuffer_H__ */
Note: See TracBrowser for help on using the repository browser.