TurnTimerWidget.cpp 6.0 KB

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