TurnTimerWidget.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "../render/EFont.h"
  18. #include "../render/Graphics.h"
  19. #include "../gui/CGuiHandler.h"
  20. #include "../gui/TextAlignment.h"
  21. #include "../widgets/Images.h"
  22. #include "../widgets/TextControls.h"
  23. #include "../../CCallback.h"
  24. #include "../../lib/CStack.h"
  25. #include "../../lib/CPlayerState.h"
  26. #include "../../lib/filesystem/ResourcePath.h"
  27. TurnTimerWidget::DrawRect::DrawRect(const Rect & r, const ColorRGBA & c):
  28. CIntObject(), rect(r), color(c)
  29. {
  30. }
  31. void TurnTimerWidget::DrawRect::showAll(Canvas & to)
  32. {
  33. to.drawColor(rect, color);
  34. CIntObject::showAll(to);
  35. }
  36. TurnTimerWidget::TurnTimerWidget():
  37. InterfaceObjectConfigurable(TIME),
  38. turnTime(0), lastTurnTime(0), cachedTurnTime(0), lastPlayer(PlayerColor::CANNOT_DETERMINE)
  39. {
  40. REGISTER_BUILDER("drawRect", &TurnTimerWidget::buildDrawRect);
  41. recActions &= ~DEACTIVATE;
  42. const JsonNode config(JsonPath::builtin("config/widgets/turnTimer.json"));
  43. build(config);
  44. std::transform(variables["notificationTime"].Vector().begin(),
  45. variables["notificationTime"].Vector().end(),
  46. std::inserter(notifications, notifications.begin()),
  47. [](const JsonNode & node){ return node.Integer(); });
  48. }
  49. std::shared_ptr<TurnTimerWidget::DrawRect> TurnTimerWidget::buildDrawRect(const JsonNode & config) const
  50. {
  51. logGlobal->debug("Building widget TurnTimerWidget::DrawRect");
  52. auto rect = readRect(config["rect"]);
  53. auto color = readColor(config["color"]);
  54. return std::make_shared<TurnTimerWidget::DrawRect>(rect, color);
  55. }
  56. void TurnTimerWidget::show(Canvas & to)
  57. {
  58. showAll(to);
  59. }
  60. void TurnTimerWidget::setTime(PlayerColor player, int time)
  61. {
  62. int newTime = time / 1000;
  63. if(player == LOCPLINT->playerID
  64. && newTime != turnTime
  65. && notifications.count(newTime))
  66. {
  67. CCS->soundh->playSound(variables["notificationSound"].String());
  68. }
  69. turnTime = newTime;
  70. if(auto w = widget<CLabel>("timer"))
  71. {
  72. std::ostringstream oss;
  73. oss << turnTime / 60 << ":" << std::setw(2) << std::setfill('0') << turnTime % 60;
  74. w->setText(oss.str());
  75. if(graphics && LOCPLINT && LOCPLINT->cb
  76. && variables["textColorFromPlayerColor"].Bool()
  77. && player.isValidPlayer())
  78. {
  79. w->setColor(graphics->playerColors[player]);
  80. }
  81. }
  82. }
  83. void TurnTimerWidget::updateTimer(PlayerColor player, uint32_t msPassed)
  84. {
  85. const auto & time = LOCPLINT->cb->getPlayerTurnTime(player);
  86. if(time.isActive)
  87. cachedTurnTime -= msPassed;
  88. if(cachedTurnTime < 0)
  89. cachedTurnTime = 0; //do not go below zero
  90. if(lastPlayer != player)
  91. {
  92. lastPlayer = player;
  93. lastTurnTime = 0;
  94. }
  95. auto timeCheckAndUpdate = [&](int time)
  96. {
  97. if(time / 1000 != lastTurnTime / 1000)
  98. {
  99. //do not update timer on this tick
  100. lastTurnTime = time;
  101. cachedTurnTime = time;
  102. }
  103. else
  104. setTime(player, cachedTurnTime);
  105. };
  106. auto * playerInfo = LOCPLINT->cb->getPlayer(player);
  107. if(player.isValidPlayer() || (playerInfo && playerInfo->isHuman()))
  108. {
  109. if(time.isBattle)
  110. timeCheckAndUpdate(time.creatureTimer);
  111. else
  112. timeCheckAndUpdate(time.turnTimer);
  113. }
  114. else
  115. timeCheckAndUpdate(0);
  116. }
  117. void TurnTimerWidget::tick(uint32_t msPassed)
  118. {
  119. if(!LOCPLINT || !LOCPLINT->cb)
  120. return;
  121. if(LOCPLINT->battleInt)
  122. {
  123. if(auto * stack = LOCPLINT->battleInt->stacksController->getActiveStack())
  124. updateTimer(stack->getOwner(), msPassed);
  125. else
  126. updateTimer(PlayerColor::NEUTRAL, msPassed);
  127. }
  128. else
  129. {
  130. for(PlayerColor p(0); p < PlayerColor::PLAYER_LIMIT; ++p)
  131. {
  132. if(LOCPLINT->cb->isPlayerMakingTurn(p))
  133. updateTimer(p, msPassed);
  134. }
  135. }
  136. }