VideoWidget.cpp 5.3 KB

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