TurnTimerWidget.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * TurnTimerWidget.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "TurnTimerWidget.h"
  12. #include "../CGameInfo.h"
  13. #include "../CMusicHandler.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../battle/BattleInterface.h"
  16. #include "../battle/BattleStacksController.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../render/Graphics.h"
  19. #include "../widgets/Images.h"
  20. #include "../widgets/MiscWidgets.h"
  21. #include "../widgets/TextControls.h"
  22. #include "../../CCallback.h"
  23. #include "../../lib/CPlayerState.h"
  24. #include "../../lib/CStack.h"
  25. #include "../../lib/StartInfo.h"
  26. TurnTimerWidget::TurnTimerWidget(const Point & position)
  27. : TurnTimerWidget(position, PlayerColor::NEUTRAL)
  28. {}
  29. TurnTimerWidget::TurnTimerWidget(const Point & position, PlayerColor player)
  30. : CIntObject(TIME)
  31. , lastSoundCheckSeconds(0)
  32. {
  33. pos += position;
  34. pos.w = 50;
  35. pos.h = 0;
  36. recActions &= ~DEACTIVATE;
  37. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  38. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), pos); // 1 px smaller on all sides
  39. backgroundBorder = std::make_shared<TransparentFilledRectangle>(pos, ColorRGBA(0, 0, 0, 128), Colors::METALLIC_GOLD);
  40. if (player.isValidPlayer())
  41. {
  42. pos.h += 16;
  43. playerLabels[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 8, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");
  44. }
  45. else
  46. {
  47. for(PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player)
  48. {
  49. if (LOCPLINT->cb->getStartInfo()->playerInfos.count(player) == 0)
  50. continue;
  51. if (!LOCPLINT->cb->getStartInfo()->playerInfos.at(player).isControlledByHuman())
  52. continue;
  53. pos.h += 16;
  54. playerLabels[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 8, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");
  55. }
  56. }
  57. backgroundTexture->pos = Rect::createAround(pos, -1);
  58. backgroundBorder->pos = pos;
  59. }
  60. void TurnTimerWidget::show(Canvas & to)
  61. {
  62. showAll(to);
  63. }
  64. void TurnTimerWidget::updateNotifications(PlayerColor player, int timeMs)
  65. {
  66. int newTimeSeconds = timeMs / 1000;
  67. if(player == LOCPLINT->playerID
  68. && newTimeSeconds != lastSoundCheckSeconds
  69. && notificationThresholds.count(newTimeSeconds))
  70. {
  71. CCS->soundh->playSound(AudioPath::builtin("WE5"));
  72. }
  73. lastSoundCheckSeconds = newTimeSeconds;
  74. }
  75. void TurnTimerWidget::updateTextLabel(PlayerColor player, int timeMs)
  76. {
  77. auto label = playerLabels[player];
  78. std::ostringstream oss;
  79. oss << lastSoundCheckSeconds / 60 << ":" << std::setw(2) << std::setfill('0') << lastSoundCheckSeconds % 60;
  80. label->setText(oss.str());
  81. label->setColor(graphics->playerColors[player]);
  82. }
  83. void TurnTimerWidget::updateTimer(PlayerColor player, uint32_t msPassed)
  84. {
  85. const auto & gamestateTimer = LOCPLINT->cb->getPlayerTurnTime(player);
  86. if (!(lastUpdateTimers[player] == gamestateTimer))
  87. {
  88. lastUpdateTimers[player] = gamestateTimer;
  89. countingDownTimers[player] = gamestateTimer;
  90. }
  91. auto & countingDownTimer = countingDownTimers[player];
  92. if(countingDownTimer.isActive && LOCPLINT->cb->isPlayerMakingTurn(player))
  93. countingDownTimer.substractTimer(msPassed);
  94. updateNotifications(player, countingDownTimer.valueMs());
  95. updateTextLabel(player, countingDownTimer.valueMs());
  96. }
  97. void TurnTimerWidget::tick(uint32_t msPassed)
  98. {
  99. for(const auto & player : playerLabels)
  100. {
  101. if (LOCPLINT->battleInt)
  102. {
  103. const auto & battle = LOCPLINT->battleInt->getBattle();
  104. bool isDefender = battle->sideToPlayer(BattleSide::DEFENDER) == player.first;
  105. bool isAttacker = battle->sideToPlayer(BattleSide::ATTACKER) == player.first;
  106. bool isMakingUnitTurn = battle->battleActiveUnit()->unitOwner() == player.first;
  107. bool isEngagedInBattle = isDefender || isAttacker;
  108. // Due to way our network message queue works during battle animation
  109. // client actually does not receives updates from server as to which timer is active when game has battle animations playing
  110. // so during battle skip updating timer unless game is waiting for player to select action
  111. if (isEngagedInBattle && !isMakingUnitTurn)
  112. continue;
  113. }
  114. updateTimer(player.first, msPassed);
  115. }
  116. }