Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/core/InputManager.h @ 1443

Last change on this file since 1443 was 1428, checked in by rgrieder, 17 years ago
  • added SetConfigValueGeneric to set the config value of the actual class when using hierarchies
  • fixed a bug when handling derived mouse input
  • refined 'keybind' command so that input goes to KeyDetector only
File size: 8.4 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/**
30 @file
31 @brief Implementation of a little Input handler that distributes everything
32        coming from OIS.
33 */
34
35#ifndef _InputManager_H__
36#define _InputManager_H__
37
38#include "CorePrereqs.h"
39
40#include <map>
41#include <vector>
42
43#include "ois/OIS.h"
44#include "util/Math.h"
45#include "Tickable.h"
46#include "InputInterfaces.h"
47
48namespace orxonox
49{
50  /**
51  * Helper class to realise a vector<int[4]>
52  */
53  class POVStates
54  {
55  public:
56    int operator[](unsigned int index) { return povStates[index]; }
57    int povStates[4];
58  };
59
60  /**
61  * Helper class to realise a vector< {int[4], int[4]} >
62  */
63  class SliderStates
64  {
65  public:
66    IntVector2 sliderStates[4];
67  };
68
69  /**
70  * Struct for storing a custom input state
71  */
72  struct StoredState
73  {
74    std::vector<KeyHandler*>                    activeKeyHandlers_;
75    std::vector<MouseHandler*>                  activeMouseHandlers_;
76    std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_;
77    std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_;
78  };
79
80  /**
81    @brief Captures and distributes mouse and keyboard input.
82  */
83  class _CoreExport InputManager
84        : public TickableReal,
85          public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener
86  {
87  public: // enumerations
88    /**
89      @brief Designates the way input is handled and redirected.
90    */
91    enum InputState
92    {
93      IS_UNINIT,    //!< InputManager has not yet been initialised.
94      IS_NONE,      //!< Input is discarded.
95      IS_NORMAL,    //!< Normal play state. Key and button bindings are active.
96      IS_GUI,       //!< All OIS input events are passed to CEGUI.
97      IS_CONSOLE,   //!< Keyboard input is redirected to the InputBuffer.
98      IS_DETECT,    //!< All the input additionally goes to the KeyDetector
99      IS_NODETECT,  //!< remove KeyDetector
100      IS_CUSTOM     //!< Any possible configuration.
101    };
102
103  public: // static functions
104    static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight,
105          bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false);
106    static bool initialiseKeyboard();
107    static bool initialiseMouse();
108    static bool initialiseJoySticks();
109    static int  numberOfKeyboards();
110    static int  numberOfMice();
111    static int  numberOfJoySticks();
112
113    static void destroy();
114    static void destroyKeyboard();
115    static void destroyMouse();
116    static void destroyJoySticks();
117
118    //static bool isModifierDown(KeyboardModifier::Enum modifier);
119    //static bool isKeyDown(KeyCode::Enum key);
120    //static const MouseState getMouseState();
121    //static const JoyStickState getJoyStickState(unsigned int ID);
122
123    static void setWindowExtents(const int width, const int height);
124
125    static void setInputState(const InputState state);
126    static InputState getInputState();
127
128    static void storeKeyStroke(const std::string& name);
129    static void keyBind(const std::string& command);
130
131    static bool addKeyHandler                 (KeyHandler* handler, const std::string& name);
132    static bool removeKeyHandler              (const std::string& name);
133    static KeyHandler* getKeyHandler          (const std::string& name);
134    static bool enableKeyHandler              (const std::string& name);
135    static bool disableKeyHandler             (const std::string& name);
136    static bool isKeyHandlerActive            (const std::string& name);
137
138    static bool addMouseHandler               (MouseHandler* handler, const std::string& name);
139    static bool removeMouseHandler            (const std::string& name);
140    static MouseHandler* getMouseHandler      (const std::string& name);
141    static bool enableMouseHandler            (const std::string& name);
142    static bool disableMouseHandler           (const std::string& name);
143    static bool isMouseHandlerActive          (const std::string& name);
144
145    static bool addJoyStickHandler            (JoyStickHandler* handler, const std::string& name);
146    static bool removeJoyStickHandler         (const std::string& name);
147    static JoyStickHandler* getJoyStickHandler(const std::string& name);
148    static bool enableJoyStickHandler         (const std::string& name, unsigned int id);
149    static bool disableJoyStickHandler        (const std::string& name, unsigned int id);
150    static bool isJoyStickHandlerActive       (const std::string& name, unsigned int id);
151
152  private: // functions
153    // don't mess with a Singleton
154    InputManager ();
155    InputManager (const InputManager&);
156    ~InputManager();
157
158    // Intenal methods
159    bool _initialise(const size_t, int, int, bool, bool, bool);
160    bool _initialiseKeyboard();
161    bool _initialiseMouse();
162    bool _initialiseJoySticks();
163
164    void _destroy();
165    void _destroyKeyboard();
166    void _destroyMouse();
167    void _destroyJoySticks();
168
169    void _updateTickables();
170
171    void _saveState();
172    void _restoreState();
173
174    void tick(float dt);
175
176    // input events
177    bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
178    bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
179    bool mouseMoved    (const OIS::MouseEvent    &arg);
180    bool keyPressed    (const OIS::KeyEvent      &arg);
181    bool keyReleased   (const OIS::KeyEvent      &arg);
182    bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
183    bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
184    bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
185    bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
186    bool povMoved      (const OIS::JoyStickEvent &arg, int id);
187    //bool vector3Moved  (const OIS::JoyStickEvent &arg, int id);
188
189    static InputManager& _getSingleton();
190    static InputManager* _getSingletonPtr() { return &_getSingleton(); }
191
192  private: // variables
193    OIS::InputManager*                          inputSystem_;     //!< OIS input manager
194    OIS::Keyboard*                              keyboard_;        //!< OIS mouse
195    OIS::Mouse*                                 mouse_;           //!< OIS keyboard
196    std::vector<OIS::JoyStick*>                 joySticks_;       //!< OIS joy sticks
197    unsigned int                                joySticksSize_;
198
199    KeyBinder*                                  keyBinder_;       //!< KeyBinder instance
200    KeyDetector*                                keyDetector_;     //!< KeyDetector instance
201    InputBuffer*                                buffer_;          //!< InputBuffer instance
202
203    InputState state_;
204    InputState stateRequest_;
205    unsigned int keyboardModifiers_;
206    InputState savedState_;
207    StoredState savedHandlers_;
208
209    //! Keeps track of the joy stick POV states
210    std::vector<POVStates>                      povStates_;
211    //! Keeps track of the possibly two slider axes
212    std::vector<SliderStates>                   sliderStates_;
213
214    std::map<std::string, KeyHandler*>          keyHandlers_;
215    std::map<std::string, MouseHandler*>        mouseHandlers_;
216    std::map<std::string, JoyStickHandler*>     joyStickHandlers_;
217
218    std::vector<KeyHandler*>                    activeKeyHandlers_;
219    std::vector<MouseHandler*>                  activeMouseHandlers_;
220    std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_;
221    std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_;
222
223    std::vector<Key>                            keysDown_;
224    std::vector<MouseButton::Enum>              mouseButtonsDown_;
225    std::vector<std::vector<int> >              joyStickButtonsDown_;
226
227    static std::string                          bindingCommmandString_s;
228  };
229
230}
231
232#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.