1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/client/widgets/VideoWidget.cpp

170 lines
3.8 KiB
C++
Raw Normal View History

/*
* 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"
2024-10-16 02:36:26 +02:00
#include "TextControls.h"
#include "../CGameInfo.h"
#include "../gui/CGuiHandler.h"
#include "../media/ISoundPlayer.h"
#include "../media/IVideoPlayer.h"
#include "../render/Canvas.h"
2024-05-15 18:34:23 +02:00
VideoWidgetBase::VideoWidgetBase(const Point & position, const VideoPath & video, bool playAudio)
2024-09-12 23:06:33 +02:00
: VideoWidgetBase(position, video, playAudio, 1.0)
{
}
VideoWidgetBase::VideoWidgetBase(const Point & position, const VideoPath & video, bool playAudio, float scaleFactor)
: playAudio(playAudio), scaleFactor(scaleFactor)
{
addUsedEvents(TIME);
2024-05-15 18:34:23 +02:00
pos += position;
playVideo(video);
}
2024-05-15 18:34:23 +02:00
VideoWidgetBase::~VideoWidgetBase() = default;
void VideoWidgetBase::playVideo(const VideoPath & fileToPlay)
{
2024-10-16 02:36:26 +02:00
OBJECT_CONSTRUCTION;
2024-09-12 23:06:33 +02:00
videoInstance = CCS->videoh->open(fileToPlay, scaleFactor);
2024-05-15 18:34:23 +02:00
if (videoInstance)
{
pos.w = videoInstance->size().x;
pos.h = videoInstance->size().y;
2024-10-16 02:36:26 +02:00
subTitle = std::make_unique<CMultiLineLabel>(Rect(0, (pos.h / 5) * 4, pos.w, pos.h / 5), EFonts::FONT_HIGH_SCORE, ETextAlignment::CENTER, Colors::WHITE, "");
2024-05-15 18:34:23 +02:00
}
2024-05-15 18:34:23 +02:00
if (playAudio)
{
loadAudio(fileToPlay);
if (isActive())
startAudio();
}
}
2024-05-15 18:34:23 +02:00
void VideoWidgetBase::show(Canvas & to)
{
if(videoInstance)
videoInstance->show(pos.topLeft(), to);
2024-10-16 02:36:26 +02:00
if(subTitle)
subTitle->showAll(to);
}
2024-05-15 18:34:23 +02:00
void VideoWidgetBase::loadAudio(const VideoPath & fileToPlay)
{
if (!playAudio)
return;
audioData = CCS->videoh->getAudio(fileToPlay);
}
void VideoWidgetBase::startAudio()
{
2024-05-15 18:34:23 +02:00
if(audioData.first == nullptr)
return;
audioHandle = CCS->soundh->playSound(audioData);
2024-05-15 18:34:23 +02:00
if(audioHandle != -1)
{
CCS->soundh->setCallback(
2024-05-15 18:34:23 +02:00
audioHandle,
[this]()
{
2024-05-15 18:34:23 +02:00
this->audioHandle = -1;
}
2024-05-15 18:34:23 +02:00
);
}
}
2024-05-15 18:34:23 +02:00
void VideoWidgetBase::stopAudio()
{
2024-05-15 18:34:23 +02:00
if(audioHandle != -1)
{
CCS->soundh->resetCallback(audioHandle);
CCS->soundh->stopSound(audioHandle);
audioHandle = -1;
}
}
2024-05-15 18:34:23 +02:00
void VideoWidgetBase::activate()
{
CIntObject::activate();
startAudio();
}
void VideoWidgetBase::deactivate()
{
CIntObject::deactivate();
stopAudio();
}
void VideoWidgetBase::showAll(Canvas & to)
{
if(videoInstance)
videoInstance->show(pos.topLeft(), to);
2024-10-16 02:36:26 +02:00
if(subTitle)
subTitle->showAll(to);
}
2024-05-15 18:34:23 +02:00
void VideoWidgetBase::tick(uint32_t msPassed)
{
if(videoInstance)
{
videoInstance->tick(msPassed);
if(videoInstance->videoEnded())
2024-05-15 18:34:23 +02:00
{
videoInstance.reset();
stopAudio();
onPlaybackFinished();
}
}
2024-10-16 02:36:26 +02:00
if(subTitle && videoInstance)
subTitle->setText(std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()) + "\n" + std::to_string(msPassed) + "\n" + std::to_string(videoInstance->timeStamp()));
}
2024-05-15 18:34:23 +02:00
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, const std::function<void()> & callback)
: VideoWidgetBase(position, video, playAudio)
, callback(callback)
{
}
2024-09-12 23:06:33 +02:00
VideoWidgetOnce::VideoWidgetOnce(const Point & position, const VideoPath & video, bool playAudio, float scaleFactor, const std::function<void()> & callback)
: VideoWidgetBase(position, video, playAudio, scaleFactor)
, callback(callback)
{
}
2024-05-15 18:34:23 +02:00
void VideoWidgetOnce::onPlaybackFinished()
{
callback();
}