Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1182 was 1182, checked in by rgrieder, 16 years ago
  • InputManager works so far, but has to be largely extended
File size: 7.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 <list>
42#include <OIS/OIS.h>
43
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:
59    /**
60      @brief Designates the way input is handled currently.
61      IM_GUI:    All the OIS input events are passed to CEGUI
62      IM_BUFFER: Only keyboard input is captured and passed to the InputBuffer
63      IM_NORMAL: Normal game mode. Key bindings and mouse are active.
64    */
65    enum InputState
66    {
67      IS_UNINIT,
68      IS_NONE,
69      IS_NORMAL,
70      IS_GUI,
71      IS_CONSOLE,
72      IS_CUSTOM
73    };
74
75  public:
76    void tick(float dt);
77
78    static bool initialise(size_t windowHnd, int windowWidth, int windowHeight);
79    static void destroy();
80
81    static void setWindowExtents(int width, int height);
82    static void setInputState(const InputState state);
83    static InputState getInputState();
84
85    static bool addKeyListener(OIS::KeyListener* listener, const std::string& name);
86    //static bool removeKeyListener(OIS::KeyListener* listener);
87    static bool removeKeyListener(const std::string& name);
88    static bool enableKeyListener(const std::string& name);
89    static bool disableKeyListener(const std::string& name);
90    static OIS::KeyListener* getKeyListener(const std::string& name);
91    static bool isKeyListenerActive(const std::string& name);
92
93    static bool addMouseListener(OIS::MouseListener* listener, const std::string& name);
94    //static bool removeMouseListener(OIS::MouseListener* listener);
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 OIS::MouseListener* getMouseListener(const std::string& name);
99    static bool isMouseListenerActive(const std::string& name);
100
101    static bool addJoyStickListener(OIS::JoyStickListener* listener, const std::string& name);
102    //static bool removeJoyStickListener(OIS::JoyStickListener* listener);
103    static bool removeJoyStickListener(const std::string& name);
104    static bool enableJoyStickListener(const std::string& name);
105    static bool disableJoyStickListener(const std::string& name);
106    static OIS::JoyStickListener* getJoyStickListener(const std::string& name);
107    static bool isJoyStickListenerActive(const std::string& name);
108
109    // Temporary solutions. Will be removed soon!
110    static OIS::Mouse*    getMouse()    { return _getSingleton().mouse_   ; }
111    static OIS::Keyboard* getKeyboard() { return _getSingleton().keyboard_; }
112
113  private:
114    // don't mess with a Singleton
115    InputManager ();
116    InputManager (const InputManager&);
117    ~InputManager();
118
119    bool _initialise(size_t windowHnd, int windowWidth, int windowHeight);
120    void _destroy();
121    void _setDefaultState();
122    void _loadBindings();
123
124    // input events
125                bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
126                bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
127    bool mouseMoved    (const OIS::MouseEvent    &arg);
128                bool keyPressed    (const OIS::KeyEvent      &arg);
129                bool keyReleased   (const OIS::KeyEvent      &arg);
130                bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
131                bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
132                bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
133                bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
134                bool povMoved      (const OIS::JoyStickEvent &arg, int id);
135
136    static InputManager& _getSingleton();
137    static InputManager* _getSingletonPtr() { return &_getSingleton(); }
138
139  private:
140    OIS::InputManager* inputSystem_;    //!< OIS input manager
141    OIS::Keyboard*     keyboard_;       //!< OIS mouse
142    OIS::Mouse*        mouse_;          //!< OIS keyboard
143
144    InputState state_;
145    InputState stateRequest_;
146
147    std::list<OIS::KeyCode> keysDown_;
148
149    bool bDefaultKeyInput;
150    bool bDefaultMouseInput;
151    bool bDefaultJoyStickInput;
152
153    std::map<std::string, OIS::KeyListener*>      keyListeners_;
154    std::list<OIS::KeyListener*>                  activeKeyListeners_;
155    std::map<std::string, OIS::MouseListener*>    mouseListeners_;
156    std::list<OIS::MouseListener*>                activeMouseListeners_;
157    std::map<std::string, OIS::JoyStickListener*> joystickListeners_;
158    std::list<OIS::JoyStickListener*>             activeJoyStickListeners_;
159
160
161    /** denotes the maximum number of different keys there are in OIS.
162        256 should be ok since the highest number in the enum is 237. */
163    static const int numberOfKeys_s = 256;
164    //! Array of input events for every pressed key
165    std::string bindingsKeyPress_  [numberOfKeys_s];
166    //! Array of input events for every released key
167    std::string bindingsKeyRelease_[numberOfKeys_s];
168    //! Array of input events for every held key
169    std::string bindingsKeyHold_   [numberOfKeys_s];
170
171    /** denotes the maximum number of different buttons there are in OIS.
172        16 should be ok since the highest number in the enum is 7. */
173    static const int numberOfMouseButtons_s = 16;
174    //! Array of input events for every pressed mouse button
175    std::string bindingsMouseButtonPress_  [numberOfMouseButtons_s];
176    //! Array of input events for every released mouse button
177    std::string bindingsMouseButtonRelease_[numberOfMouseButtons_s];
178    //! Array of input events for every held mouse button
179    std::string bindingsMouseButtonHold_   [numberOfMouseButtons_s];
180
181    /** denotes the maximum number of different buttons there are in OIS.
182        32 should be ok. */
183    static const int numberOfJoyStickButtons_s = 32;
184    //! Array of input events for every pressed joystick button
185    std::string bindingsJoyStickButtonPress_  [numberOfJoyStickButtons_s];
186    //! Array of input events for every released joystick button
187    std::string bindingsJoyStickButtonRelease_[numberOfJoyStickButtons_s];
188    //! Array of input events for every held joystick button
189    std::string bindingsJoyStickButtonHold_   [numberOfJoyStickButtons_s];
190
191  };
192}
193
194#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.