Implement key bindings
authorunC0Rr
Mon, 20 May 2024 14:58:30 +0200
changeset 16045 4933920eba89
parent 16044 0e90bdfc2642
child 16046 e8afb1bf2779
Implement key bindings
qmlfrontend/Page1.qml
qmlfrontend/engine_instance.cpp
qmlfrontend/engine_instance.h
qmlfrontend/engine_interface.h
rust/lib-hedgewars-engine/src/lib.rs
--- a/qmlfrontend/Page1.qml	Sun May 19 15:55:26 2024 +0200
+++ b/qmlfrontend/Page1.qml	Mon May 20 14:58:30 2024 +0200
@@ -2,18 +2,70 @@
 import Hedgewars.Engine 1.0
 
 Page1Form {
+  property HWEngine hwEngine
+  property var keyBindings: ({
+      "long": {
+        [Qt.Key_Space]: Engine.Attack,
+        [Qt.Key_Up]: Engine.ArrowUp,
+        [Qt.Key_Right]: Engine.ArrowRight,
+        [Qt.Key_Down]: Engine.ArrowDown,
+        [Qt.Key_Left]: Engine.ArrowLeft,
+        [Qt.Key_Shift]: Engine.Precision
+      },
+      "simple": {
+        [Qt.Key_Tab]: Engine.SwitchHedgehog,
+        [Qt.Key_Enter]: Engine.LongJump,
+        [Qt.Key_Backspace]: Engine.HighJump,
+        [Qt.Key_Y]: Engine.Accept,
+        [Qt.Key_N]: Engine.Deny
+      }
+    })
+  property NetSession netSession
+
   focus: true
 
-  property HWEngine hwEngine
-  property NetSession netSession
+  Component.onCompleted: {
+    hwEngine = hwEngineComponent.createObject();
+  }
+  Keys.onPressed: event => {
+    if (event.isAutoRepeat) {
+      return;
+    }
+    let action = keyBindings["simple"][event.key];
+    if (action !== undefined) {
+      gameView.engineInstance.simpleEvent(action);
+      event.accepted = true;
+      return;
+    }
+    action = keyBindings["long"][event.key];
+    if (action !== undefined) {
+      gameView.engineInstance.longEvent(action, Engine.Set);
+      event.accepted = true;
+    }
+  }
+  Keys.onReleased: event => {
+    if (event.isAutoRepeat) {
+      return;
+    }
+    const action = keyBindings["long"][event.key];
+    if (action !== undefined) {
+      gameView.engineInstance.longEvent(action, Engine.Unset);
+      event.accepted = true;
+    }
+  }
+  netButton.onClicked: {
+    netSession = netSessionComponent.createObject();
+    netSession.open();
+  }
 
   Component {
     id: hwEngineComponent
 
     HWEngine {
+      dataPath: "../share/hedgewars/Data"
       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"
     }
@@ -28,58 +80,41 @@
     }
   }
 
-  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
+      const engineInstance = hwEngine.runQuickGame();
+      gameView.engineInstance = engineInstance;
     }
   }
+
   button1 {
     visible: !gameView.engineInstance
+
     onClicked: {
-      hwEngine.getPreview()
+      hwEngine.getPreview();
     }
   }
+
   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.onReleased: {
-    if (event.key === Qt.Key_Enter)
-      gameView.engineInstance.longEvent(Engine.Attack, Engine.Unset)
-  }
-
   gameMouseArea {
+    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);
+    }
     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)
-                       }
+      gameMouseArea.lastPoint = Qt.point(event.x, event.y);
+    }
   }
 }
