CPrologEpilogVideo.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. //TODO: remove hardcoded paths. Some of campaigns video actually consist from 2 parts
  29. // however, currently our campaigns format expects only a single video file
  30. static const std::map<VideoPath, VideoPath> pairedVideoFiles = {
  31. { VideoPath::builtin("EVIL2AP1"), VideoPath::builtin("EVIL2AP2") },
  32. { VideoPath::builtin("H3ABdb4"), VideoPath::builtin("H3ABdb4b") },
  33. { VideoPath::builtin("H3x2_RNe1"), VideoPath::builtin("H3x2_RNe2") },
  34. };
  35. if (pairedVideoFiles.count(spe.prologVideo))
  36. videoPlayer = std::make_shared<VideoWidget>(Point(0, 0), spe.prologVideo, pairedVideoFiles.at(spe.prologVideo), true);
  37. else
  38. videoPlayer = std::make_shared<VideoWidget>(Point(0, 0), spe.prologVideo, true);
  39. //some videos are 800x600 in size while some are 800x400
  40. if (videoPlayer->pos.h == 400)
  41. videoPlayer->moveBy(Point(0, 100));
  42. ENGINE->music().setVolume(ENGINE->music().getVolume() / 2); // background volume is too loud by default
  43. ENGINE->music().playMusic(spe.prologMusic, true, true);
  44. voiceDurationMilliseconds = ENGINE->sound().getSoundDurationMilliseconds(spe.prologVoice);
  45. voiceSoundHandle = ENGINE->sound().playSound(spe.prologVoice);
  46. auto onVoiceStop = [this]()
  47. {
  48. voiceStopped = true;
  49. elapsedTimeMilliseconds = 0;
  50. };
  51. ENGINE->sound().setCallback(voiceSoundHandle, onVoiceStop);
  52. if(!settings["general"]["enableSubtitle"].Bool())
  53. return;
  54. text = std::make_shared<CMultiLineLabel>(Rect(100, 500, 600, 100), EFonts::FONT_BIG, ETextAlignment::CENTER, Colors::METALLIC_GOLD, spe.prologText.toString());
  55. if(text->getLines().size() == 3)
  56. text->scrollTextTo(-25); // beginning of text in the vertical middle of black area
  57. else if(text->getLines().size() > 3)
  58. text->scrollTextTo(-50); // beginning of text in the vertical middle of black area
  59. }
  60. void CPrologEpilogVideo::tick(uint32_t msPassed)
  61. {
  62. elapsedTimeMilliseconds += msPassed;
  63. const uint32_t speed = (!text || voiceDurationMilliseconds == 0) ? 150 : (voiceDurationMilliseconds / (text->textSize.y));
  64. if(text && elapsedTimeMilliseconds > speed && text->textSize.y - 50 > positionCounter)
  65. {
  66. text->scrollTextBy(1);
  67. elapsedTimeMilliseconds -= speed;
  68. ++positionCounter;
  69. }
  70. else if(elapsedTimeMilliseconds > (voiceDurationMilliseconds == 0 ? 8000 : 3000) && voiceStopped) // pause after completed scrolling (longer for intros missing voice)
  71. clickPressed(ENGINE->getCursorPosition());
  72. }
  73. void CPrologEpilogVideo::show(Canvas & to)
  74. {
  75. to.drawColor(pos, Colors::BLACK);
  76. videoPlayer->show(to);
  77. if(text)
  78. text->showAll(to); // blit text over video, if needed
  79. }
  80. void CPrologEpilogVideo::exit()
  81. {
  82. ENGINE->music().setVolume(ENGINE->music().getVolume() * 2); // restore background volume
  83. close();
  84. ENGINE->sound().resetCallback(voiceSoundHandle); // reset callback to avoid memory corruption since 'this' will be destroyed
  85. ENGINE->sound().stopSound(voiceSoundHandle);
  86. ENGINE->sound().stopSound(videoSoundHandle);
  87. if(exitCb)
  88. exitCb();
  89. }
  90. void CPrologEpilogVideo::clickPressed(const Point & cursorPosition)
  91. {
  92. exit();
  93. }
  94. void CPrologEpilogVideo::keyPressed(EShortcut key)
  95. {
  96. if(key == EShortcut::GLOBAL_RETURN)
  97. exit();
  98. }