Source: Window_Command.js

Window_Command.js

//-----------------------------------------------------------------------------
// Window_Command
//
// The superclass of windows for selecting a command.
/**
 * The superclass of windows for selecting a command.
 *
 * @class
 * @extends Window_Selectable
 */
function Window_Command() {
    this.initialize(...arguments);
}

Window_Command.prototype = Object.create(Window_Selectable.prototype);
Window_Command.prototype.constructor = Window_Command;

Window_Command.prototype.initialize = function(rect) {
    Window_Selectable.prototype.initialize.call(this, rect);
    this.refresh();
    this.select(0);
    this.activate();
};

/**
 * Get the amount of items in the window
 *
 * @return {number} Amount of items
 */
Window_Command.prototype.maxItems = function() {
    return this._list.length;
};

/**
 * Clears the command list
 */
Window_Command.prototype.clearCommandList = function() {
    this._list = [];
};

/**
 * Makes the command list
 */
Window_Command.prototype.makeCommandList = function() {
    //
};

// prettier-ignore
/**
 * Adds a command to the command list
 *
 * @param {string} name - The name of the command
 * @param {string} symbol - The symbol of the command
 * @param {boolean} [enabled=true] - If the command is enabled
 * @param {*} [ext=null] - Extra data for the command
 */
Window_Command.prototype.addCommand = function(
    name, symbol, enabled = true, ext = null
) {
    this._list.push({ name: name, symbol: symbol, enabled: enabled, ext: ext });
};

/**
 * Gets the name of a command at a given index
 *
 * @param {number} index - The index of the command
 * @return {string} The name of the command
 */
Window_Command.prototype.commandName = function(index) {
    return this._list[index].name;
};

/**
 * Gets the symbol of a command at a given index
 *
 * @param {number} index - The index of the command
 * @return {string} The symbol of the command
 */
Window_Command.prototype.commandSymbol = function(index) {
    return this._list[index].symbol;
};

/**
 * Check if the command at a given index is enabled
 *
 * @param {number} index - The index of the command
 * @return {boolean} True if the command is enabled
 */
Window_Command.prototype.isCommandEnabled = function(index) {
    return this._list[index].enabled;
};

/**
 * Gets the currently selected command data
 *
 * @return {Object} The current command data
 */
Window_Command.prototype.currentData = function() {
    return this.index() >= 0 ? this._list[this.index()] : null;
};

/**
 * Check if the currently selected command is enabled
 *
 * @return {boolean} True if the current item is enabled
 */
Window_Command.prototype.isCurrentItemEnabled = function() {
    return this.currentData() ? this.currentData().enabled : false;
};

/**
 * Gets the currently selected command's symbol
 *
 * @return {string} The current command's symbol
 */
Window_Command.prototype.currentSymbol = function() {
    return this.currentData() ? this.currentData().symbol : null;
};

/**
 * Gets the currently selected command's extra data
 *
 * @return {*} The current command's extra data
 */
Window_Command.prototype.currentExt = function() {
    return this.currentData() ? this.currentData().ext : null;
};

/**
 * Find a command by symbol
 *
 * @param {string} symbol - The symbol to search for
 * @return {Object} The command that was found
 */
Window_Command.prototype.findSymbol = function(symbol) {
    return this._list.findIndex(item => item.symbol === symbol);
};

/**
 * Select a command by symbol
 *
 * @param {string} symbol - The symbol of the command to select
 */
Window_Command.prototype.selectSymbol = function(symbol) {
    const index = this.findSymbol(symbol);
    if (index >= 0) {
        this.forceSelect(index);
    } else {
        this.forceSelect(0);
    }
};

/**
 * Find a command by ext
 *
 * @param {*} ext - The extra data to search for
 * @return {Object} The command that was found
 */
Window_Command.prototype.findExt = function(ext) {
    return this._list.findIndex(item => item.ext === ext);
};

/**
 * Selects a command by ext
 *
 * @param {*} ext - The ext to search for
 */
Window_Command.prototype.selectExt = function(ext) {
    const index = this.findExt(ext);
    if (index >= 0) {
        this.forceSelect(index);
    } else {
        this.forceSelect(0);
    }
};

/**
 * Draws the item at the given index
 *
 * @param {number} index - The index to draw
 */
Window_Command.prototype.drawItem = function(index) {
    const rect = this.itemLineRect(index);
    const align = this.itemTextAlign();
    this.resetTextColor();
    this.changePaintOpacity(this.isCommandEnabled(index));
    this.drawText(this.commandName(index), rect.x, rect.y, rect.width, align);
};

/**
 * Get the alignment of text
 *
 * @return {string} The alignment of the text
 */
Window_Command.prototype.itemTextAlign = function() {
    return "center";
};

/**
 * Check if OK is enabled
 *
 * @return {boolean} True if OK is enabled
 */
Window_Command.prototype.isOkEnabled = function() {
    return true;
};

/**
 * Calls the OK handler
 */
Window_Command.prototype.callOkHandler = function() {
    const symbol = this.currentSymbol();
    if (this.isHandled(symbol)) {
        this.callHandler(symbol);
    } else if (this.isHandled("ok")) {
        Window_Selectable.prototype.callOkHandler.call(this);
    } else {
        this.activate();
    }
};

Window_Command.prototype.refresh = function() {
    this.clearCommandList();
    this.makeCommandList();
    Window_Selectable.prototype.refresh.call(this);
};