Index: code/branches/input/src/core/input/InputManager.h
===================================================================
--- code/branches/input/src/core/input/InputManager.h	(revision 1630)
+++ code/branches/input/src/core/input/InputManager.h	(revision 1637)
@@ -41,4 +41,5 @@
 #include <map>
 #include <vector>
+#include <stack>
 #include "util/Math.h"
 #include "core/OrxonoxClass.h"
@@ -68,16 +69,4 @@
     };
 
-    /**
-    @brief
-        Struct for storing a custom input state
-    */
-    struct StoredState
-    {
-        std::vector<KeyHandler*>                    activeKeyHandlers_;
-        std::vector<MouseHandler*>                  activeMouseHandlers_;
-        std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_;
-        std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_;
-    };
-
     struct JoyStickCalibration
     {
@@ -95,25 +84,6 @@
         public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener
     {
-    public: // enumerations
-        /**
-        @brief
-            Designates the way input is handled and redirected.
-        */
-        enum InputState
-        {
-            IS_UNINIT,    //!< InputManager has not yet been initialised.
-            IS_NONE,      //!< Input is discarded.
-            IS_NORMAL,    //!< Normal play state. Key and button bindings are active.
-            IS_GUI,       //!< All OIS input events are passed to CEGUI.
-            IS_CONSOLE,   //!< Keyboard input is redirected to the InputBuffer.
-            IS_DETECT,    //!< All the input additionally goes to the KeyDetector
-            IS_NODETECT,  //!< remove KeyDetector
-            IS_NOCALIBRATE,
-            IS_CALIBRATE,
-            IS_CUSTOM     //!< Any possible configuration.
-        };
-
-    public: // member functions
-        void setConfigValues();
+        // --> setConfigValues is private
+        friend ClassIdentifier<InputManager>;
 
     public: // static functions
@@ -139,7 +109,4 @@
         static void setWindowExtents(const int width, const int height);
 
-        static void setInputState(const InputState state);
-        static InputState getInputState();
-
         static void storeKeyStroke(const std::string& name);
         static void keyBind(const std::string& command);
@@ -149,24 +116,12 @@
         static void tick(float dt);
 
-        static bool addKeyHandler                 (KeyHandler* handler, const std::string& name);
-        static bool removeKeyHandler              (const std::string& name);
-        static KeyHandler* getKeyHandler          (const std::string& name);
-        static bool enableKeyHandler              (const std::string& name);
-        static bool disableKeyHandler             (const std::string& name);
-        static bool isKeyHandlerActive            (const std::string& name);
-
-        static bool addMouseHandler               (MouseHandler* handler, const std::string& name);
-        static bool removeMouseHandler            (const std::string& name);
-        static MouseHandler* getMouseHandler      (const std::string& name);
-        static bool enableMouseHandler            (const std::string& name);
-        static bool disableMouseHandler           (const std::string& name);
-        static bool isMouseHandlerActive          (const std::string& name);
-
-        static bool addJoyStickHandler            (JoyStickHandler* handler, const std::string& name);
-        static bool removeJoyStickHandler         (const std::string& name);
-        static JoyStickHandler* getJoyStickHandler(const std::string& name);
-        static bool enableJoyStickHandler         (const std::string& name, unsigned int id);
-        static bool disableJoyStickHandler        (const std::string& name, unsigned int id);
-        static bool isJoyStickHandlerActive       (const std::string& name, unsigned int id);
+        static SimpleInputState*   createSimpleInputState  (const std::string& name, int priority);
+        static ExtendedInputState* createExtendedInputState(const std::string& name, int priority);
+        static bool destroyState (const std::string& name);
+        //static bool removeState (const std::string& name);
+        static InputState* getState       (const std::string& name);
+        static InputState* getCurrentState();
+        static bool requestEnterState     (const std::string& name);
+        static bool requestLeaveState     (const std::string& name);
 
     private: // functions
@@ -181,4 +136,5 @@
         bool _initialiseMouse();
         bool _initialiseJoySticks();
+        void _redimensionLists();
 
         void _destroy();
@@ -186,9 +142,5 @@
         void _destroyMouse();
         void _destroyJoySticks();
-
-        void _updateTickables();
-
-        void _saveState();
-        void _restoreState();
+        void _destroyState(InputState* state);
 
         void _completeCalibration();
@@ -198,4 +150,7 @@
 
         void _tick(float dt);
+
+        void _updateActiveStates();
+        bool _configureInputState(InputState* state, const std::string& name, int priority);
 
         // input events
@@ -210,8 +165,8 @@
         bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
         bool povMoved      (const OIS::JoyStickEvent &arg, int id);
-        //bool vector3Moved  (const OIS::JoyStickEvent &arg, int id);
-
-        static InputManager& _getSingleton();
-        static InputManager* _getSingletonPtr() { return &_getSingleton(); }
+
+        void setConfigValues();
+
+        static InputManager& _getInstance();
 
     private: // variables
@@ -221,15 +176,20 @@
         std::vector<OIS::JoyStick*>                 joySticks_;       //!< OIS joy sticks
         unsigned int                                joySticksSize_;
-
-        KeyBinder*                                  keyBinder_;       //!< KeyBinder instance
-        KeyDetector*                                keyDetector_;     //!< KeyDetector instance
-        InputBuffer*                                buffer_;          //!< InputBuffer instance
-        CalibratorCallback*                         calibratorCallback_;
-
-        InputState state_;
-        InputState stateRequest_;
-        InputState savedState_;
-        unsigned int keyboardModifiers_;
-        StoredState savedHandlers_;
+        unsigned int                                devicesNum_;
+
+        // some internally handled states
+        SimpleInputState*                                 stateDetector_;   //!< KeyDetector instance
+        SimpleInputState*                                 stateCalibrator_;
+        SimpleInputState*                                 stateEmpty_;
+
+        std::map<std::string, InputState*>          inputStatesByName_;
+        std::map<int, InputState*>                  inputStatesByPriority_;
+
+        std::vector<InputState*> stateEnterRequests_;                  //!< Request to enter a new state
+        std::vector<InputState*> stateLeaveRequests_;                    //!< Request to leave the current state
+
+        std::map<int, InputState*> activeStates_;
+        std::vector<InputState*>   activeStatesTop_;    //!< Current input states for joy stick events.
+        std::vector<InputState*>   activeStatesTicked_;    //!< Current input states for joy stick events.
 
         // joystick calibration
@@ -239,5 +199,7 @@
         int marginalsMin_[24];
         bool bCalibrated_;
-
+        bool bCalibrating_;
+
+        unsigned int keyboardModifiers_;           //!< Bit mask representing keyboard modifiers
         //! Keeps track of the joy stick POV states
         std::vector<POVStates>                      povStates_;
@@ -246,13 +208,4 @@
         std::vector<JoyStickCalibration>            joySticksCalibration_;
 
-        std::map<std::string, KeyHandler*>          keyHandlers_;
-        std::map<std::string, MouseHandler*>        mouseHandlers_;
-        std::map<std::string, JoyStickHandler*>     joyStickHandlers_;
-
-        std::vector<KeyHandler*>                    activeKeyHandlers_;
-        std::vector<MouseHandler*>                  activeMouseHandlers_;
-        std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_;
-        std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_;
-
         std::vector<Key>                            keysDown_;
         std::vector<MouseButton::Enum>              mouseButtonsDown_;
