1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-09-16 09:26:28 +02:00

Reworked timer widget to show timers for all players

This commit is contained in:
Ivan Savenko
2023-12-19 17:41:20 +02:00
parent 4ed283a357
commit da9c0feebc
5 changed files with 147 additions and 152 deletions

View File

@@ -65,8 +65,8 @@ AdventureMapInterface::AdventureMapInterface():
shortcuts->setState(EAdventureState::MAKING_TURN); shortcuts->setState(EAdventureState::MAKING_TURN);
widget->getMapView()->onViewMapActivated(); widget->getMapView()->onViewMapActivated();
if(LOCPLINT->cb->getStartInfo()->turnTimerInfo.isEnabled() || LOCPLINT->cb->getStartInfo()->turnTimerInfo.isBattleEnabled()) if(LOCPLINT->cb->getStartInfo()->turnTimerInfo.turnTimer != 0)
watches = std::make_shared<TurnTimerWidget>(); watches = std::make_shared<TurnTimerWidget>(Point(24, 24));
addUsedEvents(KEYBOARD | TIME); addUsedEvents(KEYBOARD | TIME);
} }

View File

@@ -15,54 +15,56 @@
#include "../CPlayerInterface.h" #include "../CPlayerInterface.h"
#include "../battle/BattleInterface.h" #include "../battle/BattleInterface.h"
#include "../battle/BattleStacksController.h" #include "../battle/BattleStacksController.h"
#include "../render/EFont.h"
#include "../render/Graphics.h"
#include "../gui/CGuiHandler.h" #include "../gui/CGuiHandler.h"
#include "../gui/TextAlignment.h" #include "../render/Graphics.h"
#include "../widgets/Images.h" #include "../widgets/Images.h"
#include "../widgets/MiscWidgets.h"
#include "../widgets/TextControls.h" #include "../widgets/TextControls.h"
#include "../../CCallback.h" #include "../../CCallback.h"
#include "../../lib/CStack.h"
#include "../../lib/CPlayerState.h" #include "../../lib/CPlayerState.h"
#include "../../lib/filesystem/ResourcePath.h" #include "../../lib/CStack.h"
#include "../../lib/StartInfo.h"
TurnTimerWidget::DrawRect::DrawRect(const Rect & r, const ColorRGBA & c): TurnTimerWidget::TurnTimerWidget(const Point & position)
CIntObject(), rect(r), color(c) : TurnTimerWidget(position, PlayerColor::NEUTRAL)
{ {}
}
void TurnTimerWidget::DrawRect::showAll(Canvas & to) TurnTimerWidget::TurnTimerWidget(const Point & position, PlayerColor player)
: CIntObject(TIME)
, lastSoundCheckSeconds(0)
{ {
to.drawColor(rect, color); pos += position;
pos.w = 50;
CIntObject::showAll(to); pos.h = 0;
}
TurnTimerWidget::TurnTimerWidget():
InterfaceObjectConfigurable(TIME),
turnTime(0), lastTurnTime(0), cachedTurnTime(0), lastPlayer(PlayerColor::CANNOT_DETERMINE)
{
REGISTER_BUILDER("drawRect", &TurnTimerWidget::buildDrawRect);
recActions &= ~DEACTIVATE; recActions &= ~DEACTIVATE;
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
const JsonNode config(JsonPath::builtin("config/widgets/turnTimer.json"));
build(config);
std::transform(variables["notificationTime"].Vector().begin(),
variables["notificationTime"].Vector().end(),
std::inserter(notifications, notifications.begin()),
[](const JsonNode & node){ return node.Integer(); });
}
std::shared_ptr<TurnTimerWidget::DrawRect> TurnTimerWidget::buildDrawRect(const JsonNode & config) const backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), pos); // 1 px smaller on all sides
{ backgroundBorder = std::make_shared<TransparentFilledRectangle>(pos, ColorRGBA(0, 0, 0, 128), Colors::METALLIC_GOLD);
logGlobal->debug("Building widget TurnTimerWidget::DrawRect");
auto rect = readRect(config["rect"]); if (player.isValidPlayer())
auto color = readColor(config["color"]); {
return std::make_shared<TurnTimerWidget::DrawRect>(rect, color); pos.h += 16;
playerLabels[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 8, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");
}
else
{
for(PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player)
{
if (LOCPLINT->cb->getStartInfo()->playerInfos.count(player) == 0)
continue;
if (!LOCPLINT->cb->getStartInfo()->playerInfos.at(player).isControlledByHuman())
continue;
pos.h += 16;
playerLabels[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 8, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");
}
}
backgroundTexture->pos = Rect::createAround(pos, -1);
backgroundBorder->pos = pos;
} }
void TurnTimerWidget::show(Canvas & to) void TurnTimerWidget::show(Canvas & to)
@@ -70,98 +72,67 @@ void TurnTimerWidget::show(Canvas & to)
showAll(to); showAll(to);
} }
void TurnTimerWidget::setTime(PlayerColor player, int time) void TurnTimerWidget::updateNotifications(PlayerColor player, int timeMs)
{ {
int newTime = time / 1000; int newTimeSeconds = timeMs / 1000;
if(player == LOCPLINT->playerID if(player == LOCPLINT->playerID
&& newTime != turnTime && newTimeSeconds != lastSoundCheckSeconds
&& notifications.count(newTime)) && notificationThresholds.count(newTimeSeconds))
{ {
CCS->soundh->playSound(AudioPath::fromJson(variables["notificationSound"])); CCS->soundh->playSound(AudioPath::builtin("WE5"));
} }
lastSoundCheckSeconds = newTimeSeconds;
}
turnTime = newTime; void TurnTimerWidget::updateTextLabel(PlayerColor player, int timeMs)
{
auto label = playerLabels[player];
if(auto w = widget<CLabel>("timer")) std::ostringstream oss;
{ oss << lastSoundCheckSeconds / 60 << ":" << std::setw(2) << std::setfill('0') << lastSoundCheckSeconds % 60;
std::ostringstream oss; label->setText(oss.str());
oss << turnTime / 60 << ":" << std::setw(2) << std::setfill('0') << turnTime % 60; label->setColor(graphics->playerColors[player]);
w->setText(oss.str());
if(graphics && LOCPLINT && LOCPLINT->cb
&& variables["textColorFromPlayerColor"].Bool()
&& player.isValidPlayer())
{
w->setColor(graphics->playerColors[player]);
}
}
} }
void TurnTimerWidget::updateTimer(PlayerColor player, uint32_t msPassed) void TurnTimerWidget::updateTimer(PlayerColor player, uint32_t msPassed)
{ {
const auto & time = LOCPLINT->cb->getPlayerTurnTime(player); const auto & gamestateTimer = LOCPLINT->cb->getPlayerTurnTime(player);
if(time.isActive)
cachedTurnTime -= msPassed; if (!(lastUpdateTimers[player] == gamestateTimer))
if(cachedTurnTime < 0)
cachedTurnTime = 0; //do not go below zero
if(lastPlayer != player)
{ {
lastPlayer = player; lastUpdateTimers[player] = gamestateTimer;
lastTurnTime = 0; countingDownTimers[player] = gamestateTimer;
} }
auto & countingDownTimer = countingDownTimers[player];
if(countingDownTimer.isActive && LOCPLINT->cb->isPlayerMakingTurn(player))
countingDownTimer.substractTimer(msPassed);
auto timeCheckAndUpdate = [&](int time) updateNotifications(player, countingDownTimer.valueMs());
{ updateTextLabel(player, countingDownTimer.valueMs());
if(time / 1000 != lastTurnTime / 1000)
{
//do not update timer on this tick
lastTurnTime = time;
cachedTurnTime = time;
}
else
setTime(player, cachedTurnTime);
};
auto * playerInfo = LOCPLINT->cb->getPlayer(player);
if(player.isValidPlayer() || (playerInfo && playerInfo->isHuman()))
{
if(time.isBattle)
timeCheckAndUpdate(time.baseTimer + time.turnTimer + time.battleTimer + time.unitTimer);
else
timeCheckAndUpdate(time.baseTimer + time.turnTimer);
}
else
timeCheckAndUpdate(0);
} }
void TurnTimerWidget::tick(uint32_t msPassed) void TurnTimerWidget::tick(uint32_t msPassed)
{ {
if(!LOCPLINT || !LOCPLINT->cb) for(const auto & player : playerLabels)
return;
if(LOCPLINT->battleInt)
{ {
if(auto * stack = LOCPLINT->battleInt->stacksController->getActiveStack()) if (LOCPLINT->battleInt)
updateTimer(stack->getOwner(), msPassed);
else
updateTimer(PlayerColor::NEUTRAL, msPassed);
}
else
{
if(LOCPLINT->makingTurn)
updateTimer(LOCPLINT->playerID, msPassed);
else
{ {
for(PlayerColor p(0); p < PlayerColor::PLAYER_LIMIT; ++p) const auto & battle = LOCPLINT->battleInt->getBattle();
{
if(LOCPLINT->cb->isPlayerMakingTurn(p)) bool isDefender = battle->sideToPlayer(BattleSide::DEFENDER) == player.first;
{ bool isAttacker = battle->sideToPlayer(BattleSide::ATTACKER) == player.first;
updateTimer(p, msPassed); bool isMakingUnitTurn = battle->battleActiveUnit()->unitOwner() == player.first;
break; bool isEngagedInBattle = isDefender || isAttacker;
}
} // Due to way our network message queue works during battle animation
// client actually does not receives updates from server as to which timer is active when game has battle animations playing
// so during battle skip updating timer unless game is waiting for player to select action
if (isEngagedInBattle && !isMakingUnitTurn)
continue;
} }
updateTimer(player.first, msPassed);
} }
} }

