Advertisement
Guest User

Untitled

a guest
Dec 24th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // SilvEventText.js
  3. // Version: 1.00
  4. //=============================================================================
  5. /*:
  6.  * @plugindesc v1.00 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.  * @help
  48.  *--------------------------------------
  49.  * Notes
  50.  *--------------------------------------
  51.  * - 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
  52.  * - 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.
  53.  * - This plugin does not have 'jittering-text'
  54.  * - Events can have multiple event-texts.
  55.  * - Switching the event-page will clear the EventText(s) from the old page.
  56.  *
  57.  *--------------------------------------
  58.  * Event Comment (NOT the event-notetag):
  59.  *--------------------------------------
  60.  * The comment must start with: <evtext>
  61.  * Nothing is case-sensitive here
  62.  *
  63.  * Supported tags:
  64.  * Text: value          // << this tag must be the first tag!
  65.  * FontSize: value
  66.  * FontColor: value     // Supported format examples: #FFFFFF, FFFFFF, rgba(0, 0, 0, 0.5)
  67.  * OutlineColor: value  // Supported format examples: #FFFFFF, FFFFFF, rgba(0, 0, 0, 0.5)
  68.  * OutlineWidth: value
  69.  * offset_x: value
  70.  * offset_y: value
  71.  * Opacity: value       // 0-255
  72.  * align: value         // Supported values: topleft, topcenter, topright, center
  73.  * alignment: value     // same as align-tag
  74.  * italic: value        // true/false
  75.  * </evtext>            // without this tag the text will not be drawn. So don't forget it.
  76.  *
  77.  *
  78.  * Example comment:
  79.  * <evText>
  80.  * Text:Well Hello There!
  81.  * FontSize: 30
  82.  * FontColor: FF0000
  83.  * Italic: true
  84.  * </evText>
  85.  *
  86.  *--------------------------------------
  87.  * Dev notes (ONLY for other developers):
  88.  *--------------------------------------
  89.  * - The Game Events maintain a reference to the EventTexts using this variable: this.silvEvTexts[]
  90.  * - To change an event's fontsize for example (after it's been created):
  91.  * this.silvEvTexts[0].fontSize = 40;
  92.  * this.silvEvTexts[0].render();
  93.  *
  94.  *--------------------------------------
  95.  * Version History:
  96.  *--------------------------------------
  97.  * v.1.00 (24 December 2015)
  98.  * - First Release
  99.  *
  100.  */
  101. // Imported
  102. var Imported = Imported || {};
  103. Imported.SILV_EventText = 1.00;
  104.  
  105. // #Parameters
  106. var Silv = Silv || {};
  107. Silv.EvText = Silv.EvText || {};
  108. Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilvEventText>'); })[0].parameters;
  109. // Default Values
  110. Silv.EvText.DefaultFontSize = parseInt(Silv.Parameters['Default Font Size']);
  111. Silv.EvText.DefaultFontColor = Silv.Parameters['Default Font Color'];
  112. Silv.EvText.DefaultOutlineColor = Silv.Parameters['Default Outline Color'];
  113. Silv.EvText.DefaultOutlineWidth =  parseInt(Silv.Parameters['Default Outline Width']);
  114. Silv.EvText.DefaultTextAlign = Silv.Parameters['Default Text Alignment'];
  115. Silv.EvText.DefaultOpacity = parseInt(Silv.Parameters['Default Opacity']);
  116. Silv.EvText.DefaultItalic = parseInt(Silv.Parameters['Default Italic']);
  117. Silv.EvText.DefaultOffset_X = parseFloat(Silv.Parameters['Default Offset X']);
  118. Silv.EvText.DefaultOffset_Y = parseFloat(Silv.Parameters['Default Offset Y']);
  119.  
  120. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  121. // Utilities
  122. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  123. Silv.EvText.GetTextAfter = function(str, character)
  124. {
  125.     return str.substr(str.indexOf(character) + 1);
  126. };
  127.  
  128. Silv.EvText.MeasureBmp = new Bitmap(1, 1);
  129.  
  130. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  131. // Scene Map
  132. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. var alias_silv_evText_Scene_Map_initialize = Scene_Map.prototype.initialize;
  134. Scene_Map.prototype.initialize = function()
  135. {
  136.     alias_silv_evText_Scene_Map_initialize.apply(this, arguments);
  137.     this.evSpritesToAdd = [];
  138. };
  139.  
  140. var alias_silv_evText_Scene_Map_createSpriteset = Scene_Map.prototype.createSpriteset;
  141. Scene_Map.prototype.createSpriteset = function()
  142. {
  143.     alias_silv_evText_Scene_Map_createSpriteset.apply(this, arguments);
  144.    
  145.     this.evSpritesToAdd.forEach(function(sprite)
  146.     {
  147.         this._spriteset.addChild(sprite);
  148.     }, this);
  149. };
  150.  
  151. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  152. // Event Text
  153. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  154. function EventText() { this.initialize.apply(this, arguments); }
  155.  
  156. EventText.prototype = Object.create(Sprite_Base.prototype);
  157. EventText.prototype.constructor = EventText;
  158.  
  159. EventText.prototype.initialize = function(gameEvent, text)
  160. {
  161.     Sprite_Base.prototype.initialize.call(this);
  162.     this.gameEvent = gameEvent;
  163.    
  164.     this.setDefaults();
  165.     this.setText(text);
  166.    
  167.     if (SceneManager._scene._spriteset)
  168.     {
  169.         SceneManager._scene._spriteset.addChild(this); // SceneManager._scene can be null when the map was just loaded
  170.     }
  171.     else
  172.     {
  173.         SceneManager._scene.evSpritesToAdd.push(this); // if it's null, just push it to a temporary list instead
  174.     }
  175. };
  176.  
  177. EventText.prototype.setDefaults = function()
  178. {
  179.     this.fontSize = Silv.EvText.DefaultFontSize;
  180.     this.fontColor = Silv.EvText.DefaultFontColor;
  181.     this.outlineColor = Silv.EvText.DefaultOutlineColor;
  182.     this.outlineWidth = Silv.EvText.DefaultOutlineWidth;
  183.     this.textOpacity = Silv.EvText.DefaultOpacity;
  184.     this.italic = Silv.EvText.DefaultItalic;
  185.     this.extraOffset_x = Silv.EvText.DefaultOffset_X;
  186.     this.extraOffset_y = Silv.EvText.DefaultOffset_Y;
  187. };
  188.  
  189. // Call this method instead of setting the this.fontSize directly to change this EventText AFTER it was already fully created through a notetag. The note-tag code must use this.fontSize directly.
  190. EventText.prototype.setFontSize = function(newFontSize)
  191. {
  192.     this.fontSize = newFontSize;
  193.     this.setSizePropertyAfter();
  194. };
  195.  
  196. // Call this method instead of setting the this.outlineWidth directly to change this EventText AFTER it was already fully created through a notetag. The note-tag code must use this.outlineWidth directly.
  197. EventText.prototype.setOutlineWidth = function(outlineWidth)
  198. {
  199.     this.outlineWidth = outlineWidth;
  200.     this.setSizePropertyAfter();
  201. };
  202.  
  203. // 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
  204. EventText.prototype.setSizePropertyAfter = function()
  205. {
  206.     this.createBitmap();
  207.     this.setAlignment(this.alignment);
  208.     if (!this.requiresRender) { this.render(); }
  209. };
  210.  
  211. EventText.prototype.createBitmap = function(text)
  212. {
  213.     Silv.EvText.MeasureBmp.fontSize = this.fontSize;
  214.     Silv.EvText.MeasureBmp.outlinewidth = this.outlinewidth;
  215.     Silv.EvText.MeasureBmp.fontItalic = this.italic;
  216.    
  217.     this.bitmap = new Bitmap(Silv.EvText.MeasureBmp.measureTextWidth(text), this.fontSize + this.outlineWidth * 2);
  218. };
  219.  
  220. EventText.prototype.setText = function(text)
  221. {
  222.     this.createBitmap();
  223.     this.text = text;
  224.     this.setAlignment(Silv.EvText.DefaultTextAlign);
  225.     this.requiresRender = true;
  226. };
  227.  
  228. // alignment: topLeft, topCenter, topRight, center
  229. EventText.prototype.setAlignment = function(alignment)
  230. {  
  231.     if (!this.text) { throw new Error('setAlignment() requires the text to be set first.'); }
  232.     this.alignment = alignment;
  233.    
  234.     switch(alignment.toLowerCase())
  235.     {  
  236.         case 'topleft':
  237.             this.alignOffset_x = -$gameMap.tileWidth() / 2;
  238.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  239.             break;
  240.         case 'topcenter':
  241.             this.alignOffset_x = -this.bitmap.width / 2;
  242.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  243.             break;
  244.         case 'topright':
  245.             this.alignOffset_x = +$gameMap.tileWidth() / 3 - this.bitmap.width;
  246.             this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
  247.             break;
  248.         case 'center':
  249.             this.alignOffset_x = -this.bitmap.width / 2;
  250.             this.alignOffset_y = -$gameMap.tileHeight() / 2 - this.bitmap.height / 2;
  251.             break;
  252.         default:
  253.             throw new Error('Unknown alignment: ' + alignment);
  254.     }
  255. };
  256.  
  257. // SetLocation must be set from here instead of from the GameEvent because that would cause it to jitter when the camera scrolls... Like the other plugin writers seem to do...
  258. // The disadvantage of this way is that it may consume a bit more performance (repositions this sprite even when it's far off-screen). But better than that jittering.
  259. EventText.prototype.update = function()
  260. {
  261.     Sprite_Base.prototype.update.call(this);
  262.     this.setLocation(this.gameEvent.screenX(), this.gameEvent.screenY());
  263. };
  264.  
  265. // x and y are expected to be the center-coords of a tile.
  266. EventText.prototype.setLocation = function(x, y)
  267. {
  268.     this.x = x + this.alignOffset_x + this.extraOffset_x;
  269.     this.y = y + this.alignOffset_y + this.extraOffset_y;
  270. };
  271.  
  272. EventText.prototype.render = function()
  273. {
  274.     this.bitmap.clear();
  275.    
  276.     // (Re)-Initialize bitmap settings
  277.     this.bitmap.fontSize = this.fontSize;
  278.     this.bitmap.textColor = this.fontColor;
  279.     this.bitmap.outlineColor = this.outlineColor;
  280.     this.bitmap.outlineWidth = this.outlineWidth;
  281.     this.bitmap.paintOpacity = this.textOpacity;
  282.     this.bitmap.fontItalic  = this.italic;
  283.    
  284.     this.bitmap.drawText(this.text, 0, 0, this.bitmap.width, this.bitmap.height, 'left');//
  285.    
  286.     this.requiresRender = false;
  287. };
  288.  
  289. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  290. // Game Event
  291. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  292. var alias_silv_evText_Game_Event_setupPage = Game_Event.prototype.setupPage;
  293. Game_Event.prototype.setupPage = function()
  294. {
  295.     alias_silv_evText_Game_Event_setupPage.apply(this, arguments);
  296.    
  297.     this.deleteEventTexts();
  298.     this.checkEventText();
  299. };
  300.  
  301. Game_Event.prototype.deleteEventTexts = function()
  302. {
  303.     if (this.silvEvTexts)
  304.     {
  305.         this.silvEvTexts.forEach(function(evText){ SceneManager._scene._spriteset.removeChild(evText); });
  306.     }
  307.     this.silvEvTexts = [];
  308. }
  309.  
  310. Game_Event.prototype.checkEventText = function()
  311. {  
  312.     var page = this.page();
  313.     if (!page) { return; }
  314.    
  315.     var newEventText = null;
  316.     var evTextCmdFound = false;
  317.     page.list.forEach(function(cmd)
  318.     {
  319.         if (!evTextCmdFound && (cmd.code === 108)) // 108 = first comment-line, 408 = subsequent comment-lines
  320.         {
  321.             if (cmd.parameters[0].trim().toLowerCase() === '<evtext>') { evTextCmdFound = true; }
  322.         }
  323.         else if (evTextCmdFound && ((cmd.code === 108) || cmd.code === 408))
  324.         {
  325.             var option = (cmd.parameters[0].split(':')[0]).trim().toLowerCase();
  326.             var value = Silv.EvText.GetTextAfter(cmd.parameters[0], ':');
  327.            
  328.             switch(option)
  329.             {
  330.                 case 'text':
  331.                     newEventText = new EventText(this, value);
  332.                     this.silvEvTexts.push(newEventText);
  333.                     break;
  334.                 case 'fontsize':
  335.                     newEventText.fontSize = parseInt(value);
  336.                     break;
  337.                 case 'fontcolor':
  338.                     var fontColor = value.trim();
  339.                     if ((fontColor[0] !== '#') && (fontColor[0] !== 'r')) { fontColor = '#' + fontColor; }
  340.                     newEventText.fontColor = fontColor;
  341.                     break;
  342.                 case 'outlinecolor':
  343.                     var outlineColor = value.trim();
  344.                     if ((outlineColor[0] !== '#') && (outlineColor[0] !== 'r')) { outlineColor = '#' + outlineColor; }
  345.                     newEventText.outlineColor = outlineColor;
  346.                     break;
  347.                 case 'outlinewidth':
  348.                     newEventText.outlineWidth = parseInt(value);
  349.                     break;
  350.                 case 'opacity':
  351.                     newEventText.textOpacity = parseInt(value);
  352.                     break;
  353.                 case 'offset_x':
  354.                     newEventText.extraOffset_x = parseFloat(value);
  355.                     break;
  356.                 case 'offset_y':
  357.                     newEventText.extraOffset_y = parseFloat(value);
  358.                     break;
  359.                 case 'align':
  360.                 case 'alignment':
  361.                     newEventText.setAlignment(value);
  362.                     break;
  363.                 case 'italic':
  364.                     newEventText.italic = (value.trim().toLowerCase() === 'true');
  365.                     break;
  366.                 case '</evtext>':
  367.                     newEventText.render();
  368.                     evTextCmdFound = false; // This line is not required but nice to have in case of 'code-expansion' later
  369.                     return;
  370.                 default:
  371.                     throw new Error('Empty line or unknown command for line: ' + cmd.parameters[0]);
  372.             }
  373.         }
  374.     }, this);
  375. };
  376.  
  377. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  378. // This is the end of this awesome script!
  379. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement