Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 12, 2009, 4:12:04 PM (15 years ago)
Author:
rgrieder
Message:

Added a few more generic parts to the input library:

  • Created Mouse and Keyboard to join JoyStick and provided them with a templated base class (InputDeviceTemplated) that does most of the work (reduces quite some redundancy)
  • Created InputPrereqs.h from InputInterfaces.h and destroyed the latter
  • Exported InputHandler to its own file and replaced KeyHandler, MouseHandler and JoyStickHandler with the single InputHandler.
  • Deleted the SimpleInputState: There is only one class now which fulfills all our needs.

In general there is now less code and the code itself has more 'pluses'. However I haven't really thrown away any feature at all.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core4/src/core/input/InputState.h

    r3270 r3274  
    2727 */
    2828
    29 /**
    30 @file
    31 @brief
    32 */
    33 
    3429#ifndef _InputState_H__
    3530#define _InputState_H__
    3631
    37 #include "core/CorePrereqs.h"
     32#include "InputPrereqs.h"
    3833
     34#include <cassert>
    3935#include <string>
    4036#include <vector>
    41 #include "InputInterfaces.h"
    42 #include "JoyStickDeviceNumberListener.h"
     37
     38#include "InputHandler.h"
     39#include "JoyStickQuantityListener.h"
    4340
    4441namespace orxonox
    4542{
    46     class _CoreExport InputState : public JoyStickDeviceNumberListener
     43    class _CoreExport InputState : public JoyStickQuantityListener
    4744    {
    4845        friend class InputManager;
    4946
     47        static const InputDeviceEnumerator::Value keyboardIndex_s      = InputDeviceEnumerator::Keyboard;
     48        static const InputDeviceEnumerator::Value mouseIndex_s         = InputDeviceEnumerator::Mouse;
     49        static const InputDeviceEnumerator::Value firstJoyStickIndex_s = InputDeviceEnumerator::FirstJoyStick;
     50
    5051    public:
     52        void setKeyHandler     (InputHandler* handler)
     53            { handlers_[keyboardIndex_s] = handler; bExpired_ = true; }
     54        void setMouseHandler   (InputHandler* handler)
     55            { handlers_[mouseIndex_s]    = handler; bExpired_ = true; }
     56        bool setJoyStickHandler(InputHandler* handler, unsigned int joyStick);
     57        bool setJoyStickHandler(InputHandler* handler);
     58        bool setHandler        (InputHandler* handler);
     59
    5160        const std::string& getName() const { return name_; }
    5261        int getPriority()            const { return priority_; }
    5362
    54         bool isInputDeviceEnabled(unsigned int device)
    55         {
    56             if (device < bInputDeviceEnabled_.size())
    57                 return bInputDeviceEnabled_[device];
    58             else
    59                 return false;
    60         }
     63        bool isInputDeviceEnabled(unsigned int device);
    6164
    62         bool handlersChanged() { return this->bHandlersChanged_; }
    63         void resetHandlersChanged() { bHandlersChanged_ = false; }
     65        bool hasExpired()      { return this->bExpired_; }
     66        void resetExpiration() { bExpired_ = false; }
    6467
    65         virtual void onEnter() = 0;
    66         virtual void onLeave() = 0;
     68        void update(float dt, unsigned int device);
     69        void update(float dt);
    6770
    68         virtual void registerOnEnter(Executor* executor)      { executorOnEnter_ = executor; }
    69         virtual void unRegisterOnEnter()                      { executorOnEnter_ = 0; }
    70         virtual void registerOnLeave(Executor* executor)      { executorOnLeave_ = executor; }
    71         virtual void unRegisterOnLeave()                      { executorOnLeave_ = 0; }
     71        template <typename EventType, class Traits>
     72        void buttonEvent(unsigned int device, const typename Traits::ButtonTypeParam button);
    7273
    73         virtual void updateInput(float dt, unsigned int device) = 0;
    74         virtual void updateInput(float dt) = 0;
     74        void mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize);
     75        void mouseScrolled(int abs, int rel);
     76        void joyStickAxisMoved(unsigned int device, unsigned int axis, float value);
    7577
    76         virtual void keyPressed (const KeyEvent& evt) = 0;
    77         virtual void keyReleased(const KeyEvent& evt) = 0;
    78         virtual void keyHeld    (const KeyEvent& evt) = 0;
    79 
    80         virtual void mouseButtonPressed (MouseButtonCode::ByEnum id) = 0;
    81         virtual void mouseButtonReleased(MouseButtonCode::ByEnum id) = 0;
    82         virtual void mouseButtonHeld    (MouseButtonCode::ByEnum id) = 0;
    83         virtual void mouseMoved         (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) = 0;
    84         virtual void mouseScrolled      (int abs, int rel) = 0;
    85 
    86         virtual void joyStickButtonPressed (unsigned int joyStickID, JoyStickButtonCode::ByEnum id) = 0;
    87         virtual void joyStickButtonReleased(unsigned int joyStickID, JoyStickButtonCode::ByEnum id) = 0;
    88         virtual void joyStickButtonHeld    (unsigned int joyStickID, JoyStickButtonCode::ByEnum id) = 0;
    89         virtual void joyStickAxisMoved     (unsigned int joyStickID, unsigned int axis, float value) = 0;
    90 
    91     protected:
    92         InputState()
    93             : bHandlersChanged_(false)
    94             , executorOnEnter_(0)
    95             , executorOnLeave_(0)
    96             , priority_(0)
    97             , bAlwaysGetsInput_(false)
    98             , bTransparent_(false)
    99         { }
    100         virtual ~InputState() { }
    101 
    102         virtual void numberOfJoySticksChanged(unsigned int n) = 0;
    103         void setInputDeviceEnabled(unsigned int device, bool bEnabled)
    104         {
    105             if (device < bInputDeviceEnabled_.size())
    106                 bInputDeviceEnabled_[device] = bEnabled;
    107         }
    108 
    109         bool bHandlersChanged_;
    110         Executor*                                   executorOnEnter_;
    111         Executor*                                   executorOnLeave_;
     78        // Functors
     79        void entered();
     80        void left();
     81        void setEnterFunctor(Functor* functor) { this->enterFunctor_ = functor; }
     82        void setLeaveFunctor(Functor* functor) { this->leaveFunctor_ = functor; }
    11283
    11384    private:
    114         void JoyStickDeviceNumberChanged(unsigned int n)
     85        InputState();
     86        ~InputState() { }
     87
     88        void JoyStickQuantityChanged(unsigned int n);
     89
     90        void setName(const std::string& name) { name_ = name; }
     91        void setPriority(int priority)        { priority_ = priority; }
     92
     93        std::string                 name_;
     94        int                         priority_;
     95        bool                        bAlwaysGetsInput_;
     96        bool                        bTransparent_;
     97        bool                        bExpired_;
     98        std::vector<InputHandler*>  handlers_;
     99        InputHandler*               joyStickHandlerAll_;
     100        Functor*                    enterFunctor_;
     101        Functor*                    leaveFunctor_;
     102    };
     103
     104    inline void InputState::update(float dt)
     105    {
     106        for (unsigned int i = 0; i < handlers_.size(); ++i)
     107            if (handlers_[i] != NULL)
     108                handlers_[i]->allDevicesUpdated(dt);
     109    }
     110
     111    inline void InputState::update(float dt, unsigned int device)
     112    {
     113        switch (device)
    115114        {
    116             bInputDeviceEnabled_.resize(n + 2);
    117             numberOfJoySticksChanged(n);
     115        case InputDeviceEnumerator::Keyboard:
     116            if (handlers_[keyboardIndex_s] != NULL)
     117                handlers_[keyboardIndex_s]->keyboardUpdated(dt);
     118            break;
     119
     120        case InputDeviceEnumerator::Mouse:
     121            if (handlers_[mouseIndex_s] != NULL)
     122                handlers_[mouseIndex_s]->mouseUpdated(dt);
     123            break;
     124
     125        default: // joy sticks
     126            if (handlers_[device] != NULL)
     127                handlers_[device]->joyStickUpdated(device - firstJoyStickIndex_s, dt);
     128            break;
    118129        }
    119         void setName(const std::string& name)  { name_ = name; }
    120         void setPriority(int priority)         { priority_ = priority; }
     130    }
    121131
    122         std::string                                 name_;
    123         int                                         priority_;
    124         std::vector<bool>                           bInputDeviceEnabled_;
    125         bool                                        bAlwaysGetsInput_;
    126         bool                                        bTransparent_;
    127     };
     132    template <typename EventType, class Traits>
     133    inline void InputState::buttonEvent(unsigned int device, const typename Traits::ButtonTypeParam button)
     134    {
     135        assert(device < handlers_.size());
     136        if (handlers_[device] != NULL)
     137            handlers_[device]->buttonEvent(device, button, EventType());
     138    }
     139
     140    inline void InputState::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
     141    {
     142        if (handlers_[mouseIndex_s] != NULL)
     143            handlers_[mouseIndex_s]->mouseMoved(abs, rel, clippingSize);
     144    }
     145
     146    inline void InputState::mouseScrolled(int abs, int rel)
     147    {
     148        if (handlers_[mouseIndex_s] != NULL)
     149            handlers_[mouseIndex_s]->mouseScrolled(abs, rel);
     150    }
     151
     152    inline void InputState::joyStickAxisMoved(unsigned int device, unsigned int axis, float value)
     153    {
     154        assert(device < handlers_.size());
     155        if (handlers_[device] != NULL)
     156            handlers_[device]->axisMoved(device - firstJoyStickIndex_s, axis, value);
     157    }
    128158}
    129159
Note: See TracChangeset for help on using the changeset viewer.