Advertisement
Guest User

SixLoves_Responsive.js 0.1.0

a guest
Nov 2nd, 2015
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*jslint nomen:false*/
  2.  
  3. /*:
  4.  * @author David Wendt (fantranslation.org)
  5.  * @plugindesc Makes your game responsive. v0.1.0
  6.  *
  7.  * @param ArtScale
  8.  * @desc The intended scale of your art assets. (RPG Maker MV default: 1.5)
  9.  * @default 1.5
  10.  *
  11.  * @param MinWidth
  12.  * @desc The minimum width of the playfield, in virtual pixel units.
  13.  * @default 544
  14.  *
  15.  * @param MinHeight
  16.  * @desc The minimum height of the playfield, in virtual pixel units.
  17.  * @default 416
  18.  *
  19.  * @param MaxWidth
  20.  * @desc The maximum width of the playfield, in virtual pixel units.
  21.  * @default 816
  22.  *
  23.  * @param MaxHeight
  24.  * @desc The maximum height of the playfield, in virtual pixel units.
  25.  * @default 624
  26.  *
  27.  * @help
  28.  *
  29.  *      Adjusts your game's playfield to fit the screen viewport it has, such
  30.  * that the game in question has no letterboxing bars and the scale of rendered
  31.  * art assets corresponds to some physical quantity.
  32.  *
  33.  *      This plugin's main advantage is that you can design games with detailed
  34.  * art for high-density displays without forcing players on lower-density
  35.  * monitors to view excessively large sprites. Art is scaled up or down as
  36.  * needed such that the final result is at a visually appropriate size for any
  37.  * monitor.
  38.  *
  39.  *      You can also define a minimum size for your playfield. Viewports smaller
  40.  * than this will have their art assets shrunk further to ensure a minimum
  41.  * amount of virtual screen space is available to the user interface.
  42.  * Additionally, a maximum size can also be defined to prevent assets from
  43.  * becoming too small and the playfield too large. If these two parameters are
  44.  * in conflict, the maximum size will override the minimum size. (Such a
  45.  * situation may happen if the viewport is not tall enough for the minimum
  46.  * height, but wide enough to exceed maximum width.)
  47.  *
  48.  *      Maximum and minimum size defaults are set to the sizes of the default
  49.  * RPG Maker MV and VX Ace viewports, respectively. You may wish to adjust them
  50.  * based on your target platform. In that case, please keep in mind that the
  51.  * units for these parameters are specified in virtual units, so if you increase
  52.  * your ArtScale, the sizes specified here do not need to be increased.
  53.  *
  54.  *      Please note that this plugin does not currently adjust UI to account for
  55.  * a higher or lower ArtScale. You must first provide all other parts of RPG
  56.  * Maker high-resolution assets to increase the size of all content, and then
  57.  * use ArtScale to shrink the content of the game back down to the same apparant
  58.  * size.
  59.  *
  60.  *      This plugin creates a new method for Scenes and Windows called layout.
  61.  * This method is called to force the target object to adjust it's contents to
  62.  * fit a different screen size. Default implementations for the base game's
  63.  * scenes and windows will be created. Additional scenes and windows in other
  64.  * plugins or your own code should have layout methods created for them if the
  65.  * defaults are unsuitable. The default implementations will use existing layout
  66.  * properties where available. Developers of custom Window or Scene classes
  67.  * should take a look at what this plugin does to ensure nothing breaks when it
  68.  * is enabled.
  69.  */
  70.  
  71. (function (root) {
  72.     "use strict";
  73.    
  74.     var parameters = root.PluginManager.parameters('SixLoves_Responsive'),
  75.         artScale = Number(parameters.ArtScale || 1.5),
  76.         minWidth = Number(parameters.MinWidth || 544),
  77.         minHeight = Number(parameters.MinHeight || 416),
  78.         maxWidth = Number(parameters.MaxWidth || 816),
  79.         maxHeight = Number(parameters.MaxHeight || 624),
  80.        
  81.         /* Preceding implementations of patched code. */
  82.         _SceneManager_initGraphics = root.SceneManager.initGraphics,
  83.         _Graphics_centerElement = root.Graphics._centerElement;
  84.    
  85.     /* Code which resizes the Graphics viewport to match the screen, and scale
  86.      * art assets down to their appropriate physical size.
  87.      * (e.g. it allows high-DPI assets to be high-DPI when needed)
  88.      */
  89.     function adapt_to_viewport() {
  90.         var finalScale = artScale,
  91.             cssWidth = window.innerWidth,
  92.             cssHeight = window.innerHeight;
  93.        
  94.         if (cssWidth < minWidth || cssHeight < minHeight) {
  95.             finalScale = artScale / Math.min(cssWidth / minWidth,
  96.                                              cssHeight / minHeight);
  97.         }
  98.        
  99.         if (cssWidth > maxWidth || cssHeight > maxHeight) {
  100.             finalScale = artScale / Math.max(cssWidth / maxWidth,
  101.                                              cssHeight / maxHeight);
  102.         }
  103.        
  104.         console.log(window.innerWidth);
  105.         console.log(window.innerHeight);
  106.         console.log(artScale);
  107.         console.log(finalScale);
  108.        
  109.         root.Graphics.width = cssWidth * finalScale;
  110.         root.Graphics.height = cssHeight * finalScale;
  111.         root.Graphics.boxWidth = cssWidth * finalScale;
  112.         root.Graphics.boxHeight = cssHeight * finalScale;
  113.         root.Graphics.scale = 1 / finalScale;
  114.        
  115.         if (root.SceneManager._scene) {
  116.             root.SceneManager._scene.layout();
  117.         }
  118.     }
  119.    
  120.     /* Monkey-patch the Scene Manager to fill the screen.
  121.      */
  122.     root.SceneManager.initGraphics = function () {
  123.         _SceneManager_initGraphics.apply(this);
  124.        
  125.         adapt_to_viewport();
  126.     };
  127.    
  128.     window.addEventListener("resize", adapt_to_viewport);
  129.    
  130.     function layoutAll(children) {
  131.         var i;
  132.        
  133.         for (i = 0; i < children.length; i += 1) {
  134.             if (children[i].layout) {
  135.                 children[i].layout();
  136.             }
  137.         }
  138.     }
  139.    
  140.     /* == GENERIC IMPLEMENTATIONS == */
  141.    
  142.     /* Add the layout method to the Scene implementation.
  143.      */
  144.     root.Scene_Base.prototype.layout = function () {
  145.         var width = root.Graphics.boxWidth,
  146.             height = root.Graphics.boxHeight,
  147.             x = (root.Graphics.width - width) / 2,
  148.             y = (root.Graphics.height - height) / 2;
  149.        
  150.         if (!this._windowLayer) {
  151.             return this.createWindowLayer();
  152.         }
  153.        
  154.         this._windowLayer.move(x, y, width, height);
  155.         this.width = width;
  156.         this.height = height;
  157.        
  158.         layoutAll(this.children);
  159.     };
  160.    
  161.     /* Add the layout method to the WindowLayer implementation.
  162.      *
  163.      * Triggering layout on a WindowLayer just lays out all children.
  164.      */
  165.     root.WindowLayer.prototype.layout = function () {
  166.         layoutAll(this.children);
  167.     }
  168.    
  169.     /* Same for sprites, too.
  170.      */
  171.     root.Sprite.prototype.layout = function () {
  172.         layoutAll(this.children);
  173.     }
  174.    
  175.     /* Add the layout method to the Window implementation.
  176.      *
  177.      * This method relies on the existence of methods which comprise the way
  178.      * that Window layout is usually done for non-fixed windows:
  179.      *
  180.      *  - windowWidth: Returns the desired width of the window.
  181.      *  - windowHeight: Returns the desired height of the window.
  182.      *  - updatePlacement: Sets the window position.
  183.      *
  184.      * If your Window class lays itself out differently, but is not laid out by
  185.      * the parent scene, please override .layout with an appropriate
  186.      * implementation.
  187.      */
  188.     root.Window.prototype.layout = function () {
  189.         if (this.windowWidth && this.windowHeight) {
  190.             this.width = this.windowWidth();
  191.             this.height = this.windowHeight();
  192.         }
  193.        
  194.         if (this.updatePlacement) {
  195.             this.updatePlacement();
  196.         }
  197.        
  198.         layoutAll(this.children);
  199.     }
  200.    
  201.     /* Ensure screen-filling sprites actually, y'know, fill the screen.
  202.      */
  203.     root.ScreenSprite.prototype.layout = function () {
  204.         this.scale.x = Graphics.width;
  205.         this.scale.y = Graphics.height;
  206.        
  207.         layoutAll(this.children);
  208.     }
  209.    
  210.     /* == SPECIAL-PURPOSE IMPLEMENTATIONS: TITLE SCREEN == */
  211.    
  212.     /* Recenter the background when a layout is triggered.  
  213.      */
  214.     root.Scene_Title.prototype.layout = function () {
  215.         this.centerSprite(this._backSprite1);
  216.         this.centerSprite(this._backSprite2);
  217.        
  218.         this.removeChild(this._gameTitleSprite);
  219.         this.createForeground();
  220.        
  221.         root.Scene_Base.prototype.layout.call(this);
  222.     }
  223.    
  224.     root.Scene_Title.prototype.centerSprite = (function (old_impl) {
  225.         return function (sprite) {
  226.             var fillingScale;
  227.            
  228.             console.log(sprite.bitmap.width);
  229.             console.log(sprite.bitmap.height);
  230.            
  231.             //Awful hack because I can't figure out how to get Sprite/Bitmap to spit out
  232.             //their unscaled sizes
  233.             if (sprite.SCENE_TITLE__firstW === undefined) {
  234.                 sprite.SCENE_TITLE__firstW = sprite.bitmap.width;
  235.             }
  236.  
  237.             if (sprite.SCENE_TITLE__firstH === undefined) {
  238.                 sprite.SCENE_TITLE__firstH = sprite.bitmap.height;
  239.             }
  240.            
  241.             fillingScale = Math.max(root.Graphics.width / sprite.SCENE_TITLE__firstW,
  242.                                     root.Graphics.height / sprite.SCENE_TITLE__firstH);
  243.            
  244.             sprite.scale.x = fillingScale;
  245.             sprite.scale.y = fillingScale;
  246.            
  247.             old_impl(sprite);
  248.         };
  249.     }(root.Scene_Title.prototype.centerSprite));
  250.    
  251.     /* == SPECIAL-PURPOSE IMPLEMENTATIONS: MENU SCREEN == */
  252.    
  253.     /* Reposition the Gold window when needed  
  254.      */
  255.     root.Scene_Menu.prototype.layout = function () {
  256.         if (this._goldWindow !== undefined) {
  257.             this._goldWindow.y = root.Graphics.boxHeight - this._goldWindow.height;
  258.         }
  259.        
  260.         root.Scene_MenuBase.prototype.layout.call(this);
  261.     }
  262.    
  263.     /* == SPECIAL-PURPOSE IMPLEMENTATIONS: MAP SCREEN == */
  264.    
  265.     /* Resize objects managed by the spriteset management code.
  266.      */
  267.     root.Spriteset_Base.prototype.layout = function () {
  268.         var width = Graphics.boxWidth,
  269.             height = Graphics.boxHeight,
  270.             x = (Graphics.width - width) / 2,
  271.             y = (Graphics.height - height) / 2;
  272.        
  273.         this.setFrame(0, 0, Graphics.width, Graphics.height);
  274.         this._pictureContainer.setFrame(x, y, width, height);
  275.         this._baseSprite.setFrame(0, 0, width, height);
  276.        
  277.         root.Sprite.prototype.layout.call(this);
  278.     }
  279.    
  280.     /* Recreate the entire Map scene
  281.      *
  282.      * We do this instead of trying to properly size it's children because that
  283.      * approach caused... problems. The Spritesheet or Tilemap always seemed to
  284.      * be getting clipped on the edge you expanded from, for some reason.
  285.      */
  286.     root.Scene_Map.prototype.layout = function () {
  287.         this.removeChildren();
  288.         this.createDisplayObjects();
  289.        
  290.         root.Scene_Base.prototype.layout.call(this);
  291.     }
  292. }(this));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement