Source: Window_NameBox.js

Window_NameBox.js

//-----------------------------------------------------------------------------
// Window_NameBox
//
// The window for displaying a speaker name above the message window.
/**
 * The window for displaying a speaker name above the message window.
 *
 * @class
 * @extends Window_Base
 */
function Window_NameBox() {
    this.initialize(...arguments);
}

Window_NameBox.prototype = Object.create(Window_Base.prototype);
Window_NameBox.prototype.constructor = Window_NameBox;

Window_NameBox.prototype.initialize = function() {
    Window_Base.prototype.initialize.call(this, new Rectangle());
    this.openness = 0;
    this._name = "";
};

/**
 * Associates a message window with this window
 *
 * @param {Window_Message} messageWindow - The message window
 */
Window_NameBox.prototype.setMessageWindow = function(messageWindow) {
    this._messageWindow = messageWindow;
};

/**
 * Sets the name to show in the window
 *
 * @param {string} name - The name to display
 */
Window_NameBox.prototype.setName = function(name) {
    if (this._name !== name) {
        this._name = name;
        this.refresh();
    }
};

/**
 * Clears the name from the window
 */
Window_NameBox.prototype.clear = function() {
    this.setName("");
};

/**
 * Starts the window
 */
Window_NameBox.prototype.start = function() {
    this.updatePlacement();
    this.updateBackground();
    this.createContents();
    this.refresh();
};

/**
 * Updates the placement of the window
 */
Window_NameBox.prototype.updatePlacement = function() {
    this.width = this.windowWidth();
    this.height = this.windowHeight();
    const messageWindow = this._messageWindow;
    if ($gameMessage.isRTL()) {
        this.x = messageWindow.x + messageWindow.width - this.width;
    } else {
        this.x = messageWindow.x;
    }
    if (messageWindow.y > 0) {
        this.y = messageWindow.y - this.height;
    } else {
        this.y = messageWindow.y + messageWindow.height;
    }
};

/**
 * Updates the background type of the window
 */
Window_NameBox.prototype.updateBackground = function() {
    this.setBackgroundType($gameMessage.background());
};

/**
 * Gets the width of the window
 *
 * @return {number} The width of the window
 */
Window_NameBox.prototype.windowWidth = function() {
    if (this._name) {
        const textWidth = this.textSizeEx(this._name).width;
        const padding = this.padding + this.itemPadding();
        const width = Math.ceil(textWidth) + padding * 2;
        return Math.min(width, Graphics.boxWidth);
    } else {
        return 0;
    }
};

/**
 * Gets the height of the window
 *
 * @return {number} The height of the window
 */
Window_NameBox.prototype.windowHeight = function() {
    return this.fittingHeight(1);
};

/**
 * Refreshes the window
 */
Window_NameBox.prototype.refresh = function() {
    const rect = this.baseTextRect();
    this.contents.clear();
    this.drawTextEx(this._name, rect.x, rect.y, rect.width);
};