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