Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates3/src/libraries/core/input/InputManager.h @ 10422

Last change on this file since 10422 was 6746, checked in by rgrieder, 15 years ago

Merged gamestates2 branch back to trunk.
This brings in some heavy changes in the GUI framework.
It should also fix problems with triggered asserts in the InputManager.

Note: PickupInventory does not seem to work —> Segfault when showing because before, the owner in GUIOverlay::setGUIName is already NULL.
I haven't tested it before, so I can't tell whether it's my changes.

  • Property svn:eol-style set to native
File size: 9.0 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#include <boost/function.hpp>
39
40#include "util/Singleton.h"
41#include "util/TriBool.h"
42#include "core/WindowEventListener.h"
43
44// tolua_begin
45namespace orxonox
46{
47    /**
48    @brief
49        Manages the input devices (mouse, keyboard, joy sticks) and the input states.
50
51        Every input device has its own wrapper class which does the actually input event
52        distribution. The InputManager only creates reloads (on request) those devices.
53
54        The other functionality concerns handling InputStates. They act as a layer
55        between InputHandlers (like the KeyBinder or the GUIManager) and InputDevices.
56        InputStates are memory managed by the IputManager. You cannot create or destroy
57        them on your own. Therefore all states get destroyed with the InputManager d'tor.
58    @note
59        - The actual lists containing all the InputStates for a specific device are stored
60          in the InputDevices themselves.
61        - The devices_ vector always has at least two elements: Keyboard (first) and mouse.
62          You best access them internally with InputDeviceEnumerator::Keyboard/Mouse
63          The first joy stick is accessed with InputDeviceEnumerator::FirstJoyStick.
64        - Keyboard construction is mandatory , mouse and joy sticks are not.
65          If the OIS::InputManager or the Keyboard fail, an exception is thrown.
66    */
67    class _CoreExport InputManager
68// tolua_end
69        : public Singleton<InputManager>, public WindowEventListener
70    { // tolua_export
71        friend class Singleton<InputManager>;
72    public:
73        //! Represents internal states of the InputManager.
74        enum State
75        {
76            Nothing       = 0x00,
77            Bad           = 0x02,
78            Calibrating   = 0x04,
79        };
80
81        /**
82        @brief
83            Loads the devices and initialises the KeyDetector and the Calibrator.
84
85            If either the OIS input system and/or the keyboard could not be created,
86            the constructor fails with an std::exception.
87        */
88        InputManager();
89        //! Destroys all devices AND all input states!
90        ~InputManager();
91        void setConfigValues();
92
93        /**
94        @brief
95            Updates the devices (which distribute the input events) and the input states.
96
97            Any InpuStates changes (destroy, enter, leave) and happens here. If a reload request
98            was submitted while updating, the request will be postponed until the next update call.
99        */
100        void preUpdate(const Clock& time);
101        //! Clears all input device buffers. This usually only includes the pressed button list.
102        void clearBuffers();
103        //! Starts joy stick calibration.
104        void calibrate();
105        /**
106        @brief
107            Reloads all the input devices. Use this method to initialise new joy sticks.
108        @note
109            Only reloads immediately if the call stack doesn't include the preUpdate() method.
110        */
111        void reload();
112
113        //-------------------------------
114        // Input States
115        //-------------------------------
116        /**
117        @brief
118            Creates a new InputState that gets managed by the InputManager.
119        @remarks
120            The InputManager will take care of the state completely. That also
121            means it gets deleted when the InputManager is destroyed!
122        @param name
123            Unique name of the InputState when referenced as string
124        @param priority
125            Priority matters when multiple states are active. You can specify any
126            number, but 1 - 99 is preferred (99 means high priority).
127        */
128        InputState* createInputState(const std::string& name, bool bAlwaysGetsInput = false, bool bTransparent = false, InputStatePriority priority = InputStatePriority::Dynamic);
129        /**
130        @brief
131            Returns a pointer to a InputState referenced by name.
132        @return
133            Returns NULL if state was not found.
134        */
135        InputState* getState(const std::string& name);
136        /**
137        @brief
138            Activates a specific input state.
139            It might not actually be activated if the priority is too low!
140        @return
141            False if name was not found, true otherwise.
142        */
143        bool enterState(const std::string& name); // tolua_export
144        /**
145        @brief
146            Deactivates a specific input state.
147        @return
148            False if name was not found, true otherwise.
149        */
150        bool leaveState(const std::string& name); // tolua_export
151        /**
152        @brief
153            Removes and destroys an input state.
154        @return
155            True if removal was successful, false if name was not found.
156        @remarks
157            - You can't remove the internal states "empty", "calibrator" and "detector".
158            - The removal process is being postponed if InputManager::preUpdate() is currently running.
159        */
160        bool destroyState(const std::string& name); // tolua_export
161
162        //-------------------------------
163        // Various getters and setters
164        //-------------------------------
165        //! Returns the number of joy stick that have been created since the c'tor or last call to reload().
166        unsigned int getJoyStickQuantity() const
167            { return devices_.size() - InputDeviceEnumerator::FirstJoyStick; }
168        //! Returns a pointer to the OIS InputManager. Only you if you know what you're doing!
169        OIS::InputManager* getOISInputManager() { return this->oisInputManager_; }
170        //! Returns the position of the cursor as std::pair of ints
171        std::pair<int, int> getMousePosition() const;
172        //! Tells whether the mouse is used exclusively to the game
173        bool isMouseExclusive() const { return this->exclusiveMouse_; } // tolua_export
174
175        //-------------------------------
176        // Function call caching
177        //-------------------------------
178        void pushCall(const boost::function<void ()>& function)
179            { this->callBuffer_.push_back(function); }
180
181        static InputManager& getInstance() { return Singleton<InputManager>::getInstance(); } // tolua_export
182
183    private: // functions
184        // don't mess with a Singleton
185        InputManager(const InputManager&);
186
187        // Internal methods
188        void loadDevices();
189        void loadMouse();
190        void loadJoySticks();
191        void destroyDevices();
192
193        void stopCalibration();
194        void reloadInternal();
195
196        void destroyStateInternal(InputState* state);
197        void updateActiveStates();
198
199        // From WindowEventListener
200        void windowFocusChanged();
201
202    private: // variables
203        State                               internalState_;        //!< Current internal state
204        OIS::InputManager*                  oisInputManager_;      //!< OIS input manager
205        std::vector<InputDevice*>           devices_;              //!< List of all input devices (keyboard, mouse, joy sticks)
206        TriBool::Value                      exclusiveMouse_;       //!< Currently applied mouse mode
207
208        // some internally handled states and handlers
209        InputState*                         emptyState_;           //!< Lowest priority states (makes handling easier)
210        //! InputBuffer that reacts to the Enter key when calibrating the joy sticks
211        InputBuffer*                        calibratorCallbackHandler_;
212
213        std::map<std::string, InputState*>  statesByName_;         //!< Contains all the created input states by name
214        std::map<int, InputState*>          activeStates_;         //!< Contains all active input states by priority (std::map is sorted!)
215        std::vector<InputState*>            activeStatesTicked_;   //!< Like activeStates_, but only contains the ones that currently receive events
216
217        std::vector<boost::function<void ()> > callBuffer_;        //!< Caches all calls from InputStates to be executed afterwards (see preUpdate)
218
219        static InputManager*                singletonPtr_s;        //!< Pointer reference to the singleton
220    }; // tolua_export
221} // tolua_export
222
223#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.