Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2016
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // SilvEventText.js
  3. // Version: 1.02
  4. //=============================================================================
  5. /*:
  6.  * @plugindesc v1.02 Attaches texts on the map to events. <SilvEventText>
  7.  * @author Silver
  8.  *
  9.  * @param -- Default Values --
  10.  *
  11.  * @param Default Font Size
  12.  * @desc Default font size
  13.  * @default 24
  14.  *
  15.  * @param Default Font Color
  16.  * @desc Default font color
  17.  * @default #FFFFFF
  18.  *
  19.  * @param Default Outline Color
  20.  * @desc Default text outline color. Red, Green, Blue, Alpha
  21.  * @default rgba(0, 0, 0, 0.5)
  22.  *
  23.  * @param Default Outline Width
  24.  * @desc Default width of the text-outline
  25.  * @default 4
  26.  *
  27.  * @param Default Text Alignment
  28.  * @desc Allowed values: topleft, topcenter, topright, center
  29.  * @default topcenter
  30.  *
  31.  * @param Default Opacity
  32.  * @desc Text Opacity 0-255
  33.  * @default 255
  34.  *
  35.  * @param Default Italic
  36.  * @desc By default, make all the text italic?
  37.  * @default false
  38.  *
  39.  * @param Default Offset X
  40.  * @desc Offset x. A negative value means more to the left.
  41.  * @default 0
  42.  *
  43.  * @param Default Offset Y
  44.  * @desc Offset y. A negative value means more to the top.
  45.  * @default 0
  46.  *
  47.  * @param Default Extra Bitmap Width
  48.  * @desc The default extra bitmap-width.
  49.  * @default 0
  50.  *
  51.  * @param Default Extra Bitmap Height
  52.  * @desc The default extra bitmap-height.
  53.  * @default 0
  54.  *
  55.  * @help
  56.  *--------------------------------------
  57.  * Notes:
  58.  *--------------------------------------
  59.  * - This plugin only works for sprites that are max 48 pixels in height. But you can use the offset-tags to make it work for any size
  60.  * - Use multiple comments to fit in all of your tags. But only 1 tag per line and don't forget the </evtext> at the end.
  61.  * - This plugin does not have 'jittering-text'
  62.  * - Events can have multiple event-texts.
  63.  * - Switching the event-page will clear the EventText(s) from the old page.
  64.  *
  65.  *--------------------------------------
  66.  * Plugin Commands (not case sensitive):
  67.  *--------------------------------------
  68.  * EventText ShowAll
  69.  * EventText HideAll
  70.  *
  71.  *--------------------------------------
  72.  * Event Comment (NOT the event-notetag):
  73.  *--------------------------------------
  74.  * The comment must start with: <evtext>
  75.  * Nothing is case-sensitive here
  76.  *
  77.  * Supported tags:
  78.  * Text: value            // << this tag must be the first tag!
  79.  * FontSize: value
  80.  * FontColor: value       // Supported format examples: #FFFFFF, FFFFFF, rgba(0, 0, 0, 0.5)
  81.  * OutlineColor: value    // Supported format examples: #FFFFFF, FFFFFF, rgba(0, 0, 0, 0.5)
  82.  * OutlineWidth: value
  83.  * Offset_x: value
  84.  * Offset_y: value
  85.  * Opacity: value         // 0-255
  86.  * Align: value           // Supported values: topleft, topcenter, topright, center
  87.  * Alignment: value       // same as align-tag
  88.  * Italic: value          // true/false
  89.  * ExtraBmpWidth: value   // handy for thick outlineWidth's (and because RPG Maker MV fails)
  90.  * ExtraBmp_W: value      // Same as the above
  91.  * ExtraBmpHeight: value  // handy for thick outlineWidth's (and because RPG Maker MV fails)
  92.  * ExtraBmp_H: value      // Same as the above
  93.  * </evtext>              // without this tag the text will not be drawn. So don't forget it.
  94.  *
  95.  *
  96.  * Example comment:
  97.  * <evText>
  98.  * Text:Well Hello There!
  99.  * FontSize: 30
  100.  * FontColor: FF0000
  101.  * Italic: true
  102.  * </evText>
  103.  *
  104.  *--------------------------------------
  105.  * Dev notes (ONLY for other developers):
  106.  *--------------------------------------
  107.  * - The Game Events maintain a reference to the EventTexts using this variable: this.silvEvTexts[]
  108.  * - To change an event's fontsize for example (after it's been created):
  109.  * this.silvEvTexts[0].fontSize = 40;
  110.  * this.silvEvTexts[0].render();
  111.  *
  112.  *--------------------------------------
  113.  * Version History:
  114.  *--------------------------------------
  115.  * v.1.02 (3 April 2016)
  116.  * - Fixed a bug where entering the text "0" (w/o quotes) would cause an error.
  117.  *
  118.  * v.1.01b (26 December 2015)
  119.  * - Added 2 new parameters and 2 new commands.
  120.  * - Applied my new coding standards.
  121.  * - Fixed a crash.
  122.  * - EventTexts are now deleted when the events themselves are erased.
  123.  * - Added 2 new plugin commands.
  124.  * - Fixed a bug where opening&closing the menu would delete all labels.
  125.  *
  126.  * v.1.00 (24 December 2015)
  127.  * - First Release
  128.  *
  129.  */
  130. // Imported
  131. var Imported = Imported || {};
  132. Imported.SILV_EventText = 1.02;
  133.  
  134. // #Parameters
  135. var Silv = Silv || {};
  136. Silv.EvText = Silv.EvText || {};
  137. Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilvEventText>'); })[0].parameters;
  138. // Default Values
  139. Silv.EvText.DefaultFontSize = parseInt(Silv.Parameters['Default Font Size']);
  140. Silv.EvText.DefaultFontColor = Silv.Parameters['Default Font Color'];
  141. Silv.EvText.DefaultOutlineColor = Silv.Parameters['Default Outline Color'];
  142. Silv.EvText.DefaultOutlineWidth =  parseInt(Silv.Parameters['Default Outline Width']);
  143. Silv.EvText.DefaultTextAlign = Silv.Parameters['Default Text Alignment'];
  144. Silv.EvText.DefaultOpacity = parseInt(Silv.Parameters['Default Opacity']);
  145. Silv.EvText.DefaultItalic = parseInt(Silv.Parameters['Default Italic']);
  146. Silv.EvText.DefaultOffset_X = parseFloat(Silv.Parameters['Default Offset X']);
  147. Silv.EvText.DefaultOffset_Y = parseFloat(Silv.Parameters['Default Offset Y']);
  148. Silv.EvText.DefaultExtraBmpWidth = parseInt(Silv.Parameters['Default Extra Bitmap Width']);
  149. Silv.EvText.DefaultExtraBmpHeight = parseInt(Silv.Parameters['Default Extra Bitmap Height']);
  150.  
  151. // Alias
  152. Silv.Alias = Silv.Alias || {};
  153. if (!Silv.AddAlias)
  154. {
  155.     Silv.AddAlias = function(alias, original_method)
  156.     {
  157.         if (Silv.Alias[alias]) { throw new Error('Alias already exists: ' + alias); }
  158.         Silv.Alias[alias] = original_method;
  159.     };
  160. }
  161.  
  162. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  163. // Utilities
  164. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  165. Silv.EvText.GetTextAfter = function(str, character)
  166. {
  167.     return str.substr(str.indexOf(character) + 1);
  168. };
  169.  
  170. Silv.EvText.MeasureBmp = new Bitmap(1, 1);
  171.  
  172. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  173. // Scene Map
  174. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  175. Silv.AddAlias('evText_Scene_Map_initialize', Scene_Map.prototype.initialize);
  176. Scene_Map.prototype.initialize = function()
  177. {
  178.     Silv.Alias.evText_Scene_Map_initialize.apply(this, arguments);
  179.     this.evSpritesToAdd = [];
  180. };
  181.  
  182. Silv.AddAlias('evText_Scene_Map_createSpriteset', Scene_Map.prototype.createSpriteset);
  183. Scene_Map.prototype.createSpriteset = function()
  184. {
  185.     Silv.Alias.evText_Scene_Map_createSpriteset.apply(this, arguments);
  186.    
  187.     this.evSpritesToAdd.forEach(function(sprite)
  188.     {
  189.         this._spriteset.addChild(sprite);
  190.     }, this);
  191. };
  192.  
  193. Silv.AddAlias('evText_Scene_Map_createDisplayObjects', Scene_Map.prototype.createDisplayObjects);
  194. Scene_Map.prototype.createDisplayObjects = function()
  195. {
  196.     Silv.Alias.evText_Scene_Map_createDisplayObjects.apply(this, arguments);
  197.     this.addOldEventTexts();
  198. };
  199.  
  200. // Because if the user just came back from another scene (like the menu) then the spriteset was destroyed and recreated and the sprites require to be added to the spriteset again.
  201. Scene_Map.prototype.addOldEventTexts = function()
  202. {
  203.     $gameMap.events().forEach(function(event)
  204.     {
  205.         if (event.silvEvTexts)
  206.         {
  207.             event.silvEvTexts.forEach(function(evText)
  208.             {
  209.                 if (this._spriteset.children.indexOf(evText) == -1)
  210.                 {
  211.                     this._spriteset.addChild(evText);
  212.                 }
  213.             }, this);
  214.         }
  215.     }, this);
  216. };
  217.  
  218. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  219. // Event Text
  220. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  221. function EventText() { this.initialize.apply(this, arguments); }
  222.  
  223. EventText.prototype = Object.create(Sprite_Base.prototype);
  224. EventText.prototype.constructor = EventText;
  225.  
  226. EventText.prototype.initialize = function(gameEvent, text)
  227. {
  228.     Sprite_Base.prototype.initialize.call(this);
  229.     this.gameEvent = gameEvent;
  230.    
  231.     this.setDefaults();
  232.     this.setText(text);
  233.    
  234.     if (SceneManager._scene._spriteset)
  235.     {
  236.         SceneManager._scene._spriteset.addChild(this); // SceneManager._scene can be null when the map was just loaded
  237.     }
  238.     else
  239.     {
  240.         SceneManager._scene.evSpritesToAdd.push(this); // if it's null, just push it to a temporary list instead
  241.     }
  242. };
  243.  
  244. EventText.prototype.setDefaults = function()
  245. {
  246.     this._fontSize = Silv.EvText.DefaultFontSize;
  247.     this.fontColor = Silv.EvText.DefaultFontColor;
  248.     this.outlineColor = Silv.EvText.DefaultOutlineColor;
  249.     this._outlineWidth = Silv.EvText.DefaultOutlineWidth;
  250.     this.textOpacity = Silv.EvText.DefaultOpacity;
  251.     this.italic = Silv.EvText.DefaultItalic;
  252.     this.extraOffset_x = Silv.EvText.DefaultOffset_X;
  253.     this.extraOffset_y = Silv.EvText.DefaultOffset_Y;
  254.     this._extraBmpWidth = Silv.EvText.DefaultExtraBmpWidth;
  255.     this._extraBmpHeight = Silv.EvText.DefaultExtraBmpHeight;
  256. };
  257.  
  258. EventText.prototype.extraBmpWidth = function(value)
  259. {
  260.     this._extraBmpWidth = value;
  261.     this.setSizePropertyAfter();
  262. };
  263.  
  264. EventText.prototype.extraBmpHeight = function(value)
  265. {
  266.     this._extraBmpHeight = value;
  267.     this.setSizePropertyAfter();
  268. };
  269.  
  270. EventText.prototype.fontSize = function(value)
  271. {
  272.     this._fontSize = value;
  273.     this.setSizePropertyAfter();
  274. };
  275.  
  276. EventText.prototype.outlineWidth = function(value)
  277. {
  278.     this._outlineWidth = value;
  279.     this.setSizePropertyAfter();
  280. };
  281.  
  282. // Creates a new bitmap to fit the size of the new properties, recalculates the alignment and of course draws the contents onto the new bitmap
  283. EventText.prototype.setSizePropertyAfter = function()
  284. {
  285.     this.createBitmap();
  286.     this.setAlignment(this.alignment);
  287.     if (!this.requiresRender) { this.render(); }
  288. };
  289.  
  290. EventText.prototype.createBitmap = function(text)
  291. {
  292.     Silv.EvText.MeasureBmp.fontSize = this._fontSize;
  293.     Silv.EvText.MeasureBmp.outlineWidth = this._outlineWidth;
  294.     Silv.EvText.MeasureBmp.fontItalic = this.italic;
  295.     this.bitmap = new Bitmap(Silv.EvText.MeasureBmp.measureTextWidth(text) + this._extraBmpWidth, this._fontSize + this._extraBmpHeight);
  296. };
  297.  
  298. EventText.prototype.setText = function(text)
  299. {
  300.     this.createBitmap();
  301.     this.text = text;
  302.     this.setAlignment(Silv.EvText.DefaultTextAlign);
  303.     this.requiresRender = true;
  304. };
  305.  
  306. // alignment: topLeft, topCenter, topRight, center
  307. EventText.prototype.setAlignment = function(alignment)
  308. {  
  309.     if (typeof this.text === undefined) { throw new Error('setAlignment() requires the text to be set first.'); }
  310.     this.alignment = alignment;
  311.    
  312.     switch(alignment.toLowerCase())
  313.     {  
  314.         case 'topleft':
  315.             this.alignOffset_x = -$gameMap.tileWidth() / 2;
  316.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  317.             break;
  318.         case 'topcenter':
  319.             this.alignOffset_x = -this.bitmap.width / 2;
  320.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  321.             break;
  322.         case 'topright':
  323.             this.alignOffset_x = +$gameMap.tileWidth() / 3 - this.bitmap.width;
  324.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  325.             break;
  326.         case 'center':
  327.             this.alignOffset_x = -this.bitmap.width / 2;
  328.             this.alignOffset_y = -$gameMap.tileHeight() / 2 - this.bitmap.height / 2;
  329.             break;
  330.         default:
  331.             throw new Error('Unknown alignment: ' + alignment);
  332.     }
  333. };
  334.  
  335. EventText.prototype.update = function()
  336. {
  337.     Sprite_Base.prototype.update.call(this);
  338.     this.setLocation(this.gameEvent.screenX(), this.gameEvent.screenY());
  339. };
  340.  
  341. // x and y are expected to be the center-coords of a tile.
  342. EventText.prototype.setLocation = function(x, y)
  343. {
  344.     this.x = x + this.alignOffset_x + this.extraOffset_x;
  345.     this.y = y + this.alignOffset_y + this.extraOffset_y;
  346. };
  347.  
  348. EventText.prototype.render = function()
  349. {
  350.     this.bitmap.clear();
  351.    
  352.     // (Re)-Initialize bitmap settings
  353.     this.bitmap.fontSize = this._fontSize;
  354.     this.bitmap.textColor = this.fontColor;
  355.     this.bitmap.outlineColor = this.outlineColor;
  356.     this.bitmap.outlineWidth = this._outlineWidth;
  357.     this.bitmap.paintOpacity = this.textOpacity;
  358.     this.bitmap.fontItalic  = this.italic;
  359.    
  360.     this.bitmap.drawText(this.text, 0, 0, this.bitmap.width, this.bitmap.height, 'center');
  361.    
  362.     this.requiresRender = false;
  363. };
  364.  
  365. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  366. // Game Event
  367. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  368. Silv.AddAlias('evText_Game_Event_setupPage', Game_Event.prototype.setupPage);
  369. Game_Event.prototype.setupPage = function()
  370. {
  371.     Silv.Alias.evText_Game_Event_setupPage.apply(this, arguments);
  372.    
  373.     if (!this._erased)
  374.     {
  375.         this.deleteEventTexts();
  376.         this.checkEventText();
  377.     }
  378. };
  379.  
  380. Silv.AddAlias('evText_Game_Event_erase', Game_Event.prototype.erase);
  381. Game_Event.prototype.erase = function()
  382. {
  383.     Silv.Alias.evText_Game_Event_erase.apply(this, arguments);
  384.     this.deleteEventTexts();
  385. };
  386.  
  387. Game_Event.prototype.deleteEventTexts = function()
  388. {
  389.     if (this.silvEvTexts)
  390.     {
  391.         if (SceneManager._scene._spriteset)
  392.         {
  393.             this.silvEvTexts.forEach(function(evText) { SceneManager._scene._spriteset.removeChild(evText); });
  394.         }
  395.         else
  396.         {
  397.             this.silvEvTexts.forEach(function(evText)
  398.             {
  399.                 var arrayIdx = SceneManager._scene.evSpritesToAdd.indexOf(evText);
  400.                 if (arrayIdx > -1) { SceneManager._scene.evSpritesToAdd.splice(arrayIdx, 1); } // delete from array
  401.             });
  402.         }
  403.     }
  404.     this.silvEvTexts = [];
  405. };
  406.  
  407. Game_Event.prototype.checkEventText = function()
  408. {  
  409.     var page = this.page();
  410.     if (!page) { return; }
  411.    
  412.     var newEventText = null;
  413.     var evTextCmdFound = false;
  414.     page.list.forEach(function(cmd)
  415.     {
  416.         if (!evTextCmdFound && (cmd.code === 108)) // 108 = first comment-line, 408 = subsequent comment-lines
  417.         {
  418.             if (cmd.parameters[0].trim().toLowerCase() === '<evtext>') { evTextCmdFound = true; }
  419.         }
  420.         else if (evTextCmdFound && ((cmd.code === 108) || cmd.code === 408))
  421.         {
  422.             var option = (cmd.parameters[0].split(':')[0]).trim().toLowerCase();
  423.             var value = Silv.EvText.GetTextAfter(cmd.parameters[0], ':');
  424.            
  425.             switch(option)
  426.             {
  427.                 case 'text':
  428.                     newEventText = new EventText(this, value);
  429.                     this.silvEvTexts.push(newEventText);
  430.                     break;
  431.                 case 'fontsize':
  432.                     newEventText.fontSize(parseInt(value));
  433.                     break;
  434.                 case 'fontcolor':
  435.                     var fontColor = value.trim();
  436.                     if ((fontColor[0] !== '#') && (fontColor[0] !== 'r')) { fontColor = '#' + fontColor; }
  437.                     newEventText.fontColor = fontColor;
  438.                     break;
  439.                 case 'outlinecolor':
  440.                     var outlineColor = value.trim();
  441.                     if ((outlineColor[0] !== '#') && (outlineColor[0] !== 'r')) { outlineColor = '#' + outlineColor; }
  442.                     newEventText.outlineColor = outlineColor;
  443.                     break;
  444.                 case 'outlinewidth':
  445.                     newEventText.outlineWidth(parseInt(value));
  446.                     break;
  447.                 case 'opacity':
  448.                     newEventText.textOpacity = parseInt(value);
  449.                     break;
  450.                 case 'offset_x':
  451.                     newEventText.extraOffset_x = parseFloat(value);
  452.                     break;
  453.                 case 'offset_y':
  454.                     newEventText.extraOffset_y = parseFloat(value);
  455.                     break;
  456.                 case 'align':
  457.                 case 'alignment':
  458.                     newEventText.setAlignment(value);
  459.                     break;
  460.                 case 'italic':
  461.                     newEventText.italic = (value.trim().toLowerCase() === 'true');
  462.                     break;
  463.                 case 'extrabmp_w':
  464.                 case 'extrabmpwidth':
  465.                     newEventText.extraBmpWidth(parseInt(value));
  466.                     break;
  467.                 case 'extrabmp_h':
  468.                 case 'extrabmpheight':
  469.                     newEventText.extraBmpHeight(parseInt(value));
  470.                     break;
  471.                 case '</evtext>':
  472.                     newEventText.render();
  473.                     evTextCmdFound = false; // This line is not required but nice to have in case of 'code-expansion' later
  474.                     return;
  475.                 default:
  476.                     throw new Error('Empty line or unknown command for line: ' + cmd.parameters[0]);
  477.             }
  478.         }
  479.     }, this);
  480. };
  481.  
  482. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  483. // #Plugin Command
  484. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  485. Silv.AddAlias('evText_Game_Interpreter_pluginCommand', Game_Interpreter.prototype.pluginCommand);
  486. Game_Interpreter.prototype.pluginCommand = function(command, args)
  487. {
  488.     Silv.Alias.evText_Game_Interpreter_pluginCommand.call(this, command, args);
  489.     if (command.toLowerCase() === 'eventtext') { Silv.EvText.pluginCommand(command, args); }
  490. };
  491.  
  492. Silv.EvText.pluginCommand = function(cmd, args)
  493. {
  494.     switch(args[0].toLowerCase())
  495.     {
  496.         case 'hideall':
  497.             Silv.EvText.setAllEventTextVisibility(false);
  498.             break;
  499.         case 'showall':
  500.             Silv.EvText.setAllEventTextVisibility(true);
  501.             break;
  502.         default:
  503.             console.log('Plugin arguments:');
  504.             console.log(args);
  505.             throw new Error('Unknown plugin command: ' + cmd);
  506.     }
  507. };
  508.  
  509. Silv.EvText.setAllEventTextVisibility = function(visible)
  510. {
  511.     var allEvents = $gameMap.events();
  512.     allEvents.forEach(function(event)
  513.     {
  514.         event.silvEvTexts.forEach(function(evText)
  515.         {
  516.             (visible) ? evText.opacity = 255 : evText.opacity = 0;
  517.             // evText.visible = visible; // For some reason this line does not work
  518.         });
  519.     });
  520. };
  521.  
  522. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  523. // This is the end of this awesome script!
  524. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement