Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/shader/src/core/KeyBinder.h @ 10634

Last change on this file since 10634 was 1505, checked in by rgrieder, 16 years ago

f* svn: It doesn't even inform you if you attempt to set a non existing property. It is svn:eol-style and not eol-style when using the command by the way…

  • Property svn:eol-style set to native
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 _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 "CommandEvaluation.h"
46#include "InputInterfaces.h"
47
48namespace orxonox
49{
50  class _CoreExport BufferedParamCommand
51  {
52  public:
53    BufferedParamCommand() : value_(0.0f), nValuesAdded_(0), paramIndex_(-1) { }
54    bool execute();
55
56    float value_;
57    unsigned int nValuesAdded_;
58    int paramIndex_;
59    CommandEvaluation evaluation_;
60  };
61
62  class _CoreExport BaseCommand
63  {
64  public:
65    virtual ~BaseCommand() { }
66    virtual bool execute(float abs = 1.0f, float rel = 1.0f) = 0;
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    void resetJoyStickAxes();
149
150  protected: // functions
151    void tickInput(float dt, const HandlerState& state);
152
153    virtual void readTrigger(Button& button);
154
155    void keyPressed (const KeyEvent& evt);
156    void keyReleased(const KeyEvent& evt);
157    void keyHeld    (const KeyEvent& evt);
158
159    void mouseButtonPressed (MouseButton::Enum id);
160    void mouseButtonReleased(MouseButton::Enum id);
161    void mouseButtonHeld    (MouseButton::Enum id);
162    void mouseMoved         (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize);
163    void mouseScrolled      (int abs, int rel);
164
165    void joyStickButtonPressed (int joyStickID, int button);
166    void joyStickButtonReleased(int joyStickID, int button);
167    void joyStickButtonHeld    (int joyStickID, int button);
168    void joyStickAxisMoved     (int joyStickID, int axis, float value);
169
170  protected: // variables
171    //! denotes the number of different keys there are in OIS.
172    static const unsigned int nKeys_s = 0xEE;
173    //! Actual key bindings as bundle for Press, Hold and Release
174    Button keys_ [nKeys_s];
175
176    //! denotes the number of different mouse buttons there are in OIS.
177    static const unsigned int nMouseButtons_s = 8 + 2*2; // 8 buttons and 2 scroll wheels
178    //! Actual key bindings as bundle for Press, Hold and Release
179    Button mouseButtons_ [nMouseButtons_s];
180
181    //! denotes the number of different joy stick buttons there are in OIS.
182    static const unsigned int nJoyStickButtons_s = 32 + 4 * 4; // 32 buttons and 4 POVs with 4 buttons
183    //! Actual key bindings as bundle for Press, Hold and Release
184    Button joyStickButtons_ [nJoyStickButtons_s];
185
186    //! denotes the number of half axes (every axis twice) there can be.
187    static const unsigned int nHalfAxes_s = 56;
188    /**
189    * Array with all the half axes for mouse and joy sticks.
190    * Keep in mind that the positions are fixed and that the first entry is the
191    * positive one and the second is negative.
192    * Sequence is as follows:
193    *  0 -  3: Mouse x and y
194    *  4 -  7: empty
195    *  8 - 23: joy stick slider axes 1 to 8
196    * 24 - 55: joy stick axes 1 - 16
197    */
198    HalfAxis halfAxes_[nHalfAxes_s];
199
200    /**
201    * Commands that have additional parameters (axes) are executed at the end of
202    * the tick() so that all values can be buffered for single execution.
203    */
204    std::vector<BufferedParamCommand*> paramCommandBuffer_;
205
206    //! Keeps track of the absolute mouse value (incl. scroll wheel)
207    int mousePosition_[2];
208    //! Used to derive mouse input if requested
209    int mouseRelative_[2];
210    float deriveTime_;
211
212    //##### ConfigValues #####
213    //! Threshold for analog triggers until which the state is 0.
214    float analogThreshold_;
215    //! Threshold for analog triggers until which the button is not pressed.
216    float buttonThreshold_;
217    //! Derive mouse input for absolute values?
218    bool bDeriveMouseInput_;
219    //! Accuracy of the mouse input deriver. The higher the more precise, but laggier.
220    float derivePeriod_;
221    //! mouse sensitivity
222    float mouseSensitivity_;
223    //! mouse sensitivity if mouse input is derived
224    float mouseSensitivityDerived_;
225    //! Whether or not to clip abslute mouse values to 1024
226    bool bClipMouse_;
227  };
228
229
230  class _CoreExport KeyDetector : public KeyBinder
231  {
232  public:
233    KeyDetector();
234    ~KeyDetector();
235    void loadBindings();
236
237  protected:
238    void readTrigger(Button& button);
239  };
240
241  class _CoreExport CalibratorCallback : public KeyHandler
242  {
243  public:
244    CalibratorCallback() {}
245    ~CalibratorCallback() {}
246
247  private:
248    void keyPressed (const KeyEvent& evt);
249    void keyReleased(const KeyEvent& evt) {}
250    void keyHeld    (const KeyEvent& evt) {}
251
252    void tickInput(float dt, const HandlerState &state) { }
253  };
254}
255
256
257
258
259#endif /* _KeyBinder_H__ */
Note: See TracBrowser for help on using the repository browser.