VideoWidget.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "../CGameInfo.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../media/ISoundPlayer.h"
  15. #include "../media/IVideoPlayer.h"
  16. #include "../render/Canvas.h"
  17. VideoWidgetBase::VideoWidgetBase(const Point & position, const VideoPath & video, bool playAudio)
  18. : VideoWidgetBase(position, video, playAudio, 1.0)
  19. {
  20. }
  21. VideoWidgetBase::VideoWidgetBase(const Point & position, const VideoPath & video, bool playAudio, float scaleFactor)
  22. : playAudio(playAudio), scaleFactor(scaleFactor)
  23. {
  24. addUsedEvents(TIME);
  25. pos += position;
  26. playVideo(video);
  27. }
  28. VideoWidgetBase::~VideoWidgetBase() = default;
  29. void VideoWidgetBase::playVideo(const VideoPath & fileToPlay)
  30. {
  31. videoInstance = CCS->videoh->open(fileToPlay, scaleFactor);
  32. if (videoInstance)
  33. {
  34. pos.w = videoInstance->size().x;
  35. pos.h = videoInstance->size().y;
  36. }
  37. if (playAudio)
  38. {
  39. loadAudio(fileToPlay);
  40. if (isActive())
  41. startAudio();
  42. }
  43. }
  44. void VideoWidgetBase::show(Canvas & to)
  45. {
  46. if(videoInstance)
  47. videoInstance->show(pos.topLeft(), to);
  48. }
  49. void VideoWidgetBase::loadAudio(const VideoPath & fileToPlay)
  50. {
  51. if (!playAudio)
  52. return;
  53. audioData = CCS->videoh->getAudio(fileToPlay);
  54. }
  55. void VideoWidgetBase::startAudio()
  56. {
  57. if(audioData.first == nullptr)
  58. return;
  59. audioHandle = CCS->soundh->playSound(audioData);
  60. if(audioHandle != -1)
  61. {
  62. CCS->soundh->setCallback(
  63. audioHandle,
  64. [this]()
  65. {
  66. this->audioHandle = -1;
  67. }
  68. );
  69. }
  70. }
  71. void VideoWidgetBase::stopAudio()
  72. {
  73. if(audioHandle != -1)
  74. {
  75. CCS->soundh->resetCallback(audioHandle);
  76. CCS->soundh->stopSound(audioHandle);
  77. audioHandle = -1;
  78. }
  79. }
  80. void VideoWidgetBase::activate()
  81. {
  82. CIntObject::activate();
  83. startAudio();
  84. }
  85. void VideoWidgetBase::deactivate()
  86. {
  87. CIntObject::deactivate();
  88. stopAudio();
  89. }
  90. void VideoWidgetBase::showAll(Canvas & to)
  91. {
  92. if(videoInstance)
  93. videoInstance->show(pos.topLeft(), to);
  94. }
  95. void VideoWidgetBase::tick(uint32_t msPassed)
  96. {
  97. if(videoInstance)
  98. {
  99. videoInstance->tick(msPassed);
  100. if(videoInstance->videoEnded())
  101. {
  102. videoInstance.reset();
  103. stopAudio();
  104. onPlaybackFinished();
  105. }
  106. }
  107. }
  108. VideoWidget::VideoWidget(const Point & position, const VideoPath & prologue, const VideoPath & looped, bool playAudio)
  109. : VideoWidgetBase(position, prologue, playAudio)
  110. , loopedVideo(looped)
  111. {
  112. }
  113. VideoWidget::VideoWidget(const Point & position, const VideoPath & looped, bool playAudio)
  114. : VideoWidgetBase(position, looped, playAudio)
  115. , loopedVideo(looped)
  116. {
  117. }
  118. void VideoWidget::onPlaybackFinished()
  119. {
  120. playVideo(loopedVideo);
  121. }
  122. VideoWidgetOnce::VideoWidgetOnce(const Point & position, const VideoPath & video, bool playAudio, const std::function<void()> & callback)
  123. : VideoWidgetBase(position, video, playAudio)
  124. , callback(callback)
  125. {
  126. }
  127. VideoWidgetOnce::VideoWidgetOnce(const Point & position, const VideoPath & video, bool playAudio, float scaleFactor, const std::function<void()> & callback)
  128. : VideoWidgetBase(position, video, playAudio, scaleFactor)
  129. , callback(callback)
  130. {
  131. }
  132. void VideoWidgetOnce::onPlaybackFinished()
  133. {
  134. callback();
  135. }