TurnTimerWidget.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/Canvas.h"
  14. #include "../render/Colors.h"
  15. #include "../render/EFont.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/TextAlignment.h"
  18. #include "../widgets/Images.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../../CCallback.h"
  21. #include "../../lib/CPlayerState.h"
  22. #include <SDL_render.h>
  23. TurnTimerWidget::TurnTimerWidget():
  24. CIntObject(TIME),
  25. turnTime(0), lastTurnTime(0)
  26. {
  27. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  28. watches = std::make_shared<CAnimImage>("VCMI/BATTLEQUEUE/STATESSMALL", 1, 0, 4, 6);
  29. label = std::make_shared<CLabel>(24, 2, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, std::to_string(turnTime));
  30. recActions &= ~DEACTIVATE;
  31. }
  32. void TurnTimerWidget::showAll(Canvas & to)
  33. {
  34. to.drawColor(Rect(4, 4, 68, 24), ColorRGBA(10, 10, 10, 255));
  35. CIntObject::showAll(to);
  36. }
  37. void TurnTimerWidget::show(Canvas & to)
  38. {
  39. showAll(to);
  40. }
  41. void TurnTimerWidget::setTime(int time)
  42. {
  43. turnTime = time / 1000;
  44. std::ostringstream oss;
  45. oss << turnTime / 60 << ":" << std::setw(2) << std::setfill('0') << turnTime % 60;
  46. label->setText(oss.str());
  47. }
  48. void TurnTimerWidget::tick(uint32_t msPassed)
  49. {
  50. if(LOCPLINT && LOCPLINT->cb && !LOCPLINT->battleInt)
  51. {
  52. auto player = LOCPLINT->cb->getCurrentPlayer();
  53. auto time = LOCPLINT->cb->getPlayerTurnTime(player);
  54. cachedTurnTime -= msPassed;
  55. if(time / 1000 != lastTurnTime / 1000)
  56. {
  57. //do not update timer on this tick
  58. lastTurnTime = time;
  59. cachedTurnTime = time;
  60. }
  61. else setTime(cachedTurnTime);
  62. }
  63. }