Advertisement
Kakakadafi

SRD_PictureChoices

Sep 7th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc Allows players to have the ability to select from a collection of pictures.
  3.  * @author SumRndmDde
  4.  *
  5.  * @param Min Scale
  6.  * @desc The scale of the picture when it is not selected.
  7.  * @default 1
  8.  *
  9.  * @param Max Scale
  10.  * @desc The scale of the picture when it is selected.
  11.  * @default 1.5
  12.  *
  13.  * @param Scaling Speed
  14.  * @desc The speed in which the pictures zoom when selecting them.
  15.  * @default 0.04
  16.  *
  17.  * @param Confirm Speed
  18.  * @desc The speed of the animation of a picture when it is chosen.
  19.  * @default 0.2
  20.  *
  21.  * @param Close Speed
  22.  * @desc The speed of the animation of a picture when it closes.
  23.  * @default 0.4
  24.  *
  25.  * @help
  26.  *
  27.  * Picture Choices
  28.  * Version 1.00
  29.  * SumRndmDde
  30.  *
  31.  *
  32.  * This is a plugin that allows players to have the ability to select from
  33.  * a collection of pictures.
  34.  *
  35.  *
  36.  * ==========================================================================
  37.  *  Where to Store the Pictures
  38.  * ==========================================================================
  39.  *
  40.  * All of the image files need to be stored in:
  41.  *
  42.  *   /img/SumRndmDde/choices/
  43.  *
  44.  *
  45.  * ==========================================================================
  46.  *  How to Set up Picture Choices
  47.  * ==========================================================================
  48.  *
  49.  * Within one of the slots for a Choice in a "Show Choices" event, use:
  50.  *
  51.  *   \picture[image-name]
  52.  *
  53.  * For example, if you had an image called "Happy.png", then you could do:
  54.  *
  55.  *   \picture[Happy]
  56.  *
  57.  *
  58.  * Using this, you can assign pictures to each choice.
  59.  * Keep in mind if even one choice is set up like this, the entire choice
  60.  * window will use the Picture Choices format.
  61.  * I would HIGHLY recommend making every choice have an picture assigned to it.
  62.  *
  63.  *
  64.  * ==========================================================================
  65.  *  Picture Choices Grid
  66.  * ==========================================================================
  67.  *
  68.  * By default, the grid used for the Picture Choices is based on the amount
  69.  * of choices that are used.
  70.  *
  71.  * For example:
  72.  *
  73.  *   2 Choices   -   2 Columns | 1 Rows
  74.  *   3 Choices   -   3 Columns | 1 Rows
  75.  *   4 Choices   -   2 Columns | 2 Rows
  76.  *   5 Choices   -   5 Columns | 1 Rows
  77.  *   6 Choices   -   3 Columns | 2 Rows
  78.  *
  79.  *
  80.  * However, say for example, you wish to set up a custom grid (like you may
  81.  * wish to have 3 Rows for 3 Choices), you can use the Plugin Command:
  82.  *
  83.  *   SetPictureChoicesGrid [cols] [rows]
  84.  *
  85.  * For example:
  86.  *
  87.  *   SetPictureChoicesGrid 1 3
  88.  *
  89.  *
  90.  * If you wish to set it back to automatic mode, use the Plugin Command:
  91.  *
  92.  *   SetPictureChoicesGrid auto
  93.  *
  94.  *
  95.  *   MovePictureChoice [choice] [x] [y]
  96.  *
  97.  * For example:
  98.  *
  99.  *   MovePictureChoice 1 100 150
  100.  *
  101.  *   This example of move Choice #1 to position (100, 150)
  102.  *
  103.  * ==========================================================================
  104.  *  End of Help File
  105.  * ==========================================================================
  106.  *
  107.  * Welcome to the bottom of the Help file.
  108.  *
  109.  *
  110.  * Thanks for reading!
  111.  * If you have questions, or if you enjoyed this Plugin, please check
  112.  * out my YouTube channel!
  113.  *
  114.  * https://www.youtube.com/c/SumRndmDde
  115.  *
  116.  *
  117.  * Until next time,
  118.  *   ~ SumRndmDde
  119.  *
  120.  */
  121.  
  122. var SRD = SRD || {};
  123. SRD.PictureChoices = SRD.PictureChoices || {};
  124.  
  125. var Imported = Imported || {};
  126. Imported["SumRndmDde Picture Choices"] = 1.00;
  127.  
  128. function Window_PictureChoiceList() {
  129.     this.initialize.apply(this, arguments);
  130. }
  131.  
  132. function Sprite_PictureChoice() {
  133.     this.initialize.apply(this, arguments);
  134. }
  135.  
  136. (function(_) {
  137.    
  138. "use strict";
  139.  
  140. var params = PluginManager.parameters('SRD_PictureChoices');
  141.  
  142. //-----------------------------------------------------------------------------
  143. // SRD.PictureChoices
  144. //-----------------------------------------------------------------------------
  145.  
  146. _.min = parseFloat(params['Min Scale']);
  147. _.max = parseFloat(params['Max Scale']);
  148. _.speed = parseFloat(params['Scaling Speed']);
  149. _.confirm = parseFloat(params['Confirm Speed']);
  150. _.close = parseFloat(params['Close Speed']);
  151.  
  152. _.loadPicture = function(filename, hue) {
  153.     return ImageManager.loadBitmap('img/SumRndmDde/choices/', filename, hue, true);
  154. };
  155.  
  156. _.matchesAny = function(name) {
  157.     return !!name.match(/\\picture\[(.*)\]/i);
  158. };
  159.  
  160. //-----------------------------------------------------------------------------
  161. // Game_Message
  162. //-----------------------------------------------------------------------------
  163.  
  164. var _Game_Message_clear = Game_Message.prototype.clear;
  165. Game_Message.prototype.clear = function() {
  166.     _Game_Message_clear.apply(this, arguments);
  167.     this.isPictureChoices = false;
  168.     this.pictureChoiceSize = [Graphics.boxWidth, Graphics.boxHeight];
  169.     this.pictureChoiceGrid = [1, 1];
  170.     this.customizedPictureChoiceGrid = false;
  171.     this.pictureChoicePosition = [null, null, null, null, null, null];
  172. };
  173.  
  174. var _Game_Message_setChoices = Game_Message.prototype.setChoices;
  175. Game_Message.prototype.setChoices = function(choices, defaultType, cancelType) {
  176.     this.pictureChoicesCheck(choices);
  177.     _Game_Message_setChoices.apply(this, arguments);
  178. };
  179.  
  180. Game_Message.prototype.pictureChoicesCheck = function(choices) {
  181.     this.isPictureChoices = false;
  182.     for(var i = 0; i < choices.length; i++) {
  183.         if(_.matchesAny(choices[i])) {
  184.             this.isPictureChoices = true;
  185.             break;
  186.         }
  187.     }
  188.     if(!this.customizedPictureChoiceGrid) {
  189.         var amount = choices.length;
  190.         if(amount === 1) this.pictureChoiceGrid = [1, 1];
  191.         else if(amount === 2) this.pictureChoiceGrid = [2, 1];
  192.         else if(amount === 3) this.pictureChoiceGrid = [3, 1];
  193.         else if(amount === 4) this.pictureChoiceGrid = [2, 2];
  194.         else if(amount === 5) this.pictureChoiceGrid = [5, 1];
  195.         else if(amount === 6) this.pictureChoiceGrid = [6, 2];
  196.     }
  197. };
  198.  
  199. Game_Message.prototype.setPictureChoicesGrid = function(cols, rows) {
  200.     this.customizedPictureChoiceGrid = true;
  201.     this.pictureChoiceGrid = [cols, rows];
  202. };
  203.  
  204. Game_Message.prototype.disablePictureChoicesGrid = function() {
  205.     this.customizedPictureChoiceGrid = false;
  206. };
  207.  
  208. Game_Message.prototype.movePictureChoice = function(index, x, y) {
  209.     this.pictureChoicePosition[index] = [x, y];
  210. };
  211.  
  212. //-----------------------------------------------------------------------------
  213. // Game_Interpreter SceneManager._scene._messageWindow._pictureChoiceWindow._spriteChoices[0].x = 0
  214. //-----------------------------------------------------------------------------
  215.  
  216. var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  217. Game_Interpreter.prototype.pluginCommand = function(command, args) {
  218.     _Game_Interpreter_pluginCommand.apply(this, arguments);
  219.     if(command.trim().toLowerCase() === 'setpicturechoicesgrid') {
  220.         if(String(args[0]).trim().toLowerCase() === 'auto') {
  221.             $gameMessage.disablePictureChoicesGrid();
  222.         } else {
  223.             var cols = parseInt(args[0]);
  224.             var rows = parseInt(args[1]);
  225.             $gameMessage.setPictureChoicesGrid(cols, rows);
  226.         }
  227.     } else if(command.trim().toLowerCase() === 'movepicturechoice') {
  228.             var index = parseInt(args[0]) - 1;
  229.             var posX = parseInt(args[1]);
  230.             var posY = parseInt(args[2]);
  231.             $gameMessage.movePictureChoice(index, posX, posY);
  232.     }
  233. };
  234.  
  235. //-----------------------------------------------------------------------------
  236. // Window_Message
  237. //-----------------------------------------------------------------------------
  238.  
  239. var _Window_Message_subWindows = Window_Message.prototype.subWindows;
  240. Window_Message.prototype.subWindows = function() {
  241.     var result = _Window_Message_subWindows.apply(this, arguments);
  242.     result.push(this._pictureChoiceWindow);
  243.     return result;
  244. };
  245.  
  246. var _Window_Message_createSubWindows = Window_Message.prototype.createSubWindows;
  247. Window_Message.prototype.createSubWindows = function() {
  248.     _Window_Message_createSubWindows.apply(this, arguments);
  249.     this._pictureChoiceWindow = new Window_PictureChoiceList(this);
  250. };
  251.  
  252. var _Window_Message_isAnySubWindowActive = Window_Message.prototype.isAnySubWindowActive;
  253. Window_Message.prototype.isAnySubWindowActive = function() {
  254.     return _Window_Message_isAnySubWindowActive.apply(this, arguments) || this._pictureChoiceWindow.active;
  255. };
  256.  
  257. var _Window_Message_startInput = Window_Message.prototype.startInput;
  258. Window_Message.prototype.startInput = function() {
  259.     if($gameMessage.isPictureChoices && $gameMessage.isChoice()) {
  260.         this._pictureChoiceWindow.start();
  261.         return true;
  262.     }
  263.     return _Window_Message_startInput.apply(this, arguments);
  264. };
  265.  
  266. var _Scene_Map_createMessageWindow = Scene_Map.prototype.createMessageWindow;
  267. Scene_Map.prototype.createMessageWindow = function() {
  268.     _Scene_Map_createMessageWindow.apply(this, arguments);
  269.     if(this._messageWindow._pictureChoiceWindow) {
  270.         this._windowLayer.removeChild(this._messageWindow._pictureChoiceWindow);
  271.         this.addChild(this._messageWindow._pictureChoiceWindow);
  272.     }
  273. };
  274.  
  275. //-----------------------------------------------------------------------------
  276. // Window_PictureChoiceList
  277. //-----------------------------------------------------------------------------
  278.  
  279. Window_PictureChoiceList.prototype = Object.create(Window_ChoiceList.prototype);
  280. Window_PictureChoiceList.prototype.constructor = Window_PictureChoiceList;
  281.  
  282. Window_PictureChoiceList.prototype._refreshAllParts = function() {};
  283. Window_PictureChoiceList.prototype._refreshCursor = function() {};
  284.  
  285. var _Window_PictureChoiceList_initialize = Window_PictureChoiceList.prototype.initialize;
  286. Window_PictureChoiceList.prototype.initialize = function(messageWindow) {
  287.     Window_ChoiceList.prototype.initialize.apply(this, arguments);
  288.     this._spriteChoices = [];
  289. };
  290.  
  291. Window_PictureChoiceList.prototype.updatePlacement = function() {
  292.     this.width = this.windowWidth();
  293.     this.height = this.windowHeight();
  294.     this.x = (Graphics.boxWidth - this.width) / 2;
  295.     this.y = (Graphics.boxHeight - this.height) / 2;
  296.     if(!this._messageWindow.isClosed()) {
  297.         if(this._messageWindow.y > Graphics.boxHeight/2) this.y -= this._messageWindow.height / 2;
  298.         else if(this._messageWindow.y <= 0) this.y += this._messageWindow.height / 2;
  299.     }
  300. };
  301.  
  302. var _Window_PictureChoiceList_start = Window_PictureChoiceList.prototype.start;
  303. Window_PictureChoiceList.prototype.start = function() {
  304.     this._spriteChoices.forEach(function(sprite) {
  305.         if(sprite) {
  306.             this.removeChild(sprite);
  307.         }
  308.     }, this);
  309.     _Window_PictureChoiceList_start.apply(this, arguments);
  310. };
  311.  
  312. Window_PictureChoiceList.prototype.windowWidth = function() {
  313.     return $gameMessage.pictureChoiceSize[0];
  314. };
  315.  
  316. Window_PictureChoiceList.prototype.windowHeight = function() {
  317.     if(!this._messageWindow.isClosed()) {
  318.         return $gameMessage.pictureChoiceSize[1] - this._messageWindow.height;
  319.     }
  320.     return $gameMessage.pictureChoiceSize[1];
  321. };
  322.  
  323. Window_PictureChoiceList.prototype.maxCols = function() {
  324.     return $gameMessage.pictureChoiceGrid[0];
  325. };
  326.  
  327. Window_PictureChoiceList.prototype.numVisibleRows = function() {
  328.     return $gameMessage.pictureChoiceGrid[1];
  329. };
  330.  
  331. Window_PictureChoiceList.prototype.itemHeight = function() {
  332.     return (this.contentsHeight() / this.numVisibleRows());
  333. };
  334.  
  335. Window_PictureChoiceList.prototype.contentsHeight = Window_Base.prototype.contentsHeight;
  336.  
  337. var _Window_PictureChoiceList_drawItem = Window_PictureChoiceList.prototype.drawItem;
  338. Window_PictureChoiceList.prototype.drawItem = function(index) {
  339.     var name = this.commandName(index);
  340.     var rect = this.itemRectForText(index);
  341.     if(name.match(/\\picture\[(.*)\]/i)) {
  342.         if(this._spriteChoices[index]) this.removeChild(this._spriteChoices[index]);
  343.         var imageName = String(RegExp.$1);
  344.         var bit = _.loadPicture(imageName);
  345.         var sprite = new Sprite_PictureChoice(bit);
  346.         if ($gameMessage.pictureChoicePosition[index] !== null) {
  347.             sprite.x = $gameMessage.pictureChoicePosition[index][0];
  348.             sprite.y = $gameMessage.pictureChoicePosition[index][1];
  349.             $gameMessage.pictureChoicePosition[index] = null;
  350.         } else {
  351.             sprite.x = rect.x + (rect.width/2);
  352.             sprite.y = rect.y + (rect.height/2);
  353.         }
  354.         this._spriteChoices[index] = sprite;
  355.         this.addChild(sprite);
  356.     } else {
  357.         var width = this.textWidthEx(name);
  358.         var bit = new Bitmap(width, this.contents.fontSize + 4);
  359.         var tempCont = this.contents;
  360.         this.contents = bit;
  361.         this.drawTextEx(name, 0, 0);
  362.         this.contents = tempCont;
  363.         var sprite = new Sprite_PictureChoice(bit);
  364.         if ($gameMessage.pictureChoicePosition[index] !== null) {
  365.             sprite.x = $gameMessage.pictureChoicePosition[index][0];
  366.             sprite.y = $gameMessage.pictureChoicePosition[index][1];
  367.             $gameMessage.pictureChoicePosition[index] = null;
  368.         } else {
  369.             sprite.x = rect.x + (rect.width/2);
  370.             sprite.y = rect.y + (rect.height/2);
  371.         }
  372.         this._spriteChoices[index] = sprite;
  373.         this.addChild(sprite);
  374.     }
  375. };
  376.  
  377. Window_PictureChoiceList.prototype.itemTextAlign = function() {
  378.     return 'center';
  379. };
  380.  
  381. var _Window_PictureChoiceList_update = Window_PictureChoiceList.prototype.update;
  382. Window_PictureChoiceList.prototype.update = function() {
  383.     _Window_PictureChoiceList_update.apply(this, arguments);
  384.     var length = this._spriteChoices.length;
  385.     for(var i = 0; i < length; i++) {
  386.         if(this._spriteChoices[i]) {
  387.             if(this.index() === i) {
  388.                 this._spriteChoices[i].updateIncrease();
  389.             } else {
  390.                 this._spriteChoices[i].updateDecrease();
  391.             }
  392.         }
  393.     }
  394. };
  395.  
  396. Window_PictureChoiceList.prototype.close = function() {
  397.     for(var i = 0; i < this._spriteChoices.length; i++) {
  398.         if(this._spriteChoices[i]) {
  399.             if(i === this.index()) {
  400.                 this._spriteChoices[i].startConfirm();
  401.             } else {
  402.                 this._spriteChoices[i].startDecline();
  403.             }
  404.         }
  405.     }
  406.     Window_ChoiceList.prototype.close.apply(this, arguments);
  407. };
  408.  
  409. //-----------------------------------------------------------------------------
  410. // Sprite_PictureChoice
  411. //-----------------------------------------------------------------------------
  412.  
  413. Sprite_PictureChoice.prototype = Object.create(Sprite.prototype);
  414. Sprite_PictureChoice.prototype.constructor = Sprite_PictureChoice;
  415.  
  416. Sprite_PictureChoice.prototype.initialize = function() {
  417.     Sprite.prototype.initialize.apply(this, arguments);
  418.     this.anchor.x = 0.5;
  419.     this.anchor.y = 0.5;
  420.     this._breath = 1;
  421.     this._direction = 0;
  422.     this.initTone();
  423.     this._duration = 120;
  424.     this._counter1 = this._duration;
  425.     this._counter2 = this._duration;
  426. };
  427.  
  428. Sprite_PictureChoice.prototype.update = function() {
  429.     Sprite.prototype.update.apply(this, arguments);
  430.     if(this._direction) {
  431.         if(this._direction === 1) {
  432.             this.opacity -= (_.close * 100);
  433.             this._breath += _.close;
  434.             this.scale.x = this._breath;
  435.             this.scale.y = this._breath;
  436.             if(this.opacity <= 0) {
  437.                 this._direction = 0;
  438.                 this.parent.removeChild(this);
  439.             }
  440.         } else if(this._direction === 2) {
  441.             this._breath -= _.confirm;
  442.             this.scale.x = this._breath;
  443.             this.scale.y = this._breath;
  444.             if(this._breath <= 0) {
  445.                 this._direction = 0;
  446.                 this.parent.removeChild(this);
  447.             }
  448.         }
  449.     }
  450. };
  451.  
  452. Sprite_PictureChoice.prototype.startConfirm = function() {
  453.     this._direction = 1;
  454. };
  455.  
  456. Sprite_PictureChoice.prototype.startDecline = function() {
  457.     this._direction = 2;
  458. };
  459.  
  460. Sprite_PictureChoice.prototype.updateIncrease = function() {
  461.     if(this._breath < _.max) {
  462.         this._breath += _.speed;
  463.         this.scale.x = this._breath;
  464.         this.scale.y = this._breath;
  465.        
  466.         var d = this._breath;
  467.         for (var i = 0; i < 4; i++) {
  468.             this._tone[i] = (this._tone[i] * (d - 1) + this._toneTarget2[i]) / d;
  469.         }
  470.         this.setColorTone(this._tone);
  471.     }
  472.     // if(this._counter1 > 0) {
  473.         // var d = this._counter1;
  474.         // for (var i = 0; i < 4; i++) {
  475.             // this._tone[i] = (this._tone[i] * (d - 1) + this._toneTarget2[i]) / d;
  476.         // }
  477.         // this.setColorTone(this._tone);
  478.         // this._counter1--;
  479.         // if (this._counter1 === 0) {
  480.             // this._counter1 = this._duration;
  481.         // }
  482.     // }
  483. };
  484.  
  485. Sprite_PictureChoice.prototype.updateDecrease = function() {
  486.     if(this._breath > _.min) {
  487.         this._breath -= _.speed;
  488.         this.scale.x = this._breath;
  489.         this.scale.y = this._breath;
  490.        
  491.         var d = this._breath;
  492.         for (var i = 0; i < 4; i++) {
  493.             this._tone[i] = (this._tone[i] * (d - 1) + this._toneTarget1[i]) / d;
  494.         }
  495.         this.setColorTone(this._tone);
  496.     }
  497.     // if(this._counter2 > 0) {
  498.         // var d = this._counter2;
  499.         // for (var i = 0; i < 4; i++) {
  500.             // this._tone[i] = (this._tone[i] * (d - 1) + this._toneTarget1[i]) / d;
  501.         // }
  502.         // this.setColorTone(this._tone);
  503.         // this._counter2--;
  504.         // if (this._counter2 === 0) {
  505.             // this._counter2 = this._duration;
  506.         // }
  507.     // }
  508. };
  509.  
  510. Sprite_PictureChoice.prototype.initTone = function() {
  511.     this._tone = [0, 0, 0, 0];
  512.     this._toneTarget1 = [0, 0, 0, 0]; // Fade Out
  513.     this._toneTarget2 = [-68, -68, -68, 0]; // Fade In
  514. };
  515.  
  516. })(SRD.PictureChoices);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement