Overte C++ Documentation
Pointer.h
1 //
2 // Created by Sam Gondelman 10/17/2017
3 // Copyright 2017 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 #ifndef hifi_Pointer_h
9 #define hifi_Pointer_h
10 
11 #include <unordered_set>
12 #include <unordered_map>
13 
14 #include <QtCore/QUuid>
15 #include <QVector>
16 #include <QVariant>
17 
18 #include <shared/ReadWriteLockable.h>
19 #include "Pick.h"
20 
21 #include <controllers/impl/Endpoint.h>
22 #include "PointerEvent.h"
23 
24 #include "Pick.h"
25 
26 class PointerTrigger {
27 public:
28  PointerTrigger(controller::Endpoint::Pointer endpoint, const std::string& button) : _endpoint(endpoint), _button(button) {}
29 
30  controller::Endpoint::Pointer getEndpoint() const { return _endpoint; }
31  const std::string& getButton() const { return _button; }
32 
33 private:
34  controller::Endpoint::Pointer _endpoint;
35  std::string _button { "" };
36 };
37 
38 using PointerTriggers = std::vector<PointerTrigger>;
39 
40 class Pointer : protected ReadWriteLockable {
41 public:
42  Pointer(unsigned int uid, bool enabled, bool hover) : _pickUID(uid), _enabled(enabled), _hover(hover) {}
43 
44  virtual ~Pointer();
45 
46  virtual void enable();
47  virtual void disable();
48  virtual bool isEnabled();
49  virtual PickQuery::PickType getType() const = 0;
50  virtual PickResultPointer getPrevPickResult();
51 
52  virtual void setRenderState(const std::string& state) = 0;
53  virtual void editRenderState(const std::string& state, const QVariant& startProps, const QVariant& pathProps, const QVariant& endProps) = 0;
54 
55  virtual QVariantMap toVariantMap() const;
56  virtual void setScriptParameters(const QVariantMap& scriptParameters);
57  virtual QVariantMap getScriptParameters() const;
58 
59  virtual void setPrecisionPicking(bool precisionPicking);
60  virtual void setIgnoreItems(const QVector<QUuid>& ignoreItems) const;
61  virtual void setIncludeItems(const QVector<QUuid>& includeItems) const;
62  virtual void setDelay(float delay) const;
63 
64  bool isLeftHand() const;
65  bool isRightHand() const;
66  bool isMouse() const;
67 
68  // Pointers can choose to implement these
69  virtual void setLength(float length) {}
70  virtual void setLockEndUUID(const QUuid& objectID, bool isAvatar, const glm::mat4& offsetMat = glm::mat4()) {}
71 
72  void update(unsigned int pointerID);
73  virtual void updateVisuals(const PickResultPointer& pickResult) = 0;
74  void generatePointerEvents(unsigned int pointerID, const PickResultPointer& pickResult);
75 
76  struct PickedObject {
77  PickedObject(const QUuid& objectID = QUuid(), IntersectionType type = IntersectionType::NONE) : objectID(objectID), type(type) {}
78 
79  QUuid objectID;
80  IntersectionType type;
81  } typedef PickedObject;
82 
83  using Buttons = std::unordered_set<std::string>;
84 
85  unsigned int getRayUID() { return _pickUID; }
86 
87 protected:
88  const unsigned int _pickUID;
89  bool _enabled;
90  bool _hover;
91 
92  // The parameters used to create this pointer when created through a script
93  QVariantMap _scriptParameters;
94 
95  virtual PointerEvent buildPointerEvent(const PickedObject& target, const PickResultPointer& pickResult, const std::string& button = "", bool hover = true) = 0;
96 
97  virtual PickedObject getHoveredObject(const PickResultPointer& pickResult) = 0;
98  virtual Buttons getPressedButtons(const PickResultPointer& pickResult) = 0;
99  virtual glm::vec2 getScroll(const PickResultPointer& pickResult) = 0;
100 
101  virtual bool shouldHover(const PickResultPointer& pickResult) { return true; }
102  virtual bool shouldTrigger(const PickResultPointer& pickResult) { return true; }
103  virtual bool shouldScroll(const PickResultPointer& pickResult) { return true; }
104  virtual PickResultPointer getPickResultCopy(const PickResultPointer& pickResult) const = 0;
105  virtual PickResultPointer getVisualPickResult(const PickResultPointer& pickResult) { return pickResult; };
106 
107  static const float POINTER_MOVE_DELAY;
108  static const float TOUCH_PRESS_TO_MOVE_DEADSPOT_SQUARED;
109 
110 private:
111  PickedObject _prevHoveredObject;
112  Buttons _prevButtons;
113  bool _prevEnabled { false };
114  bool _prevDoHover { false };
115  std::unordered_map<std::string, PickedObject> _triggeredObjects;
116 
117  PointerEvent::Button chooseButton(const std::string& button);
118 
119 };
120 
121 #endif // hifi_Pick_h
Represents a 2D or 3D pointer to the scripting engine. Exposed as PointerEvent
Definition: PointerEvent.h:32