Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3825 was 3370, checked in by rgrieder, 15 years ago

Merged resource branch back to the trunk. Changes:

  • Automated graphics loading by evaluating whether a GameState requires it
  • Using native Tcl library (x3n)

Windows users: Update your dependency package!

  • Property svn:eol-style set to native
File size: 8.9 KB
RevLine 
[918]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1502]3 *                    > www.orxonox.net <
[918]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 */
[973]28
29#ifndef _InputManager_H__
30#define _InputManager_H__
31
[3327]32#include "InputPrereqs.h"
[1062]33
[1219]34#include <map>
[3196]35#include <set>
36#include <string>
[1293]37#include <vector>
[3196]38
[3370]39#include "util/Singleton.h"
[3327]40#include "core/WindowEventListener.h"
41#include "InputState.h"
[973]42
43namespace orxonox
44{
[1755]45    /**
46    @brief
[3327]47        Manages the input devices (mouse, keyboard, joy sticks) and the input states.
[1349]48
[3327]49        Every input device has its own wrapper class which does the actualy input event
50        distribution. The InputManager only creates reloads (on request) those devices.
[973]51
[3327]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 intenally 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.
[1755]64    */
[3370]65    class _CoreExport InputManager : public Singleton<InputManager>, public WindowEventListener
[1755]66    {
[3370]67        friend class Singleton<InputManager>;
[1755]68    public:
[3327]69        //! Represents internal states of the InputManager.
70        enum State
[1755]71        {
[3327]72            Nothing       = 0x00,
73            Bad           = 0x02,
74            Ticking       = 0x04,
75            Calibrating   = 0x08,
76            ReloadRequest = 0x10,
[1755]77        };
[1219]78
[3327]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(size_t windowHnd);
87        //! Destroys all devices AND all input states!
[1755]88        ~InputManager();
[3327]89        void setConfigValues();
[1293]90
[3327]91        /**
92        @brief
93            Updates the devices (which distribute the input events) and the input states.
[1219]94
[3327]95            Any InpuStates changes (destroy, enter, leave) and happens here. If a reload request
96            was submitted while updating, the request wil 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.
[1878]100        void clearBuffers();
[3327]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();
[1878]110
[3327]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 be actually 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);
[1502]159
[3327]160        //-------------------------------
161        // Various getters and setters
162        //-------------------------------
163        //! Sets the the name of the command used by the KeyDetector as callback.
[1887]164        void setKeyDetectorCallback(const std::string& command);
[3327]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()
170            { return this->oisInputManager_; }
[1502]171
[1755]172    private: // functions
173        // don't mess with a Singleton
[3327]174        InputManager(const InputManager&);
[973]175
[1755]176        // Intenal methods
[3327]177        void loadDevices(size_t windowHnd);
178        void loadMouse();
179        void loadJoySticks();
180        void destroyDevices();
[973]181
[3327]182        void stopCalibration();
183        void reloadInternal();
[2662]184
[3327]185        void destroyStateInternal(InputState* state);
186        void updateActiveStates();
[1349]187
[3327]188        // From WindowEventListener
189        void windowFocusChanged();
[1502]190
[1755]191    private: // variables
[3327]192        State                               internalState_;        //!< Current internal state
193        OIS::InputManager*                  oisInputManager_;      //!< OIS input manager
194        std::vector<InputDevice*>           devices_;              //!< List of all input devices (keyboard, mouse, joy sticks)
195        // TODO: Get this from the GraphicsManager during reload
196        size_t                              windowHnd_;            //!< Render window handle (used to reload the InputManager)
[1219]197
[1878]198        // some internally handled states and handlers
[3327]199        InputState*                         emptyState_;           //!< Lowest priority states (makes handling easier)
[2662]200        KeyDetector*                        keyDetector_;          //!< KeyDetector instance
[3327]201        //! InputBuffer that reacts to the Enter key when calibrating the joy sticks
202        InputBuffer*                        calibratorCallbackHandler_;
[1502]203
[3327]204        std::map<std::string, InputState*>  statesByName_;         //!< Contains all the created input states by name
205        std::map<int, InputState*>          activeStates_;         //!< Contains all active input states by priority (std::map is sorted!)
206        std::vector<InputState*>            activeStatesTicked_;   //!< Like activeStates_, but only contains the ones that currently receive events
[1219]207
[3327]208        std::set<InputState*>               stateEnterRequests_;   //!< Requests to enter a new state
209        std::set<InputState*>               stateLeaveRequests_;   //!< Requests to leave a running state
210        std::set<InputState*>               stateDestroyRequests_; //!< Requests to destroy a state
[1502]211
[3370]212        static InputManager*                singletonPtr_s;        //!< Pointer reference to the singleton
[1755]213    };
[973]214}
215
216#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.