HUD.qml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import QtQuick 2.15
  2. import QtQuick.Controls 2.15
  3. import QtQuick.Layouts 2.15
  4. Item {
  5. id: hud
  6. property bool gameIsPaused: false
  7. property real currentSpeed: 1
  8. property string currentCommandMode: "normal"
  9. property int topPanelHeight: topPanel.height
  10. property int bottomPanelHeight: bottomPanel.height
  11. property int selectionTick: 0
  12. property bool hasMovableUnits: false
  13. signal pauseToggled()
  14. signal speedChanged(real speed)
  15. signal commandModeChanged(string mode)
  16. signal recruit(string unitType)
  17. signal returnToMainMenuRequested()
  18. signal hudBecameVisible()
  19. onVisibleChanged: {
  20. if (visible)
  21. hudBecameVisible();
  22. }
  23. Connections {
  24. function onSelected_units_changed() {
  25. selectionTick += 1;
  26. var hasTroops = false;
  27. if (typeof game !== 'undefined' && game.has_units_selected && game.has_selected_type) {
  28. var troopTypes = ["warrior", "archer", "swordsman", "spearman", "healer", "catapult", "ballista", "horse_archer", "horse_swordsman", "horse_spearman", "elephant"];
  29. for (var i = 0; i < troopTypes.length; i++) {
  30. if (game.has_selected_type(troopTypes[i])) {
  31. hasTroops = true;
  32. break;
  33. }
  34. }
  35. }
  36. var actualMode = "normal";
  37. if (hasTroops && typeof game !== 'undefined' && game.get_selected_units_command_mode)
  38. actualMode = game.get_selected_units_command_mode();
  39. if (currentCommandMode !== actualMode) {
  40. currentCommandMode = actualMode;
  41. commandModeChanged(actualMode);
  42. }
  43. hasMovableUnits = hasTroops;
  44. }
  45. target: (typeof game !== 'undefined') ? game : null
  46. }
  47. Timer {
  48. id: productionRefresh
  49. interval: 100
  50. repeat: true
  51. running: true
  52. onTriggered: {
  53. selectionTick += 1;
  54. if (hasMovableUnits && typeof game !== 'undefined' && game.get_selected_units_command_mode) {
  55. var actualMode = game.get_selected_units_command_mode();
  56. if (currentCommandMode !== actualMode)
  57. currentCommandMode = actualMode;
  58. }
  59. }
  60. }
  61. Item {
  62. id: topPanel
  63. anchors.top: parent.top
  64. anchors.left: parent.left
  65. anchors.right: parent.right
  66. height: Math.max(50, parent.height * 0.08)
  67. HUDTop {
  68. id: hudTop
  69. anchors.fill: parent
  70. gameIsPaused: hud.gameIsPaused
  71. currentSpeed: hud.currentSpeed
  72. onPauseToggled: {
  73. hud.gameIsPaused = !hud.gameIsPaused;
  74. hud.pauseToggled();
  75. }
  76. onSpeedChanged: function(s) {
  77. hud.currentSpeed = s;
  78. hud.speedChanged(s);
  79. }
  80. }
  81. }
  82. Item {
  83. id: bottomPanel
  84. anchors.bottom: parent.bottom
  85. anchors.left: parent.left
  86. anchors.right: parent.right
  87. height: Math.max(140, parent.height * 0.2)
  88. HUDBottom {
  89. id: hudBottom
  90. anchors.fill: parent
  91. currentCommandMode: hud.currentCommandMode
  92. selectionTick: hud.selectionTick
  93. hasMovableUnits: hud.hasMovableUnits
  94. onCommandModeChanged: function(m) {
  95. hud.currentCommandMode = m;
  96. hud.commandModeChanged(m);
  97. }
  98. onRecruit: function(t) {
  99. hud.recruit(t);
  100. }
  101. }
  102. }
  103. HUDVictory {
  104. id: hudVictory
  105. anchors.fill: parent
  106. onReturnToMainMenuRequested: {
  107. hud.returnToMainMenuRequested();
  108. }
  109. Connections {
  110. function onHudBecameVisible() {
  111. if (typeof game !== 'undefined' && game.victory_state === "")
  112. hudVictory.forceHide();
  113. }
  114. target: hud
  115. }
  116. }
  117. }