Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/input/src/core/InputManager.h @ 1195

Last change on this file since 1195 was 1195, checked in by rgrieder, 16 years ago
  • added OIS to the orxonox src directory
  • adapted VS files
File size: 7.8 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 <list>
42
43#include "ois/OIS.h"
44#include "Tickable.h"
45#include "InputEvent.h"
46
47namespace orxonox
48{
49  /**
50    @brief Captures and distributes mouse and keyboard input.
51    It resolves the key bindings to InputEvents which can be heard by
52    implementing the InputEventListener interface.
53  */
54  class _CoreExport InputManager
55        : public Tickable,
56          public OIS::MouseListener, public OIS::KeyListener, public OIS::JoyStickListener
57  {
58  public: // enumerations
59    /**
60      @brief Designates the way input is handled and redirected.
61    */
62    enum InputState
63    {
64      IS_UNINIT,  //!< InputManager has not yet been initialised.
65      IS_NONE,    //!< Input is discarded.
66      IS_NORMAL,  //!< Normal play state. Key and button bindings are active.
67      IS_GUI,     //!< All OIS input events are passed to CEGUI.
68      IS_CONSOLE, //!< Keyboard input is redirected to the InputBuffer.
69      IS_CUSTOM   //!< Any possible configuration.
70    };
71
72  public: // functions
73    // METHODS OF THE STATIC INTERFACE
74
75    static bool initialise(size_t windowHnd, int windowWidth, int windowHeight,
76          bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false);
77    static void destroy();
78
79    static void setWindowExtents(int width, int height);
80
81    static void setInputState(const InputState state);
82    static InputState getInputState();
83    static void setKeyBindingState           (bool bActive);
84    static void setMouseButtonBindingState   (bool bActive);
85    static void setJoyStickButtonBindingState(bool bActive);
86
87    static bool addKeyListener(OIS::KeyListener* listener, const std::string& name);
88    static bool removeKeyListener  (const std::string& name);
89    static bool enableKeyListener  (const std::string& name);
90    static bool disableKeyListener (const std::string& name);
91    static bool isKeyListenerActive(const std::string& name);
92    static OIS::KeyListener* getKeyListener(const std::string& name);
93
94    static bool addMouseListener(OIS::MouseListener* listener, const std::string& name);
95    static bool removeMouseListener  (const std::string& name);
96    static bool enableMouseListener  (const std::string& name);
97    static bool disableMouseListener (const std::string& name);
98    static bool isMouseListenerActive(const std::string& name);
99    static OIS::MouseListener* getMouseListener(const std::string& name);
100
101    static bool addJoyStickListener(OIS::JoyStickListener* listener, const std::string& name);
102    static bool removeJoyStickListener  (const std::string& name);
103    static bool enableJoyStickListener  (const std::string& name);
104    static bool disableJoyStickListener (const std::string& name);
105    static bool isJoyStickListenerActive(const std::string& name);
106    static OIS::JoyStickListener* getJoyStickListener(const std::string& name);
107
108    // Temporary solutions. Will be removed soon!
109    static OIS::Mouse*    getMouse()    { return _getSingleton().mouse_   ; }
110    static OIS::Keyboard* getKeyboard() { return _getSingleton().keyboard_; }
111
112  private: // functions
113    // don't mess with a Singleton
114    InputManager ();
115    InputManager (const InputManager&);
116    ~InputManager();
117
118    // Intenal methods
119    bool _initialise(size_t, int, int, bool, bool, bool);
120    void _initialiseKeyboard();
121    void _initialiseMouse();
122    void _initialiseJoySticks();
123    void _destroy();
124    //void _setDefaultState();
125    bool _loadBindings();
126    void _clearBindings();
127    void _setNumberOfJoysticks(int size);
128
129    void tick(float dt);
130
131    // input events
132                bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
133                bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
134    bool mouseMoved    (const OIS::MouseEvent    &arg);
135                bool keyPressed    (const OIS::KeyEvent      &arg);
136                bool keyReleased   (const OIS::KeyEvent      &arg);
137                bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
138                bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
139                bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
140                bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
141                bool povMoved      (const OIS::JoyStickEvent &arg, int id);
142
143    static InputManager& _getSingleton();
144    static InputManager* _getSingletonPtr() { return &_getSingleton(); }
145
146  private: // variables
147    OIS::InputManager* inputSystem_;    //!< OIS input manager
148    OIS::Keyboard*     keyboard_;       //!< OIS mouse
149    OIS::Mouse*        mouse_;          //!< OIS keyboard
150    std::vector<OIS::JoyStick*> joySticks_;       //!< OIS joy sticks
151
152    InputState state_;
153    InputState stateRequest_;
154
155    bool bKeyBindingsActive_;
156    bool bMouseButtonBindingsActive_;
157    std::vector<bool> bJoyStickButtonBindingsActive_;
158
159    std::map<std::string, OIS::KeyListener*>      listenersKey_;
160    std::map<std::string, OIS::MouseListener*>    listenersMouse_;
161    std::map<std::string, OIS::JoyStickListener*> listenersJoySticks_;
162
163    std::list<OIS::KeyListener*>      listenersKeyActive_;
164    std::list<OIS::MouseListener*>    listenersMouseActive_;
165    std::list<OIS::JoyStickListener*> listenersJoySticksActive_;
166
167    std::list<OIS::KeyCode>         keysDown_;
168    std::list<OIS::MouseButtonID>   mouseButtonsDown_;
169    std::vector< std::list<int> >   joyStickButtonsDown_;
170
171
172    /** denotes the maximum number of different keys there are in OIS.
173        256 should be ok since the highest number in the OIS enum is 237. */
174    static const int numberOfKeys_s = 256;
175    //! Array of input events for every pressed key
176    std::string bindingsKeyPress_  [numberOfKeys_s];
177    //! Array of input events for every released key
178    std::string bindingsKeyRelease_[numberOfKeys_s];
179    //! Array of input events for every held key
180    std::string bindingsKeyHold_   [numberOfKeys_s];
181
182    /** denotes the maximum number of different buttons there are in OIS.
183        16 should be ok since the highest number in the OIS enum is 7. */
184    static const int numberOfMouseButtons_s = 16;
185    //! Array of input events for every pressed mouse button
186    std::string bindingsMouseButtonPress_  [numberOfMouseButtons_s];
187    //! Array of input events for every released mouse button
188    std::string bindingsMouseButtonRelease_[numberOfMouseButtons_s];
189    //! Array of input events for every held mouse button
190    std::string bindingsMouseButtonHold_   [numberOfMouseButtons_s];
191
192    /** denotes the maximum number of different buttons there are in OIS.
193        32 should be ok. */
194    static const int numberOfJoyStickButtons_s = 32;
195    std::vector< std::vector< std::string > > bindingsJoyStickButtonPress_;
196    std::vector< std::vector< std::string > > bindingsJoyStickButtonRelease_;
197    std::vector< std::vector< std::string > > bindingsJoyStickButtonHold_;
198
199  };
200}
201
202#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.