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