Advertisement
Guest User

Untitled

a guest
Dec 25th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // SilvEventText.js
  3. // Version: 1.01
  4. //=============================================================================
  5. /*:
  6.  * @plugindesc v1.01 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.  * Event Comment (NOT the event-notetag):
  67.  *--------------------------------------
  68.  * The comment must start with: <evtext>
  69.  * Nothing is case-sensitive here
  70.  *
  71.  * Supported tags:
  72.  * Text: value            // << this tag must be the first tag!
  73.  * FontSize: value
  74.  * FontColor: value       // Supported format examples: #FFFFFF, FFFFFF, rgba(0, 0, 0, 0.5)
  75.  * OutlineColor: value    // Supported format examples: #FFFFFF, FFFFFF, rgba(0, 0, 0, 0.5)
  76.  * OutlineWidth: value
  77.  * Offset_x: value
  78.  * Offset_y: value
  79.  * Opacity: value         // 0-255
  80.  * Align: value           // Supported values: topleft, topcenter, topright, center
  81.  * Alignment: value       // same as align-tag
  82.  * Italic: value          // true/false
  83.  * ExtraBmpWidth: value   // handy for thick outlineWidth's (and because RPG Maker MV fails)
  84.  * ExtraBmp_W: value      // Same as the above
  85.  * ExtraBmpHeight: value  // handy for thick outlineWidth's (and because RPG Maker MV fails)
  86.  * ExtraBmp_H: value      // Same as the above
  87.  * </evtext>              // without this tag the text will not be drawn. So don't forget it.
  88.  *
  89.  *
  90.  * Example comment:
  91.  * <evText>
  92.  * Text:Well Hello There!
  93.  * FontSize: 30
  94.  * FontColor: FF0000
  95.  * Italic: true
  96.  * </evText>
  97.  *
  98.  *--------------------------------------
  99.  * Dev notes (ONLY for other developers):
  100.  *--------------------------------------
  101.  * - The Game Events maintain a reference to the EventTexts using this variable: this.silvEvTexts[]
  102.  * - To change an event's fontsize for example (after it's been created):
  103.  * this.silvEvTexts[0].fontSize = 40;
  104.  * this.silvEvTexts[0].render();
  105.  *
  106.  *--------------------------------------
  107.  * Version History:
  108.  *--------------------------------------
  109.  * v.1.01 (25 December 2015)
  110.  * - Added 2 new parameters and 2 new commands.
  111.  * - Applied my new coding standards.
  112.  * - Fixed a crash.
  113.  * - EventTexts are now deleted when the events themselves are erased.
  114.  *
  115.  * v.1.00 (24 December 2015)
  116.  * - First Release
  117.  *
  118.  */
  119. // Imported
  120. var Imported = Imported || {};
  121. Imported.SILV_EventText = 1.01;
  122.  
  123. // #Parameters
  124. var Silv = Silv || {};
  125. Silv.EvText = Silv.EvText || {};
  126. Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilvEventText>'); })[0].parameters;
  127. // Default Values
  128. Silv.EvText.DefaultFontSize = parseInt(Silv.Parameters['Default Font Size']);
  129. Silv.EvText.DefaultFontColor = Silv.Parameters['Default Font Color'];
  130. Silv.EvText.DefaultOutlineColor = Silv.Parameters['Default Outline Color'];
  131. Silv.EvText.DefaultOutlineWidth =  parseInt(Silv.Parameters['Default Outline Width']);
  132. Silv.EvText.DefaultTextAlign = Silv.Parameters['Default Text Alignment'];
  133. Silv.EvText.DefaultOpacity = parseInt(Silv.Parameters['Default Opacity']);
  134. Silv.EvText.DefaultItalic = parseInt(Silv.Parameters['Default Italic']);
  135. Silv.EvText.DefaultOffset_X = parseFloat(Silv.Parameters['Default Offset X']);
  136. Silv.EvText.DefaultOffset_Y = parseFloat(Silv.Parameters['Default Offset Y']);
  137. Silv.EvText.DefaultExtraBmpWidth = parseInt(Silv.Parameters['Default Extra Bitmap Width']);
  138. Silv.EvText.DefaultExtraBmpHeight = parseInt(Silv.Parameters['Default Extra Bitmap Height']);
  139.  
  140. // Alias
  141. Silv.Alias = Silv.Alias || {};
  142. if (!Silv.AddAlias)
  143. {
  144.     Silv.AddAlias = function(alias, original_method)
  145.     {
  146.         if (Silv.Alias[alias]) { throw new Error('Alias already exists: ' + alias); }
  147.         Silv.Alias[alias] = original_method;
  148.     };
  149. }
  150.  
  151. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  152. // Utilities
  153. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  154. Silv.EvText.GetTextAfter = function(str, character)
  155. {
  156.     return str.substr(str.indexOf(character) + 1);
  157. };
  158.  
  159. Silv.EvText.MeasureBmp = new Bitmap(1, 1);
  160.  
  161. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  162. // Scene Map
  163. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  164. Silv.AddAlias('evText_Scene_Map_initialize', Scene_Map.prototype.initialize);
  165. Scene_Map.prototype.initialize = function()
  166. {
  167.     Silv.Alias.evText_Scene_Map_initialize.apply(this, arguments);
  168.     this.evSpritesToAdd = [];
  169. };
  170.  
  171. Silv.AddAlias('evText_Scene_Map_createSpriteset', Scene_Map.prototype.createSpriteset);
  172. Scene_Map.prototype.createSpriteset = function()
  173. {
  174.     Silv.Alias.evText_Scene_Map_createSpriteset.apply(this, arguments);
  175.    
  176.     this.evSpritesToAdd.forEach(function(sprite)
  177.     {
  178.         this._spriteset.addChild(sprite);
  179.     }, this);
  180. };
  181.  
  182. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  183. // Event Text
  184. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  185. function EventText() { this.initialize.apply(this, arguments); }
  186.  
  187. EventText.prototype = Object.create(Sprite_Base.prototype);
  188. EventText.prototype.constructor = EventText;
  189.  
  190. EventText.prototype.initialize = function(gameEvent, text)
  191. {
  192.     Sprite_Base.prototype.initialize.call(this);
  193.     this.gameEvent = gameEvent;
  194.    
  195.     this.setDefaults();
  196.     this.setText(text);
  197.    
  198.     if (SceneManager._scene._spriteset)
  199.     {
  200.         SceneManager._scene._spriteset.addChild(this); // SceneManager._scene can be null when the map was just loaded
  201.     }
  202.     else
  203.     {
  204.         SceneManager._scene.evSpritesToAdd.push(this); // if it's null, just push it to a temporary list instead
  205.     }
  206. };
  207.  
  208. EventText.prototype.setDefaults = function()
  209. {
  210.     this._fontSize = Silv.EvText.DefaultFontSize;
  211.     this.fontColor = Silv.EvText.DefaultFontColor;
  212.     this.outlineColor = Silv.EvText.DefaultOutlineColor;
  213.     this._outlineWidth = Silv.EvText.DefaultOutlineWidth;
  214.     this.textOpacity = Silv.EvText.DefaultOpacity;
  215.     this.italic = Silv.EvText.DefaultItalic;
  216.     this.extraOffset_x = Silv.EvText.DefaultOffset_X;
  217.     this.extraOffset_y = Silv.EvText.DefaultOffset_Y;
  218.     this._extraBmpWidth = Silv.EvText.DefaultExtraBmpWidth;
  219.     this._extraBmpHeight = Silv.EvText.DefaultExtraBmpHeight;
  220. };
  221.  
  222. EventText.prototype.extraBmpWidth = function(value)
  223. {
  224.     this._extraBmpWidth = value;
  225.     this.setSizePropertyAfter();
  226. };
  227.  
  228. EventText.prototype.extraBmpHeight = function(value)
  229. {
  230.     this._extraBmpHeight = value;
  231.     this.setSizePropertyAfter();
  232. };
  233.  
  234. EventText.prototype.fontSize = function(value)
  235. {
  236.     this._fontSize = value;
  237.     this.setSizePropertyAfter();
  238. };
  239.  
  240. EventText.prototype.outlineWidth = function(value)
  241. {
  242.     this._outlineWidth = value;
  243.     this.setSizePropertyAfter();
  244. };
  245.  
  246. // 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
  247. EventText.prototype.setSizePropertyAfter = function()
  248. {
  249.     this.createBitmap();
  250.     this.setAlignment(this.alignment);
  251.     if (!this.requiresRender) { this.render(); }
  252. };
  253.  
  254. EventText.prototype.createBitmap = function(text)
  255. {
  256.     Silv.EvText.MeasureBmp.fontSize = this._fontSize;
  257.     Silv.EvText.MeasureBmp.outlineWidth = this._outlineWidth;
  258.     Silv.EvText.MeasureBmp.fontItalic = this.italic;
  259.     this.bitmap = new Bitmap(Silv.EvText.MeasureBmp.measureTextWidth(text) + this._extraBmpWidth, this._fontSize + this._extraBmpHeight);
  260. };
  261.  
  262. EventText.prototype.setText = function(text)
  263. {
  264.     this.createBitmap();
  265.     this.text = text;
  266.     this.setAlignment(Silv.EvText.DefaultTextAlign);
  267.     this.requiresRender = true;
  268. };
  269.  
  270. // alignment: topLeft, topCenter, topRight, center
  271. EventText.prototype.setAlignment = function(alignment)
  272. {  
  273.     if (!this.text) { throw new Error('setAlignment() requires the text to be set first.'); }
  274.     this.alignment = alignment;
  275.    
  276.     switch(alignment.toLowerCase())
  277.     {  
  278.         case 'topleft':
  279.             this.alignOffset_x = -$gameMap.tileWidth() / 2;
  280.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  281.             break;
  282.         case 'topcenter':
  283.             this.alignOffset_x = -this.bitmap.width / 2;
  284.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  285.             break;
  286.         case 'topright':
  287.             this.alignOffset_x = +$gameMap.tileWidth() / 3 - this.bitmap.width;
  288.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  289.             break;
  290.         case 'center':
  291.             this.alignOffset_x = -this.bitmap.width / 2;
  292.             this.alignOffset_y = -$gameMap.tileHeight() / 2 - this.bitmap.height / 2;
  293.             break;
  294.         default:
  295.             throw new Error('Unknown alignment: ' + alignment);
  296.     }
  297. };
  298.  
  299. EventText.prototype.update = function()
  300. {
  301.     Sprite_Base.prototype.update.call(this);
  302.     this.setLocation(this.gameEvent.screenX(), this.gameEvent.screenY());
  303. };
  304.  
  305. // x and y are expected to be the center-coords of a tile.
  306. EventText.prototype.setLocation = function(x, y)
  307. {
  308.     this.x = x + this.alignOffset_x + this.extraOffset_x;
  309.     this.y = y + this.alignOffset_y + this.extraOffset_y;
  310. };
  311.  
  312. EventText.prototype.render = function()
  313. {
  314.     this.bitmap.clear();
  315.    
  316.     // (Re)-Initialize bitmap settings
  317.     this.bitmap.fontSize = this._fontSize;
  318.     this.bitmap.textColor = this.fontColor;
  319.     this.bitmap.outlineColor = this.outlineColor;
  320.     this.bitmap.outlineWidth = this._outlineWidth;
  321.     this.bitmap.paintOpacity = this.textOpacity;
  322.     this.bitmap.fontItalic  = this.italic;
  323.    
  324.     this.bitmap.drawText(this.text, 0, 0, this.bitmap.width, this.bitmap.height, 'center');
  325.    
  326.     this.requiresRender = false;
  327. };
  328.  
  329. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  330. // Game Event
  331. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  332. Silv.AddAlias('evText_Game_Event_setupPage', Game_Event.prototype.setupPage);
  333. Game_Event.prototype.setupPage = function()
  334. {
  335.     Silv.Alias.evText_Game_Event_setupPage.apply(this, arguments);
  336.    
  337.     if (!this._erased)
  338.     {
  339.         this.deleteEventTexts();
  340.         this.checkEventText();
  341.     }
  342. };
  343.  
  344. Silv.AddAlias('evText_Game_Event_erase', Game_Event.prototype.erase);
  345. Game_Event.prototype.erase = function()
  346. {
  347.     Silv.Alias.evText_Game_Event_erase.apply(this, arguments);
  348.     this.deleteEventTexts();
  349. };
  350.  
  351. Game_Event.prototype.deleteEventTexts = function()
  352. {
  353.     if (this.silvEvTexts)
  354.     {
  355.         if (SceneManager._scene._spriteset)
  356.         {
  357.             this.silvEvTexts.forEach(function(evText) { SceneManager._scene._spriteset.removeChild(evText); });
  358.         }
  359.         else
  360.         {
  361.             this.silvEvTexts.forEach(function(evText)
  362.             {
  363.                 var arrayIdx = SceneManager._scene.evSpritesToAdd.indexOf(evText);
  364.                 if (arrayIdx > -1) { SceneManager._scene.evSpritesToAdd.splice(arrayIdx, 1); } // delete from array
  365.             });
  366.         }
  367.     }
  368.     this.silvEvTexts = [];
  369. };
  370.  
  371. Game_Event.prototype.checkEventText = function()
  372. {  
  373.     var page = this.page();
  374.     if (!page) { return; }
  375.    
  376.     var newEventText = null;
  377.     var evTextCmdFound = false;
  378.     page.list.forEach(function(cmd)
  379.     {
  380.         if (!evTextCmdFound && (cmd.code === 108)) // 108 = first comment-line, 408 = subsequent comment-lines
  381.         {
  382.             if (cmd.parameters[0].trim().toLowerCase() === '<evtext>') { evTextCmdFound = true; }
  383.         }
  384.         else if (evTextCmdFound && ((cmd.code === 108) || cmd.code === 408))
  385.         {
  386.             var option = (cmd.parameters[0].split(':')[0]).trim().toLowerCase();
  387.             var value = Silv.EvText.GetTextAfter(cmd.parameters[0], ':');
  388.            
  389.             switch(option)
  390.             {
  391.                 case 'text':
  392.                     newEventText = new EventText(this, value);
  393.                     this.silvEvTexts.push(newEventText);
  394.                     break;
  395.                 case 'fontsize':
  396.                     newEventText.fontSize(parseInt(value));
  397.                     break;
  398.                 case 'fontcolor':
  399.                     var fontColor = value.trim();
  400.                     if ((fontColor[0] !== '#') && (fontColor[0] !== 'r')) { fontColor = '#' + fontColor; }
  401.                     newEventText.fontColor = fontColor;
  402.                     break;
  403.                 case 'outlinecolor':
  404.                     var outlineColor = value.trim();
  405.                     if ((outlineColor[0] !== '#') && (outlineColor[0] !== 'r')) { outlineColor = '#' + outlineColor; }
  406.                     newEventText.outlineColor = outlineColor;
  407.                     break;
  408.                 case 'outlinewidth':
  409.                     newEventText.outlineWidth(parseInt(value));
  410.                     break;
  411.                 case 'opacity':
  412.                     newEventText.textOpacity = parseInt(value);
  413.                     break;
  414.                 case 'offset_x':
  415.                     newEventText.extraOffset_x = parseFloat(value);
  416.                     break;
  417.                 case 'offset_y':
  418.                     newEventText.extraOffset_y = parseFloat(value);
  419.                     break;
  420.                 case 'align':
  421.                 case 'alignment':
  422.                     newEventText.setAlignment(value);
  423.                     break;
  424.                 case 'italic':
  425.                     newEventText.italic = (value.trim().toLowerCase() === 'true');
  426.                     break;
  427.                 case 'extrabmp_w':
  428.                 case 'extrabmpwidth':
  429.                     newEventText.extraBmpWidth(parseInt(value));
  430.                     break;
  431.                 case 'extrabmp_h':
  432.                 case 'extrabmpheight':
  433.                     newEventText.extraBmpHeight(parseInt(value));
  434.                     break;
  435.                 case '</evtext>':
  436.                     newEventText.render();
  437.                     evTextCmdFound = false; // This line is not required but nice to have in case of 'code-expansion' later
  438.                     return;
  439.                 default:
  440.                     throw new Error('Empty line or unknown command for line: ' + cmd.parameters[0]);
  441.             }
  442.         }
  443.     }, this);
  444. };
  445.  
  446. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  447. // This is the end of this awesome script!
  448. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement