Source: Window_Options.js

Window_Options.js

//-----------------------------------------------------------------------------
// Window_Options
//
// The window for changing various settings on the options screen.
/**
 * The window for changing various settings on the options screen.
 *
 * @class
 * @extends Window_Command
 */
function Window_Options() {
    this.initialize(...arguments);
}

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

Window_Options.prototype.initialize = function(rect) {
    Window_Command.prototype.initialize.call(this, rect);
};

/**
 * Make the list of commands to display in the window
 */
Window_Options.prototype.makeCommandList = function() {
    this.addGeneralOptions();
    this.addVolumeOptions();
};

/**
 * Add the general options (always dash, command remember, touch UI)
 */
Window_Options.prototype.addGeneralOptions = function() {
    this.addCommand(TextManager.alwaysDash, "alwaysDash");
    this.addCommand(TextManager.commandRemember, "commandRemember");
    this.addCommand(TextManager.touchUI, "touchUI");
};

/**
 * Add the volume options
 */
Window_Options.prototype.addVolumeOptions = function() {
    this.addCommand(TextManager.bgmVolume, "bgmVolume");
    this.addCommand(TextManager.bgsVolume, "bgsVolume");
    this.addCommand(TextManager.meVolume, "meVolume");
    this.addCommand(TextManager.seVolume, "seVolume");
};

/**
 * Draw the item at the given index
 *
 * @param {number} index - The index to draw
 */
Window_Options.prototype.drawItem = function(index) {
    const title = this.commandName(index);
    const status = this.statusText(index);
    const rect = this.itemLineRect(index);
    const statusWidth = this.statusWidth();
    const titleWidth = rect.width - statusWidth;
    this.resetTextColor();
    this.changePaintOpacity(this.isCommandEnabled(index));
    this.drawText(title, rect.x, rect.y, titleWidth, "left");
    this.drawText(status, rect.x + titleWidth, rect.y, statusWidth, "right");
};

/**
 * Get the width of the option's status
 *
 * @return {number} Width of the status area
 */
Window_Options.prototype.statusWidth = function() {
    return 120;
};

/**
 * Get the status text of the item at the given index
 *
 * @param {number} index - The index to check
 * @return {string} The text representing the item's status
 */
Window_Options.prototype.statusText = function(index) {
    const symbol = this.commandSymbol(index);
    const value = this.getConfigValue(symbol);
    if (this.isVolumeSymbol(symbol)) {
        return this.volumeStatusText(value);
    } else {
        return this.booleanStatusText(value);
    }
};

/**
 * Check if the given symbol is a volume symbol
 *
 * @param {string} symbol - The command symbol to check
 * @return {boolean} True if the symbol is a volume symbol
 */
Window_Options.prototype.isVolumeSymbol = function(symbol) {
    return symbol.includes("Volume");
};

/**
 * Convert the boolean value into status text
 *
 * @param {boolean} value - The boolean value to convert to text
 * @return {string} ON if true, OFF if false
 */
Window_Options.prototype.booleanStatusText = function(value) {
    return value ? "ON" : "OFF";
};

/**
 * Convert the volume value into status text
 *
 * @param {boolean} value - The volume value to convert to text
 * @return {string} Value as a percentage string
 */
Window_Options.prototype.volumeStatusText = function(value) {
    return value + "%";
};

/**
 * Processing for OK input
 */
Window_Options.prototype.processOk = function() {
    const index = this.index();
    const symbol = this.commandSymbol(index);
    if (this.isVolumeSymbol(symbol)) {
        this.changeVolume(symbol, true, true);
    } else {
        this.changeValue(symbol, !this.getConfigValue(symbol));
    }
};

/**
 * Handles when the cursor moves right
 */
Window_Options.prototype.cursorRight = function() {
    const index = this.index();
    const symbol = this.commandSymbol(index);
    if (this.isVolumeSymbol(symbol)) {
        this.changeVolume(symbol, true, false);
    } else {
        this.changeValue(symbol, true);
    }
};

/**
 * Handles when the cursor moves left
 */
Window_Options.prototype.cursorLeft = function() {
    const index = this.index();
    const symbol = this.commandSymbol(index);
    if (this.isVolumeSymbol(symbol)) {
        this.changeVolume(symbol, false, false);
    } else {
        this.changeValue(symbol, false);
    }
};

/**
 * Changes the volume
 *
 * @param {string} symbol - The volume command's symbol to change
 * @param {boolean} forward - If volume is increasing
 * @param {boolean} wrap - If the volume should wrap
 */
Window_Options.prototype.changeVolume = function(symbol, forward, wrap) {
    const lastValue = this.getConfigValue(symbol);
    const offset = this.volumeOffset();
    const value = lastValue + (forward ? offset : -offset);
    if (value > 100 && wrap) {
        this.changeValue(symbol, 0);
    } else {
        this.changeValue(symbol, value.clamp(0, 100));
    }
};

/**
 * Amount to change the volume by each time
 *
 * @return {number} Amount to change the volume by
 */
Window_Options.prototype.volumeOffset = function() {
    return 20;
};

/**
 * Changes the value of an option
 *
 * @param {string} symbol - The command's symbol to change
 * @param {*} value - The new value
 */
Window_Options.prototype.changeValue = function(symbol, value) {
    const lastValue = this.getConfigValue(symbol);
    if (lastValue !== value) {
        this.setConfigValue(symbol, value);
        this.redrawItem(this.findSymbol(symbol));
        this.playCursorSound();
    }
};

/**
 * Get the value of an option by symbol
 *
 * @param {string} symbol - The option's symbol
 * @return {*} The option's value
 */
Window_Options.prototype.getConfigValue = function(symbol) {
    return ConfigManager[symbol];
};

/**
 * Set the value of an option by symbol
 *
 * @param {string} symbol - The option's symbol
 * @param {*} volume - The option's new value
 */
Window_Options.prototype.setConfigValue = function(symbol, volume) {
    ConfigManager[symbol] = volume;
};