Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/input/InputBuffer.h @ 2814

Last change on this file since 2814 was 2800, checked in by rgrieder, 15 years ago

Renaming "tick" to "update" for all those classes not inheriting from Tickable to avoid confusions.
GameState::ticked still exists, but that's going to change anyway.

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