Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- // SilvEventText.js
- // Version: 1.00
- //=============================================================================
- /*:
- * @plugindesc v1.00 Attaches texts on the map to events. <SilvEventText>
- * @author Silver
- *
- * @param -- Default Values --
- *
- * @param Default Font Size
- * @desc Default font size
- * @default 24
- *
- * @param Default Font Color
- * @desc Default font color
- * @default #FFFFFF
- *
- * @param Default Outline Color
- * @desc Default text outline color. Red, Green, Blue, Alpha
- * @default rgba(0, 0, 0, 0.5)
- *
- * @param Default Outline Width
- * @desc Default width of the text-outline
- * @default 4
- *
- * @param Default Text Alignment
- * @desc Allowed values: topleft, topcenter, topright, center
- * @default topcenter
- *
- * @param Default Opacity
- * @desc Text Opacity 0-255
- * @default 255
- *
- * @param Default Italic
- * @desc By default, make all the text italic?
- * @default false
- *
- * @param Default Offset X
- * @desc Offset x. A negative value means more to the left.
- * @default 0
- *
- * @param Default Offset Y
- * @desc Offset y. A negative value means more to the top.
- * @default 0
- *
- * @help
- *--------------------------------------
- * Notes
- *--------------------------------------
- * - 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
- * - 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.
- * - This plugin does not have 'jittering-text'
- * - Events can have multiple event-texts.
- * - Switching the event-page will clear the EventText(s) from the old page.
- *
- *--------------------------------------
- * Event Comment (NOT the event-notetag):
- *--------------------------------------
- * The comment must start with: <evtext>
- * Nothing is case-sensitive here
- *
- * Supported tags:
- * Text: value // << this tag must be the first tag!
- * FontSize: value
- * FontColor: value // Supported format examples: #FFFFFF, FFFFFF, rgba(0, 0, 0, 0.5)
- * OutlineColor: value // Supported format examples: #FFFFFF, FFFFFF, rgba(0, 0, 0, 0.5)
- * OutlineWidth: value
- * offset_x: value
- * offset_y: value
- * Opacity: value // 0-255
- * align: value // Supported values: topleft, topcenter, topright, center
- * alignment: value // same as align-tag
- * italic: value // true/false
- * </evtext> // without this tag the text will not be drawn. So don't forget it.
- *
- *
- * Example comment:
- * <evText>
- * Text:Well Hello There!
- * FontSize: 30
- * FontColor: FF0000
- * Italic: true
- * </evText>
- *
- *--------------------------------------
- * Dev notes (ONLY for other developers):
- *--------------------------------------
- * - The Game Events maintain a reference to the EventTexts using this variable: this.silvEvTexts[]
- * - To change an event's fontsize for example (after it's been created):
- * this.silvEvTexts[0].fontSize = 40;
- * this.silvEvTexts[0].render();
- *
- *--------------------------------------
- * Version History:
- *--------------------------------------
- * v.1.00 (24 December 2015)
- * - First Release
- *
- */
- // Imported
- var Imported = Imported || {};
- Imported.SILV_EventText = 1.00;
- // #Parameters
- var Silv = Silv || {};
- Silv.EvText = Silv.EvText || {};
- Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilvEventText>'); })[0].parameters;
- // Default Values
- Silv.EvText.DefaultFontSize = parseInt(Silv.Parameters['Default Font Size']);
- Silv.EvText.DefaultFontColor = Silv.Parameters['Default Font Color'];
- Silv.EvText.DefaultOutlineColor = Silv.Parameters['Default Outline Color'];
- Silv.EvText.DefaultOutlineWidth = parseInt(Silv.Parameters['Default Outline Width']);
- Silv.EvText.DefaultTextAlign = Silv.Parameters['Default Text Alignment'];
- Silv.EvText.DefaultOpacity = parseInt(Silv.Parameters['Default Opacity']);
- Silv.EvText.DefaultItalic = parseInt(Silv.Parameters['Default Italic']);
- Silv.EvText.DefaultOffset_X = parseFloat(Silv.Parameters['Default Offset X']);
- Silv.EvText.DefaultOffset_Y = parseFloat(Silv.Parameters['Default Offset Y']);
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Utilities
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Silv.EvText.GetTextAfter = function(str, character)
- {
- return str.substr(str.indexOf(character) + 1);
- };
- Silv.EvText.MeasureBmp = new Bitmap(1, 1);
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Scene Map
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- var alias_silv_evText_Scene_Map_initialize = Scene_Map.prototype.initialize;
- Scene_Map.prototype.initialize = function()
- {
- alias_silv_evText_Scene_Map_initialize.apply(this, arguments);
- this.evSpritesToAdd = [];
- };
- var alias_silv_evText_Scene_Map_createSpriteset = Scene_Map.prototype.createSpriteset;
- Scene_Map.prototype.createSpriteset = function()
- {
- alias_silv_evText_Scene_Map_createSpriteset.apply(this, arguments);
- this.evSpritesToAdd.forEach(function(sprite)
- {
- this._spriteset.addChild(sprite);
- }, this);
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Event Text
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- function EventText() { this.initialize.apply(this, arguments); }
- EventText.prototype = Object.create(Sprite_Base.prototype);
- EventText.prototype.constructor = EventText;
- EventText.prototype.initialize = function(gameEvent, text)
- {
- Sprite_Base.prototype.initialize.call(this);
- this.gameEvent = gameEvent;
- this.setDefaults();
- this.setText(text);
- if (SceneManager._scene._spriteset)
- {
- SceneManager._scene._spriteset.addChild(this); // SceneManager._scene can be null when the map was just loaded
- }
- else
- {
- SceneManager._scene.evSpritesToAdd.push(this); // if it's null, just push it to a temporary list instead
- }
- };
- EventText.prototype.setDefaults = function()
- {
- this.fontSize = Silv.EvText.DefaultFontSize;
- this.fontColor = Silv.EvText.DefaultFontColor;
- this.outlineColor = Silv.EvText.DefaultOutlineColor;
- this.outlineWidth = Silv.EvText.DefaultOutlineWidth;
- this.textOpacity = Silv.EvText.DefaultOpacity;
- this.italic = Silv.EvText.DefaultItalic;
- this.extraOffset_x = Silv.EvText.DefaultOffset_X;
- this.extraOffset_y = Silv.EvText.DefaultOffset_Y;
- };
- // 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.
- EventText.prototype.setFontSize = function(newFontSize)
- {
- this.fontSize = newFontSize;
- this.setSizePropertyAfter();
- };
- // 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.
- EventText.prototype.setOutlineWidth = function(outlineWidth)
- {
- this.outlineWidth = outlineWidth;
- this.setSizePropertyAfter();
- };
- // 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
- EventText.prototype.setSizePropertyAfter = function()
- {
- this.createBitmap();
- this.setAlignment(this.alignment);
- if (!this.requiresRender) { this.render(); }
- };
- EventText.prototype.createBitmap = function(text)
- {
- Silv.EvText.MeasureBmp.fontSize = this.fontSize;
- Silv.EvText.MeasureBmp.outlinewidth = this.outlinewidth;
- Silv.EvText.MeasureBmp.fontItalic = this.italic;
- this.bitmap = new Bitmap(Silv.EvText.MeasureBmp.measureTextWidth(text), this.fontSize + this.outlineWidth * 2);
- };
- EventText.prototype.setText = function(text)
- {
- this.createBitmap();
- this.text = text;
- this.setAlignment(Silv.EvText.DefaultTextAlign);
- this.requiresRender = true;
- };
- // alignment: topLeft, topCenter, topRight, center
- EventText.prototype.setAlignment = function(alignment)
- {
- if (!this.text) { throw new Error('setAlignment() requires the text to be set first.'); }
- this.alignment = alignment;
- switch(alignment.toLowerCase())
- {
- case 'topleft':
- this.alignOffset_x = -$gameMap.tileWidth() / 2;
- this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
- break;
- case 'topcenter':
- this.alignOffset_x = -this.bitmap.width / 2;
- this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
- break;
- case 'topright':
- this.alignOffset_x = +$gameMap.tileWidth() / 3 - this.bitmap.width;
- this.alignOffset_y = -$gameMap.tileHeight() - this.bitmap.height;
- break;
- case 'center':
- this.alignOffset_x = -this.bitmap.width / 2;
- this.alignOffset_y = -$gameMap.tileHeight() / 2 - this.bitmap.height / 2;
- break;
- default:
- throw new Error('Unknown alignment: ' + alignment);
- }
- };
- // 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...
- // 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.
- EventText.prototype.update = function()
- {
- Sprite_Base.prototype.update.call(this);
- this.setLocation(this.gameEvent.screenX(), this.gameEvent.screenY());
- };
- // x and y are expected to be the center-coords of a tile.
- EventText.prototype.setLocation = function(x, y)
- {
- this.x = x + this.alignOffset_x + this.extraOffset_x;
- this.y = y + this.alignOffset_y + this.extraOffset_y;
- };
- EventText.prototype.render = function()
- {
- this.bitmap.clear();
- // (Re)-Initialize bitmap settings
- this.bitmap.fontSize = this.fontSize;
- this.bitmap.textColor = this.fontColor;
- this.bitmap.outlineColor = this.outlineColor;
- this.bitmap.outlineWidth = this.outlineWidth;
- this.bitmap.paintOpacity = this.textOpacity;
- this.bitmap.fontItalic = this.italic;
- this.bitmap.drawText(this.text, 0, 0, this.bitmap.width, this.bitmap.height, 'left');//
- this.requiresRender = false;
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Game Event
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- var alias_silv_evText_Game_Event_setupPage = Game_Event.prototype.setupPage;
- Game_Event.prototype.setupPage = function()
- {
- alias_silv_evText_Game_Event_setupPage.apply(this, arguments);
- this.deleteEventTexts();
- this.checkEventText();
- };
- Game_Event.prototype.deleteEventTexts = function()
- {
- if (this.silvEvTexts)
- {
- this.silvEvTexts.forEach(function(evText){ SceneManager._scene._spriteset.removeChild(evText); });
- }
- this.silvEvTexts = [];
- }
- Game_Event.prototype.checkEventText = function()
- {
- var page = this.page();
- if (!page) { return; }
- var newEventText = null;
- var evTextCmdFound = false;
- page.list.forEach(function(cmd)
- {
- if (!evTextCmdFound && (cmd.code === 108)) // 108 = first comment-line, 408 = subsequent comment-lines
- {
- if (cmd.parameters[0].trim().toLowerCase() === '<evtext>') { evTextCmdFound = true; }
- }
- else if (evTextCmdFound && ((cmd.code === 108) || cmd.code === 408))
- {
- var option = (cmd.parameters[0].split(':')[0]).trim().toLowerCase();
- var value = Silv.EvText.GetTextAfter(cmd.parameters[0], ':');
- switch(option)
- {
- case 'text':
- newEventText = new EventText(this, value);
- this.silvEvTexts.push(newEventText);
- break;
- case 'fontsize':
- newEventText.fontSize = parseInt(value);
- break;
- case 'fontcolor':
- var fontColor = value.trim();
- if ((fontColor[0] !== '#') && (fontColor[0] !== 'r')) { fontColor = '#' + fontColor; }
- newEventText.fontColor = fontColor;
- break;
- case 'outlinecolor':
- var outlineColor = value.trim();
- if ((outlineColor[0] !== '#') && (outlineColor[0] !== 'r')) { outlineColor = '#' + outlineColor; }
- newEventText.outlineColor = outlineColor;
- break;
- case 'outlinewidth':
- newEventText.outlineWidth = parseInt(value);
- break;
- case 'opacity':
- newEventText.textOpacity = parseInt(value);
- break;
- case 'offset_x':
- newEventText.extraOffset_x = parseFloat(value);
- break;
- case 'offset_y':
- newEventText.extraOffset_y = parseFloat(value);
- break;
- case 'align':
- case 'alignment':
- newEventText.setAlignment(value);
- break;
- case 'italic':
- newEventText.italic = (value.trim().toLowerCase() === 'true');
- break;
- case '</evtext>':
- newEventText.render();
- evTextCmdFound = false; // This line is not required but nice to have in case of 'code-expansion' later
- return;
- default:
- throw new Error('Empty line or unknown command for line: ' + cmd.parameters[0]);
- }
- }
- }, this);
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // This is the end of this awesome script!
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement