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 
63  bool isLeftHand() const;
64  bool isRightHand() const;
65  bool isMouse() const;
66 
67  // Pointers can choose to implement these
68  virtual void setLength(float length) {}
69  virtual void setLockEndUUID(const QUuid& objectID, bool isAvatar, const glm::mat4& offsetMat = glm::mat4()) {}
70 
71  void update(unsigned int pointerID);
72  virtual void updateVisuals(const PickResultPointer& pickResult) = 0;
73  void generatePointerEvents(unsigned int pointerID, const PickResultPointer& pickResult);
74 
75  struct PickedObject {
76  PickedObject(const QUuid& objectID = QUuid(), IntersectionType type = IntersectionType::NONE) : objectID(objectID), type(type) {}
77 
78  QUuid objectID;
79  IntersectionType type;
80  } typedef PickedObject;
81 
82  using Buttons = std::unordered_set<std::string>;
83 
84  unsigned int getRayUID() { return _pickUID; }
85 
86 protected:
87  const unsigned int _pickUID;
88  bool _enabled;
89  bool _hover;
90 
91  // The parameters used to create this pointer when created through a script
92  QVariantMap _scriptParameters;
93 
94  virtual PointerEvent buildPointerEvent(const PickedObject& target, const PickResultPointer& pickResult, const std::string& button = "", bool hover = true) = 0;
95 
96  virtual PickedObject getHoveredObject(const PickResultPointer& pickResult) = 0;
97  virtual Buttons getPressedButtons(const PickResultPointer& pickResult) = 0;
98 
99  virtual bool shouldHover(const PickResultPointer& pickResult) { return true; }
100  virtual bool shouldTrigger(const PickResultPointer& pickResult) { return true; }
101  virtual PickResultPointer getPickResultCopy(const PickResultPointer& pickResult) const = 0;
102  virtual PickResultPointer getVisualPickResult(const PickResultPointer& pickResult) { return pickResult; };
103 
104  static const float POINTER_MOVE_DELAY;
105  static const float TOUCH_PRESS_TO_MOVE_DEADSPOT_SQUARED;
106 
107 private:
108  PickedObject _prevHoveredObject;
109  Buttons _prevButtons;
110  bool _prevEnabled { false };
111  bool _prevDoHover { false };
112  std::unordered_map<std::string, PickedObject> _triggeredObjects;
113 
114  PointerEvent::Button chooseButton(const std::string& button);
115 
116 };
117 
118 #endif // hifi_Pick_h
Represents a 2D or 3D pointer to the scripting engine. Exposed as PointerEvent
Definition: PointerEvent.h:30