Source: Window_ItemList.js

Window_ItemList.js

//-----------------------------------------------------------------------------
// Window_ItemList
//
// The window for selecting an item on the item screen.
/**
 * The window for selecting an item on the item screen.
 *
 * @class
 * @extends Window_Selectable
 */
function Window_ItemList() {
    this.initialize(...arguments);
}

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

Window_ItemList.prototype.initialize = function(rect) {
    Window_Selectable.prototype.initialize.call(this, rect);
    this._category = "none";
    this._data = [];
};

/**
 * Sets the category of items in the list. Default valid values are item/weapon/armor/keyItem
 *
 * @param {string} category - The new category for the window
 */
Window_ItemList.prototype.setCategory = function(category) {
    if (this._category !== category) {
        this._category = category;
        this.refresh();
        this.scrollTo(0, 0);
    }
};

/**
 * Get the maximum number of columns the window should support
 *
 * @return {number} The maximum amount of columns to show in the window
 */
Window_ItemList.prototype.maxCols = function() {
    return 2;
};

/**
 * Get the spacing between columns
 *
 * @return {number} The spacing between columns
 */
Window_ItemList.prototype.colSpacing = function() {
    return 16;
};

/**
 * Get maximum number of items in the window
 *
 * @return {number} The maximum number of items in the window
 */
Window_ItemList.prototype.maxItems = function() {
    return this._data ? this._data.length : 1;
};

/**
 * Get the item object at the currently selected index
 *
 * @return {Object} The item at the currently selected index
 */
Window_ItemList.prototype.item = function() {
    return this.itemAt(this.index());
};

/**
 * Get the item object at the given index
 *
 * @param {number} index - The index to check
 * @return {Object} The item at the given index
 */
Window_ItemList.prototype.itemAt = function(index) {
    return this._data && index >= 0 ? this._data[index] : null;
};

/**
 * Check if the currently selected item is enabled
 *
 * @return {boolean} True if the currently selected item is enabled
 */
Window_ItemList.prototype.isCurrentItemEnabled = function() {
    return this.isEnabled(this.item());
};

/**
 * Check if the item should be included in the window
 *
 * @param {Object} item - The item object to check
 * @return {boolean} True if the item should be included in the window
 */
Window_ItemList.prototype.includes = function(item) {
    switch (this._category) {
        case "item":
            return DataManager.isItem(item) && item.itypeId === 1;
        case "weapon":
            return DataManager.isWeapon(item);
        case "armor":
            return DataManager.isArmor(item);
        case "keyItem":
            return DataManager.isItem(item) && item.itypeId === 2;
        default:
            return false;
    }
};

/**
 * Check if items should show numbers
 *
 * @return {boolean} True if items should show numbers
 */
Window_ItemList.prototype.needsNumber = function() {
    if (this._category === "keyItem") {
        return $dataSystem.optKeyItemsNumber;
    } else {
        return true;
    }
};

/**
 * Check if the given item should be enabled
 *
 * @param {Object} item - The item object to check
 * @return {boolean} True if item is enabled
 */
Window_ItemList.prototype.isEnabled = function(item) {
    return $gameParty.canUse(item);
};

/**
 * Create the list of items to display in the window
 */
Window_ItemList.prototype.makeItemList = function() {
    this._data = $gameParty.allItems().filter(item => this.includes(item));
    if (this.includes(null)) {
        this._data.push(null);
    }
};

/**
 * Select the last item, if remembered
 */
Window_ItemList.prototype.selectLast = function() {
    const index = this._data.indexOf($gameParty.lastItem());
    this.forceSelect(index >= 0 ? index : 0);
};

/**
 * Draw the item at the given index
 *
 * @param {number} index - The index of the item to draw
 */
Window_ItemList.prototype.drawItem = function(index) {
    const item = this.itemAt(index);
    if (item) {
        const numberWidth = this.numberWidth();
        const rect = this.itemLineRect(index);
        this.changePaintOpacity(this.isEnabled(item));
        this.drawItemName(item, rect.x, rect.y, rect.width - numberWidth);
        this.drawItemNumber(item, rect.x, rect.y, rect.width);
        this.changePaintOpacity(1);
    }
};

/**
 * Get the width of the number for an item
 * 
 * @return {number} The width of an item's number area
 */
Window_ItemList.prototype.numberWidth = function() {
    return this.textWidth("000");
};

/**
 * Draws an item's number (how many the party has)
 *
 * @param {Object} item - The item object
 * @param {number} x - The x coordinate to draw the number
 * @param {number} y - The y coordinate to draw the number
 * @param {number} width - The width available for the number
 */
Window_ItemList.prototype.drawItemNumber = function(item, x, y, width) {
    if (this.needsNumber()) {
        this.drawText(":", x, y, width - this.textWidth("00"), "right");
        this.drawText($gameParty.numItems(item), x, y, width, "right");
    }
};

/**
 * Update the help window
 */
Window_ItemList.prototype.updateHelp = function() {
    this.setHelpWindowItem(this.item());
};

Window_ItemList.prototype.refresh = function() {
    this.makeItemList();
    Window_Selectable.prototype.refresh.call(this);
};