Ratatattat

Rata_ClassNameInput (RMMZ)

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