Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 1, 2008, 3:54:20 PM (16 years ago)
Author:
rgrieder
Message:
  • @everyone: Do not create a branch until I've added the svn:eol-style property correctly. Otherwise this would cost me another 4 hours or so when we want to merge back.
  • merged network branch back to trunk
  • I had to omit the changes from last evening concerning the line endings
  • might not work yet because of the line endings
  • @beni: script branch is the only branch still open. you probably will have to apply a patch because of inconsistent new lines
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/core/InputBuffer.h

    r1349 r1502  
    3636
    3737#include "InputInterfaces.h"
    38 #include "Tickable.h"
     38#include "OrxonoxClass.h"
    3939
    4040namespace orxonox
    4141{
    42   class _CoreExport InputBufferListener
    43   {};
    44 
    45   class _CoreExport InputBuffer : public KeyHandler, public OrxonoxClass
    46   {
    47     struct InputBufferListenerTuple
     42    class BaseInputBufferListenerTuple
    4843    {
    49       InputBufferListener* listener_;
    50       void (InputBufferListener::*function_)();
    51       bool bListenToAllChanges_;
    52       bool bOnlySingleInput_;
    53       char char_;
     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 ~BaseInputBufferListenerTuple() { }
     51        virtual void callFunction() = 0;
     52        bool bListenToAllChanges_;
     53        bool bOnlySingleInput_;
     54        bool trueKeyFalseChar_;
     55        char char_;
     56        KeyCode::Enum key_;
    5457    };
    5558
    56   public:
    57     InputBuffer(const std::string allowedChars =
    58       "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜ0123456789 \\\"(){}[]<>.:,;_-+*/=!?|$&%^");
     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::Enum 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    };
    5976
    60     void setConfigValues();
     77    class _CoreExport InputBuffer : public KeyHandler, public OrxonoxClass
     78    {
     79        public:
     80            InputBuffer();
     81            InputBuffer(const std::string allowedChars);
    6182
    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     }
     83            void setConfigValues();
    7484
    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     }
     85            template <class T>
     86            void registerListener(T* listener, void (T::*function)(), bool bOnlySingleInput)
     87            {
     88                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned);
     89                this->listeners_.insert(this->listeners_.end(), newTuple);
     90            }
     91            template <class T>
     92            void registerListener(T* listener, void (T::*function)() const, bool bOnlySingleInput)
     93            {
     94                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, true, bOnlySingleInput, false, '\0', KeyCode::Unassigned);
     95                this->listeners_.insert(this->listeners_.end(), newTuple);
     96            }
     97            template <class T>
     98            void registerListener(T* listener, void (T::*function)(), char _char, bool bOnlySingleInput)
     99            {
     100                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned);
     101                this->listeners_.insert(this->listeners_.end(), newTuple);
     102            }
     103            template <class T>
     104            void registerListener(T* listener, void (T::*function)() const, char _char, bool bOnlySingleInput)
     105            {
     106                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, bOnlySingleInput, false, _char, KeyCode::Unassigned);
     107                this->listeners_.insert(this->listeners_.end(), newTuple);
     108            }
    87109
    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();
     110            template <class T>
     111            void registerListener(T* listener, void (T::*function)(), KeyCode::Enum key)
     112            {
     113                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, true, true, '\0', key);
     114                this->listeners_.insert(this->listeners_.end(), newTuple);
     115            }
     116            template <class T>
     117            void registerListener(T* listener, void (T::*function)() const, KeyCode::Enum key)
     118            {
     119                InputBufferListenerTuple<T>* newTuple = new InputBufferListenerTuple<T>(listener, (void (T::*)())function, false, true, true, '\0', key);
     120                this->listeners_.insert(this->listeners_.end(), newTuple);
     121            }
    93122
    94     void updated();
    95     void updated(const char& update, bool bSingleInput);
     123            template <class T>
     124            void unregisterListener(T* listener)
     125            {
     126                for (std::list<BaseInputBufferListenerTuple*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
     127                {
     128                    InputBufferListenerTuple<T>* refListener = dynamic_cast<InputBufferListenerTuple<T>*>(*it);
     129                    if (refListener && refListener->listener_ == listener)
     130                        this->listeners_.erase(it++);
     131                    else
     132                        it++;
     133                }
     134            }
    96135
    97     inline std::string get() const
    98     { return this->buffer_; }
     136            void set(const std::string& input, bool update = true);
     137            void insert(const std::string& input, bool update = true);
     138            void insert(const char& input, bool update = true);
     139            void clear(bool update = true);
     140            void removeAtCursor(bool update = true);
     141            void removeBehindCursor(bool update = true);
    99142
    100   private:  // functions
    101     bool charIsAllowed(const char& input);
     143            void updated();
     144            void updated(const char& update, bool bSingleInput);
    102145
    103     void keyPressed (const KeyEvent& evt);
    104     void keyReleased(const KeyEvent& evt) { }
    105     void keyHeld    (const KeyEvent& evt);
    106     void processKey (const KeyEvent &e);
     146            inline std::string get() const
     147                { return this->buffer_; }
     148            inline unsigned int getSize() const
     149                { return this->buffer_.size(); }
    107150
    108     void tick(float dt);
     151            inline unsigned int getCursorPosition() const
     152                { return this->cursor_; }
     153            inline void setCursorPosition(unsigned int cursor)
     154                { if (cursor <= this->buffer_.size()) { this->cursor_ = cursor; } }
     155            inline void setCursorToEnd()
     156                { this->cursor_ = this->buffer_.size(); }
     157            inline void setCursorToBegin()
     158                { this->cursor_ = 0; }
     159            inline void increaseCursor()
     160                { if (this->cursor_ < this->buffer_.size()) { ++this->cursor_; } }
     161            inline void decreaseCursor()
     162                { if (this->cursor_ > 0) { --this->cursor_; } }
    109163
    110   private: // variables
    111     std::string buffer_;
    112     std::list<InputBufferListenerTuple> listeners_;
    113     std::string allowedChars_;
     164        private:
     165            bool charIsAllowed(const char& input);
    114166
    115     KeyCode::Enum lastKey_;
    116     float timeSinceKeyPressed_;
    117     float timeSinceKeyRepeated_;
    118     int keysToRepeat_;
     167            void keyPressed (const KeyEvent& evt);
     168            void keyReleased(const KeyEvent& evt) { }
     169            void keyHeld    (const KeyEvent& evt);
     170            void processKey (const KeyEvent &e);
    119171
    120     // configured values
    121     float keyRepeatDeleay_;
    122     float keyRepeatTime_;
    123   };
     172            void tickInput(float dt, const HandlerState& state);
     173
     174            std::string buffer_;
     175            std::list<BaseInputBufferListenerTuple*> listeners_;
     176            std::string allowedChars_;
     177            unsigned int cursor_;
     178
     179            KeyCode::Enum lastKey_;
     180            float timeSinceKeyPressed_;
     181            float timeSinceKeyRepeated_;
     182            int keysToRepeat_;
     183
     184            float keyRepeatDeleay_;
     185            float keyRepeatTime_;
     186    };
    124187}
    125188
Note: See TracChangeset for help on using the changeset viewer.