Advertisement
Kakakadafi

YEP_EventMiniLabel

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