Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1413 was 1413, checked in by rgrieder, 16 years ago
  • renamed InputHandler to KeyBinder
  • added feature to detect a key in input part
  • added def_keybindings.ini and removed keybindings.ini
File size: 7.9 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_DETECTION, //!< All the input goes to the KeyDetector
88      IS_CUSTOM     //!< Any possible configuration.
89    };
90
91  public: // static functions
92    static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight,
93          bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false);
94    static bool initialiseKeyboard();
95    static bool initialiseMouse();
96    static bool initialiseJoySticks();
97    static int  numberOfKeyboards();
98    static int  numberOfMice();
99    static int  numberOfJoySticks();
100
101    static void destroy();
102    static void destroyKeyboard();
103    static void destroyMouse();
104    static void destroyJoySticks();
105
106    //static bool isModifierDown(KeyboardModifier::Enum modifier);
107    //static bool isKeyDown(KeyCode::Enum key);
108    //static const MouseState getMouseState();
109    //static const JoyStickState getJoyStickState(unsigned int ID);
110
111    static void setWindowExtents(const int width, const int height);
112
113    static void setInputState(const InputState state);
114    static InputState getInputState();
115
116    static void storeKeyStroke(const std::string& name);
117    static const std::string& getLastKeyStroke();
118
119    static bool addKeyHandler                 (KeyHandler* handler, const std::string& name);
120    static bool removeKeyHandler              (const std::string& name);
121    static KeyHandler* getKeyHandler          (const std::string& name);
122    static bool enableKeyHandler              (const std::string& name);
123    static bool disableKeyHandler             (const std::string& name);
124    static bool isKeyHandlerActive            (const std::string& name);
125
126    static bool addMouseHandler               (MouseHandler* handler, const std::string& name);
127    static bool removeMouseHandler            (const std::string& name);
128    static MouseHandler* getMouseHandler      (const std::string& name);
129    static bool enableMouseHandler            (const std::string& name);
130    static bool disableMouseHandler           (const std::string& name);
131    static bool isMouseHandlerActive          (const std::string& name);
132
133    static bool addJoyStickHandler            (JoyStickHandler* handler, const std::string& name);
134    static bool removeJoyStickHandler         (const std::string& name);
135    static JoyStickHandler* getJoyStickHandler(const std::string& name);
136    static bool enableJoyStickHandler         (const std::string& name, unsigned int id);
137    static bool disableJoyStickHandler        (const std::string& name, unsigned int id);
138    static bool isJoyStickHandlerActive       (const std::string& name, unsigned int id);
139
140  private: // functions
141    // don't mess with a Singleton
142    InputManager ();
143    InputManager (const InputManager&);
144    ~InputManager();
145
146    // Intenal methods
147    bool _initialise(const size_t, int, int, bool, bool, bool);
148    bool _initialiseKeyboard();
149    bool _initialiseMouse();
150    bool _initialiseJoySticks();
151
152    void _destroy();
153    void _destroyKeyboard();
154    void _destroyMouse();
155    void _destroyJoySticks();
156
157    void _updateTickables();
158
159    void tick(float dt);
160
161    // input events
162    bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
163    bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
164    bool mouseMoved    (const OIS::MouseEvent    &arg);
165    bool keyPressed    (const OIS::KeyEvent      &arg);
166    bool keyReleased   (const OIS::KeyEvent      &arg);
167    bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
168    bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
169    bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
170    bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
171    bool povMoved      (const OIS::JoyStickEvent &arg, int id);
172    //bool vector3Moved  (const OIS::JoyStickEvent &arg, int id);
173
174    static InputManager& _getSingleton();
175    static InputManager* _getSingletonPtr() { return &_getSingleton(); }
176
177  private: // variables
178    OIS::InputManager*                          inputSystem_;     //!< OIS input manager
179    OIS::Keyboard*                              keyboard_;        //!< OIS mouse
180    OIS::Mouse*                                 mouse_;           //!< OIS keyboard
181    std::vector<OIS::JoyStick*>                 joySticks_;       //!< OIS joy sticks
182    unsigned int                                joySticksSize_;
183
184    KeyBinder*                                  keyBinder_;       //!< KeyBinder instance
185    KeyDetector*                                keyDetector_;     //!< KeyDetector instance
186    InputBuffer*                                buffer_;          //!< InputBuffer instance
187
188    InputState state_;
189    InputState stateRequest_;
190    unsigned int keyboardModifiers_;
191    std::string lastStroke_;
192
193    //! Keeps track of the joy stick POV states
194    std::vector<POVStates>                      povStates_;
195    //! Keeps track of the possibly two slider axes
196    std::vector<SliderStates>                   sliderStates_;
197
198    std::map<std::string, KeyHandler*>          keyHandlers_;
199    std::map<std::string, MouseHandler*>        mouseHandlers_;
200    std::map<std::string, JoyStickHandler*>     joyStickHandlers_;
201
202    std::vector<KeyHandler*>                    activeKeyHandlers_;
203    std::vector<MouseHandler*>                  activeMouseHandlers_;
204    std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_;
205    std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_;
206
207    std::vector<Key>                            keysDown_;
208    std::vector<MouseButton::Enum>              mouseButtonsDown_;
209    std::vector<std::vector<int> >              joyStickButtonsDown_;
210
211  };
212
213}
214
215#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.