File: VideoWidget.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 (221 lines) | stat: -rw-r--r-- 5,470 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * TextControls.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 "VideoWidget.h"
#include "TextControls.h"
#include "IVideoHolder.h"

#include "../CGameInfo.h"
#include "../gui/CGuiHandler.h"
#include "../media/ISoundPlayer.h"
#include "../media/IVideoPlayer.h"
#include "../render/Canvas.h"
#include "../render/IScreenHandler.h"

#include "../../lib/filesystem/Filesystem.h"

VideoWidgetBase::VideoWidgetBase(const Point & position, const VideoPath & video, bool playAudio)
	: VideoWidgetBase(position, video, playAudio, 1.0)
{
}

VideoWidgetBase::VideoWidgetBase(const Point & position, const VideoPath & video, bool playAudio, float scaleFactor)
	: playAudio(playAudio), scaleFactor(scaleFactor)
{
	addUsedEvents(TIME);
	pos += position;
	playVideo(video);
}

VideoWidgetBase::~VideoWidgetBase() = default;

void VideoWidgetBase::playVideo(const VideoPath & fileToPlay)
{
	OBJECT_CONSTRUCTION;

	JsonPath subTitlePath = fileToPlay.toType<EResType::JSON>();
	JsonPath subTitlePathVideoDir = subTitlePath.addPrefix("VIDEO/");
	if(CResourceHandler::get()->existsResource(subTitlePath))
		subTitleData = JsonNode(subTitlePath);
	else if(CResourceHandler::get()->existsResource(subTitlePathVideoDir))
		subTitleData = JsonNode(subTitlePathVideoDir);

	float preScaleFactor = 1;
	VideoPath videoFile = fileToPlay;
	if(GH.screenHandler().getScalingFactor() > 1)
	{
		std::vector<int> factorsToCheck = {GH.screenHandler().getScalingFactor(), 4, 3, 2};
		for(auto factorToCheck : factorsToCheck)
		{
			std::string name = boost::algorithm::to_upper_copy(videoFile.getName());
			boost::replace_all(name, "VIDEO/", std::string("VIDEO") + std::to_string(factorToCheck) + std::string("X/"));
			auto p = VideoPath::builtin(name).addPrefix("VIDEO" + std::to_string(factorToCheck) + "X/");
			if(CResourceHandler::get()->existsResource(p))
			{
				preScaleFactor = 1.0 / static_cast<float>(factorToCheck);
				videoFile = p;
				break;
			}
		}
	}

	videoInstance = CCS->videoh->open(videoFile, scaleFactor * preScaleFactor);
	if (videoInstance)
	{
		pos.w = videoInstance->size().x;
		pos.h = videoInstance->size().y;
		if(!subTitleData.isNull())
			subTitle = std::make_unique<CMultiLineLabel>(Rect(0, (pos.h / 5) * 4, pos.w, pos.h / 5), EFonts::FONT_HIGH_SCORE, ETextAlignment::CENTER, Colors::WHITE);
	}

	if (playAudio)
	{
		loadAudio(fileToPlay);
		if (isActive())
			startAudio();
	}
}

void VideoWidgetBase::show(Canvas & to)
{
	if(videoInstance)
		to.draw(*videoInstance, pos.topLeft());
	if(subTitle)
		subTitle->showAll(to);
}

void VideoWidgetBase::loadAudio(const VideoPath & fileToPlay)
{
	if (!playAudio)
		return;

	audioData = CCS->videoh->getAudio(fileToPlay);
}

void VideoWidgetBase::startAudio()
{
	if(audioData.first == nullptr)
		return;

	audioHandle = CCS->soundh->playSound(audioData);

	if(audioHandle != -1)
	{
		CCS->soundh->setCallback(
			audioHandle,
			[this]()
			{
				this->audioHandle = -1;
			}
		);
	}
}

void VideoWidgetBase::stopAudio()
{
	if(audioHandle != -1)
	{
		CCS->soundh->resetCallback(audioHandle);
		CCS->soundh->stopSound(audioHandle);
		audioHandle = -1;
	}
}

std::string VideoWidgetBase::getSubTitleLine(double timestamp)
{
	if(subTitleData.isNull())
		return {};

	for(auto & segment : subTitleData.Vector())
		if(timestamp > segment["timeStart"].Float() && timestamp < segment["timeEnd"].Float())
			return segment["text"].String();
	
	return {};
}

void VideoWidgetBase::activate()
{
	CIntObject::activate();
	if(audioHandle != -1)
		CCS->soundh->resumeSound(audioHandle);
	else
		startAudio();
	if(videoInstance)
		videoInstance->activate();
}

void VideoWidgetBase::deactivate()
{
	CIntObject::deactivate();
	CCS->soundh->pauseSound(audioHandle);
	if(videoInstance)
		videoInstance->deactivate();
}

void VideoWidgetBase::showAll(Canvas & to)
{
	if(videoInstance)
		to.draw(*videoInstance, pos.topLeft());
	if(subTitle)
		subTitle->showAll(to);
}

void VideoWidgetBase::tick(uint32_t msPassed)
{
	if(videoInstance)
	{
		videoInstance->tick(msPassed);

		if(!videoInstance->videoEnded() && subTitle)
			subTitle->setText(getSubTitleLine(videoInstance->timeStamp()));

		if(videoInstance->videoEnded())
		{
			videoInstance.reset();
			stopAudio();
			onPlaybackFinished();
			// WARNING: onPlaybackFinished call may destoy `this`. Make sure that this is the very last operation in this method!
		}
	}
}

VideoWidget::VideoWidget(const Point & position, const VideoPath & prologue, const VideoPath & looped, bool playAudio)
	: VideoWidgetBase(position, prologue, playAudio)
	, loopedVideo(looped)
{
}

VideoWidget::VideoWidget(const Point & position, const VideoPath & looped, bool playAudio)
	: VideoWidgetBase(position, looped, playAudio)
	, loopedVideo(looped)
{
}

void VideoWidget::onPlaybackFinished()
{
	playVideo(loopedVideo);
}

VideoWidgetOnce::VideoWidgetOnce(const Point & position, const VideoPath & video, bool playAudio, IVideoHolder * owner)
	: VideoWidgetBase(position, video, playAudio)
	, owner(owner)
{
}

VideoWidgetOnce::VideoWidgetOnce(const Point & position, const VideoPath & video, bool playAudio, float scaleFactor, IVideoHolder * owner)
	: VideoWidgetBase(position, video, playAudio, scaleFactor)
	, owner(owner)
{
}

void VideoWidgetOnce::onPlaybackFinished()
{
	owner->onVideoPlaybackFinished();
}