2
0

TurnTimerWidget.cpp 6.1 KB

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