VideoWidget.cpp 2.8 KB

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