1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/client/adventureMap/TurnTimerWidget.cpp

89 lines
2.1 KiB
C++
Raw Normal View History

2023-08-13 12:06:35 +02:00
/*
* TurnTimerWidget.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
2023-08-13 21:50:40 +02:00
#include "StdInc.h"
2023-08-13 12:06:35 +02:00
#include "TurnTimerWidget.h"
#include "../CPlayerInterface.h"
2023-08-13 23:13:37 +02:00
2023-08-13 12:06:35 +02:00
#include "../render/EFont.h"
#include "../gui/CGuiHandler.h"
#include "../gui/TextAlignment.h"
#include "../widgets/Images.h"
#include "../widgets/TextControls.h"
#include "../../CCallback.h"
#include "../../lib/CPlayerState.h"
2023-08-13 23:13:37 +02:00
#include "../../lib/filesystem/ResourceID.h"
TurnTimerWidget::DrawRect::DrawRect(const Rect & r, const ColorRGBA & c):
CIntObject(), rect(r), color(c)
{
}
2023-08-13 12:06:35 +02:00
2023-08-13 23:13:37 +02:00
void TurnTimerWidget::DrawRect::showAll(Canvas & to)
{
to.drawColor(rect, color);
CIntObject::showAll(to);
}
2023-08-13 12:06:35 +02:00
TurnTimerWidget::TurnTimerWidget():
2023-08-13 23:13:37 +02:00
InterfaceObjectConfigurable(TIME),
2023-08-13 12:06:35 +02:00
turnTime(0), lastTurnTime(0)
{
2023-08-13 23:13:37 +02:00
REGISTER_BUILDER("drawRect", &TurnTimerWidget::buildDrawRect);
2023-08-13 12:06:35 +02:00
recActions &= ~DEACTIVATE;
2023-08-13 23:13:37 +02:00
const JsonNode config(ResourceID("config/widgets/turnTimer.json"));
build(config);
2023-08-13 12:06:35 +02:00
}
2023-08-13 23:13:37 +02:00
std::shared_ptr<TurnTimerWidget::DrawRect> TurnTimerWidget::buildDrawRect(const JsonNode & config) const
2023-08-13 12:06:35 +02:00
{
2023-08-13 23:13:37 +02:00
logGlobal->debug("Building widget TurnTimerWidget::DrawRect");
auto rect = readRect(config["rect"]);
auto color = readColor(config["color"]);
return std::make_shared<TurnTimerWidget::DrawRect>(rect, color);
2023-08-13 12:06:35 +02:00
}
void TurnTimerWidget::show(Canvas & to)
{
showAll(to);
}
void TurnTimerWidget::setTime(int time)
{
turnTime = time / 1000;
2023-08-13 23:13:37 +02:00
if(auto w = widget<CLabel>("timer"))
{
std::ostringstream oss;
oss << turnTime / 60 << ":" << std::setw(2) << std::setfill('0') << turnTime % 60;
w->setText(oss.str());
}
2023-08-13 12:06:35 +02:00
}
void TurnTimerWidget::tick(uint32_t msPassed)
{
if(LOCPLINT && LOCPLINT->cb && !LOCPLINT->battleInt)
{
auto player = LOCPLINT->cb->getCurrentPlayer();
auto time = LOCPLINT->cb->getPlayerTurnTime(player);
cachedTurnTime -= msPassed;
if(time / 1000 != lastTurnTime / 1000)
{
//do not update timer on this tick
lastTurnTime = time;
cachedTurnTime = time;
}
else setTime(cachedTurnTime);
}
}