Overte C++ Documentation
TouchscreenVirtualPadDevice.h
1 //
2 // TouchscreenVirtualPadDevice.h
3 // input-plugins/src/input-plugins
4 //
5 // Created by Triplelexx on 1/31/16.
6 // Copyright 2016 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_TouchscreenVirtualPadDevice_h
13 #define hifi_TouchscreenVirtualPadDevice_h
14 
15 #include <controllers/InputDevice.h>
16 #include "InputPlugin.h"
17 #include <QtGui/qtouchdevice.h>
18 #if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
19 #include <QtGui/QList>
20 #else
21 #include <QTouchEvent>
22 #include <QtCore/QList>
23 #endif
24 #include "VirtualPadManager.h"
25 
26 class QTouchEvent;
27 class QGestureEvent;
28 
29 class TouchscreenVirtualPadDevice : public InputPlugin {
30 Q_OBJECT
31 public:
32 
33  // Plugin functions
34  virtual void init() override;
35  virtual void resize();
36  virtual bool isSupported() const override;
37  virtual const QString getName() const override { return NAME; }
38 
39  bool isHandController() const override { return false; }
40 
41  virtual void pluginFocusOutEvent() override { _inputDevice->focusOutEvent(); }
42  virtual void pluginUpdate(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
43 
44  void touchBeginEvent(const QTouchEvent* event);
45  void touchEndEvent(const QTouchEvent* event);
46  void touchUpdateEvent(const QTouchEvent* event);
47  void touchGestureEvent(const QGestureEvent* event);
48 
49  static const char* NAME;
50 
51  int _viewTouchUpdateCount;
52  enum TouchAxisChannel {
53  LX,
54  LY,
55  RX,
56  RY
57  };
58 
59  enum TouchButtonChannel {
60  JUMP,
61  RB
62  };
63 
64 protected:
65 
66  class InputDevice : public controller::InputDevice {
67  public:
68  InputDevice() : controller::InputDevice("TouchscreenVirtualPad") {}
69  private:
70  // Device functions
71  virtual controller::Input::NamedVector getAvailableInputs() const override;
72  virtual QString getDefaultMappingConfig() const override;
73 
74  virtual bool triggerHapticPulse(float strength, float duration, uint16_t index) override;
75  virtual void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
76  virtual void focusOutEvent() override;
77 
78  friend class TouchscreenVirtualPadDevice;
79 
80  controller::Input makeInput(TouchAxisChannel axis) const;
81  controller::Input makeInput(TouchButtonChannel button) const;
82  };
83 
84 public:
85  const std::shared_ptr<InputDevice>& getInputDevice() const { return _inputDevice; }
86 
87 protected:
88 
89  enum TouchType {
90  MOVE = 1,
91  VIEW,
92  JUMP_BUTTON,
93  RB_BUTTON
94  };
95 
96  class TouchscreenButton {
97  public:
98 
99  TouchscreenButton() {};
100 
101  TouchscreenButton(TouchButtonChannel channelIn, TouchType touchTypeIn, float buttonRadiusIn, glm::vec2 buttonPositionIn,
102  std::shared_ptr<InputDevice> inputDeviceIn);
103 
104  void touchBegin(glm::vec2 touchPoint);
105  void touchUpdate(glm::vec2 touchPoint);
106  void touchEnd();
107  bool touchBeginIsValid(glm::vec2 touchPoint);
108 
109  bool hasValidTouch { false };
110  int currentTouchId;
111 
112  // per event tmp values
113  int _candidatePointIdx { -1 };
114  bool _found { false };
115  void resetEventValues();
116 
117  glm::vec2 buttonPosition;
118  float buttonRadius;
119  TouchType touchType;
120  TouchButtonChannel channel;
121 
122  std::shared_ptr<InputDevice> _inputDevice;
123 
124  };
125 
126  class TouchscreenButtonsManager {
127  public:
128 
129  TouchscreenButtonsManager();
130 
131  QVector<TouchscreenButton> buttons;
132 
133  void addButton(TouchscreenButton button);
134  int buttonsCount() {
135  return buttons.size();
136  }
137 
138  void resetEventValues();
139  bool processOngoingTouch(glm::vec2 thisPoint, int thisPointId);
140  bool findStartingTouchPointCandidate(glm::vec2 thisPoint, int thisPointId, int thisPointIdx, std::map<int, TouchType> &globalUnusedTouches);
141  void saveUnusedTouches(std::map<int, TouchType> &unusedTouchesInEvent, glm::vec2 thisPoint, int thisPointId);
142  void processBeginOrEnd(glm::vec2 thisPoint, const QList<QTouchEvent::TouchPoint>& tPoints, std::map<int, TouchType> globalUnusedTouches);
143 
144  void endTouchForAll();
145  bool touchBeginInvalidForAllButtons(glm::vec2 touchPoint);
146  };
147 
148  float _lastPinchScale;
149  float _pinchScale;
150  float _screenDPI;
151  qreal _screenDPIProvided;
152  glm::vec2 _screenDPIScale;
153 
154  bool _moveHasValidTouch;
155  glm::vec2 _moveRefTouchPoint;
156  glm::vec2 _moveCurrentTouchPoint;
157  int _moveCurrentTouchId;
158 
159  bool _viewHasValidTouch;
160  glm::vec2 _viewRefTouchPoint;
161  glm::vec2 _viewCurrentTouchPoint;
162  int _viewCurrentTouchId;
163 
164  std::map<int, TouchType> _unusedTouches;
165 
166  int _touchPointCount;
167  int _screenWidthCenter;
168  std::shared_ptr<InputDevice> _inputDevice { std::make_shared<InputDevice>() };
169 
170  bool _fixedPosition;
171  glm::vec2 _fixedCenterPosition;
172  float _fixedRadius;
173  float _fixedRadiusForCalc;
174  int _extraBottomMargin {0};
175 
176  float _buttonRadius;
177 
178  TouchscreenButtonsManager _buttonsManager;
179 
180  void moveTouchBegin(glm::vec2 touchPoint);
181  void moveTouchUpdate(glm::vec2 touchPoint);
182  void moveTouchEnd();
183  bool moveTouchBeginIsValid(glm::vec2 touchPoint);
184 
185  void viewTouchBegin(glm::vec2 touchPoint);
186  void viewTouchUpdate(glm::vec2 touchPoint);
187  void viewTouchEnd();
188  bool viewTouchBeginIsValid(glm::vec2 touchPoint);
189 
190  void setupControlsPositions(VirtualPad::Manager& virtualPadManager, bool force = false);
191 
192  void processInputDeviceForMove(VirtualPad::Manager& virtualPadManager);
193  glm::vec2 clippedPointInCircle(float radius, glm::vec2 origin, glm::vec2 touchPoint);
194 
195  void processUnusedTouches(std::map<int, TouchType> unusedTouchesInEvent);
196 
197  void processInputDeviceForView();
198 // just for debug
199 private:
200  void debugPoints(const QTouchEvent* event, QString who);
201 
202 };
203 
204 #endif // hifi_TouchscreenVirtualPadDevice_h