TurnTimerWidget.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. , isBattleMode(player.isValidPlayer())
  33. {
  34. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  35. pos += position;
  36. pos.w = 0;
  37. pos.h = 0;
  38. recActions &= ~DEACTIVATE;
  39. const auto & timers = LOCPLINT->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 += 16;
  49. playerLabelsMain[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 8, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");
  50. if (timers.battleTimer != 0)
  51. {
  52. pos.h += 16;
  53. playerLabelsBattle[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 8, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");
  54. }
  55. if (!timers.accumulatingUnitTimer && timers.unitTimer != 0)
  56. {
  57. pos.h += 16;
  58. playerLabelsUnit[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 8, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");
  59. }
  60. }
  61. else
  62. {
  63. if (!timers.accumulatingTurnTimer && timers.baseTimer != 0)
  64. pos.w = 120;
  65. else
  66. pos.w = 50;
  67. for(PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player)
  68. {
  69. if (LOCPLINT->cb->getStartInfo()->playerInfos.count(player) == 0)
  70. continue;
  71. if (!LOCPLINT->cb->getStartInfo()->playerInfos.at(player).isControlledByHuman())
  72. continue;
  73. pos.h += 16;
  74. playerLabelsMain[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 8, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");
  75. }
  76. }
  77. backgroundTexture->pos = Rect::createAround(pos, -1);
  78. backgroundBorder->pos = pos;
  79. }
  80. void TurnTimerWidget::show(Canvas & to)
  81. {
  82. showAll(to);
  83. }
  84. void TurnTimerWidget::updateNotifications(PlayerColor player, int timeMs)
  85. {
  86. int newTimeSeconds = timeMs / 1000;
  87. if(player == LOCPLINT->playerID
  88. && newTimeSeconds != lastSoundCheckSeconds
  89. && notificationThresholds.count(newTimeSeconds))
  90. {
  91. CCS->soundh->playSound(AudioPath::builtin("WE5"));
  92. }
  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 = LOCPLINT->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 = LOCPLINT->cb->getPlayerTurnTime(player);
  142. if (!(lastUpdateTimers[player] == gamestateTimer))
  143. {
  144. lastUpdateTimers[player] = gamestateTimer;
  145. countingDownTimers[player] = gamestateTimer;
  146. }
  147. auto & countingDownTimer = countingDownTimers[player];
  148. if(countingDownTimer.isActive && LOCPLINT->cb->isPlayerMakingTurn(player))
  149. countingDownTimer.substractTimer(msPassed);
  150. updateNotifications(player, countingDownTimer.valueMs());
  151. updateTextLabel(player, countingDownTimer);
  152. }
  153. void TurnTimerWidget::tick(uint32_t msPassed)
  154. {
  155. for(const auto & player : playerLabelsMain)
  156. {
  157. if (LOCPLINT->battleInt)
  158. {
  159. const auto & battle = LOCPLINT->battleInt->getBattle();
  160. bool isDefender = battle->sideToPlayer(BattleSide::DEFENDER) == player.first;
  161. bool isAttacker = battle->sideToPlayer(BattleSide::ATTACKER) == player.first;
  162. bool isMakingUnitTurn = battle->battleActiveUnit() && battle->battleActiveUnit()->unitOwner() == player.first;
  163. bool isEngagedInBattle = isDefender || isAttacker;
  164. // Due to way our network message queue works during battle animation
  165. // client actually does not receives updates from server as to which timer is active when game has battle animations playing
  166. // so during battle skip updating timer unless game is waiting for player to select action
  167. if (isEngagedInBattle && !isMakingUnitTurn)
  168. continue;
  169. }
  170. updateTimer(player.first, msPassed);
  171. }
  172. }