CPrologEpilogVideo.cpp 3.7 KB

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