Overte C++ Documentation
Keyboard.h
1 //
2 // Keyboard.h
3 // interface/src/scripting
4 //
5 // Created by Dante Ruiz on 2018-08-27.
6 // Copyright 2017 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_Keyboard_h
13 #define hifi_Keyboard_h
14 
15 #include <map>
16 #include <memory>
17 #include <vector>
18 
19 #include <QtCore/QObject>
20 #include <QTimer>
21 #include <QHash>
22 #include <QUuid>
23 #include <DependencyManager.h>
24 #include <Sound.h>
25 #include <shared/ReadWriteLockable.h>
26 #include <SettingHandle.h>
27 
28 class PointerEvent;
29 
30 
31 class Key {
32 public:
33  Key() = default;
34  ~Key() = default;
35 
36  enum Type {
37  CHARACTER,
38  CAPS,
39  CLOSE,
40  LAYER,
41  BACKSPACE,
42  SPACE,
43  ENTER,
44  SELECT_ALL,
45  COPY,
46  CUT,
47  PASTE,
48  CURSOR_UP,
49  CURSOR_RIGHT,
50  CURSOR_DOWN,
51  CURSOR_LEFT,
52  };
53 
54  static Key::Type getKeyTypeFromString(const QString& keyTypeString);
55 
56  QUuid getID() const { return _keyID; }
57  void setID(const QUuid& id) { _keyID = id; }
58 
59  void startTimer(int time);
60  bool timerFinished();
61 
62  void setKeyString(QString keyString) { _keyString = keyString; }
63  QString getKeyString(bool toUpper) const;
64  int getScanCode(bool toUpper) const;
65 
66  bool isPressed() const { return _pressed; }
67  void setIsPressed(bool pressed) { _pressed = pressed; }
68 
69  void setSwitchToLayerIndex(int layerIndex) { _switchToLayer = layerIndex; }
70  int getSwitchToLayerIndex() const { return _switchToLayer; }
71 
72  Type getKeyType() const { return _type; }
73  void setKeyType(Type type) { _type = type; }
74 
75  glm::vec3 getCurrentLocalPosition() const { return _currentLocalPosition; }
76 
77  void saveDimensionsAndLocalPosition();
78 
79  void scaleKey(float sensorToWorldScale);
80 private:
81  Type _type { Type::CHARACTER };
82 
83  int _switchToLayer { 0 };
84  bool _pressed { false };
85 
86  QUuid _keyID;
87  QString _keyString;
88 
89  glm::vec3 _originalLocalPosition;
90  glm::vec3 _originalDimensions;
91  glm::vec3 _currentLocalPosition;
92  bool _originalDimensionsAndLocalPositionSaved { false };
93 
94  std::shared_ptr<QTimer> _timer { std::make_shared<QTimer>() };
95 };
96 
97 class Keyboard : public QObject, public Dependency, public ReadWriteLockable {
98  Q_OBJECT
99 public:
100  Keyboard();
101  void createKeyboard();
102  void registerKeyboardHighlighting();
103  bool isRaised() const;
104  void setRaised(bool raised, bool inputToHudUI = false);
105  void setResetKeyboardPositionOnRaise(bool reset);
106  bool isPassword() const;
107  void setPassword(bool password);
108  void enableRightMallet();
109  void scaleKeyboard(float sensorToWorldScale);
110  void enableLeftMallet();
111  void disableRightMallet();
112  void disableLeftMallet();
113 
114  void setLeftHandLaser(unsigned int leftHandLaser);
115  void setRightHandLaser(unsigned int rightHandLaser);
116 
117  void setPreferMalletsOverLasers(bool preferMalletsOverLasers);
118  bool getPreferMalletsOverLasers() const;
119 
120  bool getUse3DKeyboard() const;
121  void setUse3DKeyboard(bool use);
122  bool containsID(const QUuid& id) const;
123 
124  void loadKeyboardFile(const QString& keyboardFile);
125  QSet<QUuid> getKeyIDs();
126  QUuid getAnchorID();
127 
128 public slots:
129  void handleTriggerBegin(const QUuid& id, const PointerEvent& event);
130  void handleTriggerEnd(const QUuid& id, const PointerEvent& event);
131  void handleTriggerContinue(const QUuid& id, const PointerEvent& event);
132  void handleHoverBegin(const QUuid& id, const PointerEvent& event);
133  void handleHoverEnd(const QUuid& id, const PointerEvent& event);
134 
135 private:
136  struct Anchor {
137  QUuid entityID;
138  glm::vec3 originalDimensions;
139  };
140 
141  struct BackPlate {
142  QUuid entityID;
143  glm::vec3 dimensions;
144  glm::vec3 localPosition;
145  };
146 
147  struct TextDisplay {
148  float lineHeight;
149  QUuid entityID;
150  glm::vec3 localPosition;
151  glm::vec3 dimensions;
152  };
153 
154  void raiseKeyboard(bool raise) const;
155  void raiseKeyboardAnchor(bool raise) const;
156  void enableStylus();
157  void disableStylus();
158  void enableSelectionLists();
159  void disableSelectionLists();
160 
161  void setLayerIndex(int layerIndex);
162  void clearKeyboardKeys();
163  void switchToLayer(int layerIndex);
164  void updateTextDisplay();
165  bool shouldProcessEntityAndPointerEvent(const PointerEvent& event) const;
166  bool shouldProcessPointerEvent(const PointerEvent& event) const;
167  bool shouldProcessEntity() const;
168  void addIncludeItemsToMallets();
169 
170  void startLayerSwitchTimer();
171  bool isLayerSwitchTimerFinished() const;
172  void handleSpecialKey(Key::Type keyType);
173 
174  bool _raised { false };
175  bool _resetKeyboardPositionOnRaise { true };
176  bool _password { false };
177  bool _capsEnabled { false };
178  int _layerIndex { 0 };
179  Setting::Handle<bool> _preferMalletsOverLasers { "preferMalletsOverLaser", true };
180  unsigned int _leftHandStylus { 0 };
181  unsigned int _rightHandStylus { 0 };
182  unsigned int _leftHandLaser { 0 };
183  unsigned int _rightHandLaser { 0 };
184  SharedSoundPointer _keySound { nullptr };
185  std::shared_ptr<QTimer> _layerSwitchTimer { std::make_shared<QTimer>() };
186 
187  mutable ReadWriteLockable _use3DKeyboardLock;
188  mutable ReadWriteLockable _handLaserLock;
189  mutable ReadWriteLockable _preferMalletsOverLasersSettingLock;
190  mutable ReadWriteLockable _ignoreItemsLock;
191 
192  Setting::Handle<bool> _use3DKeyboard { "use3DKeyboard", true };
193 
194  QString _typedCharacters;
195  TextDisplay _textDisplay;
196  Anchor _anchor;
197  BackPlate _backPlate;
198 
199  QSet<QUuid> _itemsToIgnore;
200  std::vector<QHash<QUuid, Key>> _keyboardLayers;
201 
202  // Send keyboard events to hud UI if true
203  std::atomic<bool> _inputToHudUI { false };
204  bool _created { false };
205 };
206 
207 #endif
Represents a 2D or 3D pointer to the scripting engine. Exposed as PointerEvent
Definition: PointerEvent.h:32