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 
30 class PointerEvent {
31 public:
32  enum Button {
33  NoButtons = 0x0,
34  PrimaryButton = 0x1,
35  SecondaryButton = 0x2,
36  TertiaryButton = 0x4
37  };
38 
39  enum EventType {
40  Press, // A button has just been pressed
41  DoublePress, // A button has just been double pressed
42  Release, // A button has just been released
43  Move, // The pointer has just moved
44  NumEventTypes
45  };
46 
47  PointerEvent() {}
48  PointerEvent(EventType type, uint32_t id);
49  PointerEvent(EventType type, uint32_t id, const glm::vec2& pos2D, Button button, uint32_t buttons, Qt::KeyboardModifiers keyboardModifiers);
50  PointerEvent(const glm::vec2& pos2D, const glm::vec3& pos3D, const glm::vec3& normal, const glm::vec3& direction);
51  PointerEvent(EventType type, uint32_t id,
52  const glm::vec2& pos2D, const glm::vec3& pos3D,
53  const glm::vec3& normal, const glm::vec3& direction,
54  Button button = NoButtons, uint32_t buttons = NoButtons, Qt::KeyboardModifiers keyboardModifiers = Qt::NoModifier);
55 
56  static ScriptValue toScriptValue(ScriptEngine* engine, const PointerEvent& event);
57  static bool fromScriptValue(const ScriptValue& object, PointerEvent& event);
58 
59  ScriptValue toScriptValue(ScriptEngine* engine) const { return PointerEvent::toScriptValue(engine, *this); }
60 
61  EventType getType() const { return _type; }
62  uint32_t getID() const { return _id; }
63  const glm::vec2& getPos2D() const { return _pos2D; }
64  const glm::vec3& getPos3D() const { return _pos3D; }
65  const glm::vec3& getNormal() const { return _normal; }
66  const glm::vec3& getDirection() const { return _direction; }
67  Button getButton() const { return _button; }
68  uint32_t getButtons() const { return _buttons; }
69  Qt::KeyboardModifiers getKeyboardModifiers() const { return _keyboardModifiers; }
70  bool shouldFocus() const { return _shouldFocus; }
71  bool sendMoveOnHoverLeave() const { return _moveOnHoverLeave; }
72 
73  void setID(uint32_t id) { _id = id; }
74  void setType(EventType type) { _type = type; }
75  void setButton(Button button);
76  void setPos2D(const glm::vec2& pos2D) { _pos2D = pos2D; }
77  void setShouldFocus(bool focus) { _shouldFocus = focus; }
78  void setMoveOnHoverLeave(bool moveOnHoverLeave) { _moveOnHoverLeave = moveOnHoverLeave; }
79 
80  static const unsigned int INVALID_POINTER_ID { 0 };
81 
82 private:
83  EventType _type;
84  uint32_t _id { INVALID_POINTER_ID }; // used to identify the pointer. (left vs right hand, for example)
85  glm::vec2 _pos2D { glm::vec2(NAN) }; // (in meters) projected onto the xy plane of entities dimension box, (0, 0) is upper right hand corner
86  glm::vec3 _pos3D { glm::vec3(NAN) }; // surface location in world coordinates (in meters)
87  glm::vec3 _normal { glm::vec3(NAN) }; // surface normal
88  glm::vec3 _direction { glm::vec3(NAN) }; // incoming direction of pointer ray.
89 
90  Button _button { NoButtons }; // button associated with this event, (if type is Press, this will be the button that is pressed)
91  uint32_t _buttons { NoButtons }; // the current state of all the buttons.
92  Qt::KeyboardModifiers _keyboardModifiers { Qt::KeyboardModifier::NoModifier }; // set of keys held when event was generated
93 
94  bool _shouldFocus { true };
95  bool _moveOnHoverLeave { true };
96 };
97 
98 QDebug& operator<<(QDebug& dbg, const PointerEvent& p);
99 
100 Q_DECLARE_METATYPE(PointerEvent)
101 
102 #endif // hifi_PointerEvent_h
103 
Represents a 2D or 3D pointer to the scripting engine. Exposed as PointerEvent
Definition: PointerEvent.h:30
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