Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- // Rata_ClassNameInput
- // by Ratatattat
- //
- // for RPG Maker MV
- // version 1.0.0
- //=============================================================================
- /*:
- * @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
- * plugin command or script call.
- *
- * NOTE: The class that will be renamed is determined by the current class of
- * the actor whose ID you specify in the plugin command or 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 ;)
- *
- * =============================================================================
- * PLUGIN COMMAND
- * =============================================================================
- *
- * Plugin Command:
- *
- * nameclass actorId maxLength
- *
- * Key:
- *
- * nameclass => the command (do not change or omit this)
- * 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:
- *
- * nameclass 14 10
- *
- * - The class name input window will allow renaming of the current class of
- * actor 14, with a character limit of 10.
- *
- * =============================================================================
- * 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
- * by Ratatattat
- *
- * for RPG Maker MV
- * version 1.0.0
- *
- * =============================================================================
- */
- //=============================================================================
- // CODE
- //=============================================================================
- var Imported = Imported || {};
- Imported.Rata_ClassNameInput = "1.0.0";
- var Rata = Rata || {};
- Rata.CNI = Rata.CNI || {};
- (function($) {
- "use strict";
- //-----------------------------------------------------------------------------
- // 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.apply(this, arguments);
- }
- Scene_ClassName.prototype = Object.create(Scene_MenuBase.prototype);
- Scene_ClassName.prototype.constructor = Scene_ClassName;
- Scene_ClassName.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() {
- this._editWindow = new Window_ClassNameEdit(this._actor, this._maxLength);
- this.addWindow(this._editWindow);
- };
- Scene_ClassName.prototype.createInputWindow = function() {
- this._inputWindow = new Window_NameInput(this._editWindow);
- this._inputWindow.setHandler('ok', this.onInputOk.bind(this));
- this.addWindow(this._inputWindow);
- };
- 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.apply(this, arguments);
- }
- Window_ClassNameEdit.prototype = Object.create(Window_Base.prototype);
- Window_ClassNameEdit.prototype.constructor = Window_ClassNameEdit;
- Window_ClassNameEdit.prototype.initialize = function(actor, maxLength) {
- var width = this.windowWidth();
- var height = this.windowHeight();
- var x = (Graphics.boxWidth - width) / 2;
- var y = (Graphics.boxHeight - (height + this.fittingHeight(9) + 8)) / 2;
- Window_Base.prototype.initialize.call(this, x, y, width, height);
- this._actor = actor;
- this._name = actor.currentClass().name.slice(0, this._maxLength);
- this._index = this._name.length;
- this._maxLength = maxLength;
- this._defaultName = this._name;
- this.deactivate();
- this.refresh();
- };
- Window_ClassNameEdit.prototype.windowWidth = function() {
- return 480;
- };
- Window_ClassNameEdit.prototype.windowHeight = function() {
- return this.fittingHeight(4);
- };
- 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() {
- var text = $gameSystem.isJapanese() ? '\uff21' : 'A';
- return this.textWidth(text);
- };
- Window_ClassNameEdit.prototype.left = function() {
- var nameCenter = this.contentsWidth() / 2;
- var nameWidth = (this._maxLength + 1) * this.charWidth();
- return Math.min(nameCenter - nameWidth / 2, this.contentsWidth() - nameWidth);
- };
- Window_ClassNameEdit.prototype.itemRect = function(index) {
- return {
- x: this.left() + index * this.charWidth(),
- y: 54,
- width: this.charWidth(),
- height: this.lineHeight()
- };
- };
- Window_ClassNameEdit.prototype.underlineRect = function(index) {
- var 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 this.normalColor();
- };
- Window_ClassNameEdit.prototype.drawUnderline = function(index) {
- var rect = this.underlineRect(index);
- var 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) {
- var rect = this.itemRect(index);
- this.resetTextColor();
- this.drawText(this._name[index] || '', rect.x, rect.y);
- };
- Window_ClassNameEdit.prototype.refresh = function() {
- this.contents.clear();
- for (var i = 0; i < this._maxLength; i++) {
- this.drawUnderline(i);
- }
- for (var j = 0; j < this._name.length; j++) {
- this.drawChar(j);
- }
- var rect = this.itemRect(this._index);
- this.setCursorRect(rect.x, rect.y, rect.width, rect.height);
- };
- //-----------------------------------------------------------------------------
- // PLUGIN COMMAND
- //-----------------------------------------------------------------------------
- // Plugin Command: "nameclass actorId maxLength"
- $.Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
- Game_Interpreter.prototype.pluginCommand = function(command, args) {
- $.Game_Interpreter_pluginCommand.call(this, command, args);
- if (command.toLowerCase() === "nameclass") {
- this.classNameInput(args);
- }
- };
- Game_Interpreter.prototype.classNameInput = function(args) {
- const actorId = parseInt(args[0]);
- const maxLength = parseInt(args[1]);
- SceneManager.push(Scene_ClassName);
- SceneManager.prepareNextScene(actorId, maxLength);
- };
- //-----------------------------------------------------------------------------
- // 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