From dd39d468838fb46bad5192f6fae58c9d842ab6ee Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Fri, 9 Sep 2016 13:41:09 -0700 Subject: [PATCH] fold WebEntityAPIHelper members into OffscreenQmlSurface --- .../src/RenderableWebEntityItem.cpp | 8 ++-- libraries/gl/src/gl/OffscreenQmlSurface.cpp | 40 +++++++----------- libraries/gl/src/gl/OffscreenQmlSurface.h | 41 ++++++------------- 3 files changed, 29 insertions(+), 60 deletions(-) diff --git a/libraries/entities-renderer/src/RenderableWebEntityItem.cpp b/libraries/entities-renderer/src/RenderableWebEntityItem.cpp index 8ff9187adf..e6ebbf7668 100644 --- a/libraries/entities-renderer/src/RenderableWebEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableWebEntityItem.cpp @@ -100,7 +100,7 @@ bool RenderableWebEntityItem::buildWebSurface(EntityTreeRenderer* renderer) { // forward web events to EntityScriptingInterface auto entities = DependencyManager::get(); const EntityItemID entityItemID = getID(); - QObject::connect(_webSurface->_webEntityAPIHelper, &WebEntityAPIHelper::webEventReceived, [=](const QVariant& message) { + QObject::connect(_webSurface, &OffscreenQmlSurface::webEventReceived, [=](const QVariant& message) { emit entities->webEventReceived(entityItemID, message); }); @@ -308,7 +308,7 @@ void RenderableWebEntityItem::destroyWebSurface() { // The lifetime of the QML surface MUST be managed by the main thread // Additionally, we MUST use local variables copied by value, rather than - // member variables, since they would implicitly refer to a this that + // member variables, since they would implicitly refer to a this that // is no longer valid auto webSurface = _webSurface; AbstractViewStateInterface::instance()->postLambdaEvent([webSurface] { @@ -318,7 +318,6 @@ void RenderableWebEntityItem::destroyWebSurface() { } } - void RenderableWebEntityItem::update(const quint64& now) { auto interval = now - _lastRenderTime; if (interval > MAX_NO_RENDER_INTERVAL) { @@ -326,12 +325,11 @@ void RenderableWebEntityItem::update(const quint64& now) { } } - bool RenderableWebEntityItem::isTransparent() { float fadeRatio = _isFading ? Interpolate::calculateFadeRatio(_fadeStartTime) : 1.0f; return fadeRatio < OPAQUE_ALPHA_THRESHOLD; } void RenderableWebEntityItem::emitScriptEvent(const QVariant& message) { - _webSurface->_webEntityAPIHelper->emitScriptEvent(message); + _webSurface->emitScriptEvent(message); } diff --git a/libraries/gl/src/gl/OffscreenQmlSurface.cpp b/libraries/gl/src/gl/OffscreenQmlSurface.cpp index 6fcdee2812..fe25940198 100644 --- a/libraries/gl/src/gl/OffscreenQmlSurface.cpp +++ b/libraries/gl/src/gl/OffscreenQmlSurface.cpp @@ -419,9 +419,7 @@ bool OffscreenQmlRenderThread::allowNewFrame(uint8_t fps) { } OffscreenQmlSurface::OffscreenQmlSurface() { - _webEntityAPIHelper = new WebEntityAPIHelper; - _webEntityAPIHelper->setOffscreenQmlSurface(this); - _webEntityAPIHelper->moveToThread(qApp->thread()); + // moveToThread(qApp->thread()); } static const uint64_t MAX_SHUTDOWN_WAIT_SECS = 2; @@ -435,9 +433,6 @@ OffscreenQmlSurface::~OffscreenQmlSurface() { qWarning() << "Failed to shut down the QML Renderer Thread"; } - _webEntityAPIHelper->setOffscreenQmlSurface(nullptr); - _webEntityAPIHelper->deleteLater(); - delete _rootItem; delete _renderer; delete _qmlComponent; @@ -540,13 +535,13 @@ QObject* OffscreenQmlSurface::load(const QUrl& qmlSource, std::functionloadUrl(qmlSource, QQmlComponent::PreferSynchronous); if (_qmlComponent->isLoading()) { - connect(_qmlComponent, &QQmlComponent::statusChanged, this, + connect(_qmlComponent, &QQmlComponent::statusChanged, this, [this, f](QQmlComponent::Status){ finishQmlLoad(f); }); return nullptr; } - + return finishQmlLoad(f); } @@ -588,7 +583,7 @@ QObject* OffscreenQmlSurface::finishQmlLoad(std::function(newObject); if (newItem) { - // Make sure we make items focusable (critical for + // Make sure we make items focusable (critical for // supporting keyboard shortcuts) newItem->setFlag(QQuickItem::ItemIsFocusScope, true); } @@ -616,11 +611,11 @@ QObject* OffscreenQmlSurface::finishQmlLoad(std::function_rendering || !_renderer->allowNewFrame(_maxFps)) { return; } @@ -691,7 +686,6 @@ bool OffscreenQmlSurface::eventFilter(QObject* originalDestination, QEvent* even } #endif - switch (event->type()) { case QEvent::Resize: { QResizeEvent* resizeEvent = static_cast(event); @@ -761,8 +755,8 @@ void OffscreenQmlSurface::resume() { _paused = false; requestRender(); - getRootItem()->setProperty("eventBridge", QVariant::fromValue(_webEntityAPIHelper)); - getRootContext()->setContextProperty("webEntity", _webEntityAPIHelper); + getRootItem()->setProperty("eventBridge", QVariant::fromValue(this)); + getRootContext()->setContextProperty("webEntity", this); } bool OffscreenQmlSurface::isPaused() const { @@ -897,13 +891,7 @@ void OffscreenQmlSurface::setKeyboardRaised(bool raised) { } -void WebEntityAPIHelper::synthesizeKeyPress(QString key) { - if (_offscreenQmlSurface) { - _offscreenQmlSurface->synthesizeKeyPress(key); - } -} - -void WebEntityAPIHelper::emitScriptEvent(const QVariant& message) { +void OffscreenQmlSurface::emitScriptEvent(const QVariant& message) { if (QThread::currentThread() != thread()) { QMetaObject::invokeMethod(this, "emitScriptEvent", Qt::QueuedConnection, Q_ARG(QVariant, message)); } else { @@ -911,15 +899,15 @@ void WebEntityAPIHelper::emitScriptEvent(const QVariant& message) { } } -void WebEntityAPIHelper::emitWebEvent(const QVariant& message) { +void OffscreenQmlSurface::emitWebEvent(const QVariant& message) { if (QThread::currentThread() != thread()) { QMetaObject::invokeMethod(this, "emitWebEvent", Qt::QueuedConnection, Q_ARG(QVariant, message)); } else { // special case to handle raising and lowering the virtual keyboard - if (message.type() == QVariant::String && message.toString() == "_RAISE_KEYBOARD" && _offscreenQmlSurface) { - _offscreenQmlSurface->setKeyboardRaised(true); - } else if (message.type() == QVariant::String && message.toString() == "_LOWER_KEYBOARD" && _offscreenQmlSurface) { - _offscreenQmlSurface->setKeyboardRaised(false); + if (message.type() == QVariant::String && message.toString() == "_RAISE_KEYBOARD") { + setKeyboardRaised(true); + } else if (message.type() == QVariant::String && message.toString() == "_LOWER_KEYBOARD") { + setKeyboardRaised(false); } else { emit webEventReceived(message); } diff --git a/libraries/gl/src/gl/OffscreenQmlSurface.h b/libraries/gl/src/gl/OffscreenQmlSurface.h index 5c7444c947..ecdf3fbef5 100644 --- a/libraries/gl/src/gl/OffscreenQmlSurface.h +++ b/libraries/gl/src/gl/OffscreenQmlSurface.h @@ -25,31 +25,7 @@ class QQmlContext; class QQmlComponent; class QQuickWindow; class QQuickItem; - class OffscreenQmlRenderThread; -class OffscreenQmlSurface; - - -class WebEntityAPIHelper : public QObject { - Q_OBJECT -public: - void setOffscreenQmlSurface(OffscreenQmlSurface* renderableWebEntityItem) { - _offscreenQmlSurface = renderableWebEntityItem; - } - Q_INVOKABLE void synthesizeKeyPress(QString key); - - // event bridge -public slots: - void emitScriptEvent(const QVariant& scriptMessage); - void emitWebEvent(const QVariant& webMessage); -signals: - void scriptEventReceived(const QVariant& message); - void webEventReceived(const QVariant& message); - -protected: - OffscreenQmlSurface* _offscreenQmlSurface{ nullptr }; -}; - class OffscreenQmlSurface : public QObject { Q_OBJECT @@ -92,13 +68,10 @@ public: QPointF mapToVirtualScreen(const QPointF& originalPoint, QObject* originalWidget); bool eventFilter(QObject* originalDestination, QEvent* event) override; - void setKeyboardRaised(bool raised); - void synthesizeKeyPress(QString key); + Q_INVOKABLE void setKeyboardRaised(bool raised); + Q_INVOKABLE void synthesizeKeyPress(QString key); - // XXX make private - WebEntityAPIHelper* _webEntityAPIHelper; - signals: void textureUpdated(unsigned int texture); void focusObjectChanged(QObject* newFocus); @@ -109,6 +82,16 @@ public slots: void requestRender(); void onAboutToQuit(); + + // event bridge +public slots: + void emitScriptEvent(const QVariant& scriptMessage); + void emitWebEvent(const QVariant& webMessage); +signals: + void scriptEventReceived(const QVariant& message); + void webEventReceived(const QVariant& message); + + protected: bool filterEnabled(QObject* originalDestination, QEvent* event) const; void setFocusText(bool newFocusText);