Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- // Rata_ClassNameInput
- // by Ratatattat
- //
- // for RPG Maker MZ
- // version 1.0.0
- //=============================================================================
- /*:
- * @target MZ
- * @plugindesc Opens a name input processing window for players to name their own classes.
- * Requires a dedicated game variable to hold the custom class names.
- * <Rata_ClassNameInput>
- * @author Ratatattat
- *
- * @param Storage Variable
- * @type variable
- * @desc Choose a variable to store custom class names. Changing this
- * variable's value will permanently erase all custom names.
- * @default 0
- *
- * @help
- * =============================================================================
- * DESCRIPTION
- * =============================================================================
- *
- * This plugin allows you to open a name input window for your players to choose
- * their own custom names for the actors' classes. You can call this window via
- * script call.
- *
- * NOTE: The class that will be renamed is determined by the current class of
- * the actor whose ID you specify in the script call parameters. However,
- * the class will still be renamed for all actors who share it. If you
- * want to ensure each actor can have their own uniquely-named class,
- * you'll need to dedicate a unique class in the database to that actor
- * alone.
- *
- * =============================================================================
- * SETTING UP
- * =============================================================================
- *
- * You must set up one parameter before this plugin can work. To the right, pick
- * a game variable to store all of the player's custom class names.
- *
- * WARNING: Changing the value of this variable at any point in the game will
- * permanently erase all of the class names stored in it, and every
- * class that the player has renamed over the course of the game will
- * revert back to its database name the next time the player opens the
- * game. So it's probably best to avoid that ;)
- *
- * =============================================================================
- * SCRIPT CALL
- * =============================================================================
- *
- * Script Call:
- *
- * Rata.CNI.nameClass(actorId, maxLength);
- *
- * Key:
- *
- * actorId => the actor whose class you want the player to rename
- * maxLength => the maximum number of characters you want the player to be
- * able to enter
- *
- * Example:
- *
- * Rata.CNI.nameClass(2, 12);
- *
- * - The class name input window will allow renaming of the current class of
- * actor 2, with a character limit of 12.
- *
- * =============================================================================
- * TERMS OF USE
- * =============================================================================
- *
- * This plugin is free for both non-commercial and commercial use, with credit
- * given to Ratatattat.
- *
- * This plugin may be altered and redistributed in any way that suits the user's
- * needs, as long as the original code, in part or in full, is not:
- *
- * i. Passed off as the work of someone else
- * ii. Monetarily profited from outside of its use in an RPG Maker game -
- * (exception for commissions to alter this plugin for compatibility or
- * porting purposes, or to add new features; however, these terms may
- * not be altered and the resulting altered plugin may not be sold to
- * anyone besides the commissioner)
- * iii. Used in any way that would prevent others from using it as freely as
- * outlined in these terms
- * iv. Redistributed either in the absence of, or with an edited version of,
- * these terms
- *
- * This plugin is provided as is. Ratatattat is not liable for any harm that
- * may result from its use or misuse.
- *
- * =============================================================================
- * CHANGELOG
- * =============================================================================
- *
- * Version 1.0.0 - Finished plugin.
- *
- * =============================================================================
- *
- * Rata_ClassNameInput.js
- * by Ratatattat
- *
- * for RPG Maker MZ
- * version 1.0.0
- *
- * =============================================================================
- */
- //=============================================================================
- // CODE
- //=============================================================================
- var Imported = Imported || {};
- Imported.Rata_ClassNameInput = "1.0.0";
- var Rata = Rata || {};
- Rata.CNI = Rata.CNI || {};
- (($) => {
- "use strict";
- const pluginName = 'Rata_ClassNameInput';
- //-----------------------------------------------------------------------------
- // PARAMETERS
- //-----------------------------------------------------------------------------
- let parameters = $plugins.filter(function(plugin) {
- return plugin.description.contains('<Rata_ClassNameInput>');
- });
- if (parameters.length == 0) {
- throw new Error("Can't find parameters for Rata_ClassNameInput.");
- }
- $.getParameters = parameters[0].parameters;
- $.parameter = $.parameter || {};
- $.parameter.var = Number($.getParameters['Storage Variable']);
- //-----------------------------------------------------------------------------
- // UPDATE CLASS NAMES
- //-----------------------------------------------------------------------------
- $.Scene_Map_updateScene = Scene_Map.prototype.updateScene;
- Scene_Map.prototype.updateScene = function() {
- $.Scene_Map_updateScene.call(this);
- if (!SceneManager.isSceneChanging()) {
- this.updateClassNames();
- }
- };
- Scene_Map.prototype.updateClassNames = function() {
- if (Array.isArray($gameVariables.value($.parameter.var)) == false) {
- let emptyArray = [];
- $gameVariables.setValue($.parameter.var, emptyArray);
- }
- var storageVar = $gameVariables.value($.parameter.var);
- var customClassNames = storageVar;
- if (customClassNames.length > 0) {
- customClassNames.forEach((renamedClass) => {
- let actorId = renamedClass[0];
- let customName = renamedClass[1];
- let currentClass = $gameActors.actor(actorId).currentClass();
- if (currentClass.name !== customName) {
- currentClass.name = customName;
- }
- });
- }
- };
- //-----------------------------------------------------------------------------
- // Scene_ClassName
- //
- // The scene class of the class name input screen.
- //-----------------------------------------------------------------------------
- function Scene_ClassName() {
- this.initialize(...arguments);
- }
- Scene_ClassName.prototype = Object.create(Scene_MenuBase.prototype);
- Scene_ClassName.prototype.constructor = Scene_ClassName;
- Scene_Name.prototype.initialize = function() {
- Scene_MenuBase.prototype.initialize.call(this);
- };
- Scene_ClassName.prototype.prepare = function(actorId, maxLength) {
- this._actorId = actorId;
- this._maxLength = maxLength;
- };
- Scene_ClassName.prototype.create = function() {
- Scene_MenuBase.prototype.create.call(this);
- this._actor = $gameActors.actor(this._actorId);
- this.createEditWindow();
- this.createInputWindow();
- };
- Scene_ClassName.prototype.start = function() {
- Scene_MenuBase.prototype.start.call(this);
- this._editWindow.refresh();
- };
- Scene_ClassName.prototype.createEditWindow = function() {
- const rect = this.editWindowRect();
- this._editWindow = new Window_ClassNameEdit(rect);
- this._editWindow.setup(this._actor, this._maxLength);
- this.addWindow(this._editWindow);
- };
- Scene_ClassName.prototype.editWindowRect = function() {
- const inputWindowHeight = this.calcWindowHeight(9, true);
- const padding = $gameSystem.windowPadding();
- const ww = 600;
- const wh = ImageManager.faceHeight + padding * 2;
- const wx = (Graphics.boxWidth - ww) / 2;
- const wy = (Graphics.boxHeight - (wh + inputWindowHeight + 8)) / 2;
- return new Rectangle(wx, wy, ww, wh);
- };
- Scene_ClassName.prototype.createInputWindow = function() {
- const rect = this.inputWindowRect();
- this._inputWindow = new Window_NameInput(rect);
- this._inputWindow.setEditWindow(this._editWindow);
- this._inputWindow.setHandler("ok", this.onInputOk.bind(this));
- this.addWindow(this._inputWindow);
- };
- Scene_ClassName.prototype.inputWindowRect = function() {
- const wx = this._editWindow.x;
- const wy = this._editWindow.y + this._editWindow.height + 8;
- const ww = this._editWindow.width;
- const wh = this.calcWindowHeight(9, true);
- return new Rectangle(wx, wy, ww, wh);
- };
- Scene_ClassName.prototype.onInputOk = function() {
- var storageVar = $gameVariables.value($.parameter.var);
- var customClassNames = storageVar;
- var needToOverwrite = false;
- if (customClassNames.length > 0) {
- customClassNames.forEach((item) => {
- if (item[0] == this._actorId) {
- // Overwrite existing custom class name for this actor.
- let index = customClassNames.indexOf(item);
- this.deleteClassName(index);
- needToOverwrite = true;
- this.popScene();
- }
- });
- }
- if (needToOverwrite == false) {
- // Save new custom class name for this actor.
- this.saveNewClassName(customClassNames);
- this.popScene();
- }
- };
- Scene_ClassName.prototype.deleteClassName = function(index) {
- var storageVar = $gameVariables.value($.parameter.var);
- var customClassNames = storageVar;
- let splicedArray = customClassNames.splice(index, 1);
- customClassNames = splicedArray;
- this.saveNewClassName(customClassNames);
- };
- Scene_ClassName.prototype.saveNewClassName = function(customClassNames) {
- let renamedClass = [];
- renamedClass.push(this._actorId);
- renamedClass.push(this._editWindow.name());
- customClassNames.push(renamedClass);
- $gameVariables.setValue($.parameter.var, customClassNames);
- };
- //-----------------------------------------------------------------------------
- // Window_ClassNameEdit
- //
- // The window for editing a class's name on the class name input screen.
- //-----------------------------------------------------------------------------
- function Window_ClassNameEdit() {
- this.initialize(...arguments);
- }
- Window_ClassNameEdit.prototype = Object.create(Window_StatusBase.prototype);
- Window_ClassNameEdit.prototype.constructor = Window_ClassNameEdit;
- Window_ClassNameEdit.prototype.initialize = function(rect) {
- Window_StatusBase.prototype.initialize.call(this, rect);
- this._actor = null;
- this._maxLength = 0;
- this._name = "";
- this._index = 0;
- this._defaultName = 0;
- this.deactivate();
- };
- Window_ClassNameEdit.prototype.setup = function(actor, maxLength) {
- this._actor = actor;
- this._maxLength = maxLength;
- this._name = actor.currentClass().name.slice(0, this._maxLength);
- this._index = this._name.length;
- this._defaultName = this._name;
- };
- Window_ClassNameEdit.prototype.name = function() {
- return this._name;
- };
- Window_ClassNameEdit.prototype.restoreDefault = function() {
- this._name = this._defaultName;
- this._index = this._name.length;
- this.refresh();
- return this._name.length > 0;
- };
- Window_ClassNameEdit.prototype.add = function(ch) {
- if (this._index < this._maxLength) {
- this._name += ch;
- this._index++;
- this.refresh();
- return true;
- } else {
- return false;
- }
- };
- Window_ClassNameEdit.prototype.back = function() {
- if (this._index > 0) {
- this._index--;
- this._name = this._name.slice(0, this._index);
- this.refresh();
- return true;
- } else {
- return false;
- }
- };
- Window_ClassNameEdit.prototype.charWidth = function() {
- const text = $gameSystem.isJapanese() ? "\uff21" : "A";
- return this.textWidth(text);
- };
- Window_ClassNameEdit.prototype.left = function() {
- const nameCenter = this.innerWidth / 2;
- const nameWidth = (this._maxLength + 1) * this.charWidth();
- return Math.min(nameCenter - nameWidth / 2, this.innerWidth - nameWidth);
- };
- Window_ClassNameEdit.prototype.itemRect = function(index) {
- const x = this.left() + index * this.charWidth();
- const y = 54;
- const width = this.charWidth();
- const height = this.lineHeight();
- return new Rectangle(x, y, width, height);
- };
- Window_ClassNameEdit.prototype.underlineRect = function(index) {
- const rect = this.itemRect(index);
- rect.x++;
- rect.y += rect.height - 4;
- rect.width -= 2;
- rect.height = 2;
- return rect;
- };
- Window_ClassNameEdit.prototype.underlineColor = function() {
- return ColorManager.normalColor();
- };
- Window_ClassNameEdit.prototype.drawUnderline = function(index) {
- const rect = this.underlineRect(index);
- const color = this.underlineColor();
- this.contents.paintOpacity = 48;
- this.contents.fillRect(rect.x, rect.y, rect.width, rect.height, color);
- this.contents.paintOpacity = 255;
- };
- Window_ClassNameEdit.prototype.drawChar = function(index) {
- const rect = this.itemRect(index);
- this.resetTextColor();
- this.drawText(this._name[index] || "", rect.x, rect.y);
- };
- Window_ClassNameEdit.prototype.refresh = function() {
- this.contents.clear();
- for (let i = 0; i < this._maxLength; i++) {
- this.drawUnderline(i);
- }
- for (let j = 0; j < this._name.length; j++) {
- this.drawChar(j);
- }
- const rect = this.itemRect(this._index);
- this.setCursorRect(rect.x, rect.y, rect.width, rect.height);
- };
- //-----------------------------------------------------------------------------
- // SCRIPT CALL
- //-----------------------------------------------------------------------------
- // Script Call: "Rata.CNI.nameClass(actorId, maxLength);"
- $.nameClass = function(actorId, maxLength) {
- SceneManager.push(Scene_ClassName);
- SceneManager.prepareNextScene(actorId, maxLength);
- };
- })(Rata.CNI);
- //=============================================================================
- // END OF FILE
- //=============================================================================
Advertisement
Add Comment
Please, Sign In to add comment