Source: Window_NameEdit.js

Window_NameEdit.js

//-----------------------------------------------------------------------------
// Window_NameEdit
//
// The window for editing an actor's name on the name input screen.
/**
 * The window for editing an actor's name on the name input screen.
 *
 * @class
 * @extends Window_StatusBase
 */
function Window_NameEdit() {
    this.initialize(...arguments);
}

Window_NameEdit.prototype = Object.create(Window_StatusBase.prototype);
Window_NameEdit.prototype.constructor = Window_NameEdit;

Window_NameEdit.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();
};

/**
 * Set up the window
 *
 * @param {Game_Actor} actor - The game actor object
 * @param {number} maxLength - The max length of the name
 */
Window_NameEdit.prototype.setup = function(actor, maxLength) {
    this._actor = actor;
    this._maxLength = maxLength;
    this._name = actor.name().slice(0, this._maxLength);
    this._index = this._name.length;
    this._defaultName = this._name;
    ImageManager.loadFace(actor.faceName());
};

/**
 * Get the name in the window
 *
 * @return {string} The name in the window
 */
Window_NameEdit.prototype.name = function() {
    return this._name;
};

/**
 * Restore the default name
 *
 * @return {boolean} True if the name was restored
 */
Window_NameEdit.prototype.restoreDefault = function() {
    this._name = this._defaultName;
    this._index = this._name.length;
    this.refresh();
    return this._name.length > 0;
};

/**
 * Add a character on to the name
 *
 * @param {string} ch - The character to add
 * @return {boolean} True if add succeeded
 */
Window_NameEdit.prototype.add = function(ch) {
    if (this._index < this._maxLength) {
        this._name += ch;
        this._index++;
        this.refresh();
        return true;
    } else {
        return false;
    }
};

/**
 * Removes the last character from the name
 *
 * @return {boolean} True if back succeeded
 */
Window_NameEdit.prototype.back = function() {
    if (this._index > 0) {
        this._index--;
        this._name = this._name.slice(0, this._index);
        this.refresh();
        return true;
    } else {
        return false;
    }
};

/**
 * Get the width of the face image
 *
 * @return {number} Width of the face image
 */
Window_NameEdit.prototype.faceWidth = function() {
    return 144;
};

/**
 * Get the width of a character
 *
 * @return {number} Width of a character
 */
Window_NameEdit.prototype.charWidth = function() {
    const text = $gameSystem.isJapanese() ? "\uff21" : "A";
    return this.textWidth(text);
};

/**
 * Get the left of the name edit
 *
 * @return {number} Left of the name edit area
 */
Window_NameEdit.prototype.left = function() {
    const nameCenter = (this.innerWidth + this.faceWidth()) / 2;
    const nameWidth = (this._maxLength + 1) * this.charWidth();
    return Math.min(nameCenter - nameWidth / 2, this.innerWidth - nameWidth);
};

/**
 * Get the rect of the item at the given index
 *
 * @param {number} index - The index to get the rect for
 * @return {Rectangle} Rectangle object representing the name edit area of the index
 */
Window_NameEdit.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);
};

/**
 * Get the underline rect of the item at the given index
 *
 * @param {number} index - The index to get the rect for
 * @return {Rectangle} Rectangle object representing the underline of the index
 */
Window_NameEdit.prototype.underlineRect = function(index) {
    const rect = this.itemRect(index);
    rect.x++;
    rect.y += rect.height - 4;
    rect.width -= 2;
    rect.height = 2;
    return rect;
};

/**
 * Get the color of the underline rect
 *
 * @return {string} The color of the underline
 */
Window_NameEdit.prototype.underlineColor = function() {
    return ColorManager.normalColor();
};

/**
 * Draws the underline at the given index
 *
 * @param {number} index - The index to draw the underline for
 */
Window_NameEdit.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;
};

/**
 * Draws the character at the given index
 *
 * @param {number} index - The index to draw the character for
 */
Window_NameEdit.prototype.drawChar = function(index) {
    const rect = this.itemRect(index);
    this.resetTextColor();
    this.drawText(this._name[index] || "", rect.x, rect.y);
};

/**
 * Refreshes the window
 */
Window_NameEdit.prototype.refresh = function() {
    this.contents.clear();
    this.drawActorFace(this._actor, 0, 0);
    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);
};