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