TurnTimerWidget.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/ResourceID.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(ResourceID("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::tick(uint32_t msPassed)
  84. {
  85. if(!LOCPLINT || !LOCPLINT->cb)
  86. return;
  87. for (PlayerColor p(0); p < PlayerColor::PLAYER_LIMIT; ++p)
  88. {
  89. auto player = p;
  90. if(LOCPLINT->battleInt)
  91. {
  92. if(auto * stack = LOCPLINT->battleInt->stacksController->getActiveStack())
  93. player = stack->getOwner();
  94. }
  95. else if (!LOCPLINT->cb->isPlayerMakingTurn(player))
  96. continue;
  97. auto time = LOCPLINT->cb->getPlayerTurnTime(player);
  98. cachedTurnTime -= msPassed;
  99. if(cachedTurnTime < 0) cachedTurnTime = 0; //do not go below zero
  100. if(lastPlayer != player)
  101. {
  102. lastPlayer = player;
  103. lastTurnTime = 0;
  104. }
  105. auto timeCheckAndUpdate = [&](int time)
  106. {
  107. if(time / 1000 != lastTurnTime / 1000)
  108. {
  109. //do not update timer on this tick
  110. lastTurnTime = time;
  111. cachedTurnTime = time;
  112. }
  113. else
  114. setTime(player, cachedTurnTime);
  115. };
  116. auto * playerInfo = LOCPLINT->cb->getPlayer(player);
  117. if(player.isValidPlayer() || (playerInfo && playerInfo->isHuman()))
  118. {
  119. if(LOCPLINT->battleInt)
  120. {
  121. if(time.isBattleEnabled())
  122. timeCheckAndUpdate(time.creatureTimer);
  123. }
  124. else
  125. {
  126. timeCheckAndUpdate(time.turnTimer);
  127. }
  128. }
  129. else
  130. timeCheckAndUpdate(0);
  131. }
  132. }