--- a/qmlfrontend/engine_instance.cpp	Sun May 19 15:55:26 2024 +0200
+++ b/qmlfrontend/engine_instance.cpp	Mon May 20 14:58:30 2024 +0200
@@ -88,17 +88,21 @@
 }
 
 void EngineInstance::simpleEvent(Engine::SimpleEventType event_type) {
-  simple_event(m_instance.get(), event_type);
+  simple_event(m_instance.get(),
+               static_cast<hwengine::SimpleEventType>(event_type));
 }
 
 void EngineInstance::longEvent(Engine::LongEventType event_type,
                                Engine::LongEventState state) {
-  long_event(m_instance.get(), event_type, state);
+  long_event(m_instance.get(), static_cast<hwengine::LongEventType>(event_type),
+             static_cast<hwengine::LongEventState>(state));
 }
 
 void EngineInstance::positionedEvent(Engine::PositionedEventType event_type,
                                      qint32 x, qint32 y) {
-  positioned_event(m_instance.get(), event_type, x, y);
+  positioned_event(m_instance.get(),
+                   static_cast<hwengine::PositionedEventType>(event_type), x,
+                   y);
 }
 
 void EngineInstance::renderFrame() { render_frame(m_instance.get()); }
--- a/qmlfrontend/engine_instance.h	Sun May 19 15:55:26 2024 +0200
+++ b/qmlfrontend/engine_instance.h	Mon May 20 14:58:30 2024 +0200
@@ -15,7 +15,7 @@
  public:
   explicit EngineInstance(const QString& libraryPath,const QString& dataPath,
                           QObject* parent = nullptr);
-  ~EngineInstance();
+  ~EngineInstance() override;
 
   Q_PROPERTY(bool isValid READ isValid NOTIFY isValidChanged)
 
--- a/qmlfrontend/engine_interface.h	Sun May 19 15:55:26 2024 +0200
+++ b/qmlfrontend/engine_interface.h	Mon May 20 14:58:30 2024 +0200
@@ -42,25 +42,51 @@
 using long_event_t = decltype(hwengine::long_event);
 using positioned_event_t = decltype(hwengine::positioned_event);
 
+}  // extern "C"
+
+Q_NAMESPACE
+
+/*
 using SimpleEventType = hwengine::SimpleEventType;
 using LongEventType = hwengine::LongEventType;
 using LongEventState = hwengine::LongEventState;
 using PositionedEventType = hwengine::PositionedEventType;
+*/
 
-}  // extern "C"
+// NOTE: have to copy these to be able to register then in Qt meta object system
+enum class LongEventState {
+  Set,
+  Unset,
+};
 
-Q_NAMESPACE
+enum class LongEventType {
+  ArrowUp,
+  ArrowDown,
+  ArrowLeft,
+  ArrowRight,
+  Precision,
+  Attack,
+};
+
+enum class PositionedEventType {
+  CursorMove,
+  CursorClick,
+};
+
+enum class SimpleEventType {
+  SwitchHedgehog,
+  Timer,
+  LongJump,
+  HighJump,
+  Accept,
+  Deny,
+};
 
 Q_ENUM_NS(SimpleEventType)
 Q_ENUM_NS(LongEventType)
 Q_ENUM_NS(LongEventState)
 Q_ENUM_NS(PositionedEventType)
 
-};  // namespace
-
-Q_DECLARE_METATYPE(Engine::SimpleEventType)
-Q_DECLARE_METATYPE(Engine::LongEventType)
-Q_DECLARE_METATYPE(Engine::LongEventState)
-Q_DECLARE_METATYPE(Engine::PositionedEventType)
+};  // namespace Engine
 
 #endif  // ENGINE_H
--- a/rust/lib-hedgewars-engine/src/lib.rs	Sun May 19 15:55:26 2024 +0200
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Mon May 20 14:58:30 2024 +0200
@@ -62,7 +62,9 @@
 }
 
 #[no_mangle]
-pub extern "C" fn simple_event(engine_state: &mut EngineInstance, event_type: SimpleEventType) {}
+pub extern "C" fn simple_event(engine_state: &mut EngineInstance, event_type: SimpleEventType) {
+    println!("{:?}", event_type);
+}
 
 #[no_mangle]
 pub extern "C" fn long_event(