CPrologEpilogVideo.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * CPrologEpilogVideo.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 "CPrologEpilogVideo.h"
  12. #include "CMainMenu.h"
  13. #include "../media/IMusicPlayer.h"
  14. #include "../media/ISoundPlayer.h"
  15. #include "../GameEngine.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../widgets/TextControls.h"
  18. #include "../widgets/VideoWidget.h"
  19. #include "../widgets/Images.h"
  20. #include "../render/Canvas.h"
  21. #include "../lib/CConfigHandler.h"
  22. CPrologEpilogVideo::CPrologEpilogVideo(CampaignScenarioPrologEpilog _spe, std::function<void()> callback)
  23. : CWindowObject(BORDERED), spe(_spe), positionCounter(0), voiceSoundHandle(-1), videoSoundHandle(-1), exitCb(callback), elapsedTimeMilliseconds(0)
  24. {
  25. OBJECT_CONSTRUCTION;
  26. addUsedEvents(LCLICK | TIME);
  27. pos = center(Rect(0, 0, 800, 600));
  28. const auto& bgConfig = CMainMenuConfig::get().getConfig()["backgroundAround"];
  29. backgroundAroundMenu = std::make_shared<CFilledTexture>(bgConfig.isString() ?ImagePath::fromJson(bgConfig) : ImagePath::builtin("DIBOXBCK"), Rect(-pos.x, -pos.y, ENGINE->screenDimensions().x, ENGINE->screenDimensions().y));
  30. if (!spe.prologVideo.second.empty())
  31. videoPlayer = std::make_shared<VideoWidget>(Point(0, 0), spe.prologVideo.first, spe.prologVideo.second, true);
  32. else
  33. videoPlayer = std::make_shared<VideoWidget>(Point(0, 0), spe.prologVideo.first, true);
  34. //some videos are 800x600 in size while some are 800x400
  35. if (videoPlayer->pos.h == 400)
  36. videoPlayer->moveBy(Point(0, 100));
  37. videoPlayer->setPlaybackFinishedCallback([this]() {
  38. videoFinishedCounter++;
  39. if((!spe.prologVideo.second.empty() && videoFinishedCounter < 2)) // play looped video at least once
  40. return;
  41. if(!videoFinished)
  42. elapsedTimeMilliseconds = 0;
  43. videoFinished = true;
  44. });
  45. ENGINE->music().setVolume(ENGINE->music().getVolume() / 2); // background volume is too loud by default
  46. ENGINE->music().playMusic(spe.prologMusic, true, true);
  47. voiceDurationMilliseconds = ENGINE->sound().getSoundDurationMilliseconds(spe.prologVoice);
  48. voiceSoundHandle = ENGINE->sound().playSound(spe.prologVoice);
  49. auto onVoiceStop = [this]()
  50. {
  51. voiceStopped = true;
  52. elapsedTimeMilliseconds = 0;
  53. };
  54. ENGINE->sound().setCallback(voiceSoundHandle, onVoiceStop);
  55. if(!settings["general"]["enableSubtitle"].Bool())
  56. return;
  57. text = std::make_shared<CMultiLineLabel>(Rect(100, 500, 600, 100), EFonts::FONT_BIG, ETextAlignment::CENTER, Colors::METALLIC_GOLD, spe.prologText.toString());
  58. if(text->getLines().size() == 3)
  59. text->scrollTextTo(-25); // beginning of text in the vertical middle of black area
  60. else if(text->getLines().size() > 3)
  61. text->scrollTextTo(-50); // beginning of text in the vertical middle of black area
  62. }
  63. void CPrologEpilogVideo::tick(uint32_t msPassed)
  64. {
  65. elapsedTimeMilliseconds += msPassed;
  66. const uint32_t speed = (!text || voiceDurationMilliseconds == 0) ? 150 : (voiceDurationMilliseconds / (text->textSize.y));
  67. if(text && elapsedTimeMilliseconds > speed && text->textSize.y - 50 > positionCounter)
  68. {
  69. text->scrollTextBy(1);
  70. elapsedTimeMilliseconds -= speed;
  71. ++positionCounter;
  72. }
  73. else if(elapsedTimeMilliseconds > (voiceDurationMilliseconds == 0 ? 8000 : 3000) && voiceStopped && videoFinished) // pause after completed scrolling (longer for intros missing voice)
  74. clickPressed(ENGINE->getCursorPosition());
  75. }
  76. void CPrologEpilogVideo::show(Canvas & to)
  77. {
  78. to.drawColor(pos, Colors::BLACK);
  79. videoPlayer->show(to);
  80. if(text)
  81. text->showAll(to); // blit text over video, if needed
  82. }
  83. void CPrologEpilogVideo::exit()
  84. {
  85. ENGINE->music().setVolume(ENGINE->music().getVolume() * 2); // restore background volume
  86. close();
  87. ENGINE->sound().resetCallback(voiceSoundHandle); // reset callback to avoid memory corruption since 'this' will be destroyed
  88. ENGINE->sound().stopSound(voiceSoundHandle);
  89. ENGINE->sound().stopSound(videoSoundHandle);
  90. if(exitCb)
  91. exitCb();
  92. }
  93. void CPrologEpilogVideo::clickPressed(const Point & cursorPosition)
  94. {
  95. exit();
  96. }
  97. void CPrologEpilogVideo::keyPressed(EShortcut key)
  98. {
  99. if(key == EShortcut::GLOBAL_RETURN)
  100. exit();
  101. }