TurnTimerWidget.cpp 3.3 KB

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