Ratatattat

Rata_ClassNameInput (RMMV)

Apr 30th, 2022 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Rata_ClassNameInput
  3. // by Ratatattat
  4. //
  5. // for RPG Maker MV
  6. // version 1.0.0
  7. //=============================================================================
  8. /*:
  9.  * @plugindesc Opens a name input processing window for players to name their own classes.
  10.  * Requires a dedicated game variable to hold the custom class names.
  11.  * <Rata_ClassNameInput>
  12.  * @author Ratatattat
  13.  *
  14.  * @param Storage Variable
  15.  * @type variable
  16.  * @desc Choose a variable to store custom class names. Changing this
  17.  * variable's value will permanently erase all custom names.
  18.  * @default 0
  19.  *
  20.  * @help
  21.  * =============================================================================
  22.  *  DESCRIPTION
  23.  * =============================================================================
  24.  *
  25.  * This plugin allows you to open a name input window for your players to choose
  26.  * their own custom names for the actors' classes. You can call this window via
  27.  * plugin command or script call.
  28.  *
  29.  * NOTE: The class that will be renamed is determined by the current class of
  30.  *        the actor whose ID you specify in the plugin command or script call
  31.  *        parameters. However, the class will still be renamed for all actors
  32.  *        who share it. If you want to ensure each actor can have their own
  33.  *        uniquely-named class, you'll need to dedicate a unique class in the
  34.  *        database to that actor alone.
  35.  *
  36.  * =============================================================================
  37.  *  SETTING UP
  38.  * =============================================================================
  39.  *
  40.  * You must set up one parameter before this plugin can work. To the right, pick
  41.  * a game variable to store all of the player's custom class names.
  42.  *
  43.  * WARNING: Changing the value of this variable at any point in the game will
  44.  *           permanently erase all of the class names stored in it, and every
  45.  *           class that the player has renamed over the course of the game will
  46.  *           revert back to its database name the next time the player opens the
  47.  *           game. So it's probably best to avoid that ;)
  48.  *
  49.  * =============================================================================
  50.  *  PLUGIN COMMAND
  51.  * =============================================================================
  52.  *
  53.  * Plugin Command:
  54.  *
  55.  *    nameclass actorId maxLength
  56.  *
  57.  * Key:
  58.  *
  59.  *    nameclass  => the command (do not change or omit this)
  60.  *    actorId    => the actor whose class you want the player to rename
  61.  *    maxLength  => the maximum number of characters you want the player to be
  62.  *                   able to enter
  63.  *
  64.  * Example:
  65.  *
  66.  *    nameclass 14 10
  67.  *
  68.  *  - The class name input window will allow renaming of the current class of
  69.  *     actor 14, with a character limit of 10.
  70.  *
  71.  * =============================================================================
  72.  *  SCRIPT CALL
  73.  * =============================================================================
  74.  *
  75.  * Script Call:
  76.  *
  77.  *    Rata.CNI.nameClass(actorId, maxLength);
  78.  *
  79.  * Key:
  80.  *
  81.  *    actorId    => the actor whose class you want the player to rename
  82.  *    maxLength  => the maximum number of characters you want the player to be
  83.  *                   able to enter
  84.  *
  85.  * Example:
  86.  *
  87.  *    Rata.CNI.nameClass(2, 12);
  88.  *
  89.  *  - The class name input window will allow renaming of the current class of
  90.  *     actor 2, with a character limit of 12.
  91.  *
  92.  * =============================================================================
  93.  *  TERMS OF USE
  94.  * =============================================================================
  95.  *
  96.  * This plugin is free for both non-commercial and commercial use, with credit
  97.  * given to Ratatattat.
  98.  *
  99.  * This plugin may be altered and redistributed in any way that suits the user's
  100.  * needs, as long as the original code, in part or in full, is not:
  101.  *
  102.  *      i. Passed off as the work of someone else
  103.  *     ii. Monetarily profited from outside of its use in an RPG Maker game -
  104.  *          (exception for commissions to alter this plugin for compatibility or
  105.  *           porting purposes, or to add new features; however, these terms may
  106.  *           not be altered and the resulting altered plugin may not be sold to
  107.  *           anyone besides the commissioner)
  108.  *    iii. Used in any way that would prevent others from using it as freely as
  109.  *          outlined in these terms
  110.  *     iv. Redistributed either in the absence of, or with an edited version of,
  111.  *          these terms
  112.  *
  113.  * This plugin is provided as is. Ratatattat is not liable for any harm that
  114.  * may result from its use or misuse.
  115.  *
  116.  * =============================================================================
  117.  *  CHANGELOG
  118.  * =============================================================================
  119.  *
  120.  * Version 1.0.0 - Finished plugin.
  121.  *
  122.  * =============================================================================
  123.  *
  124.  *  Rata_ClassNameInput
  125.  *  by Ratatattat
  126.  *
  127.  *  for RPG Maker MV
  128.  *  version 1.0.0
  129.  *
  130.  * =============================================================================
  131.  */
  132.  
  133. //=============================================================================
  134. // CODE
  135. //=============================================================================
  136.  
  137. var Imported = Imported || {};
  138. Imported.Rata_ClassNameInput = "1.0.0";
  139.  
  140. var Rata = Rata || {};
  141. Rata.CNI = Rata.CNI || {};
  142.  
  143. (function($) {
  144.     "use strict";
  145.  
  146.     //-----------------------------------------------------------------------------
  147.     // PARAMETERS
  148.     //-----------------------------------------------------------------------------
  149.  
  150.     let parameters = $plugins.filter(function(plugin) {
  151.         return plugin.description.contains('<Rata_ClassNameInput>');
  152.     });
  153.     if (parameters.length == 0) {
  154.         throw new Error("Can't find parameters for Rata_ClassNameInput.");
  155.     }
  156.  
  157.     $.getParameters = parameters[0].parameters;
  158.     $.parameter = $.parameter || {};
  159.  
  160.     $.parameter.var = Number($.getParameters['Storage Variable']);
  161.  
  162.     //-----------------------------------------------------------------------------
  163.     // UPDATE CLASS NAMES
  164.     //-----------------------------------------------------------------------------
  165.  
  166.     $.Scene_Map_updateScene = Scene_Map.prototype.updateScene;
  167.     Scene_Map.prototype.updateScene = function() {
  168.         $.Scene_Map_updateScene.call(this);
  169.         if (!SceneManager.isSceneChanging()) {
  170.             this.updateClassNames();
  171.         }
  172.     };
  173.  
  174.     Scene_Map.prototype.updateClassNames = function() {
  175.         if (Array.isArray($gameVariables.value($.parameter.var)) == false) {
  176.             let emptyArray = [];
  177.             $gameVariables.setValue($.parameter.var, emptyArray);
  178.         }
  179.         var storageVar = $gameVariables.value($.parameter.var);
  180.         var customClassNames = storageVar;
  181.         if (customClassNames.length > 0) {
  182.             customClassNames.forEach((renamedClass) => {
  183.                 let actorId = renamedClass[0];
  184.                 let customName = renamedClass[1];
  185.                 let currentClass = $gameActors.actor(actorId).currentClass();
  186.                 if (currentClass.name !== customName) {
  187.                     currentClass.name = customName;
  188.                 }
  189.             });
  190.         }
  191.     };
  192.  
  193.     //-----------------------------------------------------------------------------
  194.     // Scene_ClassName
  195.     //
  196.     // The scene class of the class name input screen.
  197.     //-----------------------------------------------------------------------------
  198.  
  199.     function Scene_ClassName() {
  200.         this.initialize.apply(this, arguments);
  201.     }
  202.  
  203.     Scene_ClassName.prototype = Object.create(Scene_MenuBase.prototype);
  204.     Scene_ClassName.prototype.constructor = Scene_ClassName;
  205.  
  206.     Scene_ClassName.prototype.initialize = function() {
  207.         Scene_MenuBase.prototype.initialize.call(this);
  208.     };
  209.  
  210.     Scene_ClassName.prototype.prepare = function(actorId, maxLength) {
  211.         this._actorId = actorId;
  212.         this._maxLength = maxLength;
  213.     };
  214.  
  215.     Scene_ClassName.prototype.create = function() {
  216.         Scene_MenuBase.prototype.create.call(this);
  217.         this._actor = $gameActors.actor(this._actorId);
  218.         this.createEditWindow();
  219.         this.createInputWindow();
  220.     };
  221.  
  222.     Scene_ClassName.prototype.start = function() {
  223.         Scene_MenuBase.prototype.start.call(this);
  224.         this._editWindow.refresh();
  225.     };
  226.  
  227.     Scene_ClassName.prototype.createEditWindow = function() {
  228.         this._editWindow = new Window_ClassNameEdit(this._actor, this._maxLength);
  229.         this.addWindow(this._editWindow);
  230.     };
  231.  
  232.     Scene_ClassName.prototype.createInputWindow = function() {
  233.         this._inputWindow = new Window_NameInput(this._editWindow);
  234.         this._inputWindow.setHandler('ok', this.onInputOk.bind(this));
  235.         this.addWindow(this._inputWindow);
  236.     };
  237.  
  238.     Scene_ClassName.prototype.onInputOk = function() {
  239.         var storageVar = $gameVariables.value($.parameter.var);
  240.         var customClassNames = storageVar;
  241.         var needToOverwrite = false;
  242.         if (customClassNames.length > 0) {
  243.             customClassNames.forEach((item) => {
  244.                 if (item[0] == this._actorId) {
  245.                     // Overwrite existing custom class name for this actor.
  246.                     let index = customClassNames.indexOf(item);
  247.                     this.deleteClassName(index);
  248.                     needToOverwrite = true;
  249.                     this.popScene();
  250.                 }
  251.             });
  252.         }
  253.         if (needToOverwrite == false) {
  254.             // Save new custom class name for this actor.
  255.             this.saveNewClassName(customClassNames);
  256.             this.popScene();
  257.         }
  258.     };
  259.  
  260.     Scene_ClassName.prototype.deleteClassName = function(index) {
  261.         var storageVar = $gameVariables.value($.parameter.var);
  262.         var customClassNames = storageVar;
  263.         let splicedArray = customClassNames.splice(index, 1);
  264.         customClassNames = splicedArray;
  265.         this.saveNewClassName(customClassNames);
  266.     };
  267.  
  268.     Scene_ClassName.prototype.saveNewClassName = function(customClassNames) {
  269.         let renamedClass = [];
  270.         renamedClass.push(this._actorId);
  271.         renamedClass.push(this._editWindow.name());
  272.         customClassNames.push(renamedClass);
  273.         $gameVariables.setValue($.parameter.var, customClassNames);
  274.     };
  275.  
  276.     //-----------------------------------------------------------------------------
  277.     // Window_ClassNameEdit
  278.     //
  279.     // The window for editing a class's name on the class name input screen.
  280.     //-----------------------------------------------------------------------------
  281.  
  282.     function Window_ClassNameEdit() {
  283.         this.initialize.apply(this, arguments);
  284.     }
  285.  
  286.     Window_ClassNameEdit.prototype = Object.create(Window_Base.prototype);
  287.     Window_ClassNameEdit.prototype.constructor = Window_ClassNameEdit;
  288.  
  289.     Window_ClassNameEdit.prototype.initialize = function(actor, maxLength) {
  290.         var width = this.windowWidth();
  291.         var height = this.windowHeight();
  292.         var x = (Graphics.boxWidth - width) / 2;
  293.         var y = (Graphics.boxHeight - (height + this.fittingHeight(9) + 8)) / 2;
  294.         Window_Base.prototype.initialize.call(this, x, y, width, height);
  295.         this._actor = actor;
  296.         this._name = actor.currentClass().name.slice(0, this._maxLength);
  297.         this._index = this._name.length;
  298.         this._maxLength = maxLength;
  299.         this._defaultName = this._name;
  300.         this.deactivate();
  301.         this.refresh();
  302.     };
  303.  
  304.     Window_ClassNameEdit.prototype.windowWidth = function() {
  305.         return 480;
  306.     };
  307.  
  308.     Window_ClassNameEdit.prototype.windowHeight = function() {
  309.         return this.fittingHeight(4);
  310.     };
  311.  
  312.     Window_ClassNameEdit.prototype.name = function() {
  313.         return this._name;
  314.     };
  315.  
  316.     Window_ClassNameEdit.prototype.restoreDefault = function() {
  317.         this._name = this._defaultName;
  318.         this._index = this._name.length;
  319.         this.refresh();
  320.         return this._name.length > 0;
  321.     };
  322.  
  323.     Window_ClassNameEdit.prototype.add = function(ch) {
  324.         if (this._index < this._maxLength) {
  325.             this._name += ch;
  326.             this._index++;
  327.             this.refresh();
  328.             return true;
  329.         } else {
  330.             return false;
  331.         }
  332.     };
  333.  
  334.     Window_ClassNameEdit.prototype.back = function() {
  335.         if (this._index > 0) {
  336.             this._index--;
  337.             this._name = this._name.slice(0, this._index);
  338.             this.refresh();
  339.             return true;
  340.         } else {
  341.             return false;
  342.         }
  343.     };
  344.  
  345.     Window_ClassNameEdit.prototype.charWidth = function() {
  346.         var text = $gameSystem.isJapanese() ? '\uff21' : 'A';
  347.         return this.textWidth(text);
  348.     };
  349.  
  350.     Window_ClassNameEdit.prototype.left = function() {
  351.         var nameCenter = this.contentsWidth() / 2;
  352.         var nameWidth = (this._maxLength + 1) * this.charWidth();
  353.         return Math.min(nameCenter - nameWidth / 2, this.contentsWidth() - nameWidth);
  354.     };
  355.  
  356.     Window_ClassNameEdit.prototype.itemRect = function(index) {
  357.         return {
  358.             x: this.left() + index * this.charWidth(),
  359.             y: 54,
  360.             width: this.charWidth(),
  361.             height: this.lineHeight()
  362.         };
  363.     };
  364.  
  365.     Window_ClassNameEdit.prototype.underlineRect = function(index) {
  366.         var rect = this.itemRect(index);
  367.         rect.x++;
  368.         rect.y += rect.height - 4;
  369.         rect.width -= 2;
  370.         rect.height = 2;
  371.         return rect;
  372.     };
  373.  
  374.     Window_ClassNameEdit.prototype.underlineColor = function() {
  375.         return this.normalColor();
  376.     };
  377.  
  378.     Window_ClassNameEdit.prototype.drawUnderline = function(index) {
  379.         var rect = this.underlineRect(index);
  380.         var color = this.underlineColor();
  381.         this.contents.paintOpacity = 48;
  382.         this.contents.fillRect(rect.x, rect.y, rect.width, rect.height, color);
  383.         this.contents.paintOpacity = 255;
  384.     };
  385.  
  386.     Window_ClassNameEdit.prototype.drawChar = function(index) {
  387.         var rect = this.itemRect(index);
  388.         this.resetTextColor();
  389.         this.drawText(this._name[index] || '', rect.x, rect.y);
  390.     };
  391.  
  392.     Window_ClassNameEdit.prototype.refresh = function() {
  393.         this.contents.clear();
  394.         for (var i = 0; i < this._maxLength; i++) {
  395.             this.drawUnderline(i);
  396.         }
  397.         for (var j = 0; j < this._name.length; j++) {
  398.             this.drawChar(j);
  399.         }
  400.         var rect = this.itemRect(this._index);
  401.         this.setCursorRect(rect.x, rect.y, rect.width, rect.height);
  402.     };
  403.  
  404.     //-----------------------------------------------------------------------------
  405.     // PLUGIN COMMAND
  406.     //-----------------------------------------------------------------------------
  407.  
  408.     // Plugin Command: "nameclass actorId maxLength"
  409.  
  410.     $.Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  411.     Game_Interpreter.prototype.pluginCommand = function(command, args) {
  412.         $.Game_Interpreter_pluginCommand.call(this, command, args);
  413.         if (command.toLowerCase() === "nameclass") {
  414.             this.classNameInput(args);
  415.         }
  416.     };
  417.  
  418.     Game_Interpreter.prototype.classNameInput = function(args) {
  419.         const actorId = parseInt(args[0]);
  420.         const maxLength = parseInt(args[1]);
  421.         SceneManager.push(Scene_ClassName);
  422.         SceneManager.prepareNextScene(actorId, maxLength);
  423.     };
  424.  
  425.     //-----------------------------------------------------------------------------
  426.     // SCRIPT CALL
  427.     //-----------------------------------------------------------------------------
  428.  
  429.     // Script Call: "Rata.CNI.nameClass(actorId, maxLength);"
  430.  
  431.     $.nameClass = function(actorId, maxLength) {
  432.         SceneManager.push(Scene_ClassName);
  433.         SceneManager.prepareNextScene(actorId, maxLength);
  434.     };
  435.  
  436. })(Rata.CNI);
  437.  
  438. //=============================================================================
  439. // END OF FILE
  440. //=============================================================================
Advertisement
Add Comment
Please, Sign In to add comment