/*************************************************************************************************** Copyright (C) 2023 The Qt Company Ltd. SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only ***************************************************************************************************/ #include "mainwindow.h" class HwndHost : public QDotNetObject { public: Q_DOTNET_OBJECT_INLINE(HwndHost, "System.Windows.Interop.HwndHost, PresentationFramework"); void *handle() { return method("get_Handle", fnGetHandle).invoke(*this); } double width() { return method("get_Width", fnGetWidth).invoke(*this); } double height() { return method("get_Height", fnGetHeight).invoke(*this); } private: QDotNetFunction fnGetHandle = nullptr; QDotNetFunction fnGetWidth = nullptr; QDotNetFunction fnGetHeight = nullptr; }; class MouseEventArgs : public QDotNetObject { public: Q_DOTNET_OBJECT_INLINE(MouseEventArgs, "System.Windows.Input.MouseEventArgs, PresentationCore"); }; class MainWindowPrivate : public QDotNetObject::IEventHandler { public: MainWindowPrivate(MainWindow *q) : q(q) {} void handleEvent(const QString &evName, QDotNetObject &evSource, QDotNetObject &evArgs) override { if (evName == "ContentRendered") { emit q->contentRendered(); } else if (evName == "SizeChanged") { double width = evArgs.object("NewSize").call("get_Width"); double height = evArgs.object("NewSize").call("get_Height"); if (width != hostWidth) { hostWidth = width; emit q->hostWidthChanged(); } if (height != hostHeight) { hostHeight = height; emit q->hostHeightChanged(); } } else if (evName == "Closed") { emit q->closed(); } else if (evName == "PropertyChanged") { QString propertyName = evArgs.call("get_PropertyName"); if (propertyName == "CameraPositionX") emit q->cameraPositionXChanged(); else if (propertyName == "CameraPositionY") emit q->cameraPositionYChanged(); else if (propertyName == "CameraPositionZ") emit q->cameraPositionZChanged(); else if (propertyName == "CameraRotationX") emit q->cameraRotationXChanged(); else if (propertyName == "CameraRotationY") emit q->cameraRotationYChanged(); else if (propertyName == "CameraRotationZ") emit q->cameraRotationZChanged(); else if (propertyName == "AnimationDuration") emit q->animationDurationChanged(); } }; HwndHost hwndHost = nullptr; double hostWidth = 0.0, hostHeight = 0.0; QDotNetFunction fnSetEmbeddedFps = nullptr; QDotNetFunction fnGetCameraPositionX = nullptr; QDotNetFunction fnGetCameraPositionY = nullptr; QDotNetFunction fnGetCameraPositionZ = nullptr; QDotNetFunction fnGetCameraRotationX = nullptr; QDotNetFunction fnGetCameraRotationY = nullptr; QDotNetFunction fnGetCameraRotationZ = nullptr; QDotNetFunction fnGetAnimationDuration = nullptr; QDotNetFunction fnGetFramesPerSecond = nullptr; QDotNetFunction fnGetBackgroundColor = nullptr; private: MainWindow *q; }; Q_DOTNET_OBJECT_IMPL(MainWindow, Q_DOTNET_OBJECT_INIT(d(new MainWindowPrivate(this)))); MainWindow::MainWindow() : QDotNetObject(nullptr), d(new MainWindowPrivate(this)) {} MainWindow::~MainWindow() {} void MainWindow::init() { *this = constructor().invoke(nullptr); d->hwndHost = method("get_HwndHost").invoke(*this); subscribeEvent("ContentRendered", d); subscribeEvent("Closed", d); subscribeEvent("PropertyChanged", d); d->hwndHost.subscribeEvent("SizeChanged", d); QtDotNet::call("WpfApp.Program, WpfApp", "set_MainWindow", *this); QtDotNet::call("WpfApp.Program, WpfApp", "Main"); } void *MainWindow::hostHandle() { return d->hwndHost.handle(); } int MainWindow::hostWidth() { return d->hostWidth; } int MainWindow::hostHeight() { return d->hostHeight; } double MainWindow::cameraPositionX() { return method("get_CameraPositionX", d->fnGetCameraPositionX).invoke(*this); } double MainWindow::cameraPositionY() { return method("get_CameraPositionY", d->fnGetCameraPositionY).invoke(*this); } double MainWindow::cameraPositionZ() { return method("get_CameraPositionZ", d->fnGetCameraPositionZ).invoke(*this); } double MainWindow::cameraRotationX() { return method("get_CameraRotationX", d->fnGetCameraRotationX).invoke(*this); } double MainWindow::cameraRotationY() { return method("get_CameraRotationY", d->fnGetCameraRotationY).invoke(*this); } double MainWindow::cameraRotationZ() { return method("get_CameraRotationZ", d->fnGetCameraRotationZ).invoke(*this); } double MainWindow::animationDuration() { return method("get_AnimationDuration", d->fnGetAnimationDuration).invoke(*this); } double MainWindow::framesPerSecond() { return method("get_FramesPerSecond", d->fnGetFramesPerSecond).invoke(*this); } QString MainWindow::backgroundColor() { return method("get_BackgroundColor", d->fnGetBackgroundColor).invoke(*this); } void MainWindow::setFramesPerSecond(double fps) { method("set_FramesPerSecond", d->fnSetEmbeddedFps).invoke(*this, fps); }