Iavra

Iavra Loading Image

May 2nd, 2016 (edited)
2,442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc v0.03 Allows the usage of an animated imagestrip for the loading screen, instead of a static image.
  3.  * <Iavra Loading>
  4.  * @author Iavra
  5.  *
  6.  * @param Image
  7.  * @desc Path to the image file to be used, relative to the project root.
  8.  * @default img/system/Loading.png
  9.  *
  10.  * @param Background
  11.  * @desc Optional background image to be displayed behind the loading image.
  12.  * @default
  13.  *
  14.  * @param Frames
  15.  * @desc Number of frames to be used. If this number is greater than 1, the image is split vertically.
  16.  * @default 1
  17.  *
  18.  * @param Interval
  19.  * @desc Delay (in frames), before switching to the next image frame. Set to 0 to disable interval behaviour.
  20.  * @default 60
  21.  *
  22.  * @param X
  23.  * @desc The loading image's x coordinate. Negative values start on the right. Set to a non-integer to center.
  24.  * @default center
  25.  *
  26.  * @param Y
  27.  * @desc The loading image's y coordinate. Negative values start at the bottom. Set to a non-integer to center.
  28.  * @default center
  29.  *
  30.  * @help
  31.  * Use the plugin parameter "Image" to specify the image file to be used. It may be treated as a horizontal imagestrip
  32.  * by setting the "Frames" parameter to the number of frames.
  33.  *
  34.  * The optional "Background" parameter can be used to display another, static image behind the (possibly animated)
  35.  * loading image. It will automatically be scaled to fit the screen.
  36.  *
  37.  * The "Interval" parameter can be used to set the number of frames to wait, before switching to the next image frame.
  38.  *
  39.  * Use the "X" and "Y" parameter to specify the image's position, anchored on its upper left corner. Negative values
  40.  * will start the right/bottom side of the screen, instead of the left/top side. A value of 0 or -0 can be used to snap
  41.  * the image to the corresponding side. Giving a non-integer value will center the image.
  42.  */
  43.  
  44. (function($, undefined) {
  45.     "use strict";
  46.  
  47.     var _params = $plugins.filter(function(p) { return p.description.contains('<Iavra Loading>'); })[0].parameters;
  48.     var _param_image = _params['Image'];
  49.     var _param_background = _params['Background'];
  50.     var _param_frames = Math.max(1, _params['Frames']|0);
  51.     var _param_interval = Math.max(0, _params['Interval']|0);
  52.  
  53.     /**
  54.      * Basic helper function to extend objects. Mainly used for inheritance and other prototype-related operations.
  55.      */
  56.     $._extend || ($._extend = function(b, e) { for(var k in e) { b[k] = e[k]; } return b; });
  57.  
  58.     /**
  59.      * Some variables used to cache stuff and calculate the current image state.
  60.      */
  61.     var _x = _params['X'], _y = _params['Y'], _cx = _x != (_x|0), _cy = _y != (_y|0), _w, _h, _t = 0, _r = false;
  62.  
  63.     /**
  64.      * Loads the background image, if any.
  65.      */
  66.     var loadBackground = function(src) {
  67.         if(!src) { return; }
  68.         Graphics._loadingBack = new Image();
  69.         Graphics._loadingBack.src = src;
  70.     };
  71.  
  72.     /**
  73.      * Loads the loading image, if any, splits it into frames and caches some values to be used during rendering.
  74.      */
  75.     var loadImage = function(src) {
  76.         if(!src) { return; }
  77.         var img = new Image();
  78.         img.src = src;
  79.         img.onload = function(img) { _w = img.width / _param_frames, _h = img.height, _r = true; }.bind(null, img);
  80.         img.onerror = function() { throw new Error("Error loading '" + src + "'."); };
  81.         Graphics._loadingImage = img;
  82.     };
  83.  
  84.     /**
  85.      * Draws the background image, if any.
  86.      */
  87.     var drawBackground = function(context, img) {
  88.         if(img) { context.drawImage(img, 0, 0, Graphics.width, Graphics.height); }
  89.     };
  90.  
  91.     /**
  92.      * Calculates the image offset and current frame and draws it onto the screen.
  93.      */
  94.     var drawImage = function(context, img) {
  95.         if(img && _r) {
  96.             var dx = _cx ? (Graphics.width - _w) / 2 : (1/_x < 0 ? Graphics.width - _w -_x : _x);
  97.             var dy = _cy ? (Graphics.height - _h) / 2 : (1/_y < 0 ? Graphics.height - _h - _y : _y);
  98.             var f = _param_interval ? (_t++ / _param_interval % _param_frames)|0 : 0;
  99.             context.drawImage(img, _w * f, 0, _w, _h, dx, dy, _w, _h);
  100.         }
  101.     };
  102.  
  103.     //=============================================================================
  104.     // Graphics
  105.     //=============================================================================
  106.  
  107.     var alias_graphics_startLoading = Graphics.startLoading;
  108.  
  109.     $._extend(Graphics, {
  110.  
  111.         /**
  112.          * When starting to load, also reset the timer, which is used to determine the currently shown image frame.
  113.          */
  114.         startLoading: function() {
  115.             alias_graphics_startLoading.call(this);
  116.             _t = 0;
  117.         },
  118.  
  119.         /**
  120.          * Ignore the default parameter passed to this function and load both of our images.
  121.          */
  122.         setLoadingImage: function() {
  123.             loadBackground(_param_background);
  124.             loadImage(_param_image);
  125.         },
  126.  
  127.         /**
  128.          * Delegate to our drawing functions to update the loading screen.
  129.          */
  130.         _paintUpperCanvas: function() {
  131.             this._clearUpperCanvas();
  132.             if(this._loadingCount >= 20) {
  133.                 var context = this._upperCanvas.getContext('2d');
  134.                 context.save();
  135.                 context.globalAlpha = ((this._loadingCount - 20) / 30).clamp(0, 1);
  136.                 drawBackground(context, this._loadingBack);
  137.                 drawImage(context, this._loadingImage);
  138.                 context.restore();
  139.             }
  140.         }
  141.  
  142.     });
  143.  
  144. })(this.IAVRA || (this.IAVRA = {}));
Add Comment
Please, Sign In to add comment