Iavra

Iavra Video Title

Dec 10th, 2015 (edited)
1,766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc v1.01 Allows you to play a video on the title screen. - Updated for MV 1.3
  3.  * <Iavra Video Title>
  4.  * @author Iavra
  5.  *
  6.  * @param Video
  7.  * @desc Name of the video to be used.
  8.  * @default
  9.  * @require 1
  10.  * @dir movies/
  11.  * @type file
  12.  *
  13.  * @help
  14.  * Specify the name of the video file in the plugin parameter "Video". The video has to be placed in the
  15.  * "movies" folder of the game and needs to exist as both ".mp4" and ".webm" files, because one of these
  16.  * will be picked, depending on the environment.
  17.  *
  18.  * The video will be played behind the title picture, so either pick "None" as the background image or
  19.  * create a transparent picture to use. You can even use a semi-transparent picture, so the video will be
  20.  * visible behind it.
  21.  *
  22.  * The video will automatically be resized to fit the with and height of the window, while retaining the
  23.  * correct scale and will be centered, if it doesn't fit exactly.
  24.  */
  25.  
  26. (function($, undefined) {
  27.     "use strict";
  28.  
  29.     /**
  30.      * Basic helper function to extend objects. Mainly used for inheritance and other prototype-related operations.
  31.      */
  32.     $._extend || ($._extend = function(b, e) { for(var k in e) { b[k] = e[k]; } return b; });
  33.  
  34.     /**
  35.      * Load plugin parameters.
  36.      */
  37.     var _params = $plugins.filter(function(p) { return p.description.contains('<Iavra Video Title>'); })[0].parameters;
  38.     var _param_video = _params['Video'];
  39.  
  40.     /**
  41.      * Holds the HTML video element used to start/pause/rewind the loaded video.
  42.      */
  43.     var _video;
  44.  
  45.     /**
  46.      * Holds the actual sprite, that gets added to Scene_Title.
  47.      */
  48.     var _videoSprite;
  49.  
  50.     /**
  51.      * Gets set to true, once the video has been loaded and causes Scene_Title to wait, beforehand.
  52.      */
  53.     var _ready;
  54.  
  55.     /**
  56.      * Create the video element, set it to auto-loop and creates the Sprite holding it. We also register a
  57.      * callback to be executed, once the video has finished loading, so we can resize and center the sprite.
  58.      */
  59.     var _loadVideo = function() {
  60.         if(!!_videoSprite) { return; }
  61.         _video = document.createElement('video');
  62.         _video.src = _getFilePath();
  63.         _video.loop = 'loop';
  64.         _videoSprite = new PIXI.Sprite(PIXI.Texture.fromVideo(_video));
  65.         _video.onloadeddata = _onLoad;
  66.     };
  67.  
  68.     /**
  69.      * Gets called, once the video has finished loading and scales the sprite. It also gets centered.
  70.      */
  71.     var _onLoad = function() {
  72.         _ready = true;
  73.         var scale = Math.min(Graphics._width / _video.videoWidth, Graphics._height / _video.videoHeight);
  74.         _videoSprite.width = _video.videoWidth * scale;
  75.         _videoSprite.height = _video.videoHeight * scale;
  76.         _videoSprite.x = (Graphics.width - _videoSprite.width) / 2;
  77.         _videoSprite.y = (Graphics.height - _videoSprite.height) / 2;
  78.     };
  79.  
  80.     /**
  81.      * Returns the path to the video file, depending on the current environment.
  82.      */
  83.     var _getFilePath = function() {
  84.         return 'movies/' + _param_video + Game_Interpreter.prototype.videoFileExt();
  85.     };
  86.  
  87.     //=============================================================================
  88.     // Scene_Title
  89.     //=============================================================================
  90.  
  91.     var _sceneTitle_create = Scene_Title.prototype.create;
  92.     var _sceneTitle_isReady = Scene_Title.prototype.isReady;
  93.     var _sceneTitle_start = Scene_Title.prototype.start;
  94.     var _sceneTitle_stop = Scene_Title.prototype.stop;
  95.     var _sceneTitle_createBackground = Scene_Title.prototype.createBackground;
  96.  
  97.     $._extend(Scene_Title.prototype, {
  98.  
  99.         /**
  100.          * On create, also create our video element. We must do this here, because otherwise we wouldn't
  101.          * be able to determine our current environment, which defines the file extension we have to load.
  102.          */
  103.         create: function() {
  104.             _loadVideo();
  105.             _sceneTitle_create.call(this);
  106.         },
  107.  
  108.         /**
  109.          * We wait, until the video has finished loading. This must only be done once, since the video is
  110.          * cached in memory (better don't use big videos ^^).
  111.          */
  112.         isReady: function() {
  113.             return _ready && _sceneTitle_isReady.call(this);
  114.         },
  115.  
  116.         /**
  117.          * On start, rewind the video to position 0 and start playing. It will automatically loop, once it
  118.          * has finished.
  119.          */
  120.         start: function() {
  121.             _sceneTitle_start.call(this);
  122.             _video.currentTime = 0;
  123.             _video.play();
  124.         },
  125.  
  126.         /**
  127.          * On stop, pause the video, because otherwise it would continue playing, even through other scenes.
  128.          */
  129.         stop: function() {
  130.             _video.pause();
  131.             _sceneTitle_stop.call(this);
  132.         },
  133.  
  134.         /**
  135.          * Position the video sprite behind the background, so it's possible to put (semi)transparent images
  136.          * above it.
  137.          */
  138.         createBackground: function() {
  139.             this.addChild(_videoSprite);
  140.             _sceneTitle_createBackground.call(this);
  141.         }
  142.  
  143.     });
  144.  
  145. })(this.IAVRA || (this.IAVRA = {}));
Add Comment
Please, Sign In to add comment