File: TurnTimerWidget.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (196 lines) | stat: -rw-r--r-- 5,998 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * 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
 *
 */
#include "StdInc.h"
#include "TurnTimerWidget.h"

#include "../CGameInfo.h"
#include "../CPlayerInterface.h"
#include "../battle/BattleInterface.h"
#include "../battle/BattleStacksController.h"
#include "../gui/CGuiHandler.h"
#include "../media/ISoundPlayer.h"
#include "../render/Graphics.h"
#include "../widgets/Images.h"
#include "../widgets/GraphicalPrimitiveCanvas.h"
#include "../widgets/TextControls.h"

#include "../../CCallback.h"
#include "../../lib/CPlayerState.h"
#include "../../lib/CStack.h"
#include "../../lib/StartInfo.h"

TurnTimerWidget::TurnTimerWidget(const Point & position)
	: TurnTimerWidget(position, PlayerColor::NEUTRAL)
{}

TurnTimerWidget::TurnTimerWidget(const Point & position, PlayerColor player)
	: CIntObject(TIME)
	, lastSoundCheckSeconds(0)
	, isBattleMode(player.isValidPlayer())
{
	OBJECT_CONSTRUCTION;

	pos += position;
	pos.w = 0;
	pos.h = 0;
	recActions &= ~DEACTIVATE;
	const auto & timers = LOCPLINT->cb->getStartInfo()->turnTimerInfo;

	backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), pos); // 1 px smaller on all sides

	if (isBattleMode)
		backgroundBorder = std::make_shared<TransparentFilledRectangle>(pos, ColorRGBA(0, 0, 0, 128), Colors::BRIGHT_YELLOW);
	else
		backgroundBorder = std::make_shared<TransparentFilledRectangle>(pos, ColorRGBA(0, 0, 0, 128), Colors::BLACK);

	if (isBattleMode)
	{
		pos.w = 76;

		pos.h += 20;
		playerLabelsMain[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 10, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");

		if (timers.battleTimer != 0)
		{
			pos.h += 20;
			playerLabelsBattle[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 10, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");
		}

		if (!timers.accumulatingUnitTimer && timers.unitTimer != 0)
		{
			pos.h += 20;
			playerLabelsUnit[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 10, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");
		}

		updateTextLabel(player, LOCPLINT->cb->getPlayerTurnTime(player));
	}
	else
	{
		if (!timers.accumulatingTurnTimer && timers.baseTimer != 0)
			pos.w = 120;
		else
			pos.w = 60;

		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 += 20;
			playerLabelsMain[player] = std::make_shared<CLabel>(pos.w / 2, pos.h - 10, FONT_BIG, ETextAlignment::CENTER, graphics->playerColors[player], "");

			updateTextLabel(player, LOCPLINT->cb->getPlayerTurnTime(player));
		}
	}

	backgroundTexture->pos = Rect::createAround(pos, -1);
	backgroundBorder->pos = pos;
}

void TurnTimerWidget::show(Canvas & to)
{
	showAll(to);
}

void TurnTimerWidget::updateNotifications(PlayerColor player, int timeMs)
{
	if(player != LOCPLINT->playerID)
		return;

	int newTimeSeconds = timeMs / 1000;

	if (newTimeSeconds != lastSoundCheckSeconds && notificationThresholds.count(newTimeSeconds))
		CCS->soundh->playSound(AudioPath::builtin("WE5"));

	lastSoundCheckSeconds = newTimeSeconds;
}

static std::string msToString(int timeMs)
{
	int timeSeconds = timeMs / 1000;
	std::ostringstream oss;
	oss << timeSeconds / 60 << ":" << std::setw(2) << std::setfill('0') << timeSeconds % 60;
	return oss.str();
}

void TurnTimerWidget::updateTextLabel(PlayerColor player, const TurnTimerInfo & timer)
{
	const auto & timerSettings = LOCPLINT->cb->getStartInfo()->turnTimerInfo;
	auto mainLabel = playerLabelsMain[player];

	if (isBattleMode)
	{
		mainLabel->setText(msToString(timer.baseTimer + timer.turnTimer));

		if (timerSettings.battleTimer != 0)
		{
			auto battleLabel = playerLabelsBattle[player];
			if (timer.battleTimer != 0)
			{
				if (timerSettings.accumulatingUnitTimer)
					battleLabel->setText("+" + msToString(timer.battleTimer + timer.unitTimer));
				else
					battleLabel->setText("+" + msToString(timer.battleTimer));
			}
			else
				battleLabel->setText("");
		}

		if (!timerSettings.accumulatingUnitTimer && timerSettings.unitTimer != 0)
		{
			auto unitLabel = playerLabelsUnit[player];
			if (timer.unitTimer != 0)
				unitLabel->setText("+" + msToString(timer.unitTimer));
			else
				unitLabel->setText("");
		}
	}
	else
	{
		if (!timerSettings.accumulatingTurnTimer && timerSettings.baseTimer != 0)
			mainLabel->setText(msToString(timer.baseTimer) + "+" + msToString(timer.turnTimer));
		else
			mainLabel->setText(msToString(timer.baseTimer + timer.turnTimer));
	}
}

void TurnTimerWidget::updateTimer(PlayerColor player, uint32_t msPassed)
{
	const auto & gamestateTimer = LOCPLINT->cb->getPlayerTurnTime(player);
	updateNotifications(player, gamestateTimer.valueMs());
	updateTextLabel(player, gamestateTimer);
}

void TurnTimerWidget::tick(uint32_t msPassed)
{
	for(const auto & player : playerLabelsMain)
	{
		if (LOCPLINT->battleInt)
		{
			const auto & battle = LOCPLINT->battleInt->getBattle();

			bool isDefender = battle->sideToPlayer(BattleSide::DEFENDER) == player.first;
			bool isAttacker = battle->sideToPlayer(BattleSide::ATTACKER) == player.first;
			bool isMakingUnitTurn = battle->battleActiveUnit() && battle->battleActiveUnit()->unitOwner() == player.first;
			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);
	}
}