Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
authorunC0Rr
Fri, 07 Dec 2018 13:35:08 +0100
changeset 14371 90bd2c331703
parent 14370 ef2fc0210362
child 14372 b6824a53d4b1
Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
qmlfrontend/CMakeLists.txt
qmlfrontend/Page1.qml
qmlfrontend/hwengine.cpp
qmlfrontend/hwengine.h
qmlfrontend/main.cpp
qmlfrontend/preview_acceptor.cpp
qmlfrontend/preview_acceptor.h
--- a/qmlfrontend/CMakeLists.txt	Fri Dec 07 13:04:00 2018 +0100
+++ b/qmlfrontend/CMakeLists.txt	Fri Dec 07 13:35:08 2018 +0100
@@ -15,6 +15,8 @@
     "team.cpp" "team.h"
     "engine_instance.cpp" "engine_instance.h"
     "preview_image_provider.cpp" "preview_image_provider.h"
-    "engine_interface.h")
+    "engine_interface.h"
+    "preview_acceptor.cpp" "preview_acceptor.h"
+    )
 
 target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Quick)
--- a/qmlfrontend/Page1.qml	Fri Dec 07 13:04:00 2018 +0100
+++ b/qmlfrontend/Page1.qml	Fri Dec 07 13:35:08 2018 +0100
@@ -2,24 +2,30 @@
 import Hedgewars.Engine 1.0
 
 Page1Form {
+  property var hwEngine
+
+  Component {
+    id: hwEngineComponent
+
+    HWEngine {
+      previewAcceptor: PreviewAcceptor
+      onPreviewImageChanged: previewImage.source = "image://preview/image"
+      onPreviewIsRendering: previewImage.source = "qrc:/res/iconTime.png"
+    }
+  }
+
+  Component.onCompleted: {
+    hwEngine = hwEngineComponent.createObject()
+  }
+
   tickButton.onClicked: {
     gameView.tick(100)
   }
   gameButton.onClicked: {
-    var engineInstance = HWEngine.runQuickGame()
+    var engineInstance = hwEngine.runQuickGame()
     gameView.engineInstance = engineInstance
   }
   button1.onClicked: {
-    HWEngine.getPreview()
-  }
-
-  Connections {
-    target: HWEngine
-    onPreviewImageChanged: {
-      previewImage.source = "image://preview/image"
-    }
-    onPreviewIsRendering: {
-      previewImage.source = "qrc:/res/iconTime.png"
-    }
+    hwEngine.getPreview()
   }
 }
--- a/qmlfrontend/hwengine.cpp	Fri Dec 07 13:04:00 2018 +0100
+++ b/qmlfrontend/hwengine.cpp	Fri Dec 07 13:35:08 2018 +0100
@@ -1,4 +1,7 @@
+#include "hwengine.h"
+
 #include <QDebug>
+#include <QImage>
 #include <QLibrary>
 #include <QQmlEngine>
 #include <QUuid>
@@ -6,37 +9,12 @@
 #include "engine_instance.h"
 #include "engine_interface.h"
 #include "game_view.h"
-#include "preview_image_provider.h"
-
-#include "hwengine.h"
+#include "preview_acceptor.h"
 
-HWEngine::HWEngine(QQmlEngine* engine, QObject* parent)
-    : QObject(parent),
-      m_engine(engine),
-      m_previewProvider(new PreviewImageProvider()) {
-  m_engine->addImageProvider(QLatin1String("preview"), m_previewProvider);
-}
+HWEngine::HWEngine(QObject* parent) : QObject(parent) {}
 
 HWEngine::~HWEngine() {}
 
-static QObject* hwengine_singletontype_provider(QQmlEngine* engine,
-                                                QJSEngine* scriptEngine) {
-  Q_UNUSED(scriptEngine)
-
-  HWEngine* hwengine = new HWEngine(engine);
-  return hwengine;
-}
-
-void HWEngine::exposeToQML() {
-  qDebug("HWEngine::exposeToQML");
-  qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine",
-                                     hwengine_singletontype_provider);
-  qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView");
-  qmlRegisterUncreatableType<EngineInstance>("Hedgewars.Engine", 1, 0,
-                                             "EngineInstance",
-                                             "Create by HWEngine run methods");
-}
-
 void HWEngine::getPreview() {
   emit previewIsRendering();
 
@@ -58,7 +36,7 @@
   previewImage.setColorTable(colorTable);
   previewImage.detach();
 
-  m_previewProvider->setImage(previewImage);
+  if (m_previewAcceptor) m_previewAcceptor->setImage(previewImage);
 
   emit previewImageChanged();
   // m_runQueue->queue(m_gameConfig);
@@ -80,3 +58,12 @@
 }
 
 int HWEngine::previewHedgehogsCount() const { return m_previewHedgehogsCount; }
+
+PreviewAcceptor* HWEngine::previewAcceptor() const { return m_previewAcceptor; }
+
+void HWEngine::setPreviewAcceptor(PreviewAcceptor* previewAcceptor) {
+  if (m_previewAcceptor == previewAcceptor) return;
+
+  m_previewAcceptor = previewAcceptor;
+  emit previewAcceptorChanged(m_previewAcceptor);
+}
--- a/qmlfrontend/hwengine.h	Fri Dec 07 13:04:00 2018 +0100
+++ b/qmlfrontend/hwengine.h	Fri Dec 07 13:35:08 2018 +0100
@@ -8,25 +8,29 @@
 #include "game_config.h"
 
 class QQmlEngine;
-class PreviewImageProvider;
 class EngineInstance;
+class PreviewAcceptor;
 
 class HWEngine : public QObject {
   Q_OBJECT
 
   Q_PROPERTY(int previewHedgehogsCount READ previewHedgehogsCount NOTIFY
                  previewHedgehogsCountChanged)
+  Q_PROPERTY(PreviewAcceptor* previewAcceptor READ previewAcceptor WRITE
+                 setPreviewAcceptor NOTIFY previewAcceptorChanged)
 
  public:
-  explicit HWEngine(QQmlEngine* engine, QObject* parent = nullptr);
+  explicit HWEngine(QObject* parent = nullptr);
   ~HWEngine();
 
-  static void exposeToQML();
-
   Q_INVOKABLE void getPreview();
   Q_INVOKABLE EngineInstance* runQuickGame();
 
   int previewHedgehogsCount() const;
+  PreviewAcceptor* previewAcceptor() const;
+
+ public slots:
+  void setPreviewAcceptor(PreviewAcceptor* previewAcceptor);
 
  signals:
   void previewIsRendering();
@@ -34,12 +38,13 @@
   void previewHogCountChanged(int count);
   void gameFinished();
   void previewHedgehogsCountChanged(int previewHedgehogsCount);
+  void previewAcceptorChanged(PreviewAcceptor* previewAcceptor);
 
  private:
   QQmlEngine* m_engine;
-  PreviewImageProvider* m_previewProvider;
   GameConfig m_gameConfig;
   int m_previewHedgehogsCount;
+  PreviewAcceptor* m_previewAcceptor;
 };
 
 #endif  // HWENGINE_H
--- a/qmlfrontend/main.cpp	Fri Dec 07 13:04:00 2018 +0100
+++ b/qmlfrontend/main.cpp	Fri Dec 07 13:35:08 2018 +0100
@@ -4,7 +4,9 @@
 #include <QQmlApplicationEngine>
 
 #include "engine_interface.h"
+#include "game_view.h"
 #include "hwengine.h"
+#include "preview_acceptor.h"
 
 namespace Engine {
 hedgewars_engine_protocol_version_t* hedgewars_engine_protocol_version;
@@ -18,6 +20,14 @@
 advance_simulation_t* advance_simulation;
 };  // namespace Engine
 
+static QObject* previewacceptor_singletontype_provider(
+    QQmlEngine* engine, QJSEngine* scriptEngine) {
+  Q_UNUSED(scriptEngine)
+
+  PreviewAcceptor* acceptor = new PreviewAcceptor(engine);
+  return acceptor;
+}
+
 void loadEngineLibrary() {
 #ifdef Q_OS_WIN
   QLibrary hwlib("./libhedgewars_engine.dll");
@@ -64,7 +74,14 @@
 
   QQmlApplicationEngine engine;
 
-  HWEngine::exposeToQML();
+  qmlRegisterSingletonType<PreviewAcceptor>(
+      "Hedgewars.Engine", 1, 0, "PreviewAcceptor",
+      previewacceptor_singletontype_provider);
+  qmlRegisterType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine");
+  qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView");
+  qmlRegisterUncreatableType<EngineInstance>("Hedgewars.Engine", 1, 0,
+                                             "EngineInstance",
+                                             "Create by HWEngine run methods");
 
   engine.load(QUrl(QLatin1String("qrc:/main.qml")));
   if (engine.rootObjects().isEmpty()) return -1;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/qmlfrontend/preview_acceptor.cpp	Fri Dec 07 13:35:08 2018 +0100
@@ -0,0 +1,15 @@
+#include "preview_acceptor.h"
+
+#include <QImage>
+#include <QQmlEngine>
+
+#include "preview_image_provider.h"
+
+PreviewAcceptor::PreviewAcceptor(QQmlEngine *engine, QObject *parent)
+    : QObject(parent), m_previewProvider(new PreviewImageProvider()) {
+  engine->addImageProvider(QLatin1String("preview"), m_previewProvider);
+}
+
+void PreviewAcceptor::setImage(const QImage &preview) {
+  m_previewProvider->setImage(preview);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/qmlfrontend/preview_acceptor.h	Fri Dec 07 13:35:08 2018 +0100
@@ -0,0 +1,21 @@
+#ifndef PREVIEW_ACCEPTOR_H
+#define PREVIEW_ACCEPTOR_H
+
+#include <QObject>
+
+class QQmlEngine;
+class PreviewImageProvider;
+
+class PreviewAcceptor : public QObject {
+  Q_OBJECT
+ public:
+  explicit PreviewAcceptor(QQmlEngine *engine, QObject *parent = nullptr);
+
+ public slots:
+  void setImage(const QImage &preview);
+
+ private:
+  PreviewImageProvider *m_previewProvider;
+};
+
+#endif  // PREVIEW_ACCEPTOR_H