Overte C++ Documentation
SixenseManager.h
1 //
2 // SixenseManager.h
3 // input-plugins/src/input-plugins
4 //
5 // Created by Andrzej Kapolka on 11/15/13.
6 // Copyright 2013 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_SixenseManager_h
13 #define hifi_SixenseManager_h
14 
15 #include <SimpleMovingAverage.h>
16 
17 #include <controllers/InputDevice.h>
18 #include <controllers/StandardControls.h>
19 
20 #include <plugins/InputPlugin.h>
21 
22 struct _sixenseControllerData;
23 using SixenseControllerData = _sixenseControllerData;
24 
25 // Handles interaction with the Sixense SDK (e.g., Razer Hydra).
26 class SixenseManager : public InputPlugin {
27  Q_OBJECT
28 public:
29  // Plugin functions
30  virtual bool isSupported() const override;
31  virtual const QString getName() const override { return NAME; }
32  virtual const QString getID() const override { return SIXENSE_ID_STRING; }
33 
34  // Sixense always seems to initialize even if the hydras are not present. Is there
35  // a way we can properly detect whether the hydras are present?
36  // bool isHandController() const override { return true; }
37 
38  virtual void init() override;
39 
40  virtual bool activate() override;
41  virtual void deactivate() override;
42 
43  virtual void pluginFocusOutEvent() override { _inputDevice->focusOutEvent(); }
44  virtual void pluginUpdate(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
45 
46  virtual void saveSettings() const override;
47  virtual void loadSettings() override;
48 
49 public slots:
50  void setSixenseFilter(bool filter);
51 
52 private:
53  static const int MAX_NUM_AVERAGING_SAMPLES = 50; // At ~100 updates per seconds this means averaging over ~.5s
54  static const int CALIBRATION_STATE_IDLE = 0;
55  static const int CALIBRATION_STATE_IN_PROGRESS = 1;
56  static const int CALIBRATION_STATE_COMPLETE = 2;
57  static const glm::vec3 DEFAULT_AVATAR_POSITION;
58  static const float CONTROLLER_THRESHOLD;
59 
60  class InputDevice : public controller::InputDevice {
61  public:
62  InputDevice() : controller::InputDevice("Hydra") {}
63  void setDebugDrawRaw(bool flag);
64  void setDebugDrawCalibrated(bool flag);
65  private:
66  // Device functions
67  virtual controller::Input::NamedVector getAvailableInputs() const override;
68  virtual QString getDefaultMappingConfig() const override;
69  virtual void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
70  virtual void focusOutEvent() override;
71 
72  void handleButtonEvent(unsigned int buttons, bool left);
73  void handlePoseEvent(float deltaTime, const controller::InputCalibrationData& inputCalibrationData, const glm::vec3& position, const glm::quat& rotation, bool left);
74  void updateCalibration(SixenseControllerData* controllers);
75 
76  friend class SixenseManager;
77 
78  int _calibrationState { CALIBRATION_STATE_IDLE };
79  // these are calibration results
80  glm::vec3 _avatarPosition { DEFAULT_AVATAR_POSITION }; // in hydra-frame
81  glm::quat _avatarRotation; // in hydra-frame
82 
83  float _lastDistance;
84  bool _requestReset { false };
85  bool _debugDrawRaw { false };
86  bool _debugDrawCalibrated { false };
87  // these are measured values used to compute the calibration results
88  quint64 _lockExpiry;
89  glm::vec3 _averageLeft;
90  glm::vec3 _averageRight;
91  glm::vec3 _reachLeft;
92  glm::vec3 _reachRight;
93  };
94 
95  std::shared_ptr<InputDevice> _inputDevice { std::make_shared<InputDevice>() };
96 
97  static const char* NAME;
98  static const char* SIXENSE_ID_STRING;
99 
100  static bool _isEnabled;
101  static bool _sixenseLoaded;
102 };
103 
104 #endif // hifi_SixenseManager_h
105