Overte C++ Documentation
Joystick.h
1 //
2 // Joystick.h
3 // input-plugins/src/input-plugins
4 //
5 // Created by Stephen Birarda on 2014-09-23.
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_Joystick_h
13 #define hifi_Joystick_h
14 
15 #include <qobject.h>
16 #include <qvector.h>
17 
18 #include <SDL2/SDL.h>
19 #undef main
20 
21 #include <controllers/InputDevice.h>
22 #include <controllers/StandardControls.h>
23 
24 class Joystick : public QObject, public controller::InputDevice {
25  Q_OBJECT
26  Q_PROPERTY(QString name READ getName)
27  Q_PROPERTY(int instanceId READ getInstanceId)
28 
29 public:
30  using Pointer = std::shared_ptr<Joystick>;
31 
32  const QString getName() const { return _name; }
33 
34  SDL_GameController* getGameController() { return _sdlGameController; }
35 
36  // Device functions
37  virtual controller::Input::NamedVector getAvailableInputs() const override;
38  virtual QString getDefaultMappingConfig() const override;
39  virtual void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
40  virtual void focusOutEvent() override;
41 
42  bool triggerHapticPulse(float strength, float duration, uint16_t index) override;
43 
44  Joystick() : InputDevice("GamePad") {}
45  ~Joystick();
46 
47  Joystick(SDL_JoystickID instanceId, SDL_GameController* sdlGameController);
48 
49  void closeJoystick();
50 
51  void handleAxisEvent(const SDL_ControllerAxisEvent& event);
52  void handleButtonEvent(const SDL_ControllerButtonEvent& event);
53 
54  int getInstanceId() const { return _instanceId; }
55 
56 private:
57  SDL_GameController* _sdlGameController;
58  SDL_Joystick* _sdlJoystick;
59  SDL_Haptic* _sdlHaptic;
60  SDL_JoystickID _instanceId;
61 
62  mutable controller::Input::NamedVector _availableInputs;
63 };
64 
65 #endif // hifi_Joystick_h