1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-03 14:52:11 +02:00
vcmi/client/adventureMap/TurnTimerWidget.cpp

130 lines
3.3 KiB
C++
Raw Normal View History

2023-08-13 14:06:35 +04: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 23:50:40 +04:00
#include "StdInc.h"
2023-08-13 14:06:35 +04:00
#include "TurnTimerWidget.h"
2023-08-17 01:10:03 +04:00
#include "../CGameInfo.h"
#include "../CMusicHandler.h"
2023-08-13 14:06:35 +04:00
#include "../CPlayerInterface.h"
2023-08-14 01:13:37 +04:00
2023-08-13 14:06:35 +04:00
#include "../render/EFont.h"
2023-08-19 19:45:25 +04:00
#include "../render/Graphics.h"
2023-08-13 14:06:35 +04:00
#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-14 01:13:37 +04:00
#include "../../lib/filesystem/ResourceID.h"
TurnTimerWidget::DrawRect::DrawRect(const Rect & r, const ColorRGBA & c):
CIntObject(), rect(r), color(c)
{
}
2023-08-13 14:06:35 +04:00
2023-08-14 01:13:37 +04:00
void TurnTimerWidget::DrawRect::showAll(Canvas & to)
{
to.drawColor(rect, color);
CIntObject::showAll(to);
}
2023-08-13 14:06:35 +04:00
TurnTimerWidget::TurnTimerWidget():
2023-08-14 01:13:37 +04:00
InterfaceObjectConfigurable(TIME),
2023-08-19 18:59:31 +04:00
turnTime(0), lastTurnTime(0), cachedTurnTime(0)
2023-08-13 14:06:35 +04:00
{
2023-08-14 01:13:37 +04:00
REGISTER_BUILDER("drawRect", &TurnTimerWidget::buildDrawRect);
2023-08-13 14:06:35 +04:00
recActions &= ~DEACTIVATE;
2023-08-14 01:13:37 +04:00
const JsonNode config(ResourceID("config/widgets/turnTimer.json"));
build(config);
2023-08-17 01:10:03 +04:00
std::transform(variables["notificationTime"].Vector().begin(),
variables["notificationTime"].Vector().end(),
std::inserter(notifications, notifications.begin()),
[](const JsonNode & node){ return node.Integer(); });
2023-08-13 14:06:35 +04:00
}
2023-08-14 01:13:37 +04:00
std::shared_ptr<TurnTimerWidget::DrawRect> TurnTimerWidget::buildDrawRect(const JsonNode & config) const
2023-08-13 14:06:35 +04:00
{
2023-08-14 01:13:37 +04: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 14:06:35 +04:00
}
void TurnTimerWidget::show(Canvas & to)
{
showAll(to);
}
void TurnTimerWidget::setTime(int time)
{
2023-08-17 01:10:03 +04:00
int newTime = time / 1000;
if((LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID)
&& (newTime != turnTime)
&& notifications.count(newTime))
2023-08-17 01:10:03 +04:00
CCS->soundh->playSound(variables["notificationSound"].String());
turnTime = newTime;
2023-08-14 01:13:37 +04: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-19 19:45:25 +04:00
if(graphics && LOCPLINT && LOCPLINT->cb
&& variables["textColorFromPlayerColor"].Bool()
&& LOCPLINT->cb->getCurrentPlayer().isValidPlayer())
{
w->setColor(graphics->playerColors[LOCPLINT->cb->getCurrentPlayer()]);
}
2023-08-14 01:13:37 +04:00
}
2023-08-13 14:06:35 +04:00
}
void TurnTimerWidget::tick(uint32_t msPassed)
{
2023-08-14 23:31:44 +04:00
if(LOCPLINT && LOCPLINT->cb)
2023-08-13 14:06:35 +04:00
{
auto player = LOCPLINT->cb->getCurrentPlayer();
auto time = LOCPLINT->cb->getPlayerTurnTime(player);
cachedTurnTime -= msPassed;
2023-08-14 23:31:44 +04:00
if(cachedTurnTime < 0) cachedTurnTime = 0; //do not go below zero
2023-08-17 01:10:03 +04:00
auto timeCheckAndUpdate = [&](int time)
2023-08-13 14:06:35 +04:00
{
2023-08-17 01:10:03 +04:00
if(time / 1000 != lastTurnTime / 1000)
2023-08-14 23:31:44 +04:00
{
2023-08-17 01:10:03 +04:00
//do not update timer on this tick
lastTurnTime = time;
cachedTurnTime = time;
2023-08-14 23:31:44 +04:00
}
2023-08-17 01:10:03 +04:00
else setTime(cachedTurnTime);
};
auto * playerInfo = LOCPLINT->cb->getPlayer(player);
if(playerInfo && playerInfo->isHuman())
2023-08-17 01:10:03 +04:00
{
if(LOCPLINT->battleInt)
{
if(time.isBattleEnabled())
timeCheckAndUpdate(time.creatureTimer);
}
else
{
timeCheckAndUpdate(time.turnTimer);
}
2023-08-14 23:31:44 +04:00
}
else
timeCheckAndUpdate(0);
2023-08-13 14:06:35 +04:00
}
}