Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- // SilvEventText.js
- // Version: 1.02
- //=============================================================================
- /*:
- * @plugindesc v1.02 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
- *
- * @param Default Extra Bitmap Width
- * @desc The default extra bitmap-width.
- * @default 0
- *
- * @param Default Extra Bitmap Height
- * @desc The default extra bitmap-height.
- * @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.
- *
- *--------------------------------------
- * Plugin Commands (not case sensitive):
- *--------------------------------------
- * EventText ShowAll
- * EventText HideAll
- *
- *--------------------------------------
- * 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
- * ExtraBmpWidth: value // handy for thick outlineWidth's (and because RPG Maker MV fails)
- * ExtraBmp_W: value // Same as the above
- * ExtraBmpHeight: value // handy for thick outlineWidth's (and because RPG Maker MV fails)
- * ExtraBmp_H: value // Same as the above
- * </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.02 (3 April 2016)
- * - Fixed a bug where entering the text "0" (w/o quotes) would cause an error.
- *
- * v.1.01b (26 December 2015)
- * - Added 2 new parameters and 2 new commands.
- * - Applied my new coding standards.
- * - Fixed a crash.
- * - EventTexts are now deleted when the events themselves are erased.
- * - Added 2 new plugin commands.
- * - Fixed a bug where opening&closing the menu would delete all labels.
- *
- * v.1.00 (24 December 2015)
- * - First Release
- *
- */
- // Imported
- var Imported = Imported || {};
- Imported.SILV_EventText = 1.02;
- // #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']);
- Silv.EvText.DefaultExtraBmpWidth = parseInt(Silv.Parameters['Default Extra Bitmap Width']);
- Silv.EvText.DefaultExtraBmpHeight = parseInt(Silv.Parameters['Default Extra Bitmap Height']);
- // Alias
- Silv.Alias = Silv.Alias || {};
- if (!Silv.AddAlias)
- {
- Silv.AddAlias = function(alias, original_method)
- {
- if (Silv.Alias[alias]) { throw new Error('Alias already exists: ' + alias); }
- Silv.Alias[alias] = original_method;
- };
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Utilities
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Silv.EvText.GetTextAfter = function(str, character)
- {
- return str.substr(str.indexOf(character) + 1);
- };
- Silv.EvText.MeasureBmp = new Bitmap(1, 1);
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Scene Map
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Silv.AddAlias('evText_Scene_Map_initialize', Scene_Map.prototype.initialize);
- Scene_Map.prototype.initialize = function()
- {
- Silv.Alias.evText_Scene_Map_initialize.apply(this, arguments);
- this.evSpritesToAdd = [];
- };
- Silv.AddAlias('evText_Scene_Map_createSpriteset', Scene_Map.prototype.createSpriteset);
- Scene_Map.prototype.createSpriteset = function()
- {
- Silv.Alias.evText_Scene_Map_createSpriteset.apply(this, arguments);
- this.evSpritesToAdd.forEach(function(sprite)
- {
- this._spriteset.addChild(sprite);
- }, this);
- };
- Silv.AddAlias('evText_Scene_Map_createDisplayObjects', Scene_Map.prototype.createDisplayObjects);
- Scene_Map.prototype.createDisplayObjects = function()
- {
- Silv.Alias.evText_Scene_Map_createDisplayObjects.apply(this, arguments);
- this.addOldEventTexts();
- };
- // 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.
- Scene_Map.prototype.addOldEventTexts = function()
- {
- $gameMap.events().forEach(function(event)
- {
- if (event.silvEvTexts)
- {
- event.silvEvTexts.forEach(function(evText)
- {
- if (this._spriteset.children.indexOf(evText) == -1)
- {
- this._spriteset.addChild(evText);
- }
- }, this);
- }
- }, 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;
- this._extraBmpWidth = Silv.EvText.DefaultExtraBmpWidth;
- this._extraBmpHeight = Silv.EvText.DefaultExtraBmpHeight;
- };
- EventText.prototype.extraBmpWidth = function(value)
- {
- this._extraBmpWidth = value;
- this.setSizePropertyAfter();
- };
- EventText.prototype.extraBmpHeight = function(value)
- {
- this._extraBmpHeight = value;
- this.setSizePropertyAfter();
- };
- EventText.prototype.fontSize = function(value)
- {
- this._fontSize = value;
- this.setSizePropertyAfter();
- };
- EventText.prototype.outlineWidth = function(value)
- {
- this._outlineWidth = value;
- 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._extraBmpWidth, this._fontSize + this._extraBmpHeight);
- };
- 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 (typeof this.text === undefined) { 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);
- }
- };
- 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, 'center');
- this.requiresRender = false;
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Game Event
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Silv.AddAlias('evText_Game_Event_setupPage', Game_Event.prototype.setupPage);
- Game_Event.prototype.setupPage = function()
- {
- Silv.Alias.evText_Game_Event_setupPage.apply(this, arguments);
- if (!this._erased)
- {
- this.deleteEventTexts();
- this.checkEventText();
- }
- };
- Silv.AddAlias('evText_Game_Event_erase', Game_Event.prototype.erase);
- Game_Event.prototype.erase = function()
- {
- Silv.Alias.evText_Game_Event_erase.apply(this, arguments);
- this.deleteEventTexts();
- };
- Game_Event.prototype.deleteEventTexts = function()
- {
- if (this.silvEvTexts)
- {
- if (SceneManager._scene._spriteset)
- {
- this.silvEvTexts.forEach(function(evText) { SceneManager._scene._spriteset.removeChild(evText); });
- }
- else
- {
- this.silvEvTexts.forEach(function(evText)
- {
- var arrayIdx = SceneManager._scene.evSpritesToAdd.indexOf(evText);
- if (arrayIdx > -1) { SceneManager._scene.evSpritesToAdd.splice(arrayIdx, 1); } // delete from array
- });
- }
- }
- 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 'extrabmp_w':
- case 'extrabmpwidth':
- newEventText.extraBmpWidth(parseInt(value));
- break;
- case 'extrabmp_h':
- case 'extrabmpheight':
- newEventText.extraBmpHeight(parseInt(value));
- 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);
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // #Plugin Command
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Silv.AddAlias('evText_Game_Interpreter_pluginCommand', Game_Interpreter.prototype.pluginCommand);
- Game_Interpreter.prototype.pluginCommand = function(command, args)
- {
- Silv.Alias.evText_Game_Interpreter_pluginCommand.call(this, command, args);
- if (command.toLowerCase() === 'eventtext') { Silv.EvText.pluginCommand(command, args); }
- };
- Silv.EvText.pluginCommand = function(cmd, args)
- {
- switch(args[0].toLowerCase())
- {
- case 'hideall':
- Silv.EvText.setAllEventTextVisibility(false);
- break;
- case 'showall':
- Silv.EvText.setAllEventTextVisibility(true);
- break;
- default:
- console.log('Plugin arguments:');
- console.log(args);
- throw new Error('Unknown plugin command: ' + cmd);
- }
- };
- Silv.EvText.setAllEventTextVisibility = function(visible)
- {
- var allEvents = $gameMap.events();
- allEvents.forEach(function(event)
- {
- event.silvEvTexts.forEach(function(evText)
- {
- (visible) ? evText.opacity = 255 : evText.opacity = 0;
- // evText.visible = visible; // For some reason this line does not work
- });
- });
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // This is the end of this awesome script!
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement