[3274] | 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 |
---|
[3327] | 31 | @brief |
---|
| 32 | Implementation of InputDevice and InputDeviceTemplated |
---|
[3274] | 33 | */ |
---|
| 34 | |
---|
| 35 | #ifndef _InputDevice_H__ |
---|
| 36 | #define _InputDevice_H__ |
---|
| 37 | |
---|
| 38 | #include "InputPrereqs.h" |
---|
| 39 | |
---|
| 40 | #include <vector> |
---|
| 41 | #include <ois/OISInputManager.h> |
---|
[7174] | 42 | #include <ois/OISException.h> |
---|
[3274] | 43 | |
---|
[5929] | 44 | #include "util/Clock.h" |
---|
[8858] | 45 | #include "util/Output.h" |
---|
[5747] | 46 | #include "util/Exception.h" |
---|
[3274] | 47 | #include "InputState.h" |
---|
| 48 | |
---|
| 49 | namespace orxonox |
---|
| 50 | { |
---|
| 51 | /** |
---|
| 52 | @brief |
---|
[3327] | 53 | Abstract base class for all input devices (mouse, keyboard and joy sticks). |
---|
| 54 | |
---|
| 55 | It provides common virtual functions to be used by the InputManager. |
---|
[3274] | 56 | */ |
---|
| 57 | class InputDevice |
---|
| 58 | { |
---|
| 59 | public: |
---|
[3327] | 60 | //! Only resets the members |
---|
| 61 | InputDevice(unsigned int id) : bCalibrating_(false), deviceID_(id) { } |
---|
[11071] | 62 | virtual ~InputDevice() = default; |
---|
[3327] | 63 | //! Returns the device class (derived) name as string |
---|
| 64 | virtual std::string getClassName() const = 0; |
---|
| 65 | //! Updates the device which should in turn distribute events |
---|
[3274] | 66 | virtual void update(const Clock& time) = 0; |
---|
[3327] | 67 | //! Clear all button related buffers |
---|
[3274] | 68 | virtual void clearBuffers() = 0; |
---|
| 69 | |
---|
[3327] | 70 | //! Start calibrating (only useful for joy sticks) |
---|
[3274] | 71 | void startCalibration() |
---|
| 72 | { |
---|
| 73 | bCalibrating_ = true; |
---|
| 74 | this->calibrationStarted(); |
---|
| 75 | } |
---|
| 76 | |
---|
[3327] | 77 | //! Stop calibrating and evaluate the data (only useful for joy sticks) |
---|
[3274] | 78 | void stopCalibration() |
---|
| 79 | { |
---|
| 80 | this->calibrationStopped(); |
---|
| 81 | bCalibrating_ = false; |
---|
| 82 | } |
---|
| 83 | |
---|
[3327] | 84 | //! Returns a reference to the internal input state vector. Use with care! |
---|
| 85 | std::vector<InputState*>& getStateListRef() { return this->inputStates_; } |
---|
| 86 | //! Returns the ID of the device (the same as in InputDeviceEnumerator for mouse and keyboard) |
---|
| 87 | unsigned int getDeviceID() const { return this->deviceID_; } |
---|
| 88 | //! Tells whether the device is in calibration mode |
---|
| 89 | bool isCalibrating() const { return bCalibrating_; } |
---|
[3274] | 90 | |
---|
| 91 | protected: |
---|
[3327] | 92 | //! To be ovrridden by the subclass |
---|
[3274] | 93 | virtual void calibrationStarted() { } |
---|
[3327] | 94 | //! To be ovrridden by the subclass |
---|
[3274] | 95 | virtual void calibrationStopped() { } |
---|
| 96 | |
---|
[3327] | 97 | //! List of all input states that receive events from this device |
---|
[3274] | 98 | std::vector<InputState*> inputStates_; |
---|
| 99 | |
---|
| 100 | private: |
---|
[11071] | 101 | // non-copyable: |
---|
| 102 | InputDevice(const InputDevice&) = delete; |
---|
| 103 | InputDevice& operator=(const InputDevice&) = delete; |
---|
[3274] | 104 | |
---|
[3327] | 105 | bool bCalibrating_; //!< Whether the device is in calibration mode |
---|
| 106 | const unsigned int deviceID_; //!< ID of the device (the same as in InputDeviceEnumerator for mouse and keyboard) |
---|
[3274] | 107 | }; |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | @brief |
---|
[3327] | 111 | Heavily templated base class for all three input devices. |
---|
| 112 | |
---|
| 113 | The purpose of this class is not to provide an interface but rather |
---|
| 114 | to reduce code redundancy. This concerns device creation and destruction |
---|
| 115 | as well as common code for button events (press, release, hold). |
---|
| 116 | |
---|
| 117 | In order to derive from this class you have to supply it with a struct |
---|
| 118 | as template parameter that contains the necessary type traits. |
---|
[3274] | 119 | */ |
---|
| 120 | template <class Traits> |
---|
| 121 | class InputDeviceTemplated : public InputDevice |
---|
| 122 | { |
---|
| 123 | typedef typename Traits::DeviceClass DeviceClass; |
---|
| 124 | typedef typename Traits::OISDeviceClass OISDeviceClass; |
---|
| 125 | typedef typename Traits::ButtonType ButtonType; |
---|
| 126 | typedef typename Traits::ButtonTypeParam ButtonTypeParam; |
---|
| 127 | static const OIS::Type OISDeviceValue = Traits::OISDeviceValue; |
---|
| 128 | |
---|
| 129 | public: |
---|
[3327] | 130 | //! Creates the OIS device |
---|
| 131 | InputDeviceTemplated(unsigned int id, OIS::InputManager* oisInputManager) |
---|
[3274] | 132 | : InputDevice(id) |
---|
[3327] | 133 | , oisInputManager_(oisInputManager) |
---|
[3274] | 134 | { |
---|
[3327] | 135 | oisDevice_ = static_cast<OISDeviceClass*>(oisInputManager_->createInputObject(OISDeviceValue, true)); |
---|
[7271] | 136 | // Note: after the static_cast here, the cast this pointer becomes |
---|
[3327] | 137 | // invalid right until the subclass has been constructed! |
---|
[3274] | 138 | oisDevice_->setEventCallback(static_cast<DeviceClass*>(this)); |
---|
[8858] | 139 | orxout(verbose, context::input) << "Instantiated a " << this->getClassName() << endl; |
---|
[3274] | 140 | } |
---|
| 141 | |
---|
[3327] | 142 | //! Destroys the OIS device |
---|
[3274] | 143 | virtual ~InputDeviceTemplated() |
---|
| 144 | { |
---|
| 145 | try |
---|
| 146 | { |
---|
[3327] | 147 | oisInputManager_->destroyInputObject(oisDevice_); |
---|
[3274] | 148 | } |
---|
[7174] | 149 | catch (const OIS::Exception& ex) |
---|
[3274] | 150 | { |
---|
[8858] | 151 | orxout(internal_error, context::input) << this->getClassName() << " destruction failed: " << ex.eText << '\n' |
---|
| 152 | << "Potential resource leak!" << endl; |
---|
[3274] | 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
[3327] | 156 | //! Captures OIS events (which then get distributed to the derived class) and creates the button held events |
---|
[11071] | 157 | virtual void update(const Clock& time) override |
---|
[3274] | 158 | { |
---|
| 159 | oisDevice_->capture(); |
---|
| 160 | |
---|
[3327] | 161 | // Call all the states with the held button event |
---|
[11071] | 162 | for (ButtonType& button : pressedButtons_) |
---|
| 163 | for (InputState* state : inputStates_) |
---|
| 164 | state->template buttonEvent<ButtonEvent::THold, typename Traits::ButtonTypeParam>( |
---|
| 165 | this->getDeviceID(), static_cast<DeviceClass*>(this)->getButtonEventArg(button)); |
---|
[3274] | 166 | |
---|
[3327] | 167 | // Call states with device update events |
---|
[11071] | 168 | for (InputState* state : inputStates_) |
---|
| 169 | state->update(time.getDeltaTime(), this->getDeviceID()); |
---|
[3274] | 170 | |
---|
| 171 | static_cast<DeviceClass*>(this)->updateImpl(time); |
---|
| 172 | } |
---|
| 173 | |
---|
[3327] | 174 | //! Clears the list of pressed buttons and calls the derived class's method |
---|
[11071] | 175 | virtual void clearBuffers() override |
---|
[3274] | 176 | { |
---|
| 177 | pressedButtons_.clear(); |
---|
| 178 | static_cast<DeviceClass*>(this)->clearBuffersImpl(); |
---|
| 179 | } |
---|
| 180 | |
---|
[3327] | 181 | // Returns a pointer to the OIS device |
---|
| 182 | OISDeviceClass* getOISDevice() { return this->oisDevice_; } |
---|
| 183 | // Returns the name of the derived class as string |
---|
[11071] | 184 | virtual std::string getClassName() const override { return DeviceClass::getClassNameImpl(); } |
---|
[3327] | 185 | |
---|
[3274] | 186 | protected: |
---|
[3327] | 187 | //! Common code for all button pressed events (updates pressed buttons list and calls the input states) |
---|
[8351] | 188 | ORX_FORCEINLINE void buttonPressed(ButtonTypeParam button) |
---|
[3274] | 189 | { |
---|
| 190 | // check whether the button already is in the list (can happen when focus was lost) |
---|
| 191 | unsigned int iButton = 0; |
---|
| 192 | while (iButton < pressedButtons_.size() && pressedButtons_[iButton] != button) |
---|
| 193 | iButton++; |
---|
| 194 | if (iButton == pressedButtons_.size()) |
---|
| 195 | pressedButtons_.push_back(button); |
---|
| 196 | else |
---|
| 197 | return; // Button already pressed |
---|
| 198 | |
---|
| 199 | // Call states |
---|
[11071] | 200 | for (InputState* state : inputStates_) |
---|
| 201 | state->template buttonEvent<ButtonEvent::TPress, typename Traits::ButtonTypeParam>(this->getDeviceID(), static_cast<DeviceClass*>(this)->getButtonEventArg(button)); |
---|
[3274] | 202 | } |
---|
| 203 | |
---|
[3327] | 204 | //! Common code for all button released events (updates pressed buttons list and calls the input states) |
---|
[8351] | 205 | ORX_FORCEINLINE void buttonReleased(ButtonTypeParam button) |
---|
[3274] | 206 | { |
---|
| 207 | // remove the button from the pressedButtons_ list |
---|
[6417] | 208 | bool found = false; |
---|
[3274] | 209 | for (unsigned int iButton = 0; iButton < pressedButtons_.size(); iButton++) |
---|
| 210 | { |
---|
| 211 | if (pressedButtons_[iButton] == button) |
---|
| 212 | { |
---|
| 213 | pressedButtons_.erase(pressedButtons_.begin() + iButton); |
---|
[6417] | 214 | found = true; |
---|
[3274] | 215 | break; |
---|
| 216 | } |
---|
| 217 | } |
---|
[6417] | 218 | if (!found) |
---|
| 219 | return; // We ignore release strokes when the press was not captured |
---|
[3274] | 220 | |
---|
| 221 | // Call states |
---|
[11071] | 222 | for (InputState* state : inputStates_) |
---|
| 223 | state->template buttonEvent<ButtonEvent::TRelease, typename Traits::ButtonTypeParam>(this->getDeviceID(), static_cast<DeviceClass*>(this)->getButtonEventArg(button)); |
---|
[3274] | 224 | } |
---|
| 225 | |
---|
[3327] | 226 | //! Managed pointer to the OIS device |
---|
[3274] | 227 | OISDeviceClass* oisDevice_; |
---|
| 228 | |
---|
| 229 | private: |
---|
| 230 | //!< Fallback dummy function for static polymorphism |
---|
[3327] | 231 | void clearBuffersImpl() { } |
---|
| 232 | //!< Fallback dummy function for static polymorphism |
---|
| 233 | void updateImpl(const Clock& time) { } |
---|
| 234 | //!< Fallback dummy function for static polymorphism |
---|
[3274] | 235 | ButtonType& getButtonEventArg(ButtonType& button) { return button; } |
---|
| 236 | |
---|
[3327] | 237 | std::vector<ButtonType> pressedButtons_; //!< List of all buttons that are currently pressed down |
---|
| 238 | OIS::InputManager* oisInputManager_; //!< Pointer to the OIS InputManager that can create and destroy devices |
---|
[3274] | 239 | }; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | #endif /* _InputDevice_H__ */ |
---|