TurnTimerWidget.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "../CPlayerInterface.h"
  13. #include "../battle/BattleInterface.h"
  14. #include "../battle/BattleStacksController.h"
  15. #include "../GameEngine.h"
  16. #include "../GameInstance.h"
  17. #include "../media/ISoundPlayer.h"
  18. #include "../render/Graphics.h"
  19. #include "../widgets/Images.h"
  20. #include "../widgets/GraphicalPrimitiveCanvas.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. , isBattleMode(player.isValidPlayer())
  33. {
  34. OBJECT_CONSTRUCTION;
  35. pos += position;
  36. pos.w = 0;
  37. pos.h = 0;
  38. recActions &= ~DEACTIVATE;
  39. const auto & timers = GAME->interface()->cb->getStartInfo()->turnTimerInfo;
  40. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), pos); // 1 px smaller on all sides
  41. if (isBattleMode)
  42. backgroundBorder = std::make_shared<TransparentFilledRectangle>(pos, ColorRGBA(0, 0, 0, 128), Colors::BRIGHT_YELLOW);
  43. else
  44. backgroundBorder = std::make_shared<TransparentFilledRectangle>(pos, ColorRGBA(0, 0, 0, 128), Colors::BLACK);
  45. if (isBattleMode)
  46. {
  47. pos.w = 76;
  48. pos.h += 20;
  49. playerLabelsMain[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 10, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player.getNum()], "");
  50. if (timers.battleTimer != 0)
  51. {
  52. pos.h += 20;
  53. playerLabelsBattle[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 10, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player.getNum()], "");
  54. }
  55. if (!timers.accumulatingUnitTimer && timers.unitTimer != 0)
  56. {
  57. pos.h += 20;
  58. playerLabelsUnit[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 10, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player.getNum()], "");
  59. }
  60. updateTextLabel(player, GAME->interface()->cb->getPlayerTurnTime(player));
  61. }
  62. else
  63. {
  64. if (!timers.accumulatingTurnTimer && timers.baseTimer != 0)
  65. pos.w = 120;
  66. else
  67. pos.w = 60;
  68. for(PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player)
  69. {
  70. if (GAME->interface()->cb->getStartInfo()->playerInfos.count(player) == 0)
  71. continue;
  72. if (!GAME->interface()->cb->getStartInfo()->playerInfos.at(player).isControlledByHuman())
  73. continue;
  74. pos.h += 20;
  75. playerLabelsMain[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 10, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player.getNum()], "");
  76. updateTextLabel(player, GAME->interface()->cb->getPlayerTurnTime(player));
  77. }
  78. }
  79. backgroundTexture->pos = Rect::createAround(pos, -1);
  80. backgroundBorder->pos = pos;
  81. }
  82. void TurnTimerWidget::show(Canvas & to)
  83. {
  84. showAll(to);
  85. }
  86. void TurnTimerWidget::updateNotifications(PlayerColor player, int timeMs)
  87. {
  88. if(player != GAME->interface()->playerID)
  89. return;
  90. int newTimeSeconds = timeMs / 1000;
  91. if (newTimeSeconds != lastSoundCheckSeconds && notificationThresholds.count(newTimeSeconds))
  92. ENGINE->sound().playSound(AudioPath::builtin("WE5"));
  93. lastSoundCheckSeconds = newTimeSeconds;
  94. }
  95. static std::string msToString(int timeMs)
  96. {
  97. int timeSeconds = timeMs / 1000;
  98. std::ostringstream oss;
  99. oss << timeSeconds / 60 << ":" << std::setw(2) << std::setfill('0') << timeSeconds % 60;
  100. return oss.str();
  101. }
  102. void TurnTimerWidget::updateTextLabel(PlayerColor player, const TurnTimerInfo & timer)
  103. {
  104. const auto & timerSettings = GAME->interface()->cb->getStartInfo()->turnTimerInfo;
  105. auto mainLabel = playerLabelsMain[player];
  106. if (isBattleMode)
  107. {
  108. mainLabel->setText(msToString(timer.baseTimer + timer.turnTimer));
  109. if (timerSettings.battleTimer != 0)
  110. {
  111. auto battleLabel = playerLabelsBattle[player];
  112. if (timer.battleTimer != 0)
  113. {
  114. if (timerSettings.accumulatingUnitTimer)
  115. battleLabel->setText("+" + msToString(timer.battleTimer + timer.unitTimer));
  116. else
  117. battleLabel->setText("+" + msToString(timer.battleTimer));
  118. }
  119. else
  120. battleLabel->setText("");
  121. }
  122. if (!timerSettings.accumulatingUnitTimer && timerSettings.unitTimer != 0)
  123. {
  124. auto unitLabel = playerLabelsUnit[player];
  125. if (timer.unitTimer != 0)
  126. unitLabel->setText("+" + msToString(timer.unitTimer));
  127. else
  128. unitLabel->setText("");
  129. }
  130. }
  131. else
  132. {
  133. if (!timerSettings.accumulatingTurnTimer && timerSettings.baseTimer != 0)
  134. mainLabel->setText(msToString(timer.baseTimer) + "+" + msToString(timer.turnTimer));
  135. else
  136. mainLabel->setText(msToString(timer.baseTimer + timer.turnTimer));
  137. }
  138. }
  139. void TurnTimerWidget::updateTimer(PlayerColor player, uint32_t msPassed)
  140. {
  141. const auto & gamestateTimer = GAME->interface()->cb->getPlayerTurnTime(player);
  142. updateNotifications(player, gamestateTimer.valueMs());
  143. updateTextLabel(player, gamestateTimer);
  144. }
  145. void TurnTimerWidget::tick(uint32_t msPassed)
  146. {
  147. for(const auto & player : playerLabelsMain)
  148. {
  149. if (GAME->interface()->battleInt)
  150. {
  151. const auto & battle = GAME->interface()->battleInt->getBattle();
  152. bool isDefender = battle->sideToPlayer(BattleSide::DEFENDER) == player.first;
  153. bool isAttacker = battle->sideToPlayer(BattleSide::ATTACKER) == player.first;
  154. bool isMakingUnitTurn = battle->battleActiveUnit() && battle->battleActiveUnit()->unitOwner() == player.first;
  155. bool isEngagedInBattle = isDefender || isAttacker;
  156. // Due to way our network message queue works during battle animation
  157. // client actually does not receives updates from server as to which timer is active when game has battle animations playing
  158. // so during battle skip updating timer unless game is waiting for player to select action
  159. if (isEngagedInBattle && !isMakingUnitTurn)
  160. continue;
  161. }
  162. updateTimer(player.first, msPassed);
  163. }
  164. }