Overte C++ Documentation
RenderableWebEntityItem.h
1 //
2 // Created by Bradley Austin Davis on 2015/05/12
3 // Copyright 2013 High Fidelity, Inc.
4 // Copyright 2020 Vircadia contributors.
5 // Copyright 2023 Overte e.V.
6 //
7 // Distributed under the Apache License, Version 2.0.
8 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9 // SPDX-License-Identifier: Apache-2.0
10 //
11 
12 #ifndef hifi_RenderableWebEntityItem_h
13 #define hifi_RenderableWebEntityItem_h
14 
15 #include <QtCore/QSharedPointer>
16 #include <WebEntityItem.h>
17 #include "RenderableEntityItem.h"
18 
19 class OffscreenQmlSurface;
20 class PointerEvent;
21 
22 namespace render { namespace entities {
23 
24 class WebEntityRenderer : public TypedEntityRenderer<WebEntityItem> {
25  Q_OBJECT
26  using Parent = TypedEntityRenderer<WebEntityItem>;
27  friend class EntityRenderer;
28 
29 public:
30  WebEntityRenderer(const EntityItemPointer& entity);
31  ~WebEntityRenderer();
32 
33  Q_INVOKABLE void hoverEnterEntity(const PointerEvent& event);
34  Q_INVOKABLE void hoverLeaveEntity(const PointerEvent& event);
35  Q_INVOKABLE void handlePointerEvent(const PointerEvent& event);
36 
37  static const QString QML;
38  static const char* URL_PROPERTY;
39  static const char* SCRIPT_URL_PROPERTY;
40  static const char* GLOBAL_POSITION_PROPERTY;
41  static const char* USE_BACKGROUND_PROPERTY;
42  static const char* USER_AGENT_PROPERTY;
43 
44  static void setAcquireWebSurfaceOperator(std::function<void(const QString&, bool, QSharedPointer<OffscreenQmlSurface>&, bool&)> acquireWebSurfaceOperator) { _acquireWebSurfaceOperator = acquireWebSurfaceOperator; }
45  static void acquireWebSurface(const QString& url, bool htmlContent, QSharedPointer<OffscreenQmlSurface>& webSurface, bool& cachedWebSurface) {
46  if (_acquireWebSurfaceOperator) {
47  _acquireWebSurfaceOperator(url, htmlContent, webSurface, cachedWebSurface);
48  }
49  }
50 
51  static void setReleaseWebSurfaceOperator(std::function<void(QSharedPointer<OffscreenQmlSurface>&, bool&, std::vector<QMetaObject::Connection>&)> releaseWebSurfaceOperator) { _releaseWebSurfaceOperator = releaseWebSurfaceOperator; }
52  static void releaseWebSurface(QSharedPointer<OffscreenQmlSurface>& webSurface, bool& cachedWebSurface, std::vector<QMetaObject::Connection>& connections) {
53  if (_releaseWebSurfaceOperator) {
54  _releaseWebSurfaceOperator(webSurface, cachedWebSurface, connections);
55  }
56  }
57 
58  virtual void setProxyWindow(QWindow* proxyWindow) override;
59  virtual QObject* getEventHandler() override;
60 
61  gpu::TexturePointer getTexture() override { return _texture; }
62 
63 protected:
64  virtual bool needsRenderUpdateFromTypedEntity(const TypedEntityPointer& entity) const override;
65  virtual void doRenderUpdateSynchronousTyped(const ScenePointer& scene, Transaction& transaction, const TypedEntityPointer& entity) override;
66  virtual void doRender(RenderArgs* args) override;
67  virtual bool isTransparent() const override;
68 
69  virtual bool wantsHandControllerPointerEvents() const override { return true; }
70  virtual bool wantsKeyboardFocus() const override { return true; }
71 
72  void handlePointerEventAsTouch(const PointerEvent& event);
73  void handlePointerEventAsMouse(const PointerEvent& event);
74 
75 private:
76  void onTimeout();
77  void buildWebSurface(const EntityItemPointer& entity, const QString& newSourceURL);
78  void destroyWebSurface();
79  glm::vec2 getWindowSize(const TypedEntityPointer& entity) const;
80 
81  int _geometryId{ 0 };
82  enum class ContentType {
83  NoContent,
84  HtmlContent,
85  QmlContent
86  };
87  static ContentType getContentType(const QString& urlString);
88  ContentType _contentType { ContentType::NoContent };
89 
90  QSharedPointer<OffscreenQmlSurface> _webSurface { nullptr };
91  bool _cachedWebSurface { false };
92  gpu::TexturePointer _texture;
93  QString _tryingToBuildURL;
94 
95  glm::u8vec3 _color;
96  float _alpha { 1.0f };
97  PulsePropertyGroup _pulseProperties;
98 
99  QString _sourceURL;
100  uint16_t _dpi;
101  QString _scriptURL;
102  uint8_t _maxFPS;
103  bool _useBackground { false };
104  QString _userAgent;
105  WebInputMode _inputMode { WebInputMode::TOUCH };
106 
107  glm::vec3 _contextPosition;
108 
109  QTimer _timer;
110  uint64_t _lastRenderTime { 0 };
111 
112  std::vector<QMetaObject::Connection> _connections;
113 
114  static std::function<void(QString, bool, QSharedPointer<OffscreenQmlSurface>&, bool&)> _acquireWebSurfaceOperator;
115  static std::function<void(QSharedPointer<OffscreenQmlSurface>&, bool&, std::vector<QMetaObject::Connection>&)> _releaseWebSurfaceOperator;
116 
117 public slots:
118  void emitScriptEvent(const QVariant& scriptMessage) override;
119 
120 signals:
121  void scriptEventReceived(const QVariant& message);
122  void webEventReceived(const QVariant& message);
123 };
124 
125 } }
126 
127 #endif // hifi_RenderableWebEntityItem_h
Represents a 2D or 3D pointer to the scripting engine. Exposed as PointerEvent
Definition: PointerEvent.h:30