// // Created by Bradley Austin Davis on 2016/01/27 // Copyright 2013-2016 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // #include "QmlOverlay.h" #include #include #include #include #include #include #include #include #include #include "Application.h" #include "text/FontFamilies.h" QmlOverlay::QmlOverlay(const QUrl& url) { buildQmlElement(url); } QmlOverlay::QmlOverlay(const QUrl& url, const QmlOverlay* textOverlay) : Overlay2D(textOverlay) { buildQmlElement(url); } void QmlOverlay::buildQmlElement(const QUrl& url) { auto offscreenUi = DependencyManager::get(); offscreenUi->returnFromUiThread([=] { offscreenUi->load(url, [=](QQmlContext* context, QObject* object) { QQuickItem* rawPtr = dynamic_cast(object); // Create a shared ptr with a custom deleter lambda, that calls deleteLater _qmlElement = std::shared_ptr(rawPtr, [](QQuickItem* ptr) { if (ptr) { ptr->deleteLater(); } }); }); while (!_qmlElement) { qApp->processEvents(); } return QVariant(); }); } QmlOverlay::~QmlOverlay() { _qmlElement.reset(); } void QmlOverlay::setProperties(const QVariantMap& properties) { Overlay2D::setProperties(properties); auto bounds = _bounds; std::weak_ptr weakQmlElement = _qmlElement; DependencyManager::get()->executeOnUiThread([weakQmlElement, bounds, properties] { // check to see if qmlElement still exists auto qmlElement = weakQmlElement.lock(); if (qmlElement) { qmlElement->setX(bounds.left()); qmlElement->setY(bounds.top()); qmlElement->setWidth(bounds.width()); qmlElement->setHeight(bounds.height()); QMetaObject::invokeMethod(qmlElement.get(), "updatePropertiesFromScript", Qt::DirectConnection, Q_ARG(QVariant, properties)); } }); } void QmlOverlay::render(RenderArgs* args) { if (!_qmlElement) { return; } if (_visible != _qmlElement->isVisible()) { _qmlElement->setVisible(_visible); } }