Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 13, 2009, 10:43:59 PM (15 years ago)
Author:
rgrieder
Message:

Finally found a satisfying way to deal with interfaces that deliver information, but only upon virtual call.
The solution involves a static variable but any other (and uglier/hackier) solution will do so too.
I applied the method the the JoyStickQuantityListener so that the KeyBinder is now independent on the InputManager.

Location:
code/branches/core4/src/core/input
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core4/src/core/input/InputManager.cc

    r3286 r3288  
    260260
    261261        // inform all JoyStick Device Number Listeners
    262         for (ObjectList<JoyStickQuantityListener>::iterator it = ObjectList<JoyStickQuantityListener>::begin(); it; ++it)
    263             it->JoyStickQuantityChanged(devices_.size() - InputDeviceEnumerator::FirstJoyStick);
     262        std::vector<JoyStick*> joyStickList;
     263        for (unsigned int i = InputDeviceEnumerator::FirstJoyStick; i < devices_.size(); ++i)
     264            joyStickList.push_back(static_cast<JoyStick*>(devices_[i]));
     265        JoyStickQuantityListener::changeJoyStickQuantity(joyStickList);
    264266    }
    265267
     
    572574        if (statesByName_.find(name) == statesByName_.end())
    573575        {
    574             InputState* state = new InputState;
    575576            if (priority >= InputStatePriority::HighPriority || priority == InputStatePriority::Empty)
    576577            {
     
    583584                        COUT(2) << "Warning: Could not add an InputState with the same priority '"
    584585                            << static_cast<int>(priority) << "' != 0." << std::endl;
    585                         return false;
     586                        return 0;
    586587                    }
    587588                }
    588589            }
     590            InputState* state = new InputState(name, bAlwaysGetsInput, bTransparent, priority);
    589591            statesByName_[name] = state;
    590             state->JoyStickQuantityChanged(devices_.size() - InputDeviceEnumerator::FirstJoyStick);
    591             state->setName(name);
    592             state->bAlwaysGetsInput_ = bAlwaysGetsInput;
    593             state->bTransparent_ = bTransparent;
    594             if (priority >= InputStatePriority::HighPriority || priority == InputStatePriority::Empty)
    595                 state->setPriority(priority);
    596592
    597593            return state;
  • code/branches/core4/src/core/input/InputState.cc

    r3274 r3288  
    3232namespace orxonox
    3333{
    34     InputState::InputState()
    35         : priority_(0)
    36         , bAlwaysGetsInput_(false)
    37         , bTransparent_(false)
     34    InputState::InputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority)
     35        : name_(name)
     36        , bAlwaysGetsInput_(bAlwaysGetsInput)
     37        , bTransparent_(bTransparent)
    3838        , bExpired_(true)
    3939        , handlers_(2)
     
    4242        , leaveFunctor_(0)
    4343    {
     44        if (priority >= InputStatePriority::HighPriority || priority == InputStatePriority::Empty)
     45            priority_ = priority;
     46        else
     47            priority_ = 0;
     48
     49        handlers_.resize(InputDeviceEnumerator::FirstJoyStick + JoyStickQuantityListener::getJoyStickList().size(), NULL);
    4450    }
    4551
     
    5258    }
    5359
    54     void InputState::JoyStickQuantityChanged(unsigned int n)
     60    void InputState::JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList)
    5561    {
    5662        unsigned int oldSize = handlers_.size();
    57         handlers_.resize(InputDeviceEnumerator::FirstJoyStick + n, NULL);
     63        handlers_.resize(InputDeviceEnumerator::FirstJoyStick + joyStickList.size(), NULL);
    5864
    5965        for (unsigned int i = oldSize; i < handlers_.size(); ++i)
  • code/branches/core4/src/core/input/InputState.h

    r3279 r3288  
    9797
    9898    private:
    99         InputState();
     99        InputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority);
    100100        ~InputState() { }
    101101
    102         void JoyStickQuantityChanged(unsigned int n);
     102        void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList);
    103103
    104         void setName(const std::string& name) { name_ = name; }
    105         void setPriority(int priority)        { priority_ = priority; }
     104        void setPriority(int priority) { priority_ = priority; }
    106105
    107         std::string                 name_;
     106        const std::string           name_;
     107        const bool                  bAlwaysGetsInput_;
     108        const bool                  bTransparent_;
    108109        int                         priority_;
    109         bool                        bAlwaysGetsInput_;
    110         bool                        bTransparent_;
    111110        bool                        bExpired_;
    112111        std::vector<InputHandler*>  handlers_;
  • code/branches/core4/src/core/input/JoyStickQuantityListener.cc

    r3286 r3288  
    2828
    2929#include "JoyStickQuantityListener.h"
     30
    3031#include "core/CoreIncludes.h"
     32#include "core/ObjectList.h"
    3133
    3234namespace orxonox
    3335{
     36    std::vector<JoyStick*> JoyStickQuantityListener::joyStickList_s;
     37
    3438    JoyStickQuantityListener::JoyStickQuantityListener()
    3539    {
    3640        RegisterObject(JoyStickQuantityListener);
    3741    }
     42
     43    //! Calls all registered objects and sets the static variable
     44    /*static*/ void JoyStickQuantityListener::changeJoyStickQuantity(const std::vector<JoyStick*>& joyStickList)
     45    {
     46        joyStickList_s = joyStickList;
     47        for (ObjectList<JoyStickQuantityListener>::iterator it = ObjectList<JoyStickQuantityListener>::begin(); it; ++it)
     48            it->JoyStickQuantityChanged(joyStickList);
     49    }
    3850}
  • code/branches/core4/src/core/input/JoyStickQuantityListener.h

    r3286 r3288  
    4343    class _CoreExport JoyStickQuantityListener : virtual public OrxonoxClass
    4444    {
     45        friend InputManager;
    4546    public:
    4647        JoyStickQuantityListener();
    4748        virtual ~JoyStickQuantityListener() { }
    4849
    49         //! Called when joy sticks get added/removed
    50         virtual void JoyStickQuantityChanged(unsigned int value) = 0;
     50        //! Returns a list with all JoySticks currently loaded
     51        const std::vector<JoyStick*>& getJoyStickList() const { return joyStickList_s; }
     52
     53    private:
     54        //! Called whenever joy sticks get added/removed
     55        virtual void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList) = 0;
     56
     57        static void changeJoyStickQuantity(const std::vector<JoyStick*>& joyStickList);
     58
     59        //! Static variable that holds the latest distributed information
     60        static std::vector<JoyStick*> joyStickList_s;
    5161    };
    5262}
  • code/branches/core4/src/core/input/KeyBinder.cc

    r3279 r3288  
    4040#include "core/ConfigFileManager.h"
    4141#include "InputCommands.h"
    42 #include "InputManager.h"
    4342
    4443namespace orxonox
     
    103102
    104103        // initialise joy sticks separatly to allow for reloading
    105         numberOfJoySticks_ = InputManager::getInstance().getJoyStickQuantity();
     104        numberOfJoySticks_ = this->getJoyStickList().size();
    106105        initialiseJoyStickBindings();
    107106
     
    155154    }
    156155
    157     void KeyBinder::JoyStickQuantityChanged(unsigned int value)
     156    void KeyBinder::JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList)
    158157    {
    159158        unsigned int oldValue = numberOfJoySticks_;
    160         numberOfJoySticks_ = value;
     159        numberOfJoySticks_ = joyStickList.size();
    161160
    162161        // initialise joy stick bindings
  • code/branches/core4/src/core/input/KeyBinder.h

    r3274 r3288  
    7676        void buttonThresholdChanged();
    7777        // from JoyStickQuantityListener interface
    78         virtual void JoyStickQuantityChanged(unsigned int value);
     78        virtual void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList);
    7979        void initialiseJoyStickBindings();
    8080        void compilePointerLists();
  • code/branches/core4/src/core/input/KeyDetector.cc

    r3274 r3288  
    7373    }
    7474
    75     void KeyDetector::JoyStickQuantityChanged(unsigned int value)
     75    void KeyDetector::JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList)
    7676    {
    77         KeyBinder::JoyStickQuantityChanged(value);
     77        KeyBinder::JoyStickQuantityChanged(joyStickList);
    7878        setCallbackCommand(callbackCommand_);
    7979    }
  • code/branches/core4/src/core/input/KeyDetector.h

    r3274 r3288  
    4949        ~KeyDetector();
    5050        void setCallbackCommand(const std::string& command);
    51         void JoyStickQuantityChanged(unsigned int value);
     51        void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList);
    5252
    5353    private:
Note: See TracChangeset for help on using the changeset viewer.