View File

@@ -11,50 +11,42 @@
#pragma once #pragma once
#include "../gui/CIntObject.h" #include "../gui/CIntObject.h"
#include "../gui/InterfaceObjectConfigurable.h"
#include "../render/Canvas.h"
#include "../render/Colors.h" #include "../render/Colors.h"
#include "../../lib/TurnTimerInfo.h"
class CAnimImage; class CAnimImage;
class CLabel; class CLabel;
class CFilledTexture;
class TransparentFilledRectangle;
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
class PlayerColor; class PlayerColor;
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END
class TurnTimerWidget : public InterfaceObjectConfigurable class TurnTimerWidget : public CIntObject
{ {
private: int lastSoundCheckSeconds;
class DrawRect : public CIntObject std::set<int> notificationThresholds;
{ std::map<PlayerColor, TurnTimerInfo> lastUpdateTimers;
const Rect rect; std::map<PlayerColor, TurnTimerInfo> countingDownTimers;
const ColorRGBA color;
std::map<PlayerColor, std::shared_ptr<CLabel>> playerLabels;
public: std::shared_ptr<CFilledTexture> backgroundTexture;
DrawRect(const Rect &, const ColorRGBA &); std::shared_ptr<TransparentFilledRectangle> backgroundBorder;
void showAll(Canvas & to) override;
};
int turnTime;
int lastTurnTime;
int cachedTurnTime;
PlayerColor lastPlayer;
std::set<int> notifications;
std::shared_ptr<DrawRect> buildDrawRect(const JsonNode & config) const;
void updateTimer(PlayerColor player, uint32_t msPassed); void updateTimer(PlayerColor player, uint32_t msPassed);
public:
void show(Canvas & to) override; void show(Canvas & to) override;
void tick(uint32_t msPassed) override; void tick(uint32_t msPassed) override;
void setTime(PlayerColor player, int time); void updateNotifications(PlayerColor player, int timeMs);
void updateTextLabel(PlayerColor player, int timeMs);
TurnTimerWidget(); public:
/// Activates adventure map mode in which widget will display timer for all players
TurnTimerWidget(const Point & position);
/// Activates battle mode in which timer displays only timer of specific player
TurnTimerWidget(const Point & position, PlayerColor player);
}; };

