Source: Window_SavefileList.js

Window_SavefileList.js

//-----------------------------------------------------------------------------
// Window_SavefileList
//
// The window for selecting a save file on the save and load screens.
/**
 * The window for selecting a save file on the save and load screens.
 *
 * @class
 * @extends Window_Selectable
 */
function Window_SavefileList() {
    this.initialize(...arguments);
}

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

Window_SavefileList.prototype.initialize = function(rect) {
    Window_Selectable.prototype.initialize.call(this, rect);
    this.activate();
    this._mode = null;
    this._autosave = false;
};

/**
 * Sets the mode of the window
 *
 * @param {string} mode - The mode for saving/loading
 * @param {boolean} autosave - If autosave is needed or not
 */
Window_SavefileList.prototype.setMode = function(mode, autosave) {
    this._mode = mode;
    this._autosave = autosave;
    this.refresh();
};

/**
 * Get the maximum amount of items to appear in the window
 *
 * @return {number} The maximum amount of items to appear in the window
 */
Window_SavefileList.prototype.maxItems = function() {
    return DataManager.maxSavefiles() - (this._autosave ? 0 : 1);
};

/**
 * Get the amount of rows visible at one time
 *
 * @return {number} The amount of visible rows at one time
 */
Window_SavefileList.prototype.numVisibleRows = function() {
    return 5;
};

/**
 * Get the height of an item
 *
 * @return {number} The height of an item
 */
Window_SavefileList.prototype.itemHeight = function() {
    return Math.floor(this.innerHeight / this.numVisibleRows());
};

/**
 * Draws the item at the given index
 *
 * @param {number} index - The index to draw
 */
Window_SavefileList.prototype.drawItem = function(index) {
    const savefileId = this.indexToSavefileId(index);
    const info = DataManager.savefileInfo(savefileId);
    const rect = this.itemRectWithPadding(index);
    this.resetTextColor();
    this.changePaintOpacity(this.isEnabled(savefileId));
    this.drawTitle(savefileId, rect.x, rect.y + 4);
    if (info) {
        this.drawContents(info, rect);
    }
};

/**
 * Converts the index to the save file's id
 *
 * @param {number} index - The index to convert
 * @return {number} The save file's id
 */
Window_SavefileList.prototype.indexToSavefileId = function(index) {
    return index + (this._autosave ? 0 : 1);
};

/**
 * Converts a save file id to the save file's index
 *
 * @param {number} savefileId - The save file id to convert
 * @return {number} The save file's index
 */
Window_SavefileList.prototype.savefileIdToIndex = function(savefileId) {
    return savefileId - (this._autosave ? 0 : 1);
};

/**
 * Check if a given save file is enabled
 *
 * @param {number} savefileId - The save file id to check
 * @return {boolean} True if the save file is enabled
 */
Window_SavefileList.prototype.isEnabled = function(savefileId) {
    if (this._mode === "save") {
        return savefileId > 0;
    } else {
        return !!DataManager.savefileInfo(savefileId);
    }
};

/**
 * Get the currently selected save file's id
 *
 * @return {number} The save file's index
 */
Window_SavefileList.prototype.savefileId = function() {
    return this.indexToSavefileId(this.index());
};

/**
 * Select a save file by save file id
 *
 * @param {number} savefileId - The save file id to select
 */
Window_SavefileList.prototype.selectSavefile = function(savefileId) {
    const index = Math.max(0, this.savefileIdToIndex(savefileId));
    this.select(index);
    this.setTopRow(index - 2);
};

/**
 * Draw a save file title
 *
 * @param {number} savefileId - The save file id
 * @param {number} x - The x coordinate to draw the save file title
 * @param {number} y - The y coordinate to draw the save file title
 */
Window_SavefileList.prototype.drawTitle = function(savefileId, x, y) {
    if (savefileId === 0) {
        this.drawText(TextManager.autosave, x, y, 180);
    } else {
        this.drawText(TextManager.file + " " + savefileId, x, y, 180);
    }
};

/**
 * Draw save file contents
 *
 * @param {Object} info - The save file's info
 * @param {Rectangle} rect - The rectangle where the contents will be drawn
 */
Window_SavefileList.prototype.drawContents = function(info, rect) {
    const bottom = rect.y + rect.height;
    if (rect.width >= 420) {
        this.drawPartyCharacters(info, rect.x + 220, bottom - 8);
    }
    const lineHeight = this.lineHeight();
    const y2 = bottom - lineHeight - 4;
    if (y2 >= lineHeight) {
        this.drawPlaytime(info, rect.x, y2, rect.width);
    }
};

/**
 * Draw save file party characters
 *
 * @param {Object} info - The save file's info
 * @param {number} x - The x coordinate to draw the characters
 * @param {number} y - The y coordinate to draw the characters
 */
Window_SavefileList.prototype.drawPartyCharacters = function(info, x, y) {
    if (info.characters) {
        let characterX = x;
        for (const data of info.characters) {
            this.drawCharacter(data[0], data[1], characterX, y);
            characterX += 48;
        }
    }
};

/**
 * Draw save file playtime
 *
 * @param {Object} info - The save file's info
 * @param {number} x - The x coordinate to draw the playtime
 * @param {number} y - The y coordinate to draw the playtime
 * @param {number} width - The width available to draw the playtime
 */
Window_SavefileList.prototype.drawPlaytime = function(info, x, y, width) {
    if (info.playtime) {
        this.drawText(info.playtime, x, y, width, "right");
    }
};

/**
 * Play the OK sound. In Window_SavefileList, this does nothing.
 */
Window_SavefileList.prototype.playOkSound = function() {
    //
};