VideoWidget.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * TextControls.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "VideoWidget.h"
  12. #include "TextControls.h"
  13. #include "IVideoHolder.h"
  14. #include "../GameEngine.h"
  15. #include "../media/ISoundPlayer.h"
  16. #include "../media/IVideoPlayer.h"
  17. #include "../render/Canvas.h"
  18. #include "../render/IScreenHandler.h"
  19. #include "../../lib/CConfigHandler.h"
  20. #include "../../lib/filesystem/Filesystem.h"
  21. VideoWidgetBase::VideoWidgetBase(const Point & position, const VideoPath & video, bool playAudio)
  22. : VideoWidgetBase(position, video, playAudio, 1.0)
  23. {
  24. }
  25. VideoWidgetBase::VideoWidgetBase(const Point & position, const VideoPath & video, bool playAudio, float scaleFactor)
  26. : playAudio(playAudio), scaleFactor(scaleFactor)
  27. {
  28. addUsedEvents(TIME);
  29. pos += position;
  30. playVideo(video);
  31. }
  32. VideoWidgetBase::~VideoWidgetBase() = default;
  33. void VideoWidgetBase::playVideo(const VideoPath & fileToPlay)
  34. {
  35. OBJECT_CONSTRUCTION;
  36. if(settings["general"]["enableSubtitle"].Bool())
  37. {
  38. JsonPath subTitlePath = fileToPlay.toType<EResType::JSON>();
  39. JsonPath subTitlePathVideoDir = subTitlePath.addPrefix("VIDEO/");
  40. if(CResourceHandler::get()->existsResource(subTitlePath))
  41. subTitleData = JsonNode(subTitlePath);
  42. else if(CResourceHandler::get()->existsResource(subTitlePathVideoDir))
  43. subTitleData = JsonNode(subTitlePathVideoDir);
  44. }
  45. float preScaleFactor = 1;
  46. VideoPath videoFile = fileToPlay;
  47. if(ENGINE->screenHandler().getScalingFactor() > 1)
  48. {
  49. std::vector<int> factorsToCheck = {ENGINE->screenHandler().getScalingFactor(), 4, 3, 2};
  50. for(auto factorToCheck : factorsToCheck)
  51. {
  52. std::string name = boost::algorithm::to_upper_copy(videoFile.getName());
  53. boost::replace_all(name, "VIDEO/", std::string("VIDEO") + std::to_string(factorToCheck) + std::string("X/"));
  54. auto p = VideoPath::builtin(name).addPrefix("VIDEO" + std::to_string(factorToCheck) + "X/");
  55. if(CResourceHandler::get()->existsResource(p))
  56. {
  57. preScaleFactor = 1.0 / static_cast<float>(factorToCheck);
  58. videoFile = p;
  59. break;
  60. }
  61. }
  62. }
  63. videoInstance = ENGINE->video().open(videoFile, scaleFactor * preScaleFactor);
  64. if (videoInstance)
  65. {
  66. pos.w = videoInstance->size().x;
  67. pos.h = videoInstance->size().y;
  68. if(!subTitleData.isNull())
  69. subTitle = std::make_unique<CMultiLineLabel>(Rect(0, (pos.h / 5) * 4, pos.w, pos.h / 5), EFonts::FONT_HIGH_SCORE, ETextAlignment::CENTER, Colors::WHITE);
  70. }
  71. if (playAudio)
  72. {
  73. loadAudio(fileToPlay);
  74. if (isActive())
  75. startAudio();
  76. }
  77. }
  78. void VideoWidgetBase::show(Canvas & to)
  79. {
  80. if(videoInstance)
  81. to.draw(*videoInstance, pos.topLeft());
  82. if(subTitle)
  83. subTitle->showAll(to);
  84. }
  85. void VideoWidgetBase::loadAudio(const VideoPath & fileToPlay)
  86. {
  87. if (!playAudio)
  88. return;
  89. audioData = ENGINE->video().getAudio(fileToPlay);
  90. }
  91. void VideoWidgetBase::startAudio()
  92. {
  93. if(audioData.first == nullptr)
  94. return;
  95. audioHandle = ENGINE->sound().playSound(audioData);
  96. if(audioHandle != -1)
  97. {
  98. ENGINE->sound().setCallback(
  99. audioHandle,
  100. [this]()
  101. {
  102. this->audioHandle = -1;
  103. }
  104. );
  105. }
  106. }
  107. void VideoWidgetBase::stopAudio()
  108. {
  109. if(audioHandle != -1)
  110. {
  111. ENGINE->sound().resetCallback(audioHandle);
  112. ENGINE->sound().stopSound(audioHandle);
  113. audioHandle = -1;
  114. }
  115. }
  116. std::string VideoWidgetBase::getSubTitleLine(double timestamp)
  117. {
  118. if(subTitleData.isNull())
  119. return {};
  120. for(auto & segment : subTitleData.Vector())
  121. if(timestamp > segment["timeStart"].Float() && timestamp < segment["timeEnd"].Float())
  122. return segment["text"].String();
  123. return {};
  124. }
  125. void VideoWidgetBase::activate()
  126. {
  127. CIntObject::activate();
  128. if(audioHandle != -1)
  129. ENGINE->sound().resumeSound(audioHandle);
  130. else
  131. startAudio();
  132. if(videoInstance)
  133. videoInstance->activate();
  134. }
  135. void VideoWidgetBase::deactivate()
  136. {
  137. CIntObject::deactivate();
  138. ENGINE->sound().pauseSound(audioHandle);
  139. if(videoInstance)
  140. videoInstance->deactivate();
  141. }
  142. void VideoWidgetBase::showAll(Canvas & to)
  143. {
  144. if(videoInstance)
  145. to.draw(*videoInstance, pos.topLeft());
  146. if(subTitle)
  147. subTitle->showAll(to);
  148. }
  149. void VideoWidgetBase::tick(uint32_t msPassed)
  150. {
  151. if(videoInstance)
  152. {
  153. videoInstance->tick(msPassed);
  154. if(!videoInstance->videoEnded() && subTitle)
  155. subTitle->setText(getSubTitleLine(videoInstance->timeStamp()));
  156. if(videoInstance->videoEnded())
  157. {
  158. videoInstance.reset();
  159. stopAudio();
  160. onPlaybackFinished();
  161. // WARNING: onPlaybackFinished call may destoy `this`. Make sure that this is the very last operation in this method!
  162. }
  163. }
  164. }
  165. VideoWidget::VideoWidget(const Point & position, const VideoPath & prologue, const VideoPath & looped, bool playAudio)
  166. : VideoWidgetBase(position, prologue, playAudio)
  167. , loopedVideo(looped)
  168. {
  169. }
  170. VideoWidget::VideoWidget(const Point & position, const VideoPath & looped, bool playAudio)
  171. : VideoWidgetBase(position, looped, playAudio)
  172. , loopedVideo(looped)
  173. {
  174. }
  175. void VideoWidget::onPlaybackFinished()
  176. {
  177. playVideo(loopedVideo);
  178. }
  179. VideoWidgetOnce::VideoWidgetOnce(const Point & position, const VideoPath & video, bool playAudio, IVideoHolder * owner)
  180. : VideoWidgetBase(position, video, playAudio)
  181. , owner(owner)
  182. {
  183. }
  184. VideoWidgetOnce::VideoWidgetOnce(const Point & position, const VideoPath & video, bool playAudio, float scaleFactor, IVideoHolder * owner)
  185. : VideoWidgetBase(position, video, playAudio, scaleFactor)
  186. , owner(owner)
  187. {
  188. }
  189. void VideoWidgetOnce::onPlaybackFinished()
  190. {
  191. owner->onVideoPlaybackFinished();
  192. }