View File

@@ -22,4 +22,41 @@ bool TurnTimerInfo::isBattleEnabled() const
return turnTimer > 0 || baseTimer > 0 || unitTimer > 0 || battleTimer > 0; return turnTimer > 0 || baseTimer > 0 || unitTimer > 0 || battleTimer > 0;
} }
void TurnTimerInfo::substractTimer(int timeMs)
{
auto const & substractTimer = [&timeMs](int & targetTimer)
{
if (targetTimer > timeMs)
{
targetTimer -= timeMs;
timeMs = 0;
}
else
{
timeMs -= targetTimer;
targetTimer = 0;
}
};
substractTimer(unitTimer);
substractTimer(battleTimer);
substractTimer(turnTimer);
substractTimer(baseTimer);
}
int TurnTimerInfo::valueMs() const
{
return baseTimer + turnTimer + battleTimer + unitTimer;
}
bool TurnTimerInfo::operator == (const TurnTimerInfo & other) const
{
return turnTimer == other.turnTimer &&
baseTimer == other.baseTimer &&
battleTimer == other.battleTimer &&
unitTimer == other.unitTimer &&
accumulatingTurnTimer == other.accumulatingTurnTimer &&
accumulatingUnitTimer == other.accumulatingUnitTimer;
}
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@@ -28,16 +28,11 @@ struct DLL_LINKAGE TurnTimerInfo
bool isEnabled() const; bool isEnabled() const;
bool isBattleEnabled() const; bool isBattleEnabled() const;
bool operator == (const TurnTimerInfo & other) const void substractTimer(int timeMs);
{ int valueMs() const;
return turnTimer == other.turnTimer &&
baseTimer == other.baseTimer && bool operator == (const TurnTimerInfo & other) const;
battleTimer == other.battleTimer &&
unitTimer == other.unitTimer &&
accumulatingTurnTimer == other.accumulatingTurnTimer &&
accumulatingUnitTimer == other.accumulatingUnitTimer;
}
template <typename Handler> template <typename Handler>
void serialize(Handler &h, const int version) void serialize(Handler &h, const int version)
{ {