Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/libraries/core/input/KeyBinder.h @ 6301

Last change on this file since 6301 was 6214, checked in by scheusso, 15 years ago

a small fix in IOConsole
some changes in GUI-system and preparation for keybindings menu
fix in menu handling

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