qmlfrontend/Page1.qml
author unC0Rr
Mon, 20 May 2024 14:58:30 +0200
changeset 16045 4933920eba89
parent 16041 caba603f461f
child 16046 e8afb1bf2779
permissions -rw-r--r--
Implement key bindings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
     1
import QtQuick
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents: 12854
diff changeset
     2
import Hedgewars.Engine 1.0
12854
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
     3
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
     4
Page1Form {
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
     5
  property HWEngine hwEngine
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
     6
  property var keyBindings: ({
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
     7
      "long": {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
     8
        [Qt.Key_Space]: Engine.Attack,
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
     9
        [Qt.Key_Up]: Engine.ArrowUp,
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    10
        [Qt.Key_Right]: Engine.ArrowRight,
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    11
        [Qt.Key_Down]: Engine.ArrowDown,
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    12
        [Qt.Key_Left]: Engine.ArrowLeft,
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    13
        [Qt.Key_Shift]: Engine.Precision
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    14
      },
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    15
      "simple": {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    16
        [Qt.Key_Tab]: Engine.SwitchHedgehog,
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    17
        [Qt.Key_Enter]: Engine.LongJump,
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    18
        [Qt.Key_Backspace]: Engine.HighJump,
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    19
        [Qt.Key_Y]: Engine.Accept,
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    20
        [Qt.Key_N]: Engine.Deny
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    21
      }
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    22
    })
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    23
  property NetSession netSession
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    24
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    25
  focus: true
14875
aed75d439027 Implement external events approach to input user actions into engine.
unc0rr
parents: 14393
diff changeset
    26
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    27
  Component.onCompleted: {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    28
    hwEngine = hwEngineComponent.createObject();
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    29
  }
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    30
  Keys.onPressed: event => {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    31
    if (event.isAutoRepeat) {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    32
      return;
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    33
    }
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    34
    let action = keyBindings["simple"][event.key];
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    35
    if (action !== undefined) {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    36
      gameView.engineInstance.simpleEvent(action);
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    37
      event.accepted = true;
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    38
      return;
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    39
    }
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    40
    action = keyBindings["long"][event.key];
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    41
    if (action !== undefined) {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    42
      gameView.engineInstance.longEvent(action, Engine.Set);
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    43
      event.accepted = true;
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    44
    }
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    45
  }
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    46
  Keys.onReleased: event => {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    47
    if (event.isAutoRepeat) {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    48
      return;
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    49
    }
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    50
    const action = keyBindings["long"][event.key];
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    51
    if (action !== undefined) {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    52
      gameView.engineInstance.longEvent(action, Engine.Unset);
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    53
      event.accepted = true;
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    54
    }
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    55
  }
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    56
  netButton.onClicked: {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    57
    netSession = netSessionComponent.createObject();
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    58
    netSession.open();
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    59
  }
15919
d52f5d8e75e6 Allow passing data_path from QML
unc0rr
parents: 15238
diff changeset
    60
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    61
  Component {
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    62
    id: hwEngineComponent
14392
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14319
diff changeset
    63
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    64
    HWEngine {
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    65
      dataPath: "../share/hedgewars/Data"
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    66
      engineLibrary: "../rust/lib-hedgewars-engine/target/debug/libhedgewars_engine.so"
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    67
      previewAcceptor: PreviewAcceptor
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    68
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    69
      onPreviewImageChanged: previewImage.source = "image://preview/image"
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    70
      onPreviewIsRendering: previewImage.source = "qrc:/res/iconTime.png"
14392
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14319
diff changeset
    71
    }
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    72
  }
14392
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14319
diff changeset
    73
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    74
  Component {
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    75
    id: netSessionComponent
14936
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents: 14875
diff changeset
    76
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    77
    NetSession {
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    78
      nickname: "test0272"
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    79
      url: "hwnet://gameserver.hedgewars.org:46632"
14936
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents: 14875
diff changeset
    80
    }
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    81
  }
14936
a3ad06ac390e Proof of concept for new net game client
unc0rr
parents: 14875
diff changeset
    82
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    83
  tickButton {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    84
    onClicked: {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    85
      tickButton.visible = false;
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    86
      gameView.tick(100);
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    87
    }
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    88
  }
14392
90bd2c331703 Add possibility to instantiate HWEngine objects from QML, reorganize work with preview
unC0Rr
parents: 14319
diff changeset
    89
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    90
  gameButton {
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    91
    visible: !gameView.engineInstance
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    92
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    93
    onClicked: {
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    94
      const engineInstance = hwEngine.runQuickGame();
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    95
      gameView.engineInstance = engineInstance;
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    96
    }
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    97
  }
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
    98
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
    99
  button1 {
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
   100
    visible: !gameView.engineInstance
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
   101
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
   102
    onClicked: {
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
   103
      hwEngine.getPreview();
14875
aed75d439027 Implement external events approach to input user actions into engine.
unc0rr
parents: 14393
diff changeset
   104
    }
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
   105
  }
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
   106
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
   107
  preview {
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
   108
    visible: !gameView.engineInstance
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
   109
  }
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
   110
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
   111
  gameMouseArea {
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
   112
    onPositionChanged: event => {
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
   113
      gameView.engineInstance.moveCamera(Qt.point(event.x - gameMouseArea.lastPoint.x, event.y - gameMouseArea.lastPoint.y));
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
   114
      gameMouseArea.lastPoint = Qt.point(event.x, event.y);
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
   115
    }
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
   116
    onPressed: event => {
16045
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
   117
      gameMouseArea.lastPoint = Qt.point(event.x, event.y);
4933920eba89 Implement key bindings
unC0Rr
parents: 16041
diff changeset
   118
    }
16041
caba603f461f Allow to move camera by dragging mouse cursor over game field
unC0Rr
parents: 15919
diff changeset
   119
  }
12854
28cb18c5e712 Add new qmlfrontend project template
unc0rr
parents:
diff changeset
   120
}