Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 19, 2009, 5:31:02 PM (15 years ago)
Author:
rgrieder
Message:

Merged all remaining revisions from core4 back to the trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/core/input/InputState.h

    r3196 r3327  
    2727 */
    2828
    29 /**
    30 @file
    31 @brief
    32 */
    33 
    3429#ifndef _InputState_H__
    3530#define _InputState_H__
    3631
    37 #include "core/CorePrereqs.h"
    38 
     32#include "InputPrereqs.h"
     33
     34#include <cassert>
    3935#include <string>
    4036#include <vector>
    41 #include "InputInterfaces.h"
     37
     38#include "util/OrxEnum.h"
     39#include "InputHandler.h"
     40#include "JoyStickQuantityListener.h"
    4241
    4342namespace orxonox
    4443{
    45     class _CoreExport InputState
     44    //! Enumeration wrapper for input state priorities
     45    struct InputStatePriority : OrxEnum<InputStatePriority>
     46    {
     47        OrxEnumConstructors(InputStatePriority);
     48
     49        static const int Empty        = -1;
     50        static const int Dynamic      = 0;
     51
     52        static const int HighPriority = 1000;
     53        static const int Console      = HighPriority + 0;
     54        static const int Calibrator   = HighPriority + 1;
     55        static const int Detector     = HighPriority + 2;
     56    };
     57
     58    /**
     59    @brief
     60        InputStates allow you to customise the input event targets at runtime.
     61
     62        The general idea is a stack: Every activated InputState will be pushed on
     63        that stack and only the top one gets the input events. This is done for
     64        every device (keyboard, mouse, all joy sticks) separately to allow
     65        for intance keyboard input capturing for the console while you can still
     66        steer a ship with the mouse.
     67        There are two exceptions to this behaviour though:
     68        - If an InputState is created with the 'Transparent' parameter on, the
     69          state will not prevent input from getting to the state below it on the stack.
     70          This can be useful for instance if you need to deploy input to multiple
     71          handlers: Simply create two InputStates and make the high priority one transparent.
     72        - If an InputState is created with the 'AlwaysGetsInput' parameter on, then
     73          the state will always receive input as long as it is activated.
     74        - Note: If you mark an InputState with both parameters on, then it will
     75          not influence ony other InputState at all.
     76
     77        Priorities
     78        **********
     79        Every InputState has a priority when on the stack, but mostly this
     80        priority is dynamic (InputStatePriority::Dynamic) which means that a state
     81        pushed onto the stack will simply have a higher priority than the top one.
     82        This behaviour really only applies to normal states that don't have
     83        a high priority (InputStatePriority::HighPriority). These 'special' ones
     84        are used for features like the KeyDetector or the console. Use with care!
     85    */
     86    class _CoreExport InputState : public JoyStickQuantityListener
    4687    {
    4788        friend class InputManager;
    4889
     90        //! Marks the index in the handler vector for the keyboard handler
     91        static const InputDeviceEnumerator::Value keyboardIndex_s = InputDeviceEnumerator::Keyboard;
     92        //! Marks the index in the handler vector for the mouse handler
     93        static const InputDeviceEnumerator::Value mouseIndex_s = InputDeviceEnumerator::Mouse;
     94        //! Marks the index in the handler vector for the first joy stick handler
     95        static const InputDeviceEnumerator::Value firstJoyStickIndex_s = InputDeviceEnumerator::FirstJoyStick;
     96
    4997    public:
     98        //! Sets the keyboard event handler (overwrites if there already was one!)
     99        void setKeyHandler     (InputHandler* handler)
     100            { handlers_[keyboardIndex_s] = handler; bExpired_ = true; }
     101        //! Sets the mouse event handler (overwrites if there already was one!)
     102        void setMouseHandler   (InputHandler* handler)
     103            { handlers_[mouseIndex_s]    = handler; bExpired_ = true; }
     104        /**
     105        @brief
     106            Sets the joy stick event handler for one specific joy stick (overwrites if there already was one!)
     107        @return
     108            Returns false if the specified device was not found
     109        */
     110        bool setJoyStickHandler(InputHandler* handler, unsigned int joyStick);
     111        //! Sets the joy stick event handler for all joy sticks (overwrites if there already was one!)
     112        void setJoyStickHandler(InputHandler* handler);
     113        //! Sets an InputHandler to be used for all devices
     114        void setHandler        (InputHandler* handler);
     115
     116        //! Returns the name of the state (which is unique!)
    50117        const std::string& getName() const { return name_; }
     118        //! Returns the priority of the state (which is unique if != 0)
    51119        int getPriority()            const { return priority_; }
    52120
    53         bool isInputDeviceEnabled(unsigned int device)
     121        //! Tells whether there a handler installed for a specific device
     122        bool isInputDeviceEnabled(unsigned int device);
     123
     124        //! Returns true if the handler situation has changed
     125        bool hasExpired()      { return this->bExpired_; }
     126        //! Call this if you have applied the changes resulting from changed handlers
     127        void resetExpiration() { bExpired_ = false; }
     128
     129        //! Updates one specific device handler with #device#Updated
     130        void update(float dt, unsigned int device);
     131        //! Updates all handlers with allDevicesUpdated
     132        void update(float dt);
     133
     134        //! Generic function that distributes all 9 button events
     135        template <typename EventType, class Traits>
     136        void buttonEvent(unsigned int device, const typename Traits::ButtonTypeParam button);
     137
     138        //! Event handler
     139        void mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize);
     140        //! Event handler
     141        void mouseScrolled(int abs, int rel);
     142        //! Event handler
     143        void joyStickAxisMoved(unsigned int device, unsigned int axis, float value);
     144
     145        // Functors
     146        //! Called when the state is being activated (even if it doesn't get any events afterwards!)
     147        void entered();
     148        //! Called upon deactivation of the state
     149        void left();
     150        //! Sets a functor to be called upon activation of the state
     151        void setEnterFunctor(Functor* functor) { this->enterFunctor_ = functor; }
     152        //! Sets a functor to be called upon deactivation of the state
     153        void setLeaveFunctor(Functor* functor) { this->leaveFunctor_ = functor; }
     154
     155    private:
     156        InputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority);
     157        ~InputState() { }
     158
     159        void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList);
     160
     161        //! Sets the priority (only to be used by the InputManager!)
     162        void setPriority(int priority) { priority_ = priority; }
     163
     164        const std::string           name_;                  //!< Name of the state
     165        const bool                  bAlwaysGetsInput_;      //!< See class declaration for explanation
     166        const bool                  bTransparent_;          //!< See class declaration for explanation
     167        int                         priority_;              //!< Current priority (might change)
     168        bool                        bExpired_;              //!< See hasExpired()
     169        std::vector<InputHandler*>  handlers_;              //!< Vector with all handlers where the index is the device ID
     170        //! Handler to be used for all joy sticks (needs to be saved in case another joy stick gets attached)
     171        InputHandler*               joyStickHandlerAll_;
     172        Functor*                    enterFunctor_;          //!< Functor to be executed on enter
     173        Functor*                    leaveFunctor_;          //!< Functor to be executed on leave
     174    };
     175
     176    FORCEINLINE void InputState::update(float dt)
     177    {
     178        for (unsigned int i = 0; i < handlers_.size(); ++i)
     179            if (handlers_[i] != NULL)
     180                handlers_[i]->allDevicesUpdated(dt);
     181    }
     182
     183    FORCEINLINE void InputState::update(float dt, unsigned int device)
     184    {
     185        switch (device)
    54186        {
    55             if (device < bInputDeviceEnabled_.size())
    56                 return bInputDeviceEnabled_[device];
    57             else
    58                 return false;
     187        case InputDeviceEnumerator::Keyboard:
     188            if (handlers_[keyboardIndex_s] != NULL)
     189                handlers_[keyboardIndex_s]->keyboardUpdated(dt);
     190            break;
     191
     192        case InputDeviceEnumerator::Mouse:
     193            if (handlers_[mouseIndex_s] != NULL)
     194                handlers_[mouseIndex_s]->mouseUpdated(dt);
     195            break;
     196
     197        default: // joy sticks
     198            if (handlers_[device] != NULL)
     199                handlers_[device]->joyStickUpdated(device - firstJoyStickIndex_s, dt);
     200            break;
    59201        }
    60 
    61         bool handlersChanged() { return this->bHandlersChanged_; }
    62         void resetHandlersChanged() { bHandlersChanged_ = false; }
    63 
    64         virtual void onEnter() = 0;
    65         virtual void onLeave() = 0;
    66 
    67         virtual void registerOnEnter(Executor* executor)      { executorOnEnter_ = executor; }
    68         virtual void unRegisterOnEnter()                      { executorOnEnter_ = 0; }
    69         virtual void registerOnLeave(Executor* executor)      { executorOnLeave_ = executor; }
    70         virtual void unRegisterOnLeave()                      { executorOnLeave_ = 0; }
    71 
    72         virtual void updateInput(float dt, unsigned int device) = 0;
    73         virtual void updateInput(float dt) = 0;
    74 
    75         virtual void keyPressed (const KeyEvent& evt) = 0;
    76         virtual void keyReleased(const KeyEvent& evt) = 0;
    77         virtual void keyHeld    (const KeyEvent& evt) = 0;
    78 
    79         virtual void mouseButtonPressed (MouseButtonCode::ByEnum id) = 0;
    80         virtual void mouseButtonReleased(MouseButtonCode::ByEnum id) = 0;
    81         virtual void mouseButtonHeld    (MouseButtonCode::ByEnum id) = 0;
    82         virtual void mouseMoved         (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) = 0;
    83         virtual void mouseScrolled      (int abs, int rel) = 0;
    84 
    85         virtual void joyStickButtonPressed (unsigned int joyStickID, JoyStickButtonCode::ByEnum id) = 0;
    86         virtual void joyStickButtonReleased(unsigned int joyStickID, JoyStickButtonCode::ByEnum id) = 0;
    87         virtual void joyStickButtonHeld    (unsigned int joyStickID, JoyStickButtonCode::ByEnum id) = 0;
    88         virtual void joyStickAxisMoved     (unsigned int joyStickID, unsigned int axis, float value) = 0;
    89 
    90     protected:
    91         InputState()
    92             : bHandlersChanged_(false)
    93             , executorOnEnter_(0)
    94             , executorOnLeave_(0)
    95             , priority_(0)
    96             , bAlwaysGetsInput_(false)
    97             , bTransparent_(false)
    98         { }
    99         virtual ~InputState() { }
    100 
    101         virtual void numberOfJoySticksChanged(unsigned int n) = 0;
    102         void setInputDeviceEnabled(unsigned int device, bool bEnabled)
    103         {
    104             if (device < bInputDeviceEnabled_.size())
    105                 bInputDeviceEnabled_[device] = bEnabled;
    106         }
    107 
    108         bool bHandlersChanged_;
    109         Executor*                                   executorOnEnter_;
    110         Executor*                                   executorOnLeave_;
    111 
    112     private:
    113         void setNumOfJoySticks(unsigned int n)
    114         {
    115             bInputDeviceEnabled_.resize(n + 2);
    116             numberOfJoySticksChanged(n);
    117         }
    118         void setName(const std::string& name)  { name_ = name; }
    119         void setPriority(int priority)         { priority_ = priority; }
    120 
    121         std::string                                 name_;
    122         int                                         priority_;
    123         std::vector<bool>                           bInputDeviceEnabled_;
    124         bool                                        bAlwaysGetsInput_;
    125         bool                                        bTransparent_;
    126     };
     202    }
     203
     204    template <typename EventType, class Traits>
     205    FORCEINLINE void InputState::buttonEvent(unsigned int device, const typename Traits::ButtonTypeParam button)
     206    {
     207        assert(device < handlers_.size());
     208        if (handlers_[device] != NULL)
     209            handlers_[device]->buttonEvent(device, button, EventType());
     210    }
     211
     212    FORCEINLINE void InputState::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
     213    {
     214        if (handlers_[mouseIndex_s] != NULL)
     215            handlers_[mouseIndex_s]->mouseMoved(abs, rel, clippingSize);
     216    }
     217
     218    FORCEINLINE void InputState::mouseScrolled(int abs, int rel)
     219    {
     220        if (handlers_[mouseIndex_s] != NULL)
     221            handlers_[mouseIndex_s]->mouseScrolled(abs, rel);
     222    }
     223
     224    FORCEINLINE void InputState::joyStickAxisMoved(unsigned int device, unsigned int axis, float value)
     225    {
     226        assert(device < handlers_.size());
     227        if (handlers_[device] != NULL)
     228            handlers_[device]->axisMoved(device - firstJoyStickIndex_s, axis, value);
     229    }
    127230}
    128231
Note: See TracChangeset for help on using the changeset viewer.