Overte C++ Documentation
PointerEvent.h
1 //
2 // PointerEvent.h
3 // script-engine/src
4 //
5 // Created by Anthony Thibault on 2016-8-11.
6 // Copyright 2016 High Fidelity, Inc.
7 // Copyright 2023 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 // SPDX-License-Identifier: Apache-2.0
12 //
13 
16 
17 #ifndef hifi_PointerEvent_h
18 #define hifi_PointerEvent_h
19 
20 #include <Qt>
21 
22 #include <stdint.h>
23 #include <glm/glm.hpp>
24 
25 #include "ScriptValue.h"
26 
27 class ScriptEngine;
28 
29 #define POINTEREVENT_SCROLL_SENSITIVITY -16.0f
30 
32 class PointerEvent {
33 public:
34  enum Button {
35  NoButtons = 0x0,
36  PrimaryButton = 0x1,
37  SecondaryButton = 0x2,
38  TertiaryButton = 0x4
39  };
40 
41  enum EventType {
42  Press, // A button has just been pressed
43  DoublePress, // A button has just been double pressed
44  Release, // A button has just been released
45  Move, // The pointer has just moved
46  Scroll, // Thumbstick scrolling
47  NumEventTypes
48  };
49 
50  PointerEvent() {}
51  PointerEvent(EventType type, uint32_t id);
52  PointerEvent(EventType type, uint32_t id, const glm::vec2& pos2D, Button button, uint32_t buttons, Qt::KeyboardModifiers keyboardModifiers);
53  PointerEvent(const glm::vec2& pos2D, const glm::vec3& pos3D, const glm::vec3& normal, const glm::vec3& direction);
54  PointerEvent(EventType type, uint32_t id,
55  const glm::vec2& pos2D, const glm::vec3& pos3D,
56  const glm::vec3& normal, const glm::vec3& direction,
57  Button button = NoButtons, uint32_t buttons = NoButtons, Qt::KeyboardModifiers keyboardModifiers = Qt::NoModifier);
58 
59  static ScriptValue toScriptValue(ScriptEngine* engine, const PointerEvent& event);
60  static bool fromScriptValue(const ScriptValue& object, PointerEvent& event);
61 
62  ScriptValue toScriptValue(ScriptEngine* engine) const { return PointerEvent::toScriptValue(engine, *this); }
63 
64  EventType getType() const { return _type; }
65  uint32_t getID() const { return _id; }
66  const glm::vec2& getPos2D() const { return _pos2D; }
67  const glm::vec3& getPos3D() const { return _pos3D; }
68  const glm::vec3& getNormal() const { return _normal; }
69  const glm::vec3& getDirection() const { return _direction; }
70  const glm::vec2& getScroll() const { return _scroll; }
71  Button getButton() const { return _button; }
72  uint32_t getButtons() const { return _buttons; }
73  Qt::KeyboardModifiers getKeyboardModifiers() const { return _keyboardModifiers; }
74  bool shouldFocus() const { return _shouldFocus; }
75  bool sendMoveOnHoverLeave() const { return _moveOnHoverLeave; }
76 
77  void setID(uint32_t id) { _id = id; }
78  void setType(EventType type) { _type = type; }
79  void setButton(Button button);
80  void setPos2D(const glm::vec2& pos2D) { _pos2D = pos2D; }
81  void setShouldFocus(bool focus) { _shouldFocus = focus; }
82  void setMoveOnHoverLeave(bool moveOnHoverLeave) { _moveOnHoverLeave = moveOnHoverLeave; }
83  void setScroll(const glm::vec2& value) { _scroll = value; }
84 
85  static const unsigned int INVALID_POINTER_ID { 0 };
86 
87 private:
88  EventType _type;
89  uint32_t _id { INVALID_POINTER_ID }; // used to identify the pointer. (left vs right hand, for example)
90  glm::vec2 _pos2D { glm::vec2(NAN) }; // (in meters) projected onto the xy plane of entities dimension box, (0, 0) is upper right hand corner
91  glm::vec3 _pos3D { glm::vec3(NAN) }; // surface location in world coordinates (in meters)
92  glm::vec3 _normal { glm::vec3(NAN) }; // surface normal
93  glm::vec3 _direction { glm::vec3(NAN) }; // incoming direction of pointer ray.
94  glm::vec2 _scroll { glm::vec3{NAN} };
95 
96  Button _button { NoButtons }; // button associated with this event, (if type is Press, this will be the button that is pressed)
97  uint32_t _buttons { NoButtons }; // the current state of all the buttons.
98  Qt::KeyboardModifiers _keyboardModifiers { Qt::KeyboardModifier::NoModifier }; // set of keys held when event was generated
99 
100  bool _shouldFocus { true };
101  bool _moveOnHoverLeave { true };
102 };
103 
104 QDebug& operator<<(QDebug& dbg, const PointerEvent& p);
105 
106 Q_DECLARE_METATYPE(PointerEvent)
107 
108 #endif // hifi_PointerEvent_h
109 
Represents a 2D or 3D pointer to the scripting engine. Exposed as PointerEvent
Definition: PointerEvent.h:32
Provides an engine-independent interface for a scripting engine.
Definition: ScriptEngine.h:93
[ScriptInterface] Provides an engine-independent interface for QScriptValue
Definition: ScriptValue.h:40