baflink

jsRPGFaceFlip.js

Jul 7th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // jsRPGFaceFlip.js
  3. //=============================================================================
  4.  
  5. /*:
  6.  * @plugindesc Flip faces on the X axis in message boxes
  7.  * @author Brent Farris
  8.  *
  9.  * @help
  10.  * This will flip any face within a message box along the X axis.
  11.  * If the face is looking left they will look right.
  12.  * If it is looking right they will be look left
  13.  *
  14.  */
  15.  
  16. // Overriding these two internal functions to force faces to flip
  17. (function() {
  18.     Window_Base.prototype.drawFace = function(faceName, faceIndex, x, y, width, height) {
  19.         width = width || Window_Base._faceWidth;
  20.         height = height || Window_Base._faceHeight;
  21.         var bitmap = ImageManager.loadFace(faceName);
  22.         var pw = Window_Base._faceWidth;
  23.         var ph = Window_Base._faceHeight;
  24.         var sw = Math.min(width, pw);
  25.         var sh = Math.min(height, ph);
  26.         var dx = Math.floor(x + Math.max(width - pw, 0) / 2);
  27.         var dy = Math.floor(y + Math.max(height - ph, 0) / 2);
  28.         var sx = faceIndex % 4 * pw + (pw - sw) / 2;
  29.         var sy = Math.floor(faceIndex / 4) * ph + (ph - sh) / 2;
  30.         this.contents.blt(bitmap, sx, sy, sw, sh, dx, dy, null, null, true);
  31.     };
  32.  
  33.     /**
  34.      * Performs a block transfer.
  35.      *
  36.      * @method blt
  37.      * @param {Bitmap} source The bitmap to draw
  38.      * @param {Number} sx The x coordinate in the source
  39.      * @param {Number} sy The y coordinate in the source
  40.      * @param {Number} sw The width of the source image
  41.      * @param {Number} sh The height of the source image
  42.      * @param {Number} dx The x coordinate in the destination
  43.      * @param {Number} dy The y coordinate in the destination
  44.      * @param {Number} [dw=sw] The width to draw the image in the destination
  45.      * @param {Number} [dh=sh] The height to draw the image in the destination
  46.      * @param {Boolean} [flipX=false] Determines if the render should flip on the x axis
  47.      * @param {Boolean} [flipY=false] Determines if the render should flip on the x axis
  48.      */
  49.     Bitmap.prototype.blt = function(source, sx, sy, sw, sh, dx, dy, dw, dh, flipX, flipY) {
  50.         dw = dw || sw;
  51.         dh = dh || sh;
  52.  
  53.         if (flipX || flipY) {
  54.             this._context.save();
  55.  
  56.             if (flipX) {
  57.                 this._context.scale(-1, 1);
  58.                 dx = dx - (dw * 0.5) - (sw * 0.5);
  59.             }
  60.  
  61.             if (flipY) {
  62.                 this._context.scale(1, -1);
  63.                 dy = dy - (dh * 0.5) - (sh * 0.5);
  64.             }
  65.         }
  66.  
  67.         if (sx >= 0 && sy >= 0 && sw > 0 && sh > 0 && dw > 0 && dh > 0 &&
  68.             sx + sw <= source.width && sy + sh <= source.height) {
  69.             this._context.globalCompositeOperation = 'source-over';
  70.             this._context.drawImage(source._canvas, sx, sy, sw, sh, dx, dy, dw, dh);
  71.             this._setDirty();
  72.         }
  73.  
  74.         if (flipX || flipY) {
  75.             this._context.restore();
  76.         }
  77.     };
  78. })();
Advertisement
Add Comment
Please, Sign In to add comment