Overte C++ Documentation
InputDevice.h
1 //
2 // InputDevice.h
3 // input-plugins/src/input-plugins
4 //
5 // Created by Sam Gondelman on 7/15/2015
6 // Copyright 2015 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 #pragma once
12 
13 #include <memory>
14 #include <map>
15 #include <unordered_set>
16 
17 #include <QtCore/QString>
18 
19 #include "AxisValue.h"
20 #include "Pose.h"
21 #include "Input.h"
22 #include "StandardControls.h"
23 #include "DeviceProxy.h"
24 
25 
26 // Event types for each controller
27 const unsigned int CONTROLLER_0_EVENT = 1500U;
28 const unsigned int CONTROLLER_1_EVENT = 1501U;
29 
30 namespace controller {
31 
32 class Endpoint;
33 using EndpointPointer = std::shared_ptr<Endpoint>;
34 
35 /*@jsdoc
36  * <p>Some controller actions may be associated with one or both hands:</p>
37  * <table>
38  * <thead>
39  * <tr><th>Value</th><th>Description</th></tr>
40  * </thead>
41  * <tbody>
42  * <tr><td><code>0</code></td><td>Left hand.</td></tr>
43  * <tr><td><code>1</code></td><td>Right hand.</td></tr>
44  * <tr><td><code>2</code></td><td>Both hands.</td></tr>
45  * </tbody>
46  * </table>
47  * @typedef {number} Controller.Hand
48  */
49 enum Hand {
50  LEFT = 0,
51  RIGHT,
52  BOTH
53 };
54 
55 /*@jsdoc
56  * <p>The <code>Controller.Hardware</code> object has properties representing standard and hardware-specific controller and
57  * computer outputs, plus predefined actions on Interface and the user's avatar. <em>Read-only.</em></p>
58  * <p>The outputs can be mapped to actions or functions in a {@link RouteObject} mapping. Additionally, hardware-specific
59  * controller outputs can be mapped to standard controller outputs.
60  * <p>Controllers typically implement a subset of the {@link Controller.Standard} controls, plus they may implement some extras.
61  * Some common controllers are included in the table. You can see the outputs provided by these and others by
62  * viewing their {@link Controller.MappingJSON|MappingJSON} files at
63  * <a href="https://github.com/highfidelity/hifi/tree/master/interface/resources/controllers">
64  * https://github.com/highfidelity/hifi/tree/master/interface/resources/controllers</a>.</p>
65  *
66  * <table>
67  * <thead>
68  * <tr><th>Property</th><th>Type</th><th>Description</th></tr>
69  * </thead>
70  * <tbody>
71  * <tr><td><code>Controller.Hardware.Actions</code></td><td>object</td><td>Synonym for {@link Controller.Actions}.</td></tr>
72  * <tr><td><code>Controller.Hardware.Application</code></td><td>object</td><td>Interface state outputs. See
73  * {@link Controller.Hardware-Application}.</td></tr>
74  * <tr><td><code>Controller.Hardware.Keyboard</code></td><td>object</td><td>Keyboard, mouse, and touch pad outputs. See
75  * {@link Controller.Hardware-Keyboard}.</td></tr>
76  * <tr><td><code>Controller.Hardware.OculusTouch</code></td><td>object</td><td>Oculus Rift HMD outputs. See
77  * {@link Controller.Hardware-OculusTouch}.</td></tr>
78  * <tr><td><code>Controller.Hardware.Vive</code></td><td>object</td><td>Vive HMD outputs. See
79  * {@link Controller.Hardware-Vive}.</td></tr>
80  * </tbody>
81  * </table>
82  * @typedef {object} Controller.Hardware
83  * @example <caption>List all the currently available <code>Controller.Hardware</code> properties.</caption>
84  * function printProperties(string, item) {
85  * print(string);
86  * for (var key in item) {
87  * if (item.hasOwnProperty(key)) {
88  * printProperties(string + "." + key, item[key]);
89  * }
90  * }
91  * }
92  *
93  * printProperties("Controller.Hardware", Controller.Hardware);
94  */
95 // NOTE: If something inherits from both InputDevice and InputPlugin, InputPlugin must go first.
96 // e.g. class Example : public InputPlugin, public InputDevice
97 // instead of class Example : public InputDevice, public InputPlugin
98 class InputDevice {
99 public:
100  InputDevice(const QString& name) : _name(name) {}
101  virtual ~InputDevice() = default;
102 
103  using Pointer = std::shared_ptr<InputDevice>;
104 
105  typedef std::unordered_set<int> ButtonPressedMap;
106  typedef std::map<int, AxisValue> AxisStateMap;
107  typedef std::map<int, Pose> PoseStateMap;
108 
109  // Get current state for each channel
110  float getButton(int channel) const;
111  AxisValue getAxis(int channel) const;
112  Pose getPose(int channel) const;
113 
114  AxisValue getValue(const Input& input) const;
115  AxisValue getValue(ChannelType channelType, uint16_t channel) const;
116  Pose getPoseValue(uint16_t channel) const;
117 
118  const QString& getName() const { return _name; }
119 
120  // By default, Input Devices do not support haptics
121  virtual bool triggerHapticPulse(float strength, float duration, uint16_t index) { return false; }
122 
123  // Update call MUST be called once per simulation loop
124  // It takes care of updating the action states and deltas
125  virtual void update(float deltaTime, const InputCalibrationData& inputCalibrationData) {};
126 
127  virtual void focusOutEvent() {};
128 
129  int getDeviceID() { return _deviceID; }
130  void setDeviceID(int deviceID) { _deviceID = deviceID; }
131 
132  Input makeInput(StandardButtonChannel button) const;
133  Input makeInput(StandardAxisChannel axis) const;
134  Input makeInput(StandardPoseChannel pose) const;
135  Input::NamedPair makePair(StandardButtonChannel button, const QString& name) const;
136  Input::NamedPair makePair(StandardAxisChannel button, const QString& name) const;
137  Input::NamedPair makePair(StandardPoseChannel button, const QString& name) const;
138 
139 protected:
140  friend class UserInputMapper;
141 
142  virtual Input::NamedVector getAvailableInputs() const = 0;
143  virtual QStringList getDefaultMappingConfigs() const { return QStringList() << getDefaultMappingConfig(); }
144  virtual QString getDefaultMappingConfig() const { return QString(); }
145  virtual EndpointPointer createEndpoint(const Input& input) const;
146 
147  uint16_t _deviceID { Input::INVALID_DEVICE };
148 
149  const QString _name;
150 
151  ButtonPressedMap _buttonPressedMap;
152  AxisStateMap _axisStateMap;
153  PoseStateMap _poseStateMap;
154 };
155 
156 }