Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/input/src/core/InputHandler.h @ 1340

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