Source: Sprite_Name.js

Sprite_Name.js

//-----------------------------------------------------------------------------
// Sprite_Name
//
// The sprite for displaying a status gauge.
/**
 * The sprite for displaying a status gauge.
 *
 * @class
 * @extends Sprite
 */
function Sprite_Name() {
    this.initialize(...arguments);
}

Sprite_Name.prototype = Object.create(Sprite.prototype);
Sprite_Name.prototype.constructor = Sprite_Name;

Sprite_Name.prototype.initialize = function() {
    Sprite.prototype.initialize.call(this);
    this.initMembers();
    this.createBitmap();
};

/**
 * Initialize the sprite's variables
 */
Sprite_Name.prototype.initMembers = function() {
    this._battler = null;
    this._name = "";
    this._textColor = "";
};

Sprite_Name.prototype.destroy = function(options) {
    this.bitmap.destroy();
    Sprite.prototype.destroy.call(this, options);
};

/**
 * Creates the sprite's bitmap
 */
Sprite_Name.prototype.createBitmap = function() {
    const width = this.bitmapWidth();
    const height = this.bitmapHeight();
    this.bitmap = new Bitmap(width, height);
};

/**
 * Get the width of the sprite's bitmap
 *
 * @return {number} The bitmap width
 */
Sprite_Name.prototype.bitmapWidth = function() {
    return 128;
};

/**
 * Get the height of the sprite's bitmap
 *
 * @return {number} The bitmap height
 */
Sprite_Name.prototype.bitmapHeight = function() {
    return 24;
};

/**
 * Get the sprite's font face
 *
 * @return {string} The font face
 */
Sprite_Name.prototype.fontFace = function() {
    return $gameSystem.mainFontFace();
};

/**
 * Get the sprite's font size
 *
 * @return {number} The font size
 */
Sprite_Name.prototype.fontSize = function() {
    return $gameSystem.mainFontSize();
};

/**
 * Sets the sprite up
 *
 * @param {Game_Battler} battler - The battler to track for the sprite
 */
Sprite_Name.prototype.setup = function(battler) {
    this._battler = battler;
    this.updateBitmap();
};

Sprite_Name.prototype.update = function() {
    Sprite.prototype.update.call(this);
    this.updateBitmap();
};

/**
 * Updates the bitmap
 */
Sprite_Name.prototype.updateBitmap = function() {
    const name = this.name();
    const color = this.textColor();
    if (name !== this._name || color !== this._textColor) {
        this._name = name;
        this._textColor = color;
        this.redraw();
    }
};

/**
 * The name to show
 *
 * @return {string} The name
 */
Sprite_Name.prototype.name = function() {
    return this._battler ? this._battler.name() : "";
};

/**
 * The color to make the text
 *
 * @return {string} The text color
 */
Sprite_Name.prototype.textColor = function() {
    return ColorManager.hpColor(this._battler);
};

/**
 * The text outline color
 *
 * @return {string} The outline color
 */
Sprite_Name.prototype.outlineColor = function() {
    return ColorManager.outlineColor();
};

/**
 * The text outline width
 *
 * @return {number} The width
 */
Sprite_Name.prototype.outlineWidth = function() {
    return 3;
};

/**
 * Redraws the bitmap
 */
Sprite_Name.prototype.redraw = function() {
    const name = this.name();
    const width = this.bitmapWidth();
    const height = this.bitmapHeight();
    this.setupFont();
    this.bitmap.clear();
    this.bitmap.drawText(name, 0, 0, width, height, "left");
};

/**
 * Sets up the bitmap's font
 */
Sprite_Name.prototype.setupFont = function() {
    this.bitmap.fontFace = this.fontFace();
    this.bitmap.fontSize = this.fontSize();
    this.bitmap.textColor = this.textColor();
    this.bitmap.outlineColor = this.outlineColor();
    this.bitmap.outlineWidth = this.outlineWidth();
};