Advertisement
Guest User

YEP_EventMiniLabel / STV Edit

a guest
Oct 27th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Yanfly Engine Plugins - Event Mini Label (STV Edit)
  3. // YEP_EventMiniLabel.js
  4. //=============================================================================
  5.  
  6. var Imported = Imported || {};
  7. Imported.YEP_EventMiniLabel = true;
  8.  
  9. var Yanfly = Yanfly || {};
  10. Yanfly.EML = Yanfly.EML || {};
  11.  
  12. //=============================================================================
  13.  /*:
  14.  * @plugindesc v1.11 - STV Edit - Creates miniature-sized labels over events to allow
  15.  * you to insert whatever text you'd like in them.
  16.  * @author Yanfly Engine Plugins
  17.  *
  18.  * @param Default Show
  19.  * @desc Show mini labels by default?
  20.  * NO - false     YES - true
  21.  * @default true
  22.  *
  23.  * @param Minimum Width
  24.  * @desc What is the minimum width in pixels for mini labels?
  25.  * @default 136
  26.  *
  27.  * @param Font Size
  28.  * @desc What is the font size used for text inside a mini label?
  29.  * Default: 28
  30.  * @default 20
  31.  *
  32.  * @param X Buffer
  33.  * @desc Alter the X position of the label by this much.
  34.  * @default 0
  35.  *
  36.  * @param Y Buffer
  37.  * @desc Alter the Y position of the label by this much.
  38.  * @default 36
  39.  *
  40.  * @param Approach Distance
  41.  * @desc How many Tiles away at least from Hero to let the Label appear.
  42.  * @default 2
  43.  *
  44.  * @help
  45.  * ============================================================================
  46.  * Introduction
  47.  * ============================================================================
  48.  *
  49.  * This plugin lets you place text above the heads of various events using a
  50.  * miniature label through a comment tag.
  51.  *
  52.  * ============================================================================
  53.  * Comment Tags
  54.  * ============================================================================
  55.  *
  56.  * Comment tags are 'notetags' used within the lines of an event's comments.
  57.  * The reason I'm using the comment tags instead of the notetags is because
  58.  * each page of an event can yield a different potential name.
  59.  *
  60.  * To use this, make a comment within the event you wish to make the mini
  61.  * label for and insert the following:
  62.  *
  63.  *   <Mini Label: text>
  64.  *   This will display the 'text' above the event. You can use text codes for
  65.  *   this comment tag and it will create dynamic messages.
  66.  *
  67.  *   <Mini Label Font Size: x>
  68.  *   This will change the font size used for the mini label to x. If this tag
  69.  *   isn't used, the font size will be the default value in the parameters.
  70.  *
  71.  *   <Mini Label X Buffer: +x>
  72.  *   <Mini Label X Buffer: -x>
  73.  *   This will adjust the X buffer for the mini label by x value. If this tag
  74.  *   isn't used, the X buffer will be the default value in the parameters.
  75.  *
  76.  *   <Mini Label Y Buffer: +x>
  77.  *   <Mini Label Y Buffer: -x>
  78.  *   This will adjust the Y buffer for the mini label by x value. If this tag
  79.  *   isn't used, the Y buffer will be the default value in the parameters.
  80.  *
  81.  *   <Always Show Mini Label>
  82.  *   This will make the mini label to always be shown, even when the plugin
  83.  *   command to hide mini labels is used.
  84.  *
  85.  *   <Mini Label Range: x>
  86.  *   The player will have to be within x tiles of this event in order for the
  87.  *   mini label to appear visibly.
  88.  *
  89.  *   <Mini Label Require Facing>
  90.  *   This will require the player to be facing the direction of the event in
  91.  *   order for the mini label to appear.
  92.  *
  93.  *   <Mini Label Require Approach>                                                              // -------------------------- Skotty-TV addition !
  94.  *   This will require the player to be close X Tiles to the target object
  95.  *   for the mini label to appear.
  96.  *
  97.  * ============================================================================
  98.  * Plugin Commands
  99.  * ============================================================================
  100.  *
  101.  * If you would like to shut off the Event Mini Label mid-game or turn it on,
  102.  * you can use the following plugin commands:
  103.  *
  104.  * Plugin Command:
  105.  *
  106.  *   HideMiniLabel
  107.  *   Hides all Event Mini Label.
  108.  *
  109.  *   ShowMiniLabel
  110.  *   Shows all Event Mini Label.
  111.  *
  112.  *   RefreshMiniLabel
  113.  *   Refreshes all Event Mini Labels on the map.
  114.  *
  115.  * ============================================================================
  116.  * Changelog
  117.  * ============================================================================
  118.  *
  119.  * Version 1.11:
  120.  * - SkottyTV Edit: Option to only show the Label while near the object
  121.  * with: <Mini Label Require Facing>
  122.  *
  123.  * Version 1.10:
  124.  * - Mini Windows will now readjust their size to show at normal scale if the
  125.  * map is zoomed in.
  126.  *
  127.  * Version 1.09:
  128.  * - Fixed a bug that caused Mini Labels that started off as hidden to remain
  129.  * hidden even after turning the Mini Labels on.
  130.  *
  131.  * Version 1.08:
  132.  * - Added <Mini Label Require Facing> comment tag.
  133.  * - Moved the priority of the Mini Labels to be later added to the spriteset
  134.  * so they can stay on top of more effects.
  135.  *
  136.  * Version 1.07:
  137.  * - Added more padding space so text doesn't get cut off.
  138.  *
  139.  * Version 1.06:
  140.  * - Fixed a bug that caused some mini labels to show if the event was loaded
  141.  * onto the map without any currently active pages.
  142.  *
  143.  * Version 1.05:
  144.  * - Added 'X Buffer' plugin parameter and the <Mini Label X Buffer: +x> and
  145.  * <Mini Label X Buffer: -x> comment tags to alter the X position of the event
  146.  * mini label.
  147.  *
  148.  * Version 1.04:
  149.  * - Added 'RefreshMiniLabel' plugin command to allow you to manually refresh
  150.  * all mini labels on the map.
  151.  *
  152.  * Version 1.03:
  153.  * - Optimized updating performance to reduce lag on maps with many events.
  154.  *
  155.  * Version 1.01:
  156.  * - Fixed a bug that didn't update event labels under certain page conditions.
  157.  * - Added <Mini Label Range: x> notetag.
  158.  *
  159.  * Version 1.00:
  160.  * - Finished Plugin!
  161.  */
  162. //=============================================================================
  163.  
  164. //=============================================================================
  165. // Parameter Variables
  166. //=============================================================================
  167.  
  168. Yanfly.Parameters = PluginManager.parameters('YEP_EventMiniLabel');
  169. Yanfly.Param = Yanfly.Param || {};
  170.  
  171. Yanfly.Param.EMWDefaultShow = eval(String(Yanfly.Parameters['Default Show']));
  172. Yanfly.Param.EMWMinWidth = Number(Yanfly.Parameters['Minimum Width']);
  173. Yanfly.Param.EMWFontSize = Number(Yanfly.Parameters['Font Size']);
  174. Yanfly.Param.EMWBufferX = Number(Yanfly.Parameters['X Buffer']);
  175. Yanfly.Param.EMWBufferY = Number(Yanfly.Parameters['Y Buffer']);
  176. Yanfly.Param.EMWApproachDistance = Number(Yanfly.Parameters['Approach Distance']);             // -------------------------- Skotty-TV addition !
  177.  
  178. //=============================================================================
  179. // Game_System
  180. //=============================================================================
  181.  
  182. Yanfly.EML.Game_System_initialize = Game_System.prototype.initialize;
  183. Game_System.prototype.initialize = function() {
  184.     Yanfly.EML.Game_System_initialize.call(this);
  185.     this.initEventMiniLabel();
  186. };
  187.  
  188. Game_System.prototype.initEventMiniLabel = function() {
  189.     this._showEventMiniLabel = Yanfly.Param.EMWDefaultShow;
  190. };
  191.  
  192. Game_System.prototype.isShowEventMiniLabel = function() {
  193.     if (this._showEventMiniLabel === undefined) this.initEventMiniLabel();
  194.     return this._showEventMiniLabel;
  195. };
  196.  
  197. Game_System.prototype.setEventMiniLabel = function(value) {
  198.     this._showEventMiniLabel = value;
  199.     Game_Interpreter.prototype.refreshEventMiniLabel.call(this);
  200. };
  201.  
  202. //=============================================================================
  203. // Game_Interpreter
  204. //=============================================================================
  205.  
  206. Yanfly.EML.Game_Interpreter_pluginCommand =
  207.     Game_Interpreter.prototype.pluginCommand;
  208. Game_Interpreter.prototype.pluginCommand = function(command, args) {
  209.   Yanfly.EML.Game_Interpreter_pluginCommand.call(this, command, args);
  210.   if (command === 'HideMiniLabel') $gameSystem.setEventMiniLabel(false);
  211.   if (command === 'ShowMiniLabel') $gameSystem.setEventMiniLabel(true);
  212.   if (command === 'RefreshMiniLabel') this.refreshEventMiniLabel();
  213. };
  214.  
  215. Game_Interpreter.prototype.refreshEventMiniLabel = function() {
  216.     if ($gameParty.inBattle()) return;
  217.     var scene = SceneManager._scene;
  218.     if (scene instanceof Scene_Map) {
  219.       scene.refreshAllMiniLabels();
  220.     }
  221. };
  222.  
  223. //=============================================================================
  224. // Window_EventMiniLabel
  225. //=============================================================================
  226.  
  227. function Window_EventMiniLabel() {
  228.     this.initialize.apply(this, arguments);
  229. }
  230.  
  231. Window_EventMiniLabel.prototype = Object.create(Window_Base.prototype);
  232. Window_EventMiniLabel.prototype.constructor = Window_EventMiniLabel;
  233.  
  234. Window_EventMiniLabel.prototype.initialize = function() {
  235.     this._bufferX = Yanfly.Param.EMWBufferX;
  236.     this._bufferY = Yanfly.Param.EMWBufferY;
  237.     this._fontSize = Yanfly.Param.EMWFontSize;
  238.     this._alwaysShow = false;
  239.     var width = Yanfly.Param.EMWMinWidth;
  240.     var height = this.windowHeight();
  241.     this._range = 500;
  242.     this._reqFacing = false;
  243.     this._reqApproach = false;                                                                  // -------------------------- Skotty-TV addition !
  244.     Window_Base.prototype.initialize.call(this, 0, 0, width, height);
  245.     this.opacity = 0;
  246.     this.contentsOpacity = 0;
  247.     this._character = null;
  248.     this._page = 0;
  249.     this._text = '';
  250. };
  251.  
  252. Window_EventMiniLabel.prototype.standardFontSize = function() {
  253.     if (this._fontSize !== undefined) return this._fontSize;
  254.     return Yanfly.Param.EMWFontSize;
  255. };
  256.  
  257. Window_EventMiniLabel.prototype.windowHeight = function() {
  258.     var height = this.fittingHeight(1);
  259.     height = Math.max(height, 36 + this.standardPadding() * 2);
  260.     return height;
  261. };
  262.  
  263. Window_EventMiniLabel.prototype.lineHeight = function() {
  264.     return this.standardFontSize() + 8;
  265. };
  266.  
  267. Window_EventMiniLabel.prototype.bufferX = function() {
  268.     if (this._bufferX !== undefined) return this._bufferX;
  269.     return Yanfly.Param.EMWBufferX;
  270. };
  271.  
  272. Window_EventMiniLabel.prototype.bufferY = function() {
  273.     if (this._bufferY !== undefined) return this._bufferY;
  274.     return Yanfly.Param.EMWBufferY;
  275. };
  276.  
  277. Window_EventMiniLabel.prototype.setCharacter = function(character) {
  278.     this.setText('');
  279.     this._character = character;
  280.     if (character._eventId) this.gatherDisplayData();
  281. };
  282.  
  283. Window_EventMiniLabel.prototype.gatherDisplayData = function() {
  284.     this._page = this._character.page();
  285.     this._pageIndex = this._character._pageIndex;
  286.     this._range = 500;
  287.     this._bufferY = Yanfly.Param.EMWBufferY;
  288.     this._fontSize = Yanfly.Param.EMWFontSize;
  289.     this._alwaysShow = false;
  290.     this._reqFacing = false;
  291.     this._reqApproach = false;                                                                  // -------------------------- Skotty-TV addition !
  292.     if (!this._character.page()) {
  293.       return this.visible = false;
  294.     }
  295.     var list = this._character.list();
  296.     var max = list.length;
  297.     var comment = '';
  298.     for (var i = 0; i < max; ++i) {
  299.       var ev = list[i];
  300.       if ([108, 408].contains(ev.code)) comment += ev.parameters[0] + '\n';
  301.     }
  302.     this.extractNotedata(comment);
  303. };
  304.  
  305. Window_EventMiniLabel.prototype.extractNotedata = function(comment) {
  306.   if (comment === '') return;
  307.   var tag1 = /<(?:MINI WINDOW|MINI LABEL):[ ](.*)>/i;
  308.   var tag2 = /<(?:MINI WINDOW FONT SIZE|MINI LABEL FONT SIZE):[ ](\d+)>/i;
  309.   var tag3 = /<(?:MINI WINDOW Y BUFFER|MINI LABEL Y BUFFER):[ ]([\+\-]\d+)>/i;
  310.   var tag4 = /<(?:ALWAYS SHOW MINI WINDOW|ALWAYS SHOW MINI LABEL)>/i;
  311.   var tag5 = /<(?:MINI WINDOW RANGE|MINI LABEL RANGE):[ ](\d+)>/i;
  312.   var tag6 = /<(?:MINI WINDOW X BUFFER|MINI LABEL X BUFFER):[ ]([\+\-]\d+)>/i;
  313.   var tag7 = /<(?:MINI WINDOW REQUIRE FACING|MINI LABEL REQUIRE FACING)>/i;
  314.   var tag8 = /<(?:MINI WINDOW REQUIRE APPROACH|MINI LABEL REQUIRE APPROACH)>/i;                 // -------------------------- Skotty-TV addition !
  315.   var notedata = comment.split(/[\r\n]+/);
  316.   var text = '';
  317.   for (var i = 0; i < notedata.length; ++i) {
  318.     var line = notedata[i];
  319.     if (line.match(tag1)) {
  320.       text = String(RegExp.$1);
  321.     } else if (line.match(tag2)) {
  322.       this._fontSize = parseInt(RegExp.$1);
  323.     } else if (line.match(tag3)) {
  324.       this._bufferY = parseInt(RegExp.$1);
  325.     } else if (line.match(tag4)) {
  326.       this._alwaysShow = true;
  327.     } else if (line.match(tag5)) {
  328.       this._range = parseInt(RegExp.$1);
  329.     } else if (line.match(tag6)) {
  330.       this._bufferX = parseInt(RegExp.$1);
  331.     } else if (line.match(tag7)) {
  332.       this._reqFacing = true;
  333.     } else if (line.match(tag8)) {                                                              // -------------------------- Skotty-TV addition !
  334.       this._reqApproach = true;
  335.     }
  336.   }
  337.   this.setText(text);
  338.   if (this._text === '' || !$gameSystem.isShowEventMiniLabel()) {
  339.     this.visible = false;
  340.     this.contentsOpacity = 0;
  341.   } else {
  342.     this.visible = true;
  343.     if (this._reqFacing || this._reqApproach) {                                                 // -------------------------- Skotty-TV addition !
  344.       this.contentsOpacity = 0;
  345.     } else {
  346.       this.contentsOpacity = 255;
  347.     }
  348.   }
  349.  
  350. };
  351.  
  352. Window_EventMiniLabel.prototype.setText = function(text) {
  353.     if (this._text === text) return;
  354.     this._text = text;
  355.     this.refresh();
  356. };
  357.  
  358. Window_EventMiniLabel.prototype.refresh = function() {
  359.     if (Imported.YEP_SelfSwVar) {
  360.       $gameTemp.setSelfSwVarEvent(this._character._mapId, this._character._eventId);
  361.     }
  362.     this.contents.clear();
  363.     var txWidth = this.textWidthEx(this._text);
  364.     txWidth += this.textPadding() * 2;
  365.     var width = txWidth;
  366.     this.width = Math.max(width, Yanfly.Param.EMWMinWidth);
  367.     this.width += this.standardPadding() * 2;
  368.     this.height = this.windowHeight();
  369.     this.createContents();
  370.     var wx = (this.contents.width - txWidth) / 2;
  371.     var wy = 0;
  372.     this.drawTextEx(this._text, wx + this.textPadding(), wy);
  373.     if (Imported.YEP_SelfSwVar) $gameTemp.clearSelfSwVarEvent();
  374. };
  375.  
  376. Window_EventMiniLabel.prototype.forceRefresh = function() {
  377.     this.refresh();
  378.     this.updateOpacity();
  379. };
  380.  
  381. Window_EventMiniLabel.prototype.textWidthEx = function(text) {
  382.     return this.drawTextEx(text, 0, this.contents.height);
  383. };
  384.  
  385. Window_EventMiniLabel.prototype.update = function() {
  386.     Window_Base.prototype.update.call(this);
  387.     if (!this._character) return;
  388.     if (!this._character._eventId) return;
  389.     this.updatePage();
  390.     if (this._text === '') return;
  391.     this.updateOpacity();
  392. };
  393.  
  394. Window_EventMiniLabel.prototype.updatePage = function() {
  395.     if (this._pageIndex === this._character._pageIndex) return;
  396.     this._pageIndex = this._character._pageIndex;
  397.     this.contents.clear();
  398.     this._text = '';
  399.     this.gatherDisplayData();
  400. };
  401.  
  402. Window_EventMiniLabel.prototype.updateOpacity = function() {
  403.     if (this.showMiniLabel()) {
  404.       this.show();
  405.     } else {
  406.       this.hide();
  407.     }
  408. };
  409.  
  410. Window_EventMiniLabel.prototype.show = function() {
  411.     if (this.contentsOpacity >= 255) return;
  412.     this.contentsOpacity += 16;
  413.     this.visible = true;
  414. };
  415.  
  416. Window_EventMiniLabel.prototype.hide = function() {
  417.     if (this.contentsOpacity <= 0) {
  418.       if (this.visible) this.visible = false;
  419.       return;
  420.     }
  421.     this.contentsOpacity -= 16;
  422. };
  423.  
  424. Window_EventMiniLabel.prototype.showMiniLabel = function() {
  425.     if (this._alwaysShow) return true;
  426.     if (!this.withinRange()) return false;
  427.     if (!this.meetsFacingRequirements()) return false;
  428.     if (!this.meetsApproachRequirements()) return false;                                        // -------------------------- Skotty-TV addition !
  429.     return $gameSystem.isShowEventMiniLabel();
  430. };
  431.  
  432. Window_EventMiniLabel.prototype.withinRange = function() {
  433.     if (this._range >= 500) return true;
  434.     var player = $gamePlayer;
  435.     var chara = this._character;
  436.     if (this._range >= Math.abs(player.x - chara.x)) {
  437.       if (this._range >= Math.abs(player.y - chara.y)) {
  438.         return true;
  439.       }
  440.     }
  441.     return false;
  442. };
  443.  
  444. Window_EventMiniLabel.prototype.meetsApproachRequirements = function() {                        // -------------------------- Skotty-TV addition !
  445.     if (!this._character) return true;
  446.     if (!this._reqApproach) return true;
  447.     var playerX = $gamePlayer.x;
  448.     var playerY = $gamePlayer.y;
  449.     var eventX = this._character.x;
  450.     var eventY = this._character.y;
  451.     var distanceX = Math.abs(playerX-eventX);
  452.     var distanceY = Math.abs(playerY-eventY);
  453.     var distance = Yanfly.Param.EMWApproachDistance;
  454.    
  455.     return distanceX <= distance && distanceY <= distance;
  456. };
  457.  
  458. Window_EventMiniLabel.prototype.meetsFacingRequirements = function() {
  459.   if (!this._character) return true;
  460.   if (!this._reqFacing) return true;
  461.   var direction = $gamePlayer.direction();
  462.   var playerX = $gamePlayer.x;
  463.   var playerY = $gamePlayer.y;
  464.   var eventX = this._character.x;
  465.   var eventY = this._character.y;
  466.   switch (direction) {
  467.   case 1:
  468.     return playerX >= eventX && playerY <= eventY;
  469.     break;
  470.   case 2:
  471.     return playerY <= eventY;
  472.     break;
  473.   case 3:
  474.     return playerX <= eventX && playerY <= eventY;
  475.     break;
  476.   case 4:
  477.     return playerX >= eventX;
  478.     break;
  479.   case 6:
  480.     return playerX <= eventX;
  481.     break;
  482.   case 7:
  483.     return playerX >= eventX && playerY >= eventY;
  484.     break;
  485.   case 8:
  486.     return playerY >= eventY;
  487.     break;
  488.   case 9:
  489.     return playerX <= eventX && playerY >= eventY;
  490.     break;
  491.   default:
  492.     return true;
  493.     break;
  494.   }
  495. };
  496.  
  497. //=============================================================================
  498. // Sprite_Character
  499. //=============================================================================
  500.  
  501. Yanfly.EML.Sprite_Character_update = Sprite_Character.prototype.update;
  502. Sprite_Character.prototype.update = function() {
  503.     Yanfly.EML.Sprite_Character_update.call(this);
  504.     this.updateMiniLabel();
  505.     this.updateMiniLabelZoom();
  506. };
  507.  
  508. Sprite_Character.prototype.updateMiniLabel = function() {
  509.     this.setupMiniLabel();
  510.     if (!this._miniLabel) return;
  511.     this.positionMiniLabel();
  512. };
  513.  
  514. Sprite_Character.prototype.setupMiniLabel = function() {
  515.     if (this._miniLabel) return;
  516.     if (!SceneManager._scene._spriteset) return;
  517.     this._miniLabel = new Window_EventMiniLabel();
  518.     this._miniLabel.setCharacter(this._character);
  519.     //this.parent.parent.addChild(this._miniLabel);
  520.     SceneManager._scene._spriteset.addChild(this._miniLabel);
  521. };
  522.  
  523. Sprite_Character.prototype.positionMiniLabel = function() {
  524.     var win = this._miniLabel;
  525.     var width = win.width * win.scale.x;
  526.     win.x = this.x + width / -2 + win.bufferX();
  527.     var height = win.height * win.scale.y;
  528.     var buffer = win.bufferY() * win.scale.y;
  529.     win.y = this.y + (this.height * -1) - height + buffer;
  530. };
  531.  
  532. Sprite_Character.prototype.updateMiniLabelZoom = function() {
  533.   if (!this._miniLabel) return;
  534.   var spriteset = SceneManager._scene._spriteset;
  535.   this._miniLabel.scale.x = 1 / spriteset.scale.x;
  536.   this._miniLabel.scale.y = 1 / spriteset.scale.y;
  537. };
  538.  
  539. Sprite_Character.prototype.refreshMiniLabel = function() {
  540.     if (this._miniLabel) this._miniLabel.forceRefresh();
  541. };
  542.  
  543. //=============================================================================
  544. // Scene_Map
  545. //=============================================================================
  546.  
  547. Scene_Map.prototype.refreshAllMiniLabels = function() {
  548.     var length = this._spriteset._characterSprites.length;
  549.     for (var i = 0; i < length; ++i) {
  550.       var sp = this._spriteset._characterSprites[i];
  551.       sp.refreshMiniLabel();
  552.     }
  553. };
  554.  
  555. //=============================================================================
  556. // End of File
  557. //=============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement