Allow to move camera by dragging mouse cursor over game field
authorunC0Rr
Wed, 01 May 2024 16:49:16 +0200
changeset 16041 caba603f461f
parent 16040 52b51d92e88d
child 16042 2e529b1a9d5b
Allow to move camera by dragging mouse cursor over game field
qmlfrontend/Page1.qml
qmlfrontend/Page1Form.ui.qml
qmlfrontend/main.qml
--- a/qmlfrontend/Page1.qml	Fri Apr 19 17:45:05 2024 +0200
+++ b/qmlfrontend/Page1.qml	Wed May 01 16:49:16 2024 +0200
@@ -1,68 +1,85 @@
-import QtQuick 2.7
+import QtQuick
 import Hedgewars.Engine 1.0
 
 Page1Form {
-    focus: true
+  focus: true
 
-    property HWEngine hwEngine
-    property NetSession netSession
+  property HWEngine hwEngine
+  property NetSession netSession
 
-    Component {
-        id: hwEngineComponent
+  Component {
+    id: hwEngineComponent
 
-        HWEngine {
-            engineLibrary: "../rust/lib-hedgewars-engine/target/debug/libhedgewars_engine.so"
-            dataPath: "../share/hedgewars/Data"
-            previewAcceptor: PreviewAcceptor
-            onPreviewImageChanged: previewImage.source = "image://preview/image"
-            onPreviewIsRendering: previewImage.source = "qrc:/res/iconTime.png"
-        }
+    HWEngine {
+      engineLibrary: "../rust/lib-hedgewars-engine/target/debug/libhedgewars_engine.so"
+      dataPath: "../share/hedgewars/Data"
+      previewAcceptor: PreviewAcceptor
+      onPreviewImageChanged: previewImage.source = "image://preview/image"
+      onPreviewIsRendering: previewImage.source = "qrc:/res/iconTime.png"
     }
+  }
 
-    Component {
-        id: netSessionComponent
+  Component {
+    id: netSessionComponent
 
-        NetSession {
-            nickname: "test0272"
-            url: "hwnet://gameserver.hedgewars.org:46632"
-        }
+    NetSession {
+      nickname: "test0272"
+      url: "hwnet://gameserver.hedgewars.org:46632"
     }
+  }
 
-    Component.onCompleted: {
-        hwEngine = hwEngineComponent.createObject()
-    }
+  Component.onCompleted: {
+    hwEngine = hwEngineComponent.createObject()
+  }
 
-    tickButton {
-        onClicked: {
-            tickButton.visible = false
-            gameView.tick(100)
-        }
+  tickButton {
+    onClicked: {
+      tickButton.visible = false
+      gameView.tick(100)
     }
-    gameButton {
-        visible: !gameView.engineInstance
-        onClicked: {
-            const engineInstance = hwEngine.runQuickGame()
-            gameView.engineInstance = engineInstance
-        }
+  }
+  gameButton {
+    visible: !gameView.engineInstance
+    onClicked: {
+      const engineInstance = hwEngine.runQuickGame()
+      gameView.engineInstance = engineInstance
+    }
+  }
+  button1 {
+    visible: !gameView.engineInstance
+    onClicked: {
+      hwEngine.getPreview()
     }
-    button1 {
-        visible: !gameView.engineInstance
-        onClicked: {
-            hwEngine.getPreview()
-        }
-    }
-    netButton.onClicked: {
-        netSession = netSessionComponent.createObject()
-        netSession.open()
-    }
+  }
+  preview {
+    visible: !gameView.engineInstance
+  }
+
+  netButton.onClicked: {
+    netSession = netSessionComponent.createObject()
+    netSession.open()
+  }
+
+  Keys.onPressed: {
+    if (event.key === Qt.Key_Enter)
+      gameView.engineInstance.longEvent(Engine.Attack, Engine.Set)
+  }
 
-    Keys.onPressed: {
-        if (event.key === Qt.Key_Enter)
-            gameView.engineInstance.longEvent(Engine.Attack, Engine.Set)
-    }
+  Keys.onReleased: {
+    if (event.key === Qt.Key_Enter)
+      gameView.engineInstance.longEvent(Engine.Attack, Engine.Unset)
+  }
 
-    Keys.onReleased: {
-        if (event.key === Qt.Key_Enter)
-            gameView.engineInstance.longEvent(Engine.Attack, Engine.Unset)
-    }
+  gameMouseArea {
+    onPressed: event => {
+                 gameMouseArea.lastPoint = Qt.point(event.x, event.y)
+               }
+    onPositionChanged: event => {
+                         gameView.engineInstance.moveCamera(
+                           Qt.point(event.x - gameMouseArea.lastPoint.x,
+                                    event.y - gameMouseArea.lastPoint.y))
+
+                         gameMouseArea.lastPoint = Qt.point(event.x, event.y)
+                       }
+  }
 }
--- a/qmlfrontend/Page1Form.ui.qml	Fri Apr 19 17:45:05 2024 +0200
+++ b/qmlfrontend/Page1Form.ui.qml	Wed May 01 16:49:16 2024 +0200
@@ -1,6 +1,6 @@
-import QtQuick 2.7
-import QtQuick.Controls 2.0
-import QtQuick.Layouts 1.3
+import QtQuick
+import QtQuick.Controls
+import QtQuick.Layouts
 
 import Hedgewars.Engine 1.0
 
@@ -8,12 +8,12 @@
   id: element
   property alias button1: button1
   property alias previewImage: previewImage
+  property alias preview: preview
   property alias gameButton: gameButton
-  width: 1024
-  height: 800
   property alias netButton: netButton
   property alias tickButton: tickButton
   property alias gameView: gameView
+  property alias gameMouseArea: gameMouseArea
 
   ColumnLayout {
     anchors.fill: parent
@@ -39,6 +39,7 @@
     }
 
     Rectangle {
+      id: preview
       border.color: "orange"
       border.width: 5
       radius: 5
@@ -83,7 +84,10 @@
       Layout.fillHeight: true
 
       MouseArea {
+        id: gameMouseArea
         anchors.fill: parent
+
+        property point lastPoint
       }
     }
   }
--- a/qmlfrontend/main.qml	Fri Apr 19 17:45:05 2024 +0200
+++ b/qmlfrontend/main.qml	Wed May 01 16:49:16 2024 +0200
@@ -1,6 +1,6 @@
-import QtQuick 2.7
-import QtQuick.Controls 2.0
-import QtQuick.Layouts 1.3
+import QtQuick
+import QtQuick.Controls
+import QtQuick.Layouts
 
 ApplicationWindow {
   visible: true