Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1426 was 1426, checked in by rgrieder, 16 years ago
  • you can now tpye "keybind command" and then press a button/key to bind that string directly. doesn't seem to work yet with mouse axes
File size: 8.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/**
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    @brief Captures and distributes mouse and keyboard input.
71  */
72  class _CoreExport InputManager
73        : public TickableReal,
74          public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener
75  {
76  public: // enumerations
77    /**
78      @brief Designates the way input is handled and redirected.
79    */
80    enum InputState
81    {
82      IS_UNINIT,    //!< InputManager has not yet been initialised.
83      IS_NONE,      //!< Input is discarded.
84      IS_NORMAL,    //!< Normal play state. Key and button bindings are active.
85      IS_GUI,       //!< All OIS input events are passed to CEGUI.
86      IS_CONSOLE,   //!< Keyboard input is redirected to the InputBuffer.
87      IS_DETECT,    //!< All the input additionally goes to the KeyDetector
88      IS_NODETECT,  //!< remove KeyDetector
89      IS_CUSTOM     //!< Any possible configuration.
90    };
91
92  public: // static functions
93    static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight,
94          bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false);
95    static bool initialiseKeyboard();
96    static bool initialiseMouse();
97    static bool initialiseJoySticks();
98    static int  numberOfKeyboards();
99    static int  numberOfMice();
100    static int  numberOfJoySticks();
101
102    static void destroy();
103    static void destroyKeyboard();
104    static void destroyMouse();
105    static void destroyJoySticks();
106
107    //static bool isModifierDown(KeyboardModifier::Enum modifier);
108    //static bool isKeyDown(KeyCode::Enum key);
109    //static const MouseState getMouseState();
110    //static const JoyStickState getJoyStickState(unsigned int ID);
111
112    static void setWindowExtents(const int width, const int height);
113
114    static void setInputState(const InputState state);
115    static InputState getInputState();
116
117    static void storeKeyStroke(const std::string& name);
118    static void keyBind(const std::string& command);
119
120    static bool addKeyHandler                 (KeyHandler* handler, const std::string& name);
121    static bool removeKeyHandler              (const std::string& name);
122    static KeyHandler* getKeyHandler          (const std::string& name);
123    static bool enableKeyHandler              (const std::string& name);
124    static bool disableKeyHandler             (const std::string& name);
125    static bool isKeyHandlerActive            (const std::string& name);
126
127    static bool addMouseHandler               (MouseHandler* handler, const std::string& name);
128    static bool removeMouseHandler            (const std::string& name);
129    static MouseHandler* getMouseHandler      (const std::string& name);
130    static bool enableMouseHandler            (const std::string& name);
131    static bool disableMouseHandler           (const std::string& name);
132    static bool isMouseHandlerActive          (const std::string& name);
133
134    static bool addJoyStickHandler            (JoyStickHandler* handler, const std::string& name);
135    static bool removeJoyStickHandler         (const std::string& name);
136    static JoyStickHandler* getJoyStickHandler(const std::string& name);
137    static bool enableJoyStickHandler         (const std::string& name, unsigned int id);
138    static bool disableJoyStickHandler        (const std::string& name, unsigned int id);
139    static bool isJoyStickHandlerActive       (const std::string& name, unsigned int id);
140
141  private: // functions
142    // don't mess with a Singleton
143    InputManager ();
144    InputManager (const InputManager&);
145    ~InputManager();
146
147    // Intenal methods
148    bool _initialise(const size_t, int, int, bool, bool, bool);
149    bool _initialiseKeyboard();
150    bool _initialiseMouse();
151    bool _initialiseJoySticks();
152
153    void _destroy();
154    void _destroyKeyboard();
155    void _destroyMouse();
156    void _destroyJoySticks();
157
158    void _updateTickables();
159
160    void tick(float dt);
161
162    // input events
163    bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
164    bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
165    bool mouseMoved    (const OIS::MouseEvent    &arg);
166    bool keyPressed    (const OIS::KeyEvent      &arg);
167    bool keyReleased   (const OIS::KeyEvent      &arg);
168    bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
169    bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
170    bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
171    bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
172    bool povMoved      (const OIS::JoyStickEvent &arg, int id);
173    //bool vector3Moved  (const OIS::JoyStickEvent &arg, int id);
174
175    static InputManager& _getSingleton();
176    static InputManager* _getSingletonPtr() { return &_getSingleton(); }
177
178  private: // variables
179    OIS::InputManager*                          inputSystem_;     //!< OIS input manager
180    OIS::Keyboard*                              keyboard_;        //!< OIS mouse
181    OIS::Mouse*                                 mouse_;           //!< OIS keyboard
182    std::vector<OIS::JoyStick*>                 joySticks_;       //!< OIS joy sticks
183    unsigned int                                joySticksSize_;
184
185    KeyBinder*                                  keyBinder_;       //!< KeyBinder instance
186    KeyDetector*                                keyDetector_;     //!< KeyDetector instance
187    InputBuffer*                                buffer_;          //!< InputBuffer instance
188
189    InputState state_;
190    InputState stateRequest_;
191    unsigned int keyboardModifiers_;
192    InputState savedState_;
193
194    //! Keeps track of the joy stick POV states
195    std::vector<POVStates>                      povStates_;
196    //! Keeps track of the possibly two slider axes
197    std::vector<SliderStates>                   sliderStates_;
198
199    std::map<std::string, KeyHandler*>          keyHandlers_;
200    std::map<std::string, MouseHandler*>        mouseHandlers_;
201    std::map<std::string, JoyStickHandler*>     joyStickHandlers_;
202
203    std::vector<KeyHandler*>                    activeKeyHandlers_;
204    std::vector<MouseHandler*>                  activeMouseHandlers_;
205    std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_;
206    std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_;
207
208    std::vector<Key>                            keysDown_;
209    std::vector<MouseButton::Enum>              mouseButtonsDown_;
210    std::vector<std::vector<int> >              joyStickButtonsDown_;
211
212    static std::string                          bindingCommmandString_s;
213  };
214
215}
216
217#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.