Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/menu/src/libraries/core/input/KeyBinder.h @ 6051

Last change on this file since 6051 was 5929, checked in by rgrieder, 15 years ago

Merged core5 branch back to the trunk.
Key features include clean level unloading and an extended XML event system.

Two important notes:
Delete your keybindings.ini files! * or you will still get parser errors when loading the key bindings.
Delete build_dir/lib/modules/libgamestates.module! * or orxonox won't start.
Best thing to do is to delete the build folder ;)

  • Property svn:eol-style set to native
File size: 8.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#ifndef _KeyBinder_H__
30#define _KeyBinder_H__
31
32#include "InputPrereqs.h"
33
34#include <cassert>
35#include <string>
36#include <vector>
37#include <boost/shared_ptr.hpp>
38
39#include "InputHandler.h"
40#include "Button.h"
41#include "HalfAxis.h"
42#include "InputCommands.h"
43#include "JoyStickQuantityListener.h"
44
45namespace orxonox
46{
47    /**
48    @brief
49        Maps mouse, keyboard and joy stick input to command strings and executes them.
50
51        The bindings are stored in ini-files (like the one for configValues) in the config Path.
52    @remarks
53        You cannot change the filename because the KeyBinderManager maps these filenames to the
54        KeyBinders. If you need to load other bindings, just create a new one.
55    */
56    class _CoreExport KeyBinder : public InputHandler, public JoyStickQuantityListener
57    {
58    public:
59        KeyBinder (const std::string& filename);
60        virtual ~KeyBinder();
61
62        void clearBindings();
63        bool setBinding(const std::string& binding, const std::string& name, bool bTemporary = false);
64        const std::string& getBindingsFilename()
65            { return this->filename_; }
66        void setConfigValues();
67        void resetJoyStickAxes();
68
69    protected: // functions
70        void loadBindings();
71        void buttonThresholdChanged();
72        void initialiseJoyStickBindings();
73        void compilePointerLists();
74        // from JoyStickQuantityListener interface
75        virtual void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList);
76
77        void allDevicesUpdated(float dt);
78        void mouseUpdated(float dt);
79        void joyStickUpdated(unsigned int joyStick, float dt);
80        // internal
81        void tickHalfAxis(HalfAxis& halfAxis);
82
83        void buttonPressed (const KeyEvent& evt);
84        void buttonReleased(const KeyEvent& evt);
85        void buttonHeld    (const KeyEvent& evt);
86
87        void buttonPressed (MouseButtonCode::ByEnum button);
88        void buttonReleased(MouseButtonCode::ByEnum button);
89        void buttonHeld    (MouseButtonCode::ByEnum button);
90        void mouseMoved    (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize);
91        void mouseScrolled (int abs, int rel);
92
93        void buttonPressed (unsigned int device, JoyStickButtonCode::ByEnum button);
94        void buttonReleased(unsigned int device, JoyStickButtonCode::ByEnum button);
95        void buttonHeld    (unsigned int device, JoyStickButtonCode::ByEnum button);
96        void axisMoved     (unsigned int device, unsigned int axis, float value);
97
98    protected: // variables
99        //! Currently active joy sticks
100        std::vector<JoyStick*>  joySticks_;
101
102        //! Actual key bindings for keys on the keyboard
103        Button keys_            [KeyCode::numberOfKeys];
104        //! Number of mouse buttons in KeyBinder (+4)
105        static const unsigned int numberOfMouseButtons_ = MouseButtonCode::numberOfButtons + 4;
106        //! Actual key bindings for mouse buttons including the wheel(s)
107        Button mouseButtons_    [numberOfMouseButtons_];
108        //! Actual key bindings for mouse axes
109        HalfAxis mouseAxes_     [MouseAxisCode::numberOfAxes * 2];
110
111        //! Helper class to use something like std:vector<Button[64]>
112        struct JoyStickButtonVector
113        {
114            Button& operator[](unsigned int index) { return buttons[index]; }
115            Button buttons[JoyStickButtonCode::numberOfButtons];
116        };
117        //! Actual key bindings for joy stick buttons
118        std::vector<shared_ptr<JoyStickButtonVector> > joyStickButtons_;
119        //! Helper class to use something like std:vector<HalfAxis[48]>
120        struct JoyStickAxisVector
121        {
122            HalfAxis& operator[](unsigned int index) { return halfAxes[index]; }
123            HalfAxis halfAxes[JoyStickAxisCode::numberOfAxes * 2];
124        };
125        //! Actual key bindings for joy stick axes (and sliders)
126        std::vector<shared_ptr<JoyStickAxisVector> > joyStickAxes_;
127
128        //! Pointer map with all Buttons, including half axes
129        std::map<std::string, Button*> allButtons_;
130        //! Pointer list with all half axes
131        std::vector<HalfAxis*> allHalfAxes_;
132
133        /**
134        @brief
135            Commands that have additional parameters (axes) are executed at the end of
136            update() so that all values can be buffered for single execution.
137        */
138        std::vector<BufferedParamCommand*> paramCommandBuffer_;
139
140        //! Keeps track of the absolute mouse value (incl. scroll wheel)
141        int mousePosition_[2];
142        //! Used to derive mouse input if requested
143        int mouseRelative_[2];
144        float deriveTime_;
145
146        //! Name of the file used in this KeyBinder (constant!)
147        const std::string filename_;
148        //! Config file used. ConfigFileType::NoType in case of KeyDetector. Also indicates whether we've already loaded.
149        ConfigFileType configFile_;
150
151    private:
152        //##### ConfigValues #####
153        //! Whether to filter small value analog input
154        bool bFilterAnalogNoise_;
155        //! Threshold for analog triggers until which the state is 0.
156        float analogThreshold_;
157        //! Threshold for analog triggers until which the button is not pressed.
158        float buttonThreshold_;
159        //! Derive mouse input for absolute values?
160        bool bDeriveMouseInput_;
161        //! Accuracy of the mouse input deriver. The higher the more precise, but laggier.
162        float derivePeriod_;
163        //! mouse sensitivity
164        float mouseSensitivity_;
165        //! mouse sensitivity if mouse input is derived
166        float mouseSensitivityDerived_;
167        //! Equals one step of the mousewheel
168        int mouseWheelStepSize_;
169
170        //##### Constant config variables #####
171        // Use some value at about 1000. This can be configured with mouseSensitivity_ anyway.
172        static const int mouseClippingSize_ = 1024;
173    };
174
175
176    inline void KeyBinder::buttonPressed (const KeyEvent& evt)
177    { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnPress); }
178
179    inline void KeyBinder::buttonReleased(const KeyEvent& evt)
180    { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnRelease); }
181
182    inline void KeyBinder::buttonHeld    (const KeyEvent& evt)
183    { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnHold); }
184
185
186    inline void KeyBinder::buttonPressed (MouseButtonCode::ByEnum button)
187    { mouseButtons_[button].execute(KeybindMode::OnPress); }
188
189    inline void KeyBinder::buttonReleased(MouseButtonCode::ByEnum button)
190    { mouseButtons_[button].execute(KeybindMode::OnRelease); }
191
192    inline void KeyBinder::buttonHeld    (MouseButtonCode::ByEnum button)
193    { mouseButtons_[button].execute(KeybindMode::OnHold); }
194
195
196    inline void KeyBinder::buttonPressed (unsigned int device, JoyStickButtonCode::ByEnum button)
197    { (*joyStickButtons_[device])[button].execute(KeybindMode::OnPress); }
198
199    inline void KeyBinder::buttonReleased(unsigned int device, JoyStickButtonCode::ByEnum button)
200    { (*joyStickButtons_[device])[button].execute(KeybindMode::OnRelease); }
201
202    inline void KeyBinder::buttonHeld    (unsigned int device, JoyStickButtonCode::ByEnum button)
203    { (*joyStickButtons_[device])[button].execute(KeybindMode::OnHold); }
204
205    inline void KeyBinder::allDevicesUpdated(float dt)
206    {
207        // execute all buffered bindings (additional parameter)
208        for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
209        {
210            paramCommandBuffer_[i]->rel_ *= dt;
211            paramCommandBuffer_[i]->execute();
212        }
213
214        // always reset the relative movement of the mouse
215        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
216            mouseAxes_[i].relVal_ = 0.0f;
217    }
218}
219
220#endif /* _KeyBinder_H__ */
Note: See TracBrowser for help on using the repository browser.