Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/input/InputHandler.h

Last change on this file was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 5.9 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 _InputHandler_H__
30#define _InputHandler_H__
31
32#include "InputPrereqs.h"
33
34namespace orxonox
35{
36    /// A Vector class containing two integers @a x and @a y.
37    class IntVector2
38    {
39    public:
40        IntVector2() : x(0), y(0) { }
41        IntVector2(int _x, int _y) : x(_x), y(_y) { }
42        int x;
43        int y;
44    };
45
46    namespace ButtonEvent
47    {
48        //! Helper enum to deploy events with the help of templates
49        enum Value
50        {
51            Press,
52            Release,
53            Hold
54        };
55
56        //! Enables function overloading with integer values
57        template <ButtonEvent::Value Event>
58        struct EnumToType { };
59        typedef EnumToType<Press>   TPress;
60        typedef EnumToType<Release> TRelease;
61        typedef EnumToType<Hold>    THold;
62    }
63
64    namespace KeyboardModifier
65    {
66        //! Keyboard modifiers (shift, ctrl and alt)
67        enum Enum
68        {
69            Shift = 0x0000001,
70            Ctrl  = 0x0000010,
71            Alt   = 0x0000100
72        };
73    }
74
75    //! Event argument for key events
76    class _CoreExport KeyEvent
77    {
78    public:
79        KeyEvent(const OIS::KeyEvent& evt)
80            : key_(static_cast<KeyCode::ByEnum>(evt.key))
81            , text_(evt.text)
82            , modifiers_(0)
83        { }
84        KeyEvent(KeyCode::ByEnum key, unsigned int text, int modifiers)
85            : key_(key)
86            , text_(text)
87            , modifiers_(modifiers)
88        { }
89        bool operator==(const KeyEvent& rhs) const
90            { return rhs.key_ == key_; }
91        bool operator!=(const KeyEvent& rhs) const
92            { return rhs.key_ != key_; }
93        void setModifiers(int modifiers)
94            { modifiers_ = modifiers; }
95
96        bool isModifierDown(KeyboardModifier::Enum modifier) const
97            { return static_cast<KeyboardModifier::Enum>(modifier & modifiers_); }
98        KeyCode::ByEnum getKeyCode() const
99            { return key_; }
100        unsigned int getText() const { return text_; }
101
102    private:
103        KeyCode::ByEnum key_;
104        unsigned int text_;
105        int modifiers_;
106    };
107
108    /**
109    @brief
110        Base class for all input handlers like KeyBinder, InputBuffer, etc.
111
112        Derive from this class if you wish to receive input events.
113        But keep in mind that this is pointless without first having an InputState.
114    @note
115        The definitions for the button events with the weird arguments are simply
116        to avoid redundant code in the input devices.
117    */
118    class _CoreExport InputHandler
119    {
120    public:
121        virtual ~InputHandler() = default;
122
123        template<class T> void buttonEvent(unsigned int device, T button, ButtonEvent::TPress)
124            { this->buttonPressed(button); }
125        template<class T> void buttonEvent(unsigned int device, T button, ButtonEvent::TRelease)
126            { this->buttonReleased(button); }
127        template<class T> void buttonEvent(unsigned int device, T button, ButtonEvent::THold)
128            { this->buttonHeld(button); }
129
130        virtual void buttonPressed (const KeyEvent& evt) { }
131        virtual void buttonReleased(const KeyEvent& evt) { }
132        virtual void buttonHeld    (const KeyEvent& evt) { }
133
134        virtual void buttonPressed (MouseButtonCode::ByEnum button) { }
135        virtual void buttonReleased(MouseButtonCode::ByEnum button) { }
136        virtual void buttonHeld    (MouseButtonCode::ByEnum button) { }
137        virtual void mouseMoved    (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) { }
138        virtual void mouseScrolled (int abs, int rel) { }
139
140        virtual void buttonPressed (unsigned int joyStick, JoyStickButtonCode::ByEnum button) { }
141        virtual void buttonReleased(unsigned int joyStick, JoyStickButtonCode::ByEnum button) { }
142        virtual void buttonHeld    (unsigned int joyStick, JoyStickButtonCode::ByEnum button) { }
143        virtual void axisMoved     (unsigned int joyStick, unsigned int axis, float value){ }
144
145        virtual void keyboardUpdated(float dt) { }
146        virtual void mouseUpdated   (float dt) { }
147        virtual void joyStickUpdated(unsigned int joyStick, float dt) { }
148
149        virtual void allDevicesUpdated(float dt) { }
150
151        //! Use this input handler if you want to occupy a device in an input state.
152        static InputHandler EMPTY;
153    };
154
155    template<> inline void InputHandler::buttonEvent<JoyStickButtonCode::ByEnum>(unsigned int device, JoyStickButtonCode::ByEnum button, ButtonEvent::TPress)
156        { this->buttonPressed(device - InputDeviceEnumerator::FirstJoyStick, button); }
157    template<> inline void InputHandler::buttonEvent<JoyStickButtonCode::ByEnum>(unsigned int device, JoyStickButtonCode::ByEnum button, ButtonEvent::TRelease)
158        { this->buttonReleased(device - InputDeviceEnumerator::FirstJoyStick, button); }
159    template<> inline void InputHandler::buttonEvent<JoyStickButtonCode::ByEnum>(unsigned int device, JoyStickButtonCode::ByEnum button, ButtonEvent::THold)
160        { this->buttonHeld(device - InputDeviceEnumerator::FirstJoyStick, button); }
161}
162
163#endif /* _InputHandler_H__ */
Note: See TracBrowser for help on using the repository browser.