Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/InputHandler.h @ 1417

Last change on this file since 1417 was 1349, checked in by rgrieder, 16 years ago
  • merged input branch back to trunk
File size: 8.1 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 Different definitions of input processing.
32 */
33
34#ifndef _InputHandler_H__
35#define _InputHandler_H__
36
37#include "CorePrereqs.h"
38
39#include <string>
40#include <vector>
41
42#include "ois/OIS.h"
43#include "util/Math.h"
44#include "OrxonoxClass.h"
45#include "CommandExecutor.h"
46#include "InputInterfaces.h"
47
48namespace orxonox
49{
50  class _CoreExport BaseCommand
51  {
52  public:
53    virtual ~BaseCommand() { }
54    virtual bool execute(float abs = 1.0f, float rel = 1.0f) = 0;
55  };
56
57  class _CoreExport BufferedParamCommand
58  {
59  public:
60    BufferedParamCommand() : value_(0.0f), nValuesAdded_(0), paramIndex_(-1) { }
61    bool execute();
62
63    float value_;
64    unsigned int nValuesAdded_;
65    int paramIndex_;
66    CommandEvaluation evaluation_;
67  };
68
69  class _CoreExport SimpleCommand : public BaseCommand
70  {
71  public:
72    bool execute(float abs = 1.0f, float rel = 1.0f);
73
74    CommandEvaluation evaluation_;
75  };
76
77  class _CoreExport ParamCommand : public BaseCommand
78  {
79  public:
80    ParamCommand() : bRelative_(false), paramModifier_(1.0f), paramCommand_(0) { }
81    bool execute(float abs = 1.0f, float rel = 1.0f);
82
83    bool bRelative_;
84    float paramModifier_;
85    BufferedParamCommand* paramCommand_;
86  };
87
88  class _CoreExport Button
89  {
90  public:
91    Button() { nCommands_[0]=0; nCommands_[1]=0; nCommands_[2]=0; clear(); }
92    virtual ~Button() { clear(); }
93    virtual void clear();
94    virtual bool addParamCommand(ParamCommand* command) { return false; }
95    void parse(std::vector<BufferedParamCommand*>& paramCommandBuffer);
96    bool execute(KeybindMode::Enum mode, float abs = 1.0f, float rel = 1.0f);
97
98    //! The configured string value
99    std::string bindingString_;
100    //! Name of the trigger as strings
101    std::string name_;
102    //! Basic commands for OnPress, OnHold and OnRelease
103    BaseCommand** commands_[3];
104    //! Number of basic commands
105    unsigned int nCommands_[3];
106    //! Says how much it takes for an analog axis to trigger a button
107    //! Note: This variable is here to have only one parse() function.
108    float buttonThreshold_;
109  };
110
111
112  class _CoreExport HalfAxis : public Button
113  {
114  public:
115    HalfAxis() : relVal_(0.0f), absVal_(0.0f), paramCommands_(0), nParamCommands_(0),
116                 wasDown_(false), hasChanged_(false) { }
117    using Button::execute;
118    bool execute();
119    //bool execute(KeybindMode::Enum mode) { return Button::execute(mode); }
120    bool addParamCommand(ParamCommand* command);
121    void clear();
122
123    // axis related
124    float relVal_;
125    float absVal_;
126    ParamCommand** paramCommands_;
127    unsigned int nParamCommands_;
128
129    // button related
130    bool wasDown_;
131    bool hasChanged_;
132  };
133
134
135  /**
136    @brief Handles mouse, keyboard and joy stick input while in the actual game mode.
137           Manages the key bindings.
138  */
139  class _CoreExport KeyBinder : public KeyHandler, public MouseHandler, public JoyStickHandler, public OrxonoxClass
140  {
141  public:
142    KeyBinder ();
143    ~KeyBinder();
144
145    void loadBindings();
146    void clearBindings(bool bInit = false);
147
148    void setConfigValues();
149
150  private: // functions
151    void readTrigger(Button& button);
152
153    //static void clearBundle(KeyBindingBundle& bundle, bool bInit);
154    //static void redimensionBinding(KeyBinding& binding);
155
156    void tick(float dt);
157
158    void keyPressed (const KeyEvent& evt);
159    void keyReleased(const KeyEvent& evt);
160    void keyHeld    (const KeyEvent& evt);
161
162    void mouseButtonPressed (MouseButton::Enum id);
163    void mouseButtonReleased(MouseButton::Enum id);
164    void mouseButtonHeld    (MouseButton::Enum id);
165    void mouseMoved         (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize);
166    void mouseScrolled      (int abs, int rel);
167
168    void joyStickButtonPressed (int joyStickID, int button);
169    void joyStickButtonReleased(int joyStickID, int button);
170    void joyStickButtonHeld    (int joyStickID, int button);
171    void joyStickAxisMoved     (int joyStickID, int axis, int value);
172
173  private: // variables
174    //! denotes the number of different keys there are in OIS.
175    static const unsigned int nKeys_s = 0xEE;
176    //! Actual key bindings as bundle for Press, Hold and Release
177    Button keys_ [nKeys_s];
178
179    //! denotes the number of different mouse buttons there are in OIS.
180    static const unsigned int nMouseButtons_s = 8 + 2*2; // 8 buttons and 2 scroll wheels
181    //! Actual key bindings as bundle for Press, Hold and Release
182    Button mouseButtons_ [nMouseButtons_s];
183
184    //! denotes the number of different joy stick buttons there are in OIS.
185    static const unsigned int nJoyStickButtons_s = 32 + 4 * 4; // 32 buttons and 4 POVs with 4 buttons
186    //! Actual key bindings as bundle for Press, Hold and Release
187    Button joyStickButtons_ [nJoyStickButtons_s];
188
189    //! denotes the number of half axes (every axis twice) there can be.
190    static const unsigned int nHalfAxes_s = 56;
191    /**
192    * Array with all the half axes for mouse and joy sticks.
193    * Keep in mind that the positions are fixed and that the first entry is the
194    * positive one and the second is negative.
195    * Sequence is as follows:
196    *  0 -  3: Mouse x and y
197    *  4 -  7: empty
198    *  8 - 23: joy stick (slider) axes 1 to 8
199    * 24 - 55: joy stick axes 1 - 16
200    */
201    HalfAxis halfAxes_[nHalfAxes_s];
202
203    /**
204    * Commands that have additional parameters (axes) are executed at the end of
205    * the tick() so that all values can be buffered for single execution.
206    */
207    std::vector<BufferedParamCommand*> paramCommandBuffer_;
208
209    //! Keeps track of the absolute mouse value (incl. scroll wheel)
210    int mousePosition_[3];
211    //! Used to derive mouse input if requested
212    int mouseRelative_[2];
213    float deriveTime_;
214
215    //##### ConfigValues #####
216    //! Threshold for analog triggers until which the state is 0.
217    float analogThreshold_;
218    //! Threshold for analog triggers until which the button is not pressed.
219    float buttonThreshold_;
220    //! Derive mouse input for absolute values?
221    bool bDeriveMouseInput_;
222    //! Accuracy of the mouse input deriver. The higher the more precise, but laggier.
223    float derivePeriod_;
224    //! mouse sensitivity
225    float mouseSensitivity_;
226  };
227
228
229  /**
230    @brief Captures mouse and keyboard input and distributes it to the
231    GUI.
232  */
233  //class _CoreExport GUIInputHandler : public KeyHandler, public MouseHandler, public JoyStickHandler
234  //{
235  //public:
236  //  GUIInputHandler ();
237  //  ~GUIInputHandler();
238
239  //private:
240  //  // input events
241    //bool keyPressed   (const OIS::KeyEvent   &arg);
242    //bool keyReleased  (const OIS::KeyEvent   &arg);
243    //bool keyHeld      (const OIS::KeyEvent   &arg);
244
245  //  bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButton id);
246    //bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButton id);
247    //bool mouseHeld    (const OIS::MouseEvent &arg, OIS::MouseButton id);
248  //  bool mouseMoved   (const OIS::MouseEvent &arg);
249
250    //bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
251    //bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
252    //bool buttonHeld    (const OIS::JoyStickEvent &arg, int button);
253    //bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
254    //bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
255    //bool povMoved      (const OIS::JoyStickEvent &arg, int id);
256  //};
257
258}
259
260#endif /* _InputHandler_H__ */
Note: See TracBrowser for help on using the repository browser.