HUD.qml 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import QtQuick 2.15
  2. import QtQuick.Controls 2.15
  3. import QtQuick.Layouts 2.15
  4. Item {
  5. id: hud
  6. signal pauseToggled()
  7. signal speedChanged(real speed)
  8. signal commandModeChanged(string mode)
  9. signal recruit(string unitType)
  10. property bool gameIsPaused: false
  11. property real currentSpeed: 1.0
  12. property string currentCommandMode: "normal"
  13. property int topPanelHeight: topPanel.height
  14. property int bottomPanelHeight: bottomPanel.height
  15. property int selectionTick: 0
  16. property bool hasMovableUnits: false
  17. Connections {
  18. target: (typeof game !== 'undefined') ? game : null
  19. function onSelectedUnitsChanged() {
  20. selectionTick += 1
  21. var hasTroops = false
  22. if (typeof game !== 'undefined' && game.hasUnitsSelected && game.hasSelectedType) {
  23. var troopTypes = ["warrior", "archer"]
  24. for (var i = 0; i < troopTypes.length; i++) {
  25. if (game.hasSelectedType(troopTypes[i])) {
  26. hasTroops = true
  27. break
  28. }
  29. }
  30. }
  31. var actualMode = "normal"
  32. if (hasTroops && typeof game !== 'undefined' && game.getSelectedUnitsCommandMode) {
  33. actualMode = game.getSelectedUnitsCommandMode()
  34. }
  35. if (currentCommandMode !== actualMode) {
  36. currentCommandMode = actualMode
  37. commandModeChanged(actualMode)
  38. }
  39. hasMovableUnits = hasTroops
  40. }
  41. }
  42. Timer {
  43. id: productionRefresh
  44. interval: 100
  45. repeat: true
  46. running: true
  47. onTriggered: {
  48. selectionTick += 1
  49. if (hasMovableUnits && typeof game !== 'undefined' && game.getSelectedUnitsCommandMode) {
  50. var actualMode = game.getSelectedUnitsCommandMode()
  51. if (currentCommandMode !== actualMode) {
  52. currentCommandMode = actualMode
  53. }
  54. }
  55. }
  56. }
  57. Item {
  58. id: topPanel
  59. anchors.top: parent.top
  60. anchors.left: parent.left
  61. anchors.right: parent.right
  62. height: Math.max(50, parent.height * 0.08)
  63. HUDTop {
  64. id: hudTop
  65. anchors.fill: parent
  66. gameIsPaused: hud.gameIsPaused
  67. currentSpeed: hud.currentSpeed
  68. onPauseToggled: { hud.gameIsPaused = !hud.gameIsPaused; hud.pauseToggled(); }
  69. onSpeedChanged: function(s) { hud.currentSpeed = s; hud.speedChanged(s); }
  70. }
  71. }
  72. Item {
  73. id: bottomPanel
  74. anchors.bottom: parent.bottom
  75. anchors.left: parent.left
  76. anchors.right: parent.right
  77. height: Math.max(140, parent.height * 0.20)
  78. HUDBottom {
  79. id: hudBottom
  80. anchors.fill: parent
  81. currentCommandMode: hud.currentCommandMode
  82. selectionTick: hud.selectionTick
  83. hasMovableUnits: hud.hasMovableUnits
  84. onCommandModeChanged: function(m) { hud.currentCommandMode = m; hud.commandModeChanged(m); }
  85. onRecruit: function(t) { hud.recruit(t); }
  86. }
  87. }
  88. HUDVictory { anchors.fill: parent }
  89. }