Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/core/KeyBinder.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.3 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 _KeyBinder_H__
35#define _KeyBinder_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    virtual ~KeyBinder();
144
145    void loadBindings();
146    void clearBindings();
147    void setConfigValues();
148
149  protected: // functions
150    void tickInput(float dt, const HandlerState& state);
151
152    void readTrigger(Button& button);
153
154    void keyPressed (const KeyEvent& evt);
155    void keyReleased(const KeyEvent& evt);
156    void keyHeld    (const KeyEvent& evt);
157
158    void mouseButtonPressed (MouseButton::Enum id);
159    void mouseButtonReleased(MouseButton::Enum id);
160    void mouseButtonHeld    (MouseButton::Enum id);
161    void mouseMoved         (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize);
162    void mouseScrolled      (int abs, int rel);
163
164    void joyStickButtonPressed (int joyStickID, int button);
165    void joyStickButtonReleased(int joyStickID, int button);
166    void joyStickButtonHeld    (int joyStickID, int button);
167    void joyStickAxisMoved     (int joyStickID, int axis, int value);
168
169  protected: // variables
170    //! denotes the number of different keys there are in OIS.
171    static const unsigned int nKeys_s = 0xEE;
172    //! Actual key bindings as bundle for Press, Hold and Release
173    Button keys_ [nKeys_s];
174
175    //! denotes the number of different mouse buttons there are in OIS.
176    static const unsigned int nMouseButtons_s = 8 + 2*2; // 8 buttons and 2 scroll wheels
177    //! Actual key bindings as bundle for Press, Hold and Release
178    Button mouseButtons_ [nMouseButtons_s];
179
180    //! denotes the number of different joy stick buttons there are in OIS.
181    static const unsigned int nJoyStickButtons_s = 32 + 4 * 4; // 32 buttons and 4 POVs with 4 buttons
182    //! Actual key bindings as bundle for Press, Hold and Release
183    Button joyStickButtons_ [nJoyStickButtons_s];
184
185    //! denotes the number of half axes (every axis twice) there can be.
186    static const unsigned int nHalfAxes_s = 56;
187    /**
188    * Array with all the half axes for mouse and joy sticks.
189    * Keep in mind that the positions are fixed and that the first entry is the
190    * positive one and the second is negative.
191    * Sequence is as follows:
192    *  0 -  3: Mouse x and y
193    *  4 -  7: empty
194    *  8 - 23: joy stick (slider) axes 1 to 8
195    * 24 - 55: joy stick axes 1 - 16
196    */
197    HalfAxis halfAxes_[nHalfAxes_s];
198
199    /**
200    * Commands that have additional parameters (axes) are executed at the end of
201    * the tick() so that all values can be buffered for single execution.
202    */
203    std::vector<BufferedParamCommand*> paramCommandBuffer_;
204
205    //! Keeps track of the absolute mouse value (incl. scroll wheel)
206    int mousePosition_[2];
207    //! Used to derive mouse input if requested
208    int mouseRelative_[2];
209    float deriveTime_;
210
211    //##### ConfigValues #####
212    //! Threshold for analog triggers until which the state is 0.
213    float analogThreshold_;
214    //! Threshold for analog triggers until which the button is not pressed.
215    float buttonThreshold_;
216    //! Derive mouse input for absolute values?
217    bool bDeriveMouseInput_;
218    //! Accuracy of the mouse input deriver. The higher the more precise, but laggier.
219    float derivePeriod_;
220    //! mouse sensitivity
221    float mouseSensitivity_;
222    //! mouse sensitivity if mouse input is derived
223    float mouseSensitivityDerived_;
224    //! default ini file for the keybindings
225    std::string keybindingsDefault_;
226  };
227
228
229  class _CoreExport KeyDetector : public KeyBinder
230  {
231  public:
232    KeyDetector();
233    ~KeyDetector();
234    void loadBindings();
235    void setConfigValues();
236  protected:
237    void readTrigger(Button& button);
238  };
239}
240
241
242
243
244#endif /* _KeyBinder_H__ */
Note: See TracBrowser for help on using the repository browser.