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