Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/input/InputManager.h @ 6106

Last change on this file since 6106 was 6084, checked in by rgrieder, 15 years ago
  • Fixed typing error in a cmake file causing a rebuild of the compilations every time CMake was run
  • Found spelling mistakes in InputManager.h
  • Found tabs in Test.cc
  • Property svn:eol-style set to native
File size: 8.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#ifndef _InputManager_H__
30#define _InputManager_H__
31
32#include "InputPrereqs.h"
33
34#include <map>
35#include <set>
36#include <string>
37#include <vector>
38
39#include "util/Singleton.h"
40#include "core/WindowEventListener.h"
41#include "InputState.h"
42
43namespace orxonox
44{
45    /**
46    @brief
47        Manages the input devices (mouse, keyboard, joy sticks) and the input states.
48
49        Every input device has its own wrapper class which does the actually input event
50        distribution. The InputManager only creates reloads (on request) those devices.
51
52        The other functionality concerns handling InputStates. They act as a layer
53        between InputHandlers (like the KeyBinder or the GUIManager) and InputDevices.
54        InputStates are memory managed by the IputManager. You cannot create or destroy
55        them on your own. Therefore all states get destroyed with the InputManager d'tor.
56    @note
57        - The actual lists containing all the InputStates for a specific device are stored
58          in the InputDevices themselves.
59        - The devices_ vector always has at least two elements: Keyboard (first) and mouse.
60          You best access them internally with InputDeviceEnumerator::Keyboard/Mouse
61          The first joy stick is accessed with InputDeviceEnumerator::FirstJoyStick.
62        - Keyboard construction is mandatory , mouse and joy sticks are not.
63          If the OIS::InputManager or the Keyboard fail, an exception is thrown.
64    */
65    class _CoreExport InputManager : public Singleton<InputManager>, public WindowEventListener
66    {
67        friend class Singleton<InputManager>;
68    public:
69        //! Represents internal states of the InputManager.
70        enum State
71        {
72            Nothing       = 0x00,
73            Bad           = 0x02,
74            Ticking       = 0x04,
75            Calibrating   = 0x08,
76            ReloadRequest = 0x10,
77        };
78
79        /**
80        @brief
81            Loads the devices and initialises the KeyDetector and the Calibrator.
82           
83            If either the OIS input system and/or the keyboard could not be created,
84            the constructor fails with an std::exception.
85        */
86        InputManager();
87        //! Destroys all devices AND all input states!
88        ~InputManager();
89        void setConfigValues();
90
91        /**
92        @brief
93            Updates the devices (which distribute the input events) and the input states.
94
95            Any InpuStates changes (destroy, enter, leave) and happens here. If a reload request
96            was submitted while updating, the request will be postponed until the next update call.
97        */
98        void update(const Clock& time);
99        //! Clears all input device buffers. This usually only includes the pressed button list.
100        void clearBuffers();
101        //! Starts joy stick calibration.
102        void calibrate();
103        /**
104        @brief
105            Reloads all the input devices. Use this method to initialise new joy sticks.
106        @note
107            Only reloads immediately if the call stack doesn't include the update() method.
108        */
109        void reload();
110
111        //-------------------------------
112        // Input States
113        //-------------------------------
114        /**
115        @brief
116            Creates a new InputState that gets managed by the InputManager.
117        @remarks
118            The InputManager will take care of the state completely. That also
119            means it gets deleted when the InputManager is destroyed!
120        @param name
121            Unique name of the InputState when referenced as string
122        @param priority
123            Priority matters when multiple states are active. You can specify any
124            number, but 1 - 99 is preferred (99 means high priority).
125        */
126        InputState* createInputState(const std::string& name, bool bAlwaysGetsInput = false, bool bTransparent = false, InputStatePriority priority = InputStatePriority::Dynamic);
127        /**
128        @brief
129            Returns a pointer to a InputState referenced by name.
130        @return
131            Returns NULL if state was not found.
132        */
133        InputState* getState(const std::string& name);
134        /**
135        @brief
136            Activates a specific input state.
137            It might not actually be activated if the priority is too low!
138        @return
139            False if name was not found, true otherwise.
140        */
141        bool enterState(const std::string& name);
142        /**
143        @brief
144            Deactivates a specific input state.
145        @return
146            False if name was not found, true otherwise.
147        */
148        bool leaveState(const std::string& name);
149        /**
150        @brief
151            Removes and destroys an input state.
152        @return
153            True if removal was successful, false if name was not found.
154        @remarks
155            - You can't remove the internal states "empty", "calibrator" and "detector".
156            - The removal process is being postponed if InputManager::update() is currently running.
157        */
158        bool destroyState(const std::string& name);
159
160        //-------------------------------
161        // Various getters and setters
162        //-------------------------------
163        //! Returns the number of joy stick that have been created since the c'tor or last call to reload().
164        unsigned int getJoyStickQuantity() const
165            { return devices_.size() - InputDeviceEnumerator::FirstJoyStick; }
166        //! Returns a pointer to the OIS InputManager. Only you if you know what you're doing!
167        OIS::InputManager* getOISInputManager() { return this->oisInputManager_; }
168        std::pair<int, int> getMousePosition() const;
169
170    private: // functions
171        // don't mess with a Singleton
172        InputManager(const InputManager&);
173
174        // Internal methods
175        void loadDevices();
176        void loadMouse();
177        void loadJoySticks();
178        void destroyDevices();
179
180        void stopCalibration();
181        void reloadInternal();
182
183        void destroyStateInternal(InputState* state);
184        void updateActiveStates();
185
186        // From WindowEventListener
187        void windowFocusChanged();
188
189    private: // variables
190        State                               internalState_;        //!< Current internal state
191        OIS::InputManager*                  oisInputManager_;      //!< OIS input manager
192        std::vector<InputDevice*>           devices_;              //!< List of all input devices (keyboard, mouse, joy sticks)
193        MouseMode::Value                    mouseMode_;            //!< Currently applied mouse mode
194
195        // some internally handled states and handlers
196        InputState*                         emptyState_;           //!< Lowest priority states (makes handling easier)
197        //! InputBuffer that reacts to the Enter key when calibrating the joy sticks
198        InputBuffer*                        calibratorCallbackHandler_;
199
200        std::map<std::string, InputState*>  statesByName_;         //!< Contains all the created input states by name
201        std::map<int, InputState*>          activeStates_;         //!< Contains all active input states by priority (std::map is sorted!)
202        std::vector<InputState*>            activeStatesTicked_;   //!< Like activeStates_, but only contains the ones that currently receive events
203
204        std::set<InputState*>               stateEnterRequests_;   //!< Requests to enter a new state
205        std::set<InputState*>               stateLeaveRequests_;   //!< Requests to leave a running state
206        std::set<InputState*>               stateDestroyRequests_; //!< Requests to destroy a state
207
208        static InputManager*                singletonPtr_s;        //!< Pointer reference to the singleton
209    };
210}
211
212#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.