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

155 lines
3.8 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"
2023-08-16 23:10:03 +02:00
#include "../CGameInfo.h"
#include "../CMusicHandler.h"
2023-08-13 12:06:35 +02:00
#include "../CPlayerInterface.h"
#include "../battle/BattleInterface.h"
#include "../battle/BattleStacksController.h"
2023-08-13 23:13:37 +02:00
2023-08-13 12:06:35 +02:00
#include "../render/EFont.h"
2023-08-19 17:45:25 +02:00
#include "../render/Graphics.h"
2023-08-13 12:06:35 +02:00
#include "../gui/CGuiHandler.h"
#include "../gui/TextAlignment.h"
#include "../widgets/Images.h"
#include "../widgets/TextControls.h"
#include "../../CCallback.h"
#include "../../lib/CStack.h"
2023-08-13 12:06:35 +02:00
#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-26 04:15:28 +02:00
turnTime(0), lastTurnTime(0), cachedTurnTime(0), lastPlayer(PlayerColor::CANNOT_DETERMINE)
2023-08-13 12:06:35 +02:00
{
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-16 23:10:03 +02: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 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(PlayerColor player, int time)
2023-08-13 12:06:35 +02:00
{
2023-08-16 23:10:03 +02:00
int newTime = time / 1000;
2023-08-26 04:15:28 +02:00
if(player == LOCPLINT->playerID
&& newTime != turnTime
&& notifications.count(newTime))
{
2023-08-16 23:10:03 +02:00
CCS->soundh->playSound(variables["notificationSound"].String());
}
2023-08-16 23:10:03 +02:00
turnTime = newTime;
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-19 17:45:25 +02:00
if(graphics && LOCPLINT && LOCPLINT->cb
&& variables["textColorFromPlayerColor"].Bool()
&& player.isValidPlayer())
2023-08-19 17:45:25 +02:00
{
w->setColor(graphics->playerColors[player]);
2023-08-19 17:45:25 +02:00
}
2023-08-13 23:13:37 +02:00
}
2023-08-13 12:06:35 +02:00
}
void TurnTimerWidget::tick(uint32_t msPassed)
{
if(!LOCPLINT || !LOCPLINT->cb)
return;
for (PlayerColor p(0); p < PlayerColor::PLAYER_LIMIT; ++p)
2023-08-13 12:06:35 +02:00
{
auto player = p;
if(LOCPLINT->battleInt)
{
if(auto * stack = LOCPLINT->battleInt->stacksController->getActiveStack())
player = stack->getOwner();
}
else if (!LOCPLINT->cb->isPlayerMakingTurn(player))
continue;
2023-08-13 12:06:35 +02:00
auto time = LOCPLINT->cb->getPlayerTurnTime(player);
cachedTurnTime -= msPassed;
2023-08-14 21:31:44 +02:00
if(cachedTurnTime < 0) cachedTurnTime = 0; //do not go below zero
2023-08-26 04:15:28 +02:00
if(lastPlayer != player)
{
lastPlayer = player;
lastTurnTime = 0;
}
2023-08-16 23:10:03 +02:00
auto timeCheckAndUpdate = [&](int time)
2023-08-13 12:06:35 +02:00
{
2023-08-16 23:10:03 +02:00
if(time / 1000 != lastTurnTime / 1000)
2023-08-14 21:31:44 +02:00
{
2023-08-16 23:10:03 +02:00
//do not update timer on this tick
lastTurnTime = time;
cachedTurnTime = time;
2023-08-14 21:31:44 +02:00
}
else
setTime(player, cachedTurnTime);
2023-08-16 23:10:03 +02:00
};
auto * playerInfo = LOCPLINT->cb->getPlayer(player);
2023-08-26 04:15:28 +02:00
if(player.isValidPlayer() || (playerInfo && playerInfo->isHuman()))
2023-08-16 23:10:03 +02:00
{
if(LOCPLINT->battleInt)
{
if(time.isBattleEnabled())
timeCheckAndUpdate(time.creatureTimer);
}
else
{
timeCheckAndUpdate(time.turnTimer);
}
2023-08-14 21:31:44 +02:00
}
else
timeCheckAndUpdate(0);
2023-08-13 12:06:35 +02:00
}
}