Advertisement
Guest User

Untitled

a guest
Dec 25th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // SilvEventText.js
  3. // Version: 1.01a
  4. //=============================================================================
  5. /*:
  6.  * @plugindesc v1.01a 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.01a (25 December 2015)
  116.  * - Added 2 new parameters and 2 new commands.
  117.  * - Applied my new coding standards.
  118.  * - Fixed a crash.
  119.  * - EventTexts are now deleted when the events themselves are erased.
  120.  * - Added 2 new plugin commands.
  121.  *
  122.  * v.1.00 (24 December 2015)
  123.  * - First Release
  124.  *
  125.  */
  126. // Imported
  127. var Imported = Imported || {};
  128. Imported.SILV_EventText = 1.01;
  129.  
  130. // #Parameters
  131. var Silv = Silv || {};
  132. Silv.EvText = Silv.EvText || {};
  133. Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilvEventText>'); })[0].parameters;
  134. // Default Values
  135. Silv.EvText.DefaultFontSize = parseInt(Silv.Parameters['Default Font Size']);
  136. Silv.EvText.DefaultFontColor = Silv.Parameters['Default Font Color'];
  137. Silv.EvText.DefaultOutlineColor = Silv.Parameters['Default Outline Color'];
  138. Silv.EvText.DefaultOutlineWidth =  parseInt(Silv.Parameters['Default Outline Width']);
  139. Silv.EvText.DefaultTextAlign = Silv.Parameters['Default Text Alignment'];
  140. Silv.EvText.DefaultOpacity = parseInt(Silv.Parameters['Default Opacity']);
  141. Silv.EvText.DefaultItalic = parseInt(Silv.Parameters['Default Italic']);
  142. Silv.EvText.DefaultOffset_X = parseFloat(Silv.Parameters['Default Offset X']);
  143. Silv.EvText.DefaultOffset_Y = parseFloat(Silv.Parameters['Default Offset Y']);
  144. Silv.EvText.DefaultExtraBmpWidth = parseInt(Silv.Parameters['Default Extra Bitmap Width']);
  145. Silv.EvText.DefaultExtraBmpHeight = parseInt(Silv.Parameters['Default Extra Bitmap Height']);
  146.  
  147. // Alias
  148. Silv.Alias = Silv.Alias || {};
  149. if (!Silv.AddAlias)
  150. {
  151.     Silv.AddAlias = function(alias, original_method)
  152.     {
  153.         if (Silv.Alias[alias]) { throw new Error('Alias already exists: ' + alias); }
  154.         Silv.Alias[alias] = original_method;
  155.     };
  156. }
  157.  
  158. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  159. // Utilities
  160. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  161. Silv.EvText.GetTextAfter = function(str, character)
  162. {
  163.     return str.substr(str.indexOf(character) + 1);
  164. };
  165.  
  166. Silv.EvText.MeasureBmp = new Bitmap(1, 1);
  167.  
  168. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  169. // Scene Map
  170. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  171. Silv.AddAlias('evText_Scene_Map_initialize', Scene_Map.prototype.initialize);
  172. Scene_Map.prototype.initialize = function()
  173. {
  174.     Silv.Alias.evText_Scene_Map_initialize.apply(this, arguments);
  175.     this.evSpritesToAdd = [];
  176. };
  177.  
  178. Silv.AddAlias('evText_Scene_Map_createSpriteset', Scene_Map.prototype.createSpriteset);
  179. Scene_Map.prototype.createSpriteset = function()
  180. {
  181.     Silv.Alias.evText_Scene_Map_createSpriteset.apply(this, arguments);
  182.    
  183.     this.evSpritesToAdd.forEach(function(sprite)
  184.     {
  185.         this._spriteset.addChild(sprite);
  186.     }, this);
  187. };
  188.  
  189. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  190. // Event Text
  191. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  192. function EventText() { this.initialize.apply(this, arguments); }
  193.  
  194. EventText.prototype = Object.create(Sprite_Base.prototype);
  195. EventText.prototype.constructor = EventText;
  196.  
  197. EventText.prototype.initialize = function(gameEvent, text)
  198. {
  199.     Sprite_Base.prototype.initialize.call(this);
  200.     this.gameEvent = gameEvent;
  201.    
  202.     this.setDefaults();
  203.     this.setText(text);
  204.    
  205.     if (SceneManager._scene._spriteset)
  206.     {
  207.         SceneManager._scene._spriteset.addChild(this); // SceneManager._scene can be null when the map was just loaded
  208.     }
  209.     else
  210.     {
  211.         SceneManager._scene.evSpritesToAdd.push(this); // if it's null, just push it to a temporary list instead
  212.     }
  213. };
  214.  
  215. EventText.prototype.setDefaults = function()
  216. {
  217.     this._fontSize = Silv.EvText.DefaultFontSize;
  218.     this.fontColor = Silv.EvText.DefaultFontColor;
  219.     this.outlineColor = Silv.EvText.DefaultOutlineColor;
  220.     this._outlineWidth = Silv.EvText.DefaultOutlineWidth;
  221.     this.textOpacity = Silv.EvText.DefaultOpacity;
  222.     this.italic = Silv.EvText.DefaultItalic;
  223.     this.extraOffset_x = Silv.EvText.DefaultOffset_X;
  224.     this.extraOffset_y = Silv.EvText.DefaultOffset_Y;
  225.     this._extraBmpWidth = Silv.EvText.DefaultExtraBmpWidth;
  226.     this._extraBmpHeight = Silv.EvText.DefaultExtraBmpHeight;
  227. };
  228.  
  229. EventText.prototype.extraBmpWidth = function(value)
  230. {
  231.     this._extraBmpWidth = value;
  232.     this.setSizePropertyAfter();
  233. };
  234.  
  235. EventText.prototype.extraBmpHeight = function(value)
  236. {
  237.     this._extraBmpHeight = value;
  238.     this.setSizePropertyAfter();
  239. };
  240.  
  241. EventText.prototype.fontSize = function(value)
  242. {
  243.     this._fontSize = value;
  244.     this.setSizePropertyAfter();
  245. };
  246.  
  247. EventText.prototype.outlineWidth = function(value)
  248. {
  249.     this._outlineWidth = value;
  250.     this.setSizePropertyAfter();
  251. };
  252.  
  253. // 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
  254. EventText.prototype.setSizePropertyAfter = function()
  255. {
  256.     this.createBitmap();
  257.     this.setAlignment(this.alignment);
  258.     if (!this.requiresRender) { this.render(); }
  259. };
  260.  
  261. EventText.prototype.createBitmap = function(text)
  262. {
  263.     Silv.EvText.MeasureBmp.fontSize = this._fontSize;
  264.     Silv.EvText.MeasureBmp.outlineWidth = this._outlineWidth;
  265.     Silv.EvText.MeasureBmp.fontItalic = this.italic;
  266.     this.bitmap = new Bitmap(Silv.EvText.MeasureBmp.measureTextWidth(text) + this._extraBmpWidth, this._fontSize + this._extraBmpHeight);
  267. };
  268.  
  269. EventText.prototype.setText = function(text)
  270. {
  271.     this.createBitmap();
  272.     this.text = text;
  273.     this.setAlignment(Silv.EvText.DefaultTextAlign);
  274.     this.requiresRender = true;
  275. };
  276.  
  277. // alignment: topLeft, topCenter, topRight, center
  278. EventText.prototype.setAlignment = function(alignment)
  279. {  
  280.     if (!this.text) { throw new Error('setAlignment() requires the text to be set first.'); }
  281.     this.alignment = alignment;
  282.    
  283.     switch(alignment.toLowerCase())
  284.     {  
  285.         case 'topleft':
  286.             this.alignOffset_x = -$gameMap.tileWidth() / 2;
  287.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  288.             break;
  289.         case 'topcenter':
  290.             this.alignOffset_x = -this.bitmap.width / 2;
  291.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  292.             break;
  293.         case 'topright':
  294.             this.alignOffset_x = +$gameMap.tileWidth() / 3 - this.bitmap.width;
  295.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  296.             break;
  297.         case 'center':
  298.             this.alignOffset_x = -this.bitmap.width / 2;
  299.             this.alignOffset_y = -$gameMap.tileHeight() / 2 - this.bitmap.height / 2;
  300.             break;
  301.         default:
  302.             throw new Error('Unknown alignment: ' + alignment);
  303.     }
  304. };
  305.  
  306. EventText.prototype.update = function()
  307. {
  308.     Sprite_Base.prototype.update.call(this);
  309.     this.setLocation(this.gameEvent.screenX(), this.gameEvent.screenY());
  310. };
  311.  
  312. // x and y are expected to be the center-coords of a tile.
  313. EventText.prototype.setLocation = function(x, y)
  314. {
  315.     this.x = x + this.alignOffset_x + this.extraOffset_x;
  316.     this.y = y + this.alignOffset_y + this.extraOffset_y;
  317. };
  318.  
  319. EventText.prototype.render = function()
  320. {
  321.     this.bitmap.clear();
  322.    
  323.     // (Re)-Initialize bitmap settings
  324.     this.bitmap.fontSize = this._fontSize;
  325.     this.bitmap.textColor = this.fontColor;
  326.     this.bitmap.outlineColor = this.outlineColor;
  327.     this.bitmap.outlineWidth = this._outlineWidth;
  328.     this.bitmap.paintOpacity = this.textOpacity;
  329.     this.bitmap.fontItalic  = this.italic;
  330.    
  331.     this.bitmap.drawText(this.text, 0, 0, this.bitmap.width, this.bitmap.height, 'center');
  332.    
  333.     this.requiresRender = false;
  334. };
  335.  
  336. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  337. // Game Event
  338. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  339. Silv.AddAlias('evText_Game_Event_setupPage', Game_Event.prototype.setupPage);
  340. Game_Event.prototype.setupPage = function()
  341. {
  342.     Silv.Alias.evText_Game_Event_setupPage.apply(this, arguments);
  343.    
  344.     if (!this._erased)
  345.     {
  346.         this.deleteEventTexts();
  347.         this.checkEventText();
  348.     }
  349. };
  350.  
  351. Silv.AddAlias('evText_Game_Event_erase', Game_Event.prototype.erase);
  352. Game_Event.prototype.erase = function()
  353. {
  354.     Silv.Alias.evText_Game_Event_erase.apply(this, arguments);
  355.     this.deleteEventTexts();
  356. };
  357.  
  358. Game_Event.prototype.deleteEventTexts = function()
  359. {
  360.     if (this.silvEvTexts)
  361.     {
  362.         if (SceneManager._scene._spriteset)
  363.         {
  364.             this.silvEvTexts.forEach(function(evText) { SceneManager._scene._spriteset.removeChild(evText); });
  365.         }
  366.         else
  367.         {
  368.             this.silvEvTexts.forEach(function(evText)
  369.             {
  370.                 var arrayIdx = SceneManager._scene.evSpritesToAdd.indexOf(evText);
  371.                 if (arrayIdx > -1) { SceneManager._scene.evSpritesToAdd.splice(arrayIdx, 1); } // delete from array
  372.             });
  373.         }
  374.     }
  375.     this.silvEvTexts = [];
  376. };
  377.  
  378. Game_Event.prototype.checkEventText = function()
  379. {  
  380.     var page = this.page();
  381.     if (!page) { return; }
  382.    
  383.     var newEventText = null;
  384.     var evTextCmdFound = false;
  385.     page.list.forEach(function(cmd)
  386.     {
  387.         if (!evTextCmdFound && (cmd.code === 108)) // 108 = first comment-line, 408 = subsequent comment-lines
  388.         {
  389.             if (cmd.parameters[0].trim().toLowerCase() === '<evtext>') { evTextCmdFound = true; }
  390.         }
  391.         else if (evTextCmdFound && ((cmd.code === 108) || cmd.code === 408))
  392.         {
  393.             var option = (cmd.parameters[0].split(':')[0]).trim().toLowerCase();
  394.             var value = Silv.EvText.GetTextAfter(cmd.parameters[0], ':');
  395.            
  396.             switch(option)
  397.             {
  398.                 case 'text':
  399.                     newEventText = new EventText(this, value);
  400.                     this.silvEvTexts.push(newEventText);
  401.                     break;
  402.                 case 'fontsize':
  403.                     newEventText.fontSize(parseInt(value));
  404.                     break;
  405.                 case 'fontcolor':
  406.                     var fontColor = value.trim();
  407.                     if ((fontColor[0] !== '#') && (fontColor[0] !== 'r')) { fontColor = '#' + fontColor; }
  408.                     newEventText.fontColor = fontColor;
  409.                     break;
  410.                 case 'outlinecolor':
  411.                     var outlineColor = value.trim();
  412.                     if ((outlineColor[0] !== '#') && (outlineColor[0] !== 'r')) { outlineColor = '#' + outlineColor; }
  413.                     newEventText.outlineColor = outlineColor;
  414.                     break;
  415.                 case 'outlinewidth':
  416.                     newEventText.outlineWidth(parseInt(value));
  417.                     break;
  418.                 case 'opacity':
  419.                     newEventText.textOpacity = parseInt(value);
  420.                     break;
  421.                 case 'offset_x':
  422.                     newEventText.extraOffset_x = parseFloat(value);
  423.                     break;
  424.                 case 'offset_y':
  425.                     newEventText.extraOffset_y = parseFloat(value);
  426.                     break;
  427.                 case 'align':
  428.                 case 'alignment':
  429.                     newEventText.setAlignment(value);
  430.                     break;
  431.                 case 'italic':
  432.                     newEventText.italic = (value.trim().toLowerCase() === 'true');
  433.                     break;
  434.                 case 'extrabmp_w':
  435.                 case 'extrabmpwidth':
  436.                     newEventText.extraBmpWidth(parseInt(value));
  437.                     break;
  438.                 case 'extrabmp_h':
  439.                 case 'extrabmpheight':
  440.                     newEventText.extraBmpHeight(parseInt(value));
  441.                     break;
  442.                 case '</evtext>':
  443.                     newEventText.render();
  444.                     evTextCmdFound = false; // This line is not required but nice to have in case of 'code-expansion' later
  445.                     return;
  446.                 default:
  447.                     throw new Error('Empty line or unknown command for line: ' + cmd.parameters[0]);
  448.             }
  449.         }
  450.     }, this);
  451. };
  452.  
  453. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  454. // #Plugin Command
  455. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  456. Silv.AddAlias('evText_Game_Interpreter_pluginCommand', Game_Interpreter.prototype.pluginCommand);
  457. Game_Interpreter.prototype.pluginCommand = function(command, args)
  458. {
  459.     Silv.Alias.evText_Game_Interpreter_pluginCommand.call(this, command, args);
  460.     if (command.toLowerCase() == 'eventtext') { Silv.EvText.pluginCommand(command, args); }
  461. };
  462.  
  463. Silv.EvText.pluginCommand = function(cmd, args)
  464. {
  465.     switch(args[0].toLowerCase())
  466.     {
  467.         case 'hideall':
  468.             Silv.EvText.setAllEventTextVisibility(false);
  469.             break;
  470.         case 'showall':
  471.             Silv.EvText.setAllEventTextVisibility(true);
  472.             break;
  473.         default:
  474.             console.log('Plugin arguments:');
  475.             console.log(args);
  476.             throw new Error('Unknown plugin command: ' + cmd);
  477.     }
  478. };
  479.  
  480. Silv.EvText.setAllEventTextVisibility = function(visible)
  481. {
  482.     var allEvents = $gameMap.events();
  483.     allEvents.forEach(function(event)
  484.     {
  485.         event.silvEvTexts.forEach(function(evText)
  486.         {
  487.             (visible) ? evText.opacity = 255 : evText.opacity = 0;
  488.             // evText.visible = visible; // For some reason this line does not work
  489.         });
  490.     });
  491. };
  492.  
  493. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  494. // This is the end of this awesome script!
  495. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement