Overte C++ Documentation
LeapMotionPlugin.h
1 //
2 // LeapMotionPlugin.h
3 //
4 // Created by David Rowe on 15 Jun 2017.
5 // Copyright 2017 High Fidelity, Inc.
6 //
7 // Distributed under the Apache License, Version 2.0.
8 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9 //
10 
11 #ifndef hifi_LeapMotionPlugin_h
12 #define hifi_LeapMotionPlugin_h
13 
14 #include <controllers/InputDevice.h>
15 #include <plugins/InputPlugin.h>
16 
17 // LeapMotion SDK
18 #include <Leap.h>
19 
20 class LeapMotionPlugin : public InputPlugin {
21  Q_OBJECT
22 
23 public:
24  // InputPlugin methods
25  virtual void pluginFocusOutEvent() override { _inputDevice->focusOutEvent(); }
26  virtual void pluginUpdate(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
27 
28  bool isHandController() const override { return true; }
29 
30  // Plugin methods
31  virtual const QString getName() const override { return NAME; }
32  const QString getID() const override { return LEAPMOTION_ID_STRING; }
33  bool isRunning() const override { return _active && _enabled; }
34  virtual void init() override;
35 
36  virtual bool activate() override;
37  virtual void deactivate() override;
38 
39  virtual void saveSettings() const override;
40  virtual void loadSettings() override;
41 
42 protected:
43  static const char* NAME;
44  static const char* LEAPMOTION_ID_STRING;
45  const float DEFAULT_DESKTOP_HEIGHT_OFFSET = 0.2f;
46  QString _sensorLocation;
47  float _desktopHeightOffset { DEFAULT_DESKTOP_HEIGHT_OFFSET };
48 
49  struct LeapMotionJoint {
50  glm::vec3 position;
51  glm::quat orientation;
52  };
53 
54  std::vector<LeapMotionJoint> _joints;
55  std::vector<LeapMotionJoint> _prevJoints;
56 
57  class InputDevice : public controller::InputDevice {
58  public:
59  friend class LeapMotionPlugin;
60 
61  InputDevice() : controller::InputDevice("LeapMotion") {}
62 
63  // Device functions
64  virtual controller::Input::NamedVector getAvailableInputs() const override;
65  virtual QString getDefaultMappingConfig() const override;
66  virtual void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override {};
67  virtual void focusOutEvent() override {};
68 
69  void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData,
70  const std::vector<LeapMotionPlugin::LeapMotionJoint>& joints,
71  const std::vector<LeapMotionPlugin::LeapMotionJoint>& prevJoints);
72 
73  void clearState();
74 
75  void setDektopHeightOffset(float desktopHeightOffset) { _desktopHeightOffset = desktopHeightOffset; };
76  void setIsLeapOnHMD(bool isLeapOnHMD) { _isLeapOnHMD = isLeapOnHMD; };
77 
78  private:
79  float _desktopHeightOffset { 0.0f };
80  bool _isLeapOnHMD { false };
81  };
82 
83  std::shared_ptr<InputDevice> _inputDevice { std::make_shared<InputDevice>() };
84 
85 private:
86  void applySensorLocation();
87  void applyDesktopHeightOffset();
88 
89  void processFrame(const Leap::Frame& frame);
90 
91  Leap::Controller _controller;
92 
93  bool _hasLeapMotionBeenConnected { false };
94  int64_t _lastFrameID { -1 };
95 };
96 
97 #endif // hifi_LeapMotionPlugin_h