TurnTimerWidget.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "../render/EFont.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/TextAlignment.h"
  16. #include "../widgets/Images.h"
  17. #include "../widgets/TextControls.h"
  18. #include "../../CCallback.h"
  19. #include "../../lib/CPlayerState.h"
  20. #include "../../lib/filesystem/ResourceID.h"
  21. TurnTimerWidget::DrawRect::DrawRect(const Rect & r, const ColorRGBA & c):
  22. CIntObject(), rect(r), color(c)
  23. {
  24. }
  25. void TurnTimerWidget::DrawRect::showAll(Canvas & to)
  26. {
  27. to.drawColor(rect, color);
  28. CIntObject::showAll(to);
  29. }
  30. TurnTimerWidget::TurnTimerWidget():
  31. InterfaceObjectConfigurable(TIME),
  32. turnTime(0), lastTurnTime(0)
  33. {
  34. REGISTER_BUILDER("drawRect", &TurnTimerWidget::buildDrawRect);
  35. recActions &= ~DEACTIVATE;
  36. const JsonNode config(ResourceID("config/widgets/turnTimer.json"));
  37. build(config);
  38. }
  39. std::shared_ptr<TurnTimerWidget::DrawRect> TurnTimerWidget::buildDrawRect(const JsonNode & config) const
  40. {
  41. logGlobal->debug("Building widget TurnTimerWidget::DrawRect");
  42. auto rect = readRect(config["rect"]);
  43. auto color = readColor(config["color"]);
  44. return std::make_shared<TurnTimerWidget::DrawRect>(rect, color);
  45. }
  46. void TurnTimerWidget::show(Canvas & to)
  47. {
  48. showAll(to);
  49. }
  50. void TurnTimerWidget::setTime(int time)
  51. {
  52. turnTime = time / 1000;
  53. if(auto w = widget<CLabel>("timer"))
  54. {
  55. std::ostringstream oss;
  56. oss << turnTime / 60 << ":" << std::setw(2) << std::setfill('0') << turnTime % 60;
  57. w->setText(oss.str());
  58. }
  59. }
  60. void TurnTimerWidget::tick(uint32_t msPassed)
  61. {
  62. if(LOCPLINT && LOCPLINT->cb)
  63. {
  64. auto player = LOCPLINT->cb->getCurrentPlayer();
  65. auto time = LOCPLINT->cb->getPlayerTurnTime(player);
  66. cachedTurnTime -= msPassed;
  67. if(cachedTurnTime < 0) cachedTurnTime = 0; //do not go below zero
  68. if(LOCPLINT->battleInt)
  69. {
  70. if(time.isBattleEnabled())
  71. {
  72. if(time.creatureTimer / 1000 != lastTurnTime / 1000)
  73. {
  74. //do not update timer on this tick
  75. lastTurnTime = time.creatureTimer;
  76. cachedTurnTime = time.creatureTimer;
  77. }
  78. else setTime(cachedTurnTime);
  79. }
  80. }
  81. else
  82. {
  83. if(time.turnTimer / 1000 != lastTurnTime / 1000)
  84. {
  85. //do not update timer on this tick
  86. lastTurnTime = time.turnTimer;
  87. cachedTurnTime = time.turnTimer;
  88. }
  89. else setTime(cachedTurnTime);
  90. }
  91. }
  92. }